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 = 0; /* default is 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;
94 struct address_space *mapping = file->f_mapping;
95 spin_lock(&mapping->i_mmap_lock);
96 __remove_shared_vm_struct(vma, file, mapping);
97 spin_unlock(&mapping->i_mmap_lock);
99 if (vma->vm_ops && vma->vm_ops->close)
100 vma->vm_ops->close(vma);
103 anon_vma_unlink(vma);
104 mpol_free(vma_policy(vma));
105 kmem_cache_free(vm_area_cachep, vma);
109 * sys_brk() for the most part doesn't need the global kernel
110 * lock, except when an application is doing something nasty
111 * like trying to un-brk an area that has already been mapped
112 * to a regular file. in this case, the unmapping will need
113 * to invoke file system routines that need the global lock.
115 asmlinkage unsigned long sys_brk(unsigned long brk)
117 unsigned long rlim, retval;
118 unsigned long newbrk, oldbrk;
119 struct mm_struct *mm = current->mm;
121 down_write(&mm->mmap_sem);
123 if (brk < mm->end_code)
125 newbrk = PAGE_ALIGN(brk);
126 oldbrk = PAGE_ALIGN(mm->brk);
127 if (oldbrk == newbrk)
130 /* Always allow shrinking brk. */
131 if (brk <= mm->brk) {
132 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
137 /* Check against rlimit.. */
138 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
139 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
142 /* Check against existing mmap mappings. */
143 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
146 /* Ok, looks good - let it rip. */
147 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
153 up_write(&mm->mmap_sem);
158 static int browse_rb(struct rb_root *root)
161 struct rb_node *nd, *pn = NULL;
162 unsigned long prev = 0, pend = 0;
164 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
165 struct vm_area_struct *vma;
166 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
167 if (vma->vm_start < prev)
168 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
169 if (vma->vm_start < pend)
170 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
171 if (vma->vm_start > vma->vm_end)
172 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
177 for (nd = pn; nd; nd = rb_prev(nd)) {
181 printk("backwards %d, forwards %d\n", j, i), i = 0;
185 void validate_mm(struct mm_struct *mm)
189 struct vm_area_struct *tmp = mm->mmap;
194 if (i != mm->map_count)
195 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
196 i = browse_rb(&mm->mm_rb);
197 if (i != mm->map_count)
198 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
203 #define validate_mm(mm) do { } while (0)
206 static struct vm_area_struct *
207 find_vma_prepare(struct mm_struct *mm, unsigned long addr,
208 struct vm_area_struct **pprev, struct rb_node ***rb_link,
209 struct rb_node ** rb_parent)
211 struct vm_area_struct * vma;
212 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
214 __rb_link = &mm->mm_rb.rb_node;
215 rb_prev = __rb_parent = NULL;
219 struct vm_area_struct *vma_tmp;
221 __rb_parent = *__rb_link;
222 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
224 if (vma_tmp->vm_end > addr) {
226 if (vma_tmp->vm_start <= addr)
228 __rb_link = &__rb_parent->rb_left;
230 rb_prev = __rb_parent;
231 __rb_link = &__rb_parent->rb_right;
237 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
238 *rb_link = __rb_link;
239 *rb_parent = __rb_parent;
244 __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
245 struct vm_area_struct *prev, struct rb_node *rb_parent)
248 vma->vm_next = prev->vm_next;
253 vma->vm_next = rb_entry(rb_parent,
254 struct vm_area_struct, vm_rb);
260 void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
261 struct rb_node **rb_link, struct rb_node *rb_parent)
263 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
264 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
267 static inline void __vma_link_file(struct vm_area_struct *vma)
273 struct address_space *mapping = file->f_mapping;
275 if (vma->vm_flags & VM_DENYWRITE)
276 atomic_dec(&file->f_dentry->d_inode->i_writecount);
277 if (vma->vm_flags & VM_SHARED)
278 mapping->i_mmap_writable++;
280 flush_dcache_mmap_lock(mapping);
281 if (unlikely(vma->vm_flags & VM_NONLINEAR))
282 list_add_tail(&vma->shared.vm_set.list,
283 &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_init(next);
454 vma_prio_tree_insert(next, root);
456 vma_prio_tree_init(vma);
457 vma_prio_tree_insert(vma, root);
458 flush_dcache_mmap_unlock(mapping);
463 * vma_merge has merged next into vma, and needs
464 * us to remove next before dropping the locks.
466 __vma_unlink(mm, next, vma);
468 __remove_shared_vm_struct(next, file, mapping);
470 __anon_vma_merge(vma, next);
473 * split_vma has split insert from vma, and needs
474 * us to insert it before dropping the locks
475 * (it may either follow vma or precede it).
477 __insert_vm_struct(mm, insert);
481 spin_unlock(&anon_vma->lock);
483 spin_unlock(&mapping->i_mmap_lock);
489 mpol_free(vma_policy(next));
490 kmem_cache_free(vm_area_cachep, next);
492 * In mprotect's case 6 (see comments on vma_merge),
493 * we must remove another next too. It would clutter
494 * up the code too much to do both in one go.
496 if (remove_next == 2) {
506 * If the vma has a ->close operation then the driver probably needs to release
507 * per-vma resources, so we don't attempt to merge those.
509 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
511 static inline int is_mergeable_vma(struct vm_area_struct *vma,
512 struct file *file, unsigned long vm_flags)
514 if (vma->vm_flags != vm_flags)
516 if (vma->vm_file != file)
518 if (vma->vm_ops && vma->vm_ops->close)
523 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
524 struct anon_vma *anon_vma2)
526 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
530 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
531 * in front of (at a lower virtual address and file offset than) the vma.
533 * We cannot merge two vmas if they have differently assigned (non-NULL)
534 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
536 * We don't check here for the merged mmap wrapping around the end of pagecache
537 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
538 * wrap, nor mmaps which cover the final page at index -1UL.
541 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
542 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
544 if (is_mergeable_vma(vma, file, vm_flags) &&
545 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
546 if (vma->vm_pgoff == vm_pgoff)
553 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
554 * beyond (at a higher virtual address and file offset than) the vma.
556 * We cannot merge two vmas if they have differently assigned (non-NULL)
557 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
560 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
561 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
563 if (is_mergeable_vma(vma, file, vm_flags) &&
564 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
566 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
567 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
574 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
575 * whether that can be merged with its predecessor or its successor.
576 * Or both (it neatly fills a hole).
578 * In most cases - when called for mmap, brk or mremap - [addr,end) is
579 * certain not to be mapped by the time vma_merge is called; but when
580 * called for mprotect, it is certain to be already mapped (either at
581 * an offset within prev, or at the start of next), and the flags of
582 * this area are about to be changed to vm_flags - and the no-change
583 * case has already been eliminated.
585 * The following mprotect cases have to be considered, where AAAA is
586 * the area passed down from mprotect_fixup, never extending beyond one
587 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
589 * AAAA AAAA AAAA AAAA
590 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
591 * cannot merge might become might become might become
592 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
593 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
594 * mremap move: PPPPNNNNNNNN 8
596 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
597 * might become case 1 below case 2 below case 3 below
599 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
600 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
602 struct vm_area_struct *vma_merge(struct mm_struct *mm,
603 struct vm_area_struct *prev, unsigned long addr,
604 unsigned long end, unsigned long vm_flags,
605 struct anon_vma *anon_vma, struct file *file,
606 pgoff_t pgoff, struct mempolicy *policy)
608 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
609 struct vm_area_struct *area, *next;
612 * We later require that vma->vm_flags == vm_flags,
613 * so this tests vma->vm_flags & VM_SPECIAL, too.
615 if (vm_flags & VM_SPECIAL)
619 next = prev->vm_next;
623 if (next && next->vm_end == end) /* cases 6, 7, 8 */
624 next = next->vm_next;
627 * Can it merge with the predecessor?
629 if (prev && prev->vm_end == addr &&
630 mpol_equal(vma_policy(prev), policy) &&
631 can_vma_merge_after(prev, vm_flags,
632 anon_vma, file, pgoff)) {
634 * OK, it can. Can we now merge in the successor as well?
636 if (next && end == next->vm_start &&
637 mpol_equal(policy, vma_policy(next)) &&
638 can_vma_merge_before(next, vm_flags,
639 anon_vma, file, pgoff+pglen) &&
640 is_mergeable_anon_vma(prev->anon_vma,
643 vma_adjust(prev, prev->vm_start,
644 next->vm_end, prev->vm_pgoff, NULL);
645 } else /* cases 2, 5, 7 */
646 vma_adjust(prev, prev->vm_start,
647 end, prev->vm_pgoff, NULL);
652 * Can this new request be merged in front of next?
654 if (next && end == next->vm_start &&
655 mpol_equal(policy, vma_policy(next)) &&
656 can_vma_merge_before(next, vm_flags,
657 anon_vma, file, pgoff+pglen)) {
658 if (prev && addr < prev->vm_end) /* case 4 */
659 vma_adjust(prev, prev->vm_start,
660 addr, prev->vm_pgoff, NULL);
661 else /* cases 3, 8 */
662 vma_adjust(area, addr, next->vm_end,
663 next->vm_pgoff - pglen, NULL);
671 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
672 * neighbouring vmas for a suitable anon_vma, before it goes off
673 * to allocate a new anon_vma. It checks because a repetitive
674 * sequence of mprotects and faults may otherwise lead to distinct
675 * anon_vmas being allocated, preventing vma merge in subsequent
678 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
680 struct vm_area_struct *near;
681 unsigned long vm_flags;
688 * Since only mprotect tries to remerge vmas, match flags
689 * which might be mprotected into each other later on.
690 * Neither mlock nor madvise tries to remerge at present,
691 * so leave their flags as obstructing a merge.
693 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
694 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
696 if (near->anon_vma && vma->vm_end == near->vm_start &&
697 mpol_equal(vma_policy(vma), vma_policy(near)) &&
698 can_vma_merge_before(near, vm_flags,
699 NULL, vma->vm_file, vma->vm_pgoff +
700 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
701 return near->anon_vma;
704 * It is potentially slow to have to call find_vma_prev here.
705 * But it's only on the first write fault on the vma, not
706 * every time, and we could devise a way to avoid it later
707 * (e.g. stash info in next's anon_vma_node when assigning
708 * an anon_vma, or when trying vma_merge). Another time.
710 if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
715 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
716 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
718 if (near->anon_vma && near->vm_end == vma->vm_start &&
719 mpol_equal(vma_policy(near), vma_policy(vma)) &&
720 can_vma_merge_after(near, vm_flags,
721 NULL, vma->vm_file, vma->vm_pgoff))
722 return near->anon_vma;
725 * There's no absolute need to look only at touching neighbours:
726 * we could search further afield for "compatible" anon_vmas.
727 * But it would probably just be a waste of time searching,
728 * or lead to too many vmas hanging off the same anon_vma.
729 * We're trying to allow mprotect remerging later on,
730 * not trying to minimize memory used for anon_vmas.
736 * The caller must hold down_write(current->mm->mmap_sem).
739 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
740 unsigned long len, unsigned long prot,
741 unsigned long flags, unsigned long pgoff)
743 struct mm_struct * mm = current->mm;
744 struct vm_area_struct * vma, * prev;
746 unsigned int vm_flags;
747 int correct_wcount = 0;
749 struct rb_node ** rb_link, * rb_parent;
751 unsigned long charged = 0;
754 * Does the application expect PROT_READ to imply PROT_EXEC:
756 if (unlikely((prot & PROT_READ) &&
757 (current->personality & READ_IMPLIES_EXEC)))
761 if (is_file_hugepages(file))
764 if (!file->f_op || !file->f_op->mmap)
767 if ((prot & PROT_EXEC) &&
768 (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
775 /* Careful about overflows.. */
776 len = PAGE_ALIGN(len);
777 if (!len || len > TASK_SIZE)
780 /* offset overflow? */
781 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
784 /* Too many mappings? */
785 if (mm->map_count > sysctl_max_map_count)
788 /* Obtain the address to map to. we verify (or select) it and ensure
789 * that it represents a valid section of the address space.
791 addr = get_unmapped_area(file, addr, len, pgoff, flags);
792 if (addr & ~PAGE_MASK)
795 /* Do simple checking here so the lower-level routines won't have
796 * to. we assume access permissions have been handled by the open
797 * of the memory object, so we don't do any here.
799 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
800 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
802 if (flags & MAP_LOCKED) {
803 if (!capable(CAP_IPC_LOCK))
805 vm_flags |= VM_LOCKED;
807 /* mlock MCL_FUTURE? */
808 if (vm_flags & VM_LOCKED) {
809 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
811 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
815 inode = file ? file->f_dentry->d_inode : NULL;
818 switch (flags & MAP_TYPE) {
820 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
824 * Make sure we don't allow writing to an append-only
827 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
831 * Make sure there are no mandatory locks on the file.
833 if (locks_verify_locked(inode))
836 vm_flags |= VM_SHARED | VM_MAYSHARE;
837 if (!(file->f_mode & FMODE_WRITE))
838 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
842 if (!(file->f_mode & FMODE_READ))
850 switch (flags & MAP_TYPE) {
852 vm_flags |= VM_SHARED | VM_MAYSHARE;
856 * Set pgoff according to addr for anon_vma.
858 pgoff = addr >> PAGE_SHIFT;
865 error = security_file_mmap(file, prot, flags);
872 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
873 if (vma && vma->vm_start < addr + len) {
874 if (do_munmap(mm, addr, len))
879 /* Check against address space limit. */
880 if ((mm->total_vm << PAGE_SHIFT) + len
881 > current->rlim[RLIMIT_AS].rlim_cur)
884 /* check context space, maybe only Private writable mapping? */
885 if (!vx_vmpages_avail(mm, len >> PAGE_SHIFT))
888 if (accountable && (!(flags & MAP_NORESERVE) ||
889 sysctl_overcommit_memory > 1)) {
890 if (vm_flags & VM_SHARED) {
891 /* Check memory availability in shmem_file_setup? */
892 vm_flags |= VM_ACCOUNT;
893 } else if (vm_flags & VM_WRITE) {
895 * Private writable mapping: check memory availability
897 charged = len >> PAGE_SHIFT;
898 if (security_vm_enough_memory(charged))
900 vm_flags |= VM_ACCOUNT;
905 * Can we just expand an old private anonymous mapping?
906 * The VM_SHARED test is necessary because shmem_zero_setup
907 * will create the file object for a shared anonymous map below.
909 if (!file && !(vm_flags & VM_SHARED) &&
910 vma_merge(mm, prev, addr, addr + len, vm_flags,
911 NULL, NULL, pgoff, NULL))
915 * Determine the object being mapped and call the appropriate
916 * specific mapper. the address has already been validated, but
917 * not unmapped, but the maps are removed from the list.
919 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
924 memset(vma, 0, sizeof(*vma));
927 vma->vm_start = addr;
928 vma->vm_end = addr + len;
929 vma->vm_flags = vm_flags;
930 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
931 vma->vm_pgoff = pgoff;
935 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
937 if (vm_flags & VM_DENYWRITE) {
938 error = deny_write_access(file);
945 error = file->f_op->mmap(file, vma);
947 goto unmap_and_free_vma;
948 } else if (vm_flags & VM_SHARED) {
949 error = shmem_zero_setup(vma);
954 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
955 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
956 * that memory reservation must be checked; but that reservation
957 * belongs to shared memory object, not to vma: so now clear it.
959 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
960 vma->vm_flags &= ~VM_ACCOUNT;
962 /* Can addr have changed??
964 * Answer: Yes, several device drivers can do it in their
965 * f_op->mmap method. -DaveM
967 addr = vma->vm_start;
969 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
970 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
971 vma_link(mm, vma, prev, rb_link, rb_parent);
973 atomic_inc(&inode->i_writecount);
977 atomic_inc(&inode->i_writecount);
980 mpol_free(vma_policy(vma));
981 kmem_cache_free(vm_area_cachep, vma);
984 // mm->total_vm += len >> PAGE_SHIFT;
985 vx_vmpages_add(mm, len >> PAGE_SHIFT);
986 if (vm_flags & VM_LOCKED) {
987 // mm->locked_vm += len >> PAGE_SHIFT;
988 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
989 make_pages_present(addr, addr + len);
991 if (flags & MAP_POPULATE) {
992 up_write(&mm->mmap_sem);
993 sys_remap_file_pages(addr, len, 0,
994 pgoff, flags & MAP_NONBLOCK);
995 down_write(&mm->mmap_sem);
1001 atomic_inc(&inode->i_writecount);
1002 vma->vm_file = NULL;
1005 /* Undo any partial mapping done by a device driver. */
1006 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
1008 kmem_cache_free(vm_area_cachep, vma);
1011 vm_unacct_memory(charged);
1015 EXPORT_SYMBOL(do_mmap_pgoff);
1017 /* Get an address range which is currently unmapped.
1018 * For shmat() with addr=0.
1020 * Ugly calling convention alert:
1021 * Return value with the low bits set means error value,
1023 * if (ret & ~PAGE_MASK)
1026 * This function "knows" that -ENOMEM has the bits set.
1028 #ifndef HAVE_ARCH_UNMAPPED_AREA
1029 static inline unsigned long
1030 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1031 unsigned long len, unsigned long pgoff, unsigned long flags)
1033 struct mm_struct *mm = current->mm;
1034 struct vm_area_struct *vma;
1035 unsigned long start_addr;
1037 if (len > TASK_SIZE)
1041 addr = PAGE_ALIGN(addr);
1042 vma = find_vma(mm, addr);
1043 if (TASK_SIZE - len >= addr &&
1044 (!vma || addr + len <= vma->vm_start))
1047 start_addr = addr = mm->free_area_cache;
1050 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1051 /* At this point: (!vma || addr < vma->vm_end). */
1052 if (TASK_SIZE - len < addr) {
1054 * Start a new search - just in case we missed
1057 if (start_addr != TASK_UNMAPPED_BASE) {
1058 start_addr = addr = TASK_UNMAPPED_BASE;
1063 if (!vma || addr + len <= vma->vm_start) {
1065 * Remember the place where we stopped the search:
1067 mm->free_area_cache = addr + len;
1074 extern unsigned long
1075 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
1076 unsigned long, unsigned long);
1080 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1081 unsigned long pgoff, unsigned long flags)
1083 if (flags & MAP_FIXED) {
1086 if (addr > TASK_SIZE - len)
1088 if (addr & ~PAGE_MASK)
1090 if (file && is_file_hugepages(file)) {
1092 * Check if the given range is hugepage aligned, and
1093 * can be made suitable for hugepages.
1095 ret = prepare_hugepage_range(addr, len);
1098 * Ensure that a normal request is not falling in a
1099 * reserved hugepage range. For some archs like IA-64,
1100 * there is a separate region for hugepages.
1102 ret = is_hugepage_only_range(addr, len);
1109 if (file && file->f_op && file->f_op->get_unmapped_area)
1110 return file->f_op->get_unmapped_area(file, addr, len,
1113 return arch_get_unmapped_area(file, addr, len, pgoff, flags);
1116 EXPORT_SYMBOL(get_unmapped_area);
1118 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1119 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1121 struct vm_area_struct *vma = NULL;
1124 /* Check the cache first. */
1125 /* (Cache hit rate is typically around 35%.) */
1126 vma = mm->mmap_cache;
1127 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1128 struct rb_node * rb_node;
1130 rb_node = mm->mm_rb.rb_node;
1134 struct vm_area_struct * vma_tmp;
1136 vma_tmp = rb_entry(rb_node,
1137 struct vm_area_struct, vm_rb);
1139 if (vma_tmp->vm_end > addr) {
1141 if (vma_tmp->vm_start <= addr)
1143 rb_node = rb_node->rb_left;
1145 rb_node = rb_node->rb_right;
1148 mm->mmap_cache = vma;
1154 EXPORT_SYMBOL(find_vma);
1156 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1157 struct vm_area_struct *
1158 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1159 struct vm_area_struct **pprev)
1161 struct vm_area_struct *vma = NULL, *prev = NULL;
1162 struct rb_node * rb_node;
1166 /* Guard against addr being lower than the first VMA */
1169 /* Go through the RB tree quickly. */
1170 rb_node = mm->mm_rb.rb_node;
1173 struct vm_area_struct *vma_tmp;
1174 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1176 if (addr < vma_tmp->vm_end) {
1177 rb_node = rb_node->rb_left;
1180 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1182 rb_node = rb_node->rb_right;
1188 return prev ? prev->vm_next : vma;
1191 #ifdef CONFIG_STACK_GROWSUP
1193 * vma is the first one with address > vma->vm_end. Have to extend vma.
1195 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1199 if (!(vma->vm_flags & VM_GROWSUP))
1203 * We must make sure the anon_vma is allocated
1204 * so that the anon_vma locking is not a noop.
1206 if (unlikely(anon_vma_prepare(vma)))
1211 * vma->vm_start/vm_end cannot change under us because the caller
1212 * is required to hold the mmap_sem in read mode. We need the
1213 * anon_vma lock to serialize against concurrent expand_stacks.
1215 address += 4 + PAGE_SIZE - 1;
1216 address &= PAGE_MASK;
1217 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1219 /* Overcommit.. vx check first to avoid vm_unacct_memory() */
1220 if (!vx_vmpages_avail(vma->vm_mm, grow) ||
1221 security_vm_enough_memory(grow)) {
1222 anon_vma_unlock(vma);
1226 if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
1227 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1228 current->rlim[RLIMIT_AS].rlim_cur) {
1229 anon_vma_unlock(vma);
1230 vm_unacct_memory(grow);
1233 vma->vm_end = address;
1234 // vma->vm_mm->total_vm += grow;
1235 vx_vmpages_add(vma->vm_mm, grow);
1236 if (vma->vm_flags & VM_LOCKED)
1237 // vma->vm_mm->locked_vm += grow;
1238 vx_vmlocked_add(vma->vm_mm, grow);
1239 anon_vma_unlock(vma);
1243 struct vm_area_struct *
1244 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1246 struct vm_area_struct *vma, *prev;
1249 vma = find_vma_prev(mm, addr, &prev);
1250 if (vma && (vma->vm_start <= addr))
1252 if (!prev || expand_stack(prev, addr))
1254 if (prev->vm_flags & VM_LOCKED) {
1255 make_pages_present(addr, prev->vm_end);
1261 * vma is the first one with address < vma->vm_start. Have to extend vma.
1263 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1268 * We must make sure the anon_vma is allocated
1269 * so that the anon_vma locking is not a noop.
1271 if (unlikely(anon_vma_prepare(vma)))
1276 * vma->vm_start/vm_end cannot change under us because the caller
1277 * is required to hold the mmap_sem in read mode. We need the
1278 * anon_vma lock to serialize against concurrent expand_stacks.
1280 address &= PAGE_MASK;
1281 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1283 /* Overcommit.. vx check first to avoid vm_unacct_memory() */
1284 if (!vx_vmpages_avail(vma->vm_mm, grow) ||
1285 security_vm_enough_memory(grow)) {
1286 anon_vma_unlock(vma);
1290 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
1291 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1292 current->rlim[RLIMIT_AS].rlim_cur) {
1293 anon_vma_unlock(vma);
1294 vm_unacct_memory(grow);
1297 vma->vm_start = address;
1298 vma->vm_pgoff -= grow;
1299 // vma->vm_mm->total_vm += grow;
1300 vx_vmpages_add(vma->vm_mm, grow);
1301 if (vma->vm_flags & VM_LOCKED)
1302 // vma->vm_mm->locked_vm += grow;
1303 vx_vmlocked_add(vma->vm_mm, grow);
1304 anon_vma_unlock(vma);
1308 struct vm_area_struct *
1309 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1311 struct vm_area_struct * vma;
1312 unsigned long start;
1315 vma = find_vma(mm,addr);
1318 if (vma->vm_start <= addr)
1320 if (!(vma->vm_flags & VM_GROWSDOWN))
1322 start = vma->vm_start;
1323 if (expand_stack(vma, addr))
1325 if (vma->vm_flags & VM_LOCKED) {
1326 make_pages_present(addr, start);
1333 * Try to free as many page directory entries as we can,
1334 * without having to work very hard at actually scanning
1335 * the page tables themselves.
1337 * Right now we try to free page tables if we have a nice
1338 * PGDIR-aligned area that got free'd up. We could be more
1339 * granular if we want to, but this is fast and simple,
1340 * and covers the bad cases.
1342 * "prev", if it exists, points to a vma before the one
1343 * we just free'd - but there's no telling how much before.
1345 static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
1346 unsigned long start, unsigned long end)
1348 unsigned long first = start & PGDIR_MASK;
1349 unsigned long last = end + PGDIR_SIZE - 1;
1350 unsigned long start_index, end_index;
1351 struct mm_struct *mm = tlb->mm;
1357 if (prev->vm_end > start) {
1358 if (last > prev->vm_start)
1359 last = prev->vm_start;
1364 struct vm_area_struct *next = prev->vm_next;
1367 if (next->vm_start < start) {
1371 if (last > next->vm_start)
1372 last = next->vm_start;
1374 if (prev->vm_end > first)
1375 first = prev->vm_end + PGDIR_SIZE - 1;
1379 if (last < first) /* for arches with discontiguous pgd indices */
1382 * If the PGD bits are not consecutive in the virtual address, the
1383 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1385 start_index = pgd_index(first);
1386 if (start_index < FIRST_USER_PGD_NR)
1387 start_index = FIRST_USER_PGD_NR;
1388 end_index = pgd_index(last);
1389 if (end_index > start_index) {
1390 clear_page_tables(tlb, start_index, end_index - start_index);
1391 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
1395 /* Normal function to fix up a mapping
1396 * This function is the default for when an area has no specific
1397 * function. This may be used as part of a more specific routine.
1399 * By the time this function is called, the area struct has been
1400 * removed from the process mapping list.
1402 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1404 size_t len = area->vm_end - area->vm_start;
1406 // area->vm_mm->total_vm -= len >> PAGE_SHIFT;
1407 vx_vmpages_sub(area->vm_mm, len >> PAGE_SHIFT);
1409 if (area->vm_flags & VM_LOCKED)
1410 // area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1411 vx_vmlocked_sub(area->vm_mm, len >> PAGE_SHIFT);
1413 * Is this a new hole at the lowest possible address?
1415 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1416 area->vm_start < area->vm_mm->free_area_cache)
1417 area->vm_mm->free_area_cache = area->vm_start;
1419 remove_vm_struct(area);
1423 * Update the VMA and inode share lists.
1425 * Ok - we have the memory areas we should free on the 'free' list,
1426 * so release them, and do the vma updates.
1428 static void unmap_vma_list(struct mm_struct *mm,
1429 struct vm_area_struct *mpnt)
1432 struct vm_area_struct *next = mpnt->vm_next;
1433 unmap_vma(mm, mpnt);
1435 } while (mpnt != NULL);
1440 * Get rid of page table information in the indicated region.
1442 * Called with the page table lock held.
1444 static void unmap_region(struct mm_struct *mm,
1445 struct vm_area_struct *vma,
1446 struct vm_area_struct *prev,
1447 unsigned long start,
1450 struct mmu_gather *tlb;
1451 unsigned long nr_accounted = 0;
1454 tlb = tlb_gather_mmu(mm, 0);
1455 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1456 vm_unacct_memory(nr_accounted);
1458 if (is_hugepage_only_range(start, end - start))
1459 hugetlb_free_pgtables(tlb, prev, start, end);
1461 free_pgtables(tlb, prev, start, end);
1462 tlb_finish_mmu(tlb, start, end);
1466 * Create a list of vma's touched by the unmap, removing them from the mm's
1467 * vma list as we go..
1470 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1471 struct vm_area_struct *prev, unsigned long end)
1473 struct vm_area_struct **insertion_point;
1474 struct vm_area_struct *tail_vma = NULL;
1476 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1478 rb_erase(&vma->vm_rb, &mm->mm_rb);
1482 } while (vma && vma->vm_start < end);
1483 *insertion_point = vma;
1484 tail_vma->vm_next = NULL;
1485 mm->mmap_cache = NULL; /* Kill the cache. */
1489 * Split a vma into two pieces at address 'addr', a new vma is allocated
1490 * either for the first part or the the tail.
1492 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1493 unsigned long addr, int new_below)
1495 struct mempolicy *pol;
1496 struct vm_area_struct *new;
1498 if (mm->map_count >= sysctl_max_map_count)
1501 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1505 /* most fields are the same, copy all, and then fixup */
1507 vma_prio_tree_init(new);
1512 new->vm_start = addr;
1513 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1516 pol = mpol_copy(vma_policy(vma));
1518 kmem_cache_free(vm_area_cachep, new);
1519 return PTR_ERR(pol);
1521 vma_set_policy(new, pol);
1524 get_file(new->vm_file);
1526 if (new->vm_ops && new->vm_ops->open)
1527 new->vm_ops->open(new);
1530 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1531 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1533 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1538 /* Munmap is split into 2 main parts -- this part which finds
1539 * what needs doing, and the areas themselves, which do the
1540 * work. This now handles partial unmappings.
1541 * Jeremy Fitzhardinge <jeremy@goop.org>
1543 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1546 struct vm_area_struct *mpnt, *prev, *last;
1548 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1551 if ((len = PAGE_ALIGN(len)) == 0)
1554 /* Find the first overlapping VMA */
1555 mpnt = find_vma_prev(mm, start, &prev);
1558 /* we have start < mpnt->vm_end */
1560 if (is_vm_hugetlb_page(mpnt)) {
1561 int ret = is_aligned_hugepage_range(start, len);
1567 /* if it doesn't overlap, we have nothing.. */
1569 if (mpnt->vm_start >= end)
1572 /* Something will probably happen, so notify. */
1573 if (mpnt->vm_file && (mpnt->vm_flags & VM_EXEC))
1574 profile_exec_unmap(mm);
1577 * If we need to split any vma, do it now to save pain later.
1579 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1580 * unmapped vm_area_struct will remain in use: so lower split_vma
1581 * places tmp vma above, and higher split_vma places tmp vma below.
1583 if (start > mpnt->vm_start) {
1584 if (split_vma(mm, mpnt, start, 0))
1589 /* Does it split the last one? */
1590 last = find_vma(mm, end);
1591 if (last && end > last->vm_start) {
1592 if (split_vma(mm, last, end, 1))
1595 mpnt = prev? prev->vm_next: mm->mmap;
1598 * Remove the vma's, and unmap the actual pages
1600 detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1601 spin_lock(&mm->page_table_lock);
1602 unmap_region(mm, mpnt, prev, start, end);
1603 spin_unlock(&mm->page_table_lock);
1605 /* Fix up all other VM information */
1606 unmap_vma_list(mm, mpnt);
1611 EXPORT_SYMBOL(do_munmap);
1613 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1616 struct mm_struct *mm = current->mm;
1618 down_write(&mm->mmap_sem);
1619 ret = do_munmap(mm, addr, len);
1620 up_write(&mm->mmap_sem);
1625 * this is really a simplified "do_mmap". it only handles
1626 * anonymous maps. eventually we may be able to do some
1627 * brk-specific accounting here.
1629 unsigned long do_brk(unsigned long addr, unsigned long len)
1631 struct mm_struct * mm = current->mm;
1632 struct vm_area_struct * vma, * prev;
1633 unsigned long flags;
1634 struct rb_node ** rb_link, * rb_parent;
1635 pgoff_t pgoff = addr >> PAGE_SHIFT;
1637 len = PAGE_ALIGN(len);
1641 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1647 if (mm->def_flags & VM_LOCKED) {
1648 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
1650 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
1652 if (!vx_vmlocked_avail(mm, len >> PAGE_SHIFT))
1657 * Clear old maps. this also does some error checking for us
1660 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1661 if (vma && vma->vm_start < addr + len) {
1662 if (do_munmap(mm, addr, len))
1667 /* Check against address space limits *after* clearing old maps... */
1668 if ((mm->total_vm << PAGE_SHIFT) + len
1669 > current->rlim[RLIMIT_AS].rlim_cur)
1672 if (mm->map_count > sysctl_max_map_count)
1675 if (security_vm_enough_memory(len >> PAGE_SHIFT) ||
1676 !vx_vmpages_avail(mm, len >> PAGE_SHIFT))
1679 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1681 /* Can we just expand an old private anonymous mapping? */
1682 if (vma_merge(mm, prev, addr, addr + len, flags,
1683 NULL, NULL, pgoff, NULL))
1687 * create a vma struct for an anonymous mapping
1689 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1691 vm_unacct_memory(len >> PAGE_SHIFT);
1694 memset(vma, 0, sizeof(*vma));
1697 vma->vm_start = addr;
1698 vma->vm_end = addr + len;
1699 vma->vm_pgoff = pgoff;
1700 vma->vm_flags = flags;
1701 vma->vm_page_prot = protection_map[flags & 0x0f];
1702 vma_link(mm, vma, prev, rb_link, rb_parent);
1704 // mm->total_vm += len >> PAGE_SHIFT;
1705 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1706 if (flags & VM_LOCKED) {
1707 // mm->locked_vm += len >> PAGE_SHIFT;
1708 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1709 make_pages_present(addr, addr + len);
1714 EXPORT_SYMBOL(do_brk);
1716 /* Release all mmaps. */
1717 void exit_mmap(struct mm_struct *mm)
1719 struct mmu_gather *tlb;
1720 struct vm_area_struct *vma;
1721 unsigned long nr_accounted = 0;
1723 profile_exit_mmap(mm);
1727 spin_lock(&mm->page_table_lock);
1729 tlb = tlb_gather_mmu(mm, 1);
1731 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1732 mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1733 ~0UL, &nr_accounted, NULL);
1734 vm_unacct_memory(nr_accounted);
1735 BUG_ON(mm->map_count); /* This is just debugging */
1736 clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1737 tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1740 mm->mmap = mm->mmap_cache = NULL;
1741 mm->mm_rb = RB_ROOT;
1743 vx_rsspages_sub(mm, mm->rss);
1744 // mm->total_vm = 0;
1745 vx_vmpages_sub(mm, mm->total_vm);
1746 // mm->locked_vm = 0;
1747 vx_vmlocked_sub(mm, mm->locked_vm);
1749 spin_unlock(&mm->page_table_lock);
1752 * Walk the list again, actually closing and freeing it
1753 * without holding any MM locks.
1756 struct vm_area_struct *next = vma->vm_next;
1757 remove_vm_struct(vma);
1762 /* Insert vm structure into process list sorted by address
1763 * and into the inode's i_mmap tree. If vm_file is non-NULL
1764 * then i_mmap_lock is taken here.
1766 void insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1768 struct vm_area_struct * __vma, * prev;
1769 struct rb_node ** rb_link, * rb_parent;
1772 * The vm_pgoff of a purely anonymous vma should be irrelevant
1773 * until its first write fault, when page's anon_vma and index
1774 * are set. But now set the vm_pgoff it will almost certainly
1775 * end up with (unless mremap moves it elsewhere before that
1776 * first wfault), so /proc/pid/maps tells a consistent story.
1778 * By setting it to reflect the virtual start address of the
1779 * vma, merges and splits can happen in a seamless way, just
1780 * using the existing file pgoff checks and manipulations.
1781 * Similarly in do_mmap_pgoff and in do_brk.
1783 if (!vma->vm_file) {
1784 BUG_ON(vma->anon_vma);
1785 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
1787 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
1788 if (__vma && __vma->vm_start < vma->vm_end)
1790 vma_link(mm, vma, prev, rb_link, rb_parent);
1794 * Copy the vma structure to a new location in the same mm,
1795 * prior to moving page table entries, to effect an mremap move.
1797 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1798 unsigned long addr, unsigned long len, pgoff_t pgoff)
1800 struct vm_area_struct *vma = *vmap;
1801 unsigned long vma_start = vma->vm_start;
1802 struct mm_struct *mm = vma->vm_mm;
1803 struct vm_area_struct *new_vma, *prev;
1804 struct rb_node **rb_link, *rb_parent;
1805 struct mempolicy *pol;
1808 * If anonymous vma has not yet been faulted, update new pgoff
1809 * to match new location, to increase its chance of merging.
1811 if (!vma->vm_file && !vma->anon_vma)
1812 pgoff = addr >> PAGE_SHIFT;
1814 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1815 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
1816 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
1819 * Source vma may have been merged into new_vma
1821 if (vma_start >= new_vma->vm_start &&
1822 vma_start < new_vma->vm_end)
1825 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1828 vma_prio_tree_init(new_vma);
1829 pol = mpol_copy(vma_policy(vma));
1831 kmem_cache_free(vm_area_cachep, new_vma);
1834 vma_set_policy(new_vma, pol);
1835 new_vma->vm_start = addr;
1836 new_vma->vm_end = addr + len;
1837 new_vma->vm_pgoff = pgoff;
1838 if (new_vma->vm_file)
1839 get_file(new_vma->vm_file);
1840 if (new_vma->vm_ops && new_vma->vm_ops->open)
1841 new_vma->vm_ops->open(new_vma);
1842 vma_link(mm, new_vma, prev, rb_link, rb_parent);