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/pgalloc.h>
29 #include <asm/cacheflush.h>
33 * WARNING: the debugging will use recursive algorithms so never enable this
34 * unless you know what you are doing.
38 /* description of effects of mapping type and prot in current implementation.
39 * this is due to the limited x86 page protection hardware. The expected
40 * behavior is in parens:
43 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
44 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
45 * w: (no) no w: (no) no w: (yes) yes w: (no) no
46 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
48 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
49 * w: (no) no w: (no) no w: (copy) copy w: (no) no
50 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
53 pgprot_t protection_map[16] = {
54 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
55 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
58 int sysctl_overcommit_memory = 0; /* default is heuristic overcommit */
59 int sysctl_overcommit_ratio = 50; /* default is 50% */
60 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
61 atomic_t vm_committed_space = ATOMIC_INIT(0);
63 EXPORT_SYMBOL(sysctl_overcommit_memory);
64 EXPORT_SYMBOL(sysctl_overcommit_ratio);
65 EXPORT_SYMBOL(sysctl_max_map_count);
66 EXPORT_SYMBOL(vm_committed_space);
69 * Requires inode->i_mapping->i_mmap_lock
71 static void __remove_shared_vm_struct(struct vm_area_struct *vma,
72 struct file *file, struct address_space *mapping)
74 if (vma->vm_flags & VM_DENYWRITE)
75 atomic_inc(&file->f_dentry->d_inode->i_writecount);
76 if (vma->vm_flags & VM_SHARED)
77 mapping->i_mmap_writable--;
79 flush_dcache_mmap_lock(mapping);
80 if (unlikely(vma->vm_flags & VM_NONLINEAR))
81 list_del_init(&vma->shared.vm_set.list);
83 vma_prio_tree_remove(vma, &mapping->i_mmap);
84 flush_dcache_mmap_unlock(mapping);
88 * Remove one vm structure and free it.
90 static void remove_vm_struct(struct vm_area_struct *vma)
92 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 list_add_tail(&vma->shared.vm_set.list,
284 &mapping->i_mmap_nonlinear);
286 vma_prio_tree_insert(vma, &mapping->i_mmap);
287 flush_dcache_mmap_unlock(mapping);
292 __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
293 struct vm_area_struct *prev, struct rb_node **rb_link,
294 struct rb_node *rb_parent)
296 __vma_link_list(mm, vma, prev, rb_parent);
297 __vma_link_rb(mm, vma, rb_link, rb_parent);
298 __anon_vma_link(vma);
301 static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
302 struct vm_area_struct *prev, struct rb_node **rb_link,
303 struct rb_node *rb_parent)
305 struct address_space *mapping = NULL;
308 mapping = vma->vm_file->f_mapping;
311 spin_lock(&mapping->i_mmap_lock);
314 __vma_link(mm, vma, prev, rb_link, rb_parent);
315 __vma_link_file(vma);
317 anon_vma_unlock(vma);
319 spin_unlock(&mapping->i_mmap_lock);
321 mark_mm_hugetlb(mm, vma);
327 * Helper for vma_adjust in the split_vma insert case:
328 * insert vm structure into list and rbtree and anon_vma,
329 * but it has already been inserted into prio_tree earlier.
332 __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
334 struct vm_area_struct * __vma, * prev;
335 struct rb_node ** rb_link, * rb_parent;
337 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
338 if (__vma && __vma->vm_start < vma->vm_end)
340 __vma_link(mm, vma, prev, rb_link, rb_parent);
345 __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
346 struct vm_area_struct *prev)
348 prev->vm_next = vma->vm_next;
349 rb_erase(&vma->vm_rb, &mm->mm_rb);
350 if (mm->mmap_cache == vma)
351 mm->mmap_cache = prev;
355 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
356 * is already present in an i_mmap tree without adjusting the tree.
357 * The following helper function should be used when such adjustments
358 * are necessary. The "insert" vma (if any) is to be inserted
359 * before we drop the necessary locks.
361 void vma_adjust(struct vm_area_struct *vma, unsigned long start,
362 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
364 struct mm_struct *mm = vma->vm_mm;
365 struct vm_area_struct *next = vma->vm_next;
366 struct address_space *mapping = NULL;
367 struct prio_tree_root *root = NULL;
368 struct file *file = vma->vm_file;
369 struct anon_vma *anon_vma = NULL;
370 long adjust_next = 0;
373 if (next && !insert) {
374 if (end >= next->vm_end) {
376 * vma expands, overlapping all the next, and
377 * perhaps the one after too (mprotect case 6).
379 again: remove_next = 1 + (end > next->vm_end);
381 anon_vma = next->anon_vma;
382 } else if (end > next->vm_start) {
384 * vma expands, overlapping part of the next:
385 * mprotect case 5 shifting the boundary up.
387 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
388 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;
401 mapping = file->f_mapping;
402 if (!(vma->vm_flags & VM_NONLINEAR))
403 root = &mapping->i_mmap;
404 spin_lock(&mapping->i_mmap_lock);
407 * Put into prio_tree now, so instantiated pages
408 * are visible to arm/parisc __flush_dcache_page
409 * throughout; but we cannot insert into address
410 * space until vma start or end is updated.
412 __vma_link_file(insert);
417 * When changing only vma->vm_end, we don't really need
418 * anon_vma lock: but is that case worth optimizing out?
421 anon_vma = vma->anon_vma;
423 spin_lock(&anon_vma->lock);
426 flush_dcache_mmap_lock(mapping);
427 vma_prio_tree_remove(vma, root);
429 vma_prio_tree_remove(next, root);
432 vma->vm_start = start;
434 vma->vm_pgoff = pgoff;
436 next->vm_start += adjust_next << PAGE_SHIFT;
437 next->vm_pgoff += adjust_next;
442 vma_prio_tree_init(next);
443 vma_prio_tree_insert(next, root);
445 vma_prio_tree_init(vma);
446 vma_prio_tree_insert(vma, root);
447 flush_dcache_mmap_unlock(mapping);
452 * vma_merge has merged next into vma, and needs
453 * us to remove next before dropping the locks.
455 __vma_unlink(mm, next, vma);
457 __remove_shared_vm_struct(next, file, mapping);
459 __anon_vma_merge(vma, next);
462 * split_vma has split insert from vma, and needs
463 * us to insert it before dropping the locks
464 * (it may either follow vma or precede it).
466 __insert_vm_struct(mm, insert);
470 spin_unlock(&anon_vma->lock);
472 spin_unlock(&mapping->i_mmap_lock);
478 mpol_free(vma_policy(next));
479 kmem_cache_free(vm_area_cachep, next);
481 * In mprotect's case 6 (see comments on vma_merge),
482 * we must remove another next too. It would clutter
483 * up the code too much to do both in one go.
485 if (remove_next == 2) {
495 * If the vma has a ->close operation then the driver probably needs to release
496 * per-vma resources, so we don't attempt to merge those.
498 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
500 static inline int is_mergeable_vma(struct vm_area_struct *vma,
501 struct file *file, unsigned long vm_flags)
503 if (vma->vm_flags != vm_flags)
505 if (vma->vm_file != file)
507 if (vma->vm_ops && vma->vm_ops->close)
512 static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
513 struct anon_vma *anon_vma2)
515 return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
519 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
520 * in front of (at a lower virtual address and file offset than) the vma.
522 * We cannot merge two vmas if they have differently assigned (non-NULL)
523 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
525 * We don't check here for the merged mmap wrapping around the end of pagecache
526 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
527 * wrap, nor mmaps which cover the final page at index -1UL.
530 can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
531 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
533 if (is_mergeable_vma(vma, file, vm_flags) &&
534 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
535 if (vma->vm_pgoff == vm_pgoff)
542 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
543 * beyond (at a higher virtual address and file offset than) the vma.
545 * We cannot merge two vmas if they have differently assigned (non-NULL)
546 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
549 can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
550 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
552 if (is_mergeable_vma(vma, file, vm_flags) &&
553 is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
555 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
556 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
563 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
564 * whether that can be merged with its predecessor or its successor.
565 * Or both (it neatly fills a hole).
567 * In most cases - when called for mmap, brk or mremap - [addr,end) is
568 * certain not to be mapped by the time vma_merge is called; but when
569 * called for mprotect, it is certain to be already mapped (either at
570 * an offset within prev, or at the start of next), and the flags of
571 * this area are about to be changed to vm_flags - and the no-change
572 * case has already been eliminated.
574 * The following mprotect cases have to be considered, where AAAA is
575 * the area passed down from mprotect_fixup, never extending beyond one
576 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
578 * AAAA AAAA AAAA AAAA
579 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
580 * cannot merge might become might become might become
581 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
582 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
583 * mremap move: PPPPNNNNNNNN 8
585 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
586 * might become case 1 below case 2 below case 3 below
588 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
589 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
591 struct vm_area_struct *vma_merge(struct mm_struct *mm,
592 struct vm_area_struct *prev, unsigned long addr,
593 unsigned long end, unsigned long vm_flags,
594 struct anon_vma *anon_vma, struct file *file,
595 pgoff_t pgoff, struct mempolicy *policy)
597 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
598 struct vm_area_struct *area, *next;
601 * We later require that vma->vm_flags == vm_flags,
602 * so this tests vma->vm_flags & VM_SPECIAL, too.
604 if (vm_flags & VM_SPECIAL)
608 next = prev->vm_next;
612 if (next && next->vm_end == end) /* cases 6, 7, 8 */
613 next = next->vm_next;
616 * Can it merge with the predecessor?
618 if (prev && prev->vm_end == addr &&
619 mpol_equal(vma_policy(prev), policy) &&
620 can_vma_merge_after(prev, vm_flags,
621 anon_vma, file, pgoff)) {
623 * OK, it can. Can we now merge in the successor as well?
625 if (next && end == next->vm_start &&
626 mpol_equal(policy, vma_policy(next)) &&
627 can_vma_merge_before(next, vm_flags,
628 anon_vma, file, pgoff+pglen) &&
629 is_mergeable_anon_vma(prev->anon_vma,
632 vma_adjust(prev, prev->vm_start,
633 next->vm_end, prev->vm_pgoff, NULL);
634 } else /* cases 2, 5, 7 */
635 vma_adjust(prev, prev->vm_start,
636 end, prev->vm_pgoff, NULL);
641 * Can this new request be merged in front of next?
643 if (next && end == next->vm_start &&
644 mpol_equal(policy, vma_policy(next)) &&
645 can_vma_merge_before(next, vm_flags,
646 anon_vma, file, pgoff+pglen)) {
647 if (prev && addr < prev->vm_end) /* case 4 */
648 vma_adjust(prev, prev->vm_start,
649 addr, prev->vm_pgoff, NULL);
650 else /* cases 3, 8 */
651 vma_adjust(area, addr, next->vm_end,
652 next->vm_pgoff - pglen, NULL);
660 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
661 * neighbouring vmas for a suitable anon_vma, before it goes off
662 * to allocate a new anon_vma. It checks because a repetitive
663 * sequence of mprotects and faults may otherwise lead to distinct
664 * anon_vmas being allocated, preventing vma merge in subsequent
667 struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
669 struct vm_area_struct *near;
670 unsigned long vm_flags;
677 * Since only mprotect tries to remerge vmas, match flags
678 * which might be mprotected into each other later on.
679 * Neither mlock nor madvise tries to remerge at present,
680 * so leave their flags as obstructing a merge.
682 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
683 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
685 if (near->anon_vma && vma->vm_end == near->vm_start &&
686 mpol_equal(vma_policy(vma), vma_policy(near)) &&
687 can_vma_merge_before(near, vm_flags,
688 NULL, vma->vm_file, vma->vm_pgoff +
689 ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
690 return near->anon_vma;
693 * It is potentially slow to have to call find_vma_prev here.
694 * But it's only on the first write fault on the vma, not
695 * every time, and we could devise a way to avoid it later
696 * (e.g. stash info in next's anon_vma_node when assigning
697 * an anon_vma, or when trying vma_merge). Another time.
699 if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
704 vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
705 vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
707 if (near->anon_vma && near->vm_end == vma->vm_start &&
708 mpol_equal(vma_policy(near), vma_policy(vma)) &&
709 can_vma_merge_after(near, vm_flags,
710 NULL, vma->vm_file, vma->vm_pgoff))
711 return near->anon_vma;
714 * There's no absolute need to look only at touching neighbours:
715 * we could search further afield for "compatible" anon_vmas.
716 * But it would probably just be a waste of time searching,
717 * or lead to too many vmas hanging off the same anon_vma.
718 * We're trying to allow mprotect remerging later on,
719 * not trying to minimize memory used for anon_vmas.
725 * The caller must hold down_write(current->mm->mmap_sem).
728 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
729 unsigned long len, unsigned long prot,
730 unsigned long flags, unsigned long pgoff)
732 struct mm_struct * mm = current->mm;
733 struct vm_area_struct * vma, * prev;
735 unsigned int vm_flags;
736 int correct_wcount = 0;
738 struct rb_node ** rb_link, * rb_parent;
740 unsigned long charged = 0;
743 if (is_file_hugepages(file))
746 if (!file->f_op || !file->f_op->mmap)
749 if ((prot & PROT_EXEC) &&
750 (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
757 /* Careful about overflows.. */
758 len = PAGE_ALIGN(len);
759 if (!len || len > TASK_SIZE)
762 /* offset overflow? */
763 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
766 /* Too many mappings? */
767 if (mm->map_count > sysctl_max_map_count)
770 /* Obtain the address to map to. we verify (or select) it and ensure
771 * that it represents a valid section of the address space.
773 addr = get_unmapped_area(file, addr, len, pgoff, flags);
774 if (addr & ~PAGE_MASK)
777 /* Do simple checking here so the lower-level routines won't have
778 * to. we assume access permissions have been handled by the open
779 * of the memory object, so we don't do any here.
781 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
782 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
784 if (flags & MAP_LOCKED) {
785 if (!capable(CAP_IPC_LOCK))
787 vm_flags |= VM_LOCKED;
789 /* mlock MCL_FUTURE? */
790 if (vm_flags & VM_LOCKED) {
791 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
793 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
797 inode = file ? file->f_dentry->d_inode : NULL;
800 switch (flags & MAP_TYPE) {
802 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
806 * Make sure we don't allow writing to an append-only
809 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
813 * Make sure there are no mandatory locks on the file.
815 if (locks_verify_locked(inode))
818 vm_flags |= VM_SHARED | VM_MAYSHARE;
819 if (!(file->f_mode & FMODE_WRITE))
820 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
824 if (!(file->f_mode & FMODE_READ))
832 switch (flags & MAP_TYPE) {
834 vm_flags |= VM_SHARED | VM_MAYSHARE;
838 * Set pgoff according to addr for anon_vma.
840 pgoff = addr >> PAGE_SHIFT;
847 error = security_file_mmap(file, prot, flags);
854 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
855 if (vma && vma->vm_start < addr + len) {
856 if (do_munmap(mm, addr, len))
861 /* Check against address space limit. */
862 if ((mm->total_vm << PAGE_SHIFT) + len
863 > current->rlim[RLIMIT_AS].rlim_cur)
866 /* check context space, maybe only Private writable mapping? */
867 if (!vx_vmpages_avail(mm, len >> PAGE_SHIFT))
870 if (accountable && (!(flags & MAP_NORESERVE) ||
871 sysctl_overcommit_memory > 1)) {
872 if (vm_flags & VM_SHARED) {
873 /* Check memory availability in shmem_file_setup? */
874 vm_flags |= VM_ACCOUNT;
875 } else if (vm_flags & VM_WRITE) {
877 * Private writable mapping: check memory availability
879 charged = len >> PAGE_SHIFT;
880 if (security_vm_enough_memory(charged))
882 vm_flags |= VM_ACCOUNT;
887 * Can we just expand an old private anonymous mapping?
888 * The VM_SHARED test is necessary because shmem_zero_setup
889 * will create the file object for a shared anonymous map below.
891 if (!file && !(vm_flags & VM_SHARED) &&
892 vma_merge(mm, prev, addr, addr + len, vm_flags,
893 NULL, NULL, pgoff, NULL))
897 * Determine the object being mapped and call the appropriate
898 * specific mapper. the address has already been validated, but
899 * not unmapped, but the maps are removed from the list.
901 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
906 memset(vma, 0, sizeof(*vma));
909 vma->vm_start = addr;
910 vma->vm_end = addr + len;
911 vma->vm_flags = vm_flags;
912 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
913 vma->vm_pgoff = pgoff;
917 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
919 if (vm_flags & VM_DENYWRITE) {
920 error = deny_write_access(file);
927 error = file->f_op->mmap(file, vma);
929 goto unmap_and_free_vma;
930 } else if (vm_flags & VM_SHARED) {
931 error = shmem_zero_setup(vma);
936 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
937 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
938 * that memory reservation must be checked; but that reservation
939 * belongs to shared memory object, not to vma: so now clear it.
941 if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
942 vma->vm_flags &= ~VM_ACCOUNT;
944 /* Can addr have changed??
946 * Answer: Yes, several device drivers can do it in their
947 * f_op->mmap method. -DaveM
949 addr = vma->vm_start;
951 if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
952 vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
953 vma_link(mm, vma, prev, rb_link, rb_parent);
955 atomic_inc(&inode->i_writecount);
959 atomic_inc(&inode->i_writecount);
962 mpol_free(vma_policy(vma));
963 kmem_cache_free(vm_area_cachep, vma);
966 // mm->total_vm += len >> PAGE_SHIFT;
967 vx_vmpages_add(mm, len >> PAGE_SHIFT);
968 if (vm_flags & VM_LOCKED) {
969 // mm->locked_vm += len >> PAGE_SHIFT;
970 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
971 make_pages_present(addr, addr + len);
973 if (flags & MAP_POPULATE) {
974 up_write(&mm->mmap_sem);
975 sys_remap_file_pages(addr, len, 0,
976 pgoff, flags & MAP_NONBLOCK);
977 down_write(&mm->mmap_sem);
983 atomic_inc(&inode->i_writecount);
987 /* Undo any partial mapping done by a device driver. */
988 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
990 kmem_cache_free(vm_area_cachep, vma);
993 vm_unacct_memory(charged);
997 EXPORT_SYMBOL(do_mmap_pgoff);
999 /* Get an address range which is currently unmapped.
1000 * For shmat() with addr=0.
1002 * Ugly calling convention alert:
1003 * Return value with the low bits set means error value,
1005 * if (ret & ~PAGE_MASK)
1008 * This function "knows" that -ENOMEM has the bits set.
1010 #ifndef HAVE_ARCH_UNMAPPED_AREA
1011 static inline unsigned long
1012 arch_get_unmapped_area(struct file *filp, unsigned long addr,
1013 unsigned long len, unsigned long pgoff, unsigned long flags)
1015 struct mm_struct *mm = current->mm;
1016 struct vm_area_struct *vma;
1017 unsigned long start_addr;
1019 if (len > TASK_SIZE)
1023 addr = PAGE_ALIGN(addr);
1024 vma = find_vma(mm, addr);
1025 if (TASK_SIZE - len >= addr &&
1026 (!vma || addr + len <= vma->vm_start))
1029 start_addr = addr = mm->free_area_cache;
1032 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1033 /* At this point: (!vma || addr < vma->vm_end). */
1034 if (TASK_SIZE - len < addr) {
1036 * Start a new search - just in case we missed
1039 if (start_addr != TASK_UNMAPPED_BASE) {
1040 start_addr = addr = TASK_UNMAPPED_BASE;
1045 if (!vma || addr + len <= vma->vm_start) {
1047 * Remember the place where we stopped the search:
1049 mm->free_area_cache = addr + len;
1056 extern unsigned long
1057 arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
1058 unsigned long, unsigned long);
1062 get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1063 unsigned long pgoff, unsigned long flags)
1065 if (flags & MAP_FIXED) {
1068 if (addr > TASK_SIZE - len)
1070 if (addr & ~PAGE_MASK)
1072 if (file && is_file_hugepages(file)) {
1074 * Check if the given range is hugepage aligned, and
1075 * can be made suitable for hugepages.
1077 ret = prepare_hugepage_range(addr, len);
1080 * Ensure that a normal request is not falling in a
1081 * reserved hugepage range. For some archs like IA-64,
1082 * there is a separate region for hugepages.
1084 ret = is_hugepage_only_range(addr, len);
1091 if (file && file->f_op && file->f_op->get_unmapped_area)
1092 return file->f_op->get_unmapped_area(file, addr, len,
1095 return arch_get_unmapped_area(file, addr, len, pgoff, flags);
1098 EXPORT_SYMBOL(get_unmapped_area);
1100 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1101 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
1103 struct vm_area_struct *vma = NULL;
1106 /* Check the cache first. */
1107 /* (Cache hit rate is typically around 35%.) */
1108 vma = mm->mmap_cache;
1109 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1110 struct rb_node * rb_node;
1112 rb_node = mm->mm_rb.rb_node;
1116 struct vm_area_struct * vma_tmp;
1118 vma_tmp = rb_entry(rb_node,
1119 struct vm_area_struct, vm_rb);
1121 if (vma_tmp->vm_end > addr) {
1123 if (vma_tmp->vm_start <= addr)
1125 rb_node = rb_node->rb_left;
1127 rb_node = rb_node->rb_right;
1130 mm->mmap_cache = vma;
1136 EXPORT_SYMBOL(find_vma);
1138 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1139 struct vm_area_struct *
1140 find_vma_prev(struct mm_struct *mm, unsigned long addr,
1141 struct vm_area_struct **pprev)
1143 struct vm_area_struct *vma = NULL, *prev = NULL;
1144 struct rb_node * rb_node;
1148 /* Guard against addr being lower than the first VMA */
1151 /* Go through the RB tree quickly. */
1152 rb_node = mm->mm_rb.rb_node;
1155 struct vm_area_struct *vma_tmp;
1156 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1158 if (addr < vma_tmp->vm_end) {
1159 rb_node = rb_node->rb_left;
1162 if (!prev->vm_next || (addr < prev->vm_next->vm_end))
1164 rb_node = rb_node->rb_right;
1170 return prev ? prev->vm_next : vma;
1173 #ifdef CONFIG_STACK_GROWSUP
1175 * vma is the first one with address > vma->vm_end. Have to extend vma.
1177 int expand_stack(struct vm_area_struct * vma, unsigned long address)
1181 if (!(vma->vm_flags & VM_GROWSUP))
1185 * We must make sure the anon_vma is allocated
1186 * so that the anon_vma locking is not a noop.
1188 if (unlikely(anon_vma_prepare(vma)))
1193 * vma->vm_start/vm_end cannot change under us because the caller
1194 * is required to hold the mmap_sem in read mode. We need the
1195 * anon_vma lock to serialize against concurrent expand_stacks.
1197 address += 4 + PAGE_SIZE - 1;
1198 address &= PAGE_MASK;
1199 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1201 /* Overcommit.. vx check first to avoid vm_unacct_memory() */
1202 if (!vx_vmpages_avail(vma->vm_mm, grow) ||
1203 security_vm_enough_memory(grow)) {
1204 anon_vma_unlock(vma);
1208 if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
1209 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1210 current->rlim[RLIMIT_AS].rlim_cur) {
1211 anon_vma_unlock(vma);
1212 vm_unacct_memory(grow);
1215 vma->vm_end = address;
1216 // vma->vm_mm->total_vm += grow;
1217 vx_vmpages_add(vma->vm_mm, grow);
1218 if (vma->vm_flags & VM_LOCKED)
1219 // vma->vm_mm->locked_vm += grow;
1220 vx_vmlocked_add(vma->vm_mm, grow);
1221 anon_vma_unlock(vma);
1225 struct vm_area_struct *
1226 find_extend_vma(struct mm_struct *mm, unsigned long addr)
1228 struct vm_area_struct *vma, *prev;
1231 vma = find_vma_prev(mm, addr, &prev);
1232 if (vma && (vma->vm_start <= addr))
1234 if (!prev || expand_stack(prev, addr))
1236 if (prev->vm_flags & VM_LOCKED) {
1237 make_pages_present(addr, prev->vm_end);
1243 * vma is the first one with address < vma->vm_start. Have to extend vma.
1245 int expand_stack(struct vm_area_struct *vma, unsigned long address)
1250 * We must make sure the anon_vma is allocated
1251 * so that the anon_vma locking is not a noop.
1253 if (unlikely(anon_vma_prepare(vma)))
1258 * vma->vm_start/vm_end cannot change under us because the caller
1259 * is required to hold the mmap_sem in read mode. We need the
1260 * anon_vma lock to serialize against concurrent expand_stacks.
1262 address &= PAGE_MASK;
1263 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1265 /* Overcommit.. vx check first to avoid vm_unacct_memory() */
1266 if (!vx_vmpages_avail(vma->vm_mm, grow) ||
1267 security_vm_enough_memory(grow)) {
1268 anon_vma_unlock(vma);
1272 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
1273 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1274 current->rlim[RLIMIT_AS].rlim_cur) {
1275 anon_vma_unlock(vma);
1276 vm_unacct_memory(grow);
1279 vma->vm_start = address;
1280 vma->vm_pgoff -= grow;
1281 // vma->vm_mm->total_vm += grow;
1282 vx_vmpages_add(vma->vm_mm, grow);
1283 if (vma->vm_flags & VM_LOCKED)
1284 // vma->vm_mm->locked_vm += grow;
1285 vx_vmlocked_add(vma->vm_mm, grow);
1286 anon_vma_unlock(vma);
1290 struct vm_area_struct *
1291 find_extend_vma(struct mm_struct * mm, unsigned long addr)
1293 struct vm_area_struct * vma;
1294 unsigned long start;
1297 vma = find_vma(mm,addr);
1300 if (vma->vm_start <= addr)
1302 if (!(vma->vm_flags & VM_GROWSDOWN))
1304 start = vma->vm_start;
1305 if (expand_stack(vma, addr))
1307 if (vma->vm_flags & VM_LOCKED) {
1308 make_pages_present(addr, start);
1315 * Try to free as many page directory entries as we can,
1316 * without having to work very hard at actually scanning
1317 * the page tables themselves.
1319 * Right now we try to free page tables if we have a nice
1320 * PGDIR-aligned area that got free'd up. We could be more
1321 * granular if we want to, but this is fast and simple,
1322 * and covers the bad cases.
1324 * "prev", if it exists, points to a vma before the one
1325 * we just free'd - but there's no telling how much before.
1327 static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
1328 unsigned long start, unsigned long end)
1330 unsigned long first = start & PGDIR_MASK;
1331 unsigned long last = end + PGDIR_SIZE - 1;
1332 unsigned long start_index, end_index;
1333 struct mm_struct *mm = tlb->mm;
1339 if (prev->vm_end > start) {
1340 if (last > prev->vm_start)
1341 last = prev->vm_start;
1346 struct vm_area_struct *next = prev->vm_next;
1349 if (next->vm_start < start) {
1353 if (last > next->vm_start)
1354 last = next->vm_start;
1356 if (prev->vm_end > first)
1357 first = prev->vm_end + PGDIR_SIZE - 1;
1361 if (last < first) /* for arches with discontiguous pgd indices */
1364 * If the PGD bits are not consecutive in the virtual address, the
1365 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1367 start_index = pgd_index(first);
1368 if (start_index < FIRST_USER_PGD_NR)
1369 start_index = FIRST_USER_PGD_NR;
1370 end_index = pgd_index(last);
1371 if (end_index > start_index) {
1372 clear_page_tables(tlb, start_index, end_index - start_index);
1373 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
1377 /* Normal function to fix up a mapping
1378 * This function is the default for when an area has no specific
1379 * function. This may be used as part of a more specific routine.
1381 * By the time this function is called, the area struct has been
1382 * removed from the process mapping list.
1384 static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1386 size_t len = area->vm_end - area->vm_start;
1388 // area->vm_mm->total_vm -= len >> PAGE_SHIFT;
1389 vx_vmpages_sub(area->vm_mm, len >> PAGE_SHIFT);
1391 if (area->vm_flags & VM_LOCKED)
1392 // area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1393 vx_vmlocked_sub(area->vm_mm, len >> PAGE_SHIFT);
1395 * Is this a new hole at the lowest possible address?
1397 if (area->vm_start >= TASK_UNMAPPED_BASE &&
1398 area->vm_start < area->vm_mm->free_area_cache)
1399 area->vm_mm->free_area_cache = area->vm_start;
1401 remove_vm_struct(area);
1405 * Update the VMA and inode share lists.
1407 * Ok - we have the memory areas we should free on the 'free' list,
1408 * so release them, and do the vma updates.
1410 static void unmap_vma_list(struct mm_struct *mm,
1411 struct vm_area_struct *mpnt)
1414 struct vm_area_struct *next = mpnt->vm_next;
1415 unmap_vma(mm, mpnt);
1417 } while (mpnt != NULL);
1422 * Get rid of page table information in the indicated region.
1424 * Called with the page table lock held.
1426 static void unmap_region(struct mm_struct *mm,
1427 struct vm_area_struct *vma,
1428 struct vm_area_struct *prev,
1429 unsigned long start,
1432 struct mmu_gather *tlb;
1433 unsigned long nr_accounted = 0;
1436 tlb = tlb_gather_mmu(mm, 0);
1437 unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1438 vm_unacct_memory(nr_accounted);
1440 if (is_hugepage_only_range(start, end - start))
1441 hugetlb_free_pgtables(tlb, prev, start, end);
1443 free_pgtables(tlb, prev, start, end);
1444 tlb_finish_mmu(tlb, start, end);
1448 * Create a list of vma's touched by the unmap, removing them from the mm's
1449 * vma list as we go..
1452 detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
1453 struct vm_area_struct *prev, unsigned long end)
1455 struct vm_area_struct **insertion_point;
1456 struct vm_area_struct *tail_vma = NULL;
1458 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1460 rb_erase(&vma->vm_rb, &mm->mm_rb);
1464 } while (vma && vma->vm_start < end);
1465 *insertion_point = vma;
1466 tail_vma->vm_next = NULL;
1467 mm->mmap_cache = NULL; /* Kill the cache. */
1471 * Split a vma into two pieces at address 'addr', a new vma is allocated
1472 * either for the first part or the the tail.
1474 int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
1475 unsigned long addr, int new_below)
1477 struct mempolicy *pol;
1478 struct vm_area_struct *new;
1480 if (mm->map_count >= sysctl_max_map_count)
1483 new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1487 /* most fields are the same, copy all, and then fixup */
1489 vma_prio_tree_init(new);
1494 new->vm_start = addr;
1495 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
1498 pol = mpol_copy(vma_policy(vma));
1500 kmem_cache_free(vm_area_cachep, new);
1501 return PTR_ERR(pol);
1503 vma_set_policy(new, pol);
1506 get_file(new->vm_file);
1508 if (new->vm_ops && new->vm_ops->open)
1509 new->vm_ops->open(new);
1512 vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
1513 ((addr - new->vm_start) >> PAGE_SHIFT), new);
1515 vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1520 /* Munmap is split into 2 main parts -- this part which finds
1521 * what needs doing, and the areas themselves, which do the
1522 * work. This now handles partial unmappings.
1523 * Jeremy Fitzhardinge <jeremy@goop.org>
1525 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1528 struct vm_area_struct *mpnt, *prev, *last;
1530 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
1533 if ((len = PAGE_ALIGN(len)) == 0)
1536 /* Find the first overlapping VMA */
1537 mpnt = find_vma_prev(mm, start, &prev);
1540 /* we have start < mpnt->vm_end */
1542 if (is_vm_hugetlb_page(mpnt)) {
1543 int ret = is_aligned_hugepage_range(start, len);
1549 /* if it doesn't overlap, we have nothing.. */
1551 if (mpnt->vm_start >= end)
1554 /* Something will probably happen, so notify. */
1555 if (mpnt->vm_file && (mpnt->vm_flags & VM_EXEC))
1556 profile_exec_unmap(mm);
1559 * If we need to split any vma, do it now to save pain later.
1561 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1562 * unmapped vm_area_struct will remain in use: so lower split_vma
1563 * places tmp vma above, and higher split_vma places tmp vma below.
1565 if (start > mpnt->vm_start) {
1566 if (split_vma(mm, mpnt, start, 0))
1571 /* Does it split the last one? */
1572 last = find_vma(mm, end);
1573 if (last && end > last->vm_start) {
1574 if (split_vma(mm, last, end, 1))
1577 mpnt = prev? prev->vm_next: mm->mmap;
1580 * Remove the vma's, and unmap the actual pages
1582 detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1583 spin_lock(&mm->page_table_lock);
1584 unmap_region(mm, mpnt, prev, start, end);
1585 spin_unlock(&mm->page_table_lock);
1587 /* Fix up all other VM information */
1588 unmap_vma_list(mm, mpnt);
1593 EXPORT_SYMBOL(do_munmap);
1595 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1598 struct mm_struct *mm = current->mm;
1600 down_write(&mm->mmap_sem);
1601 ret = do_munmap(mm, addr, len);
1602 up_write(&mm->mmap_sem);
1607 * this is really a simplified "do_mmap". it only handles
1608 * anonymous maps. eventually we may be able to do some
1609 * brk-specific accounting here.
1611 unsigned long do_brk(unsigned long addr, unsigned long len)
1613 struct mm_struct * mm = current->mm;
1614 struct vm_area_struct * vma, * prev;
1615 unsigned long flags;
1616 struct rb_node ** rb_link, * rb_parent;
1617 pgoff_t pgoff = addr >> PAGE_SHIFT;
1619 len = PAGE_ALIGN(len);
1623 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1629 if (mm->def_flags & VM_LOCKED) {
1630 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
1632 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
1634 if (!vx_vmlocked_avail(mm, len >> PAGE_SHIFT))
1639 * Clear old maps. this also does some error checking for us
1642 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1643 if (vma && vma->vm_start < addr + len) {
1644 if (do_munmap(mm, addr, len))
1649 /* Check against address space limits *after* clearing old maps... */
1650 if ((mm->total_vm << PAGE_SHIFT) + len
1651 > current->rlim[RLIMIT_AS].rlim_cur)
1654 if (mm->map_count > sysctl_max_map_count)
1657 if (security_vm_enough_memory(len >> PAGE_SHIFT) ||
1658 !vx_vmpages_avail(mm, len >> PAGE_SHIFT))
1661 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
1663 /* Can we just expand an old private anonymous mapping? */
1664 if (vma_merge(mm, prev, addr, addr + len, flags,
1665 NULL, NULL, pgoff, NULL))
1669 * create a vma struct for an anonymous mapping
1671 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1673 vm_unacct_memory(len >> PAGE_SHIFT);
1676 memset(vma, 0, sizeof(*vma));
1679 vma->vm_start = addr;
1680 vma->vm_end = addr + len;
1681 vma->vm_pgoff = pgoff;
1682 vma->vm_flags = flags;
1683 vma->vm_page_prot = protection_map[flags & 0x0f];
1684 vma_link(mm, vma, prev, rb_link, rb_parent);
1686 // mm->total_vm += len >> PAGE_SHIFT;
1687 vx_vmpages_add(mm, len >> PAGE_SHIFT);
1688 if (flags & VM_LOCKED) {
1689 // mm->locked_vm += len >> PAGE_SHIFT;
1690 vx_vmlocked_add(mm, len >> PAGE_SHIFT);
1691 make_pages_present(addr, addr + len);
1696 EXPORT_SYMBOL(do_brk);
1698 /* Release all mmaps. */
1699 void exit_mmap(struct mm_struct *mm)
1701 struct mmu_gather *tlb;
1702 struct vm_area_struct *vma;
1703 unsigned long nr_accounted = 0;
1705 profile_exit_mmap(mm);
1709 spin_lock(&mm->page_table_lock);
1711 tlb = tlb_gather_mmu(mm, 1);
1713 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1714 mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1715 ~0UL, &nr_accounted, NULL);
1716 vm_unacct_memory(nr_accounted);
1717 BUG_ON(mm->map_count); /* This is just debugging */
1718 clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1719 tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1722 mm->mmap = mm->mmap_cache = NULL;
1723 mm->mm_rb = RB_ROOT;
1725 vx_rsspages_sub(mm, mm->rss);
1726 // mm->total_vm = 0;
1727 vx_vmpages_sub(mm, mm->total_vm);
1728 // mm->locked_vm = 0;
1729 vx_vmlocked_sub(mm, mm->locked_vm);
1731 spin_unlock(&mm->page_table_lock);
1734 * Walk the list again, actually closing and freeing it
1735 * without holding any MM locks.
1738 struct vm_area_struct *next = vma->vm_next;
1739 remove_vm_struct(vma);
1744 /* Insert vm structure into process list sorted by address
1745 * and into the inode's i_mmap tree. If vm_file is non-NULL
1746 * then i_mmap_lock is taken here.
1748 void insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1750 struct vm_area_struct * __vma, * prev;
1751 struct rb_node ** rb_link, * rb_parent;
1754 * The vm_pgoff of a purely anonymous vma should be irrelevant
1755 * until its first write fault, when page's anon_vma and index
1756 * are set. But now set the vm_pgoff it will almost certainly
1757 * end up with (unless mremap moves it elsewhere before that
1758 * first wfault), so /proc/pid/maps tells a consistent story.
1760 * By setting it to reflect the virtual start address of the
1761 * vma, merges and splits can happen in a seamless way, just
1762 * using the existing file pgoff checks and manipulations.
1763 * Similarly in do_mmap_pgoff and in do_brk.
1765 if (!vma->vm_file) {
1766 BUG_ON(vma->anon_vma);
1767 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
1769 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
1770 if (__vma && __vma->vm_start < vma->vm_end)
1772 vma_link(mm, vma, prev, rb_link, rb_parent);
1776 * Copy the vma structure to a new location in the same mm,
1777 * prior to moving page table entries, to effect an mremap move.
1779 struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1780 unsigned long addr, unsigned long len, pgoff_t pgoff)
1782 struct vm_area_struct *vma = *vmap;
1783 unsigned long vma_start = vma->vm_start;
1784 struct mm_struct *mm = vma->vm_mm;
1785 struct vm_area_struct *new_vma, *prev;
1786 struct rb_node **rb_link, *rb_parent;
1787 struct mempolicy *pol;
1790 * If anonymous vma has not yet been faulted, update new pgoff
1791 * to match new location, to increase its chance of merging.
1793 if (!vma->vm_file && !vma->anon_vma)
1794 pgoff = addr >> PAGE_SHIFT;
1796 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1797 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
1798 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
1801 * Source vma may have been merged into new_vma
1803 if (vma_start >= new_vma->vm_start &&
1804 vma_start < new_vma->vm_end)
1807 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1810 vma_prio_tree_init(new_vma);
1811 pol = mpol_copy(vma_policy(vma));
1813 kmem_cache_free(vm_area_cachep, new_vma);
1816 vma_set_policy(new_vma, pol);
1817 new_vma->vm_start = addr;
1818 new_vma->vm_end = addr + len;
1819 new_vma->vm_pgoff = pgoff;
1820 if (new_vma->vm_file)
1821 get_file(new_vma->vm_file);
1822 if (new_vma->vm_ops && new_vma->vm_ops->open)
1823 new_vma->vm_ops->open(new_vma);
1824 vma_link(mm, new_vma, prev, rb_link, rb_parent);