mm: vm_unmapped_area() lookup function
[linux-block.git] / mm / mmap.c
CommitLineData
1da177e4
LT
1/*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
046c6884 6 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
7 */
8
9#include <linux/slab.h>
4af3c9cc 10#include <linux/backing-dev.h>
1da177e4
LT
11#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>
c59ede7b 17#include <linux/capability.h>
1da177e4
LT
18#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>
b95f1b31 25#include <linux/export.h>
1da177e4
LT
26#include <linux/mount.h>
27#include <linux/mempolicy.h>
28#include <linux/rmap.h>
cddb8a5c 29#include <linux/mmu_notifier.h>
cdd6c482 30#include <linux/perf_event.h>
120a795d 31#include <linux/audit.h>
b15d00b6 32#include <linux/khugepaged.h>
2b144498 33#include <linux/uprobes.h>
d3737187 34#include <linux/rbtree_augmented.h>
1da177e4
LT
35
36#include <asm/uaccess.h>
37#include <asm/cacheflush.h>
38#include <asm/tlb.h>
d6dd61c8 39#include <asm/mmu_context.h>
1da177e4 40
42b77728
JB
41#include "internal.h"
42
3a459756
KK
43#ifndef arch_mmap_check
44#define arch_mmap_check(addr, len, flags) (0)
45#endif
46
08e7d9b5
MS
47#ifndef arch_rebalance_pgtables
48#define arch_rebalance_pgtables(addr, len) (addr)
49#endif
50
e0da382c
HD
51static void unmap_region(struct mm_struct *mm,
52 struct vm_area_struct *vma, struct vm_area_struct *prev,
53 unsigned long start, unsigned long end);
54
1da177e4
LT
55/* description of effects of mapping type and prot in current implementation.
56 * this is due to the limited x86 page protection hardware. The expected
57 * behavior is in parens:
58 *
59 * map_type prot
60 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
61 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
62 * w: (no) no w: (no) no w: (yes) yes w: (no) no
63 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
64 *
65 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
66 * w: (no) no w: (no) no w: (copy) copy w: (no) no
67 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
68 *
69 */
70pgprot_t protection_map[16] = {
71 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
72 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
73};
74
804af2cf
HD
75pgprot_t vm_get_page_prot(unsigned long vm_flags)
76{
b845f313
DK
77 return __pgprot(pgprot_val(protection_map[vm_flags &
78 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
79 pgprot_val(arch_vm_get_page_prot(vm_flags)));
804af2cf
HD
80}
81EXPORT_SYMBOL(vm_get_page_prot);
82
34679d7e
SL
83int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
84int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
c3d8c141 85int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
34679d7e
SL
86/*
87 * Make sure vm_committed_as in one cacheline and not cacheline shared with
88 * other variables. It can be updated by several CPUs frequently.
89 */
90struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
1da177e4
LT
91
92/*
93 * Check that a process has enough memory to allocate a new virtual
94 * mapping. 0 means there is enough memory for the allocation to
95 * succeed and -ENOMEM implies there is not.
96 *
97 * We currently support three overcommit policies, which are set via the
98 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
99 *
100 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
101 * Additional code 2002 Jul 20 by Robert Love.
102 *
103 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
104 *
105 * Note this is a helper function intended to be used by LSMs which
106 * wish to use this logic.
107 */
34b4e4aa 108int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1da177e4
LT
109{
110 unsigned long free, allowed;
111
112 vm_acct_memory(pages);
113
114 /*
115 * Sometimes we want to use more memory than we have
116 */
117 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
118 return 0;
119
120 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
c15bef30
DF
121 free = global_page_state(NR_FREE_PAGES);
122 free += global_page_state(NR_FILE_PAGES);
123
124 /*
125 * shmem pages shouldn't be counted as free in this
126 * case, they can't be purged, only swapped out, and
127 * that won't affect the overall amount of available
128 * memory in the system.
129 */
130 free -= global_page_state(NR_SHMEM);
1da177e4 131
1da177e4
LT
132 free += nr_swap_pages;
133
134 /*
135 * Any slabs which are created with the
136 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
137 * which are reclaimable, under pressure. The dentry
138 * cache and most inode caches should fall into this
139 */
972d1a7b 140 free += global_page_state(NR_SLAB_RECLAIMABLE);
1da177e4 141
6d9f7839
HA
142 /*
143 * Leave reserved pages. The pages are not for anonymous pages.
144 */
c15bef30 145 if (free <= totalreserve_pages)
6d9f7839
HA
146 goto error;
147 else
c15bef30 148 free -= totalreserve_pages;
6d9f7839
HA
149
150 /*
151 * Leave the last 3% for root
152 */
1da177e4 153 if (!cap_sys_admin)
c15bef30 154 free -= free / 32;
1da177e4
LT
155
156 if (free > pages)
157 return 0;
6d9f7839
HA
158
159 goto error;
1da177e4
LT
160 }
161
162 allowed = (totalram_pages - hugetlb_total_pages())
163 * sysctl_overcommit_ratio / 100;
164 /*
165 * Leave the last 3% for root
166 */
167 if (!cap_sys_admin)
168 allowed -= allowed / 32;
169 allowed += total_swap_pages;
170
171 /* Don't let a single process grow too big:
172 leave 3% of the size of this process for other processes */
731572d3
AC
173 if (mm)
174 allowed -= mm->total_vm / 32;
1da177e4 175
00a62ce9 176 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1da177e4 177 return 0;
6d9f7839 178error:
1da177e4
LT
179 vm_unacct_memory(pages);
180
181 return -ENOMEM;
182}
183
1da177e4 184/*
3d48ae45 185 * Requires inode->i_mapping->i_mmap_mutex
1da177e4
LT
186 */
187static void __remove_shared_vm_struct(struct vm_area_struct *vma,
188 struct file *file, struct address_space *mapping)
189{
190 if (vma->vm_flags & VM_DENYWRITE)
d3ac7f89 191 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
1da177e4
LT
192 if (vma->vm_flags & VM_SHARED)
193 mapping->i_mmap_writable--;
194
195 flush_dcache_mmap_lock(mapping);
196 if (unlikely(vma->vm_flags & VM_NONLINEAR))
6b2dbba8 197 list_del_init(&vma->shared.nonlinear);
1da177e4 198 else
6b2dbba8 199 vma_interval_tree_remove(vma, &mapping->i_mmap);
1da177e4
LT
200 flush_dcache_mmap_unlock(mapping);
201}
202
203/*
6b2dbba8 204 * Unlink a file-based vm structure from its interval tree, to hide
a8fb5618 205 * vma from rmap and vmtruncate before freeing its page tables.
1da177e4 206 */
a8fb5618 207void unlink_file_vma(struct vm_area_struct *vma)
1da177e4
LT
208{
209 struct file *file = vma->vm_file;
210
1da177e4
LT
211 if (file) {
212 struct address_space *mapping = file->f_mapping;
3d48ae45 213 mutex_lock(&mapping->i_mmap_mutex);
1da177e4 214 __remove_shared_vm_struct(vma, file, mapping);
3d48ae45 215 mutex_unlock(&mapping->i_mmap_mutex);
1da177e4 216 }
a8fb5618
HD
217}
218
219/*
220 * Close a vm structure and free it, returning the next.
221 */
222static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
223{
224 struct vm_area_struct *next = vma->vm_next;
225
a8fb5618 226 might_sleep();
1da177e4
LT
227 if (vma->vm_ops && vma->vm_ops->close)
228 vma->vm_ops->close(vma);
e9714acf 229 if (vma->vm_file)
a8fb5618 230 fput(vma->vm_file);
f0be3d32 231 mpol_put(vma_policy(vma));
1da177e4 232 kmem_cache_free(vm_area_cachep, vma);
a8fb5618 233 return next;
1da177e4
LT
234}
235
e4eb1ff6
LT
236static unsigned long do_brk(unsigned long addr, unsigned long len);
237
6a6160a7 238SYSCALL_DEFINE1(brk, unsigned long, brk)
1da177e4
LT
239{
240 unsigned long rlim, retval;
241 unsigned long newbrk, oldbrk;
242 struct mm_struct *mm = current->mm;
a5b4592c 243 unsigned long min_brk;
1da177e4
LT
244
245 down_write(&mm->mmap_sem);
246
a5b4592c 247#ifdef CONFIG_COMPAT_BRK
5520e894
JK
248 /*
249 * CONFIG_COMPAT_BRK can still be overridden by setting
250 * randomize_va_space to 2, which will still cause mm->start_brk
251 * to be arbitrarily shifted
252 */
4471a675 253 if (current->brk_randomized)
5520e894
JK
254 min_brk = mm->start_brk;
255 else
256 min_brk = mm->end_data;
a5b4592c
JK
257#else
258 min_brk = mm->start_brk;
259#endif
260 if (brk < min_brk)
1da177e4 261 goto out;
1e624196
RG
262
263 /*
264 * Check against rlimit here. If this check is done later after the test
265 * of oldbrk with newbrk then it can escape the test and let the data
266 * segment grow beyond its set limit the in case where the limit is
267 * not page aligned -Ram Gupta
268 */
59e99e5b 269 rlim = rlimit(RLIMIT_DATA);
c1d171a0
JK
270 if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
271 (mm->end_data - mm->start_data) > rlim)
1e624196
RG
272 goto out;
273
1da177e4
LT
274 newbrk = PAGE_ALIGN(brk);
275 oldbrk = PAGE_ALIGN(mm->brk);
276 if (oldbrk == newbrk)
277 goto set_brk;
278
279 /* Always allow shrinking brk. */
280 if (brk <= mm->brk) {
281 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
282 goto set_brk;
283 goto out;
284 }
285
1da177e4
LT
286 /* Check against existing mmap mappings. */
287 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
288 goto out;
289
290 /* Ok, looks good - let it rip. */
291 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
292 goto out;
293set_brk:
294 mm->brk = brk;
295out:
296 retval = mm->brk;
297 up_write(&mm->mmap_sem);
298 return retval;
299}
300
d3737187
ML
301static long vma_compute_subtree_gap(struct vm_area_struct *vma)
302{
303 unsigned long max, subtree_gap;
304 max = vma->vm_start;
305 if (vma->vm_prev)
306 max -= vma->vm_prev->vm_end;
307 if (vma->vm_rb.rb_left) {
308 subtree_gap = rb_entry(vma->vm_rb.rb_left,
309 struct vm_area_struct, vm_rb)->rb_subtree_gap;
310 if (subtree_gap > max)
311 max = subtree_gap;
312 }
313 if (vma->vm_rb.rb_right) {
314 subtree_gap = rb_entry(vma->vm_rb.rb_right,
315 struct vm_area_struct, vm_rb)->rb_subtree_gap;
316 if (subtree_gap > max)
317 max = subtree_gap;
318 }
319 return max;
320}
321
ed8ea815 322#ifdef CONFIG_DEBUG_VM_RB
1da177e4
LT
323static int browse_rb(struct rb_root *root)
324{
5a0768f6 325 int i = 0, j, bug = 0;
1da177e4
LT
326 struct rb_node *nd, *pn = NULL;
327 unsigned long prev = 0, pend = 0;
328
329 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
330 struct vm_area_struct *vma;
331 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
5a0768f6
ML
332 if (vma->vm_start < prev) {
333 printk("vm_start %lx prev %lx\n", vma->vm_start, prev);
334 bug = 1;
335 }
336 if (vma->vm_start < pend) {
1da177e4 337 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
5a0768f6
ML
338 bug = 1;
339 }
340 if (vma->vm_start > vma->vm_end) {
341 printk("vm_end %lx < vm_start %lx\n",
342 vma->vm_end, vma->vm_start);
343 bug = 1;
344 }
345 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
346 printk("free gap %lx, correct %lx\n",
347 vma->rb_subtree_gap,
348 vma_compute_subtree_gap(vma));
349 bug = 1;
350 }
1da177e4
LT
351 i++;
352 pn = nd;
d1af65d1
DM
353 prev = vma->vm_start;
354 pend = vma->vm_end;
1da177e4
LT
355 }
356 j = 0;
5a0768f6 357 for (nd = pn; nd; nd = rb_prev(nd))
1da177e4 358 j++;
5a0768f6
ML
359 if (i != j) {
360 printk("backwards %d, forwards %d\n", j, i);
361 bug = 1;
1da177e4 362 }
5a0768f6 363 return bug ? -1 : i;
1da177e4
LT
364}
365
d3737187
ML
366static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
367{
368 struct rb_node *nd;
369
370 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
371 struct vm_area_struct *vma;
372 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
373 BUG_ON(vma != ignore &&
374 vma->rb_subtree_gap != vma_compute_subtree_gap(vma));
375 }
376}
377
1da177e4
LT
378void validate_mm(struct mm_struct *mm)
379{
380 int bug = 0;
381 int i = 0;
5a0768f6 382 unsigned long highest_address = 0;
ed8ea815
ML
383 struct vm_area_struct *vma = mm->mmap;
384 while (vma) {
385 struct anon_vma_chain *avc;
63c3b902 386 vma_lock_anon_vma(vma);
ed8ea815
ML
387 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
388 anon_vma_interval_tree_verify(avc);
63c3b902 389 vma_unlock_anon_vma(vma);
5a0768f6 390 highest_address = vma->vm_end;
ed8ea815 391 vma = vma->vm_next;
1da177e4
LT
392 i++;
393 }
5a0768f6
ML
394 if (i != mm->map_count) {
395 printk("map_count %d vm_next %d\n", mm->map_count, i);
396 bug = 1;
397 }
398 if (highest_address != mm->highest_vm_end) {
399 printk("mm->highest_vm_end %lx, found %lx\n",
400 mm->highest_vm_end, highest_address);
401 bug = 1;
402 }
1da177e4 403 i = browse_rb(&mm->mm_rb);
5a0768f6
ML
404 if (i != mm->map_count) {
405 printk("map_count %d rb %d\n", mm->map_count, i);
406 bug = 1;
407 }
46a350ef 408 BUG_ON(bug);
1da177e4
LT
409}
410#else
d3737187 411#define validate_mm_rb(root, ignore) do { } while (0)
1da177e4
LT
412#define validate_mm(mm) do { } while (0)
413#endif
414
d3737187
ML
415RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
416 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
417
418/*
419 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
420 * vma->vm_prev->vm_end values changed, without modifying the vma's position
421 * in the rbtree.
422 */
423static void vma_gap_update(struct vm_area_struct *vma)
424{
425 /*
426 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
427 * function that does exacltly what we want.
428 */
429 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
430}
431
432static inline void vma_rb_insert(struct vm_area_struct *vma,
433 struct rb_root *root)
434{
435 /* All rb_subtree_gap values must be consistent prior to insertion */
436 validate_mm_rb(root, NULL);
437
438 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
439}
440
441static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
442{
443 /*
444 * All rb_subtree_gap values must be consistent prior to erase,
445 * with the possible exception of the vma being erased.
446 */
447 validate_mm_rb(root, vma);
448
449 /*
450 * Note rb_erase_augmented is a fairly large inline function,
451 * so make sure we instantiate it only once with our desired
452 * augmented rbtree callbacks.
453 */
454 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
455}
456
bf181b9f
ML
457/*
458 * vma has some anon_vma assigned, and is already inserted on that
459 * anon_vma's interval trees.
460 *
461 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
462 * vma must be removed from the anon_vma's interval trees using
463 * anon_vma_interval_tree_pre_update_vma().
464 *
465 * After the update, the vma will be reinserted using
466 * anon_vma_interval_tree_post_update_vma().
467 *
468 * The entire update must be protected by exclusive mmap_sem and by
469 * the root anon_vma's mutex.
470 */
471static inline void
472anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
473{
474 struct anon_vma_chain *avc;
475
476 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
477 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
478}
479
480static inline void
481anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
482{
483 struct anon_vma_chain *avc;
484
485 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
486 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
487}
488
6597d783
HD
489static int find_vma_links(struct mm_struct *mm, unsigned long addr,
490 unsigned long end, struct vm_area_struct **pprev,
491 struct rb_node ***rb_link, struct rb_node **rb_parent)
1da177e4 492{
6597d783 493 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
1da177e4
LT
494
495 __rb_link = &mm->mm_rb.rb_node;
496 rb_prev = __rb_parent = NULL;
1da177e4
LT
497
498 while (*__rb_link) {
499 struct vm_area_struct *vma_tmp;
500
501 __rb_parent = *__rb_link;
502 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
503
504 if (vma_tmp->vm_end > addr) {
6597d783
HD
505 /* Fail if an existing vma overlaps the area */
506 if (vma_tmp->vm_start < end)
507 return -ENOMEM;
1da177e4
LT
508 __rb_link = &__rb_parent->rb_left;
509 } else {
510 rb_prev = __rb_parent;
511 __rb_link = &__rb_parent->rb_right;
512 }
513 }
514
515 *pprev = NULL;
516 if (rb_prev)
517 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
518 *rb_link = __rb_link;
519 *rb_parent = __rb_parent;
6597d783 520 return 0;
1da177e4
LT
521}
522
1da177e4
LT
523void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
524 struct rb_node **rb_link, struct rb_node *rb_parent)
525{
d3737187
ML
526 /* Update tracking information for the gap following the new vma. */
527 if (vma->vm_next)
528 vma_gap_update(vma->vm_next);
529 else
530 mm->highest_vm_end = vma->vm_end;
531
532 /*
533 * vma->vm_prev wasn't known when we followed the rbtree to find the
534 * correct insertion point for that vma. As a result, we could not
535 * update the vma vm_rb parents rb_subtree_gap values on the way down.
536 * So, we first insert the vma with a zero rb_subtree_gap value
537 * (to be consistent with what we did on the way down), and then
538 * immediately update the gap to the correct value. Finally we
539 * rebalance the rbtree after all augmented values have been set.
540 */
1da177e4 541 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
d3737187
ML
542 vma->rb_subtree_gap = 0;
543 vma_gap_update(vma);
544 vma_rb_insert(vma, &mm->mm_rb);
1da177e4
LT
545}
546
cb8f488c 547static void __vma_link_file(struct vm_area_struct *vma)
1da177e4 548{
48aae425 549 struct file *file;
1da177e4
LT
550
551 file = vma->vm_file;
552 if (file) {
553 struct address_space *mapping = file->f_mapping;
554
555 if (vma->vm_flags & VM_DENYWRITE)
d3ac7f89 556 atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
1da177e4
LT
557 if (vma->vm_flags & VM_SHARED)
558 mapping->i_mmap_writable++;
559
560 flush_dcache_mmap_lock(mapping);
561 if (unlikely(vma->vm_flags & VM_NONLINEAR))
562 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
563 else
6b2dbba8 564 vma_interval_tree_insert(vma, &mapping->i_mmap);
1da177e4
LT
565 flush_dcache_mmap_unlock(mapping);
566 }
567}
568
569static void
570__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
571 struct vm_area_struct *prev, struct rb_node **rb_link,
572 struct rb_node *rb_parent)
573{
574 __vma_link_list(mm, vma, prev, rb_parent);
575 __vma_link_rb(mm, vma, rb_link, rb_parent);
1da177e4
LT
576}
577
578static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
579 struct vm_area_struct *prev, struct rb_node **rb_link,
580 struct rb_node *rb_parent)
581{
582 struct address_space *mapping = NULL;
583
584 if (vma->vm_file)
585 mapping = vma->vm_file->f_mapping;
586
97a89413 587 if (mapping)
3d48ae45 588 mutex_lock(&mapping->i_mmap_mutex);
1da177e4
LT
589
590 __vma_link(mm, vma, prev, rb_link, rb_parent);
591 __vma_link_file(vma);
592
1da177e4 593 if (mapping)
3d48ae45 594 mutex_unlock(&mapping->i_mmap_mutex);
1da177e4
LT
595
596 mm->map_count++;
597 validate_mm(mm);
598}
599
600/*
88f6b4c3 601 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
6b2dbba8 602 * mm's list and rbtree. It has already been inserted into the interval tree.
1da177e4 603 */
48aae425 604static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 605{
6597d783 606 struct vm_area_struct *prev;
48aae425 607 struct rb_node **rb_link, *rb_parent;
1da177e4 608
6597d783
HD
609 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
610 &prev, &rb_link, &rb_parent))
611 BUG();
1da177e4
LT
612 __vma_link(mm, vma, prev, rb_link, rb_parent);
613 mm->map_count++;
614}
615
616static inline void
617__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
618 struct vm_area_struct *prev)
619{
d3737187 620 struct vm_area_struct *next;
297c5eee 621
d3737187
ML
622 vma_rb_erase(vma, &mm->mm_rb);
623 prev->vm_next = next = vma->vm_next;
297c5eee
LT
624 if (next)
625 next->vm_prev = prev;
1da177e4
LT
626 if (mm->mmap_cache == vma)
627 mm->mmap_cache = prev;
628}
629
630/*
631 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
632 * is already present in an i_mmap tree without adjusting the tree.
633 * The following helper function should be used when such adjustments
634 * are necessary. The "insert" vma (if any) is to be inserted
635 * before we drop the necessary locks.
636 */
5beb4930 637int vma_adjust(struct vm_area_struct *vma, unsigned long start,
1da177e4
LT
638 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
639{
640 struct mm_struct *mm = vma->vm_mm;
641 struct vm_area_struct *next = vma->vm_next;
642 struct vm_area_struct *importer = NULL;
643 struct address_space *mapping = NULL;
6b2dbba8 644 struct rb_root *root = NULL;
012f1800 645 struct anon_vma *anon_vma = NULL;
1da177e4 646 struct file *file = vma->vm_file;
d3737187 647 bool start_changed = false, end_changed = false;
1da177e4
LT
648 long adjust_next = 0;
649 int remove_next = 0;
650
651 if (next && !insert) {
287d97ac
LT
652 struct vm_area_struct *exporter = NULL;
653
1da177e4
LT
654 if (end >= next->vm_end) {
655 /*
656 * vma expands, overlapping all the next, and
657 * perhaps the one after too (mprotect case 6).
658 */
659again: remove_next = 1 + (end > next->vm_end);
660 end = next->vm_end;
287d97ac 661 exporter = next;
1da177e4
LT
662 importer = vma;
663 } else if (end > next->vm_start) {
664 /*
665 * vma expands, overlapping part of the next:
666 * mprotect case 5 shifting the boundary up.
667 */
668 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
287d97ac 669 exporter = next;
1da177e4
LT
670 importer = vma;
671 } else if (end < vma->vm_end) {
672 /*
673 * vma shrinks, and !insert tells it's not
674 * split_vma inserting another: so it must be
675 * mprotect case 4 shifting the boundary down.
676 */
677 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
287d97ac 678 exporter = vma;
1da177e4
LT
679 importer = next;
680 }
1da177e4 681
5beb4930
RR
682 /*
683 * Easily overlooked: when mprotect shifts the boundary,
684 * make sure the expanding vma has anon_vma set if the
685 * shrinking vma had, to cover any anon pages imported.
686 */
287d97ac
LT
687 if (exporter && exporter->anon_vma && !importer->anon_vma) {
688 if (anon_vma_clone(importer, exporter))
5beb4930 689 return -ENOMEM;
287d97ac 690 importer->anon_vma = exporter->anon_vma;
5beb4930
RR
691 }
692 }
693
1da177e4
LT
694 if (file) {
695 mapping = file->f_mapping;
682968e0 696 if (!(vma->vm_flags & VM_NONLINEAR)) {
1da177e4 697 root = &mapping->i_mmap;
cbc91f71 698 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
682968e0
SD
699
700 if (adjust_next)
cbc91f71
SD
701 uprobe_munmap(next, next->vm_start,
702 next->vm_end);
682968e0
SD
703 }
704
3d48ae45 705 mutex_lock(&mapping->i_mmap_mutex);
1da177e4 706 if (insert) {
1da177e4 707 /*
6b2dbba8 708 * Put into interval tree now, so instantiated pages
1da177e4
LT
709 * are visible to arm/parisc __flush_dcache_page
710 * throughout; but we cannot insert into address
711 * space until vma start or end is updated.
712 */
713 __vma_link_file(insert);
714 }
715 }
716
94fcc585
AA
717 vma_adjust_trans_huge(vma, start, end, adjust_next);
718
bf181b9f
ML
719 anon_vma = vma->anon_vma;
720 if (!anon_vma && adjust_next)
721 anon_vma = next->anon_vma;
722 if (anon_vma) {
ca42b26a
ML
723 VM_BUG_ON(adjust_next && next->anon_vma &&
724 anon_vma != next->anon_vma);
012f1800 725 anon_vma_lock(anon_vma);
bf181b9f
ML
726 anon_vma_interval_tree_pre_update_vma(vma);
727 if (adjust_next)
728 anon_vma_interval_tree_pre_update_vma(next);
729 }
012f1800 730
1da177e4
LT
731 if (root) {
732 flush_dcache_mmap_lock(mapping);
6b2dbba8 733 vma_interval_tree_remove(vma, root);
1da177e4 734 if (adjust_next)
6b2dbba8 735 vma_interval_tree_remove(next, root);
1da177e4
LT
736 }
737
d3737187
ML
738 if (start != vma->vm_start) {
739 vma->vm_start = start;
740 start_changed = true;
741 }
742 if (end != vma->vm_end) {
743 vma->vm_end = end;
744 end_changed = true;
745 }
1da177e4
LT
746 vma->vm_pgoff = pgoff;
747 if (adjust_next) {
748 next->vm_start += adjust_next << PAGE_SHIFT;
749 next->vm_pgoff += adjust_next;
750 }
751
752 if (root) {
753 if (adjust_next)
6b2dbba8
ML
754 vma_interval_tree_insert(next, root);
755 vma_interval_tree_insert(vma, root);
1da177e4
LT
756 flush_dcache_mmap_unlock(mapping);
757 }
758
759 if (remove_next) {
760 /*
761 * vma_merge has merged next into vma, and needs
762 * us to remove next before dropping the locks.
763 */
764 __vma_unlink(mm, next, vma);
765 if (file)
766 __remove_shared_vm_struct(next, file, mapping);
1da177e4
LT
767 } else if (insert) {
768 /*
769 * split_vma has split insert from vma, and needs
770 * us to insert it before dropping the locks
771 * (it may either follow vma or precede it).
772 */
773 __insert_vm_struct(mm, insert);
d3737187
ML
774 } else {
775 if (start_changed)
776 vma_gap_update(vma);
777 if (end_changed) {
778 if (!next)
779 mm->highest_vm_end = end;
780 else if (!adjust_next)
781 vma_gap_update(next);
782 }
1da177e4
LT
783 }
784
bf181b9f
ML
785 if (anon_vma) {
786 anon_vma_interval_tree_post_update_vma(vma);
787 if (adjust_next)
788 anon_vma_interval_tree_post_update_vma(next);
012f1800 789 anon_vma_unlock(anon_vma);
bf181b9f 790 }
1da177e4 791 if (mapping)
3d48ae45 792 mutex_unlock(&mapping->i_mmap_mutex);
1da177e4 793
2b144498 794 if (root) {
7b2d81d4 795 uprobe_mmap(vma);
2b144498
SD
796
797 if (adjust_next)
7b2d81d4 798 uprobe_mmap(next);
2b144498
SD
799 }
800
1da177e4 801 if (remove_next) {
925d1c40 802 if (file) {
cbc91f71 803 uprobe_munmap(next, next->vm_start, next->vm_end);
1da177e4 804 fput(file);
925d1c40 805 }
5beb4930
RR
806 if (next->anon_vma)
807 anon_vma_merge(vma, next);
1da177e4 808 mm->map_count--;
f0be3d32 809 mpol_put(vma_policy(next));
1da177e4
LT
810 kmem_cache_free(vm_area_cachep, next);
811 /*
812 * In mprotect's case 6 (see comments on vma_merge),
813 * we must remove another next too. It would clutter
814 * up the code too much to do both in one go.
815 */
d3737187
ML
816 next = vma->vm_next;
817 if (remove_next == 2)
1da177e4 818 goto again;
d3737187
ML
819 else if (next)
820 vma_gap_update(next);
821 else
822 mm->highest_vm_end = end;
1da177e4 823 }
2b144498 824 if (insert && file)
7b2d81d4 825 uprobe_mmap(insert);
1da177e4
LT
826
827 validate_mm(mm);
5beb4930
RR
828
829 return 0;
1da177e4
LT
830}
831
832/*
833 * If the vma has a ->close operation then the driver probably needs to release
834 * per-vma resources, so we don't attempt to merge those.
835 */
1da177e4
LT
836static inline int is_mergeable_vma(struct vm_area_struct *vma,
837 struct file *file, unsigned long vm_flags)
838{
0b173bc4 839 if (vma->vm_flags ^ vm_flags)
1da177e4
LT
840 return 0;
841 if (vma->vm_file != file)
842 return 0;
843 if (vma->vm_ops && vma->vm_ops->close)
844 return 0;
845 return 1;
846}
847
848static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
965f55de
SL
849 struct anon_vma *anon_vma2,
850 struct vm_area_struct *vma)
1da177e4 851{
965f55de
SL
852 /*
853 * The list_is_singular() test is to avoid merging VMA cloned from
854 * parents. This can improve scalability caused by anon_vma lock.
855 */
856 if ((!anon_vma1 || !anon_vma2) && (!vma ||
857 list_is_singular(&vma->anon_vma_chain)))
858 return 1;
859 return anon_vma1 == anon_vma2;
1da177e4
LT
860}
861
862/*
863 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
864 * in front of (at a lower virtual address and file offset than) the vma.
865 *
866 * We cannot merge two vmas if they have differently assigned (non-NULL)
867 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
868 *
869 * We don't check here for the merged mmap wrapping around the end of pagecache
870 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
871 * wrap, nor mmaps which cover the final page at index -1UL.
872 */
873static int
874can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
875 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
876{
877 if (is_mergeable_vma(vma, file, vm_flags) &&
965f55de 878 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
1da177e4
LT
879 if (vma->vm_pgoff == vm_pgoff)
880 return 1;
881 }
882 return 0;
883}
884
885/*
886 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
887 * beyond (at a higher virtual address and file offset than) the vma.
888 *
889 * We cannot merge two vmas if they have differently assigned (non-NULL)
890 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
891 */
892static int
893can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
894 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
895{
896 if (is_mergeable_vma(vma, file, vm_flags) &&
965f55de 897 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
1da177e4
LT
898 pgoff_t vm_pglen;
899 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
900 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
901 return 1;
902 }
903 return 0;
904}
905
906/*
907 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
908 * whether that can be merged with its predecessor or its successor.
909 * Or both (it neatly fills a hole).
910 *
911 * In most cases - when called for mmap, brk or mremap - [addr,end) is
912 * certain not to be mapped by the time vma_merge is called; but when
913 * called for mprotect, it is certain to be already mapped (either at
914 * an offset within prev, or at the start of next), and the flags of
915 * this area are about to be changed to vm_flags - and the no-change
916 * case has already been eliminated.
917 *
918 * The following mprotect cases have to be considered, where AAAA is
919 * the area passed down from mprotect_fixup, never extending beyond one
920 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
921 *
922 * AAAA AAAA AAAA AAAA
923 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
924 * cannot merge might become might become might become
925 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
926 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
927 * mremap move: PPPPNNNNNNNN 8
928 * AAAA
929 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
930 * might become case 1 below case 2 below case 3 below
931 *
932 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
933 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
934 */
935struct vm_area_struct *vma_merge(struct mm_struct *mm,
936 struct vm_area_struct *prev, unsigned long addr,
937 unsigned long end, unsigned long vm_flags,
938 struct anon_vma *anon_vma, struct file *file,
939 pgoff_t pgoff, struct mempolicy *policy)
940{
941 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
942 struct vm_area_struct *area, *next;
5beb4930 943 int err;
1da177e4
LT
944
945 /*
946 * We later require that vma->vm_flags == vm_flags,
947 * so this tests vma->vm_flags & VM_SPECIAL, too.
948 */
949 if (vm_flags & VM_SPECIAL)
950 return NULL;
951
952 if (prev)
953 next = prev->vm_next;
954 else
955 next = mm->mmap;
956 area = next;
957 if (next && next->vm_end == end) /* cases 6, 7, 8 */
958 next = next->vm_next;
959
960 /*
961 * Can it merge with the predecessor?
962 */
963 if (prev && prev->vm_end == addr &&
964 mpol_equal(vma_policy(prev), policy) &&
965 can_vma_merge_after(prev, vm_flags,
966 anon_vma, file, pgoff)) {
967 /*
968 * OK, it can. Can we now merge in the successor as well?
969 */
970 if (next && end == next->vm_start &&
971 mpol_equal(policy, vma_policy(next)) &&
972 can_vma_merge_before(next, vm_flags,
973 anon_vma, file, pgoff+pglen) &&
974 is_mergeable_anon_vma(prev->anon_vma,
965f55de 975 next->anon_vma, NULL)) {
1da177e4 976 /* cases 1, 6 */
5beb4930 977 err = vma_adjust(prev, prev->vm_start,
1da177e4
LT
978 next->vm_end, prev->vm_pgoff, NULL);
979 } else /* cases 2, 5, 7 */
5beb4930 980 err = vma_adjust(prev, prev->vm_start,
1da177e4 981 end, prev->vm_pgoff, NULL);
5beb4930
RR
982 if (err)
983 return NULL;
b15d00b6 984 khugepaged_enter_vma_merge(prev);
1da177e4
LT
985 return prev;
986 }
987
988 /*
989 * Can this new request be merged in front of next?
990 */
991 if (next && end == next->vm_start &&
992 mpol_equal(policy, vma_policy(next)) &&
993 can_vma_merge_before(next, vm_flags,
994 anon_vma, file, pgoff+pglen)) {
995 if (prev && addr < prev->vm_end) /* case 4 */
5beb4930 996 err = vma_adjust(prev, prev->vm_start,
1da177e4
LT
997 addr, prev->vm_pgoff, NULL);
998 else /* cases 3, 8 */
5beb4930 999 err = vma_adjust(area, addr, next->vm_end,
1da177e4 1000 next->vm_pgoff - pglen, NULL);
5beb4930
RR
1001 if (err)
1002 return NULL;
b15d00b6 1003 khugepaged_enter_vma_merge(area);
1da177e4
LT
1004 return area;
1005 }
1006
1007 return NULL;
1008}
1009
d0e9fe17
LT
1010/*
1011 * Rough compatbility check to quickly see if it's even worth looking
1012 * at sharing an anon_vma.
1013 *
1014 * They need to have the same vm_file, and the flags can only differ
1015 * in things that mprotect may change.
1016 *
1017 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1018 * we can merge the two vma's. For example, we refuse to merge a vma if
1019 * there is a vm_ops->close() function, because that indicates that the
1020 * driver is doing some kind of reference counting. But that doesn't
1021 * really matter for the anon_vma sharing case.
1022 */
1023static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1024{
1025 return a->vm_end == b->vm_start &&
1026 mpol_equal(vma_policy(a), vma_policy(b)) &&
1027 a->vm_file == b->vm_file &&
1028 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
1029 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1030}
1031
1032/*
1033 * Do some basic sanity checking to see if we can re-use the anon_vma
1034 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1035 * the same as 'old', the other will be the new one that is trying
1036 * to share the anon_vma.
1037 *
1038 * NOTE! This runs with mm_sem held for reading, so it is possible that
1039 * the anon_vma of 'old' is concurrently in the process of being set up
1040 * by another page fault trying to merge _that_. But that's ok: if it
1041 * is being set up, that automatically means that it will be a singleton
1042 * acceptable for merging, so we can do all of this optimistically. But
1043 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
1044 *
1045 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1046 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1047 * is to return an anon_vma that is "complex" due to having gone through
1048 * a fork).
1049 *
1050 * We also make sure that the two vma's are compatible (adjacent,
1051 * and with the same memory policies). That's all stable, even with just
1052 * a read lock on the mm_sem.
1053 */
1054static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1055{
1056 if (anon_vma_compatible(a, b)) {
1057 struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
1058
1059 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1060 return anon_vma;
1061 }
1062 return NULL;
1063}
1064
1da177e4
LT
1065/*
1066 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1067 * neighbouring vmas for a suitable anon_vma, before it goes off
1068 * to allocate a new anon_vma. It checks because a repetitive
1069 * sequence of mprotects and faults may otherwise lead to distinct
1070 * anon_vmas being allocated, preventing vma merge in subsequent
1071 * mprotect.
1072 */
1073struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1074{
d0e9fe17 1075 struct anon_vma *anon_vma;
1da177e4 1076 struct vm_area_struct *near;
1da177e4
LT
1077
1078 near = vma->vm_next;
1079 if (!near)
1080 goto try_prev;
1081
d0e9fe17
LT
1082 anon_vma = reusable_anon_vma(near, vma, near);
1083 if (anon_vma)
1084 return anon_vma;
1da177e4 1085try_prev:
9be34c9d 1086 near = vma->vm_prev;
1da177e4
LT
1087 if (!near)
1088 goto none;
1089
d0e9fe17
LT
1090 anon_vma = reusable_anon_vma(near, near, vma);
1091 if (anon_vma)
1092 return anon_vma;
1da177e4
LT
1093none:
1094 /*
1095 * There's no absolute need to look only at touching neighbours:
1096 * we could search further afield for "compatible" anon_vmas.
1097 * But it would probably just be a waste of time searching,
1098 * or lead to too many vmas hanging off the same anon_vma.
1099 * We're trying to allow mprotect remerging later on,
1100 * not trying to minimize memory used for anon_vmas.
1101 */
1102 return NULL;
1103}
1104
1105#ifdef CONFIG_PROC_FS
ab50b8ed 1106void vm_stat_account(struct mm_struct *mm, unsigned long flags,
1da177e4
LT
1107 struct file *file, long pages)
1108{
1109 const unsigned long stack_flags
1110 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
1111
44de9d0c
HS
1112 mm->total_vm += pages;
1113
1da177e4
LT
1114 if (file) {
1115 mm->shared_vm += pages;
1116 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
1117 mm->exec_vm += pages;
1118 } else if (flags & stack_flags)
1119 mm->stack_vm += pages;
1da177e4
LT
1120}
1121#endif /* CONFIG_PROC_FS */
1122
40401530
AV
1123/*
1124 * If a hint addr is less than mmap_min_addr change hint to be as
1125 * low as possible but still greater than mmap_min_addr
1126 */
1127static inline unsigned long round_hint_to_min(unsigned long hint)
1128{
1129 hint &= PAGE_MASK;
1130 if (((void *)hint != NULL) &&
1131 (hint < mmap_min_addr))
1132 return PAGE_ALIGN(mmap_min_addr);
1133 return hint;
1134}
1135
1da177e4 1136/*
27f5de79 1137 * The caller must hold down_write(&current->mm->mmap_sem).
1da177e4
LT
1138 */
1139
e3fc629d 1140unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
1da177e4
LT
1141 unsigned long len, unsigned long prot,
1142 unsigned long flags, unsigned long pgoff)
1143{
1144 struct mm_struct * mm = current->mm;
1da177e4 1145 struct inode *inode;
ca16d140 1146 vm_flags_t vm_flags;
1da177e4 1147
1da177e4
LT
1148 /*
1149 * Does the application expect PROT_READ to imply PROT_EXEC?
1150 *
1151 * (the exception is when the underlying filesystem is noexec
1152 * mounted, in which case we dont add PROT_EXEC.)
1153 */
1154 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
d3ac7f89 1155 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
1da177e4
LT
1156 prot |= PROT_EXEC;
1157
1158 if (!len)
1159 return -EINVAL;
1160
7cd94146
EP
1161 if (!(flags & MAP_FIXED))
1162 addr = round_hint_to_min(addr);
1163
1da177e4
LT
1164 /* Careful about overflows.. */
1165 len = PAGE_ALIGN(len);
9206de95 1166 if (!len)
1da177e4
LT
1167 return -ENOMEM;
1168
1169 /* offset overflow? */
1170 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
1171 return -EOVERFLOW;
1172
1173 /* Too many mappings? */
1174 if (mm->map_count > sysctl_max_map_count)
1175 return -ENOMEM;
1176
1177 /* Obtain the address to map to. we verify (or select) it and ensure
1178 * that it represents a valid section of the address space.
1179 */
1180 addr = get_unmapped_area(file, addr, len, pgoff, flags);
1181 if (addr & ~PAGE_MASK)
1182 return addr;
1183
1184 /* Do simple checking here so the lower-level routines won't have
1185 * to. we assume access permissions have been handled by the open
1186 * of the memory object, so we don't do any here.
1187 */
1188 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
1189 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1190
cdf7b341 1191 if (flags & MAP_LOCKED)
1da177e4
LT
1192 if (!can_do_mlock())
1193 return -EPERM;
ba470de4 1194
1da177e4
LT
1195 /* mlock MCL_FUTURE? */
1196 if (vm_flags & VM_LOCKED) {
1197 unsigned long locked, lock_limit;
93ea1d0a
CW
1198 locked = len >> PAGE_SHIFT;
1199 locked += mm->locked_vm;
59e99e5b 1200 lock_limit = rlimit(RLIMIT_MEMLOCK);
93ea1d0a 1201 lock_limit >>= PAGE_SHIFT;
1da177e4
LT
1202 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1203 return -EAGAIN;
1204 }
1205
d3ac7f89 1206 inode = file ? file->f_path.dentry->d_inode : NULL;
1da177e4
LT
1207
1208 if (file) {
1209 switch (flags & MAP_TYPE) {
1210 case MAP_SHARED:
1211 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1212 return -EACCES;
1213
1214 /*
1215 * Make sure we don't allow writing to an append-only
1216 * file..
1217 */
1218 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1219 return -EACCES;
1220
1221 /*
1222 * Make sure there are no mandatory locks on the file.
1223 */
1224 if (locks_verify_locked(inode))
1225 return -EAGAIN;
1226
1227 vm_flags |= VM_SHARED | VM_MAYSHARE;
1228 if (!(file->f_mode & FMODE_WRITE))
1229 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1230
1231 /* fall through */
1232 case MAP_PRIVATE:
1233 if (!(file->f_mode & FMODE_READ))
1234 return -EACCES;
d3ac7f89 1235 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
80c5606c
LT
1236 if (vm_flags & VM_EXEC)
1237 return -EPERM;
1238 vm_flags &= ~VM_MAYEXEC;
1239 }
80c5606c
LT
1240
1241 if (!file->f_op || !file->f_op->mmap)
1242 return -ENODEV;
1da177e4
LT
1243 break;
1244
1245 default:
1246 return -EINVAL;
1247 }
1248 } else {
1249 switch (flags & MAP_TYPE) {
1250 case MAP_SHARED:
ce363942
TH
1251 /*
1252 * Ignore pgoff.
1253 */
1254 pgoff = 0;
1da177e4
LT
1255 vm_flags |= VM_SHARED | VM_MAYSHARE;
1256 break;
1257 case MAP_PRIVATE:
1258 /*
1259 * Set pgoff according to addr for anon_vma.
1260 */
1261 pgoff = addr >> PAGE_SHIFT;
1262 break;
1263 default:
1264 return -EINVAL;
1265 }
1266 }
1267
5a6fe125 1268 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
0165ab44 1269}
6be5ceb0 1270
66f0dc48
HD
1271SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1272 unsigned long, prot, unsigned long, flags,
1273 unsigned long, fd, unsigned long, pgoff)
1274{
1275 struct file *file = NULL;
1276 unsigned long retval = -EBADF;
1277
1278 if (!(flags & MAP_ANONYMOUS)) {
120a795d 1279 audit_mmap_fd(fd, flags);
66f0dc48
HD
1280 if (unlikely(flags & MAP_HUGETLB))
1281 return -EINVAL;
1282 file = fget(fd);
1283 if (!file)
1284 goto out;
1285 } else if (flags & MAP_HUGETLB) {
1286 struct user_struct *user = NULL;
1287 /*
1288 * VM_NORESERVE is used because the reservations will be
1289 * taken when vm_ops->mmap() is called
1290 * A dummy user value is used because we are not locking
1291 * memory so no accounting is necessary
1292 */
40716e29 1293 file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
42d7395f
AK
1294 VM_NORESERVE,
1295 &user, HUGETLB_ANONHUGE_INODE,
1296 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
66f0dc48
HD
1297 if (IS_ERR(file))
1298 return PTR_ERR(file);
1299 }
1300
1301 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1302
eb36c587 1303 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
66f0dc48
HD
1304 if (file)
1305 fput(file);
1306out:
1307 return retval;
1308}
1309
a4679373
CH
1310#ifdef __ARCH_WANT_SYS_OLD_MMAP
1311struct mmap_arg_struct {
1312 unsigned long addr;
1313 unsigned long len;
1314 unsigned long prot;
1315 unsigned long flags;
1316 unsigned long fd;
1317 unsigned long offset;
1318};
1319
1320SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1321{
1322 struct mmap_arg_struct a;
1323
1324 if (copy_from_user(&a, arg, sizeof(a)))
1325 return -EFAULT;
1326 if (a.offset & ~PAGE_MASK)
1327 return -EINVAL;
1328
1329 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1330 a.offset >> PAGE_SHIFT);
1331}
1332#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1333
4e950f6f
AD
1334/*
1335 * Some shared mappigns will want the pages marked read-only
1336 * to track write events. If so, we'll downgrade vm_page_prot
1337 * to the private version (using protection_map[] without the
1338 * VM_SHARED bit).
1339 */
1340int vma_wants_writenotify(struct vm_area_struct *vma)
1341{
ca16d140 1342 vm_flags_t vm_flags = vma->vm_flags;
4e950f6f
AD
1343
1344 /* If it was private or non-writable, the write bit is already clear */
1345 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1346 return 0;
1347
1348 /* The backer wishes to know when pages are first written to? */
1349 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1350 return 1;
1351
1352 /* The open routine did something to the protections already? */
1353 if (pgprot_val(vma->vm_page_prot) !=
3ed75eb8 1354 pgprot_val(vm_get_page_prot(vm_flags)))
4e950f6f
AD
1355 return 0;
1356
1357 /* Specialty mapping? */
4b6e1e37 1358 if (vm_flags & VM_PFNMAP)
4e950f6f
AD
1359 return 0;
1360
1361 /* Can the mapping track the dirty pages? */
1362 return vma->vm_file && vma->vm_file->f_mapping &&
1363 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1364}
1365
fc8744ad
LT
1366/*
1367 * We account for memory if it's a private writeable mapping,
5a6fe125 1368 * not hugepages and VM_NORESERVE wasn't set.
fc8744ad 1369 */
ca16d140 1370static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
fc8744ad 1371{
5a6fe125
MG
1372 /*
1373 * hugetlb has its own accounting separate from the core VM
1374 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1375 */
1376 if (file && is_file_hugepages(file))
1377 return 0;
1378
fc8744ad
LT
1379 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1380}
1381
0165ab44
MS
1382unsigned long mmap_region(struct file *file, unsigned long addr,
1383 unsigned long len, unsigned long flags,
ca16d140 1384 vm_flags_t vm_flags, unsigned long pgoff)
0165ab44
MS
1385{
1386 struct mm_struct *mm = current->mm;
1387 struct vm_area_struct *vma, *prev;
1388 int correct_wcount = 0;
1389 int error;
1390 struct rb_node **rb_link, *rb_parent;
1391 unsigned long charged = 0;
1392 struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
1393
1da177e4
LT
1394 /* Clear old maps */
1395 error = -ENOMEM;
1396munmap_back:
6597d783 1397 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
1da177e4
LT
1398 if (do_munmap(mm, addr, len))
1399 return -ENOMEM;
1400 goto munmap_back;
1401 }
1402
1403 /* Check against address space limit. */
119f657c 1404 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1da177e4
LT
1405 return -ENOMEM;
1406
fc8744ad
LT
1407 /*
1408 * Set 'VM_NORESERVE' if we should not account for the
5a6fe125 1409 * memory use of this mapping.
fc8744ad 1410 */
5a6fe125
MG
1411 if ((flags & MAP_NORESERVE)) {
1412 /* We honor MAP_NORESERVE if allowed to overcommit */
1413 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1414 vm_flags |= VM_NORESERVE;
1415
1416 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1417 if (file && is_file_hugepages(file))
1418 vm_flags |= VM_NORESERVE;
1419 }
cdfd4325 1420
fc8744ad
LT
1421 /*
1422 * Private writable mapping: check memory availability
1423 */
5a6fe125 1424 if (accountable_mapping(file, vm_flags)) {
fc8744ad 1425 charged = len >> PAGE_SHIFT;
191c5424 1426 if (security_vm_enough_memory_mm(mm, charged))
fc8744ad
LT
1427 return -ENOMEM;
1428 vm_flags |= VM_ACCOUNT;
1da177e4
LT
1429 }
1430
1431 /*
de33c8db 1432 * Can we just expand an old mapping?
1da177e4 1433 */
de33c8db
LT
1434 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1435 if (vma)
1436 goto out;
1da177e4
LT
1437
1438 /*
1439 * Determine the object being mapped and call the appropriate
1440 * specific mapper. the address has already been validated, but
1441 * not unmapped, but the maps are removed from the list.
1442 */
c5e3b83e 1443 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1da177e4
LT
1444 if (!vma) {
1445 error = -ENOMEM;
1446 goto unacct_error;
1447 }
1da177e4
LT
1448
1449 vma->vm_mm = mm;
1450 vma->vm_start = addr;
1451 vma->vm_end = addr + len;
1452 vma->vm_flags = vm_flags;
3ed75eb8 1453 vma->vm_page_prot = vm_get_page_prot(vm_flags);
1da177e4 1454 vma->vm_pgoff = pgoff;
5beb4930 1455 INIT_LIST_HEAD(&vma->anon_vma_chain);
1da177e4 1456
ce8fea7a
HD
1457 error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1458
1da177e4 1459 if (file) {
1da177e4
LT
1460 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1461 goto free_vma;
1462 if (vm_flags & VM_DENYWRITE) {
1463 error = deny_write_access(file);
1464 if (error)
1465 goto free_vma;
1466 correct_wcount = 1;
1467 }
cb0942b8 1468 vma->vm_file = get_file(file);
1da177e4
LT
1469 error = file->f_op->mmap(file, vma);
1470 if (error)
1471 goto unmap_and_free_vma;
f8dbf0a7
HS
1472
1473 /* Can addr have changed??
1474 *
1475 * Answer: Yes, several device drivers can do it in their
1476 * f_op->mmap method. -DaveM
1477 */
1478 addr = vma->vm_start;
1479 pgoff = vma->vm_pgoff;
1480 vm_flags = vma->vm_flags;
1da177e4 1481 } else if (vm_flags & VM_SHARED) {
835ee797
AV
1482 if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
1483 goto free_vma;
1da177e4
LT
1484 error = shmem_zero_setup(vma);
1485 if (error)
1486 goto free_vma;
1487 }
1488
c9d0bf24
MD
1489 if (vma_wants_writenotify(vma)) {
1490 pgprot_t pprot = vma->vm_page_prot;
1491
1492 /* Can vma->vm_page_prot have changed??
1493 *
1494 * Answer: Yes, drivers may have changed it in their
1495 * f_op->mmap method.
1496 *
1497 * Ensures that vmas marked as uncached stay that way.
1498 */
1ddd439e 1499 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
c9d0bf24
MD
1500 if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1501 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1502 }
d08b3851 1503
de33c8db
LT
1504 vma_link(mm, vma, prev, rb_link, rb_parent);
1505 file = vma->vm_file;
4d3d5b41
ON
1506
1507 /* Once vma denies write, undo our temporary denial count */
1508 if (correct_wcount)
1509 atomic_inc(&inode->i_writecount);
1510out:
cdd6c482 1511 perf_event_mmap(vma);
0a4a9391 1512
ab50b8ed 1513 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1da177e4 1514 if (vm_flags & VM_LOCKED) {
06f9d8c2
KM
1515 if (!mlock_vma_pages_range(vma, addr, addr + len))
1516 mm->locked_vm += (len >> PAGE_SHIFT);
ba470de4 1517 } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
54cb8821 1518 make_pages_present(addr, addr + len);
2b144498 1519
c7a3a88c
ON
1520 if (file)
1521 uprobe_mmap(vma);
2b144498 1522
1da177e4
LT
1523 return addr;
1524
1525unmap_and_free_vma:
1526 if (correct_wcount)
1527 atomic_inc(&inode->i_writecount);
1528 vma->vm_file = NULL;
1529 fput(file);
1530
1531 /* Undo any partial mapping done by a device driver. */
e0da382c
HD
1532 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1533 charged = 0;
1da177e4
LT
1534free_vma:
1535 kmem_cache_free(vm_area_cachep, vma);
1536unacct_error:
1537 if (charged)
1538 vm_unacct_memory(charged);
1539 return error;
1540}
1541
db4fbfb9
ML
1542unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1543{
1544 /*
1545 * We implement the search by looking for an rbtree node that
1546 * immediately follows a suitable gap. That is,
1547 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1548 * - gap_end = vma->vm_start >= info->low_limit + length;
1549 * - gap_end - gap_start >= length
1550 */
1551
1552 struct mm_struct *mm = current->mm;
1553 struct vm_area_struct *vma;
1554 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1555
1556 /* Adjust search length to account for worst case alignment overhead */
1557 length = info->length + info->align_mask;
1558 if (length < info->length)
1559 return -ENOMEM;
1560
1561 /* Adjust search limits by the desired length */
1562 if (info->high_limit < length)
1563 return -ENOMEM;
1564 high_limit = info->high_limit - length;
1565
1566 if (info->low_limit > high_limit)
1567 return -ENOMEM;
1568 low_limit = info->low_limit + length;
1569
1570 /* Check if rbtree root looks promising */
1571 if (RB_EMPTY_ROOT(&mm->mm_rb))
1572 goto check_highest;
1573 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1574 if (vma->rb_subtree_gap < length)
1575 goto check_highest;
1576
1577 while (true) {
1578 /* Visit left subtree if it looks promising */
1579 gap_end = vma->vm_start;
1580 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1581 struct vm_area_struct *left =
1582 rb_entry(vma->vm_rb.rb_left,
1583 struct vm_area_struct, vm_rb);
1584 if (left->rb_subtree_gap >= length) {
1585 vma = left;
1586 continue;
1587 }
1588 }
1589
1590 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1591check_current:
1592 /* Check if current node has a suitable gap */
1593 if (gap_start > high_limit)
1594 return -ENOMEM;
1595 if (gap_end >= low_limit && gap_end - gap_start >= length)
1596 goto found;
1597
1598 /* Visit right subtree if it looks promising */
1599 if (vma->vm_rb.rb_right) {
1600 struct vm_area_struct *right =
1601 rb_entry(vma->vm_rb.rb_right,
1602 struct vm_area_struct, vm_rb);
1603 if (right->rb_subtree_gap >= length) {
1604 vma = right;
1605 continue;
1606 }
1607 }
1608
1609 /* Go back up the rbtree to find next candidate node */
1610 while (true) {
1611 struct rb_node *prev = &vma->vm_rb;
1612 if (!rb_parent(prev))
1613 goto check_highest;
1614 vma = rb_entry(rb_parent(prev),
1615 struct vm_area_struct, vm_rb);
1616 if (prev == vma->vm_rb.rb_left) {
1617 gap_start = vma->vm_prev->vm_end;
1618 gap_end = vma->vm_start;
1619 goto check_current;
1620 }
1621 }
1622 }
1623
1624check_highest:
1625 /* Check highest gap, which does not precede any rbtree node */
1626 gap_start = mm->highest_vm_end;
1627 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1628 if (gap_start > high_limit)
1629 return -ENOMEM;
1630
1631found:
1632 /* We found a suitable gap. Clip it with the original low_limit. */
1633 if (gap_start < info->low_limit)
1634 gap_start = info->low_limit;
1635
1636 /* Adjust gap address to the desired alignment */
1637 gap_start += (info->align_offset - gap_start) & info->align_mask;
1638
1639 VM_BUG_ON(gap_start + info->length > info->high_limit);
1640 VM_BUG_ON(gap_start + info->length > gap_end);
1641 return gap_start;
1642}
1643
1644unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1645{
1646 struct mm_struct *mm = current->mm;
1647 struct vm_area_struct *vma;
1648 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1649
1650 /* Adjust search length to account for worst case alignment overhead */
1651 length = info->length + info->align_mask;
1652 if (length < info->length)
1653 return -ENOMEM;
1654
1655 /*
1656 * Adjust search limits by the desired length.
1657 * See implementation comment at top of unmapped_area().
1658 */
1659 gap_end = info->high_limit;
1660 if (gap_end < length)
1661 return -ENOMEM;
1662 high_limit = gap_end - length;
1663
1664 if (info->low_limit > high_limit)
1665 return -ENOMEM;
1666 low_limit = info->low_limit + length;
1667
1668 /* Check highest gap, which does not precede any rbtree node */
1669 gap_start = mm->highest_vm_end;
1670 if (gap_start <= high_limit)
1671 goto found_highest;
1672
1673 /* Check if rbtree root looks promising */
1674 if (RB_EMPTY_ROOT(&mm->mm_rb))
1675 return -ENOMEM;
1676 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1677 if (vma->rb_subtree_gap < length)
1678 return -ENOMEM;
1679
1680 while (true) {
1681 /* Visit right subtree if it looks promising */
1682 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1683 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1684 struct vm_area_struct *right =
1685 rb_entry(vma->vm_rb.rb_right,
1686 struct vm_area_struct, vm_rb);
1687 if (right->rb_subtree_gap >= length) {
1688 vma = right;
1689 continue;
1690 }
1691 }
1692
1693check_current:
1694 /* Check if current node has a suitable gap */
1695 gap_end = vma->vm_start;
1696 if (gap_end < low_limit)
1697 return -ENOMEM;
1698 if (gap_start <= high_limit && gap_end - gap_start >= length)
1699 goto found;
1700
1701 /* Visit left subtree if it looks promising */
1702 if (vma->vm_rb.rb_left) {
1703 struct vm_area_struct *left =
1704 rb_entry(vma->vm_rb.rb_left,
1705 struct vm_area_struct, vm_rb);
1706 if (left->rb_subtree_gap >= length) {
1707 vma = left;
1708 continue;
1709 }
1710 }
1711
1712 /* Go back up the rbtree to find next candidate node */
1713 while (true) {
1714 struct rb_node *prev = &vma->vm_rb;
1715 if (!rb_parent(prev))
1716 return -ENOMEM;
1717 vma = rb_entry(rb_parent(prev),
1718 struct vm_area_struct, vm_rb);
1719 if (prev == vma->vm_rb.rb_right) {
1720 gap_start = vma->vm_prev ?
1721 vma->vm_prev->vm_end : 0;
1722 goto check_current;
1723 }
1724 }
1725 }
1726
1727found:
1728 /* We found a suitable gap. Clip it with the original high_limit. */
1729 if (gap_end > info->high_limit)
1730 gap_end = info->high_limit;
1731
1732found_highest:
1733 /* Compute highest gap address at the desired alignment */
1734 gap_end -= info->length;
1735 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1736
1737 VM_BUG_ON(gap_end < info->low_limit);
1738 VM_BUG_ON(gap_end < gap_start);
1739 return gap_end;
1740}
1741
1da177e4
LT
1742/* Get an address range which is currently unmapped.
1743 * For shmat() with addr=0.
1744 *
1745 * Ugly calling convention alert:
1746 * Return value with the low bits set means error value,
1747 * ie
1748 * if (ret & ~PAGE_MASK)
1749 * error = ret;
1750 *
1751 * This function "knows" that -ENOMEM has the bits set.
1752 */
1753#ifndef HAVE_ARCH_UNMAPPED_AREA
1754unsigned long
1755arch_get_unmapped_area(struct file *filp, unsigned long addr,
1756 unsigned long len, unsigned long pgoff, unsigned long flags)
1757{
1758 struct mm_struct *mm = current->mm;
1759 struct vm_area_struct *vma;
db4fbfb9 1760 struct vm_unmapped_area_info info;
1da177e4
LT
1761
1762 if (len > TASK_SIZE)
1763 return -ENOMEM;
1764
06abdfb4
BH
1765 if (flags & MAP_FIXED)
1766 return addr;
1767
1da177e4
LT
1768 if (addr) {
1769 addr = PAGE_ALIGN(addr);
1770 vma = find_vma(mm, addr);
1771 if (TASK_SIZE - len >= addr &&
1772 (!vma || addr + len <= vma->vm_start))
1773 return addr;
1774 }
1da177e4 1775
db4fbfb9
ML
1776 info.flags = 0;
1777 info.length = len;
1778 info.low_limit = TASK_UNMAPPED_BASE;
1779 info.high_limit = TASK_SIZE;
1780 info.align_mask = 0;
1781 return vm_unmapped_area(&info);
1da177e4
LT
1782}
1783#endif
1784
1363c3cd 1785void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1da177e4
LT
1786{
1787 /*
1788 * Is this a new hole at the lowest possible address?
1789 */
f44d2198 1790 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
1363c3cd 1791 mm->free_area_cache = addr;
1da177e4
LT
1792}
1793
1794/*
1795 * This mmap-allocator allocates new areas top-down from below the
1796 * stack's low limit (the base):
1797 */
1798#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1799unsigned long
1800arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1801 const unsigned long len, const unsigned long pgoff,
1802 const unsigned long flags)
1803{
1804 struct vm_area_struct *vma;
1805 struct mm_struct *mm = current->mm;
db4fbfb9
ML
1806 unsigned long addr = addr0;
1807 struct vm_unmapped_area_info info;
1da177e4
LT
1808
1809 /* requested length too big for entire address space */
1810 if (len > TASK_SIZE)
1811 return -ENOMEM;
1812
06abdfb4
BH
1813 if (flags & MAP_FIXED)
1814 return addr;
1815
1da177e4
LT
1816 /* requesting a specific address */
1817 if (addr) {
1818 addr = PAGE_ALIGN(addr);
1819 vma = find_vma(mm, addr);
1820 if (TASK_SIZE - len >= addr &&
1821 (!vma || addr + len <= vma->vm_start))
1822 return addr;
1823 }
1824
db4fbfb9
ML
1825 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1826 info.length = len;
1827 info.low_limit = PAGE_SIZE;
1828 info.high_limit = mm->mmap_base;
1829 info.align_mask = 0;
1830 addr = vm_unmapped_area(&info);
b716ad95 1831
1da177e4
LT
1832 /*
1833 * A failed mmap() very likely causes application failure,
1834 * so fall back to the bottom-up function here. This scenario
1835 * can happen with large stack limits and large mmap()
1836 * allocations.
1837 */
db4fbfb9
ML
1838 if (addr & ~PAGE_MASK) {
1839 VM_BUG_ON(addr != -ENOMEM);
1840 info.flags = 0;
1841 info.low_limit = TASK_UNMAPPED_BASE;
1842 info.high_limit = TASK_SIZE;
1843 addr = vm_unmapped_area(&info);
1844 }
1da177e4
LT
1845
1846 return addr;
1847}
1848#endif
1849
1363c3cd 1850void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
1da177e4
LT
1851{
1852 /*
1853 * Is this a new hole at the highest possible address?
1854 */
1363c3cd
WW
1855 if (addr > mm->free_area_cache)
1856 mm->free_area_cache = addr;
1da177e4
LT
1857
1858 /* dont allow allocations above current base */
1363c3cd
WW
1859 if (mm->free_area_cache > mm->mmap_base)
1860 mm->free_area_cache = mm->mmap_base;
1da177e4
LT
1861}
1862
1863unsigned long
1864get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1865 unsigned long pgoff, unsigned long flags)
1866{
06abdfb4
BH
1867 unsigned long (*get_area)(struct file *, unsigned long,
1868 unsigned long, unsigned long, unsigned long);
1869
9206de95
AV
1870 unsigned long error = arch_mmap_check(addr, len, flags);
1871 if (error)
1872 return error;
1873
1874 /* Careful about overflows.. */
1875 if (len > TASK_SIZE)
1876 return -ENOMEM;
1877
06abdfb4
BH
1878 get_area = current->mm->get_unmapped_area;
1879 if (file && file->f_op && file->f_op->get_unmapped_area)
1880 get_area = file->f_op->get_unmapped_area;
1881 addr = get_area(file, addr, len, pgoff, flags);
1882 if (IS_ERR_VALUE(addr))
1883 return addr;
1da177e4 1884
07ab67c8
LT
1885 if (addr > TASK_SIZE - len)
1886 return -ENOMEM;
1887 if (addr & ~PAGE_MASK)
1888 return -EINVAL;
06abdfb4 1889
9ac4ed4b
AV
1890 addr = arch_rebalance_pgtables(addr, len);
1891 error = security_mmap_addr(addr);
1892 return error ? error : addr;
1da177e4
LT
1893}
1894
1895EXPORT_SYMBOL(get_unmapped_area);
1896
1897/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
48aae425 1898struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
1da177e4
LT
1899{
1900 struct vm_area_struct *vma = NULL;
1901
841e31e5
RM
1902 if (WARN_ON_ONCE(!mm)) /* Remove this in linux-3.6 */
1903 return NULL;
1904
1905 /* Check the cache first. */
1906 /* (Cache hit rate is typically around 35%.) */
1907 vma = mm->mmap_cache;
1908 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1909 struct rb_node *rb_node;
1910
1911 rb_node = mm->mm_rb.rb_node;
1912 vma = NULL;
1913
1914 while (rb_node) {
1915 struct vm_area_struct *vma_tmp;
1916
1917 vma_tmp = rb_entry(rb_node,
1918 struct vm_area_struct, vm_rb);
1919
1920 if (vma_tmp->vm_end > addr) {
1921 vma = vma_tmp;
1922 if (vma_tmp->vm_start <= addr)
1923 break;
1924 rb_node = rb_node->rb_left;
1925 } else
1926 rb_node = rb_node->rb_right;
1da177e4 1927 }
841e31e5
RM
1928 if (vma)
1929 mm->mmap_cache = vma;
1da177e4
LT
1930 }
1931 return vma;
1932}
1933
1934EXPORT_SYMBOL(find_vma);
1935
6bd4837d
KM
1936/*
1937 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
6bd4837d 1938 */
1da177e4
LT
1939struct vm_area_struct *
1940find_vma_prev(struct mm_struct *mm, unsigned long addr,
1941 struct vm_area_struct **pprev)
1942{
6bd4837d 1943 struct vm_area_struct *vma;
1da177e4 1944
6bd4837d 1945 vma = find_vma(mm, addr);
83cd904d
MP
1946 if (vma) {
1947 *pprev = vma->vm_prev;
1948 } else {
1949 struct rb_node *rb_node = mm->mm_rb.rb_node;
1950 *pprev = NULL;
1951 while (rb_node) {
1952 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1953 rb_node = rb_node->rb_right;
1954 }
1955 }
6bd4837d 1956 return vma;
1da177e4
LT
1957}
1958
1959/*
1960 * Verify that the stack growth is acceptable and
1961 * update accounting. This is shared with both the
1962 * grow-up and grow-down cases.
1963 */
48aae425 1964static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
1da177e4
LT
1965{
1966 struct mm_struct *mm = vma->vm_mm;
1967 struct rlimit *rlim = current->signal->rlim;
0d59a01b 1968 unsigned long new_start;
1da177e4
LT
1969
1970 /* address space limit tests */
119f657c 1971 if (!may_expand_vm(mm, grow))
1da177e4
LT
1972 return -ENOMEM;
1973
1974 /* Stack limit test */
59e99e5b 1975 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
1da177e4
LT
1976 return -ENOMEM;
1977
1978 /* mlock limit tests */
1979 if (vma->vm_flags & VM_LOCKED) {
1980 unsigned long locked;
1981 unsigned long limit;
1982 locked = mm->locked_vm + grow;
59e99e5b
JS
1983 limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
1984 limit >>= PAGE_SHIFT;
1da177e4
LT
1985 if (locked > limit && !capable(CAP_IPC_LOCK))
1986 return -ENOMEM;
1987 }
1988
0d59a01b
AL
1989 /* Check to ensure the stack will not grow into a hugetlb-only region */
1990 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1991 vma->vm_end - size;
1992 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1993 return -EFAULT;
1994
1da177e4
LT
1995 /*
1996 * Overcommit.. This must be the final test, as it will
1997 * update security statistics.
1998 */
05fa199d 1999 if (security_vm_enough_memory_mm(mm, grow))
1da177e4
LT
2000 return -ENOMEM;
2001
2002 /* Ok, everything looks good - let it rip */
1da177e4
LT
2003 if (vma->vm_flags & VM_LOCKED)
2004 mm->locked_vm += grow;
ab50b8ed 2005 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
1da177e4
LT
2006 return 0;
2007}
2008
46dea3d0 2009#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
1da177e4 2010/*
46dea3d0
HD
2011 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2012 * vma is the last one with address > vma->vm_end. Have to extend vma.
1da177e4 2013 */
46dea3d0 2014int expand_upwards(struct vm_area_struct *vma, unsigned long address)
1da177e4
LT
2015{
2016 int error;
2017
2018 if (!(vma->vm_flags & VM_GROWSUP))
2019 return -EFAULT;
2020
2021 /*
2022 * We must make sure the anon_vma is allocated
2023 * so that the anon_vma locking is not a noop.
2024 */
2025 if (unlikely(anon_vma_prepare(vma)))
2026 return -ENOMEM;
bb4a340e 2027 vma_lock_anon_vma(vma);
1da177e4
LT
2028
2029 /*
2030 * vma->vm_start/vm_end cannot change under us because the caller
2031 * is required to hold the mmap_sem in read mode. We need the
2032 * anon_vma lock to serialize against concurrent expand_stacks.
06b32f3a 2033 * Also guard against wrapping around to address 0.
1da177e4 2034 */
06b32f3a
HD
2035 if (address < PAGE_ALIGN(address+4))
2036 address = PAGE_ALIGN(address+4);
2037 else {
bb4a340e 2038 vma_unlock_anon_vma(vma);
06b32f3a
HD
2039 return -ENOMEM;
2040 }
1da177e4
LT
2041 error = 0;
2042
2043 /* Somebody else might have raced and expanded it already */
2044 if (address > vma->vm_end) {
2045 unsigned long size, grow;
2046
2047 size = address - vma->vm_start;
2048 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2049
42c36f63
HD
2050 error = -ENOMEM;
2051 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2052 error = acct_stack_growth(vma, size, grow);
2053 if (!error) {
bf181b9f 2054 anon_vma_interval_tree_pre_update_vma(vma);
42c36f63 2055 vma->vm_end = address;
bf181b9f 2056 anon_vma_interval_tree_post_update_vma(vma);
d3737187
ML
2057 if (vma->vm_next)
2058 vma_gap_update(vma->vm_next);
2059 else
2060 vma->vm_mm->highest_vm_end = address;
42c36f63
HD
2061 perf_event_mmap(vma);
2062 }
3af9e859 2063 }
1da177e4 2064 }
bb4a340e 2065 vma_unlock_anon_vma(vma);
b15d00b6 2066 khugepaged_enter_vma_merge(vma);
ed8ea815 2067 validate_mm(vma->vm_mm);
1da177e4
LT
2068 return error;
2069}
46dea3d0
HD
2070#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2071
1da177e4
LT
2072/*
2073 * vma is the first one with address < vma->vm_start. Have to extend vma.
2074 */
d05f3169 2075int expand_downwards(struct vm_area_struct *vma,
b6a2fea3 2076 unsigned long address)
1da177e4
LT
2077{
2078 int error;
2079
2080 /*
2081 * We must make sure the anon_vma is allocated
2082 * so that the anon_vma locking is not a noop.
2083 */
2084 if (unlikely(anon_vma_prepare(vma)))
2085 return -ENOMEM;
8869477a
EP
2086
2087 address &= PAGE_MASK;
e5467859 2088 error = security_mmap_addr(address);
8869477a
EP
2089 if (error)
2090 return error;
2091
bb4a340e 2092 vma_lock_anon_vma(vma);
1da177e4
LT
2093
2094 /*
2095 * vma->vm_start/vm_end cannot change under us because the caller
2096 * is required to hold the mmap_sem in read mode. We need the
2097 * anon_vma lock to serialize against concurrent expand_stacks.
2098 */
1da177e4
LT
2099
2100 /* Somebody else might have raced and expanded it already */
2101 if (address < vma->vm_start) {
2102 unsigned long size, grow;
2103
2104 size = vma->vm_end - address;
2105 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2106
a626ca6a
LT
2107 error = -ENOMEM;
2108 if (grow <= vma->vm_pgoff) {
2109 error = acct_stack_growth(vma, size, grow);
2110 if (!error) {
bf181b9f 2111 anon_vma_interval_tree_pre_update_vma(vma);
a626ca6a
LT
2112 vma->vm_start = address;
2113 vma->vm_pgoff -= grow;
bf181b9f 2114 anon_vma_interval_tree_post_update_vma(vma);
d3737187 2115 vma_gap_update(vma);
a626ca6a
LT
2116 perf_event_mmap(vma);
2117 }
1da177e4
LT
2118 }
2119 }
bb4a340e 2120 vma_unlock_anon_vma(vma);
b15d00b6 2121 khugepaged_enter_vma_merge(vma);
ed8ea815 2122 validate_mm(vma->vm_mm);
1da177e4
LT
2123 return error;
2124}
2125
b6a2fea3
OW
2126#ifdef CONFIG_STACK_GROWSUP
2127int expand_stack(struct vm_area_struct *vma, unsigned long address)
2128{
2129 return expand_upwards(vma, address);
2130}
2131
2132struct vm_area_struct *
2133find_extend_vma(struct mm_struct *mm, unsigned long addr)
2134{
2135 struct vm_area_struct *vma, *prev;
2136
2137 addr &= PAGE_MASK;
2138 vma = find_vma_prev(mm, addr, &prev);
2139 if (vma && (vma->vm_start <= addr))
2140 return vma;
1c127185 2141 if (!prev || expand_stack(prev, addr))
b6a2fea3 2142 return NULL;
ba470de4 2143 if (prev->vm_flags & VM_LOCKED) {
c58267c3 2144 mlock_vma_pages_range(prev, addr, prev->vm_end);
ba470de4 2145 }
b6a2fea3
OW
2146 return prev;
2147}
2148#else
2149int expand_stack(struct vm_area_struct *vma, unsigned long address)
2150{
2151 return expand_downwards(vma, address);
2152}
2153
1da177e4
LT
2154struct vm_area_struct *
2155find_extend_vma(struct mm_struct * mm, unsigned long addr)
2156{
2157 struct vm_area_struct * vma;
2158 unsigned long start;
2159
2160 addr &= PAGE_MASK;
2161 vma = find_vma(mm,addr);
2162 if (!vma)
2163 return NULL;
2164 if (vma->vm_start <= addr)
2165 return vma;
2166 if (!(vma->vm_flags & VM_GROWSDOWN))
2167 return NULL;
2168 start = vma->vm_start;
2169 if (expand_stack(vma, addr))
2170 return NULL;
ba470de4 2171 if (vma->vm_flags & VM_LOCKED) {
c58267c3 2172 mlock_vma_pages_range(vma, addr, start);
ba470de4 2173 }
1da177e4
LT
2174 return vma;
2175}
2176#endif
2177
1da177e4 2178/*
2c0b3814 2179 * Ok - we have the memory areas we should free on the vma list,
1da177e4 2180 * so release them, and do the vma updates.
2c0b3814
HD
2181 *
2182 * Called with the mm semaphore held.
1da177e4 2183 */
2c0b3814 2184static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 2185{
4f74d2c8
LT
2186 unsigned long nr_accounted = 0;
2187
365e9c87
HD
2188 /* Update high watermark before we lower total_vm */
2189 update_hiwater_vm(mm);
1da177e4 2190 do {
2c0b3814
HD
2191 long nrpages = vma_pages(vma);
2192
4f74d2c8
LT
2193 if (vma->vm_flags & VM_ACCOUNT)
2194 nr_accounted += nrpages;
2c0b3814 2195 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
a8fb5618 2196 vma = remove_vma(vma);
146425a3 2197 } while (vma);
4f74d2c8 2198 vm_unacct_memory(nr_accounted);
1da177e4
LT
2199 validate_mm(mm);
2200}
2201
2202/*
2203 * Get rid of page table information in the indicated region.
2204 *
f10df686 2205 * Called with the mm semaphore held.
1da177e4
LT
2206 */
2207static void unmap_region(struct mm_struct *mm,
e0da382c
HD
2208 struct vm_area_struct *vma, struct vm_area_struct *prev,
2209 unsigned long start, unsigned long end)
1da177e4 2210{
e0da382c 2211 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
d16dfc55 2212 struct mmu_gather tlb;
1da177e4
LT
2213
2214 lru_add_drain();
d16dfc55 2215 tlb_gather_mmu(&tlb, mm, 0);
365e9c87 2216 update_hiwater_rss(mm);
4f74d2c8 2217 unmap_vmas(&tlb, vma, start, end);
d16dfc55
PZ
2218 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
2219 next ? next->vm_start : 0);
2220 tlb_finish_mmu(&tlb, start, end);
1da177e4
LT
2221}
2222
2223/*
2224 * Create a list of vma's touched by the unmap, removing them from the mm's
2225 * vma list as we go..
2226 */
2227static void
2228detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2229 struct vm_area_struct *prev, unsigned long end)
2230{
2231 struct vm_area_struct **insertion_point;
2232 struct vm_area_struct *tail_vma = NULL;
1363c3cd 2233 unsigned long addr;
1da177e4
LT
2234
2235 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
297c5eee 2236 vma->vm_prev = NULL;
1da177e4 2237 do {
d3737187 2238 vma_rb_erase(vma, &mm->mm_rb);
1da177e4
LT
2239 mm->map_count--;
2240 tail_vma = vma;
2241 vma = vma->vm_next;
2242 } while (vma && vma->vm_start < end);
2243 *insertion_point = vma;
d3737187 2244 if (vma) {
297c5eee 2245 vma->vm_prev = prev;
d3737187
ML
2246 vma_gap_update(vma);
2247 } else
2248 mm->highest_vm_end = prev ? prev->vm_end : 0;
1da177e4 2249 tail_vma->vm_next = NULL;
1363c3cd
WW
2250 if (mm->unmap_area == arch_unmap_area)
2251 addr = prev ? prev->vm_end : mm->mmap_base;
2252 else
2253 addr = vma ? vma->vm_start : mm->mmap_base;
2254 mm->unmap_area(mm, addr);
1da177e4
LT
2255 mm->mmap_cache = NULL; /* Kill the cache. */
2256}
2257
2258/*
659ace58
KM
2259 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2260 * munmap path where it doesn't make sense to fail.
1da177e4 2261 */
659ace58 2262static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1da177e4
LT
2263 unsigned long addr, int new_below)
2264{
2265 struct mempolicy *pol;
2266 struct vm_area_struct *new;
5beb4930 2267 int err = -ENOMEM;
1da177e4 2268
a5516438
AK
2269 if (is_vm_hugetlb_page(vma) && (addr &
2270 ~(huge_page_mask(hstate_vma(vma)))))
1da177e4
LT
2271 return -EINVAL;
2272
e94b1766 2273 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1da177e4 2274 if (!new)
5beb4930 2275 goto out_err;
1da177e4
LT
2276
2277 /* most fields are the same, copy all, and then fixup */
2278 *new = *vma;
2279
5beb4930
RR
2280 INIT_LIST_HEAD(&new->anon_vma_chain);
2281
1da177e4
LT
2282 if (new_below)
2283 new->vm_end = addr;
2284 else {
2285 new->vm_start = addr;
2286 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2287 }
2288
846a16bf 2289 pol = mpol_dup(vma_policy(vma));
1da177e4 2290 if (IS_ERR(pol)) {
5beb4930
RR
2291 err = PTR_ERR(pol);
2292 goto out_free_vma;
1da177e4
LT
2293 }
2294 vma_set_policy(new, pol);
2295
5beb4930
RR
2296 if (anon_vma_clone(new, vma))
2297 goto out_free_mpol;
2298
e9714acf 2299 if (new->vm_file)
1da177e4
LT
2300 get_file(new->vm_file);
2301
2302 if (new->vm_ops && new->vm_ops->open)
2303 new->vm_ops->open(new);
2304
2305 if (new_below)
5beb4930 2306 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1da177e4
LT
2307 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2308 else
5beb4930 2309 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1da177e4 2310
5beb4930
RR
2311 /* Success. */
2312 if (!err)
2313 return 0;
2314
2315 /* Clean everything up if vma_adjust failed. */
58927533
RR
2316 if (new->vm_ops && new->vm_ops->close)
2317 new->vm_ops->close(new);
e9714acf 2318 if (new->vm_file)
5beb4930 2319 fput(new->vm_file);
2aeadc30 2320 unlink_anon_vmas(new);
5beb4930
RR
2321 out_free_mpol:
2322 mpol_put(pol);
2323 out_free_vma:
2324 kmem_cache_free(vm_area_cachep, new);
2325 out_err:
2326 return err;
1da177e4
LT
2327}
2328
659ace58
KM
2329/*
2330 * Split a vma into two pieces at address 'addr', a new vma is allocated
2331 * either for the first part or the tail.
2332 */
2333int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2334 unsigned long addr, int new_below)
2335{
2336 if (mm->map_count >= sysctl_max_map_count)
2337 return -ENOMEM;
2338
2339 return __split_vma(mm, vma, addr, new_below);
2340}
2341
1da177e4
LT
2342/* Munmap is split into 2 main parts -- this part which finds
2343 * what needs doing, and the areas themselves, which do the
2344 * work. This now handles partial unmappings.
2345 * Jeremy Fitzhardinge <jeremy@goop.org>
2346 */
2347int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2348{
2349 unsigned long end;
146425a3 2350 struct vm_area_struct *vma, *prev, *last;
1da177e4
LT
2351
2352 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
2353 return -EINVAL;
2354
2355 if ((len = PAGE_ALIGN(len)) == 0)
2356 return -EINVAL;
2357
2358 /* Find the first overlapping VMA */
9be34c9d 2359 vma = find_vma(mm, start);
146425a3 2360 if (!vma)
1da177e4 2361 return 0;
9be34c9d 2362 prev = vma->vm_prev;
146425a3 2363 /* we have start < vma->vm_end */
1da177e4
LT
2364
2365 /* if it doesn't overlap, we have nothing.. */
2366 end = start + len;
146425a3 2367 if (vma->vm_start >= end)
1da177e4
LT
2368 return 0;
2369
2370 /*
2371 * If we need to split any vma, do it now to save pain later.
2372 *
2373 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2374 * unmapped vm_area_struct will remain in use: so lower split_vma
2375 * places tmp vma above, and higher split_vma places tmp vma below.
2376 */
146425a3 2377 if (start > vma->vm_start) {
659ace58
KM
2378 int error;
2379
2380 /*
2381 * Make sure that map_count on return from munmap() will
2382 * not exceed its limit; but let map_count go just above
2383 * its limit temporarily, to help free resources as expected.
2384 */
2385 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2386 return -ENOMEM;
2387
2388 error = __split_vma(mm, vma, start, 0);
1da177e4
LT
2389 if (error)
2390 return error;
146425a3 2391 prev = vma;
1da177e4
LT
2392 }
2393
2394 /* Does it split the last one? */
2395 last = find_vma(mm, end);
2396 if (last && end > last->vm_start) {
659ace58 2397 int error = __split_vma(mm, last, end, 1);
1da177e4
LT
2398 if (error)
2399 return error;
2400 }
146425a3 2401 vma = prev? prev->vm_next: mm->mmap;
1da177e4 2402
ba470de4
RR
2403 /*
2404 * unlock any mlock()ed ranges before detaching vmas
2405 */
2406 if (mm->locked_vm) {
2407 struct vm_area_struct *tmp = vma;
2408 while (tmp && tmp->vm_start < end) {
2409 if (tmp->vm_flags & VM_LOCKED) {
2410 mm->locked_vm -= vma_pages(tmp);
2411 munlock_vma_pages_all(tmp);
2412 }
2413 tmp = tmp->vm_next;
2414 }
2415 }
2416
1da177e4
LT
2417 /*
2418 * Remove the vma's, and unmap the actual pages
2419 */
146425a3
HD
2420 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2421 unmap_region(mm, vma, prev, start, end);
1da177e4
LT
2422
2423 /* Fix up all other VM information */
2c0b3814 2424 remove_vma_list(mm, vma);
1da177e4
LT
2425
2426 return 0;
2427}
1da177e4 2428
bfce281c 2429int vm_munmap(unsigned long start, size_t len)
1da177e4
LT
2430{
2431 int ret;
bfce281c 2432 struct mm_struct *mm = current->mm;
1da177e4
LT
2433
2434 down_write(&mm->mmap_sem);
a46ef99d 2435 ret = do_munmap(mm, start, len);
1da177e4
LT
2436 up_write(&mm->mmap_sem);
2437 return ret;
2438}
a46ef99d
LT
2439EXPORT_SYMBOL(vm_munmap);
2440
2441SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
2442{
2443 profile_munmap(addr);
bfce281c 2444 return vm_munmap(addr, len);
a46ef99d 2445}
1da177e4
LT
2446
2447static inline void verify_mm_writelocked(struct mm_struct *mm)
2448{
a241ec65 2449#ifdef CONFIG_DEBUG_VM
1da177e4
LT
2450 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2451 WARN_ON(1);
2452 up_read(&mm->mmap_sem);
2453 }
2454#endif
2455}
2456
2457/*
2458 * this is really a simplified "do_mmap". it only handles
2459 * anonymous maps. eventually we may be able to do some
2460 * brk-specific accounting here.
2461 */
e4eb1ff6 2462static unsigned long do_brk(unsigned long addr, unsigned long len)
1da177e4
LT
2463{
2464 struct mm_struct * mm = current->mm;
2465 struct vm_area_struct * vma, * prev;
2466 unsigned long flags;
2467 struct rb_node ** rb_link, * rb_parent;
2468 pgoff_t pgoff = addr >> PAGE_SHIFT;
3a459756 2469 int error;
1da177e4
LT
2470
2471 len = PAGE_ALIGN(len);
2472 if (!len)
2473 return addr;
2474
3a459756
KK
2475 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2476
2c6a1016
AV
2477 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
2478 if (error & ~PAGE_MASK)
3a459756
KK
2479 return error;
2480
1da177e4
LT
2481 /*
2482 * mlock MCL_FUTURE?
2483 */
2484 if (mm->def_flags & VM_LOCKED) {
2485 unsigned long locked, lock_limit;
93ea1d0a
CW
2486 locked = len >> PAGE_SHIFT;
2487 locked += mm->locked_vm;
59e99e5b 2488 lock_limit = rlimit(RLIMIT_MEMLOCK);
93ea1d0a 2489 lock_limit >>= PAGE_SHIFT;
1da177e4
LT
2490 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
2491 return -EAGAIN;
2492 }
2493
2494 /*
2495 * mm->mmap_sem is required to protect against another thread
2496 * changing the mappings in case we sleep.
2497 */
2498 verify_mm_writelocked(mm);
2499
2500 /*
2501 * Clear old maps. this also does some error checking for us
2502 */
2503 munmap_back:
6597d783 2504 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
1da177e4
LT
2505 if (do_munmap(mm, addr, len))
2506 return -ENOMEM;
2507 goto munmap_back;
2508 }
2509
2510 /* Check against address space limits *after* clearing old maps... */
119f657c 2511 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1da177e4
LT
2512 return -ENOMEM;
2513
2514 if (mm->map_count > sysctl_max_map_count)
2515 return -ENOMEM;
2516
191c5424 2517 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
1da177e4
LT
2518 return -ENOMEM;
2519
1da177e4 2520 /* Can we just expand an old private anonymous mapping? */
ba470de4
RR
2521 vma = vma_merge(mm, prev, addr, addr + len, flags,
2522 NULL, NULL, pgoff, NULL);
2523 if (vma)
1da177e4
LT
2524 goto out;
2525
2526 /*
2527 * create a vma struct for an anonymous mapping
2528 */
c5e3b83e 2529 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1da177e4
LT
2530 if (!vma) {
2531 vm_unacct_memory(len >> PAGE_SHIFT);
2532 return -ENOMEM;
2533 }
1da177e4 2534
5beb4930 2535 INIT_LIST_HEAD(&vma->anon_vma_chain);
1da177e4
LT
2536 vma->vm_mm = mm;
2537 vma->vm_start = addr;
2538 vma->vm_end = addr + len;
2539 vma->vm_pgoff = pgoff;
2540 vma->vm_flags = flags;
3ed75eb8 2541 vma->vm_page_prot = vm_get_page_prot(flags);
1da177e4
LT
2542 vma_link(mm, vma, prev, rb_link, rb_parent);
2543out:
3af9e859 2544 perf_event_mmap(vma);
1da177e4
LT
2545 mm->total_vm += len >> PAGE_SHIFT;
2546 if (flags & VM_LOCKED) {
ba470de4
RR
2547 if (!mlock_vma_pages_range(vma, addr, addr + len))
2548 mm->locked_vm += (len >> PAGE_SHIFT);
1da177e4
LT
2549 }
2550 return addr;
2551}
2552
e4eb1ff6
LT
2553unsigned long vm_brk(unsigned long addr, unsigned long len)
2554{
2555 struct mm_struct *mm = current->mm;
2556 unsigned long ret;
2557
2558 down_write(&mm->mmap_sem);
2559 ret = do_brk(addr, len);
2560 up_write(&mm->mmap_sem);
2561 return ret;
2562}
2563EXPORT_SYMBOL(vm_brk);
1da177e4
LT
2564
2565/* Release all mmaps. */
2566void exit_mmap(struct mm_struct *mm)
2567{
d16dfc55 2568 struct mmu_gather tlb;
ba470de4 2569 struct vm_area_struct *vma;
1da177e4
LT
2570 unsigned long nr_accounted = 0;
2571
d6dd61c8 2572 /* mm's last user has gone, and its about to be pulled down */
cddb8a5c 2573 mmu_notifier_release(mm);
d6dd61c8 2574
ba470de4
RR
2575 if (mm->locked_vm) {
2576 vma = mm->mmap;
2577 while (vma) {
2578 if (vma->vm_flags & VM_LOCKED)
2579 munlock_vma_pages_all(vma);
2580 vma = vma->vm_next;
2581 }
2582 }
9480c53e
JF
2583
2584 arch_exit_mmap(mm);
2585
ba470de4 2586 vma = mm->mmap;
9480c53e
JF
2587 if (!vma) /* Can happen if dup_mmap() received an OOM */
2588 return;
2589
1da177e4 2590 lru_add_drain();
1da177e4 2591 flush_cache_mm(mm);
d16dfc55 2592 tlb_gather_mmu(&tlb, mm, 1);
901608d9 2593 /* update_hiwater_rss(mm) here? but nobody should be looking */
e0da382c 2594 /* Use -1 here to ensure all VMAs in the mm are unmapped */
4f74d2c8 2595 unmap_vmas(&tlb, vma, 0, -1);
9ba69294 2596
d16dfc55 2597 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
853f5e26 2598 tlb_finish_mmu(&tlb, 0, -1);
1da177e4 2599
1da177e4 2600 /*
8f4f8c16
HD
2601 * Walk the list again, actually closing and freeing it,
2602 * with preemption enabled, without holding any MM locks.
1da177e4 2603 */
4f74d2c8
LT
2604 while (vma) {
2605 if (vma->vm_flags & VM_ACCOUNT)
2606 nr_accounted += vma_pages(vma);
a8fb5618 2607 vma = remove_vma(vma);
4f74d2c8
LT
2608 }
2609 vm_unacct_memory(nr_accounted);
e0da382c 2610
f9aed62a 2611 WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
1da177e4
LT
2612}
2613
2614/* Insert vm structure into process list sorted by address
2615 * and into the inode's i_mmap tree. If vm_file is non-NULL
3d48ae45 2616 * then i_mmap_mutex is taken here.
1da177e4 2617 */
6597d783 2618int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 2619{
6597d783
HD
2620 struct vm_area_struct *prev;
2621 struct rb_node **rb_link, *rb_parent;
1da177e4
LT
2622
2623 /*
2624 * The vm_pgoff of a purely anonymous vma should be irrelevant
2625 * until its first write fault, when page's anon_vma and index
2626 * are set. But now set the vm_pgoff it will almost certainly
2627 * end up with (unless mremap moves it elsewhere before that
2628 * first wfault), so /proc/pid/maps tells a consistent story.
2629 *
2630 * By setting it to reflect the virtual start address of the
2631 * vma, merges and splits can happen in a seamless way, just
2632 * using the existing file pgoff checks and manipulations.
2633 * Similarly in do_mmap_pgoff and in do_brk.
2634 */
2635 if (!vma->vm_file) {
2636 BUG_ON(vma->anon_vma);
2637 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2638 }
6597d783
HD
2639 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
2640 &prev, &rb_link, &rb_parent))
1da177e4 2641 return -ENOMEM;
2fd4ef85 2642 if ((vma->vm_flags & VM_ACCOUNT) &&
34b4e4aa 2643 security_vm_enough_memory_mm(mm, vma_pages(vma)))
2fd4ef85 2644 return -ENOMEM;
2b144498 2645
1da177e4
LT
2646 vma_link(mm, vma, prev, rb_link, rb_parent);
2647 return 0;
2648}
2649
2650/*
2651 * Copy the vma structure to a new location in the same mm,
2652 * prior to moving page table entries, to effect an mremap move.
2653 */
2654struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
38a76013
ML
2655 unsigned long addr, unsigned long len, pgoff_t pgoff,
2656 bool *need_rmap_locks)
1da177e4
LT
2657{
2658 struct vm_area_struct *vma = *vmap;
2659 unsigned long vma_start = vma->vm_start;
2660 struct mm_struct *mm = vma->vm_mm;
2661 struct vm_area_struct *new_vma, *prev;
2662 struct rb_node **rb_link, *rb_parent;
2663 struct mempolicy *pol;
948f017b 2664 bool faulted_in_anon_vma = true;
1da177e4
LT
2665
2666 /*
2667 * If anonymous vma has not yet been faulted, update new pgoff
2668 * to match new location, to increase its chance of merging.
2669 */
948f017b 2670 if (unlikely(!vma->vm_file && !vma->anon_vma)) {
1da177e4 2671 pgoff = addr >> PAGE_SHIFT;
948f017b
AA
2672 faulted_in_anon_vma = false;
2673 }
1da177e4 2674
6597d783
HD
2675 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
2676 return NULL; /* should never get here */
1da177e4
LT
2677 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2678 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2679 if (new_vma) {
2680 /*
2681 * Source vma may have been merged into new_vma
2682 */
948f017b
AA
2683 if (unlikely(vma_start >= new_vma->vm_start &&
2684 vma_start < new_vma->vm_end)) {
2685 /*
2686 * The only way we can get a vma_merge with
2687 * self during an mremap is if the vma hasn't
2688 * been faulted in yet and we were allowed to
2689 * reset the dst vma->vm_pgoff to the
2690 * destination address of the mremap to allow
2691 * the merge to happen. mremap must change the
2692 * vm_pgoff linearity between src and dst vmas
2693 * (in turn preventing a vma_merge) to be
2694 * safe. It is only safe to keep the vm_pgoff
2695 * linear if there are no pages mapped yet.
2696 */
2697 VM_BUG_ON(faulted_in_anon_vma);
38a76013 2698 *vmap = vma = new_vma;
108d6642 2699 }
38a76013 2700 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
1da177e4 2701 } else {
e94b1766 2702 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1da177e4
LT
2703 if (new_vma) {
2704 *new_vma = *vma;
523d4e20
ML
2705 new_vma->vm_start = addr;
2706 new_vma->vm_end = addr + len;
2707 new_vma->vm_pgoff = pgoff;
846a16bf 2708 pol = mpol_dup(vma_policy(vma));
5beb4930
RR
2709 if (IS_ERR(pol))
2710 goto out_free_vma;
523d4e20 2711 vma_set_policy(new_vma, pol);
5beb4930
RR
2712 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2713 if (anon_vma_clone(new_vma, vma))
2714 goto out_free_mempol;
e9714acf 2715 if (new_vma->vm_file)
1da177e4
LT
2716 get_file(new_vma->vm_file);
2717 if (new_vma->vm_ops && new_vma->vm_ops->open)
2718 new_vma->vm_ops->open(new_vma);
2719 vma_link(mm, new_vma, prev, rb_link, rb_parent);
38a76013 2720 *need_rmap_locks = false;
1da177e4
LT
2721 }
2722 }
2723 return new_vma;
5beb4930
RR
2724
2725 out_free_mempol:
2726 mpol_put(pol);
2727 out_free_vma:
2728 kmem_cache_free(vm_area_cachep, new_vma);
2729 return NULL;
1da177e4 2730}
119f657c 2731
2732/*
2733 * Return true if the calling process may expand its vm space by the passed
2734 * number of pages
2735 */
2736int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2737{
2738 unsigned long cur = mm->total_vm; /* pages */
2739 unsigned long lim;
2740
59e99e5b 2741 lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
119f657c 2742
2743 if (cur + npages > lim)
2744 return 0;
2745 return 1;
2746}
fa5dc22f
RM
2747
2748
b1d0e4f5
NP
2749static int special_mapping_fault(struct vm_area_struct *vma,
2750 struct vm_fault *vmf)
fa5dc22f 2751{
b1d0e4f5 2752 pgoff_t pgoff;
fa5dc22f
RM
2753 struct page **pages;
2754
b1d0e4f5
NP
2755 /*
2756 * special mappings have no vm_file, and in that case, the mm
2757 * uses vm_pgoff internally. So we have to subtract it from here.
2758 * We are allowed to do this because we are the mm; do not copy
2759 * this code into drivers!
2760 */
2761 pgoff = vmf->pgoff - vma->vm_pgoff;
fa5dc22f 2762
b1d0e4f5
NP
2763 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2764 pgoff--;
fa5dc22f
RM
2765
2766 if (*pages) {
2767 struct page *page = *pages;
2768 get_page(page);
b1d0e4f5
NP
2769 vmf->page = page;
2770 return 0;
fa5dc22f
RM
2771 }
2772
b1d0e4f5 2773 return VM_FAULT_SIGBUS;
fa5dc22f
RM
2774}
2775
2776/*
2777 * Having a close hook prevents vma merging regardless of flags.
2778 */
2779static void special_mapping_close(struct vm_area_struct *vma)
2780{
2781}
2782
f0f37e2f 2783static const struct vm_operations_struct special_mapping_vmops = {
fa5dc22f 2784 .close = special_mapping_close,
b1d0e4f5 2785 .fault = special_mapping_fault,
fa5dc22f
RM
2786};
2787
2788/*
2789 * Called with mm->mmap_sem held for writing.
2790 * Insert a new vma covering the given region, with the given flags.
2791 * Its pages are supplied by the given array of struct page *.
2792 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2793 * The region past the last page supplied will always produce SIGBUS.
2794 * The array pointer and the pages it points to are assumed to stay alive
2795 * for as long as this mapping might exist.
2796 */
2797int install_special_mapping(struct mm_struct *mm,
2798 unsigned long addr, unsigned long len,
2799 unsigned long vm_flags, struct page **pages)
2800{
462e635e 2801 int ret;
fa5dc22f
RM
2802 struct vm_area_struct *vma;
2803
2804 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2805 if (unlikely(vma == NULL))
2806 return -ENOMEM;
2807
5beb4930 2808 INIT_LIST_HEAD(&vma->anon_vma_chain);
fa5dc22f
RM
2809 vma->vm_mm = mm;
2810 vma->vm_start = addr;
2811 vma->vm_end = addr + len;
2812
2f98735c 2813 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
3ed75eb8 2814 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
fa5dc22f
RM
2815
2816 vma->vm_ops = &special_mapping_vmops;
2817 vma->vm_private_data = pages;
2818
462e635e
TO
2819 ret = insert_vm_struct(mm, vma);
2820 if (ret)
2821 goto out;
fa5dc22f
RM
2822
2823 mm->total_vm += len >> PAGE_SHIFT;
2824
cdd6c482 2825 perf_event_mmap(vma);
089dd79d 2826
fa5dc22f 2827 return 0;
462e635e
TO
2828
2829out:
2830 kmem_cache_free(vm_area_cachep, vma);
2831 return ret;
fa5dc22f 2832}
7906d00c
AA
2833
2834static DEFINE_MUTEX(mm_all_locks_mutex);
2835
454ed842 2836static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
7906d00c 2837{
bf181b9f 2838 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
7906d00c
AA
2839 /*
2840 * The LSB of head.next can't change from under us
2841 * because we hold the mm_all_locks_mutex.
2842 */
2b575eb6 2843 mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
7906d00c
AA
2844 /*
2845 * We can safely modify head.next after taking the
2b575eb6 2846 * anon_vma->root->mutex. If some other vma in this mm shares
7906d00c
AA
2847 * the same anon_vma we won't take it again.
2848 *
2849 * No need of atomic instructions here, head.next
2850 * can't change from under us thanks to the
2b575eb6 2851 * anon_vma->root->mutex.
7906d00c
AA
2852 */
2853 if (__test_and_set_bit(0, (unsigned long *)
bf181b9f 2854 &anon_vma->root->rb_root.rb_node))
7906d00c
AA
2855 BUG();
2856 }
2857}
2858
454ed842 2859static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
7906d00c
AA
2860{
2861 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2862 /*
2863 * AS_MM_ALL_LOCKS can't change from under us because
2864 * we hold the mm_all_locks_mutex.
2865 *
2866 * Operations on ->flags have to be atomic because
2867 * even if AS_MM_ALL_LOCKS is stable thanks to the
2868 * mm_all_locks_mutex, there may be other cpus
2869 * changing other bitflags in parallel to us.
2870 */
2871 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2872 BUG();
3d48ae45 2873 mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
7906d00c
AA
2874 }
2875}
2876
2877/*
2878 * This operation locks against the VM for all pte/vma/mm related
2879 * operations that could ever happen on a certain mm. This includes
2880 * vmtruncate, try_to_unmap, and all page faults.
2881 *
2882 * The caller must take the mmap_sem in write mode before calling
2883 * mm_take_all_locks(). The caller isn't allowed to release the
2884 * mmap_sem until mm_drop_all_locks() returns.
2885 *
2886 * mmap_sem in write mode is required in order to block all operations
2887 * that could modify pagetables and free pages without need of
2888 * altering the vma layout (for example populate_range() with
2889 * nonlinear vmas). It's also needed in write mode to avoid new
2890 * anon_vmas to be associated with existing vmas.
2891 *
2892 * A single task can't take more than one mm_take_all_locks() in a row
2893 * or it would deadlock.
2894 *
bf181b9f 2895 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
7906d00c
AA
2896 * mapping->flags avoid to take the same lock twice, if more than one
2897 * vma in this mm is backed by the same anon_vma or address_space.
2898 *
2899 * We can take all the locks in random order because the VM code
2b575eb6 2900 * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
7906d00c
AA
2901 * takes more than one of them in a row. Secondly we're protected
2902 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2903 *
2904 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2905 * that may have to take thousand of locks.
2906 *
2907 * mm_take_all_locks() can fail if it's interrupted by signals.
2908 */
2909int mm_take_all_locks(struct mm_struct *mm)
2910{
2911 struct vm_area_struct *vma;
5beb4930 2912 struct anon_vma_chain *avc;
7906d00c
AA
2913
2914 BUG_ON(down_read_trylock(&mm->mmap_sem));
2915
2916 mutex_lock(&mm_all_locks_mutex);
2917
2918 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2919 if (signal_pending(current))
2920 goto out_unlock;
7906d00c 2921 if (vma->vm_file && vma->vm_file->f_mapping)
454ed842 2922 vm_lock_mapping(mm, vma->vm_file->f_mapping);
7906d00c 2923 }
7cd5a02f
PZ
2924
2925 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2926 if (signal_pending(current))
2927 goto out_unlock;
2928 if (vma->anon_vma)
5beb4930
RR
2929 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2930 vm_lock_anon_vma(mm, avc->anon_vma);
7906d00c 2931 }
7cd5a02f 2932
584cff54 2933 return 0;
7906d00c
AA
2934
2935out_unlock:
584cff54
KC
2936 mm_drop_all_locks(mm);
2937 return -EINTR;
7906d00c
AA
2938}
2939
2940static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2941{
bf181b9f 2942 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
7906d00c
AA
2943 /*
2944 * The LSB of head.next can't change to 0 from under
2945 * us because we hold the mm_all_locks_mutex.
2946 *
2947 * We must however clear the bitflag before unlocking
bf181b9f 2948 * the vma so the users using the anon_vma->rb_root will
7906d00c
AA
2949 * never see our bitflag.
2950 *
2951 * No need of atomic instructions here, head.next
2952 * can't change from under us until we release the
2b575eb6 2953 * anon_vma->root->mutex.
7906d00c
AA
2954 */
2955 if (!__test_and_clear_bit(0, (unsigned long *)
bf181b9f 2956 &anon_vma->root->rb_root.rb_node))
7906d00c 2957 BUG();
cba48b98 2958 anon_vma_unlock(anon_vma);
7906d00c
AA
2959 }
2960}
2961
2962static void vm_unlock_mapping(struct address_space *mapping)
2963{
2964 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2965 /*
2966 * AS_MM_ALL_LOCKS can't change to 0 from under us
2967 * because we hold the mm_all_locks_mutex.
2968 */
3d48ae45 2969 mutex_unlock(&mapping->i_mmap_mutex);
7906d00c
AA
2970 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2971 &mapping->flags))
2972 BUG();
2973 }
2974}
2975
2976/*
2977 * The mmap_sem cannot be released by the caller until
2978 * mm_drop_all_locks() returns.
2979 */
2980void mm_drop_all_locks(struct mm_struct *mm)
2981{
2982 struct vm_area_struct *vma;
5beb4930 2983 struct anon_vma_chain *avc;
7906d00c
AA
2984
2985 BUG_ON(down_read_trylock(&mm->mmap_sem));
2986 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2987
2988 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2989 if (vma->anon_vma)
5beb4930
RR
2990 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2991 vm_unlock_anon_vma(avc->anon_vma);
7906d00c
AA
2992 if (vma->vm_file && vma->vm_file->f_mapping)
2993 vm_unlock_mapping(vma->vm_file->f_mapping);
2994 }
2995
2996 mutex_unlock(&mm_all_locks_mutex);
2997}
8feae131
DH
2998
2999/*
3000 * initialise the VMA slab
3001 */
3002void __init mmap_init(void)
3003{
00a62ce9
KM
3004 int ret;
3005
3006 ret = percpu_counter_init(&vm_committed_as, 0);
3007 VM_BUG_ON(ret);
8feae131 3008}