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