blob: c7ed061f4507595549ec332bcb3a3bea4789f88f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
6 * Address space accounting code <alan@redhat.com>
7 */
8
9#include <linux/slab.h>
10#include <linux/mm.h>
11#include <linux/shm.h>
12#include <linux/mman.h>
13#include <linux/pagemap.h>
14#include <linux/swap.h>
15#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/file.h>
19#include <linux/fs.h>
20#include <linux/personality.h>
21#include <linux/security.h>
22#include <linux/hugetlb.h>
23#include <linux/profile.h>
24#include <linux/module.h>
25#include <linux/mount.h>
26#include <linux/mempolicy.h>
27#include <linux/rmap.h>
28
29#include <asm/uaccess.h>
30#include <asm/cacheflush.h>
31#include <asm/tlb.h>
32
Hugh Dickinse0da3822005-04-19 13:29:15 -070033static void unmap_region(struct mm_struct *mm,
34 struct vm_area_struct *vma, struct vm_area_struct *prev,
35 unsigned long start, unsigned long end);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * WARNING: the debugging will use recursive algorithms so never enable this
39 * unless you know what you are doing.
40 */
41#undef DEBUG_MM_RB
42
43/* description of effects of mapping type and prot in current implementation.
44 * this is due to the limited x86 page protection hardware. The expected
45 * behavior is in parens:
46 *
47 * map_type prot
48 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
49 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
50 * w: (no) no w: (no) no w: (yes) yes w: (no) no
51 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
52 *
53 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
54 * w: (no) no w: (no) no w: (copy) copy w: (no) no
55 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
56 *
57 */
58pgprot_t protection_map[16] = {
59 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
60 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
61};
62
Hugh Dickins804af2c2006-07-26 21:39:49 +010063pgprot_t vm_get_page_prot(unsigned long vm_flags)
64{
65 return protection_map[vm_flags &
66 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
67}
68EXPORT_SYMBOL(vm_get_page_prot);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
71int sysctl_overcommit_ratio = 50; /* default is 50% */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070072int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073atomic_t vm_committed_space = ATOMIC_INIT(0);
74
75/*
76 * Check that a process has enough memory to allocate a new virtual
77 * mapping. 0 means there is enough memory for the allocation to
78 * succeed and -ENOMEM implies there is not.
79 *
80 * We currently support three overcommit policies, which are set via the
81 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
82 *
83 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
84 * Additional code 2002 Jul 20 by Robert Love.
85 *
86 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
87 *
88 * Note this is a helper function intended to be used by LSMs which
89 * wish to use this logic.
90 */
91int __vm_enough_memory(long pages, int cap_sys_admin)
92{
93 unsigned long free, allowed;
94
95 vm_acct_memory(pages);
96
97 /*
98 * Sometimes we want to use more memory than we have
99 */
100 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
101 return 0;
102
103 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
104 unsigned long n;
105
Christoph Lameter347ce432006-06-30 01:55:35 -0700106 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 free += nr_swap_pages;
108
109 /*
110 * Any slabs which are created with the
111 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
112 * which are reclaimable, under pressure. The dentry
113 * cache and most inode caches should fall into this
114 */
115 free += atomic_read(&slab_reclaim_pages);
116
117 /*
118 * Leave the last 3% for root
119 */
120 if (!cap_sys_admin)
121 free -= free / 32;
122
123 if (free > pages)
124 return 0;
125
126 /*
127 * nr_free_pages() is very expensive on large systems,
128 * only call if we're about to fail.
129 */
130 n = nr_free_pages();
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700131
132 /*
133 * Leave reserved pages. The pages are not for anonymous pages.
134 */
135 if (n <= totalreserve_pages)
136 goto error;
137 else
138 n -= totalreserve_pages;
139
140 /*
141 * Leave the last 3% for root
142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!cap_sys_admin)
144 n -= n / 32;
145 free += n;
146
147 if (free > pages)
148 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700149
150 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 allowed = (totalram_pages - hugetlb_total_pages())
154 * sysctl_overcommit_ratio / 100;
155 /*
156 * Leave the last 3% for root
157 */
158 if (!cap_sys_admin)
159 allowed -= allowed / 32;
160 allowed += total_swap_pages;
161
162 /* Don't let a single process grow too big:
163 leave 3% of the size of this process for other processes */
164 allowed -= current->mm->total_vm / 32;
165
Simon Derr2f60f8d2005-08-04 19:52:03 -0700166 /*
167 * cast `allowed' as a signed long because vm_committed_space
168 * sometimes has a negative value
169 */
170 if (atomic_read(&vm_committed_space) < (long)allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700172error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 vm_unacct_memory(pages);
174
175 return -ENOMEM;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178EXPORT_SYMBOL(__vm_enough_memory);
179
180/*
181 * Requires inode->i_mapping->i_mmap_lock
182 */
183static void __remove_shared_vm_struct(struct vm_area_struct *vma,
184 struct file *file, struct address_space *mapping)
185{
186 if (vma->vm_flags & VM_DENYWRITE)
187 atomic_inc(&file->f_dentry->d_inode->i_writecount);
188 if (vma->vm_flags & VM_SHARED)
189 mapping->i_mmap_writable--;
190
191 flush_dcache_mmap_lock(mapping);
192 if (unlikely(vma->vm_flags & VM_NONLINEAR))
193 list_del_init(&vma->shared.vm_set.list);
194 else
195 vma_prio_tree_remove(vma, &mapping->i_mmap);
196 flush_dcache_mmap_unlock(mapping);
197}
198
199/*
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700200 * Unlink a file-based vm structure from its prio_tree, to hide
201 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700203void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct file *file = vma->vm_file;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (file) {
208 struct address_space *mapping = file->f_mapping;
209 spin_lock(&mapping->i_mmap_lock);
210 __remove_shared_vm_struct(vma, file, mapping);
211 spin_unlock(&mapping->i_mmap_lock);
212 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700213}
214
215/*
216 * Close a vm structure and free it, returning the next.
217 */
218static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
219{
220 struct vm_area_struct *next = vma->vm_next;
221
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700222 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (vma->vm_ops && vma->vm_ops->close)
224 vma->vm_ops->close(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700225 if (vma->vm_file)
226 fput(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mpol_free(vma_policy(vma));
228 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700229 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232asmlinkage unsigned long sys_brk(unsigned long brk)
233{
234 unsigned long rlim, retval;
235 unsigned long newbrk, oldbrk;
236 struct mm_struct *mm = current->mm;
237
238 down_write(&mm->mmap_sem);
239
240 if (brk < mm->end_code)
241 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700242
243 /*
244 * Check against rlimit here. If this check is done later after the test
245 * of oldbrk with newbrk then it can escape the test and let the data
246 * segment grow beyond its set limit the in case where the limit is
247 * not page aligned -Ram Gupta
248 */
249 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
250 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
251 goto out;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 newbrk = PAGE_ALIGN(brk);
254 oldbrk = PAGE_ALIGN(mm->brk);
255 if (oldbrk == newbrk)
256 goto set_brk;
257
258 /* Always allow shrinking brk. */
259 if (brk <= mm->brk) {
260 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
261 goto set_brk;
262 goto out;
263 }
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* Check against existing mmap mappings. */
266 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
267 goto out;
268
269 /* Ok, looks good - let it rip. */
270 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
271 goto out;
272set_brk:
273 mm->brk = brk;
274out:
275 retval = mm->brk;
276 up_write(&mm->mmap_sem);
277 return retval;
278}
279
280#ifdef DEBUG_MM_RB
281static int browse_rb(struct rb_root *root)
282{
283 int i = 0, j;
284 struct rb_node *nd, *pn = NULL;
285 unsigned long prev = 0, pend = 0;
286
287 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
288 struct vm_area_struct *vma;
289 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
290 if (vma->vm_start < prev)
291 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
292 if (vma->vm_start < pend)
293 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
294 if (vma->vm_start > vma->vm_end)
295 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
296 i++;
297 pn = nd;
298 }
299 j = 0;
300 for (nd = pn; nd; nd = rb_prev(nd)) {
301 j++;
302 }
303 if (i != j)
304 printk("backwards %d, forwards %d\n", j, i), i = 0;
305 return i;
306}
307
308void validate_mm(struct mm_struct *mm)
309{
310 int bug = 0;
311 int i = 0;
312 struct vm_area_struct *tmp = mm->mmap;
313 while (tmp) {
314 tmp = tmp->vm_next;
315 i++;
316 }
317 if (i != mm->map_count)
318 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
319 i = browse_rb(&mm->mm_rb);
320 if (i != mm->map_count)
321 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200322 BUG_ON(bug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324#else
325#define validate_mm(mm) do { } while (0)
326#endif
327
328static struct vm_area_struct *
329find_vma_prepare(struct mm_struct *mm, unsigned long addr,
330 struct vm_area_struct **pprev, struct rb_node ***rb_link,
331 struct rb_node ** rb_parent)
332{
333 struct vm_area_struct * vma;
334 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
335
336 __rb_link = &mm->mm_rb.rb_node;
337 rb_prev = __rb_parent = NULL;
338 vma = NULL;
339
340 while (*__rb_link) {
341 struct vm_area_struct *vma_tmp;
342
343 __rb_parent = *__rb_link;
344 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
345
346 if (vma_tmp->vm_end > addr) {
347 vma = vma_tmp;
348 if (vma_tmp->vm_start <= addr)
349 return vma;
350 __rb_link = &__rb_parent->rb_left;
351 } else {
352 rb_prev = __rb_parent;
353 __rb_link = &__rb_parent->rb_right;
354 }
355 }
356
357 *pprev = NULL;
358 if (rb_prev)
359 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
360 *rb_link = __rb_link;
361 *rb_parent = __rb_parent;
362 return vma;
363}
364
365static inline void
366__vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
367 struct vm_area_struct *prev, struct rb_node *rb_parent)
368{
369 if (prev) {
370 vma->vm_next = prev->vm_next;
371 prev->vm_next = vma;
372 } else {
373 mm->mmap = vma;
374 if (rb_parent)
375 vma->vm_next = rb_entry(rb_parent,
376 struct vm_area_struct, vm_rb);
377 else
378 vma->vm_next = NULL;
379 }
380}
381
382void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
383 struct rb_node **rb_link, struct rb_node *rb_parent)
384{
385 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
386 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
387}
388
389static inline void __vma_link_file(struct vm_area_struct *vma)
390{
391 struct file * file;
392
393 file = vma->vm_file;
394 if (file) {
395 struct address_space *mapping = file->f_mapping;
396
397 if (vma->vm_flags & VM_DENYWRITE)
398 atomic_dec(&file->f_dentry->d_inode->i_writecount);
399 if (vma->vm_flags & VM_SHARED)
400 mapping->i_mmap_writable++;
401
402 flush_dcache_mmap_lock(mapping);
403 if (unlikely(vma->vm_flags & VM_NONLINEAR))
404 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
405 else
406 vma_prio_tree_insert(vma, &mapping->i_mmap);
407 flush_dcache_mmap_unlock(mapping);
408 }
409}
410
411static void
412__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
413 struct vm_area_struct *prev, struct rb_node **rb_link,
414 struct rb_node *rb_parent)
415{
416 __vma_link_list(mm, vma, prev, rb_parent);
417 __vma_link_rb(mm, vma, rb_link, rb_parent);
418 __anon_vma_link(vma);
419}
420
421static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
422 struct vm_area_struct *prev, struct rb_node **rb_link,
423 struct rb_node *rb_parent)
424{
425 struct address_space *mapping = NULL;
426
427 if (vma->vm_file)
428 mapping = vma->vm_file->f_mapping;
429
430 if (mapping) {
431 spin_lock(&mapping->i_mmap_lock);
432 vma->vm_truncate_count = mapping->truncate_count;
433 }
434 anon_vma_lock(vma);
435
436 __vma_link(mm, vma, prev, rb_link, rb_parent);
437 __vma_link_file(vma);
438
439 anon_vma_unlock(vma);
440 if (mapping)
441 spin_unlock(&mapping->i_mmap_lock);
442
443 mm->map_count++;
444 validate_mm(mm);
445}
446
447/*
448 * Helper for vma_adjust in the split_vma insert case:
449 * insert vm structure into list and rbtree and anon_vma,
450 * but it has already been inserted into prio_tree earlier.
451 */
452static void
453__insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
454{
455 struct vm_area_struct * __vma, * prev;
456 struct rb_node ** rb_link, * rb_parent;
457
458 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200459 BUG_ON(__vma && __vma->vm_start < vma->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 __vma_link(mm, vma, prev, rb_link, rb_parent);
461 mm->map_count++;
462}
463
464static inline void
465__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
466 struct vm_area_struct *prev)
467{
468 prev->vm_next = vma->vm_next;
469 rb_erase(&vma->vm_rb, &mm->mm_rb);
470 if (mm->mmap_cache == vma)
471 mm->mmap_cache = prev;
472}
473
474/*
475 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
476 * is already present in an i_mmap tree without adjusting the tree.
477 * The following helper function should be used when such adjustments
478 * are necessary. The "insert" vma (if any) is to be inserted
479 * before we drop the necessary locks.
480 */
481void vma_adjust(struct vm_area_struct *vma, unsigned long start,
482 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
483{
484 struct mm_struct *mm = vma->vm_mm;
485 struct vm_area_struct *next = vma->vm_next;
486 struct vm_area_struct *importer = NULL;
487 struct address_space *mapping = NULL;
488 struct prio_tree_root *root = NULL;
489 struct file *file = vma->vm_file;
490 struct anon_vma *anon_vma = NULL;
491 long adjust_next = 0;
492 int remove_next = 0;
493
494 if (next && !insert) {
495 if (end >= next->vm_end) {
496 /*
497 * vma expands, overlapping all the next, and
498 * perhaps the one after too (mprotect case 6).
499 */
500again: remove_next = 1 + (end > next->vm_end);
501 end = next->vm_end;
502 anon_vma = next->anon_vma;
503 importer = vma;
504 } else if (end > next->vm_start) {
505 /*
506 * vma expands, overlapping part of the next:
507 * mprotect case 5 shifting the boundary up.
508 */
509 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
510 anon_vma = next->anon_vma;
511 importer = vma;
512 } else if (end < vma->vm_end) {
513 /*
514 * vma shrinks, and !insert tells it's not
515 * split_vma inserting another: so it must be
516 * mprotect case 4 shifting the boundary down.
517 */
518 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
519 anon_vma = next->anon_vma;
520 importer = next;
521 }
522 }
523
524 if (file) {
525 mapping = file->f_mapping;
526 if (!(vma->vm_flags & VM_NONLINEAR))
527 root = &mapping->i_mmap;
528 spin_lock(&mapping->i_mmap_lock);
529 if (importer &&
530 vma->vm_truncate_count != next->vm_truncate_count) {
531 /*
532 * unmap_mapping_range might be in progress:
533 * ensure that the expanding vma is rescanned.
534 */
535 importer->vm_truncate_count = 0;
536 }
537 if (insert) {
538 insert->vm_truncate_count = vma->vm_truncate_count;
539 /*
540 * Put into prio_tree now, so instantiated pages
541 * are visible to arm/parisc __flush_dcache_page
542 * throughout; but we cannot insert into address
543 * space until vma start or end is updated.
544 */
545 __vma_link_file(insert);
546 }
547 }
548
549 /*
550 * When changing only vma->vm_end, we don't really need
551 * anon_vma lock: but is that case worth optimizing out?
552 */
553 if (vma->anon_vma)
554 anon_vma = vma->anon_vma;
555 if (anon_vma) {
556 spin_lock(&anon_vma->lock);
557 /*
558 * Easily overlooked: when mprotect shifts the boundary,
559 * make sure the expanding vma has anon_vma set if the
560 * shrinking vma had, to cover any anon pages imported.
561 */
562 if (importer && !importer->anon_vma) {
563 importer->anon_vma = anon_vma;
564 __anon_vma_link(importer);
565 }
566 }
567
568 if (root) {
569 flush_dcache_mmap_lock(mapping);
570 vma_prio_tree_remove(vma, root);
571 if (adjust_next)
572 vma_prio_tree_remove(next, root);
573 }
574
575 vma->vm_start = start;
576 vma->vm_end = end;
577 vma->vm_pgoff = pgoff;
578 if (adjust_next) {
579 next->vm_start += adjust_next << PAGE_SHIFT;
580 next->vm_pgoff += adjust_next;
581 }
582
583 if (root) {
584 if (adjust_next)
585 vma_prio_tree_insert(next, root);
586 vma_prio_tree_insert(vma, root);
587 flush_dcache_mmap_unlock(mapping);
588 }
589
590 if (remove_next) {
591 /*
592 * vma_merge has merged next into vma, and needs
593 * us to remove next before dropping the locks.
594 */
595 __vma_unlink(mm, next, vma);
596 if (file)
597 __remove_shared_vm_struct(next, file, mapping);
598 if (next->anon_vma)
599 __anon_vma_merge(vma, next);
600 } else if (insert) {
601 /*
602 * split_vma has split insert from vma, and needs
603 * us to insert it before dropping the locks
604 * (it may either follow vma or precede it).
605 */
606 __insert_vm_struct(mm, insert);
607 }
608
609 if (anon_vma)
610 spin_unlock(&anon_vma->lock);
611 if (mapping)
612 spin_unlock(&mapping->i_mmap_lock);
613
614 if (remove_next) {
615 if (file)
616 fput(file);
617 mm->map_count--;
618 mpol_free(vma_policy(next));
619 kmem_cache_free(vm_area_cachep, next);
620 /*
621 * In mprotect's case 6 (see comments on vma_merge),
622 * we must remove another next too. It would clutter
623 * up the code too much to do both in one go.
624 */
625 if (remove_next == 2) {
626 next = vma->vm_next;
627 goto again;
628 }
629 }
630
631 validate_mm(mm);
632}
633
634/*
635 * If the vma has a ->close operation then the driver probably needs to release
636 * per-vma resources, so we don't attempt to merge those.
637 */
Hugh Dickinsa6f563d2006-03-22 00:08:29 -0800638#define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640static inline int is_mergeable_vma(struct vm_area_struct *vma,
641 struct file *file, unsigned long vm_flags)
642{
643 if (vma->vm_flags != vm_flags)
644 return 0;
645 if (vma->vm_file != file)
646 return 0;
647 if (vma->vm_ops && vma->vm_ops->close)
648 return 0;
649 return 1;
650}
651
652static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
653 struct anon_vma *anon_vma2)
654{
655 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
656}
657
658/*
659 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
660 * in front of (at a lower virtual address and file offset than) the vma.
661 *
662 * We cannot merge two vmas if they have differently assigned (non-NULL)
663 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
664 *
665 * We don't check here for the merged mmap wrapping around the end of pagecache
666 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
667 * wrap, nor mmaps which cover the final page at index -1UL.
668 */
669static int
670can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
671 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
672{
673 if (is_mergeable_vma(vma, file, vm_flags) &&
674 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
675 if (vma->vm_pgoff == vm_pgoff)
676 return 1;
677 }
678 return 0;
679}
680
681/*
682 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
683 * beyond (at a higher virtual address and file offset than) the vma.
684 *
685 * We cannot merge two vmas if they have differently assigned (non-NULL)
686 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
687 */
688static int
689can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
690 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
691{
692 if (is_mergeable_vma(vma, file, vm_flags) &&
693 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
694 pgoff_t vm_pglen;
695 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
696 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
697 return 1;
698 }
699 return 0;
700}
701
702/*
703 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
704 * whether that can be merged with its predecessor or its successor.
705 * Or both (it neatly fills a hole).
706 *
707 * In most cases - when called for mmap, brk or mremap - [addr,end) is
708 * certain not to be mapped by the time vma_merge is called; but when
709 * called for mprotect, it is certain to be already mapped (either at
710 * an offset within prev, or at the start of next), and the flags of
711 * this area are about to be changed to vm_flags - and the no-change
712 * case has already been eliminated.
713 *
714 * The following mprotect cases have to be considered, where AAAA is
715 * the area passed down from mprotect_fixup, never extending beyond one
716 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
717 *
718 * AAAA AAAA AAAA AAAA
719 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
720 * cannot merge might become might become might become
721 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
722 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
723 * mremap move: PPPPNNNNNNNN 8
724 * AAAA
725 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
726 * might become case 1 below case 2 below case 3 below
727 *
728 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
729 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
730 */
731struct vm_area_struct *vma_merge(struct mm_struct *mm,
732 struct vm_area_struct *prev, unsigned long addr,
733 unsigned long end, unsigned long vm_flags,
734 struct anon_vma *anon_vma, struct file *file,
735 pgoff_t pgoff, struct mempolicy *policy)
736{
737 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
738 struct vm_area_struct *area, *next;
739
740 /*
741 * We later require that vma->vm_flags == vm_flags,
742 * so this tests vma->vm_flags & VM_SPECIAL, too.
743 */
744 if (vm_flags & VM_SPECIAL)
745 return NULL;
746
747 if (prev)
748 next = prev->vm_next;
749 else
750 next = mm->mmap;
751 area = next;
752 if (next && next->vm_end == end) /* cases 6, 7, 8 */
753 next = next->vm_next;
754
755 /*
756 * Can it merge with the predecessor?
757 */
758 if (prev && prev->vm_end == addr &&
759 mpol_equal(vma_policy(prev), policy) &&
760 can_vma_merge_after(prev, vm_flags,
761 anon_vma, file, pgoff)) {
762 /*
763 * OK, it can. Can we now merge in the successor as well?
764 */
765 if (next && end == next->vm_start &&
766 mpol_equal(policy, vma_policy(next)) &&
767 can_vma_merge_before(next, vm_flags,
768 anon_vma, file, pgoff+pglen) &&
769 is_mergeable_anon_vma(prev->anon_vma,
770 next->anon_vma)) {
771 /* cases 1, 6 */
772 vma_adjust(prev, prev->vm_start,
773 next->vm_end, prev->vm_pgoff, NULL);
774 } else /* cases 2, 5, 7 */
775 vma_adjust(prev, prev->vm_start,
776 end, prev->vm_pgoff, NULL);
777 return prev;
778 }
779
780 /*
781 * Can this new request be merged in front of next?
782 */
783 if (next && end == next->vm_start &&
784 mpol_equal(policy, vma_policy(next)) &&
785 can_vma_merge_before(next, vm_flags,
786 anon_vma, file, pgoff+pglen)) {
787 if (prev && addr < prev->vm_end) /* case 4 */
788 vma_adjust(prev, prev->vm_start,
789 addr, prev->vm_pgoff, NULL);
790 else /* cases 3, 8 */
791 vma_adjust(area, addr, next->vm_end,
792 next->vm_pgoff - pglen, NULL);
793 return area;
794 }
795
796 return NULL;
797}
798
799/*
800 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
801 * neighbouring vmas for a suitable anon_vma, before it goes off
802 * to allocate a new anon_vma. It checks because a repetitive
803 * sequence of mprotects and faults may otherwise lead to distinct
804 * anon_vmas being allocated, preventing vma merge in subsequent
805 * mprotect.
806 */
807struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
808{
809 struct vm_area_struct *near;
810 unsigned long vm_flags;
811
812 near = vma->vm_next;
813 if (!near)
814 goto try_prev;
815
816 /*
817 * Since only mprotect tries to remerge vmas, match flags
818 * which might be mprotected into each other later on.
819 * Neither mlock nor madvise tries to remerge at present,
820 * so leave their flags as obstructing a merge.
821 */
822 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
823 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
824
825 if (near->anon_vma && vma->vm_end == near->vm_start &&
826 mpol_equal(vma_policy(vma), vma_policy(near)) &&
827 can_vma_merge_before(near, vm_flags,
828 NULL, vma->vm_file, vma->vm_pgoff +
829 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
830 return near->anon_vma;
831try_prev:
832 /*
833 * It is potentially slow to have to call find_vma_prev here.
834 * But it's only on the first write fault on the vma, not
835 * every time, and we could devise a way to avoid it later
836 * (e.g. stash info in next's anon_vma_node when assigning
837 * an anon_vma, or when trying vma_merge). Another time.
838 */
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200839 BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!near)
841 goto none;
842
843 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
844 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
845
846 if (near->anon_vma && near->vm_end == vma->vm_start &&
847 mpol_equal(vma_policy(near), vma_policy(vma)) &&
848 can_vma_merge_after(near, vm_flags,
849 NULL, vma->vm_file, vma->vm_pgoff))
850 return near->anon_vma;
851none:
852 /*
853 * There's no absolute need to look only at touching neighbours:
854 * we could search further afield for "compatible" anon_vmas.
855 * But it would probably just be a waste of time searching,
856 * or lead to too many vmas hanging off the same anon_vma.
857 * We're trying to allow mprotect remerging later on,
858 * not trying to minimize memory used for anon_vmas.
859 */
860 return NULL;
861}
862
863#ifdef CONFIG_PROC_FS
Hugh Dickinsab50b8e2005-10-29 18:15:56 -0700864void vm_stat_account(struct mm_struct *mm, unsigned long flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct file *file, long pages)
866{
867 const unsigned long stack_flags
868 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (file) {
871 mm->shared_vm += pages;
872 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
873 mm->exec_vm += pages;
874 } else if (flags & stack_flags)
875 mm->stack_vm += pages;
876 if (flags & (VM_RESERVED|VM_IO))
877 mm->reserved_vm += pages;
878}
879#endif /* CONFIG_PROC_FS */
880
881/*
882 * The caller must hold down_write(current->mm->mmap_sem).
883 */
884
885unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
886 unsigned long len, unsigned long prot,
887 unsigned long flags, unsigned long pgoff)
888{
889 struct mm_struct * mm = current->mm;
890 struct vm_area_struct * vma, * prev;
891 struct inode *inode;
892 unsigned int vm_flags;
893 int correct_wcount = 0;
894 int error;
895 struct rb_node ** rb_link, * rb_parent;
896 int accountable = 1;
897 unsigned long charged = 0, reqprot = prot;
898
899 if (file) {
900 if (is_file_hugepages(file))
901 accountable = 0;
902
903 if (!file->f_op || !file->f_op->mmap)
904 return -ENODEV;
905
906 if ((prot & PROT_EXEC) &&
907 (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
908 return -EPERM;
909 }
910 /*
911 * Does the application expect PROT_READ to imply PROT_EXEC?
912 *
913 * (the exception is when the underlying filesystem is noexec
914 * mounted, in which case we dont add PROT_EXEC.)
915 */
916 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
917 if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
918 prot |= PROT_EXEC;
919
920 if (!len)
921 return -EINVAL;
922
923 /* Careful about overflows.. */
924 len = PAGE_ALIGN(len);
925 if (!len || len > TASK_SIZE)
926 return -ENOMEM;
927
928 /* offset overflow? */
929 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
930 return -EOVERFLOW;
931
932 /* Too many mappings? */
933 if (mm->map_count > sysctl_max_map_count)
934 return -ENOMEM;
935
936 /* Obtain the address to map to. we verify (or select) it and ensure
937 * that it represents a valid section of the address space.
938 */
939 addr = get_unmapped_area(file, addr, len, pgoff, flags);
940 if (addr & ~PAGE_MASK)
941 return addr;
942
943 /* Do simple checking here so the lower-level routines won't have
944 * to. we assume access permissions have been handled by the open
945 * of the memory object, so we don't do any here.
946 */
947 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
948 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
949
950 if (flags & MAP_LOCKED) {
951 if (!can_do_mlock())
952 return -EPERM;
953 vm_flags |= VM_LOCKED;
954 }
955 /* mlock MCL_FUTURE? */
956 if (vm_flags & VM_LOCKED) {
957 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -0700958 locked = len >> PAGE_SHIFT;
959 locked += mm->locked_vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
Chris Wright93ea1d02005-05-01 08:58:38 -0700961 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
963 return -EAGAIN;
964 }
965
966 inode = file ? file->f_dentry->d_inode : NULL;
967
968 if (file) {
969 switch (flags & MAP_TYPE) {
970 case MAP_SHARED:
971 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
972 return -EACCES;
973
974 /*
975 * Make sure we don't allow writing to an append-only
976 * file..
977 */
978 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
979 return -EACCES;
980
981 /*
982 * Make sure there are no mandatory locks on the file.
983 */
984 if (locks_verify_locked(inode))
985 return -EAGAIN;
986
987 vm_flags |= VM_SHARED | VM_MAYSHARE;
988 if (!(file->f_mode & FMODE_WRITE))
989 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
990
991 /* fall through */
992 case MAP_PRIVATE:
993 if (!(file->f_mode & FMODE_READ))
994 return -EACCES;
995 break;
996
997 default:
998 return -EINVAL;
999 }
1000 } else {
1001 switch (flags & MAP_TYPE) {
1002 case MAP_SHARED:
1003 vm_flags |= VM_SHARED | VM_MAYSHARE;
1004 break;
1005 case MAP_PRIVATE:
1006 /*
1007 * Set pgoff according to addr for anon_vma.
1008 */
1009 pgoff = addr >> PAGE_SHIFT;
1010 break;
1011 default:
1012 return -EINVAL;
1013 }
1014 }
1015
1016 error = security_file_mmap(file, reqprot, prot, flags);
1017 if (error)
1018 return error;
1019
1020 /* Clear old maps */
1021 error = -ENOMEM;
1022munmap_back:
1023 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1024 if (vma && vma->vm_start < addr + len) {
1025 if (do_munmap(mm, addr, len))
1026 return -ENOMEM;
1027 goto munmap_back;
1028 }
1029
1030 /* Check against address space limit. */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001031 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -ENOMEM;
1033
1034 if (accountable && (!(flags & MAP_NORESERVE) ||
1035 sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
1036 if (vm_flags & VM_SHARED) {
1037 /* Check memory availability in shmem_file_setup? */
1038 vm_flags |= VM_ACCOUNT;
1039 } else if (vm_flags & VM_WRITE) {
1040 /*
1041 * Private writable mapping: check memory availability
1042 */
1043 charged = len >> PAGE_SHIFT;
1044 if (security_vm_enough_memory(charged))
1045 return -ENOMEM;
1046 vm_flags |= VM_ACCOUNT;
1047 }
1048 }
1049
1050 /*
1051 * Can we just expand an old private anonymous mapping?
1052 * The VM_SHARED test is necessary because shmem_zero_setup
1053 * will create the file object for a shared anonymous map below.
1054 */
1055 if (!file && !(vm_flags & VM_SHARED) &&
1056 vma_merge(mm, prev, addr, addr + len, vm_flags,
1057 NULL, NULL, pgoff, NULL))
1058 goto out;
1059
1060 /*
1061 * Determine the object being mapped and call the appropriate
1062 * specific mapper. the address has already been validated, but
1063 * not unmapped, but the maps are removed from the list.
1064 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001065 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!vma) {
1067 error = -ENOMEM;
1068 goto unacct_error;
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 vma->vm_mm = mm;
1072 vma->vm_start = addr;
1073 vma->vm_end = addr + len;
1074 vma->vm_flags = vm_flags;
David Howells9637a5e2006-06-23 02:03:43 -07001075 vma->vm_page_prot = protection_map[vm_flags &
1076 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 vma->vm_pgoff = pgoff;
1078
1079 if (file) {
1080 error = -EINVAL;
1081 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1082 goto free_vma;
1083 if (vm_flags & VM_DENYWRITE) {
1084 error = deny_write_access(file);
1085 if (error)
1086 goto free_vma;
1087 correct_wcount = 1;
1088 }
1089 vma->vm_file = file;
1090 get_file(file);
1091 error = file->f_op->mmap(file, vma);
1092 if (error)
1093 goto unmap_and_free_vma;
1094 } else if (vm_flags & VM_SHARED) {
1095 error = shmem_zero_setup(vma);
1096 if (error)
1097 goto free_vma;
1098 }
1099
David Howells9637a5e2006-06-23 02:03:43 -07001100 /* Don't make the VMA automatically writable if it's shared, but the
1101 * backer wishes to know when pages are first written to */
1102 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1103 vma->vm_page_prot =
1104 protection_map[vm_flags & (VM_READ|VM_WRITE|VM_EXEC)];
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1107 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1108 * that memory reservation must be checked; but that reservation
1109 * belongs to shared memory object, not to vma: so now clear it.
1110 */
1111 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
1112 vma->vm_flags &= ~VM_ACCOUNT;
1113
1114 /* Can addr have changed??
1115 *
1116 * Answer: Yes, several device drivers can do it in their
1117 * f_op->mmap method. -DaveM
1118 */
1119 addr = vma->vm_start;
1120 pgoff = vma->vm_pgoff;
1121 vm_flags = vma->vm_flags;
1122
1123 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
1124 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
1125 file = vma->vm_file;
1126 vma_link(mm, vma, prev, rb_link, rb_parent);
1127 if (correct_wcount)
1128 atomic_inc(&inode->i_writecount);
1129 } else {
1130 if (file) {
1131 if (correct_wcount)
1132 atomic_inc(&inode->i_writecount);
1133 fput(file);
1134 }
1135 mpol_free(vma_policy(vma));
1136 kmem_cache_free(vm_area_cachep, vma);
1137 }
1138out:
1139 mm->total_vm += len >> PAGE_SHIFT;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001140 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (vm_flags & VM_LOCKED) {
1142 mm->locked_vm += len >> PAGE_SHIFT;
1143 make_pages_present(addr, addr + len);
1144 }
1145 if (flags & MAP_POPULATE) {
1146 up_write(&mm->mmap_sem);
1147 sys_remap_file_pages(addr, len, 0,
1148 pgoff, flags & MAP_NONBLOCK);
1149 down_write(&mm->mmap_sem);
1150 }
1151 return addr;
1152
1153unmap_and_free_vma:
1154 if (correct_wcount)
1155 atomic_inc(&inode->i_writecount);
1156 vma->vm_file = NULL;
1157 fput(file);
1158
1159 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001160 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1161 charged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162free_vma:
1163 kmem_cache_free(vm_area_cachep, vma);
1164unacct_error:
1165 if (charged)
1166 vm_unacct_memory(charged);
1167 return error;
1168}
1169
1170EXPORT_SYMBOL(do_mmap_pgoff);
1171
1172/* Get an address range which is currently unmapped.
1173 * For shmat() with addr=0.
1174 *
1175 * Ugly calling convention alert:
1176 * Return value with the low bits set means error value,
1177 * ie
1178 * if (ret & ~PAGE_MASK)
1179 * error = ret;
1180 *
1181 * This function "knows" that -ENOMEM has the bits set.
1182 */
1183#ifndef HAVE_ARCH_UNMAPPED_AREA
1184unsigned long
1185arch_get_unmapped_area(struct file *filp, unsigned long addr,
1186 unsigned long len, unsigned long pgoff, unsigned long flags)
1187{
1188 struct mm_struct *mm = current->mm;
1189 struct vm_area_struct *vma;
1190 unsigned long start_addr;
1191
1192 if (len > TASK_SIZE)
1193 return -ENOMEM;
1194
1195 if (addr) {
1196 addr = PAGE_ALIGN(addr);
1197 vma = find_vma(mm, addr);
1198 if (TASK_SIZE - len >= addr &&
1199 (!vma || addr + len <= vma->vm_start))
1200 return addr;
1201 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001202 if (len > mm->cached_hole_size) {
1203 start_addr = addr = mm->free_area_cache;
1204 } else {
1205 start_addr = addr = TASK_UNMAPPED_BASE;
1206 mm->cached_hole_size = 0;
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209full_search:
1210 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1211 /* At this point: (!vma || addr < vma->vm_end). */
1212 if (TASK_SIZE - len < addr) {
1213 /*
1214 * Start a new search - just in case we missed
1215 * some holes.
1216 */
1217 if (start_addr != TASK_UNMAPPED_BASE) {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001218 addr = TASK_UNMAPPED_BASE;
1219 start_addr = addr;
1220 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto full_search;
1222 }
1223 return -ENOMEM;
1224 }
1225 if (!vma || addr + len <= vma->vm_start) {
1226 /*
1227 * Remember the place where we stopped the search:
1228 */
1229 mm->free_area_cache = addr + len;
1230 return addr;
1231 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001232 if (addr + mm->cached_hole_size < vma->vm_start)
1233 mm->cached_hole_size = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 addr = vma->vm_end;
1235 }
1236}
1237#endif
1238
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001239void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 /*
1242 * Is this a new hole at the lowest possible address?
1243 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001244 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
1245 mm->free_area_cache = addr;
1246 mm->cached_hole_size = ~0UL;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/*
1251 * This mmap-allocator allocates new areas top-down from below the
1252 * stack's low limit (the base):
1253 */
1254#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1255unsigned long
1256arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1257 const unsigned long len, const unsigned long pgoff,
1258 const unsigned long flags)
1259{
1260 struct vm_area_struct *vma;
1261 struct mm_struct *mm = current->mm;
1262 unsigned long addr = addr0;
1263
1264 /* requested length too big for entire address space */
1265 if (len > TASK_SIZE)
1266 return -ENOMEM;
1267
1268 /* requesting a specific address */
1269 if (addr) {
1270 addr = PAGE_ALIGN(addr);
1271 vma = find_vma(mm, addr);
1272 if (TASK_SIZE - len >= addr &&
1273 (!vma || addr + len <= vma->vm_start))
1274 return addr;
1275 }
1276
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001277 /* check if free_area_cache is useful for us */
1278 if (len <= mm->cached_hole_size) {
1279 mm->cached_hole_size = 0;
1280 mm->free_area_cache = mm->mmap_base;
1281 }
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* either no address requested or can't fit in requested address hole */
1284 addr = mm->free_area_cache;
1285
1286 /* make sure it can fit in the remaining address space */
Linus Torvalds49a43872005-05-18 15:39:33 -07001287 if (addr > len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 vma = find_vma(mm, addr-len);
1289 if (!vma || addr <= vma->vm_start)
1290 /* remember the address as a hint for next time */
1291 return (mm->free_area_cache = addr-len);
1292 }
1293
Chris Wright73219d12005-06-21 17:14:52 -07001294 if (mm->mmap_base < len)
1295 goto bottomup;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 addr = mm->mmap_base-len;
1298
1299 do {
1300 /*
1301 * Lookup failure means no vma is above this address,
1302 * else if new region fits below vma->vm_start,
1303 * return with success:
1304 */
1305 vma = find_vma(mm, addr);
1306 if (!vma || addr+len <= vma->vm_start)
1307 /* remember the address as a hint for next time */
1308 return (mm->free_area_cache = addr);
1309
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001310 /* remember the largest hole we saw so far */
1311 if (addr + mm->cached_hole_size < vma->vm_start)
1312 mm->cached_hole_size = vma->vm_start - addr;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* try just below the current vma->vm_start */
1315 addr = vma->vm_start-len;
Linus Torvalds49a43872005-05-18 15:39:33 -07001316 } while (len < vma->vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Chris Wright73219d12005-06-21 17:14:52 -07001318bottomup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
1320 * A failed mmap() very likely causes application failure,
1321 * so fall back to the bottom-up function here. This scenario
1322 * can happen with large stack limits and large mmap()
1323 * allocations.
1324 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001325 mm->cached_hole_size = ~0UL;
1326 mm->free_area_cache = TASK_UNMAPPED_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1328 /*
1329 * Restore the topdown base:
1330 */
1331 mm->free_area_cache = mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001332 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 return addr;
1335}
1336#endif
1337
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001338void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 /*
1341 * Is this a new hole at the highest possible address?
1342 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001343 if (addr > mm->free_area_cache)
1344 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* dont allow allocations above current base */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001347 if (mm->free_area_cache > mm->mmap_base)
1348 mm->free_area_cache = mm->mmap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351unsigned long
1352get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1353 unsigned long pgoff, unsigned long flags)
1354{
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001355 unsigned long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001357 if (!(flags & MAP_FIXED)) {
1358 unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1359
1360 get_area = current->mm->get_unmapped_area;
1361 if (file && file->f_op && file->f_op->get_unmapped_area)
1362 get_area = file->f_op->get_unmapped_area;
1363 addr = get_area(file, addr, len, pgoff, flags);
1364 if (IS_ERR_VALUE(addr))
1365 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001368 if (addr > TASK_SIZE - len)
1369 return -ENOMEM;
1370 if (addr & ~PAGE_MASK)
1371 return -EINVAL;
1372 if (file && is_file_hugepages(file)) {
1373 /*
1374 * Check if the given range is hugepage aligned, and
1375 * can be made suitable for hugepages.
1376 */
1377 ret = prepare_hugepage_range(addr, len);
1378 } else {
1379 /*
1380 * Ensure that a normal request is not falling in a
1381 * reserved hugepage range. For some archs like IA-64,
1382 * there is a separate region for hugepages.
1383 */
1384 ret = is_hugepage_only_range(current->mm, addr, len);
1385 }
1386 if (ret)
1387 return -EINVAL;
1388 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391EXPORT_SYMBOL(get_unmapped_area);
1392
1393/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1394struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1395{
1396 struct vm_area_struct *vma = NULL;
1397
1398 if (mm) {
1399 /* Check the cache first. */
1400 /* (Cache hit rate is typically around 35%.) */
1401 vma = mm->mmap_cache;
1402 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1403 struct rb_node * rb_node;
1404
1405 rb_node = mm->mm_rb.rb_node;
1406 vma = NULL;
1407
1408 while (rb_node) {
1409 struct vm_area_struct * vma_tmp;
1410
1411 vma_tmp = rb_entry(rb_node,
1412 struct vm_area_struct, vm_rb);
1413
1414 if (vma_tmp->vm_end > addr) {
1415 vma = vma_tmp;
1416 if (vma_tmp->vm_start <= addr)
1417 break;
1418 rb_node = rb_node->rb_left;
1419 } else
1420 rb_node = rb_node->rb_right;
1421 }
1422 if (vma)
1423 mm->mmap_cache = vma;
1424 }
1425 }
1426 return vma;
1427}
1428
1429EXPORT_SYMBOL(find_vma);
1430
1431/* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1432struct vm_area_struct *
1433find_vma_prev(struct mm_struct *mm, unsigned long addr,
1434 struct vm_area_struct **pprev)
1435{
1436 struct vm_area_struct *vma = NULL, *prev = NULL;
1437 struct rb_node * rb_node;
1438 if (!mm)
1439 goto out;
1440
1441 /* Guard against addr being lower than the first VMA */
1442 vma = mm->mmap;
1443
1444 /* Go through the RB tree quickly. */
1445 rb_node = mm->mm_rb.rb_node;
1446
1447 while (rb_node) {
1448 struct vm_area_struct *vma_tmp;
1449 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1450
1451 if (addr < vma_tmp->vm_end) {
1452 rb_node = rb_node->rb_left;
1453 } else {
1454 prev = vma_tmp;
1455 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1456 break;
1457 rb_node = rb_node->rb_right;
1458 }
1459 }
1460
1461out:
1462 *pprev = prev;
1463 return prev ? prev->vm_next : vma;
1464}
1465
1466/*
1467 * Verify that the stack growth is acceptable and
1468 * update accounting. This is shared with both the
1469 * grow-up and grow-down cases.
1470 */
1471static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
1472{
1473 struct mm_struct *mm = vma->vm_mm;
1474 struct rlimit *rlim = current->signal->rlim;
1475
1476 /* address space limit tests */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001477 if (!may_expand_vm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -ENOMEM;
1479
1480 /* Stack limit test */
1481 if (size > rlim[RLIMIT_STACK].rlim_cur)
1482 return -ENOMEM;
1483
1484 /* mlock limit tests */
1485 if (vma->vm_flags & VM_LOCKED) {
1486 unsigned long locked;
1487 unsigned long limit;
1488 locked = mm->locked_vm + grow;
1489 limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
1490 if (locked > limit && !capable(CAP_IPC_LOCK))
1491 return -ENOMEM;
1492 }
1493
1494 /*
1495 * Overcommit.. This must be the final test, as it will
1496 * update security statistics.
1497 */
1498 if (security_vm_enough_memory(grow))
1499 return -ENOMEM;
1500
1501 /* Ok, everything looks good - let it rip */
1502 mm->total_vm += grow;
1503 if (vma->vm_flags & VM_LOCKED)
1504 mm->locked_vm += grow;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001505 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
1507}
1508
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001509#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001511 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1512 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 */
Matthew Wilcox9ab88512005-11-18 16:16:42 -05001514#ifndef CONFIG_IA64
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001515static inline
1516#endif
1517int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 int error;
1520
1521 if (!(vma->vm_flags & VM_GROWSUP))
1522 return -EFAULT;
1523
1524 /*
1525 * We must make sure the anon_vma is allocated
1526 * so that the anon_vma locking is not a noop.
1527 */
1528 if (unlikely(anon_vma_prepare(vma)))
1529 return -ENOMEM;
1530 anon_vma_lock(vma);
1531
1532 /*
1533 * vma->vm_start/vm_end cannot change under us because the caller
1534 * is required to hold the mmap_sem in read mode. We need the
1535 * anon_vma lock to serialize against concurrent expand_stacks.
1536 */
1537 address += 4 + PAGE_SIZE - 1;
1538 address &= PAGE_MASK;
1539 error = 0;
1540
1541 /* Somebody else might have raced and expanded it already */
1542 if (address > vma->vm_end) {
1543 unsigned long size, grow;
1544
1545 size = address - vma->vm_start;
1546 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1547
1548 error = acct_stack_growth(vma, size, grow);
1549 if (!error)
1550 vma->vm_end = address;
1551 }
1552 anon_vma_unlock(vma);
1553 return error;
1554}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001555#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1556
1557#ifdef CONFIG_STACK_GROWSUP
1558int expand_stack(struct vm_area_struct *vma, unsigned long address)
1559{
1560 return expand_upwards(vma, address);
1561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563struct vm_area_struct *
1564find_extend_vma(struct mm_struct *mm, unsigned long addr)
1565{
1566 struct vm_area_struct *vma, *prev;
1567
1568 addr &= PAGE_MASK;
1569 vma = find_vma_prev(mm, addr, &prev);
1570 if (vma && (vma->vm_start <= addr))
1571 return vma;
1572 if (!prev || expand_stack(prev, addr))
1573 return NULL;
1574 if (prev->vm_flags & VM_LOCKED) {
1575 make_pages_present(addr, prev->vm_end);
1576 }
1577 return prev;
1578}
1579#else
1580/*
1581 * vma is the first one with address < vma->vm_start. Have to extend vma.
1582 */
1583int expand_stack(struct vm_area_struct *vma, unsigned long address)
1584{
1585 int error;
1586
1587 /*
1588 * We must make sure the anon_vma is allocated
1589 * so that the anon_vma locking is not a noop.
1590 */
1591 if (unlikely(anon_vma_prepare(vma)))
1592 return -ENOMEM;
1593 anon_vma_lock(vma);
1594
1595 /*
1596 * vma->vm_start/vm_end cannot change under us because the caller
1597 * is required to hold the mmap_sem in read mode. We need the
1598 * anon_vma lock to serialize against concurrent expand_stacks.
1599 */
1600 address &= PAGE_MASK;
1601 error = 0;
1602
1603 /* Somebody else might have raced and expanded it already */
1604 if (address < vma->vm_start) {
1605 unsigned long size, grow;
1606
1607 size = vma->vm_end - address;
1608 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1609
1610 error = acct_stack_growth(vma, size, grow);
1611 if (!error) {
1612 vma->vm_start = address;
1613 vma->vm_pgoff -= grow;
1614 }
1615 }
1616 anon_vma_unlock(vma);
1617 return error;
1618}
1619
1620struct vm_area_struct *
1621find_extend_vma(struct mm_struct * mm, unsigned long addr)
1622{
1623 struct vm_area_struct * vma;
1624 unsigned long start;
1625
1626 addr &= PAGE_MASK;
1627 vma = find_vma(mm,addr);
1628 if (!vma)
1629 return NULL;
1630 if (vma->vm_start <= addr)
1631 return vma;
1632 if (!(vma->vm_flags & VM_GROWSDOWN))
1633 return NULL;
1634 start = vma->vm_start;
1635 if (expand_stack(vma, addr))
1636 return NULL;
1637 if (vma->vm_flags & VM_LOCKED) {
1638 make_pages_present(addr, start);
1639 }
1640 return vma;
1641}
1642#endif
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07001645 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07001647 *
1648 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07001650static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Hugh Dickins365e9c872005-10-29 18:16:18 -07001652 /* Update high watermark before we lower total_vm */
1653 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07001655 long nrpages = vma_pages(vma);
1656
1657 mm->total_vm -= nrpages;
1658 if (vma->vm_flags & VM_LOCKED)
1659 mm->locked_vm -= nrpages;
1660 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07001661 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07001662 } while (vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 validate_mm(mm);
1664}
1665
1666/*
1667 * Get rid of page table information in the indicated region.
1668 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07001669 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
1671static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07001672 struct vm_area_struct *vma, struct vm_area_struct *prev,
1673 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Hugh Dickinse0da3822005-04-19 13:29:15 -07001675 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct mmu_gather *tlb;
1677 unsigned long nr_accounted = 0;
1678
1679 lru_add_drain();
1680 tlb = tlb_gather_mmu(mm, 0);
Hugh Dickins365e9c872005-10-29 18:16:18 -07001681 update_hiwater_rss(mm);
Hugh Dickins508034a2005-10-29 18:16:30 -07001682 unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 vm_unacct_memory(nr_accounted);
Hugh Dickinse2cdef82005-04-19 13:29:19 -07001684 free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
Hugh Dickinse0da3822005-04-19 13:29:15 -07001685 next? next->vm_start: 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 tlb_finish_mmu(tlb, start, end);
1687}
1688
1689/*
1690 * Create a list of vma's touched by the unmap, removing them from the mm's
1691 * vma list as we go..
1692 */
1693static void
1694detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1695 struct vm_area_struct *prev, unsigned long end)
1696{
1697 struct vm_area_struct **insertion_point;
1698 struct vm_area_struct *tail_vma = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001699 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1702 do {
1703 rb_erase(&vma->vm_rb, &mm->mm_rb);
1704 mm->map_count--;
1705 tail_vma = vma;
1706 vma = vma->vm_next;
1707 } while (vma && vma->vm_start < end);
1708 *insertion_point = vma;
1709 tail_vma->vm_next = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001710 if (mm->unmap_area == arch_unmap_area)
1711 addr = prev ? prev->vm_end : mm->mmap_base;
1712 else
1713 addr = vma ? vma->vm_start : mm->mmap_base;
1714 mm->unmap_area(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 mm->mmap_cache = NULL; /* Kill the cache. */
1716}
1717
1718/*
1719 * Split a vma into two pieces at address 'addr', a new vma is allocated
1720 * either for the first part or the the tail.
1721 */
1722int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1723 unsigned long addr, int new_below)
1724{
1725 struct mempolicy *pol;
1726 struct vm_area_struct *new;
1727
1728 if (is_vm_hugetlb_page(vma) && (addr & ~HPAGE_MASK))
1729 return -EINVAL;
1730
1731 if (mm->map_count >= sysctl_max_map_count)
1732 return -ENOMEM;
1733
1734 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1735 if (!new)
1736 return -ENOMEM;
1737
1738 /* most fields are the same, copy all, and then fixup */
1739 *new = *vma;
1740
1741 if (new_below)
1742 new->vm_end = addr;
1743 else {
1744 new->vm_start = addr;
1745 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1746 }
1747
1748 pol = mpol_copy(vma_policy(vma));
1749 if (IS_ERR(pol)) {
1750 kmem_cache_free(vm_area_cachep, new);
1751 return PTR_ERR(pol);
1752 }
1753 vma_set_policy(new, pol);
1754
1755 if (new->vm_file)
1756 get_file(new->vm_file);
1757
1758 if (new->vm_ops && new->vm_ops->open)
1759 new->vm_ops->open(new);
1760
1761 if (new_below)
1762 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1763 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1764 else
1765 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1766
1767 return 0;
1768}
1769
1770/* Munmap is split into 2 main parts -- this part which finds
1771 * what needs doing, and the areas themselves, which do the
1772 * work. This now handles partial unmappings.
1773 * Jeremy Fitzhardinge <jeremy@goop.org>
1774 */
1775int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1776{
1777 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07001778 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1781 return -EINVAL;
1782
1783 if ((len = PAGE_ALIGN(len)) == 0)
1784 return -EINVAL;
1785
1786 /* Find the first overlapping VMA */
Hugh Dickins146425a2005-04-19 13:29:18 -07001787 vma = find_vma_prev(mm, start, &prev);
1788 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
Hugh Dickins146425a2005-04-19 13:29:18 -07001790 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /* if it doesn't overlap, we have nothing.. */
1793 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07001794 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return 0;
1796
1797 /*
1798 * If we need to split any vma, do it now to save pain later.
1799 *
1800 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1801 * unmapped vm_area_struct will remain in use: so lower split_vma
1802 * places tmp vma above, and higher split_vma places tmp vma below.
1803 */
Hugh Dickins146425a2005-04-19 13:29:18 -07001804 if (start > vma->vm_start) {
1805 int error = split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (error)
1807 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07001808 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
1811 /* Does it split the last one? */
1812 last = find_vma(mm, end);
1813 if (last && end > last->vm_start) {
1814 int error = split_vma(mm, last, end, 1);
1815 if (error)
1816 return error;
1817 }
Hugh Dickins146425a2005-04-19 13:29:18 -07001818 vma = prev? prev->vm_next: mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 /*
1821 * Remove the vma's, and unmap the actual pages
1822 */
Hugh Dickins146425a2005-04-19 13:29:18 -07001823 detach_vmas_to_be_unmapped(mm, vma, prev, end);
1824 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07001827 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 return 0;
1830}
1831
1832EXPORT_SYMBOL(do_munmap);
1833
1834asmlinkage long sys_munmap(unsigned long addr, size_t len)
1835{
1836 int ret;
1837 struct mm_struct *mm = current->mm;
1838
1839 profile_munmap(addr);
1840
1841 down_write(&mm->mmap_sem);
1842 ret = do_munmap(mm, addr, len);
1843 up_write(&mm->mmap_sem);
1844 return ret;
1845}
1846
1847static inline void verify_mm_writelocked(struct mm_struct *mm)
1848{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08001849#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
1851 WARN_ON(1);
1852 up_read(&mm->mmap_sem);
1853 }
1854#endif
1855}
1856
1857/*
1858 * this is really a simplified "do_mmap". it only handles
1859 * anonymous maps. eventually we may be able to do some
1860 * brk-specific accounting here.
1861 */
1862unsigned long do_brk(unsigned long addr, unsigned long len)
1863{
1864 struct mm_struct * mm = current->mm;
1865 struct vm_area_struct * vma, * prev;
1866 unsigned long flags;
1867 struct rb_node ** rb_link, * rb_parent;
1868 pgoff_t pgoff = addr >> PAGE_SHIFT;
1869
1870 len = PAGE_ALIGN(len);
1871 if (!len)
1872 return addr;
1873
1874 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1875 return -EINVAL;
1876
1877 /*
1878 * mlock MCL_FUTURE?
1879 */
1880 if (mm->def_flags & VM_LOCKED) {
1881 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07001882 locked = len >> PAGE_SHIFT;
1883 locked += mm->locked_vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
Chris Wright93ea1d02005-05-01 08:58:38 -07001885 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1887 return -EAGAIN;
1888 }
1889
1890 /*
1891 * mm->mmap_sem is required to protect against another thread
1892 * changing the mappings in case we sleep.
1893 */
1894 verify_mm_writelocked(mm);
1895
1896 /*
1897 * Clear old maps. this also does some error checking for us
1898 */
1899 munmap_back:
1900 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1901 if (vma && vma->vm_start < addr + len) {
1902 if (do_munmap(mm, addr, len))
1903 return -ENOMEM;
1904 goto munmap_back;
1905 }
1906
1907 /* Check against address space limits *after* clearing old maps... */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001908 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return -ENOMEM;
1910
1911 if (mm->map_count > sysctl_max_map_count)
1912 return -ENOMEM;
1913
1914 if (security_vm_enough_memory(len >> PAGE_SHIFT))
1915 return -ENOMEM;
1916
1917 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1918
1919 /* Can we just expand an old private anonymous mapping? */
1920 if (vma_merge(mm, prev, addr, addr + len, flags,
1921 NULL, NULL, pgoff, NULL))
1922 goto out;
1923
1924 /*
1925 * create a vma struct for an anonymous mapping
1926 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001927 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (!vma) {
1929 vm_unacct_memory(len >> PAGE_SHIFT);
1930 return -ENOMEM;
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 vma->vm_mm = mm;
1934 vma->vm_start = addr;
1935 vma->vm_end = addr + len;
1936 vma->vm_pgoff = pgoff;
1937 vma->vm_flags = flags;
David Howells9637a5e2006-06-23 02:03:43 -07001938 vma->vm_page_prot = protection_map[flags &
1939 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 vma_link(mm, vma, prev, rb_link, rb_parent);
1941out:
1942 mm->total_vm += len >> PAGE_SHIFT;
1943 if (flags & VM_LOCKED) {
1944 mm->locked_vm += len >> PAGE_SHIFT;
1945 make_pages_present(addr, addr + len);
1946 }
1947 return addr;
1948}
1949
1950EXPORT_SYMBOL(do_brk);
1951
1952/* Release all mmaps. */
1953void exit_mmap(struct mm_struct *mm)
1954{
1955 struct mmu_gather *tlb;
Hugh Dickinse0da3822005-04-19 13:29:15 -07001956 struct vm_area_struct *vma = mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 unsigned long nr_accounted = 0;
Hugh Dickinsee39b372005-04-19 13:29:15 -07001958 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 flush_cache_mm(mm);
Hugh Dickinse0da3822005-04-19 13:29:15 -07001962 tlb = tlb_gather_mmu(mm, 1);
Hugh Dickins365e9c872005-10-29 18:16:18 -07001963 /* Don't update_hiwater_rss(mm) here, do_exit already did */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001964 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Hugh Dickins508034a2005-10-29 18:16:30 -07001965 end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 vm_unacct_memory(nr_accounted);
Hugh Dickinse2cdef82005-04-19 13:29:19 -07001967 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
Hugh Dickinsee39b372005-04-19 13:29:15 -07001968 tlb_finish_mmu(tlb, 0, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07001971 * Walk the list again, actually closing and freeing it,
1972 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07001974 while (vma)
1975 vma = remove_vma(vma);
Hugh Dickinse0da3822005-04-19 13:29:15 -07001976
Hugh Dickinse2cdef82005-04-19 13:29:19 -07001977 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980/* Insert vm structure into process list sorted by address
1981 * and into the inode's i_mmap tree. If vm_file is non-NULL
1982 * then i_mmap_lock is taken here.
1983 */
1984int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1985{
1986 struct vm_area_struct * __vma, * prev;
1987 struct rb_node ** rb_link, * rb_parent;
1988
1989 /*
1990 * The vm_pgoff of a purely anonymous vma should be irrelevant
1991 * until its first write fault, when page's anon_vma and index
1992 * are set. But now set the vm_pgoff it will almost certainly
1993 * end up with (unless mremap moves it elsewhere before that
1994 * first wfault), so /proc/pid/maps tells a consistent story.
1995 *
1996 * By setting it to reflect the virtual start address of the
1997 * vma, merges and splits can happen in a seamless way, just
1998 * using the existing file pgoff checks and manipulations.
1999 * Similarly in do_mmap_pgoff and in do_brk.
2000 */
2001 if (!vma->vm_file) {
2002 BUG_ON(vma->anon_vma);
2003 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2004 }
2005 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2006 if (__vma && __vma->vm_start < vma->vm_end)
2007 return -ENOMEM;
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002008 if ((vma->vm_flags & VM_ACCOUNT) &&
2009 security_vm_enough_memory(vma_pages(vma)))
2010 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 vma_link(mm, vma, prev, rb_link, rb_parent);
2012 return 0;
2013}
2014
2015/*
2016 * Copy the vma structure to a new location in the same mm,
2017 * prior to moving page table entries, to effect an mremap move.
2018 */
2019struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2020 unsigned long addr, unsigned long len, pgoff_t pgoff)
2021{
2022 struct vm_area_struct *vma = *vmap;
2023 unsigned long vma_start = vma->vm_start;
2024 struct mm_struct *mm = vma->vm_mm;
2025 struct vm_area_struct *new_vma, *prev;
2026 struct rb_node **rb_link, *rb_parent;
2027 struct mempolicy *pol;
2028
2029 /*
2030 * If anonymous vma has not yet been faulted, update new pgoff
2031 * to match new location, to increase its chance of merging.
2032 */
2033 if (!vma->vm_file && !vma->anon_vma)
2034 pgoff = addr >> PAGE_SHIFT;
2035
2036 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2037 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2038 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2039 if (new_vma) {
2040 /*
2041 * Source vma may have been merged into new_vma
2042 */
2043 if (vma_start >= new_vma->vm_start &&
2044 vma_start < new_vma->vm_end)
2045 *vmap = new_vma;
2046 } else {
2047 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2048 if (new_vma) {
2049 *new_vma = *vma;
2050 pol = mpol_copy(vma_policy(vma));
2051 if (IS_ERR(pol)) {
2052 kmem_cache_free(vm_area_cachep, new_vma);
2053 return NULL;
2054 }
2055 vma_set_policy(new_vma, pol);
2056 new_vma->vm_start = addr;
2057 new_vma->vm_end = addr + len;
2058 new_vma->vm_pgoff = pgoff;
2059 if (new_vma->vm_file)
2060 get_file(new_vma->vm_file);
2061 if (new_vma->vm_ops && new_vma->vm_ops->open)
2062 new_vma->vm_ops->open(new_vma);
2063 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2064 }
2065 }
2066 return new_vma;
2067}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002068
2069/*
2070 * Return true if the calling process may expand its vm space by the passed
2071 * number of pages
2072 */
2073int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2074{
2075 unsigned long cur = mm->total_vm; /* pages */
2076 unsigned long lim;
2077
2078 lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2079
2080 if (cur + npages > lim)
2081 return 0;
2082 return 1;
2083}