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 /* check context space, maybe only Private writable mapping? */
1024 if (!vx_vmpages_avail(mm, len >> PAGE_SHIFT))
1027 if (accountable && (!(flags & MAP_NORESERVE) ||
1028 sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
1029 if (vm_flags & VM_SHARED) {
1030 /* Check memory availability in shmem_file_setup? */
1031 vm_flags |= VM_ACCOUNT;
1032 } else if (vm_flags & VM_WRITE) {
1034 * Private writable mapping: check memory availability
1036 charged = len >> PAGE_SHIFT;
1037 if (security_vm_enough_memory(charged))
1039 vm_flags |= VM_ACCOUNT;
1044 * Can we just expand an old private anonymous mapping?
1045 * The VM_SHARED test is necessary because shmem_zero_setup
1046 * will create the file object for a shared anonymous map below.
1048 if (!file && !(vm_flags & VM_SHARED) &&
1049 vma_merge(mm, prev, addr, addr + len, vm_flags,
1050 NULL, NULL, pgoff, NULL))
1054 * Determine the object being mapped and call the appropriate
1055 * specific mapper. the address has already been validated, but
1056 * not unmapped, but the maps are removed from the list.
1058 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1063 memset(vma, 0, sizeof(*vma));
1066 vma->vm_start = addr;
1067 vma->vm_end = addr + len;
1068 vma->vm_flags = vm_flags;
1069 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
1070 vma->vm_pgoff = pgoff;
1074 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1076 if (vm_flags & VM_DENYWRITE) {
1077 error = deny_write_access(file);
1082 vma->vm_file = file;
1084 error = file->f_op->mmap(file, vma);
1086 goto unmap_and_free_vma;
1087 } else if (vm_flags & VM_SHARED) {
1088 error = shmem_zero_setup(vma);
1093 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1094 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1095 * that memory reservation must be checked; but that reservation
1096 * belongs to shared memory object, not to vma: so now clear it.
1098 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
1099 vma->vm_flags &= ~VM_ACCOUNT;
1101 /* Can addr have changed??
1103 * Answer: Yes, several device drivers can do it in their
1104 * f_op->mmap method. -DaveM
1106 addr = vma->vm_start;
1107 pgoff = vma->vm_pgoff;
1108 vm_flags = vma->vm_flags;
1110 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
1111 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
1112 file = vma->vm_file;
1113 vma_link(mm, vma, prev, rb_link, rb_parent);
1115 atomic_inc(&inode->i_writecount);
1119 atomic_inc(&inode->i_writecount);
1122 mpol_free(vma_policy(vma));
1123 kmem_cache_free(vm_area_cachep, vma);
1126 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1127 __vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1128 if (vm_flags & VM_LOCKED) {
1129 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1130 make_pages_present(addr, addr + len);
1132 if (flags & MAP_POPULATE) {
1133 up_write(&mm->mmap_sem);
1134 sys_remap_file_pages(addr, len, 0,
1135 pgoff, flags & MAP_NONBLOCK);
1136 down_write(&mm->mmap_sem);
1142 atomic_inc(&inode->i_writecount);
1143 vma->vm_file = NULL;
1146 /* Undo any partial mapping done by a device driver. */
1147 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1150 kmem_cache_free(vm_area_cachep, vma);
1153 vm_unacct_memory(charged);
1157 EXPORT_SYMBOL(do_mmap_pgoff);
1159 /* Get an address range which is currently unmapped.
1160 * For shmat() with addr=0.
1162 * Ugly calling convention alert:
1163 * Return value with the low bits set means error value,
1165 * if (ret & ~PAGE_MASK)
1168 * This function "knows" that -ENOMEM has the bits set.
1170 #ifndef HAVE_ARCH_UNMAPPED_AREA
1172 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1173 unsigned long len, unsigned long pgoff, unsigned long flags)
1175 struct mm_struct *mm = current->mm;
1176 struct vm_area_struct *vma;
1177 unsigned long start_addr;
1179 if (len > TASK_SIZE)
1183 addr = PAGE_ALIGN(addr);
1184 vma = find_vma(mm, addr);
1185 if (TASK_SIZE - len >= addr &&
1186 (!vma || addr + len <= vma->vm_start))
1189 start_addr = addr = mm->free_area_cache;
1192 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1193 /* At this point: (!vma || addr < vma->vm_end). */
1194 if (TASK_SIZE - len < addr) {
1196 * Start a new search - just in case we missed
1199 if (start_addr != TASK_UNMAPPED_BASE) {
1200 start_addr = addr = TASK_UNMAPPED_BASE;
1205 if (!vma || addr + len <= vma->vm_start) {
1207 * Remember the place where we stopped the search:
1209 mm->free_area_cache = addr + len;
1217 void arch_unmap_area(struct vm_area_struct *area)
1220 * Is this a new hole at the lowest possible address?
1222 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1223 area->vm_start < area->vm_mm->free_area_cache)
1224 area->vm_mm->free_area_cache = area->vm_start;
1228 * This mmap-allocator allocates new areas top-down from below the
1229 * stack's low limit (the base):
1231 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1233 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1234 const unsigned long len, const unsigned long pgoff,
1235 const unsigned long flags)
1237 struct vm_area_struct *vma;
1238 struct mm_struct *mm = current->mm;
1239 unsigned long addr = addr0;
1241 /* requested length too big for entire address space */
1242 if (len > TASK_SIZE)
1245 /* requesting a specific address */
1247 addr = PAGE_ALIGN(addr);
1248 vma = find_vma(mm, addr);
1249 if (TASK_SIZE - len >= addr &&
1250 (!vma || addr + len <= vma->vm_start))
1254 /* either no address requested or can't fit in requested address hole */
1255 addr = mm->free_area_cache;
1257 /* make sure it can fit in the remaining address space */
1259 vma = find_vma(mm, addr-len);
1260 if (!vma || addr <= vma->vm_start)
1261 /* remember the address as a hint for next time */
1262 return (mm->free_area_cache = addr-len);
1265 addr = mm->mmap_base-len;
1269 * Lookup failure means no vma is above this address,
1270 * else if new region fits below vma->vm_start,
1271 * return with success:
1273 vma = find_vma(mm, addr);
1274 if (!vma || addr+len <= vma->vm_start)
1275 /* remember the address as a hint for next time */
1276 return (mm->free_area_cache = addr);
1278 /* try just below the current vma->vm_start */
1279 addr = vma->vm_start-len;
1280 } while (len < vma->vm_start);
1283 * A failed mmap() very likely causes application failure,
1284 * so fall back to the bottom-up function here. This scenario
1285 * can happen with large stack limits and large mmap()
1288 mm->free_area_cache = TASK_UNMAPPED_BASE;
1289 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1291 * Restore the topdown base:
1293 mm->free_area_cache = mm->mmap_base;
1299 void arch_unmap_area_topdown(struct vm_area_struct *area)
1302 * Is this a new hole at the highest possible address?
1304 if (area->vm_end > area->vm_mm->free_area_cache)
1305 area->vm_mm->free_area_cache = area->vm_end;
1307 /* dont allow allocations above current base */
1308 if (area->vm_mm->free_area_cache > area->vm_mm->mmap_base)
1309 area->vm_mm->free_area_cache = area->vm_mm->mmap_base;
1314 get_unmapped_area_prot(struct file *file, unsigned long addr, unsigned long len,
1315 unsigned long pgoff, unsigned long flags, int exec)
1319 if (!(flags & MAP_FIXED)) {
1320 unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1322 if (exec && current->mm->get_unmapped_exec_area)
1323 get_area = current->mm->get_unmapped_exec_area;
1325 get_area = current->mm->get_unmapped_area;
1327 if (file && file->f_op && file->f_op->get_unmapped_area)
1328 get_area = file->f_op->get_unmapped_area;
1329 addr = get_area(file, addr, len, pgoff, flags);
1330 if (IS_ERR_VALUE(addr))
1334 if (addr > TASK_SIZE - len)
1336 if (addr & ~PAGE_MASK)
1338 if (file && is_file_hugepages(file)) {
1340 * Check if the given range is hugepage aligned, and
1341 * can be made suitable for hugepages.
1343 ret = prepare_hugepage_range(addr, len);
1346 * Ensure that a normal request is not falling in a
1347 * reserved hugepage range. For some archs like IA-64,
1348 * there is a separate region for hugepages.
1350 ret = is_hugepage_only_range(current->mm, addr, len);
1357 EXPORT_SYMBOL(get_unmapped_area_prot);
1359 #define SHLIB_BASE 0x00111000
1361 unsigned long arch_get_unmapped_exec_area(struct file *filp, unsigned long addr0,
1362 unsigned long len0, unsigned long pgoff, unsigned long flags)
1364 unsigned long addr = addr0, len = len0;
1365 struct mm_struct *mm = current->mm;
1366 struct vm_area_struct *vma;
1369 if (len > TASK_SIZE)
1372 if (!addr && !(flags & MAP_FIXED))
1373 addr = randomize_range(SHLIB_BASE, 0x01000000, len);
1376 addr = PAGE_ALIGN(addr);
1377 vma = find_vma(mm, addr);
1378 if (TASK_SIZE - len >= addr &&
1379 (!vma || addr + len <= vma->vm_start)) {
1385 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1386 /* At this point: (!vma || addr < vma->vm_end). */
1387 if (TASK_SIZE - len < addr)
1390 if (!vma || addr + len <= vma->vm_start) {
1392 * Must not let a PROT_EXEC mapping get into the
1395 if (addr + len > mm->brk)
1399 * Up until the brk area we randomize addresses
1400 * as much as possible:
1402 if (addr >= 0x01000000) {
1403 tmp = randomize_range(0x01000000, PAGE_ALIGN(max(mm->start_brk, 0x08000000)), len);
1404 vma = find_vma(mm, tmp);
1405 if (TASK_SIZE - len >= tmp &&
1406 (!vma || tmp + len <= vma->vm_start))
1410 * Ok, randomization didnt work out - return
1411 * the result of the linear search:
1419 return current->mm->get_unmapped_area(filp, addr0, len0, pgoff, flags);
1423 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1424 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1426 struct vm_area_struct *vma = NULL;
1429 /* Check the cache first. */
1430 /* (Cache hit rate is typically around 35%.) */
1431 vma = mm->mmap_cache;
1432 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1433 struct rb_node * rb_node;
1435 rb_node = mm->mm_rb.rb_node;
1439 struct vm_area_struct * vma_tmp;
1441 vma_tmp = rb_entry(rb_node,
1442 struct vm_area_struct, vm_rb);
1444 if (vma_tmp->vm_end > addr) {
1446 if (vma_tmp->vm_start <= addr)
1448 rb_node = rb_node->rb_left;
1450 rb_node = rb_node->rb_right;
1453 mm->mmap_cache = vma;
1459 EXPORT_SYMBOL(find_vma);
1461 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1462 struct vm_area_struct *
1463 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1464 struct vm_area_struct **pprev)
1466 struct vm_area_struct *vma = NULL, *prev = NULL;
1467 struct rb_node * rb_node;
1471 /* Guard against addr being lower than the first VMA */
1474 /* Go through the RB tree quickly. */
1475 rb_node = mm->mm_rb.rb_node;
1478 struct vm_area_struct *vma_tmp;
1479 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1481 if (addr < vma_tmp->vm_end) {
1482 rb_node = rb_node->rb_left;
1485 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1487 rb_node = rb_node->rb_right;
1493 return prev ? prev->vm_next : vma;
1496 static int over_stack_limit(unsigned long sz)
1498 if (sz < EXEC_STACK_BIAS)
1500 return (sz - EXEC_STACK_BIAS) >
1501 current->signal->rlim[RLIMIT_STACK].rlim_cur;
1505 * Verify that the stack growth is acceptable and
1506 * update accounting. This is shared with both the
1507 * grow-up and grow-down cases.
1509 static int acct_stack_growth(struct vm_area_struct * vma, unsigned long size, unsigned long grow)
1511 struct mm_struct *mm = vma->vm_mm;
1512 struct rlimit *rlim = current->signal->rlim;
1514 /* address space limit tests */
1515 if (!may_expand_vm(mm, grow))
1518 /* Stack limit test */
1519 if (over_stack_limit(size))
1522 /* mlock limit tests */
1523 if (vma->vm_flags & VM_LOCKED) {
1524 unsigned long locked;
1525 unsigned long limit;
1526 locked = mm->locked_vm + grow;
1527 limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
1528 if (locked > limit && !capable(CAP_IPC_LOCK))
1532 if (!vx_vmpages_avail(vma->vm_mm, grow))
1536 * Overcommit.. This must be the final test, as it will
1537 * update security statistics.
1539 if (security_vm_enough_memory(grow))
1542 /* Ok, everything looks good - let it rip */
1543 vx_vmpages_add(mm, grow);
1544 if (vma->vm_flags & VM_LOCKED)
1545 vx_vmlocked_add(mm, grow);
1546 __vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
1550 #ifdef CONFIG_STACK_GROWSUP
1552 * vma is the first one with address > vma->vm_end. Have to extend vma.
1554 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1558 if (!(vma->vm_flags & VM_GROWSUP))
1562 * We must make sure the anon_vma is allocated
1563 * so that the anon_vma locking is not a noop.
1565 if (unlikely(anon_vma_prepare(vma)))
1570 * vma->vm_start/vm_end cannot change under us because the caller
1571 * is required to hold the mmap_sem in read mode. We need the
1572 * anon_vma lock to serialize against concurrent expand_stacks.
1574 address += 4 + PAGE_SIZE - 1;
1575 address &= PAGE_MASK;
1578 /* Somebody else might have raced and expanded it already */
1579 if (address > vma->vm_end) {
1580 unsigned long size, grow;
1582 size = address - vma->vm_start;
1583 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1585 error = acct_stack_growth(vma, size, grow);
1587 vma->vm_end = address;
1589 anon_vma_unlock(vma);
1593 struct vm_area_struct *
1594 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1596 struct vm_area_struct *vma, *prev;
1599 vma = find_vma_prev(mm, addr, &prev);
1600 if (vma && (vma->vm_start <= addr))
1602 if (!prev || expand_stack(prev, addr))
1604 if (prev->vm_flags & VM_LOCKED) {
1605 make_pages_present(addr, prev->vm_end);
1611 * vma is the first one with address < vma->vm_start. Have to extend vma.
1613 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1618 * We must make sure the anon_vma is allocated
1619 * so that the anon_vma locking is not a noop.
1621 if (unlikely(anon_vma_prepare(vma)))
1626 * vma->vm_start/vm_end cannot change under us because the caller
1627 * is required to hold the mmap_sem in read mode. We need the
1628 * anon_vma lock to serialize against concurrent expand_stacks.
1630 address &= PAGE_MASK;
1633 /* Somebody else might have raced and expanded it already */
1634 if (address < vma->vm_start) {
1635 unsigned long size, grow;
1637 size = vma->vm_end - address;
1638 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1640 error = acct_stack_growth(vma, size, grow);
1642 vma->vm_start = address;
1643 vma->vm_pgoff -= grow;
1646 anon_vma_unlock(vma);
1650 struct vm_area_struct *
1651 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1653 struct vm_area_struct * vma;
1654 unsigned long start;
1657 vma = find_vma(mm,addr);
1660 if (vma->vm_start <= addr)
1662 if (!(vma->vm_flags & VM_GROWSDOWN))
1664 start = vma->vm_start;
1665 if (expand_stack(vma, addr))
1667 if (vma->vm_flags & VM_LOCKED) {
1668 make_pages_present(addr, start);
1674 /* Normal function to fix up a mapping
1675 * This function is the default for when an area has no specific
1676 * function. This may be used as part of a more specific routine.
1678 * By the time this function is called, the area struct has been
1679 * removed from the process mapping list.
1681 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1683 size_t len = area->vm_end - area->vm_start;
1685 vx_vmpages_sub(area->vm_mm, len >> PAGE_SHIFT);
1687 if (area->vm_flags & VM_LOCKED)
1688 vx_vmlocked_sub(area->vm_mm, len >> PAGE_SHIFT);
1689 vm_stat_unaccount(area);
1690 area->vm_mm->unmap_area(area);
1691 remove_vm_struct(area);
1695 * Update the VMA and inode share lists.
1697 * Ok - we have the memory areas we should free on the 'free' list,
1698 * so release them, and do the vma updates.
1700 static void unmap_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
1703 struct vm_area_struct *next = vma->vm_next;
1711 * Get rid of page table information in the indicated region.
1713 * Called with the page table lock held.
1715 static void unmap_region(struct mm_struct *mm,
1716 struct vm_area_struct *vma, struct vm_area_struct *prev,
1717 unsigned long start, unsigned long end)
1719 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
1720 struct mmu_gather *tlb;
1721 unsigned long nr_accounted = 0;
1724 spin_lock(&mm->page_table_lock);
1725 tlb = tlb_gather_mmu(mm, 0);
1726 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1727 vm_unacct_memory(nr_accounted);
1728 free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
1729 next? next->vm_start: 0);
1730 tlb_finish_mmu(tlb, start, end);
1731 spin_unlock(&mm->page_table_lock);
1735 * Create a list of vma's touched by the unmap, removing them from the mm's
1736 * vma list as we go..
1739 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1740 struct vm_area_struct *prev, unsigned long end)
1742 struct vm_area_struct **insertion_point;
1743 struct vm_area_struct *tail_vma = NULL;
1745 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1747 rb_erase(&vma->vm_rb, &mm->mm_rb);
1751 } while (vma && vma->vm_start < end);
1752 *insertion_point = vma;
1753 tail_vma->vm_next = NULL;
1754 mm->mmap_cache = NULL; /* Kill the cache. */
1758 * Split a vma into two pieces at address 'addr', a new vma is allocated
1759 * either for the first part or the the tail.
1761 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1762 unsigned long addr, int new_below)
1764 struct mempolicy *pol;
1765 struct vm_area_struct *new;
1767 if (is_vm_hugetlb_page(vma) && (addr & ~HPAGE_MASK))
1770 if (mm->map_count >= sysctl_max_map_count)
1773 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1777 /* most fields are the same, copy all, and then fixup */
1783 new->vm_start = addr;
1784 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1787 pol = mpol_copy(vma_policy(vma));
1789 kmem_cache_free(vm_area_cachep, new);
1790 return PTR_ERR(pol);
1792 vma_set_policy(new, pol);
1795 get_file(new->vm_file);
1797 if (new->vm_ops && new->vm_ops->open)
1798 new->vm_ops->open(new);
1801 unsigned long old_end = vma->vm_end;
1803 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1804 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1805 if (vma->vm_flags & VM_EXEC)
1806 arch_remove_exec_range(mm, old_end);
1808 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1813 /* Munmap is split into 2 main parts -- this part which finds
1814 * what needs doing, and the areas themselves, which do the
1815 * work. This now handles partial unmappings.
1816 * Jeremy Fitzhardinge <jeremy@goop.org>
1818 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1821 struct vm_area_struct *vma, *prev, *last;
1823 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1826 if ((len = PAGE_ALIGN(len)) == 0)
1829 /* Find the first overlapping VMA */
1830 vma = find_vma_prev(mm, start, &prev);
1833 /* we have start < vma->vm_end */
1835 /* if it doesn't overlap, we have nothing.. */
1837 if (vma->vm_start >= end)
1841 * If we need to split any vma, do it now to save pain later.
1843 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1844 * unmapped vm_area_struct will remain in use: so lower split_vma
1845 * places tmp vma above, and higher split_vma places tmp vma below.
1847 if (start > vma->vm_start) {
1848 int error = split_vma(mm, vma, start, 0);
1854 /* Does it split the last one? */
1855 last = find_vma(mm, end);
1856 if (last && end > last->vm_start) {
1857 int error = split_vma(mm, last, end, 1);
1861 vma = prev? prev->vm_next: mm->mmap;
1864 * Remove the vma's, and unmap the actual pages
1866 detach_vmas_to_be_unmapped(mm, vma, prev, end);
1867 unmap_region(mm, vma, prev, start, end);
1869 /* Fix up all other VM information */
1870 unmap_vma_list(mm, vma);
1875 EXPORT_SYMBOL(do_munmap);
1877 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1880 struct mm_struct *mm = current->mm;
1882 profile_munmap(addr);
1884 down_write(&mm->mmap_sem);
1885 ret = do_munmap(mm, addr, len);
1886 up_write(&mm->mmap_sem);
1890 static inline void verify_mm_writelocked(struct mm_struct *mm)
1892 #ifdef CONFIG_DEBUG_KERNEL
1893 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
1895 up_read(&mm->mmap_sem);
1901 * this is really a simplified "do_mmap". it only handles
1902 * anonymous maps. eventually we may be able to do some
1903 * brk-specific accounting here.
1905 unsigned long do_brk(unsigned long addr, unsigned long len)
1907 struct mm_struct * mm = current->mm;
1908 struct vm_area_struct * vma, * prev;
1909 unsigned long flags;
1910 struct rb_node ** rb_link, * rb_parent;
1911 pgoff_t pgoff = addr >> PAGE_SHIFT;
1913 len = PAGE_ALIGN(len);
1917 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1923 if (mm->def_flags & VM_LOCKED) {
1924 unsigned long locked, lock_limit;
1925 locked = len >> PAGE_SHIFT;
1926 locked += mm->locked_vm;
1927 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1928 lock_limit >>= PAGE_SHIFT;
1929 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1931 if (!vx_vmlocked_avail(mm, len >> PAGE_SHIFT))
1936 * mm->mmap_sem is required to protect against another thread
1937 * changing the mappings in case we sleep.
1939 verify_mm_writelocked(mm);
1942 * Clear old maps. this also does some error checking for us
1945 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1946 if (vma && vma->vm_start < addr + len) {
1947 if (do_munmap(mm, addr, len))
1952 /* Check against address space limits *after* clearing old maps... */
1953 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
1956 if (mm->map_count > sysctl_max_map_count)
1959 if (security_vm_enough_memory(len >> PAGE_SHIFT) ||
1960 !vx_vmpages_avail(mm, len >> PAGE_SHIFT))
1963 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1965 /* Can we just expand an old private anonymous mapping? */
1966 if (vma_merge(mm, prev, addr, addr + len, flags,
1967 NULL, NULL, pgoff, NULL))
1971 * create a vma struct for an anonymous mapping
1973 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1975 vm_unacct_memory(len >> PAGE_SHIFT);
1978 memset(vma, 0, sizeof(*vma));
1981 vma->vm_start = addr;
1982 vma->vm_end = addr + len;
1983 vma->vm_pgoff = pgoff;
1984 vma->vm_flags = flags;
1985 vma->vm_page_prot = protection_map[flags & 0x0f];
1986 vma_link(mm, vma, prev, rb_link, rb_parent);
1988 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1989 if (flags & VM_LOCKED) {
1990 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1991 make_pages_present(addr, addr + len);
1996 EXPORT_SYMBOL(do_brk);
1998 /* Release all mmaps. */
1999 void exit_mmap(struct mm_struct *mm)
2001 struct mmu_gather *tlb;
2002 struct vm_area_struct *vma = mm->mmap;
2003 unsigned long nr_accounted = 0;
2008 spin_lock(&mm->page_table_lock);
2011 tlb = tlb_gather_mmu(mm, 1);
2012 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2013 end = unmap_vmas(&tlb, mm, vma, 0, -1, &nr_accounted, NULL);
2014 vm_unacct_memory(nr_accounted);
2015 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
2016 tlb_finish_mmu(tlb, 0, end);
2018 mm->mmap = mm->mmap_cache = NULL;
2019 mm->mm_rb = RB_ROOT;
2020 set_mm_counter(mm, rss, 0);
2021 vx_vmpages_sub(mm, mm->total_vm);
2022 vx_vmlocked_sub(mm, mm->locked_vm);
2023 arch_flush_exec_range(mm);
2025 spin_unlock(&mm->page_table_lock);
2028 * Walk the list again, actually closing and freeing it
2029 * without holding any MM locks.
2032 struct vm_area_struct *next = vma->vm_next;
2033 remove_vm_struct(vma);
2037 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
2040 /* Insert vm structure into process list sorted by address
2041 * and into the inode's i_mmap tree. If vm_file is non-NULL
2042 * then i_mmap_lock is taken here.
2044 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2046 struct vm_area_struct * __vma, * prev;
2047 struct rb_node ** rb_link, * rb_parent;
2050 * The vm_pgoff of a purely anonymous vma should be irrelevant
2051 * until its first write fault, when page's anon_vma and index
2052 * are set. But now set the vm_pgoff it will almost certainly
2053 * end up with (unless mremap moves it elsewhere before that
2054 * first wfault), so /proc/pid/maps tells a consistent story.
2056 * By setting it to reflect the virtual start address of the
2057 * vma, merges and splits can happen in a seamless way, just
2058 * using the existing file pgoff checks and manipulations.
2059 * Similarly in do_mmap_pgoff and in do_brk.
2061 if (!vma->vm_file) {
2062 BUG_ON(vma->anon_vma);
2063 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2065 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2066 if (__vma && __vma->vm_start < vma->vm_end)
2068 vma_link(mm, vma, prev, rb_link, rb_parent);
2073 * Copy the vma structure to a new location in the same mm,
2074 * prior to moving page table entries, to effect an mremap move.
2076 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2077 unsigned long addr, unsigned long len, pgoff_t pgoff)
2079 struct vm_area_struct *vma = *vmap;
2080 unsigned long vma_start = vma->vm_start;
2081 struct mm_struct *mm = vma->vm_mm;
2082 struct vm_area_struct *new_vma, *prev;
2083 struct rb_node **rb_link, *rb_parent;
2084 struct mempolicy *pol;
2087 * If anonymous vma has not yet been faulted, update new pgoff
2088 * to match new location, to increase its chance of merging.
2090 if (!vma->vm_file && !vma->anon_vma)
2091 pgoff = addr >> PAGE_SHIFT;
2093 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2094 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2095 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2098 * Source vma may have been merged into new_vma
2100 if (vma_start >= new_vma->vm_start &&
2101 vma_start < new_vma->vm_end)
2104 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2107 pol = mpol_copy(vma_policy(vma));
2109 kmem_cache_free(vm_area_cachep, new_vma);
2112 vma_set_policy(new_vma, pol);
2113 new_vma->vm_start = addr;
2114 new_vma->vm_end = addr + len;
2115 new_vma->vm_pgoff = pgoff;
2116 if (new_vma->vm_file)
2117 get_file(new_vma->vm_file);
2118 if (new_vma->vm_ops && new_vma->vm_ops->open)
2119 new_vma->vm_ops->open(new_vma);
2120 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2127 * Return true if the calling process may expand its vm space by the passed
2130 int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2132 unsigned long cur = mm->total_vm; /* pages */
2135 lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
2137 if (cur + npages > lim)
2139 if (!vx_vmpages_avail(mm, npages))
2145 * Insert a new vma covering the given region, with the given flags and
2146 * protections. Pre-install the mappings to zero or more leading pages
2147 * in the region. Note, this does put_page on pages[0..npages-1] in all
2148 * cases, even on error return.
2150 int install_special_mapping(struct mm_struct *mm,
2151 unsigned long addr, unsigned long len,
2152 unsigned long vm_flags, pgprot_t pgprot,
2153 struct page **pages, unsigned int npages)
2155 struct vm_area_struct *vma;
2158 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
2159 if (unlikely(vma == NULL))
2161 memset(vma, 0, sizeof(*vma));
2164 vma->vm_start = addr;
2165 vma->vm_end = addr + len;
2167 vma->vm_flags = vm_flags;
2168 vma->vm_page_prot = pgprot;
2170 insert_vm_struct(mm, vma);
2171 mm->total_vm += len >> PAGE_SHIFT;
2173 for (err = 0; npages > 0 && !err; --npages, ++pages, addr += PAGE_SIZE)
2174 err = install_page(mm, vma, addr, *pages, vma->vm_page_prot);
2177 for (; npages > 0; --npages, ++pages)