blob: 5485f18e663105dc757bff5464eb256e51c554f9 [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
9#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070010#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/shm.h>
13#include <linux/mman.h>
14#include <linux/pagemap.h>
15#include <linux/swap.h>
16#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/file.h>
20#include <linux/fs.h>
21#include <linux/personality.h>
22#include <linux/security.h>
23#include <linux/hugetlb.h>
24#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040025#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mount.h>
27#include <linux/mempolicy.h>
28#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070029#include <linux/mmu_notifier.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020030#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040031#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080032#include <linux/khugepaged.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053033#include <linux/uprobes.h>
Michel Lespinassed3737182012-12-11 16:01:38 -080034#include <linux/rbtree_augmented.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060035#include <linux/sched/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38#include <asm/cacheflush.h>
39#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020040#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jan Beulich42b77722008-07-23 21:27:10 -070042#include "internal.h"
43
Kirill Korotaev3a459752006-09-07 14:17:04 +040044#ifndef arch_mmap_check
45#define arch_mmap_check(addr, len, flags) (0)
46#endif
47
Martin Schwidefsky08e7d9b2008-02-04 22:29:16 -080048#ifndef arch_rebalance_pgtables
49#define arch_rebalance_pgtables(addr, len) (addr)
50#endif
51
Hugh Dickinse0da3822005-04-19 13:29:15 -070052static void unmap_region(struct mm_struct *mm,
53 struct vm_area_struct *vma, struct vm_area_struct *prev,
54 unsigned long start, unsigned long end);
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* description of effects of mapping type and prot in current implementation.
57 * this is due to the limited x86 page protection hardware. The expected
58 * behavior is in parens:
59 *
60 * map_type prot
61 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
62 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
63 * w: (no) no w: (no) no w: (yes) yes w: (no) no
64 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
65 *
66 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
67 * w: (no) no w: (no) no w: (copy) copy w: (no) no
68 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
69 *
70 */
71pgprot_t protection_map[16] = {
72 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
73 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
74};
75
Hugh Dickins804af2c2006-07-26 21:39:49 +010076pgprot_t vm_get_page_prot(unsigned long vm_flags)
77{
Dave Kleikampb845f312008-07-08 00:28:51 +100078 return __pgprot(pgprot_val(protection_map[vm_flags &
79 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
80 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +010081}
82EXPORT_SYMBOL(vm_get_page_prot);
83
Shaohua Li34679d72011-05-24 17:11:18 -070084int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
85int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070086int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -070087unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -070088unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
Shaohua Li34679d72011-05-24 17:11:18 -070089/*
90 * Make sure vm_committed_as in one cacheline and not cacheline shared with
91 * other variables. It can be updated by several CPUs frequently.
92 */
93struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
K. Y. Srinivasan997071b2012-11-15 14:34:42 -080096 * The global memory commitment made in the system can be a metric
97 * that can be used to drive ballooning decisions when Linux is hosted
98 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
99 * balancing memory across competing virtual machines that are hosted.
100 * Several metrics drive this policy engine including the guest reported
101 * memory commitment.
102 */
103unsigned long vm_memory_committed(void)
104{
105 return percpu_counter_read_positive(&vm_committed_as);
106}
107EXPORT_SYMBOL_GPL(vm_memory_committed);
108
109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * 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{
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700127 unsigned long free, allowed, reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
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 Liec8acf22013-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 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700168 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700171 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
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 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700182 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700185 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 allowed += total_swap_pages;
187
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700188 /*
189 * Don't let a single process grow so big a user can't recover
190 */
191 if (mm) {
192 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
193 allowed -= min(mm->total_vm / 32, reserve);
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700196 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700198error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 vm_unacct_memory(pages);
200
201 return -ENOMEM;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700205 * Requires inode->i_mapping->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207static void __remove_shared_vm_struct(struct vm_area_struct *vma,
208 struct file *file, struct address_space *mapping)
209{
210 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500211 atomic_inc(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (vma->vm_flags & VM_SHARED)
213 mapping->i_mmap_writable--;
214
215 flush_dcache_mmap_lock(mapping);
216 if (unlikely(vma->vm_flags & VM_NONLINEAR))
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700217 list_del_init(&vma->shared.nonlinear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 else
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700219 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 flush_dcache_mmap_unlock(mapping);
221}
222
223/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700224 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700225 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700227void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct file *file = vma->vm_file;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (file) {
232 struct address_space *mapping = file->f_mapping;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700233 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 __remove_shared_vm_struct(vma, file, mapping);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700235 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700237}
238
239/*
240 * Close a vm structure and free it, returning the next.
241 */
242static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
243{
244 struct vm_area_struct *next = vma->vm_next;
245
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700246 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (vma->vm_ops && vma->vm_ops->close)
248 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700249 if (vma->vm_file)
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700250 fput(vma->vm_file);
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;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700263 unsigned long min_brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800264 bool populate;
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. */
308 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
309 goto out;
310
311 /* Ok, looks good - let it rip. */
312 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
313 goto out;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315set_brk:
316 mm->brk = brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800317 populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
318 up_write(&mm->mmap_sem);
319 if (populate)
320 mm_populate(oldbrk, newbrk - oldbrk);
321 return brk;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out:
324 retval = mm->brk;
325 up_write(&mm->mmap_sem);
326 return retval;
327}
328
Michel Lespinassed3737182012-12-11 16:01:38 -0800329static long vma_compute_subtree_gap(struct vm_area_struct *vma)
330{
331 unsigned long max, subtree_gap;
332 max = vma->vm_start;
333 if (vma->vm_prev)
334 max -= vma->vm_prev->vm_end;
335 if (vma->vm_rb.rb_left) {
336 subtree_gap = rb_entry(vma->vm_rb.rb_left,
337 struct vm_area_struct, vm_rb)->rb_subtree_gap;
338 if (subtree_gap > max)
339 max = subtree_gap;
340 }
341 if (vma->vm_rb.rb_right) {
342 subtree_gap = rb_entry(vma->vm_rb.rb_right,
343 struct vm_area_struct, vm_rb)->rb_subtree_gap;
344 if (subtree_gap > max)
345 max = subtree_gap;
346 }
347 return max;
348}
349
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700350#ifdef CONFIG_DEBUG_VM_RB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static int browse_rb(struct rb_root *root)
352{
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800353 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct rb_node *nd, *pn = NULL;
355 unsigned long prev = 0, pend = 0;
356
357 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
358 struct vm_area_struct *vma;
359 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800360 if (vma->vm_start < prev) {
361 printk("vm_start %lx prev %lx\n", vma->vm_start, prev);
362 bug = 1;
363 }
364 if (vma->vm_start < pend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800366 bug = 1;
367 }
368 if (vma->vm_start > vma->vm_end) {
369 printk("vm_end %lx < vm_start %lx\n",
370 vma->vm_end, vma->vm_start);
371 bug = 1;
372 }
373 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
374 printk("free gap %lx, correct %lx\n",
375 vma->rb_subtree_gap,
376 vma_compute_subtree_gap(vma));
377 bug = 1;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 i++;
380 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800381 prev = vma->vm_start;
382 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800385 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800387 if (i != j) {
388 printk("backwards %d, forwards %d\n", j, i);
389 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800391 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Michel Lespinassed3737182012-12-11 16:01:38 -0800394static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
395{
396 struct rb_node *nd;
397
398 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
399 struct vm_area_struct *vma;
400 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
401 BUG_ON(vma != ignore &&
402 vma->rb_subtree_gap != vma_compute_subtree_gap(vma));
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406void validate_mm(struct mm_struct *mm)
407{
408 int bug = 0;
409 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800410 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700411 struct vm_area_struct *vma = mm->mmap;
412 while (vma) {
413 struct anon_vma_chain *avc;
Michel Lespinasse63c3b902012-11-16 14:14:47 -0800414 vma_lock_anon_vma(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700415 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
416 anon_vma_interval_tree_verify(avc);
Michel Lespinasse63c3b902012-11-16 14:14:47 -0800417 vma_unlock_anon_vma(vma);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800418 highest_address = vma->vm_end;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700419 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 i++;
421 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800422 if (i != mm->map_count) {
423 printk("map_count %d vm_next %d\n", mm->map_count, i);
424 bug = 1;
425 }
426 if (highest_address != mm->highest_vm_end) {
427 printk("mm->highest_vm_end %lx, found %lx\n",
428 mm->highest_vm_end, highest_address);
429 bug = 1;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 i = browse_rb(&mm->mm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800432 if (i != mm->map_count) {
433 printk("map_count %d rb %d\n", mm->map_count, i);
434 bug = 1;
435 }
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200436 BUG_ON(bug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800439#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#define validate_mm(mm) do { } while (0)
441#endif
442
Michel Lespinassed3737182012-12-11 16:01:38 -0800443RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
444 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
445
446/*
447 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
448 * vma->vm_prev->vm_end values changed, without modifying the vma's position
449 * in the rbtree.
450 */
451static void vma_gap_update(struct vm_area_struct *vma)
452{
453 /*
454 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
455 * function that does exacltly what we want.
456 */
457 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
458}
459
460static inline void vma_rb_insert(struct vm_area_struct *vma,
461 struct rb_root *root)
462{
463 /* All rb_subtree_gap values must be consistent prior to insertion */
464 validate_mm_rb(root, NULL);
465
466 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
467}
468
469static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
470{
471 /*
472 * All rb_subtree_gap values must be consistent prior to erase,
473 * with the possible exception of the vma being erased.
474 */
475 validate_mm_rb(root, vma);
476
477 /*
478 * Note rb_erase_augmented is a fairly large inline function,
479 * so make sure we instantiate it only once with our desired
480 * augmented rbtree callbacks.
481 */
482 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
483}
484
Michel Lespinassebf181b92012-10-08 16:31:39 -0700485/*
486 * vma has some anon_vma assigned, and is already inserted on that
487 * anon_vma's interval trees.
488 *
489 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
490 * vma must be removed from the anon_vma's interval trees using
491 * anon_vma_interval_tree_pre_update_vma().
492 *
493 * After the update, the vma will be reinserted using
494 * anon_vma_interval_tree_post_update_vma().
495 *
496 * The entire update must be protected by exclusive mmap_sem and by
497 * the root anon_vma's mutex.
498 */
499static inline void
500anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
501{
502 struct anon_vma_chain *avc;
503
504 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
505 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
506}
507
508static inline void
509anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
510{
511 struct anon_vma_chain *avc;
512
513 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
514 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
515}
516
Hugh Dickins6597d782012-10-08 16:29:07 -0700517static int find_vma_links(struct mm_struct *mm, unsigned long addr,
518 unsigned long end, struct vm_area_struct **pprev,
519 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Hugh Dickins6597d782012-10-08 16:29:07 -0700521 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 __rb_link = &mm->mm_rb.rb_node;
524 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 while (*__rb_link) {
527 struct vm_area_struct *vma_tmp;
528
529 __rb_parent = *__rb_link;
530 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
531
532 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700533 /* Fail if an existing vma overlaps the area */
534 if (vma_tmp->vm_start < end)
535 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 __rb_link = &__rb_parent->rb_left;
537 } else {
538 rb_prev = __rb_parent;
539 __rb_link = &__rb_parent->rb_right;
540 }
541 }
542
543 *pprev = NULL;
544 if (rb_prev)
545 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
546 *rb_link = __rb_link;
547 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
552 struct rb_node **rb_link, struct rb_node *rb_parent)
553{
Michel Lespinassed3737182012-12-11 16:01:38 -0800554 /* Update tracking information for the gap following the new vma. */
555 if (vma->vm_next)
556 vma_gap_update(vma->vm_next);
557 else
558 mm->highest_vm_end = vma->vm_end;
559
560 /*
561 * vma->vm_prev wasn't known when we followed the rbtree to find the
562 * correct insertion point for that vma. As a result, we could not
563 * update the vma vm_rb parents rb_subtree_gap values on the way down.
564 * So, we first insert the vma with a zero rb_subtree_gap value
565 * (to be consistent with what we did on the way down), and then
566 * immediately update the gap to the correct value. Finally we
567 * rebalance the rbtree after all augmented values have been set.
568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800570 vma->rb_subtree_gap = 0;
571 vma_gap_update(vma);
572 vma_rb_insert(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700575static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
ZhenwenXu48aae422009-01-06 14:40:21 -0800577 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 file = vma->vm_file;
580 if (file) {
581 struct address_space *mapping = file->f_mapping;
582
583 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500584 atomic_dec(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (vma->vm_flags & VM_SHARED)
586 mapping->i_mmap_writable++;
587
588 flush_dcache_mmap_lock(mapping);
589 if (unlikely(vma->vm_flags & VM_NONLINEAR))
590 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
591 else
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700592 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 flush_dcache_mmap_unlock(mapping);
594 }
595}
596
597static void
598__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
599 struct vm_area_struct *prev, struct rb_node **rb_link,
600 struct rb_node *rb_parent)
601{
602 __vma_link_list(mm, vma, prev, rb_parent);
603 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
607 struct vm_area_struct *prev, struct rb_node **rb_link,
608 struct rb_node *rb_parent)
609{
610 struct address_space *mapping = NULL;
611
612 if (vma->vm_file)
613 mapping = vma->vm_file->f_mapping;
614
Peter Zijlstra97a89412011-05-24 17:12:04 -0700615 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700616 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 __vma_link(mm, vma, prev, rb_link, rb_parent);
619 __vma_link_file(vma);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700622 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 mm->map_count++;
625 validate_mm(mm);
626}
627
628/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700629 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700630 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800632static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Hugh Dickins6597d782012-10-08 16:29:07 -0700634 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800635 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Hugh Dickins6597d782012-10-08 16:29:07 -0700637 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
638 &prev, &rb_link, &rb_parent))
639 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 __vma_link(mm, vma, prev, rb_link, rb_parent);
641 mm->map_count++;
642}
643
644static inline void
645__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
646 struct vm_area_struct *prev)
647{
Michel Lespinassed3737182012-12-11 16:01:38 -0800648 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700649
Michel Lespinassed3737182012-12-11 16:01:38 -0800650 vma_rb_erase(vma, &mm->mm_rb);
651 prev->vm_next = next = vma->vm_next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700652 if (next)
653 next->vm_prev = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (mm->mmap_cache == vma)
655 mm->mmap_cache = prev;
656}
657
658/*
659 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
660 * is already present in an i_mmap tree without adjusting the tree.
661 * The following helper function should be used when such adjustments
662 * are necessary. The "insert" vma (if any) is to be inserted
663 * before we drop the necessary locks.
664 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800665int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
667{
668 struct mm_struct *mm = vma->vm_mm;
669 struct vm_area_struct *next = vma->vm_next;
670 struct vm_area_struct *importer = NULL;
671 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700672 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700673 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800675 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 long adjust_next = 0;
677 int remove_next = 0;
678
679 if (next && !insert) {
Linus Torvalds287d97a2010-04-10 15:22:30 -0700680 struct vm_area_struct *exporter = NULL;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (end >= next->vm_end) {
683 /*
684 * vma expands, overlapping all the next, and
685 * perhaps the one after too (mprotect case 6).
686 */
687again: remove_next = 1 + (end > next->vm_end);
688 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700689 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 importer = vma;
691 } else if (end > next->vm_start) {
692 /*
693 * vma expands, overlapping part of the next:
694 * mprotect case 5 shifting the boundary up.
695 */
696 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700697 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 importer = vma;
699 } else if (end < vma->vm_end) {
700 /*
701 * vma shrinks, and !insert tells it's not
702 * split_vma inserting another: so it must be
703 * mprotect case 4 shifting the boundary down.
704 */
705 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700706 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 importer = next;
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Rik van Riel5beb4932010-03-05 13:42:07 -0800710 /*
711 * Easily overlooked: when mprotect shifts the boundary,
712 * make sure the expanding vma has anon_vma set if the
713 * shrinking vma had, to cover any anon pages imported.
714 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700715 if (exporter && exporter->anon_vma && !importer->anon_vma) {
716 if (anon_vma_clone(importer, exporter))
Rik van Riel5beb4932010-03-05 13:42:07 -0800717 return -ENOMEM;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700718 importer->anon_vma = exporter->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -0800719 }
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (file) {
723 mapping = file->f_mapping;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530724 if (!(vma->vm_flags & VM_NONLINEAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 root = &mapping->i_mmap;
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530726 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530727
728 if (adjust_next)
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530729 uprobe_munmap(next, next->vm_start,
730 next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530731 }
732
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700733 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700736 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * are visible to arm/parisc __flush_dcache_page
738 * throughout; but we cannot insert into address
739 * space until vma start or end is updated.
740 */
741 __vma_link_file(insert);
742 }
743 }
744
Andrea Arcangeli94fcc582011-01-13 15:47:08 -0800745 vma_adjust_trans_huge(vma, start, end, adjust_next);
746
Michel Lespinassebf181b92012-10-08 16:31:39 -0700747 anon_vma = vma->anon_vma;
748 if (!anon_vma && adjust_next)
749 anon_vma = next->anon_vma;
750 if (anon_vma) {
Michel Lespinasseca42b262012-10-08 16:30:01 -0700751 VM_BUG_ON(adjust_next && next->anon_vma &&
752 anon_vma != next->anon_vma);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000753 anon_vma_lock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700754 anon_vma_interval_tree_pre_update_vma(vma);
755 if (adjust_next)
756 anon_vma_interval_tree_pre_update_vma(next);
757 }
Rik van Riel012f18002010-08-09 17:18:40 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (root) {
760 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700761 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700763 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Michel Lespinassed3737182012-12-11 16:01:38 -0800766 if (start != vma->vm_start) {
767 vma->vm_start = start;
768 start_changed = true;
769 }
770 if (end != vma->vm_end) {
771 vma->vm_end = end;
772 end_changed = true;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 vma->vm_pgoff = pgoff;
775 if (adjust_next) {
776 next->vm_start += adjust_next << PAGE_SHIFT;
777 next->vm_pgoff += adjust_next;
778 }
779
780 if (root) {
781 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700782 vma_interval_tree_insert(next, root);
783 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 flush_dcache_mmap_unlock(mapping);
785 }
786
787 if (remove_next) {
788 /*
789 * vma_merge has merged next into vma, and needs
790 * us to remove next before dropping the locks.
791 */
792 __vma_unlink(mm, next, vma);
793 if (file)
794 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 } else if (insert) {
796 /*
797 * split_vma has split insert from vma, and needs
798 * us to insert it before dropping the locks
799 * (it may either follow vma or precede it).
800 */
801 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800802 } else {
803 if (start_changed)
804 vma_gap_update(vma);
805 if (end_changed) {
806 if (!next)
807 mm->highest_vm_end = end;
808 else if (!adjust_next)
809 vma_gap_update(next);
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Michel Lespinassebf181b92012-10-08 16:31:39 -0700813 if (anon_vma) {
814 anon_vma_interval_tree_post_update_vma(vma);
815 if (adjust_next)
816 anon_vma_interval_tree_post_update_vma(next);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800817 anon_vma_unlock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700820 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530822 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100823 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530824
825 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100826 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530827 }
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700830 if (file) {
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530831 uprobe_munmap(next, next->vm_start, next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700833 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800834 if (next->anon_vma)
835 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mm->map_count--;
Hampson, Steven T1444f922013-04-29 15:07:47 -0700837 vma_set_policy(vma, vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 kmem_cache_free(vm_area_cachep, next);
839 /*
840 * In mprotect's case 6 (see comments on vma_merge),
841 * we must remove another next too. It would clutter
842 * up the code too much to do both in one go.
843 */
Michel Lespinassed3737182012-12-11 16:01:38 -0800844 next = vma->vm_next;
845 if (remove_next == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto again;
Michel Lespinassed3737182012-12-11 16:01:38 -0800847 else if (next)
848 vma_gap_update(next);
849 else
850 mm->highest_vm_end = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530852 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100853 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800856
857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/*
861 * If the vma has a ->close operation then the driver probably needs to release
862 * per-vma resources, so we don't attempt to merge those.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static inline int is_mergeable_vma(struct vm_area_struct *vma,
865 struct file *file, unsigned long vm_flags)
866{
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -0700867 if (vma->vm_flags ^ vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869 if (vma->vm_file != file)
870 return 0;
871 if (vma->vm_ops && vma->vm_ops->close)
872 return 0;
873 return 1;
874}
875
876static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700877 struct anon_vma *anon_vma2,
878 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Shaohua Li965f55d2011-05-24 17:11:20 -0700880 /*
881 * The list_is_singular() test is to avoid merging VMA cloned from
882 * parents. This can improve scalability caused by anon_vma lock.
883 */
884 if ((!anon_vma1 || !anon_vma2) && (!vma ||
885 list_is_singular(&vma->anon_vma_chain)))
886 return 1;
887 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890/*
891 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
892 * in front of (at a lower virtual address and file offset than) the vma.
893 *
894 * We cannot merge two vmas if they have differently assigned (non-NULL)
895 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
896 *
897 * We don't check here for the merged mmap wrapping around the end of pagecache
898 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
899 * wrap, nor mmaps which cover the final page at index -1UL.
900 */
901static int
902can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
903 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
904{
905 if (is_mergeable_vma(vma, file, vm_flags) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700906 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (vma->vm_pgoff == vm_pgoff)
908 return 1;
909 }
910 return 0;
911}
912
913/*
914 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
915 * beyond (at a higher virtual address and file offset than) the vma.
916 *
917 * We cannot merge two vmas if they have differently assigned (non-NULL)
918 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
919 */
920static int
921can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
922 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
923{
924 if (is_mergeable_vma(vma, file, vm_flags) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700925 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 pgoff_t vm_pglen;
927 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
928 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
929 return 1;
930 }
931 return 0;
932}
933
934/*
935 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
936 * whether that can be merged with its predecessor or its successor.
937 * Or both (it neatly fills a hole).
938 *
939 * In most cases - when called for mmap, brk or mremap - [addr,end) is
940 * certain not to be mapped by the time vma_merge is called; but when
941 * called for mprotect, it is certain to be already mapped (either at
942 * an offset within prev, or at the start of next), and the flags of
943 * this area are about to be changed to vm_flags - and the no-change
944 * case has already been eliminated.
945 *
946 * The following mprotect cases have to be considered, where AAAA is
947 * the area passed down from mprotect_fixup, never extending beyond one
948 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
949 *
950 * AAAA AAAA AAAA AAAA
951 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
952 * cannot merge might become might become might become
953 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
954 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
955 * mremap move: PPPPNNNNNNNN 8
956 * AAAA
957 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
958 * might become case 1 below case 2 below case 3 below
959 *
960 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
961 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
962 */
963struct vm_area_struct *vma_merge(struct mm_struct *mm,
964 struct vm_area_struct *prev, unsigned long addr,
965 unsigned long end, unsigned long vm_flags,
966 struct anon_vma *anon_vma, struct file *file,
967 pgoff_t pgoff, struct mempolicy *policy)
968{
969 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
970 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800971 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
974 * We later require that vma->vm_flags == vm_flags,
975 * so this tests vma->vm_flags & VM_SPECIAL, too.
976 */
977 if (vm_flags & VM_SPECIAL)
978 return NULL;
979
980 if (prev)
981 next = prev->vm_next;
982 else
983 next = mm->mmap;
984 area = next;
985 if (next && next->vm_end == end) /* cases 6, 7, 8 */
986 next = next->vm_next;
987
988 /*
989 * Can it merge with the predecessor?
990 */
991 if (prev && prev->vm_end == addr &&
992 mpol_equal(vma_policy(prev), policy) &&
993 can_vma_merge_after(prev, vm_flags,
994 anon_vma, file, pgoff)) {
995 /*
996 * OK, it can. Can we now merge in the successor as well?
997 */
998 if (next && end == next->vm_start &&
999 mpol_equal(policy, vma_policy(next)) &&
1000 can_vma_merge_before(next, vm_flags,
1001 anon_vma, file, pgoff+pglen) &&
1002 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -07001003 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001005 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 next->vm_end, prev->vm_pgoff, NULL);
1007 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001008 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001010 if (err)
1011 return NULL;
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001012 khugepaged_enter_vma_merge(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return prev;
1014 }
1015
1016 /*
1017 * Can this new request be merged in front of next?
1018 */
1019 if (next && end == next->vm_start &&
1020 mpol_equal(policy, vma_policy(next)) &&
1021 can_vma_merge_before(next, vm_flags,
1022 anon_vma, file, pgoff+pglen)) {
1023 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001024 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 addr, prev->vm_pgoff, NULL);
1026 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001027 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001029 if (err)
1030 return NULL;
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001031 khugepaged_enter_vma_merge(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return area;
1033 }
1034
1035 return NULL;
1036}
1037
1038/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001039 * Rough compatbility check to quickly see if it's even worth looking
1040 * at sharing an anon_vma.
1041 *
1042 * They need to have the same vm_file, and the flags can only differ
1043 * in things that mprotect may change.
1044 *
1045 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1046 * we can merge the two vma's. For example, we refuse to merge a vma if
1047 * there is a vm_ops->close() function, because that indicates that the
1048 * driver is doing some kind of reference counting. But that doesn't
1049 * really matter for the anon_vma sharing case.
1050 */
1051static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1052{
1053 return a->vm_end == b->vm_start &&
1054 mpol_equal(vma_policy(a), vma_policy(b)) &&
1055 a->vm_file == b->vm_file &&
1056 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
1057 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1058}
1059
1060/*
1061 * Do some basic sanity checking to see if we can re-use the anon_vma
1062 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1063 * the same as 'old', the other will be the new one that is trying
1064 * to share the anon_vma.
1065 *
1066 * NOTE! This runs with mm_sem held for reading, so it is possible that
1067 * the anon_vma of 'old' is concurrently in the process of being set up
1068 * by another page fault trying to merge _that_. But that's ok: if it
1069 * is being set up, that automatically means that it will be a singleton
1070 * acceptable for merging, so we can do all of this optimistically. But
1071 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
1072 *
1073 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1074 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1075 * is to return an anon_vma that is "complex" due to having gone through
1076 * a fork).
1077 *
1078 * We also make sure that the two vma's are compatible (adjacent,
1079 * and with the same memory policies). That's all stable, even with just
1080 * a read lock on the mm_sem.
1081 */
1082static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1083{
1084 if (anon_vma_compatible(a, b)) {
1085 struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
1086
1087 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1088 return anon_vma;
1089 }
1090 return NULL;
1091}
1092
1093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1095 * neighbouring vmas for a suitable anon_vma, before it goes off
1096 * to allocate a new anon_vma. It checks because a repetitive
1097 * sequence of mprotects and faults may otherwise lead to distinct
1098 * anon_vmas being allocated, preventing vma merge in subsequent
1099 * mprotect.
1100 */
1101struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1102{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001103 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 near = vma->vm_next;
1107 if (!near)
1108 goto try_prev;
1109
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001110 anon_vma = reusable_anon_vma(near, vma, near);
1111 if (anon_vma)
1112 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001114 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!near)
1116 goto none;
1117
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001118 anon_vma = reusable_anon_vma(near, near, vma);
1119 if (anon_vma)
1120 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121none:
1122 /*
1123 * There's no absolute need to look only at touching neighbours:
1124 * we could search further afield for "compatible" anon_vmas.
1125 * But it would probably just be a waste of time searching,
1126 * or lead to too many vmas hanging off the same anon_vma.
1127 * We're trying to allow mprotect remerging later on,
1128 * not trying to minimize memory used for anon_vmas.
1129 */
1130 return NULL;
1131}
1132
1133#ifdef CONFIG_PROC_FS
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001134void vm_stat_account(struct mm_struct *mm, unsigned long flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 struct file *file, long pages)
1136{
1137 const unsigned long stack_flags
1138 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
1139
Huang Shijie44de9d02012-07-31 16:41:49 -07001140 mm->total_vm += pages;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (file) {
1143 mm->shared_vm += pages;
1144 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
1145 mm->exec_vm += pages;
1146 } else if (flags & stack_flags)
1147 mm->stack_vm += pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149#endif /* CONFIG_PROC_FS */
1150
1151/*
Al Viro40401532012-02-13 03:58:52 +00001152 * If a hint addr is less than mmap_min_addr change hint to be as
1153 * low as possible but still greater than mmap_min_addr
1154 */
1155static inline unsigned long round_hint_to_min(unsigned long hint)
1156{
1157 hint &= PAGE_MASK;
1158 if (((void *)hint != NULL) &&
1159 (hint < mmap_min_addr))
1160 return PAGE_ALIGN(mmap_min_addr);
1161 return hint;
1162}
1163
1164/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001165 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
1167
Al Viroe3fc6292012-05-30 20:08:42 -04001168unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 unsigned long len, unsigned long prot,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001170 unsigned long flags, unsigned long pgoff,
Michel Lespinasse41badc12013-02-22 16:32:47 -08001171 unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct mm_struct * mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct inode *inode;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001175 vm_flags_t vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Michel Lespinasse41badc12013-02-22 16:32:47 -08001177 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /*
1180 * Does the application expect PROT_READ to imply PROT_EXEC?
1181 *
1182 * (the exception is when the underlying filesystem is noexec
1183 * mounted, in which case we dont add PROT_EXEC.)
1184 */
1185 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001186 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 prot |= PROT_EXEC;
1188
1189 if (!len)
1190 return -EINVAL;
1191
Eric Paris7cd94142007-11-26 18:47:40 -05001192 if (!(flags & MAP_FIXED))
1193 addr = round_hint_to_min(addr);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* Careful about overflows.. */
1196 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001197 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return -ENOMEM;
1199
1200 /* offset overflow? */
1201 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
1202 return -EOVERFLOW;
1203
1204 /* Too many mappings? */
1205 if (mm->map_count > sysctl_max_map_count)
1206 return -ENOMEM;
1207
1208 /* Obtain the address to map to. we verify (or select) it and ensure
1209 * that it represents a valid section of the address space.
1210 */
1211 addr = get_unmapped_area(file, addr, len, pgoff, flags);
1212 if (addr & ~PAGE_MASK)
1213 return addr;
1214
1215 /* Do simple checking here so the lower-level routines won't have
1216 * to. we assume access permissions have been handled by the open
1217 * of the memory object, so we don't do any here.
1218 */
1219 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
1220 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1221
Huang Shijiecdf7b342009-09-21 17:03:36 -07001222 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (!can_do_mlock())
1224 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* mlock MCL_FUTURE? */
1227 if (vm_flags & VM_LOCKED) {
1228 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07001229 locked = len >> PAGE_SHIFT;
1230 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001231 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07001232 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1234 return -EAGAIN;
1235 }
1236
Al Viro496ad9a2013-01-23 17:07:38 -05001237 inode = file ? file_inode(file) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (file) {
1240 switch (flags & MAP_TYPE) {
1241 case MAP_SHARED:
1242 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1243 return -EACCES;
1244
1245 /*
1246 * Make sure we don't allow writing to an append-only
1247 * file..
1248 */
1249 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1250 return -EACCES;
1251
1252 /*
1253 * Make sure there are no mandatory locks on the file.
1254 */
1255 if (locks_verify_locked(inode))
1256 return -EAGAIN;
1257
1258 vm_flags |= VM_SHARED | VM_MAYSHARE;
1259 if (!(file->f_mode & FMODE_WRITE))
1260 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1261
1262 /* fall through */
1263 case MAP_PRIVATE:
1264 if (!(file->f_mode & FMODE_READ))
1265 return -EACCES;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001266 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001267 if (vm_flags & VM_EXEC)
1268 return -EPERM;
1269 vm_flags &= ~VM_MAYEXEC;
1270 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001271
1272 if (!file->f_op || !file->f_op->mmap)
1273 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
1275
1276 default:
1277 return -EINVAL;
1278 }
1279 } else {
1280 switch (flags & MAP_TYPE) {
1281 case MAP_SHARED:
Tejun Heoce363942008-09-03 16:09:47 +02001282 /*
1283 * Ignore pgoff.
1284 */
1285 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 vm_flags |= VM_SHARED | VM_MAYSHARE;
1287 break;
1288 case MAP_PRIVATE:
1289 /*
1290 * Set pgoff according to addr for anon_vma.
1291 */
1292 pgoff = addr >> PAGE_SHIFT;
1293 break;
1294 default:
1295 return -EINVAL;
1296 }
1297 }
1298
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001299 /*
1300 * Set 'VM_NORESERVE' if we should not account for the
1301 * memory use of this mapping.
1302 */
1303 if (flags & MAP_NORESERVE) {
1304 /* We honor MAP_NORESERVE if allowed to overcommit */
1305 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1306 vm_flags |= VM_NORESERVE;
1307
1308 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1309 if (file && is_file_hugepages(file))
1310 vm_flags |= VM_NORESERVE;
1311 }
1312
1313 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001314 if (!IS_ERR_VALUE(addr) &&
1315 ((vm_flags & VM_LOCKED) ||
1316 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001317 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001318 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001319}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001320
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001321SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1322 unsigned long, prot, unsigned long, flags,
1323 unsigned long, fd, unsigned long, pgoff)
1324{
1325 struct file *file = NULL;
1326 unsigned long retval = -EBADF;
1327
1328 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001329 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001330 if (unlikely(flags & MAP_HUGETLB))
1331 return -EINVAL;
1332 file = fget(fd);
1333 if (!file)
1334 goto out;
1335 } else if (flags & MAP_HUGETLB) {
1336 struct user_struct *user = NULL;
1337 /*
1338 * VM_NORESERVE is used because the reservations will be
1339 * taken when vm_ops->mmap() is called
1340 * A dummy user value is used because we are not locking
1341 * memory so no accounting is necessary
1342 */
Steven Truelove40716e22012-03-21 16:34:14 -07001343 file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001344 VM_NORESERVE,
1345 &user, HUGETLB_ANONHUGE_INODE,
1346 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001347 if (IS_ERR(file))
1348 return PTR_ERR(file);
1349 }
1350
1351 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1352
Al Viroeb36c582012-05-30 20:17:35 -04001353 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001354 if (file)
1355 fput(file);
1356out:
1357 return retval;
1358}
1359
Christoph Hellwiga4679372010-03-10 15:21:15 -08001360#ifdef __ARCH_WANT_SYS_OLD_MMAP
1361struct mmap_arg_struct {
1362 unsigned long addr;
1363 unsigned long len;
1364 unsigned long prot;
1365 unsigned long flags;
1366 unsigned long fd;
1367 unsigned long offset;
1368};
1369
1370SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1371{
1372 struct mmap_arg_struct a;
1373
1374 if (copy_from_user(&a, arg, sizeof(a)))
1375 return -EFAULT;
1376 if (a.offset & ~PAGE_MASK)
1377 return -EINVAL;
1378
1379 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1380 a.offset >> PAGE_SHIFT);
1381}
1382#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1383
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001384/*
1385 * Some shared mappigns will want the pages marked read-only
1386 * to track write events. If so, we'll downgrade vm_page_prot
1387 * to the private version (using protection_map[] without the
1388 * VM_SHARED bit).
1389 */
1390int vma_wants_writenotify(struct vm_area_struct *vma)
1391{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001392 vm_flags_t vm_flags = vma->vm_flags;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001393
1394 /* If it was private or non-writable, the write bit is already clear */
1395 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1396 return 0;
1397
1398 /* The backer wishes to know when pages are first written to? */
1399 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1400 return 1;
1401
1402 /* The open routine did something to the protections already? */
1403 if (pgprot_val(vma->vm_page_prot) !=
Coly Li3ed75eb2007-10-18 23:39:15 -07001404 pgprot_val(vm_get_page_prot(vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001405 return 0;
1406
1407 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001408 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001409 return 0;
1410
1411 /* Can the mapping track the dirty pages? */
1412 return vma->vm_file && vma->vm_file->f_mapping &&
1413 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1414}
1415
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001416/*
1417 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001418 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001419 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001420static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001421{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001422 /*
1423 * hugetlb has its own accounting separate from the core VM
1424 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1425 */
1426 if (file && is_file_hugepages(file))
1427 return 0;
1428
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001429 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1430}
1431
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001432unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001433 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001434{
1435 struct mm_struct *mm = current->mm;
1436 struct vm_area_struct *vma, *prev;
1437 int correct_wcount = 0;
1438 int error;
1439 struct rb_node **rb_link, *rb_parent;
1440 unsigned long charged = 0;
Al Viro496ad9a2013-01-23 17:07:38 -05001441 struct inode *inode = file ? file_inode(file) : NULL;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Clear old maps */
1444 error = -ENOMEM;
1445munmap_back:
Hugh Dickins6597d782012-10-08 16:29:07 -07001446 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (do_munmap(mm, addr, len))
1448 return -ENOMEM;
1449 goto munmap_back;
1450 }
1451
1452 /* Check against address space limit. */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001453 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENOMEM;
1455
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001456 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001457 * Private writable mapping: check memory availability
1458 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001459 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001460 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001461 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001462 return -ENOMEM;
1463 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
1466 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001467 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 */
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001469 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1470 if (vma)
1471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /*
1474 * Determine the object being mapped and call the appropriate
1475 * specific mapper. the address has already been validated, but
1476 * not unmapped, but the maps are removed from the list.
1477 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001478 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (!vma) {
1480 error = -ENOMEM;
1481 goto unacct_error;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 vma->vm_mm = mm;
1485 vma->vm_start = addr;
1486 vma->vm_end = addr + len;
1487 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001488 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001490 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Hugh Dickinsce8fea72012-03-06 12:28:52 -08001492 error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1496 goto free_vma;
1497 if (vm_flags & VM_DENYWRITE) {
1498 error = deny_write_access(file);
1499 if (error)
1500 goto free_vma;
1501 correct_wcount = 1;
1502 }
Al Virocb0942b2012-08-27 14:48:26 -04001503 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 error = file->f_op->mmap(file, vma);
1505 if (error)
1506 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001507
1508 /* Can addr have changed??
1509 *
1510 * Answer: Yes, several device drivers can do it in their
1511 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001512 * Bug: If addr is changed, prev, rb_link, rb_parent should
1513 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001514 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001515 WARN_ON_ONCE(addr != vma->vm_start);
1516
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001517 addr = vma->vm_start;
1518 pgoff = vma->vm_pgoff;
1519 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 } else if (vm_flags & VM_SHARED) {
Al Viro835ee792012-03-05 06:39:47 +00001521 if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
1522 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 error = shmem_zero_setup(vma);
1524 if (error)
1525 goto free_vma;
1526 }
1527
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001528 if (vma_wants_writenotify(vma)) {
1529 pgprot_t pprot = vma->vm_page_prot;
1530
1531 /* Can vma->vm_page_prot have changed??
1532 *
1533 * Answer: Yes, drivers may have changed it in their
1534 * f_op->mmap method.
1535 *
1536 * Ensures that vmas marked as uncached stay that way.
1537 */
Hugh Dickins1ddd4392007-10-22 20:45:12 -07001538 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001539 if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1540 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1541 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -07001542
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001543 vma_link(mm, vma, prev, rb_link, rb_parent);
1544 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001545
1546 /* Once vma denies write, undo our temporary denial count */
1547 if (correct_wcount)
1548 atomic_inc(&inode->i_writecount);
1549out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001551
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001552 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001554 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1555 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001556 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001557 else
1558 vma->vm_flags &= ~VM_LOCKED;
1559 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301560
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001561 if (file)
1562 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return addr;
1565
1566unmap_and_free_vma:
1567 if (correct_wcount)
1568 atomic_inc(&inode->i_writecount);
1569 vma->vm_file = NULL;
1570 fput(file);
1571
1572 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001573 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1574 charged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575free_vma:
1576 kmem_cache_free(vm_area_cachep, vma);
1577unacct_error:
1578 if (charged)
1579 vm_unacct_memory(charged);
1580 return error;
1581}
1582
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001583unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1584{
1585 /*
1586 * We implement the search by looking for an rbtree node that
1587 * immediately follows a suitable gap. That is,
1588 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1589 * - gap_end = vma->vm_start >= info->low_limit + length;
1590 * - gap_end - gap_start >= length
1591 */
1592
1593 struct mm_struct *mm = current->mm;
1594 struct vm_area_struct *vma;
1595 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1596
1597 /* Adjust search length to account for worst case alignment overhead */
1598 length = info->length + info->align_mask;
1599 if (length < info->length)
1600 return -ENOMEM;
1601
1602 /* Adjust search limits by the desired length */
1603 if (info->high_limit < length)
1604 return -ENOMEM;
1605 high_limit = info->high_limit - length;
1606
1607 if (info->low_limit > high_limit)
1608 return -ENOMEM;
1609 low_limit = info->low_limit + length;
1610
1611 /* Check if rbtree root looks promising */
1612 if (RB_EMPTY_ROOT(&mm->mm_rb))
1613 goto check_highest;
1614 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1615 if (vma->rb_subtree_gap < length)
1616 goto check_highest;
1617
1618 while (true) {
1619 /* Visit left subtree if it looks promising */
1620 gap_end = vma->vm_start;
1621 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1622 struct vm_area_struct *left =
1623 rb_entry(vma->vm_rb.rb_left,
1624 struct vm_area_struct, vm_rb);
1625 if (left->rb_subtree_gap >= length) {
1626 vma = left;
1627 continue;
1628 }
1629 }
1630
1631 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1632check_current:
1633 /* Check if current node has a suitable gap */
1634 if (gap_start > high_limit)
1635 return -ENOMEM;
1636 if (gap_end >= low_limit && gap_end - gap_start >= length)
1637 goto found;
1638
1639 /* Visit right subtree if it looks promising */
1640 if (vma->vm_rb.rb_right) {
1641 struct vm_area_struct *right =
1642 rb_entry(vma->vm_rb.rb_right,
1643 struct vm_area_struct, vm_rb);
1644 if (right->rb_subtree_gap >= length) {
1645 vma = right;
1646 continue;
1647 }
1648 }
1649
1650 /* Go back up the rbtree to find next candidate node */
1651 while (true) {
1652 struct rb_node *prev = &vma->vm_rb;
1653 if (!rb_parent(prev))
1654 goto check_highest;
1655 vma = rb_entry(rb_parent(prev),
1656 struct vm_area_struct, vm_rb);
1657 if (prev == vma->vm_rb.rb_left) {
1658 gap_start = vma->vm_prev->vm_end;
1659 gap_end = vma->vm_start;
1660 goto check_current;
1661 }
1662 }
1663 }
1664
1665check_highest:
1666 /* Check highest gap, which does not precede any rbtree node */
1667 gap_start = mm->highest_vm_end;
1668 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1669 if (gap_start > high_limit)
1670 return -ENOMEM;
1671
1672found:
1673 /* We found a suitable gap. Clip it with the original low_limit. */
1674 if (gap_start < info->low_limit)
1675 gap_start = info->low_limit;
1676
1677 /* Adjust gap address to the desired alignment */
1678 gap_start += (info->align_offset - gap_start) & info->align_mask;
1679
1680 VM_BUG_ON(gap_start + info->length > info->high_limit);
1681 VM_BUG_ON(gap_start + info->length > gap_end);
1682 return gap_start;
1683}
1684
1685unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1686{
1687 struct mm_struct *mm = current->mm;
1688 struct vm_area_struct *vma;
1689 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1690
1691 /* Adjust search length to account for worst case alignment overhead */
1692 length = info->length + info->align_mask;
1693 if (length < info->length)
1694 return -ENOMEM;
1695
1696 /*
1697 * Adjust search limits by the desired length.
1698 * See implementation comment at top of unmapped_area().
1699 */
1700 gap_end = info->high_limit;
1701 if (gap_end < length)
1702 return -ENOMEM;
1703 high_limit = gap_end - length;
1704
1705 if (info->low_limit > high_limit)
1706 return -ENOMEM;
1707 low_limit = info->low_limit + length;
1708
1709 /* Check highest gap, which does not precede any rbtree node */
1710 gap_start = mm->highest_vm_end;
1711 if (gap_start <= high_limit)
1712 goto found_highest;
1713
1714 /* Check if rbtree root looks promising */
1715 if (RB_EMPTY_ROOT(&mm->mm_rb))
1716 return -ENOMEM;
1717 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1718 if (vma->rb_subtree_gap < length)
1719 return -ENOMEM;
1720
1721 while (true) {
1722 /* Visit right subtree if it looks promising */
1723 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1724 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1725 struct vm_area_struct *right =
1726 rb_entry(vma->vm_rb.rb_right,
1727 struct vm_area_struct, vm_rb);
1728 if (right->rb_subtree_gap >= length) {
1729 vma = right;
1730 continue;
1731 }
1732 }
1733
1734check_current:
1735 /* Check if current node has a suitable gap */
1736 gap_end = vma->vm_start;
1737 if (gap_end < low_limit)
1738 return -ENOMEM;
1739 if (gap_start <= high_limit && gap_end - gap_start >= length)
1740 goto found;
1741
1742 /* Visit left subtree if it looks promising */
1743 if (vma->vm_rb.rb_left) {
1744 struct vm_area_struct *left =
1745 rb_entry(vma->vm_rb.rb_left,
1746 struct vm_area_struct, vm_rb);
1747 if (left->rb_subtree_gap >= length) {
1748 vma = left;
1749 continue;
1750 }
1751 }
1752
1753 /* Go back up the rbtree to find next candidate node */
1754 while (true) {
1755 struct rb_node *prev = &vma->vm_rb;
1756 if (!rb_parent(prev))
1757 return -ENOMEM;
1758 vma = rb_entry(rb_parent(prev),
1759 struct vm_area_struct, vm_rb);
1760 if (prev == vma->vm_rb.rb_right) {
1761 gap_start = vma->vm_prev ?
1762 vma->vm_prev->vm_end : 0;
1763 goto check_current;
1764 }
1765 }
1766 }
1767
1768found:
1769 /* We found a suitable gap. Clip it with the original high_limit. */
1770 if (gap_end > info->high_limit)
1771 gap_end = info->high_limit;
1772
1773found_highest:
1774 /* Compute highest gap address at the desired alignment */
1775 gap_end -= info->length;
1776 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1777
1778 VM_BUG_ON(gap_end < info->low_limit);
1779 VM_BUG_ON(gap_end < gap_start);
1780 return gap_end;
1781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/* Get an address range which is currently unmapped.
1784 * For shmat() with addr=0.
1785 *
1786 * Ugly calling convention alert:
1787 * Return value with the low bits set means error value,
1788 * ie
1789 * if (ret & ~PAGE_MASK)
1790 * error = ret;
1791 *
1792 * This function "knows" that -ENOMEM has the bits set.
1793 */
1794#ifndef HAVE_ARCH_UNMAPPED_AREA
1795unsigned long
1796arch_get_unmapped_area(struct file *filp, unsigned long addr,
1797 unsigned long len, unsigned long pgoff, unsigned long flags)
1798{
1799 struct mm_struct *mm = current->mm;
1800 struct vm_area_struct *vma;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001801 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (len > TASK_SIZE)
1804 return -ENOMEM;
1805
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001806 if (flags & MAP_FIXED)
1807 return addr;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (addr) {
1810 addr = PAGE_ALIGN(addr);
1811 vma = find_vma(mm, addr);
1812 if (TASK_SIZE - len >= addr &&
1813 (!vma || addr + len <= vma->vm_start))
1814 return addr;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001817 info.flags = 0;
1818 info.length = len;
1819 info.low_limit = TASK_UNMAPPED_BASE;
1820 info.high_limit = TASK_SIZE;
1821 info.align_mask = 0;
1822 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824#endif
1825
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001826void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 /*
1829 * Is this a new hole at the lowest possible address?
1830 */
Xiao Guangrongf44d2192012-03-21 16:33:56 -07001831 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001832 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
1835/*
1836 * This mmap-allocator allocates new areas top-down from below the
1837 * stack's low limit (the base):
1838 */
1839#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1840unsigned long
1841arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1842 const unsigned long len, const unsigned long pgoff,
1843 const unsigned long flags)
1844{
1845 struct vm_area_struct *vma;
1846 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001847 unsigned long addr = addr0;
1848 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 /* requested length too big for entire address space */
1851 if (len > TASK_SIZE)
1852 return -ENOMEM;
1853
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001854 if (flags & MAP_FIXED)
1855 return addr;
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* requesting a specific address */
1858 if (addr) {
1859 addr = PAGE_ALIGN(addr);
1860 vma = find_vma(mm, addr);
1861 if (TASK_SIZE - len >= addr &&
1862 (!vma || addr + len <= vma->vm_start))
1863 return addr;
1864 }
1865
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001866 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1867 info.length = len;
1868 info.low_limit = PAGE_SIZE;
1869 info.high_limit = mm->mmap_base;
1870 info.align_mask = 0;
1871 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
1874 * A failed mmap() very likely causes application failure,
1875 * so fall back to the bottom-up function here. This scenario
1876 * can happen with large stack limits and large mmap()
1877 * allocations.
1878 */
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001879 if (addr & ~PAGE_MASK) {
1880 VM_BUG_ON(addr != -ENOMEM);
1881 info.flags = 0;
1882 info.low_limit = TASK_UNMAPPED_BASE;
1883 info.high_limit = TASK_SIZE;
1884 addr = vm_unmapped_area(&info);
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 return addr;
1888}
1889#endif
1890
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001891void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 /*
1894 * Is this a new hole at the highest possible address?
1895 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001896 if (addr > mm->free_area_cache)
1897 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /* dont allow allocations above current base */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001900 if (mm->free_area_cache > mm->mmap_base)
1901 mm->free_area_cache = mm->mmap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904unsigned long
1905get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1906 unsigned long pgoff, unsigned long flags)
1907{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001908 unsigned long (*get_area)(struct file *, unsigned long,
1909 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Al Viro9206de92009-12-03 15:23:11 -05001911 unsigned long error = arch_mmap_check(addr, len, flags);
1912 if (error)
1913 return error;
1914
1915 /* Careful about overflows.. */
1916 if (len > TASK_SIZE)
1917 return -ENOMEM;
1918
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001919 get_area = current->mm->get_unmapped_area;
1920 if (file && file->f_op && file->f_op->get_unmapped_area)
1921 get_area = file->f_op->get_unmapped_area;
1922 addr = get_area(file, addr, len, pgoff, flags);
1923 if (IS_ERR_VALUE(addr))
1924 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001926 if (addr > TASK_SIZE - len)
1927 return -ENOMEM;
1928 if (addr & ~PAGE_MASK)
1929 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001930
Al Viro9ac4ed42012-05-30 17:13:15 -04001931 addr = arch_rebalance_pgtables(addr, len);
1932 error = security_mmap_addr(addr);
1933 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
1936EXPORT_SYMBOL(get_unmapped_area);
1937
1938/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08001939struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 struct vm_area_struct *vma = NULL;
1942
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001943 /* Check the cache first. */
1944 /* (Cache hit rate is typically around 35%.) */
Jan Stancekb6a9b7f2013-04-04 11:35:10 -07001945 vma = ACCESS_ONCE(mm->mmap_cache);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001946 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1947 struct rb_node *rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001949 rb_node = mm->mm_rb.rb_node;
1950 vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001952 while (rb_node) {
1953 struct vm_area_struct *vma_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001955 vma_tmp = rb_entry(rb_node,
1956 struct vm_area_struct, vm_rb);
1957
1958 if (vma_tmp->vm_end > addr) {
1959 vma = vma_tmp;
1960 if (vma_tmp->vm_start <= addr)
1961 break;
1962 rb_node = rb_node->rb_left;
1963 } else
1964 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001966 if (vma)
1967 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969 return vma;
1970}
1971
1972EXPORT_SYMBOL(find_vma);
1973
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001974/*
1975 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977struct vm_area_struct *
1978find_vma_prev(struct mm_struct *mm, unsigned long addr,
1979 struct vm_area_struct **pprev)
1980{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001981 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001983 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05001984 if (vma) {
1985 *pprev = vma->vm_prev;
1986 } else {
1987 struct rb_node *rb_node = mm->mm_rb.rb_node;
1988 *pprev = NULL;
1989 while (rb_node) {
1990 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1991 rb_node = rb_node->rb_right;
1992 }
1993 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001994 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
1997/*
1998 * Verify that the stack growth is acceptable and
1999 * update accounting. This is shared with both the
2000 * grow-up and grow-down cases.
2001 */
ZhenwenXu48aae422009-01-06 14:40:21 -08002002static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct mm_struct *mm = vma->vm_mm;
2005 struct rlimit *rlim = current->signal->rlim;
Adam Litke0d59a012007-01-30 14:35:39 -08002006 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* address space limit tests */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002009 if (!may_expand_vm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return -ENOMEM;
2011
2012 /* Stack limit test */
Jiri Slaby59e99e52010-03-05 13:41:44 -08002013 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return -ENOMEM;
2015
2016 /* mlock limit tests */
2017 if (vma->vm_flags & VM_LOCKED) {
2018 unsigned long locked;
2019 unsigned long limit;
2020 locked = mm->locked_vm + grow;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002021 limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
2022 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (locked > limit && !capable(CAP_IPC_LOCK))
2024 return -ENOMEM;
2025 }
2026
Adam Litke0d59a012007-01-30 14:35:39 -08002027 /* Check to ensure the stack will not grow into a hugetlb-only region */
2028 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2029 vma->vm_end - size;
2030 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2031 return -EFAULT;
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /*
2034 * Overcommit.. This must be the final test, as it will
2035 * update security statistics.
2036 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002037 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return -ENOMEM;
2039
2040 /* Ok, everything looks good - let it rip */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (vma->vm_flags & VM_LOCKED)
2042 mm->locked_vm += grow;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07002043 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return 0;
2045}
2046
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002047#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002049 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2050 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002052int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 int error;
2055
2056 if (!(vma->vm_flags & VM_GROWSUP))
2057 return -EFAULT;
2058
2059 /*
2060 * We must make sure the anon_vma is allocated
2061 * so that the anon_vma locking is not a noop.
2062 */
2063 if (unlikely(anon_vma_prepare(vma)))
2064 return -ENOMEM;
Rik van Rielbb4a3402010-08-09 17:18:37 -07002065 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /*
2068 * vma->vm_start/vm_end cannot change under us because the caller
2069 * is required to hold the mmap_sem in read mode. We need the
2070 * anon_vma lock to serialize against concurrent expand_stacks.
Helge Deller06b32f32006-12-19 19:28:33 +01002071 * Also guard against wrapping around to address 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 */
Helge Deller06b32f32006-12-19 19:28:33 +01002073 if (address < PAGE_ALIGN(address+4))
2074 address = PAGE_ALIGN(address+4);
2075 else {
Rik van Rielbb4a3402010-08-09 17:18:37 -07002076 vma_unlock_anon_vma(vma);
Helge Deller06b32f32006-12-19 19:28:33 +01002077 return -ENOMEM;
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 error = 0;
2080
2081 /* Somebody else might have raced and expanded it already */
2082 if (address > vma->vm_end) {
2083 unsigned long size, grow;
2084
2085 size = address - vma->vm_start;
2086 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2087
Hugh Dickins42c36f62011-05-09 17:44:42 -07002088 error = -ENOMEM;
2089 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2090 error = acct_stack_growth(vma, size, grow);
2091 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002092 /*
2093 * vma_gap_update() doesn't support concurrent
2094 * updates, but we only hold a shared mmap_sem
2095 * lock here, so we need to protect against
2096 * concurrent vma expansions.
2097 * vma_lock_anon_vma() doesn't help here, as
2098 * we don't guarantee that all growable vmas
2099 * in a mm share the same root anon vma.
2100 * So, we reuse mm->page_table_lock to guard
2101 * against concurrent vma expansions.
2102 */
2103 spin_lock(&vma->vm_mm->page_table_lock);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002104 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002105 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002106 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002107 if (vma->vm_next)
2108 vma_gap_update(vma->vm_next);
2109 else
2110 vma->vm_mm->highest_vm_end = address;
Michel Lespinasse41289972012-12-12 13:52:25 -08002111 spin_unlock(&vma->vm_mm->page_table_lock);
2112
Hugh Dickins42c36f62011-05-09 17:44:42 -07002113 perf_event_mmap(vma);
2114 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07002117 vma_unlock_anon_vma(vma);
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08002118 khugepaged_enter_vma_merge(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07002119 validate_mm(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return error;
2121}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002122#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124/*
2125 * vma is the first one with address < vma->vm_start. Have to extend vma.
2126 */
Michal Hockod05f3162011-05-24 17:11:44 -07002127int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002128 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 int error;
2131
2132 /*
2133 * We must make sure the anon_vma is allocated
2134 * so that the anon_vma locking is not a noop.
2135 */
2136 if (unlikely(anon_vma_prepare(vma)))
2137 return -ENOMEM;
Eric Paris88694772007-11-26 18:47:26 -05002138
2139 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04002140 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05002141 if (error)
2142 return error;
2143
Rik van Rielbb4a3402010-08-09 17:18:37 -07002144 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 /*
2147 * vma->vm_start/vm_end cannot change under us because the caller
2148 * is required to hold the mmap_sem in read mode. We need the
2149 * anon_vma lock to serialize against concurrent expand_stacks.
2150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /* Somebody else might have raced and expanded it already */
2153 if (address < vma->vm_start) {
2154 unsigned long size, grow;
2155
2156 size = vma->vm_end - address;
2157 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2158
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002159 error = -ENOMEM;
2160 if (grow <= vma->vm_pgoff) {
2161 error = acct_stack_growth(vma, size, grow);
2162 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002163 /*
2164 * vma_gap_update() doesn't support concurrent
2165 * updates, but we only hold a shared mmap_sem
2166 * lock here, so we need to protect against
2167 * concurrent vma expansions.
2168 * vma_lock_anon_vma() doesn't help here, as
2169 * we don't guarantee that all growable vmas
2170 * in a mm share the same root anon vma.
2171 * So, we reuse mm->page_table_lock to guard
2172 * against concurrent vma expansions.
2173 */
2174 spin_lock(&vma->vm_mm->page_table_lock);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002175 anon_vma_interval_tree_pre_update_vma(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002176 vma->vm_start = address;
2177 vma->vm_pgoff -= grow;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002178 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002179 vma_gap_update(vma);
Michel Lespinasse41289972012-12-12 13:52:25 -08002180 spin_unlock(&vma->vm_mm->page_table_lock);
2181
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002182 perf_event_mmap(vma);
2183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07002186 vma_unlock_anon_vma(vma);
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08002187 khugepaged_enter_vma_merge(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07002188 validate_mm(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return error;
2190}
2191
Linus Torvalds09884962013-02-27 08:36:04 -08002192/*
2193 * Note how expand_stack() refuses to expand the stack all the way to
2194 * abut the next virtual mapping, *unless* that mapping itself is also
2195 * a stack mapping. We want to leave room for a guard page, after all
2196 * (the guard page itself is not added here, that is done by the
2197 * actual page faulting logic)
2198 *
2199 * This matches the behavior of the guard page logic (see mm/memory.c:
2200 * check_stack_guard_page()), which only allows the guard page to be
2201 * removed under these circumstances.
2202 */
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002203#ifdef CONFIG_STACK_GROWSUP
2204int expand_stack(struct vm_area_struct *vma, unsigned long address)
2205{
Linus Torvalds09884962013-02-27 08:36:04 -08002206 struct vm_area_struct *next;
2207
2208 address &= PAGE_MASK;
2209 next = vma->vm_next;
2210 if (next && next->vm_start == address + PAGE_SIZE) {
2211 if (!(next->vm_flags & VM_GROWSUP))
2212 return -ENOMEM;
2213 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002214 return expand_upwards(vma, address);
2215}
2216
2217struct vm_area_struct *
2218find_extend_vma(struct mm_struct *mm, unsigned long addr)
2219{
2220 struct vm_area_struct *vma, *prev;
2221
2222 addr &= PAGE_MASK;
2223 vma = find_vma_prev(mm, addr, &prev);
2224 if (vma && (vma->vm_start <= addr))
2225 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002226 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002227 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002228 if (prev->vm_flags & VM_LOCKED)
2229 __mlock_vma_pages_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002230 return prev;
2231}
2232#else
2233int expand_stack(struct vm_area_struct *vma, unsigned long address)
2234{
Linus Torvalds09884962013-02-27 08:36:04 -08002235 struct vm_area_struct *prev;
2236
2237 address &= PAGE_MASK;
2238 prev = vma->vm_prev;
2239 if (prev && prev->vm_end == address) {
2240 if (!(prev->vm_flags & VM_GROWSDOWN))
2241 return -ENOMEM;
2242 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002243 return expand_downwards(vma, address);
2244}
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246struct vm_area_struct *
2247find_extend_vma(struct mm_struct * mm, unsigned long addr)
2248{
2249 struct vm_area_struct * vma;
2250 unsigned long start;
2251
2252 addr &= PAGE_MASK;
2253 vma = find_vma(mm,addr);
2254 if (!vma)
2255 return NULL;
2256 if (vma->vm_start <= addr)
2257 return vma;
2258 if (!(vma->vm_flags & VM_GROWSDOWN))
2259 return NULL;
2260 start = vma->vm_start;
2261 if (expand_stack(vma, addr))
2262 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002263 if (vma->vm_flags & VM_LOCKED)
2264 __mlock_vma_pages_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return vma;
2266}
2267#endif
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002270 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002272 *
2273 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002275static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002277 unsigned long nr_accounted = 0;
2278
Hugh Dickins365e9c872005-10-29 18:16:18 -07002279 /* Update high watermark before we lower total_vm */
2280 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002282 long nrpages = vma_pages(vma);
2283
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002284 if (vma->vm_flags & VM_ACCOUNT)
2285 nr_accounted += nrpages;
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002286 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002287 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002288 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002289 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 validate_mm(mm);
2291}
2292
2293/*
2294 * Get rid of page table information in the indicated region.
2295 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002296 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 */
2298static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002299 struct vm_area_struct *vma, struct vm_area_struct *prev,
2300 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Hugh Dickinse0da3822005-04-19 13:29:15 -07002302 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002303 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 lru_add_drain();
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002306 tlb_gather_mmu(&tlb, mm, 0);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002307 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002308 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002309 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002310 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002311 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314/*
2315 * Create a list of vma's touched by the unmap, removing them from the mm's
2316 * vma list as we go..
2317 */
2318static void
2319detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2320 struct vm_area_struct *prev, unsigned long end)
2321{
2322 struct vm_area_struct **insertion_point;
2323 struct vm_area_struct *tail_vma = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002324 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002327 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 do {
Michel Lespinassed3737182012-12-11 16:01:38 -08002329 vma_rb_erase(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 mm->map_count--;
2331 tail_vma = vma;
2332 vma = vma->vm_next;
2333 } while (vma && vma->vm_start < end);
2334 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002335 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002336 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002337 vma_gap_update(vma);
2338 } else
2339 mm->highest_vm_end = prev ? prev->vm_end : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 tail_vma->vm_next = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002341 if (mm->unmap_area == arch_unmap_area)
2342 addr = prev ? prev->vm_end : mm->mmap_base;
2343 else
2344 addr = vma ? vma->vm_start : mm->mmap_base;
2345 mm->unmap_area(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 mm->mmap_cache = NULL; /* Kill the cache. */
2347}
2348
2349/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002350 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2351 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 */
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002353static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 unsigned long addr, int new_below)
2355{
2356 struct mempolicy *pol;
2357 struct vm_area_struct *new;
Rik van Riel5beb4932010-03-05 13:42:07 -08002358 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Andi Kleena5516432008-07-23 21:27:41 -07002360 if (is_vm_hugetlb_page(vma) && (addr &
2361 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return -EINVAL;
2363
Christoph Lametere94b1762006-12-06 20:33:17 -08002364 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!new)
Rik van Riel5beb4932010-03-05 13:42:07 -08002366 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 /* most fields are the same, copy all, and then fixup */
2369 *new = *vma;
2370
Rik van Riel5beb4932010-03-05 13:42:07 -08002371 INIT_LIST_HEAD(&new->anon_vma_chain);
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (new_below)
2374 new->vm_end = addr;
2375 else {
2376 new->vm_start = addr;
2377 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2378 }
2379
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002380 pol = mpol_dup(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (IS_ERR(pol)) {
Rik van Riel5beb4932010-03-05 13:42:07 -08002382 err = PTR_ERR(pol);
2383 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385 vma_set_policy(new, pol);
2386
Rik van Riel5beb4932010-03-05 13:42:07 -08002387 if (anon_vma_clone(new, vma))
2388 goto out_free_mpol;
2389
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002390 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 get_file(new->vm_file);
2392
2393 if (new->vm_ops && new->vm_ops->open)
2394 new->vm_ops->open(new);
2395
2396 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002397 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2399 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002400 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Rik van Riel5beb4932010-03-05 13:42:07 -08002402 /* Success. */
2403 if (!err)
2404 return 0;
2405
2406 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002407 if (new->vm_ops && new->vm_ops->close)
2408 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002409 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002410 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002411 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002412 out_free_mpol:
2413 mpol_put(pol);
2414 out_free_vma:
2415 kmem_cache_free(vm_area_cachep, new);
2416 out_err:
2417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002420/*
2421 * Split a vma into two pieces at address 'addr', a new vma is allocated
2422 * either for the first part or the tail.
2423 */
2424int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2425 unsigned long addr, int new_below)
2426{
2427 if (mm->map_count >= sysctl_max_map_count)
2428 return -ENOMEM;
2429
2430 return __split_vma(mm, vma, addr, new_below);
2431}
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433/* Munmap is split into 2 main parts -- this part which finds
2434 * what needs doing, and the areas themselves, which do the
2435 * work. This now handles partial unmappings.
2436 * Jeremy Fitzhardinge <jeremy@goop.org>
2437 */
2438int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2439{
2440 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002441 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
2444 return -EINVAL;
2445
2446 if ((len = PAGE_ALIGN(len)) == 0)
2447 return -EINVAL;
2448
2449 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002450 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002451 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002453 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002454 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 /* if it doesn't overlap, we have nothing.. */
2457 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002458 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 0;
2460
2461 /*
2462 * If we need to split any vma, do it now to save pain later.
2463 *
2464 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2465 * unmapped vm_area_struct will remain in use: so lower split_vma
2466 * places tmp vma above, and higher split_vma places tmp vma below.
2467 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002468 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002469 int error;
2470
2471 /*
2472 * Make sure that map_count on return from munmap() will
2473 * not exceed its limit; but let map_count go just above
2474 * its limit temporarily, to help free resources as expected.
2475 */
2476 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2477 return -ENOMEM;
2478
2479 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (error)
2481 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002482 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
2485 /* Does it split the last one? */
2486 last = find_vma(mm, end);
2487 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002488 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (error)
2490 return error;
2491 }
Hugh Dickins146425a2005-04-19 13:29:18 -07002492 vma = prev? prev->vm_next: mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002495 * unlock any mlock()ed ranges before detaching vmas
2496 */
2497 if (mm->locked_vm) {
2498 struct vm_area_struct *tmp = vma;
2499 while (tmp && tmp->vm_start < end) {
2500 if (tmp->vm_flags & VM_LOCKED) {
2501 mm->locked_vm -= vma_pages(tmp);
2502 munlock_vma_pages_all(tmp);
2503 }
2504 tmp = tmp->vm_next;
2505 }
2506 }
2507
2508 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 * Remove the vma's, and unmap the actual pages
2510 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002511 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2512 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002515 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 return 0;
2518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Al Virobfce2812012-04-20 21:57:04 -04002520int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002521{
2522 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002523 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002524
2525 down_write(&mm->mmap_sem);
2526 ret = do_munmap(mm, start, len);
2527 up_write(&mm->mmap_sem);
2528 return ret;
2529}
2530EXPORT_SYMBOL(vm_munmap);
2531
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002532SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 profile_munmap(addr);
Al Virobfce2812012-04-20 21:57:04 -04002535 return vm_munmap(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538static inline void verify_mm_writelocked(struct mm_struct *mm)
2539{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002540#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2542 WARN_ON(1);
2543 up_read(&mm->mmap_sem);
2544 }
2545#endif
2546}
2547
2548/*
2549 * this is really a simplified "do_mmap". it only handles
2550 * anonymous maps. eventually we may be able to do some
2551 * brk-specific accounting here.
2552 */
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002553static unsigned long do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 struct mm_struct * mm = current->mm;
2556 struct vm_area_struct * vma, * prev;
2557 unsigned long flags;
2558 struct rb_node ** rb_link, * rb_parent;
2559 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002560 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 len = PAGE_ALIGN(len);
2563 if (!len)
2564 return addr;
2565
Kirill Korotaev3a459752006-09-07 14:17:04 +04002566 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2567
Al Viro2c6a1012009-12-03 19:40:46 -05002568 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
2569 if (error & ~PAGE_MASK)
Kirill Korotaev3a459752006-09-07 14:17:04 +04002570 return error;
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * mlock MCL_FUTURE?
2574 */
2575 if (mm->def_flags & VM_LOCKED) {
2576 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07002577 locked = len >> PAGE_SHIFT;
2578 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002579 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07002580 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
2582 return -EAGAIN;
2583 }
2584
2585 /*
2586 * mm->mmap_sem is required to protect against another thread
2587 * changing the mappings in case we sleep.
2588 */
2589 verify_mm_writelocked(mm);
2590
2591 /*
2592 * Clear old maps. this also does some error checking for us
2593 */
2594 munmap_back:
Hugh Dickins6597d782012-10-08 16:29:07 -07002595 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (do_munmap(mm, addr, len))
2597 return -ENOMEM;
2598 goto munmap_back;
2599 }
2600
2601 /* Check against address space limits *after* clearing old maps... */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002602 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return -ENOMEM;
2604
2605 if (mm->map_count > sysctl_max_map_count)
2606 return -ENOMEM;
2607
Al Viro191c5422012-02-13 03:58:52 +00002608 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return -ENOMEM;
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002612 vma = vma_merge(mm, prev, addr, addr + len, flags,
2613 NULL, NULL, pgoff, NULL);
2614 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 goto out;
2616
2617 /*
2618 * create a vma struct for an anonymous mapping
2619 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002620 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (!vma) {
2622 vm_unacct_memory(len >> PAGE_SHIFT);
2623 return -ENOMEM;
2624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Rik van Riel5beb4932010-03-05 13:42:07 -08002626 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 vma->vm_mm = mm;
2628 vma->vm_start = addr;
2629 vma->vm_end = addr + len;
2630 vma->vm_pgoff = pgoff;
2631 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002632 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 vma_link(mm, vma, prev, rb_link, rb_parent);
2634out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002635 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 mm->total_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002637 if (flags & VM_LOCKED)
2638 mm->locked_vm += (len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return addr;
2640}
2641
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002642unsigned long vm_brk(unsigned long addr, unsigned long len)
2643{
2644 struct mm_struct *mm = current->mm;
2645 unsigned long ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002646 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002647
2648 down_write(&mm->mmap_sem);
2649 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002650 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002651 up_write(&mm->mmap_sem);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002652 if (populate)
2653 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002654 return ret;
2655}
2656EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658/* Release all mmaps. */
2659void exit_mmap(struct mm_struct *mm)
2660{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002661 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002662 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 unsigned long nr_accounted = 0;
2664
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002665 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002666 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002667
Rik van Rielba470de2008-10-18 20:26:50 -07002668 if (mm->locked_vm) {
2669 vma = mm->mmap;
2670 while (vma) {
2671 if (vma->vm_flags & VM_LOCKED)
2672 munlock_vma_pages_all(vma);
2673 vma = vma->vm_next;
2674 }
2675 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002676
2677 arch_exit_mmap(mm);
2678
Rik van Rielba470de2008-10-18 20:26:50 -07002679 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002680 if (!vma) /* Can happen if dup_mmap() received an OOM */
2681 return;
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 flush_cache_mm(mm);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002685 tlb_gather_mmu(&tlb, mm, 1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002686 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002687 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002688 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002689
Hugh Dickins6ee86302013-04-29 15:07:44 -07002690 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05002691 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002694 * Walk the list again, actually closing and freeing it,
2695 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002697 while (vma) {
2698 if (vma->vm_flags & VM_ACCOUNT)
2699 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002700 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002701 }
2702 vm_unacct_memory(nr_accounted);
Hugh Dickinse0da3822005-04-19 13:29:15 -07002703
Hugh Dickinsf9aed622012-08-21 16:15:45 -07002704 WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
2707/* Insert vm structure into process list sorted by address
2708 * and into the inode's i_mmap tree. If vm_file is non-NULL
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002709 * then i_mmap_mutex is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 */
Hugh Dickins6597d782012-10-08 16:29:07 -07002711int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Hugh Dickins6597d782012-10-08 16:29:07 -07002713 struct vm_area_struct *prev;
2714 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716 /*
2717 * The vm_pgoff of a purely anonymous vma should be irrelevant
2718 * until its first write fault, when page's anon_vma and index
2719 * are set. But now set the vm_pgoff it will almost certainly
2720 * end up with (unless mremap moves it elsewhere before that
2721 * first wfault), so /proc/pid/maps tells a consistent story.
2722 *
2723 * By setting it to reflect the virtual start address of the
2724 * vma, merges and splits can happen in a seamless way, just
2725 * using the existing file pgoff checks and manipulations.
2726 * Similarly in do_mmap_pgoff and in do_brk.
2727 */
2728 if (!vma->vm_file) {
2729 BUG_ON(vma->anon_vma);
2730 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2731 }
Hugh Dickins6597d782012-10-08 16:29:07 -07002732 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
2733 &prev, &rb_link, &rb_parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return -ENOMEM;
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002735 if ((vma->vm_flags & VM_ACCOUNT) &&
Alan Cox34b4e4a2007-08-22 14:01:28 -07002736 security_vm_enough_memory_mm(mm, vma_pages(vma)))
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002737 return -ENOMEM;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 vma_link(mm, vma, prev, rb_link, rb_parent);
2740 return 0;
2741}
2742
2743/*
2744 * Copy the vma structure to a new location in the same mm,
2745 * prior to moving page table entries, to effect an mremap move.
2746 */
2747struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07002748 unsigned long addr, unsigned long len, pgoff_t pgoff,
2749 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 struct vm_area_struct *vma = *vmap;
2752 unsigned long vma_start = vma->vm_start;
2753 struct mm_struct *mm = vma->vm_mm;
2754 struct vm_area_struct *new_vma, *prev;
2755 struct rb_node **rb_link, *rb_parent;
2756 struct mempolicy *pol;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002757 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 /*
2760 * If anonymous vma has not yet been faulted, update new pgoff
2761 * to match new location, to increase its chance of merging.
2762 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002763 if (unlikely(!vma->vm_file && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002765 faulted_in_anon_vma = false;
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Hugh Dickins6597d782012-10-08 16:29:07 -07002768 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
2769 return NULL; /* should never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2771 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2772 if (new_vma) {
2773 /*
2774 * Source vma may have been merged into new_vma
2775 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002776 if (unlikely(vma_start >= new_vma->vm_start &&
2777 vma_start < new_vma->vm_end)) {
2778 /*
2779 * The only way we can get a vma_merge with
2780 * self during an mremap is if the vma hasn't
2781 * been faulted in yet and we were allowed to
2782 * reset the dst vma->vm_pgoff to the
2783 * destination address of the mremap to allow
2784 * the merge to happen. mremap must change the
2785 * vm_pgoff linearity between src and dst vmas
2786 * (in turn preventing a vma_merge) to be
2787 * safe. It is only safe to keep the vm_pgoff
2788 * linear if there are no pages mapped yet.
2789 */
2790 VM_BUG_ON(faulted_in_anon_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002791 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07002792 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07002793 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002795 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if (new_vma) {
2797 *new_vma = *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 new_vma->vm_start = addr;
2799 new_vma->vm_end = addr + len;
2800 new_vma->vm_pgoff = pgoff;
Michel Lespinasse523d4e22012-10-08 16:31:48 -07002801 pol = mpol_dup(vma_policy(vma));
2802 if (IS_ERR(pol))
2803 goto out_free_vma;
2804 vma_set_policy(new_vma, pol);
2805 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2806 if (anon_vma_clone(new_vma, vma))
2807 goto out_free_mempol;
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002808 if (new_vma->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 get_file(new_vma->vm_file);
2810 if (new_vma->vm_ops && new_vma->vm_ops->open)
2811 new_vma->vm_ops->open(new_vma);
2812 vma_link(mm, new_vma, prev, rb_link, rb_parent);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002813 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815 }
2816 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002817
2818 out_free_mempol:
2819 mpol_put(pol);
2820 out_free_vma:
2821 kmem_cache_free(vm_area_cachep, new_vma);
2822 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002824
2825/*
2826 * Return true if the calling process may expand its vm space by the passed
2827 * number of pages
2828 */
2829int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2830{
2831 unsigned long cur = mm->total_vm; /* pages */
2832 unsigned long lim;
2833
Jiri Slaby59e99e52010-03-05 13:41:44 -08002834 lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002835
2836 if (cur + npages > lim)
2837 return 0;
2838 return 1;
2839}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002840
2841
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002842static int special_mapping_fault(struct vm_area_struct *vma,
2843 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08002844{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002845 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002846 struct page **pages;
2847
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002848 /*
2849 * special mappings have no vm_file, and in that case, the mm
2850 * uses vm_pgoff internally. So we have to subtract it from here.
2851 * We are allowed to do this because we are the mm; do not copy
2852 * this code into drivers!
2853 */
2854 pgoff = vmf->pgoff - vma->vm_pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002855
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002856 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2857 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002858
2859 if (*pages) {
2860 struct page *page = *pages;
2861 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002862 vmf->page = page;
2863 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002864 }
2865
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002866 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002867}
2868
2869/*
2870 * Having a close hook prevents vma merging regardless of flags.
2871 */
2872static void special_mapping_close(struct vm_area_struct *vma)
2873{
2874}
2875
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002876static const struct vm_operations_struct special_mapping_vmops = {
Roland McGrathfa5dc222007-02-08 14:20:41 -08002877 .close = special_mapping_close,
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002878 .fault = special_mapping_fault,
Roland McGrathfa5dc222007-02-08 14:20:41 -08002879};
2880
2881/*
2882 * Called with mm->mmap_sem held for writing.
2883 * Insert a new vma covering the given region, with the given flags.
2884 * Its pages are supplied by the given array of struct page *.
2885 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2886 * The region past the last page supplied will always produce SIGBUS.
2887 * The array pointer and the pages it points to are assumed to stay alive
2888 * for as long as this mapping might exist.
2889 */
2890int install_special_mapping(struct mm_struct *mm,
2891 unsigned long addr, unsigned long len,
2892 unsigned long vm_flags, struct page **pages)
2893{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002894 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002895 struct vm_area_struct *vma;
2896
2897 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2898 if (unlikely(vma == NULL))
2899 return -ENOMEM;
2900
Rik van Riel5beb4932010-03-05 13:42:07 -08002901 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002902 vma->vm_mm = mm;
2903 vma->vm_start = addr;
2904 vma->vm_end = addr + len;
2905
Nick Piggin2f987352008-02-02 03:08:53 +01002906 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
Coly Li3ed75eb2007-10-18 23:39:15 -07002907 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002908
2909 vma->vm_ops = &special_mapping_vmops;
2910 vma->vm_private_data = pages;
2911
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002912 ret = insert_vm_struct(mm, vma);
2913 if (ret)
2914 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002915
2916 mm->total_vm += len >> PAGE_SHIFT;
2917
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002918 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02002919
Roland McGrathfa5dc222007-02-08 14:20:41 -08002920 return 0;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002921
2922out:
2923 kmem_cache_free(vm_area_cachep, vma);
2924 return ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002925}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002926
2927static DEFINE_MUTEX(mm_all_locks_mutex);
2928
Peter Zijlstra454ed842008-08-11 09:30:25 +02002929static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002930{
Michel Lespinassebf181b92012-10-08 16:31:39 -07002931 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002932 /*
2933 * The LSB of head.next can't change from under us
2934 * because we hold the mm_all_locks_mutex.
2935 */
Jiri Kosina572043c2013-01-11 14:31:59 -08002936 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002937 /*
2938 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00002939 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002940 * the same anon_vma we won't take it again.
2941 *
2942 * No need of atomic instructions here, head.next
2943 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00002944 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002945 */
2946 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07002947 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002948 BUG();
2949 }
2950}
2951
Peter Zijlstra454ed842008-08-11 09:30:25 +02002952static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002953{
2954 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2955 /*
2956 * AS_MM_ALL_LOCKS can't change from under us because
2957 * we hold the mm_all_locks_mutex.
2958 *
2959 * Operations on ->flags have to be atomic because
2960 * even if AS_MM_ALL_LOCKS is stable thanks to the
2961 * mm_all_locks_mutex, there may be other cpus
2962 * changing other bitflags in parallel to us.
2963 */
2964 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2965 BUG();
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002966 mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002967 }
2968}
2969
2970/*
2971 * This operation locks against the VM for all pte/vma/mm related
2972 * operations that could ever happen on a certain mm. This includes
2973 * vmtruncate, try_to_unmap, and all page faults.
2974 *
2975 * The caller must take the mmap_sem in write mode before calling
2976 * mm_take_all_locks(). The caller isn't allowed to release the
2977 * mmap_sem until mm_drop_all_locks() returns.
2978 *
2979 * mmap_sem in write mode is required in order to block all operations
2980 * that could modify pagetables and free pages without need of
2981 * altering the vma layout (for example populate_range() with
2982 * nonlinear vmas). It's also needed in write mode to avoid new
2983 * anon_vmas to be associated with existing vmas.
2984 *
2985 * A single task can't take more than one mm_take_all_locks() in a row
2986 * or it would deadlock.
2987 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07002988 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002989 * mapping->flags avoid to take the same lock twice, if more than one
2990 * vma in this mm is backed by the same anon_vma or address_space.
2991 *
2992 * We can take all the locks in random order because the VM code
Yuanhan Liu631b0cf2013-02-04 14:28:48 -08002993 * taking i_mmap_mutex or anon_vma->rwsem outside the mmap_sem never
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002994 * takes more than one of them in a row. Secondly we're protected
2995 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2996 *
2997 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2998 * that may have to take thousand of locks.
2999 *
3000 * mm_take_all_locks() can fail if it's interrupted by signals.
3001 */
3002int mm_take_all_locks(struct mm_struct *mm)
3003{
3004 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003005 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003006
3007 BUG_ON(down_read_trylock(&mm->mmap_sem));
3008
3009 mutex_lock(&mm_all_locks_mutex);
3010
3011 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3012 if (signal_pending(current))
3013 goto out_unlock;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003014 if (vma->vm_file && vma->vm_file->f_mapping)
Peter Zijlstra454ed842008-08-11 09:30:25 +02003015 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003016 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003017
3018 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3019 if (signal_pending(current))
3020 goto out_unlock;
3021 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003022 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3023 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003024 }
3025
Kautuk Consul584cff52011-10-31 17:08:59 -07003026 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003027
3028out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003029 mm_drop_all_locks(mm);
3030 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003031}
3032
3033static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3034{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003035 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003036 /*
3037 * The LSB of head.next can't change to 0 from under
3038 * us because we hold the mm_all_locks_mutex.
3039 *
3040 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003041 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003042 * never see our bitflag.
3043 *
3044 * No need of atomic instructions here, head.next
3045 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003046 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003047 */
3048 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003049 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003050 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003051 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003052 }
3053}
3054
3055static void vm_unlock_mapping(struct address_space *mapping)
3056{
3057 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3058 /*
3059 * AS_MM_ALL_LOCKS can't change to 0 from under us
3060 * because we hold the mm_all_locks_mutex.
3061 */
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07003062 mutex_unlock(&mapping->i_mmap_mutex);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003063 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3064 &mapping->flags))
3065 BUG();
3066 }
3067}
3068
3069/*
3070 * The mmap_sem cannot be released by the caller until
3071 * mm_drop_all_locks() returns.
3072 */
3073void mm_drop_all_locks(struct mm_struct *mm)
3074{
3075 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003076 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003077
3078 BUG_ON(down_read_trylock(&mm->mmap_sem));
3079 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3080
3081 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3082 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003083 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3084 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003085 if (vma->vm_file && vma->vm_file->f_mapping)
3086 vm_unlock_mapping(vma->vm_file->f_mapping);
3087 }
3088
3089 mutex_unlock(&mm_all_locks_mutex);
3090}
David Howells8feae132009-01-08 12:04:47 +00003091
3092/*
3093 * initialise the VMA slab
3094 */
3095void __init mmap_init(void)
3096{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003097 int ret;
3098
3099 ret = percpu_counter_init(&vm_committed_as, 0);
3100 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003101}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003102
3103/*
3104 * Initialise sysctl_user_reserve_kbytes.
3105 *
3106 * This is intended to prevent a user from starting a single memory hogging
3107 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3108 * mode.
3109 *
3110 * The default value is min(3% of free memory, 128MB)
3111 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3112 */
3113static int __meminit init_user_reserve(void)
3114{
3115 unsigned long free_kbytes;
3116
3117 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3118
3119 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3120 return 0;
3121}
3122module_init(init_user_reserve)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003123
3124/*
3125 * Initialise sysctl_admin_reserve_kbytes.
3126 *
3127 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3128 * to log in and kill a memory hogging process.
3129 *
3130 * Systems with more than 256MB will reserve 8MB, enough to recover
3131 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3132 * only reserve 3% of free pages by default.
3133 */
3134static int __meminit init_admin_reserve(void)
3135{
3136 unsigned long free_kbytes;
3137
3138 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3139
3140 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3141 return 0;
3142}
3143module_init(init_admin_reserve)