6 * Address space accounting code <alan@redhat.com>
9 #include <linux/slab.h>
10 #include <linux/shm.h>
11 #include <linux/mman.h>
12 #include <linux/pagemap.h>
13 #include <linux/swap.h>
14 #include <linux/syscalls.h>
15 #include <linux/init.h>
16 #include <linux/file.h>
18 #include <linux/personality.h>
19 #include <linux/security.h>
20 #include <linux/hugetlb.h>
21 #include <linux/profile.h>
22 #include <linux/module.h>
23 #include <linux/mount.h>
24 #include <linux/mempolicy.h>
25 #include <linux/rmap.h>
27 #include <asm/uaccess.h>
28 #include <asm/cacheflush.h>
32 * WARNING: the debugging will use recursive algorithms so never enable this
33 * unless you know what you are doing.
37 /* description of effects of mapping type and prot in current implementation.
38 * this is due to the limited x86 page protection hardware. The expected
39 * behavior is in parens:
42 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
43 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
44 * w: (no) no w: (no) no w: (yes) yes w: (no) no
45 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
47 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
48 * w: (no) no w: (no) no w: (copy) copy w: (no) no
49 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
52 pgprot_t protection_map[16] = {
53 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
54 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
57 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
58 int sysctl_overcommit_ratio = 50; /* default is 50% */
59 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
60 atomic_t vm_committed_space = ATOMIC_INIT(0);
62 EXPORT_SYMBOL(sysctl_overcommit_memory);
63 EXPORT_SYMBOL(sysctl_overcommit_ratio);
64 EXPORT_SYMBOL(sysctl_max_map_count);
65 EXPORT_SYMBOL(vm_committed_space);
68 * Requires inode->i_mapping->i_mmap_lock
70 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
71 struct file *file, struct address_space *mapping)
73 if (vma->vm_flags & VM_DENYWRITE)
74 atomic_inc(&file->f_dentry->d_inode->i_writecount);
75 if (vma->vm_flags & VM_SHARED)
76 mapping->i_mmap_writable--;
78 flush_dcache_mmap_lock(mapping);
79 if (unlikely(vma->vm_flags & VM_NONLINEAR))
80 list_del_init(&vma->shared.vm_set.list);
82 vma_prio_tree_remove(vma, &mapping->i_mmap);
83 flush_dcache_mmap_unlock(mapping);
87 * Remove one vm structure and free it.
89 static void remove_vm_struct(struct vm_area_struct *vma)
91 struct file *file = vma->vm_file;
95 struct address_space *mapping = file->f_mapping;
96 spin_lock(&mapping->i_mmap_lock);
97 __remove_shared_vm_struct(vma, file, mapping);
98 spin_unlock(&mapping->i_mmap_lock);
100 if (vma->vm_ops && vma->vm_ops->close)
101 vma->vm_ops->close(vma);
104 anon_vma_unlink(vma);
105 mpol_free(vma_policy(vma));
106 kmem_cache_free(vm_area_cachep, vma);
110 * sys_brk() for the most part doesn't need the global kernel
111 * lock, except when an application is doing something nasty
112 * like trying to un-brk an area that has already been mapped
113 * to a regular file. in this case, the unmapping will need
114 * to invoke file system routines that need the global lock.
116 asmlinkage unsigned long sys_brk(unsigned long brk)
118 unsigned long rlim, retval;
119 unsigned long newbrk, oldbrk;
120 struct mm_struct *mm = current->mm;
122 down_write(&mm->mmap_sem);
124 if (brk < mm->end_code)
126 newbrk = PAGE_ALIGN(brk);
127 oldbrk = PAGE_ALIGN(mm->brk);
128 if (oldbrk == newbrk)
131 /* Always allow shrinking brk. */
132 if (brk <= mm->brk) {
133 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
138 /* Check against rlimit.. */
139 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
140 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
143 /* Check against existing mmap mappings. */
144 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
147 /* Ok, looks good - let it rip. */
148 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
154 up_write(&mm->mmap_sem);
159 static int browse_rb(struct rb_root *root)
162 struct rb_node *nd, *pn = NULL;
163 unsigned long prev = 0, pend = 0;
165 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
166 struct vm_area_struct *vma;
167 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
168 if (vma->vm_start < prev)
169 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
170 if (vma->vm_start < pend)
171 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
172 if (vma->vm_start > vma->vm_end)
173 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
178 for (nd = pn; nd; nd = rb_prev(nd)) {
182 printk("backwards %d, forwards %d\n", j, i), i = 0;
186 void validate_mm(struct mm_struct *mm)
190 struct vm_area_struct *tmp = mm->mmap;
195 if (i != mm->map_count)
196 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
197 i = browse_rb(&mm->mm_rb);
198 if (i != mm->map_count)
199 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
204 #define validate_mm(mm) do { } while (0)
207 static struct vm_area_struct *
208 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
209 struct vm_area_struct **pprev, struct rb_node ***rb_link,
210 struct rb_node ** rb_parent)
212 struct vm_area_struct * vma;
213 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
215 __rb_link = &mm->mm_rb.rb_node;
216 rb_prev = __rb_parent = NULL;
220 struct vm_area_struct *vma_tmp;
222 __rb_parent = *__rb_link;
223 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
225 if (vma_tmp->vm_end > addr) {
227 if (vma_tmp->vm_start <= addr)
229 __rb_link = &__rb_parent->rb_left;
231 rb_prev = __rb_parent;
232 __rb_link = &__rb_parent->rb_right;
238 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
239 *rb_link = __rb_link;
240 *rb_parent = __rb_parent;
245 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
246 struct vm_area_struct *prev, struct rb_node *rb_parent)
249 vma->vm_next = prev->vm_next;
254 vma->vm_next = rb_entry(rb_parent,
255 struct vm_area_struct, vm_rb);
261 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
262 struct rb_node **rb_link, struct rb_node *rb_parent)
264 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
265 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
268 static inline void __vma_link_file(struct vm_area_struct *vma)
274 struct address_space *mapping = file->f_mapping;
276 if (vma->vm_flags & VM_DENYWRITE)
277 atomic_dec(&file->f_dentry->d_inode->i_writecount);
278 if (vma->vm_flags & VM_SHARED)
279 mapping->i_mmap_writable++;
281 flush_dcache_mmap_lock(mapping);
282 if (unlikely(vma->vm_flags & VM_NONLINEAR))
283 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
285 vma_prio_tree_insert(vma, &mapping->i_mmap);
286 flush_dcache_mmap_unlock(mapping);
291 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
292 struct vm_area_struct *prev, struct rb_node **rb_link,
293 struct rb_node *rb_parent)
295 __vma_link_list(mm, vma, prev, rb_parent);
296 __vma_link_rb(mm, vma, rb_link, rb_parent);
297 __anon_vma_link(vma);
300 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
301 struct vm_area_struct *prev, struct rb_node **rb_link,
302 struct rb_node *rb_parent)
304 struct address_space *mapping = NULL;
307 mapping = vma->vm_file->f_mapping;
310 spin_lock(&mapping->i_mmap_lock);
313 __vma_link(mm, vma, prev, rb_link, rb_parent);
314 __vma_link_file(vma);
316 anon_vma_unlock(vma);
318 spin_unlock(&mapping->i_mmap_lock);
325 * Helper for vma_adjust in the split_vma insert case:
326 * insert vm structure into list and rbtree and anon_vma,
327 * but it has already been inserted into prio_tree earlier.
330 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
332 struct vm_area_struct * __vma, * prev;
333 struct rb_node ** rb_link, * rb_parent;
335 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
336 if (__vma && __vma->vm_start < vma->vm_end)
338 __vma_link(mm, vma, prev, rb_link, rb_parent);
343 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
344 struct vm_area_struct *prev)
346 prev->vm_next = vma->vm_next;
347 rb_erase(&vma->vm_rb, &mm->mm_rb);
348 if (mm->mmap_cache == vma)
349 mm->mmap_cache = prev;
353 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
354 * is already present in an i_mmap tree without adjusting the tree.
355 * The following helper function should be used when such adjustments
356 * are necessary. The "insert" vma (if any) is to be inserted
357 * before we drop the necessary locks.
359 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
360 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
362 struct mm_struct *mm = vma->vm_mm;
363 struct vm_area_struct *next = vma->vm_next;
364 struct vm_area_struct *importer = NULL;
365 struct address_space *mapping = NULL;
366 struct prio_tree_root *root = NULL;
367 struct file *file = vma->vm_file;
368 struct anon_vma *anon_vma = NULL;
369 long adjust_next = 0;
372 if (next && !insert) {
373 if (end >= next->vm_end) {
375 * vma expands, overlapping all the next, and
376 * perhaps the one after too (mprotect case 6).
378 again: remove_next = 1 + (end > next->vm_end);
380 anon_vma = next->anon_vma;
381 } else if (end > next->vm_start) {
383 * vma expands, overlapping part of the next:
384 * mprotect case 5 shifting the boundary up.
386 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
387 anon_vma = next->anon_vma;
389 } else if (end < vma->vm_end) {
391 * vma shrinks, and !insert tells it's not
392 * split_vma inserting another: so it must be
393 * mprotect case 4 shifting the boundary down.
395 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
396 anon_vma = next->anon_vma;
402 mapping = file->f_mapping;
403 if (!(vma->vm_flags & VM_NONLINEAR))
404 root = &mapping->i_mmap;
405 spin_lock(&mapping->i_mmap_lock);
408 * Put into prio_tree now, so instantiated pages
409 * are visible to arm/parisc __flush_dcache_page
410 * throughout; but we cannot insert into address
411 * space until vma start or end is updated.
413 __vma_link_file(insert);
418 * When changing only vma->vm_end, we don't really need
419 * anon_vma lock: but is that case worth optimizing out?
422 anon_vma = vma->anon_vma;
424 spin_lock(&anon_vma->lock);
426 * Easily overlooked: when mprotect shifts the boundary,
427 * make sure the expanding vma has anon_vma set if the
428 * shrinking vma had, to cover any anon pages imported.
430 if (importer && !importer->anon_vma) {
431 importer->anon_vma = anon_vma;
432 __anon_vma_link(importer);
437 flush_dcache_mmap_lock(mapping);
438 vma_prio_tree_remove(vma, root);
440 vma_prio_tree_remove(next, root);
443 vma->vm_start = start;
445 vma->vm_pgoff = pgoff;
447 next->vm_start += adjust_next << PAGE_SHIFT;
448 next->vm_pgoff += adjust_next;
453 vma_prio_tree_insert(next, root);
454 vma_prio_tree_insert(vma, root);
455 flush_dcache_mmap_unlock(mapping);
460 * vma_merge has merged next into vma, and needs
461 * us to remove next before dropping the locks.
463 __vma_unlink(mm, next, vma);
465 __remove_shared_vm_struct(next, file, mapping);
467 __anon_vma_merge(vma, next);
470 * split_vma has split insert from vma, and needs
471 * us to insert it before dropping the locks
472 * (it may either follow vma or precede it).
474 __insert_vm_struct(mm, insert);
478 spin_unlock(&anon_vma->lock);
480 spin_unlock(&mapping->i_mmap_lock);
486 mpol_free(vma_policy(next));
487 kmem_cache_free(vm_area_cachep, next);
489 * In mprotect's case 6 (see comments on vma_merge),
490 * we must remove another next too. It would clutter
491 * up the code too much to do both in one go.
493 if (remove_next == 2) {
503 * If the vma has a ->close operation then the driver probably needs to release
504 * per-vma resources, so we don't attempt to merge those.
506 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
508 static inline int is_mergeable_vma(struct vm_area_struct *vma,
509 struct file *file, unsigned long vm_flags)
511 if (vma->vm_flags != vm_flags)
513 if (vma->vm_file != file)
515 if (vma->vm_ops && vma->vm_ops->close)
520 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
521 struct anon_vma *anon_vma2)
523 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
527 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
528 * in front of (at a lower virtual address and file offset than) the vma.
530 * We cannot merge two vmas if they have differently assigned (non-NULL)
531 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
533 * We don't check here for the merged mmap wrapping around the end of pagecache
534 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
535 * wrap, nor mmaps which cover the final page at index -1UL.
538 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
539 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
541 if (is_mergeable_vma(vma, file, vm_flags) &&
542 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
543 if (vma->vm_pgoff == vm_pgoff)
550 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
551 * beyond (at a higher virtual address and file offset than) the vma.
553 * We cannot merge two vmas if they have differently assigned (non-NULL)
554 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
557 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
558 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
560 if (is_mergeable_vma(vma, file, vm_flags) &&
561 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
563 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
564 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
571 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
572 * whether that can be merged with its predecessor or its successor.
573 * Or both (it neatly fills a hole).
575 * In most cases - when called for mmap, brk or mremap - [addr,end) is
576 * certain not to be mapped by the time vma_merge is called; but when
577 * called for mprotect, it is certain to be already mapped (either at
578 * an offset within prev, or at the start of next), and the flags of
579 * this area are about to be changed to vm_flags - and the no-change
580 * case has already been eliminated.
582 * The following mprotect cases have to be considered, where AAAA is
583 * the area passed down from mprotect_fixup, never extending beyond one
584 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
586 * AAAA AAAA AAAA AAAA
587 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
588 * cannot merge might become might become might become
589 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
590 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
591 * mremap move: PPPPNNNNNNNN 8
593 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
594 * might become case 1 below case 2 below case 3 below
596 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
597 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
599 struct vm_area_struct *vma_merge(struct mm_struct *mm,
600 struct vm_area_struct *prev, unsigned long addr,
601 unsigned long end, unsigned long vm_flags,
602 struct anon_vma *anon_vma, struct file *file,
603 pgoff_t pgoff, struct mempolicy *policy)
605 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
606 struct vm_area_struct *area, *next;
609 * We later require that vma->vm_flags == vm_flags,
610 * so this tests vma->vm_flags & VM_SPECIAL, too.
612 if (vm_flags & VM_SPECIAL)
616 next = prev->vm_next;
620 if (next && next->vm_end == end) /* cases 6, 7, 8 */
621 next = next->vm_next;
624 * Can it merge with the predecessor?
626 if (prev && prev->vm_end == addr &&
627 mpol_equal(vma_policy(prev), policy) &&
628 can_vma_merge_after(prev, vm_flags,
629 anon_vma, file, pgoff)) {
631 * OK, it can. Can we now merge in the successor as well?
633 if (next && end == next->vm_start &&
634 mpol_equal(policy, vma_policy(next)) &&
635 can_vma_merge_before(next, vm_flags,
636 anon_vma, file, pgoff+pglen) &&
637 is_mergeable_anon_vma(prev->anon_vma,
640 vma_adjust(prev, prev->vm_start,
641 next->vm_end, prev->vm_pgoff, NULL);
642 } else /* cases 2, 5, 7 */
643 vma_adjust(prev, prev->vm_start,
644 end, prev->vm_pgoff, NULL);
649 * Can this new request be merged in front of next?
651 if (next && end == next->vm_start &&
652 mpol_equal(policy, vma_policy(next)) &&
653 can_vma_merge_before(next, vm_flags,
654 anon_vma, file, pgoff+pglen)) {
655 if (prev && addr < prev->vm_end) /* case 4 */
656 vma_adjust(prev, prev->vm_start,
657 addr, prev->vm_pgoff, NULL);
658 else /* cases 3, 8 */
659 vma_adjust(area, addr, next->vm_end,
660 next->vm_pgoff - pglen, NULL);
668 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
669 * neighbouring vmas for a suitable anon_vma, before it goes off
670 * to allocate a new anon_vma. It checks because a repetitive
671 * sequence of mprotects and faults may otherwise lead to distinct
672 * anon_vmas being allocated, preventing vma merge in subsequent
675 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
677 struct vm_area_struct *near;
678 unsigned long vm_flags;
685 * Since only mprotect tries to remerge vmas, match flags
686 * which might be mprotected into each other later on.
687 * Neither mlock nor madvise tries to remerge at present,
688 * so leave their flags as obstructing a merge.
690 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
691 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
693 if (near->anon_vma && vma->vm_end == near->vm_start &&
694 mpol_equal(vma_policy(vma), vma_policy(near)) &&
695 can_vma_merge_before(near, vm_flags,
696 NULL, vma->vm_file, vma->vm_pgoff +
697 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
698 return near->anon_vma;
701 * It is potentially slow to have to call find_vma_prev here.
702 * But it's only on the first write fault on the vma, not
703 * every time, and we could devise a way to avoid it later
704 * (e.g. stash info in next's anon_vma_node when assigning
705 * an anon_vma, or when trying vma_merge). Another time.
707 if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
712 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
713 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
715 if (near->anon_vma && near->vm_end == vma->vm_start &&
716 mpol_equal(vma_policy(near), vma_policy(vma)) &&
717 can_vma_merge_after(near, vm_flags,
718 NULL, vma->vm_file, vma->vm_pgoff))
719 return near->anon_vma;
722 * There's no absolute need to look only at touching neighbours:
723 * we could search further afield for "compatible" anon_vmas.
724 * But it would probably just be a waste of time searching,
725 * or lead to too many vmas hanging off the same anon_vma.
726 * We're trying to allow mprotect remerging later on,
727 * not trying to minimize memory used for anon_vmas.
732 #ifdef CONFIG_PROC_FS
733 void __vm_stat_account(struct mm_struct *mm, unsigned long flags,
734 struct file *file, long pages)
736 const unsigned long stack_flags
737 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
739 #ifdef CONFIG_HUGETLB
740 if (flags & VM_HUGETLB) {
741 if (!(flags & VM_DONTCOPY))
742 mm->shared_vm += pages;
745 #endif /* CONFIG_HUGETLB */
748 mm->shared_vm += pages;
749 else if (flags & stack_flags)
750 mm->stack_vm += pages;
752 mm->exec_vm += pages;
753 if (flags & (VM_RESERVED|VM_IO))
754 mm->reserved_vm += pages;
756 #endif /* CONFIG_PROC_FS */
759 * The caller must hold down_write(current->mm->mmap_sem).
762 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
763 unsigned long len, unsigned long prot,
764 unsigned long flags, unsigned long pgoff)
766 struct mm_struct * mm = current->mm;
767 struct vm_area_struct * vma, * prev;
769 unsigned int vm_flags;
770 int correct_wcount = 0;
772 struct rb_node ** rb_link, * rb_parent;
774 unsigned long charged = 0;
777 if (is_file_hugepages(file))
780 if (!file->f_op || !file->f_op->mmap)
783 if ((prot & PROT_EXEC) &&
784 (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
788 * Does the application expect PROT_READ to imply PROT_EXEC?
790 * (the exception is when the underlying filesystem is noexec
791 * mounted, in which case we dont add PROT_EXEC.)
793 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
794 if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
800 /* Careful about overflows.. */
801 len = PAGE_ALIGN(len);
802 if (!len || len > TASK_SIZE)
805 /* offset overflow? */
806 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
809 /* Too many mappings? */
810 if (mm->map_count > sysctl_max_map_count)
813 /* Obtain the address to map to. we verify (or select) it and ensure
814 * that it represents a valid section of the address space.
816 addr = get_unmapped_area(file, addr, len, pgoff, flags);
817 if (addr & ~PAGE_MASK)
820 /* Do simple checking here so the lower-level routines won't have
821 * to. we assume access permissions have been handled by the open
822 * of the memory object, so we don't do any here.
824 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
825 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
827 if (flags & MAP_LOCKED) {
830 vm_flags |= VM_LOCKED;
832 /* mlock MCL_FUTURE? */
833 if (vm_flags & VM_LOCKED) {
834 unsigned long locked, lock_limit;
835 locked = mm->locked_vm << PAGE_SHIFT;
836 lock_limit = current->rlim[RLIMIT_MEMLOCK].rlim_cur;
838 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
842 inode = file ? file->f_dentry->d_inode : NULL;
845 switch (flags & MAP_TYPE) {
847 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
851 * Make sure we don't allow writing to an append-only
854 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
858 * Make sure there are no mandatory locks on the file.
860 if (locks_verify_locked(inode))
863 vm_flags |= VM_SHARED | VM_MAYSHARE;
864 if (!(file->f_mode & FMODE_WRITE))
865 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
869 if (!(file->f_mode & FMODE_READ))
877 switch (flags & MAP_TYPE) {
879 vm_flags |= VM_SHARED | VM_MAYSHARE;
883 * Set pgoff according to addr for anon_vma.
885 pgoff = addr >> PAGE_SHIFT;
892 error = security_file_mmap(file, prot, flags);
899 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
900 if (vma && vma->vm_start < addr + len) {
901 if (do_munmap(mm, addr, len))
906 /* Check against address space limit. */
907 if ((mm->total_vm << PAGE_SHIFT) + len
908 > current->rlim[RLIMIT_AS].rlim_cur)
911 /* check context space, maybe only Private writable mapping? */
912 if (!vx_vmpages_avail(mm, len >> PAGE_SHIFT))
915 if (accountable && (!(flags & MAP_NORESERVE) ||
916 sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
917 if (vm_flags & VM_SHARED) {
918 /* Check memory availability in shmem_file_setup? */
919 vm_flags |= VM_ACCOUNT;
920 } else if (vm_flags & VM_WRITE) {
922 * Private writable mapping: check memory availability
924 charged = len >> PAGE_SHIFT;
925 if (security_vm_enough_memory(charged))
927 vm_flags |= VM_ACCOUNT;
932 * Can we just expand an old private anonymous mapping?
933 * The VM_SHARED test is necessary because shmem_zero_setup
934 * will create the file object for a shared anonymous map below.
936 if (!file && !(vm_flags & VM_SHARED) &&
937 vma_merge(mm, prev, addr, addr + len, vm_flags,
938 NULL, NULL, pgoff, NULL))
942 * Determine the object being mapped and call the appropriate
943 * specific mapper. the address has already been validated, but
944 * not unmapped, but the maps are removed from the list.
946 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
951 memset(vma, 0, sizeof(*vma));
954 vma->vm_start = addr;
955 vma->vm_end = addr + len;
956 vma->vm_flags = vm_flags;
957 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
958 vma->vm_pgoff = pgoff;
962 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
964 if (vm_flags & VM_DENYWRITE) {
965 error = deny_write_access(file);
972 error = file->f_op->mmap(file, vma);
974 goto unmap_and_free_vma;
975 } else if (vm_flags & VM_SHARED) {
976 error = shmem_zero_setup(vma);
981 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
982 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
983 * that memory reservation must be checked; but that reservation
984 * belongs to shared memory object, not to vma: so now clear it.
986 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
987 vma->vm_flags &= ~VM_ACCOUNT;
989 /* Can addr have changed??
991 * Answer: Yes, several device drivers can do it in their
992 * f_op->mmap method. -DaveM
994 addr = vma->vm_start;
996 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
997 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
998 vma_link(mm, vma, prev, rb_link, rb_parent);
1000 atomic_inc(&inode->i_writecount);
1004 atomic_inc(&inode->i_writecount);
1007 mpol_free(vma_policy(vma));
1008 kmem_cache_free(vm_area_cachep, vma);
1011 // mm->total_vm += len >> PAGE_SHIFT;
1012 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1013 if (vm_flags & VM_LOCKED) {
1014 // mm->locked_vm += len >> PAGE_SHIFT;
1015 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1016 make_pages_present(addr, addr + len);
1018 if (flags & MAP_POPULATE) {
1019 up_write(&mm->mmap_sem);
1020 sys_remap_file_pages(addr, len, 0,
1021 pgoff, flags & MAP_NONBLOCK);
1022 down_write(&mm->mmap_sem);
1024 __vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
1029 atomic_inc(&inode->i_writecount);
1030 vma->vm_file = NULL;
1033 /* Undo any partial mapping done by a device driver. */
1034 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1036 kmem_cache_free(vm_area_cachep, vma);
1039 vm_unacct_memory(charged);
1043 EXPORT_SYMBOL(do_mmap_pgoff);
1045 /* Get an address range which is currently unmapped.
1046 * For shmat() with addr=0.
1048 * Ugly calling convention alert:
1049 * Return value with the low bits set means error value,
1051 * if (ret & ~PAGE_MASK)
1054 * This function "knows" that -ENOMEM has the bits set.
1056 #ifndef HAVE_ARCH_UNMAPPED_AREA
1058 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1059 unsigned long len, unsigned long pgoff, unsigned long flags)
1061 struct mm_struct *mm = current->mm;
1062 struct vm_area_struct *vma;
1063 unsigned long start_addr;
1065 if (len > TASK_SIZE)
1069 addr = PAGE_ALIGN(addr);
1070 vma = find_vma(mm, addr);
1071 if (TASK_SIZE - len >= addr &&
1072 (!vma || addr + len <= vma->vm_start))
1075 start_addr = addr = mm->free_area_cache;
1078 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1079 /* At this point: (!vma || addr < vma->vm_end). */
1080 if (TASK_SIZE - len < addr) {
1082 * Start a new search - just in case we missed
1085 if (start_addr != TASK_UNMAPPED_BASE) {
1086 start_addr = addr = TASK_UNMAPPED_BASE;
1091 if (!vma || addr + len <= vma->vm_start) {
1093 * Remember the place where we stopped the search:
1095 mm->free_area_cache = addr + len;
1103 void arch_unmap_area(struct vm_area_struct *area)
1106 * Is this a new hole at the lowest possible address?
1108 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1109 area->vm_start < area->vm_mm->free_area_cache)
1110 area->vm_mm->free_area_cache = area->vm_start;
1114 * This mmap-allocator allocates new areas top-down from below the
1115 * stack's low limit (the base):
1117 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1119 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1120 const unsigned long len, const unsigned long pgoff,
1121 const unsigned long flags)
1123 struct vm_area_struct *vma, *prev_vma;
1124 struct mm_struct *mm = current->mm;
1125 unsigned long base = mm->mmap_base, addr = addr0;
1128 /* requested length too big for entire address space */
1129 if (len > TASK_SIZE)
1132 /* dont allow allocations above current base */
1133 if (mm->free_area_cache > base)
1134 mm->free_area_cache = base;
1136 /* requesting a specific address */
1138 addr = PAGE_ALIGN(addr);
1139 vma = find_vma(mm, addr);
1140 if (TASK_SIZE - len >= addr &&
1141 (!vma || addr + len <= vma->vm_start))
1146 /* make sure it can fit in the remaining address space */
1147 if (mm->free_area_cache < len)
1150 /* either no address requested or cant fit in requested address hole */
1151 addr = (mm->free_area_cache - len) & PAGE_MASK;
1154 * Lookup failure means no vma is above this address,
1155 * i.e. return with success:
1157 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
1161 * new region fits between prev_vma->vm_end and
1162 * vma->vm_start, use it:
1164 if (addr+len <= vma->vm_start &&
1165 (!prev_vma || (addr >= prev_vma->vm_end)))
1166 /* remember the address as a hint for next time */
1167 return (mm->free_area_cache = addr);
1169 /* pull free_area_cache down to the first hole */
1170 if (mm->free_area_cache == vma->vm_end)
1171 mm->free_area_cache = vma->vm_start;
1173 /* try just below the current vma->vm_start */
1174 addr = vma->vm_start-len;
1175 } while (len <= vma->vm_start);
1179 * if hint left us with no space for the requested
1180 * mapping then try again:
1183 mm->free_area_cache = base;
1188 * A failed mmap() very likely causes application failure,
1189 * so fall back to the bottom-up function here. This scenario
1190 * can happen with large stack limits and large mmap()
1193 mm->free_area_cache = TASK_UNMAPPED_BASE;
1194 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1196 * Restore the topdown base:
1198 mm->free_area_cache = base;
1204 void arch_unmap_area_topdown(struct vm_area_struct *area)
1207 * Is this a new hole at the highest possible address?
1209 if (area->vm_end > area->vm_mm->free_area_cache)
1210 area->vm_mm->free_area_cache = area->vm_end;
1214 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1215 unsigned long pgoff, unsigned long flags)
1217 if (flags & MAP_FIXED) {
1220 if (addr > TASK_SIZE - len)
1222 if (addr & ~PAGE_MASK)
1224 if (file && is_file_hugepages(file)) {
1226 * Check if the given range is hugepage aligned, and
1227 * can be made suitable for hugepages.
1229 ret = prepare_hugepage_range(addr, len);
1232 * Ensure that a normal request is not falling in a
1233 * reserved hugepage range. For some archs like IA-64,
1234 * there is a separate region for hugepages.
1236 ret = is_hugepage_only_range(addr, len);
1243 if (file && file->f_op && file->f_op->get_unmapped_area)
1244 return file->f_op->get_unmapped_area(file, addr, len,
1247 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
1250 EXPORT_SYMBOL(get_unmapped_area);
1252 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1253 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1255 struct vm_area_struct *vma = NULL;
1258 /* Check the cache first. */
1259 /* (Cache hit rate is typically around 35%.) */
1260 vma = mm->mmap_cache;
1261 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1262 struct rb_node * rb_node;
1264 rb_node = mm->mm_rb.rb_node;
1268 struct vm_area_struct * vma_tmp;
1270 vma_tmp = rb_entry(rb_node,
1271 struct vm_area_struct, vm_rb);
1273 if (vma_tmp->vm_end > addr) {
1275 if (vma_tmp->vm_start <= addr)
1277 rb_node = rb_node->rb_left;
1279 rb_node = rb_node->rb_right;
1282 mm->mmap_cache = vma;
1288 EXPORT_SYMBOL(find_vma);
1290 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1291 struct vm_area_struct *
1292 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1293 struct vm_area_struct **pprev)
1295 struct vm_area_struct *vma = NULL, *prev = NULL;
1296 struct rb_node * rb_node;
1300 /* Guard against addr being lower than the first VMA */
1303 /* Go through the RB tree quickly. */
1304 rb_node = mm->mm_rb.rb_node;
1307 struct vm_area_struct *vma_tmp;
1308 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1310 if (addr < vma_tmp->vm_end) {
1311 rb_node = rb_node->rb_left;
1314 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1316 rb_node = rb_node->rb_right;
1322 return prev ? prev->vm_next : vma;
1325 #ifdef CONFIG_STACK_GROWSUP
1327 * vma is the first one with address > vma->vm_end. Have to extend vma.
1329 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1333 if (!(vma->vm_flags & VM_GROWSUP))
1337 * We must make sure the anon_vma is allocated
1338 * so that the anon_vma locking is not a noop.
1340 if (unlikely(anon_vma_prepare(vma)))
1345 * vma->vm_start/vm_end cannot change under us because the caller
1346 * is required to hold the mmap_sem in read mode. We need the
1347 * anon_vma lock to serialize against concurrent expand_stacks.
1349 address += 4 + PAGE_SIZE - 1;
1350 address &= PAGE_MASK;
1351 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1353 /* Overcommit.. vx check first to avoid vm_unacct_memory() */
1354 if (!vx_vmpages_avail(vma->vm_mm, grow) ||
1355 security_vm_enough_memory(grow)) {
1356 anon_vma_unlock(vma);
1360 if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
1361 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1362 current->rlim[RLIMIT_AS].rlim_cur) {
1363 anon_vma_unlock(vma);
1364 vm_unacct_memory(grow);
1367 vma->vm_end = address;
1368 // vma->vm_mm->total_vm += grow;
1369 vx_vmpages_add(vma->vm_mm, grow);
1370 if (vma->vm_flags & VM_LOCKED)
1371 // vma->vm_mm->locked_vm += grow;
1372 vx_vmlocked_add(vma->vm_mm, grow);
1373 __vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file, grow);
1374 anon_vma_unlock(vma);
1378 struct vm_area_struct *
1379 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1381 struct vm_area_struct *vma, *prev;
1384 vma = find_vma_prev(mm, addr, &prev);
1385 if (vma && (vma->vm_start <= addr))
1387 if (!prev || expand_stack(prev, addr))
1389 if (prev->vm_flags & VM_LOCKED) {
1390 make_pages_present(addr, prev->vm_end);
1396 * vma is the first one with address < vma->vm_start. Have to extend vma.
1398 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1403 * We must make sure the anon_vma is allocated
1404 * so that the anon_vma locking is not a noop.
1406 if (unlikely(anon_vma_prepare(vma)))
1411 * vma->vm_start/vm_end cannot change under us because the caller
1412 * is required to hold the mmap_sem in read mode. We need the
1413 * anon_vma lock to serialize against concurrent expand_stacks.
1415 address &= PAGE_MASK;
1416 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1418 /* Overcommit.. vx check first to avoid vm_unacct_memory() */
1419 if (!vx_vmpages_avail(vma->vm_mm, grow) ||
1420 security_vm_enough_memory(grow)) {
1421 anon_vma_unlock(vma);
1425 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
1426 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1427 current->rlim[RLIMIT_AS].rlim_cur) {
1428 anon_vma_unlock(vma);
1429 vm_unacct_memory(grow);
1432 vma->vm_start = address;
1433 vma->vm_pgoff -= grow;
1434 // vma->vm_mm->total_vm += grow;
1435 vx_vmpages_add(vma->vm_mm, grow);
1436 if (vma->vm_flags & VM_LOCKED)
1437 // vma->vm_mm->locked_vm += grow;
1438 vx_vmlocked_add(vma->vm_mm, grow);
1439 __vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file, grow);
1440 anon_vma_unlock(vma);
1444 struct vm_area_struct *
1445 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1447 struct vm_area_struct * vma;
1448 unsigned long start;
1451 vma = find_vma(mm,addr);
1454 if (vma->vm_start <= addr)
1456 if (!(vma->vm_flags & VM_GROWSDOWN))
1458 start = vma->vm_start;
1459 if (expand_stack(vma, addr))
1461 if (vma->vm_flags & VM_LOCKED) {
1462 make_pages_present(addr, start);
1469 * Try to free as many page directory entries as we can,
1470 * without having to work very hard at actually scanning
1471 * the page tables themselves.
1473 * Right now we try to free page tables if we have a nice
1474 * PGDIR-aligned area that got free'd up. We could be more
1475 * granular if we want to, but this is fast and simple,
1476 * and covers the bad cases.
1478 * "prev", if it exists, points to a vma before the one
1479 * we just free'd - but there's no telling how much before.
1481 static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
1482 unsigned long start, unsigned long end)
1484 unsigned long first = start & PGDIR_MASK;
1485 unsigned long last = end + PGDIR_SIZE - 1;
1486 unsigned long start_index, end_index;
1487 struct mm_struct *mm = tlb->mm;
1493 if (prev->vm_end > start) {
1494 if (last > prev->vm_start)
1495 last = prev->vm_start;
1500 struct vm_area_struct *next = prev->vm_next;
1503 if (next->vm_start < start) {
1507 if (last > next->vm_start)
1508 last = next->vm_start;
1510 if (prev->vm_end > first)
1511 first = prev->vm_end + PGDIR_SIZE - 1;
1515 if (last < first) /* for arches with discontiguous pgd indices */
1518 * If the PGD bits are not consecutive in the virtual address, the
1519 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1521 start_index = pgd_index(first);
1522 if (start_index < FIRST_USER_PGD_NR)
1523 start_index = FIRST_USER_PGD_NR;
1524 end_index = pgd_index(last);
1525 if (end_index > start_index) {
1526 clear_page_tables(tlb, start_index, end_index - start_index);
1527 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
1531 /* Normal function to fix up a mapping
1532 * This function is the default for when an area has no specific
1533 * function. This may be used as part of a more specific routine.
1535 * By the time this function is called, the area struct has been
1536 * removed from the process mapping list.
1538 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1540 size_t len = area->vm_end - area->vm_start;
1542 // area->vm_mm->total_vm -= len >> PAGE_SHIFT;
1543 vx_vmpages_sub(area->vm_mm, len >> PAGE_SHIFT);
1545 if (area->vm_flags & VM_LOCKED)
1546 // area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1547 vx_vmlocked_sub(area->vm_mm, len >> PAGE_SHIFT);
1548 vm_stat_unaccount(area);
1549 area->vm_mm->unmap_area(area);
1550 remove_vm_struct(area);
1554 * Update the VMA and inode share lists.
1556 * Ok - we have the memory areas we should free on the 'free' list,
1557 * so release them, and do the vma updates.
1559 static void unmap_vma_list(struct mm_struct *mm,
1560 struct vm_area_struct *mpnt)
1563 struct vm_area_struct *next = mpnt->vm_next;
1564 unmap_vma(mm, mpnt);
1566 } while (mpnt != NULL);
1571 * Get rid of page table information in the indicated region.
1573 * Called with the page table lock held.
1575 static void unmap_region(struct mm_struct *mm,
1576 struct vm_area_struct *vma,
1577 struct vm_area_struct *prev,
1578 unsigned long start,
1581 struct mmu_gather *tlb;
1582 unsigned long nr_accounted = 0;
1585 tlb = tlb_gather_mmu(mm, 0);
1586 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1587 vm_unacct_memory(nr_accounted);
1589 if (is_hugepage_only_range(start, end - start))
1590 hugetlb_free_pgtables(tlb, prev, start, end);
1592 free_pgtables(tlb, prev, start, end);
1593 tlb_finish_mmu(tlb, start, end);
1597 * Create a list of vma's touched by the unmap, removing them from the mm's
1598 * vma list as we go..
1601 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1602 struct vm_area_struct *prev, unsigned long end)
1604 struct vm_area_struct **insertion_point;
1605 struct vm_area_struct *tail_vma = NULL;
1607 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1609 rb_erase(&vma->vm_rb, &mm->mm_rb);
1613 } while (vma && vma->vm_start < end);
1614 *insertion_point = vma;
1615 tail_vma->vm_next = NULL;
1616 mm->mmap_cache = NULL; /* Kill the cache. */
1620 * Split a vma into two pieces at address 'addr', a new vma is allocated
1621 * either for the first part or the the tail.
1623 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1624 unsigned long addr, int new_below)
1626 struct mempolicy *pol;
1627 struct vm_area_struct *new;
1629 if (mm->map_count >= sysctl_max_map_count)
1632 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1636 /* most fields are the same, copy all, and then fixup */
1642 new->vm_start = addr;
1643 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1646 pol = mpol_copy(vma_policy(vma));
1648 kmem_cache_free(vm_area_cachep, new);
1649 return PTR_ERR(pol);
1651 vma_set_policy(new, pol);
1654 get_file(new->vm_file);
1656 if (new->vm_ops && new->vm_ops->open)
1657 new->vm_ops->open(new);
1660 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1661 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1663 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1668 /* Munmap is split into 2 main parts -- this part which finds
1669 * what needs doing, and the areas themselves, which do the
1670 * work. This now handles partial unmappings.
1671 * Jeremy Fitzhardinge <jeremy@goop.org>
1673 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1676 struct vm_area_struct *mpnt, *prev, *last;
1678 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1681 if ((len = PAGE_ALIGN(len)) == 0)
1684 /* Find the first overlapping VMA */
1685 mpnt = find_vma_prev(mm, start, &prev);
1688 /* we have start < mpnt->vm_end */
1690 if (is_vm_hugetlb_page(mpnt)) {
1691 int ret = is_aligned_hugepage_range(start, len);
1697 /* if it doesn't overlap, we have nothing.. */
1699 if (mpnt->vm_start >= end)
1703 * If we need to split any vma, do it now to save pain later.
1705 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1706 * unmapped vm_area_struct will remain in use: so lower split_vma
1707 * places tmp vma above, and higher split_vma places tmp vma below.
1709 if (start > mpnt->vm_start) {
1710 if (split_vma(mm, mpnt, start, 0))
1715 /* Does it split the last one? */
1716 last = find_vma(mm, end);
1717 if (last && end > last->vm_start) {
1718 if (split_vma(mm, last, end, 1))
1721 mpnt = prev? prev->vm_next: mm->mmap;
1724 * Remove the vma's, and unmap the actual pages
1726 detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1727 spin_lock(&mm->page_table_lock);
1728 unmap_region(mm, mpnt, prev, start, end);
1729 spin_unlock(&mm->page_table_lock);
1731 /* Fix up all other VM information */
1732 unmap_vma_list(mm, mpnt);
1737 EXPORT_SYMBOL(do_munmap);
1739 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1742 struct mm_struct *mm = current->mm;
1744 profile_munmap(addr);
1746 down_write(&mm->mmap_sem);
1747 ret = do_munmap(mm, addr, len);
1748 up_write(&mm->mmap_sem);
1753 * this is really a simplified "do_mmap". it only handles
1754 * anonymous maps. eventually we may be able to do some
1755 * brk-specific accounting here.
1757 unsigned long do_brk(unsigned long addr, unsigned long len)
1759 struct mm_struct * mm = current->mm;
1760 struct vm_area_struct * vma, * prev;
1761 unsigned long flags;
1762 struct rb_node ** rb_link, * rb_parent;
1763 pgoff_t pgoff = addr >> PAGE_SHIFT;
1765 len = PAGE_ALIGN(len);
1769 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1775 if (mm->def_flags & VM_LOCKED) {
1776 unsigned long locked, lock_limit;
1777 locked = mm->locked_vm << PAGE_SHIFT;
1778 lock_limit = current->rlim[RLIMIT_MEMLOCK].rlim_cur;
1780 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1782 if (!vx_vmlocked_avail(mm, len >> PAGE_SHIFT))
1787 * Clear old maps. this also does some error checking for us
1790 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1791 if (vma && vma->vm_start < addr + len) {
1792 if (do_munmap(mm, addr, len))
1797 /* Check against address space limits *after* clearing old maps... */
1798 if ((mm->total_vm << PAGE_SHIFT) + len
1799 > current->rlim[RLIMIT_AS].rlim_cur)
1802 if (mm->map_count > sysctl_max_map_count)
1805 if (security_vm_enough_memory(len >> PAGE_SHIFT) ||
1806 !vx_vmpages_avail(mm, len >> PAGE_SHIFT))
1809 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1811 /* Can we just expand an old private anonymous mapping? */
1812 if (vma_merge(mm, prev, addr, addr + len, flags,
1813 NULL, NULL, pgoff, NULL))
1817 * create a vma struct for an anonymous mapping
1819 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1821 vm_unacct_memory(len >> PAGE_SHIFT);
1824 memset(vma, 0, sizeof(*vma));
1827 vma->vm_start = addr;
1828 vma->vm_end = addr + len;
1829 vma->vm_pgoff = pgoff;
1830 vma->vm_flags = flags;
1831 vma->vm_page_prot = protection_map[flags & 0x0f];
1832 vma_link(mm, vma, prev, rb_link, rb_parent);
1834 // mm->total_vm += len >> PAGE_SHIFT;
1835 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1836 if (flags & VM_LOCKED) {
1837 // mm->locked_vm += len >> PAGE_SHIFT;
1838 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1839 make_pages_present(addr, addr + len);
1844 EXPORT_SYMBOL(do_brk);
1846 /* Release all mmaps. */
1847 void exit_mmap(struct mm_struct *mm)
1849 struct mmu_gather *tlb;
1850 struct vm_area_struct *vma;
1851 unsigned long nr_accounted = 0;
1855 spin_lock(&mm->page_table_lock);
1857 tlb = tlb_gather_mmu(mm, 1);
1859 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1860 mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1861 ~0UL, &nr_accounted, NULL);
1862 vm_unacct_memory(nr_accounted);
1863 BUG_ON(mm->map_count); /* This is just debugging */
1864 clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1865 tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1868 mm->mmap = mm->mmap_cache = NULL;
1869 mm->mm_rb = RB_ROOT;
1871 vx_rsspages_sub(mm, mm->rss);
1872 // mm->total_vm = 0;
1873 vx_vmpages_sub(mm, mm->total_vm);
1874 // mm->locked_vm = 0;
1875 vx_vmlocked_sub(mm, mm->locked_vm);
1877 spin_unlock(&mm->page_table_lock);
1880 * Walk the list again, actually closing and freeing it
1881 * without holding any MM locks.
1884 struct vm_area_struct *next = vma->vm_next;
1885 remove_vm_struct(vma);
1890 /* Insert vm structure into process list sorted by address
1891 * and into the inode's i_mmap tree. If vm_file is non-NULL
1892 * then i_mmap_lock is taken here.
1894 void insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1896 struct vm_area_struct * __vma, * prev;
1897 struct rb_node ** rb_link, * rb_parent;
1900 * The vm_pgoff of a purely anonymous vma should be irrelevant
1901 * until its first write fault, when page's anon_vma and index
1902 * are set. But now set the vm_pgoff it will almost certainly
1903 * end up with (unless mremap moves it elsewhere before that
1904 * first wfault), so /proc/pid/maps tells a consistent story.
1906 * By setting it to reflect the virtual start address of the
1907 * vma, merges and splits can happen in a seamless way, just
1908 * using the existing file pgoff checks and manipulations.
1909 * Similarly in do_mmap_pgoff and in do_brk.
1911 if (!vma->vm_file) {
1912 BUG_ON(vma->anon_vma);
1913 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
1915 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
1916 if (__vma && __vma->vm_start < vma->vm_end)
1918 vma_link(mm, vma, prev, rb_link, rb_parent);
1922 * Copy the vma structure to a new location in the same mm,
1923 * prior to moving page table entries, to effect an mremap move.
1925 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1926 unsigned long addr, unsigned long len, pgoff_t pgoff)
1928 struct vm_area_struct *vma = *vmap;
1929 unsigned long vma_start = vma->vm_start;
1930 struct mm_struct *mm = vma->vm_mm;
1931 struct vm_area_struct *new_vma, *prev;
1932 struct rb_node **rb_link, *rb_parent;
1933 struct mempolicy *pol;
1936 * If anonymous vma has not yet been faulted, update new pgoff
1937 * to match new location, to increase its chance of merging.
1939 if (!vma->vm_file && !vma->anon_vma)
1940 pgoff = addr >> PAGE_SHIFT;
1942 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1943 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
1944 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
1947 * Source vma may have been merged into new_vma
1949 if (vma_start >= new_vma->vm_start &&
1950 vma_start < new_vma->vm_end)
1953 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1956 pol = mpol_copy(vma_policy(vma));
1958 kmem_cache_free(vm_area_cachep, new_vma);
1961 vma_set_policy(new_vma, pol);
1962 new_vma->vm_start = addr;
1963 new_vma->vm_end = addr + len;
1964 new_vma->vm_pgoff = pgoff;
1965 if (new_vma->vm_file)
1966 get_file(new_vma->vm_file);
1967 if (new_vma->vm_ops && new_vma->vm_ops->open)
1968 new_vma->vm_ops->open(new_vma);
1969 vma_link(mm, new_vma, prev, rb_link, rb_parent);