6 * Address space accounting code <alan@redhat.com>
9 #include <linux/slab.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>
16 #include <linux/init.h>
17 #include <linux/file.h>
19 #include <linux/personality.h>
20 #include <linux/security.h>
21 #include <linux/hugetlb.h>
22 #include <linux/profile.h>
23 #include <linux/module.h>
24 #include <linux/mount.h>
25 #include <linux/mempolicy.h>
26 #include <linux/rmap.h>
27 #include <linux/random.h>
29 #include <asm/uaccess.h>
30 #include <asm/cacheflush.h>
33 static 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);
38 * WARNING: the debugging will use recursive algorithms so never enable this
39 * unless you know what you are doing.
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:
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
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
58 pgprot_t protection_map[16] = {
59 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
60 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
63 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
64 int sysctl_overcommit_ratio = 50; /* default is 50% */
65 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
66 atomic_t vm_committed_space = ATOMIC_INIT(0);
69 * Check that a process has enough memory to allocate a new virtual
70 * mapping. 0 means there is enough memory for the allocation to
71 * succeed and -ENOMEM implies there is not.
73 * We currently support three overcommit policies, which are set via the
74 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
76 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
77 * Additional code 2002 Jul 20 by Robert Love.
79 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
81 * Note this is a helper function intended to be used by LSMs which
82 * wish to use this logic.
84 int __vm_enough_memory(long pages, int cap_sys_admin)
86 unsigned long free, allowed;
88 vm_acct_memory(pages);
91 * Sometimes we want to use more memory than we have
93 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
96 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
99 free = get_page_cache_size();
100 free += nr_swap_pages;
103 * Any slabs which are created with the
104 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
105 * which are reclaimable, under pressure. The dentry
106 * cache and most inode caches should fall into this
108 free += atomic_read(&slab_reclaim_pages);
111 * Leave the last 3% for root
120 * nr_free_pages() is very expensive on large systems,
121 * only call if we're about to fail.
130 vm_unacct_memory(pages);
134 allowed = (totalram_pages - hugetlb_total_pages())
135 * sysctl_overcommit_ratio / 100;
137 * Leave the last 3% for root
140 allowed -= allowed / 32;
141 allowed += total_swap_pages;
143 /* Don't let a single process grow too big:
144 leave 3% of the size of this process for other processes */
145 allowed -= current->mm->total_vm / 32;
147 if (atomic_read(&vm_committed_space) < allowed)
150 vm_unacct_memory(pages);
155 EXPORT_SYMBOL(sysctl_overcommit_memory);
156 EXPORT_SYMBOL(sysctl_overcommit_ratio);
157 EXPORT_SYMBOL(sysctl_max_map_count);
158 EXPORT_SYMBOL(vm_committed_space);
159 EXPORT_SYMBOL(__vm_enough_memory);
162 * Requires inode->i_mapping->i_mmap_lock
164 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
165 struct file *file, struct address_space *mapping)
167 if (vma->vm_flags & VM_DENYWRITE)
168 atomic_inc(&file->f_dentry->d_inode->i_writecount);
169 if (vma->vm_flags & VM_SHARED)
170 mapping->i_mmap_writable--;
172 flush_dcache_mmap_lock(mapping);
173 if (unlikely(vma->vm_flags & VM_NONLINEAR))
174 list_del_init(&vma->shared.vm_set.list);
176 vma_prio_tree_remove(vma, &mapping->i_mmap);
177 flush_dcache_mmap_unlock(mapping);
181 * Remove one vm structure and free it.
183 static void remove_vm_struct(struct vm_area_struct *vma)
185 struct file *file = vma->vm_file;
189 struct address_space *mapping = file->f_mapping;
190 spin_lock(&mapping->i_mmap_lock);
191 __remove_shared_vm_struct(vma, file, mapping);
192 spin_unlock(&mapping->i_mmap_lock);
194 if (vma->vm_ops && vma->vm_ops->close)
195 vma->vm_ops->close(vma);
198 anon_vma_unlink(vma);
199 mpol_free(vma_policy(vma));
200 kmem_cache_free(vm_area_cachep, vma);
204 * sys_brk() for the most part doesn't need the global kernel
205 * lock, except when an application is doing something nasty
206 * like trying to un-brk an area that has already been mapped
207 * to a regular file. in this case, the unmapping will need
208 * to invoke file system routines that need the global lock.
210 asmlinkage unsigned long sys_brk(unsigned long brk)
212 unsigned long rlim, retval;
213 unsigned long newbrk, oldbrk;
214 struct mm_struct *mm = current->mm;
216 down_write(&mm->mmap_sem);
218 if (brk < mm->end_code)
220 newbrk = PAGE_ALIGN(brk);
221 oldbrk = PAGE_ALIGN(mm->brk);
222 if (oldbrk == newbrk)
225 /* Always allow shrinking brk. */
226 if (brk <= mm->brk) {
227 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
232 /* Check against rlimit.. */
233 rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
234 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
237 /* Check against existing mmap mappings. */
238 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
241 /* Ok, looks good - let it rip. */
242 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
248 up_write(&mm->mmap_sem);
253 static int browse_rb(struct rb_root *root)
256 struct rb_node *nd, *pn = NULL;
257 unsigned long prev = 0, pend = 0;
259 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
260 struct vm_area_struct *vma;
261 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
262 if (vma->vm_start < prev)
263 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
264 if (vma->vm_start < pend)
265 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
266 if (vma->vm_start > vma->vm_end)
267 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
272 for (nd = pn; nd; nd = rb_prev(nd)) {
276 printk("backwards %d, forwards %d\n", j, i), i = 0;
280 void validate_mm(struct mm_struct *mm)
284 struct vm_area_struct *tmp = mm->mmap;
289 if (i != mm->map_count)
290 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
291 i = browse_rb(&mm->mm_rb);
292 if (i != mm->map_count)
293 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
298 #define validate_mm(mm) do { } while (0)
301 static struct vm_area_struct *
302 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
303 struct vm_area_struct **pprev, struct rb_node ***rb_link,
304 struct rb_node ** rb_parent)
306 struct vm_area_struct * vma;
307 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
309 __rb_link = &mm->mm_rb.rb_node;
310 rb_prev = __rb_parent = NULL;
314 struct vm_area_struct *vma_tmp;
316 __rb_parent = *__rb_link;
317 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
319 if (vma_tmp->vm_end > addr) {
321 if (vma_tmp->vm_start <= addr)
323 __rb_link = &__rb_parent->rb_left;
325 rb_prev = __rb_parent;
326 __rb_link = &__rb_parent->rb_right;
332 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
333 *rb_link = __rb_link;
334 *rb_parent = __rb_parent;
339 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
340 struct vm_area_struct *prev, struct rb_node *rb_parent)
342 if (vma->vm_flags & VM_EXEC)
343 arch_add_exec_range(mm, vma->vm_end);
345 vma->vm_next = prev->vm_next;
350 vma->vm_next = rb_entry(rb_parent,
351 struct vm_area_struct, vm_rb);
357 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
358 struct rb_node **rb_link, struct rb_node *rb_parent)
360 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
361 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
364 static inline void __vma_link_file(struct vm_area_struct *vma)
370 struct address_space *mapping = file->f_mapping;
372 if (vma->vm_flags & VM_DENYWRITE)
373 atomic_dec(&file->f_dentry->d_inode->i_writecount);
374 if (vma->vm_flags & VM_SHARED)
375 mapping->i_mmap_writable++;
377 flush_dcache_mmap_lock(mapping);
378 if (unlikely(vma->vm_flags & VM_NONLINEAR))
379 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
381 vma_prio_tree_insert(vma, &mapping->i_mmap);
382 flush_dcache_mmap_unlock(mapping);
387 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
388 struct vm_area_struct *prev, struct rb_node **rb_link,
389 struct rb_node *rb_parent)
391 __vma_link_list(mm, vma, prev, rb_parent);
392 __vma_link_rb(mm, vma, rb_link, rb_parent);
393 __anon_vma_link(vma);
396 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
397 struct vm_area_struct *prev, struct rb_node **rb_link,
398 struct rb_node *rb_parent)
400 struct address_space *mapping = NULL;
403 mapping = vma->vm_file->f_mapping;
406 spin_lock(&mapping->i_mmap_lock);
407 vma->vm_truncate_count = mapping->truncate_count;
411 __vma_link(mm, vma, prev, rb_link, rb_parent);
412 __vma_link_file(vma);
414 anon_vma_unlock(vma);
416 spin_unlock(&mapping->i_mmap_lock);
423 * Helper for vma_adjust in the split_vma insert case:
424 * insert vm structure into list and rbtree and anon_vma,
425 * but it has already been inserted into prio_tree earlier.
428 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
430 struct vm_area_struct * __vma, * prev;
431 struct rb_node ** rb_link, * rb_parent;
433 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
434 if (__vma && __vma->vm_start < vma->vm_end)
436 __vma_link(mm, vma, prev, rb_link, rb_parent);
441 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
442 struct vm_area_struct *prev)
444 prev->vm_next = vma->vm_next;
445 rb_erase(&vma->vm_rb, &mm->mm_rb);
446 if (mm->mmap_cache == vma)
447 mm->mmap_cache = prev;
448 if (vma->vm_flags & VM_EXEC)
449 arch_remove_exec_range(mm, vma->vm_end);
453 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
454 * is already present in an i_mmap tree without adjusting the tree.
455 * The following helper function should be used when such adjustments
456 * are necessary. The "insert" vma (if any) is to be inserted
457 * before we drop the necessary locks.
459 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
460 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
462 struct mm_struct *mm = vma->vm_mm;
463 struct vm_area_struct *next = vma->vm_next;
464 struct vm_area_struct *importer = NULL;
465 struct address_space *mapping = NULL;
466 struct prio_tree_root *root = NULL;
467 struct file *file = vma->vm_file;
468 struct anon_vma *anon_vma = NULL;
469 long adjust_next = 0;
472 if (next && !insert) {
473 if (end >= next->vm_end) {
475 * vma expands, overlapping all the next, and
476 * perhaps the one after too (mprotect case 6).
478 again: remove_next = 1 + (end > next->vm_end);
480 anon_vma = next->anon_vma;
482 } else if (end > next->vm_start) {
484 * vma expands, overlapping part of the next:
485 * mprotect case 5 shifting the boundary up.
487 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
488 anon_vma = next->anon_vma;
490 } else if (end < vma->vm_end) {
492 * vma shrinks, and !insert tells it's not
493 * split_vma inserting another: so it must be
494 * mprotect case 4 shifting the boundary down.
496 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
497 anon_vma = next->anon_vma;
503 mapping = file->f_mapping;
504 if (!(vma->vm_flags & VM_NONLINEAR))
505 root = &mapping->i_mmap;
506 spin_lock(&mapping->i_mmap_lock);
508 vma->vm_truncate_count != next->vm_truncate_count) {
510 * unmap_mapping_range might be in progress:
511 * ensure that the expanding vma is rescanned.
513 importer->vm_truncate_count = 0;
516 insert->vm_truncate_count = vma->vm_truncate_count;
518 * Put into prio_tree now, so instantiated pages
519 * are visible to arm/parisc __flush_dcache_page
520 * throughout; but we cannot insert into address
521 * space until vma start or end is updated.
523 __vma_link_file(insert);
528 * When changing only vma->vm_end, we don't really need
529 * anon_vma lock: but is that case worth optimizing out?
532 anon_vma = vma->anon_vma;
534 spin_lock(&anon_vma->lock);
536 * Easily overlooked: when mprotect shifts the boundary,
537 * make sure the expanding vma has anon_vma set if the
538 * shrinking vma had, to cover any anon pages imported.
540 if (importer && !importer->anon_vma) {
541 importer->anon_vma = anon_vma;
542 __anon_vma_link(importer);
547 flush_dcache_mmap_lock(mapping);
548 vma_prio_tree_remove(vma, root);
550 vma_prio_tree_remove(next, root);
553 vma->vm_start = start;
555 vma->vm_pgoff = pgoff;
557 next->vm_start += adjust_next << PAGE_SHIFT;
558 next->vm_pgoff += adjust_next;
563 vma_prio_tree_insert(next, root);
564 vma_prio_tree_insert(vma, root);
565 flush_dcache_mmap_unlock(mapping);
570 * vma_merge has merged next into vma, and needs
571 * us to remove next before dropping the locks.
573 __vma_unlink(mm, next, vma);
575 __remove_shared_vm_struct(next, file, mapping);
577 __anon_vma_merge(vma, next);
580 * split_vma has split insert from vma, and needs
581 * us to insert it before dropping the locks
582 * (it may either follow vma or precede it).
584 __insert_vm_struct(mm, insert);
588 spin_unlock(&anon_vma->lock);
590 spin_unlock(&mapping->i_mmap_lock);
596 mpol_free(vma_policy(next));
597 kmem_cache_free(vm_area_cachep, next);
599 * In mprotect's case 6 (see comments on vma_merge),
600 * we must remove another next too. It would clutter
601 * up the code too much to do both in one go.
603 if (remove_next == 2) {
613 * If the vma has a ->close operation then the driver probably needs to release
614 * per-vma resources, so we don't attempt to merge those.
616 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
618 static inline int is_mergeable_vma(struct vm_area_struct *vma,
619 struct file *file, unsigned long vm_flags)
621 if (vma->vm_flags != vm_flags)
623 if (vma->vm_file != file)
625 if (vma->vm_ops && vma->vm_ops->close)
630 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
631 struct anon_vma *anon_vma2)
633 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
637 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
638 * in front of (at a lower virtual address and file offset than) the vma.
640 * We cannot merge two vmas if they have differently assigned (non-NULL)
641 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
643 * We don't check here for the merged mmap wrapping around the end of pagecache
644 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
645 * wrap, nor mmaps which cover the final page at index -1UL.
648 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
649 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
651 if (is_mergeable_vma(vma, file, vm_flags) &&
652 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
653 if (vma->vm_pgoff == vm_pgoff)
660 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
661 * beyond (at a higher virtual address and file offset than) the vma.
663 * We cannot merge two vmas if they have differently assigned (non-NULL)
664 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
667 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
668 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
670 if (is_mergeable_vma(vma, file, vm_flags) &&
671 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
673 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
674 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
681 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
682 * whether that can be merged with its predecessor or its successor.
683 * Or both (it neatly fills a hole).
685 * In most cases - when called for mmap, brk or mremap - [addr,end) is
686 * certain not to be mapped by the time vma_merge is called; but when
687 * called for mprotect, it is certain to be already mapped (either at
688 * an offset within prev, or at the start of next), and the flags of
689 * this area are about to be changed to vm_flags - and the no-change
690 * case has already been eliminated.
692 * The following mprotect cases have to be considered, where AAAA is
693 * the area passed down from mprotect_fixup, never extending beyond one
694 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
696 * AAAA AAAA AAAA AAAA
697 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
698 * cannot merge might become might become might become
699 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
700 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
701 * mremap move: PPPPNNNNNNNN 8
703 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
704 * might become case 1 below case 2 below case 3 below
706 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
707 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
709 struct vm_area_struct *vma_merge(struct mm_struct *mm,
710 struct vm_area_struct *prev, unsigned long addr,
711 unsigned long end, unsigned long vm_flags,
712 struct anon_vma *anon_vma, struct file *file,
713 pgoff_t pgoff, struct mempolicy *policy)
715 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
716 struct vm_area_struct *area, *next;
719 * We later require that vma->vm_flags == vm_flags,
720 * so this tests vma->vm_flags & VM_SPECIAL, too.
722 if (vm_flags & VM_SPECIAL)
726 next = prev->vm_next;
730 if (next && next->vm_end == end) /* cases 6, 7, 8 */
731 next = next->vm_next;
734 * Can it merge with the predecessor?
736 if (prev && prev->vm_end == addr &&
737 mpol_equal(vma_policy(prev), policy) &&
738 can_vma_merge_after(prev, vm_flags,
739 anon_vma, file, pgoff)) {
741 * OK, it can. Can we now merge in the successor as well?
743 if (next && end == next->vm_start &&
744 mpol_equal(policy, vma_policy(next)) &&
745 can_vma_merge_before(next, vm_flags,
746 anon_vma, file, pgoff+pglen) &&
747 is_mergeable_anon_vma(prev->anon_vma,
750 vma_adjust(prev, prev->vm_start,
751 next->vm_end, prev->vm_pgoff, NULL);
752 } else /* cases 2, 5, 7 */
753 vma_adjust(prev, prev->vm_start,
754 end, prev->vm_pgoff, NULL);
755 if (prev->vm_flags & VM_EXEC)
756 arch_add_exec_range(mm, prev->vm_end);
761 * Can this new request be merged in front of next?
763 if (next && end == next->vm_start &&
764 mpol_equal(policy, vma_policy(next)) &&
765 can_vma_merge_before(next, vm_flags,
766 anon_vma, file, pgoff+pglen)) {
767 if (prev && addr < prev->vm_end) /* case 4 */
768 vma_adjust(prev, prev->vm_start,
769 addr, prev->vm_pgoff, NULL);
770 else /* cases 3, 8 */
771 vma_adjust(area, addr, next->vm_end,
772 next->vm_pgoff - pglen, NULL);
780 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
781 * neighbouring vmas for a suitable anon_vma, before it goes off
782 * to allocate a new anon_vma. It checks because a repetitive
783 * sequence of mprotects and faults may otherwise lead to distinct
784 * anon_vmas being allocated, preventing vma merge in subsequent
787 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
789 struct vm_area_struct *near;
790 unsigned long vm_flags;
797 * Since only mprotect tries to remerge vmas, match flags
798 * which might be mprotected into each other later on.
799 * Neither mlock nor madvise tries to remerge at present,
800 * so leave their flags as obstructing a merge.
802 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
803 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
805 if (near->anon_vma && vma->vm_end == near->vm_start &&
806 mpol_equal(vma_policy(vma), vma_policy(near)) &&
807 can_vma_merge_before(near, vm_flags,
808 NULL, vma->vm_file, vma->vm_pgoff +
809 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
810 return near->anon_vma;
813 * It is potentially slow to have to call find_vma_prev here.
814 * But it's only on the first write fault on the vma, not
815 * every time, and we could devise a way to avoid it later
816 * (e.g. stash info in next's anon_vma_node when assigning
817 * an anon_vma, or when trying vma_merge). Another time.
819 if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
824 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
825 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
827 if (near->anon_vma && near->vm_end == vma->vm_start &&
828 mpol_equal(vma_policy(near), vma_policy(vma)) &&
829 can_vma_merge_after(near, vm_flags,
830 NULL, vma->vm_file, vma->vm_pgoff))
831 return near->anon_vma;
834 * There's no absolute need to look only at touching neighbours:
835 * we could search further afield for "compatible" anon_vmas.
836 * But it would probably just be a waste of time searching,
837 * or lead to too many vmas hanging off the same anon_vma.
838 * We're trying to allow mprotect remerging later on,
839 * not trying to minimize memory used for anon_vmas.
844 #ifdef CONFIG_PROC_FS
845 void __vm_stat_account(struct mm_struct *mm, unsigned long flags,
846 struct file *file, long pages)
848 const unsigned long stack_flags
849 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
851 #ifdef CONFIG_HUGETLB
852 if (flags & VM_HUGETLB) {
853 if (!(flags & VM_DONTCOPY))
854 mm->shared_vm += pages;
857 #endif /* CONFIG_HUGETLB */
860 mm->shared_vm += pages;
861 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
862 mm->exec_vm += pages;
863 } else if (flags & stack_flags)
864 mm->stack_vm += pages;
865 if (flags & (VM_RESERVED|VM_IO))
866 mm->reserved_vm += pages;
868 #endif /* CONFIG_PROC_FS */
871 * The caller must hold down_write(current->mm->mmap_sem).
874 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
875 unsigned long len, unsigned long prot,
876 unsigned long flags, unsigned long pgoff)
878 struct mm_struct * mm = current->mm;
879 struct vm_area_struct * vma, * prev;
881 unsigned int vm_flags;
882 int correct_wcount = 0;
884 struct rb_node ** rb_link, * rb_parent;
886 unsigned long charged = 0, reqprot = prot;
889 if (is_file_hugepages(file))
892 if (!file->f_op || !file->f_op->mmap)
895 if ((prot & PROT_EXEC) &&
896 (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
900 * Does the application expect PROT_READ to imply PROT_EXEC?
902 * (the exception is when the underlying filesystem is noexec
903 * mounted, in which case we dont add PROT_EXEC.)
905 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
906 if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
912 /* Careful about overflows.. */
913 len = PAGE_ALIGN(len);
914 if (!len || len > TASK_SIZE)
917 /* offset overflow? */
918 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
921 /* Too many mappings? */
922 if (mm->map_count > sysctl_max_map_count)
925 /* Obtain the address to map to. we verify (or select) it and ensure
926 * that it represents a valid section of the address space.
928 addr = get_unmapped_area_prot(file, addr, len, pgoff, flags, prot & PROT_EXEC);
929 if (addr & ~PAGE_MASK)
932 /* Do simple checking here so the lower-level routines won't have
933 * to. we assume access permissions have been handled by the open
934 * of the memory object, so we don't do any here.
936 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
937 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
939 if (flags & MAP_LOCKED) {
942 vm_flags |= VM_LOCKED;
944 /* mlock MCL_FUTURE? */
945 if (vm_flags & VM_LOCKED) {
946 unsigned long locked, lock_limit;
947 locked = len >> PAGE_SHIFT;
948 locked += mm->locked_vm;
949 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
950 lock_limit >>= PAGE_SHIFT;
951 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
955 inode = file ? file->f_dentry->d_inode : NULL;
958 switch (flags & MAP_TYPE) {
960 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
964 * Make sure we don't allow writing to an append-only
967 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
971 * Make sure there are no mandatory locks on the file.
973 if (locks_verify_locked(inode))
976 vm_flags |= VM_SHARED | VM_MAYSHARE;
977 if (!(file->f_mode & FMODE_WRITE))
978 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
982 if (!(file->f_mode & FMODE_READ))
990 switch (flags & MAP_TYPE) {
992 vm_flags |= VM_SHARED | VM_MAYSHARE;
996 * Set pgoff according to addr for anon_vma.
998 pgoff = addr >> PAGE_SHIFT;
1005 error = security_file_mmap(file, reqprot, prot, flags);
1009 /* Clear old maps */
1012 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1013 if (vma && vma->vm_start < addr + len) {
1014 if (do_munmap(mm, addr, len))
1019 /* Check against address space limit. */
1020 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1023 if (accountable && (!(flags & MAP_NORESERVE) ||
1024 sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
1025 if (vm_flags & VM_SHARED) {
1026 /* Check memory availability in shmem_file_setup? */
1027 vm_flags |= VM_ACCOUNT;
1028 } else if (vm_flags & VM_WRITE) {
1030 * Private writable mapping: check memory availability
1032 charged = len >> PAGE_SHIFT;
1033 if (security_vm_enough_memory(charged))
1035 vm_flags |= VM_ACCOUNT;
1040 * Can we just expand an old private anonymous mapping?
1041 * The VM_SHARED test is necessary because shmem_zero_setup
1042 * will create the file object for a shared anonymous map below.
1044 if (!file && !(vm_flags & VM_SHARED) &&
1045 vma_merge(mm, prev, addr, addr + len, vm_flags,
1046 NULL, NULL, pgoff, NULL))
1050 * Determine the object being mapped and call the appropriate
1051 * specific mapper. the address has already been validated, but
1052 * not unmapped, but the maps are removed from the list.
1054 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1059 memset(vma, 0, sizeof(*vma));
1062 vma->vm_start = addr;
1063 vma->vm_end = addr + len;
1064 vma->vm_flags = vm_flags;
1065 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
1066 vma->vm_pgoff = pgoff;
1070 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1072 if (vm_flags & VM_DENYWRITE) {
1073 error = deny_write_access(file);
1078 vma->vm_file = file;
1080 error = file->f_op->mmap(file, vma);
1082 goto unmap_and_free_vma;
1083 } else if (vm_flags & VM_SHARED) {
1084 error = shmem_zero_setup(vma);
1089 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1090 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1091 * that memory reservation must be checked; but that reservation
1092 * belongs to shared memory object, not to vma: so now clear it.
1094 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
1095 vma->vm_flags &= ~VM_ACCOUNT;
1097 /* Can addr have changed??
1099 * Answer: Yes, several device drivers can do it in their
1100 * f_op->mmap method. -DaveM
1102 addr = vma->vm_start;
1103 pgoff = vma->vm_pgoff;
1104 vm_flags = vma->vm_flags;
1106 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
1107 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
1108 file = vma->vm_file;
1109 vma_link(mm, vma, prev, rb_link, rb_parent);
1111 atomic_inc(&inode->i_writecount);
1115 atomic_inc(&inode->i_writecount);
1118 mpol_free(vma_policy(vma));
1119 kmem_cache_free(vm_area_cachep, vma);
1122 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1123 __vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1124 if (vm_flags & VM_LOCKED) {
1125 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1126 make_pages_present(addr, addr + len);
1128 if (flags & MAP_POPULATE) {
1129 up_write(&mm->mmap_sem);
1130 sys_remap_file_pages(addr, len, 0,
1131 pgoff, flags & MAP_NONBLOCK);
1132 down_write(&mm->mmap_sem);
1138 atomic_inc(&inode->i_writecount);
1139 vma->vm_file = NULL;
1142 /* Undo any partial mapping done by a device driver. */
1143 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1146 kmem_cache_free(vm_area_cachep, vma);
1149 vm_unacct_memory(charged);
1153 EXPORT_SYMBOL(do_mmap_pgoff);
1155 /* Get an address range which is currently unmapped.
1156 * For shmat() with addr=0.
1158 * Ugly calling convention alert:
1159 * Return value with the low bits set means error value,
1161 * if (ret & ~PAGE_MASK)
1164 * This function "knows" that -ENOMEM has the bits set.
1166 #ifndef HAVE_ARCH_UNMAPPED_AREA
1168 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1169 unsigned long len, unsigned long pgoff, unsigned long flags)
1171 struct mm_struct *mm = current->mm;
1172 struct vm_area_struct *vma;
1173 unsigned long start_addr;
1175 if (len > TASK_SIZE)
1179 addr = PAGE_ALIGN(addr);
1180 vma = find_vma(mm, addr);
1181 if (TASK_SIZE - len >= addr &&
1182 (!vma || addr + len <= vma->vm_start))
1185 start_addr = addr = mm->free_area_cache;
1188 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1189 /* At this point: (!vma || addr < vma->vm_end). */
1190 if (TASK_SIZE - len < addr) {
1192 * Start a new search - just in case we missed
1195 if (start_addr != TASK_UNMAPPED_BASE) {
1196 start_addr = addr = TASK_UNMAPPED_BASE;
1201 if (!vma || addr + len <= vma->vm_start) {
1203 * Remember the place where we stopped the search:
1205 mm->free_area_cache = addr + len;
1213 void arch_unmap_area(struct vm_area_struct *area)
1216 * Is this a new hole at the lowest possible address?
1218 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1219 area->vm_start < area->vm_mm->free_area_cache)
1220 area->vm_mm->free_area_cache = area->vm_start;
1224 * This mmap-allocator allocates new areas top-down from below the
1225 * stack's low limit (the base):
1227 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1229 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1230 const unsigned long len, const unsigned long pgoff,
1231 const unsigned long flags)
1233 struct vm_area_struct *vma;
1234 struct mm_struct *mm = current->mm;
1235 unsigned long addr = addr0;
1237 /* requested length too big for entire address space */
1238 if (len > TASK_SIZE)
1241 /* requesting a specific address */
1243 addr = PAGE_ALIGN(addr);
1244 vma = find_vma(mm, addr);
1245 if (TASK_SIZE - len >= addr &&
1246 (!vma || addr + len <= vma->vm_start))
1250 /* either no address requested or can't fit in requested address hole */
1251 addr = mm->free_area_cache;
1253 /* make sure it can fit in the remaining address space */
1255 vma = find_vma(mm, addr-len);
1256 if (!vma || addr <= vma->vm_start)
1257 /* remember the address as a hint for next time */
1258 return (mm->free_area_cache = addr-len);
1261 addr = mm->mmap_base-len;
1265 * Lookup failure means no vma is above this address,
1266 * else if new region fits below vma->vm_start,
1267 * return with success:
1269 vma = find_vma(mm, addr);
1270 if (!vma || addr+len <= vma->vm_start)
1271 /* remember the address as a hint for next time */
1272 return (mm->free_area_cache = addr);
1274 /* try just below the current vma->vm_start */
1275 addr = vma->vm_start-len;
1276 } while (len < vma->vm_start);
1279 * A failed mmap() very likely causes application failure,
1280 * so fall back to the bottom-up function here. This scenario
1281 * can happen with large stack limits and large mmap()
1284 mm->free_area_cache = TASK_UNMAPPED_BASE;
1285 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1287 * Restore the topdown base:
1289 mm->free_area_cache = mm->mmap_base;
1295 void arch_unmap_area_topdown(struct vm_area_struct *area)
1298 * Is this a new hole at the highest possible address?
1300 if (area->vm_end > area->vm_mm->free_area_cache)
1301 area->vm_mm->free_area_cache = area->vm_end;
1303 /* dont allow allocations above current base */
1304 if (area->vm_mm->free_area_cache > area->vm_mm->mmap_base)
1305 area->vm_mm->free_area_cache = area->vm_mm->mmap_base;
1310 get_unmapped_area_prot(struct file *file, unsigned long addr, unsigned long len,
1311 unsigned long pgoff, unsigned long flags, int exec)
1315 if (!(flags & MAP_FIXED)) {
1316 unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1318 if (exec && current->mm->get_unmapped_exec_area)
1319 get_area = current->mm->get_unmapped_exec_area;
1321 get_area = current->mm->get_unmapped_area;
1323 if (file && file->f_op && file->f_op->get_unmapped_area)
1324 get_area = file->f_op->get_unmapped_area;
1325 addr = get_area(file, addr, len, pgoff, flags);
1326 if (IS_ERR_VALUE(addr))
1330 if (addr > TASK_SIZE - len)
1332 if (addr & ~PAGE_MASK)
1334 if (file && is_file_hugepages(file)) {
1336 * Check if the given range is hugepage aligned, and
1337 * can be made suitable for hugepages.
1339 ret = prepare_hugepage_range(addr, len);
1342 * Ensure that a normal request is not falling in a
1343 * reserved hugepage range. For some archs like IA-64,
1344 * there is a separate region for hugepages.
1346 ret = is_hugepage_only_range(current->mm, addr, len);
1353 EXPORT_SYMBOL(get_unmapped_area_prot);
1355 #define SHLIB_BASE 0x00111000
1357 unsigned long arch_get_unmapped_exec_area(struct file *filp, unsigned long addr0,
1358 unsigned long len0, unsigned long pgoff, unsigned long flags)
1360 unsigned long addr = addr0, len = len0;
1361 struct mm_struct *mm = current->mm;
1362 struct vm_area_struct *vma;
1365 if (len > TASK_SIZE)
1368 if (!addr && !(flags & MAP_FIXED))
1369 addr = randomize_range(SHLIB_BASE, 0x01000000, len);
1372 addr = PAGE_ALIGN(addr);
1373 vma = find_vma(mm, addr);
1374 if (TASK_SIZE - len >= addr &&
1375 (!vma || addr + len <= vma->vm_start)) {
1381 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1382 /* At this point: (!vma || addr < vma->vm_end). */
1383 if (TASK_SIZE - len < addr)
1386 if (!vma || addr + len <= vma->vm_start) {
1388 * Must not let a PROT_EXEC mapping get into the
1391 if (addr + len > mm->brk)
1395 * Up until the brk area we randomize addresses
1396 * as much as possible:
1398 if (addr >= 0x01000000) {
1399 tmp = randomize_range(0x01000000, PAGE_ALIGN(max(mm->start_brk, 0x08000000)), len);
1400 vma = find_vma(mm, tmp);
1401 if (TASK_SIZE - len >= tmp &&
1402 (!vma || tmp + len <= vma->vm_start))
1406 * Ok, randomization didnt work out - return
1407 * the result of the linear search:
1415 return current->mm->get_unmapped_area(filp, addr0, len0, pgoff, flags);
1419 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1420 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1422 struct vm_area_struct *vma = NULL;
1425 /* Check the cache first. */
1426 /* (Cache hit rate is typically around 35%.) */
1427 vma = mm->mmap_cache;
1428 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1429 struct rb_node * rb_node;
1431 rb_node = mm->mm_rb.rb_node;
1435 struct vm_area_struct * vma_tmp;
1437 vma_tmp = rb_entry(rb_node,
1438 struct vm_area_struct, vm_rb);
1440 if (vma_tmp->vm_end > addr) {
1442 if (vma_tmp->vm_start <= addr)
1444 rb_node = rb_node->rb_left;
1446 rb_node = rb_node->rb_right;
1449 mm->mmap_cache = vma;
1455 EXPORT_SYMBOL(find_vma);
1457 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1458 struct vm_area_struct *
1459 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1460 struct vm_area_struct **pprev)
1462 struct vm_area_struct *vma = NULL, *prev = NULL;
1463 struct rb_node * rb_node;
1467 /* Guard against addr being lower than the first VMA */
1470 /* Go through the RB tree quickly. */
1471 rb_node = mm->mm_rb.rb_node;
1474 struct vm_area_struct *vma_tmp;
1475 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1477 if (addr < vma_tmp->vm_end) {
1478 rb_node = rb_node->rb_left;
1481 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1483 rb_node = rb_node->rb_right;
1489 return prev ? prev->vm_next : vma;
1492 static int over_stack_limit(unsigned long sz)
1494 if (sz < EXEC_STACK_BIAS)
1496 return (sz - EXEC_STACK_BIAS) >
1497 current->signal->rlim[RLIMIT_STACK].rlim_cur;
1501 * Verify that the stack growth is acceptable and
1502 * update accounting. This is shared with both the
1503 * grow-up and grow-down cases.
1505 static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
1507 struct mm_struct *mm = vma->vm_mm;
1508 struct rlimit *rlim = current->signal->rlim;
1510 /* address space limit tests */
1511 if (!may_expand_vm(mm, grow))
1514 /* Stack limit test */
1515 if (over_stack_limit(size))
1518 /* mlock limit tests */
1519 if (vma->vm_flags & VM_LOCKED) {
1520 unsigned long locked;
1521 unsigned long limit;
1522 locked = mm->locked_vm + grow;
1523 limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
1524 if (locked > limit && !capable(CAP_IPC_LOCK))
1529 * Overcommit.. This must be the final test, as it will
1530 * update security statistics.
1532 if (security_vm_enough_memory(grow))
1535 /* Ok, everything looks good - let it rip */
1536 vx_vmpages_add(mm, grow);
1537 if (vma->vm_flags & VM_LOCKED)
1538 vx_vmlocked_add(mm, grow);
1539 __vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
1543 #ifdef CONFIG_STACK_GROWSUP
1545 * vma is the first one with address > vma->vm_end. Have to extend vma.
1547 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1551 if (!(vma->vm_flags & VM_GROWSUP))
1555 * We must make sure the anon_vma is allocated
1556 * so that the anon_vma locking is not a noop.
1558 if (unlikely(anon_vma_prepare(vma)))
1563 * vma->vm_start/vm_end cannot change under us because the caller
1564 * is required to hold the mmap_sem in read mode. We need the
1565 * anon_vma lock to serialize against concurrent expand_stacks.
1567 address += 4 + PAGE_SIZE - 1;
1568 address &= PAGE_MASK;
1571 /* Somebody else might have raced and expanded it already */
1572 if (address > vma->vm_end) {
1573 unsigned long size, grow;
1575 size = address - vma->vm_start;
1576 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1578 error = acct_stack_growth(vma, size, grow);
1580 vma->vm_end = address;
1582 anon_vma_unlock(vma);
1586 struct vm_area_struct *
1587 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1589 struct vm_area_struct *vma, *prev;
1592 vma = find_vma_prev(mm, addr, &prev);
1593 if (vma && (vma->vm_start <= addr))
1595 if (!prev || expand_stack(prev, addr))
1597 if (prev->vm_flags & VM_LOCKED) {
1598 make_pages_present(addr, prev->vm_end);
1604 * vma is the first one with address < vma->vm_start. Have to extend vma.
1606 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1611 * We must make sure the anon_vma is allocated
1612 * so that the anon_vma locking is not a noop.
1614 if (unlikely(anon_vma_prepare(vma)))
1619 * vma->vm_start/vm_end cannot change under us because the caller
1620 * is required to hold the mmap_sem in read mode. We need the
1621 * anon_vma lock to serialize against concurrent expand_stacks.
1623 address &= PAGE_MASK;
1626 /* Somebody else might have raced and expanded it already */
1627 if (address < vma->vm_start) {
1628 unsigned long size, grow;
1630 size = vma->vm_end - address;
1631 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1633 error = acct_stack_growth(vma, size, grow);
1635 vma->vm_start = address;
1636 vma->vm_pgoff -= grow;
1639 anon_vma_unlock(vma);
1643 struct vm_area_struct *
1644 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1646 struct vm_area_struct * vma;
1647 unsigned long start;
1650 vma = find_vma(mm,addr);
1653 if (vma->vm_start <= addr)
1655 if (!(vma->vm_flags & VM_GROWSDOWN))
1657 start = vma->vm_start;
1658 if (expand_stack(vma, addr))
1660 if (vma->vm_flags & VM_LOCKED) {
1661 make_pages_present(addr, start);
1667 /* Normal function to fix up a mapping
1668 * This function is the default for when an area has no specific
1669 * function. This may be used as part of a more specific routine.
1671 * By the time this function is called, the area struct has been
1672 * removed from the process mapping list.
1674 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1676 size_t len = area->vm_end - area->vm_start;
1678 vx_vmpages_sub(area->vm_mm, len >> PAGE_SHIFT);
1680 if (area->vm_flags & VM_LOCKED)
1681 vx_vmlocked_sub(area->vm_mm, len >> PAGE_SHIFT);
1682 vm_stat_unaccount(area);
1683 area->vm_mm->unmap_area(area);
1684 remove_vm_struct(area);
1688 * Update the VMA and inode share lists.
1690 * Ok - we have the memory areas we should free on the 'free' list,
1691 * so release them, and do the vma updates.
1693 static void unmap_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
1696 struct vm_area_struct *next = vma->vm_next;
1704 * Get rid of page table information in the indicated region.
1706 * Called with the page table lock held.
1708 static void unmap_region(struct mm_struct *mm,
1709 struct vm_area_struct *vma, struct vm_area_struct *prev,
1710 unsigned long start, unsigned long end)
1712 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
1713 struct mmu_gather *tlb;
1714 unsigned long nr_accounted = 0;
1717 spin_lock(&mm->page_table_lock);
1718 tlb = tlb_gather_mmu(mm, 0);
1719 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1720 vm_unacct_memory(nr_accounted);
1721 free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1722 next? next->vm_start: 0);
1723 tlb_finish_mmu(tlb, start, end);
1724 spin_unlock(&mm->page_table_lock);
1728 * Create a list of vma's touched by the unmap, removing them from the mm's
1729 * vma list as we go..
1732 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1733 struct vm_area_struct *prev, unsigned long end)
1735 struct vm_area_struct **insertion_point;
1736 struct vm_area_struct *tail_vma = NULL;
1738 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1740 rb_erase(&vma->vm_rb, &mm->mm_rb);
1744 } while (vma && vma->vm_start < end);
1745 *insertion_point = vma;
1746 tail_vma->vm_next = NULL;
1747 mm->mmap_cache = NULL; /* Kill the cache. */
1751 * Split a vma into two pieces at address 'addr', a new vma is allocated
1752 * either for the first part or the the tail.
1754 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1755 unsigned long addr, int new_below)
1757 struct mempolicy *pol;
1758 struct vm_area_struct *new;
1760 if (is_vm_hugetlb_page(vma) && (addr & ~HPAGE_MASK))
1763 if (mm->map_count >= sysctl_max_map_count)
1766 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1770 /* most fields are the same, copy all, and then fixup */
1776 new->vm_start = addr;
1777 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1780 pol = mpol_copy(vma_policy(vma));
1782 kmem_cache_free(vm_area_cachep, new);
1783 return PTR_ERR(pol);
1785 vma_set_policy(new, pol);
1788 get_file(new->vm_file);
1790 if (new->vm_ops && new->vm_ops->open)
1791 new->vm_ops->open(new);
1794 unsigned long old_end = vma->vm_end;
1796 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1797 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1798 if (vma->vm_flags & VM_EXEC)
1799 arch_remove_exec_range(mm, old_end);
1801 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1806 /* Munmap is split into 2 main parts -- this part which finds
1807 * what needs doing, and the areas themselves, which do the
1808 * work. This now handles partial unmappings.
1809 * Jeremy Fitzhardinge <jeremy@goop.org>
1811 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1814 struct vm_area_struct *vma, *prev, *last;
1816 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1819 if ((len = PAGE_ALIGN(len)) == 0)
1822 /* Find the first overlapping VMA */
1823 vma = find_vma_prev(mm, start, &prev);
1826 /* we have start < vma->vm_end */
1828 /* if it doesn't overlap, we have nothing.. */
1830 if (vma->vm_start >= end)
1834 * If we need to split any vma, do it now to save pain later.
1836 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1837 * unmapped vm_area_struct will remain in use: so lower split_vma
1838 * places tmp vma above, and higher split_vma places tmp vma below.
1840 if (start > vma->vm_start) {
1841 int error = split_vma(mm, vma, start, 0);
1847 /* Does it split the last one? */
1848 last = find_vma(mm, end);
1849 if (last && end > last->vm_start) {
1850 int error = split_vma(mm, last, end, 1);
1854 vma = prev? prev->vm_next: mm->mmap;
1857 * Remove the vma's, and unmap the actual pages
1859 detach_vmas_to_be_unmapped(mm, vma, prev, end);
1860 unmap_region(mm, vma, prev, start, end);
1862 /* Fix up all other VM information */
1863 unmap_vma_list(mm, vma);
1868 EXPORT_SYMBOL(do_munmap);
1870 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1873 struct mm_struct *mm = current->mm;
1875 profile_munmap(addr);
1877 down_write(&mm->mmap_sem);
1878 ret = do_munmap(mm, addr, len);
1879 up_write(&mm->mmap_sem);
1883 static inline void verify_mm_writelocked(struct mm_struct *mm)
1885 #ifdef CONFIG_DEBUG_KERNEL
1886 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
1888 up_read(&mm->mmap_sem);
1894 * this is really a simplified "do_mmap". it only handles
1895 * anonymous maps. eventually we may be able to do some
1896 * brk-specific accounting here.
1898 unsigned long do_brk(unsigned long addr, unsigned long len)
1900 struct mm_struct * mm = current->mm;
1901 struct vm_area_struct * vma, * prev;
1902 unsigned long flags;
1903 struct rb_node ** rb_link, * rb_parent;
1904 pgoff_t pgoff = addr >> PAGE_SHIFT;
1906 len = PAGE_ALIGN(len);
1910 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1916 if (mm->def_flags & VM_LOCKED) {
1917 unsigned long locked, lock_limit;
1918 locked = len >> PAGE_SHIFT;
1919 locked += mm->locked_vm;
1920 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1921 lock_limit >>= PAGE_SHIFT;
1922 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1924 if (!vx_vmlocked_avail(mm, len >> PAGE_SHIFT))
1929 * mm->mmap_sem is required to protect against another thread
1930 * changing the mappings in case we sleep.
1932 verify_mm_writelocked(mm);
1935 * Clear old maps. this also does some error checking for us
1938 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1939 if (vma && vma->vm_start < addr + len) {
1940 if (do_munmap(mm, addr, len))
1945 /* Check against address space limits *after* clearing old maps... */
1946 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1949 if (mm->map_count > sysctl_max_map_count)
1952 if (security_vm_enough_memory(len >> PAGE_SHIFT))
1955 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1957 /* Can we just expand an old private anonymous mapping? */
1958 if (vma_merge(mm, prev, addr, addr + len, flags,
1959 NULL, NULL, pgoff, NULL))
1963 * create a vma struct for an anonymous mapping
1965 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1967 vm_unacct_memory(len >> PAGE_SHIFT);
1970 memset(vma, 0, sizeof(*vma));
1973 vma->vm_start = addr;
1974 vma->vm_end = addr + len;
1975 vma->vm_pgoff = pgoff;
1976 vma->vm_flags = flags;
1977 vma->vm_page_prot = protection_map[flags & 0x0f];
1978 vma_link(mm, vma, prev, rb_link, rb_parent);
1980 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1981 if (flags & VM_LOCKED) {
1982 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1983 make_pages_present(addr, addr + len);
1988 EXPORT_SYMBOL(do_brk);
1990 /* Release all mmaps. */
1991 void exit_mmap(struct mm_struct *mm)
1993 struct mmu_gather *tlb;
1994 struct vm_area_struct *vma = mm->mmap;
1995 unsigned long nr_accounted = 0;
2000 spin_lock(&mm->page_table_lock);
2003 tlb = tlb_gather_mmu(mm, 1);
2004 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2005 end = unmap_vmas(&tlb, mm, vma, 0, -1, &nr_accounted, NULL);
2006 vm_unacct_memory(nr_accounted);
2007 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
2008 tlb_finish_mmu(tlb, 0, end);
2010 mm->mmap = mm->mmap_cache = NULL;
2011 mm->mm_rb = RB_ROOT;
2012 set_mm_counter(mm, rss, 0);
2013 vx_vmpages_sub(mm, mm->total_vm);
2014 vx_vmlocked_sub(mm, mm->locked_vm);
2015 arch_flush_exec_range(mm);
2017 spin_unlock(&mm->page_table_lock);
2020 * Walk the list again, actually closing and freeing it
2021 * without holding any MM locks.
2024 struct vm_area_struct *next = vma->vm_next;
2025 remove_vm_struct(vma);
2029 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
2032 /* Insert vm structure into process list sorted by address
2033 * and into the inode's i_mmap tree. If vm_file is non-NULL
2034 * then i_mmap_lock is taken here.
2036 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2038 struct vm_area_struct * __vma, * prev;
2039 struct rb_node ** rb_link, * rb_parent;
2042 * The vm_pgoff of a purely anonymous vma should be irrelevant
2043 * until its first write fault, when page's anon_vma and index
2044 * are set. But now set the vm_pgoff it will almost certainly
2045 * end up with (unless mremap moves it elsewhere before that
2046 * first wfault), so /proc/pid/maps tells a consistent story.
2048 * By setting it to reflect the virtual start address of the
2049 * vma, merges and splits can happen in a seamless way, just
2050 * using the existing file pgoff checks and manipulations.
2051 * Similarly in do_mmap_pgoff and in do_brk.
2053 if (!vma->vm_file) {
2054 BUG_ON(vma->anon_vma);
2055 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2057 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2058 if (__vma && __vma->vm_start < vma->vm_end)
2060 vma_link(mm, vma, prev, rb_link, rb_parent);
2065 * Copy the vma structure to a new location in the same mm,
2066 * prior to moving page table entries, to effect an mremap move.
2068 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2069 unsigned long addr, unsigned long len, pgoff_t pgoff)
2071 struct vm_area_struct *vma = *vmap;
2072 unsigned long vma_start = vma->vm_start;
2073 struct mm_struct *mm = vma->vm_mm;
2074 struct vm_area_struct *new_vma, *prev;
2075 struct rb_node **rb_link, *rb_parent;
2076 struct mempolicy *pol;
2079 * If anonymous vma has not yet been faulted, update new pgoff
2080 * to match new location, to increase its chance of merging.
2082 if (!vma->vm_file && !vma->anon_vma)
2083 pgoff = addr >> PAGE_SHIFT;
2085 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2086 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2087 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2090 * Source vma may have been merged into new_vma
2092 if (vma_start >= new_vma->vm_start &&
2093 vma_start < new_vma->vm_end)
2096 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2099 pol = mpol_copy(vma_policy(vma));
2101 kmem_cache_free(vm_area_cachep, new_vma);
2104 vma_set_policy(new_vma, pol);
2105 new_vma->vm_start = addr;
2106 new_vma->vm_end = addr + len;
2107 new_vma->vm_pgoff = pgoff;
2108 if (new_vma->vm_file)
2109 get_file(new_vma->vm_file);
2110 if (new_vma->vm_ops && new_vma->vm_ops->open)
2111 new_vma->vm_ops->open(new_vma);
2112 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2119 * Return true if the calling process may expand its vm space by the passed
2122 int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2124 unsigned long cur = mm->total_vm; /* pages */
2127 lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2129 if (cur + npages > lim)
2131 if (!vx_vmpages_avail(mm, npages))
2137 * Insert a new vma covering the given region, with the given flags and
2138 * protections. Pre-install the mappings to zero or more leading pages
2139 * in the region. Note, this does put_page on pages[0..npages-1] in all
2140 * cases, even on error return.
2142 int install_special_mapping(struct mm_struct *mm,
2143 unsigned long addr, unsigned long len,
2144 unsigned long vm_flags, pgprot_t pgprot,
2145 struct page **pages, unsigned int npages)
2147 struct vm_area_struct *vma;
2150 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2151 if (unlikely(vma == NULL))
2153 memset(vma, 0, sizeof(*vma));
2156 vma->vm_start = addr;
2157 vma->vm_end = addr + len;
2159 vma->vm_flags = vm_flags;
2160 vma->vm_page_prot = pgprot;
2162 insert_vm_struct(mm, vma);
2163 mm->total_vm += len >> PAGE_SHIFT;
2165 for (err = 0; npages > 0 && !err; --npages, ++pages, addr += PAGE_SIZE)
2166 err = install_page(mm, vma, addr, *pages, vma->vm_page_prot);
2169 for (; npages > 0; --npages, ++pages)