6 #include <linux/slab.h>
8 #include <linux/mman.h>
9 #include <linux/pagemap.h>
10 #include <linux/swap.h>
11 #include <linux/swapctl.h>
12 #include <linux/smp_lock.h>
13 #include <linux/init.h>
14 #include <linux/file.h>
16 #include <linux/personality.h>
17 #include <linux/mount.h>
19 #include <asm/uaccess.h>
20 #include <asm/pgalloc.h>
23 * WARNING: the debugging will use recursive algorithms so never enable this
24 * unless you know what you are doing.
28 /* description of effects of mapping type and prot in current implementation.
29 * this is due to the limited x86 page protection hardware. The expected
30 * behavior is in parens:
33 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
34 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
35 * w: (no) no w: (no) no w: (yes) yes w: (no) no
36 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
38 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
39 * w: (no) no w: (no) no w: (copy) copy w: (no) no
40 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
43 pgprot_t protection_map[16] = {
44 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
45 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
48 int sysctl_overcommit_memory;
49 unsigned long mmap_min_addr; /* defaults to 0 = no protection */
51 int max_map_count = DEFAULT_MAX_MAP_COUNT;
53 /* Check that a process has enough memory to allocate a
54 * new virtual mapping.
56 int vm_enough_memory(long pages)
58 /* Stupid algorithm to decide if we have enough memory: while
59 * simple, it hopefully works in most obvious cases.. Easy to
60 * fool it, but this should catch most mistakes.
62 /* 23/11/98 NJC: Somewhat less stupid version of algorithm,
63 * which tries to do "TheRightThing". Instead of using half of
64 * (buffers+cache), use the minimum values. Allow an extra 2%
65 * of num_physpages for safety margin.
70 /* Sometimes we want to use more memory than we have. */
71 if (sysctl_overcommit_memory)
74 /* The page cache contains buffer pages these days.. */
75 free = page_cache_size;
76 free += nr_free_pages();
77 free += nr_swap_pages;
80 * This double-counts: the nrpages are both in the page-cache
81 * and in the swapper space. At the same time, this compensates
82 * for the swap-space over-allocation (ie "nr_swap_pages" being
85 free += swapper_space.nrpages;
88 * The code below doesn't account for free space in the inode
89 * and dentry slab cache, slab cache fragmentation, inodes and
90 * dentries which will become freeable under VM load, etc.
91 * Lets just hope all these (complex) factors balance out...
93 free += (dentry_stat.nr_unused * sizeof(struct dentry)) >> PAGE_SHIFT;
94 free += (inodes_stat.nr_unused * sizeof(struct inode)) >> PAGE_SHIFT;
99 /* Remove one vm structure from the inode's i_mapping address space. */
100 static inline void __remove_shared_vm_struct(struct vm_area_struct *vma)
102 struct file * file = vma->vm_file;
105 struct inode *inode = file->f_dentry->d_inode;
106 if (vma->vm_flags & VM_DENYWRITE)
107 atomic_inc(&inode->i_writecount);
108 if(vma->vm_next_share)
109 vma->vm_next_share->vm_pprev_share = vma->vm_pprev_share;
110 *vma->vm_pprev_share = vma->vm_next_share;
114 static inline void remove_shared_vm_struct(struct vm_area_struct *vma)
116 lock_vma_mappings(vma);
117 __remove_shared_vm_struct(vma);
118 unlock_vma_mappings(vma);
121 void lock_vma_mappings(struct vm_area_struct *vma)
123 struct address_space *mapping;
127 mapping = vma->vm_file->f_dentry->d_inode->i_mapping;
129 spin_lock(&mapping->i_shared_lock);
132 void unlock_vma_mappings(struct vm_area_struct *vma)
134 struct address_space *mapping;
138 mapping = vma->vm_file->f_dentry->d_inode->i_mapping;
140 spin_unlock(&mapping->i_shared_lock);
144 * sys_brk() for the most part doesn't need the global kernel
145 * lock, except when an application is doing something nasty
146 * like trying to un-brk an area that has already been mapped
147 * to a regular file. in this case, the unmapping will need
148 * to invoke file system routines that need the global lock.
150 asmlinkage unsigned long sys_brk(unsigned long brk)
152 unsigned long rlim, retval;
153 unsigned long newbrk, oldbrk;
154 struct mm_struct *mm = current->mm;
156 down_write(&mm->mmap_sem);
158 if (brk < mm->end_code)
160 newbrk = PAGE_ALIGN(brk);
161 oldbrk = PAGE_ALIGN(mm->brk);
162 if (oldbrk == newbrk)
165 /* Always allow shrinking brk. */
166 if (brk <= mm->brk) {
167 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
172 /* Check against rlimit.. */
173 rlim = current->rlim[RLIMIT_DATA].rlim_cur;
174 if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
177 /* Check against existing mmap mappings. */
178 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
181 /* Check if we have enough memory.. */
182 if (!vm_enough_memory((newbrk-oldbrk) >> PAGE_SHIFT))
185 /* Ok, looks good - let it rip. */
186 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
192 up_write(&mm->mmap_sem);
196 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
197 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
200 static inline unsigned long calc_vm_flags(unsigned long prot, unsigned long flags)
202 #define _trans(x,bit1,bit2) \
203 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
205 unsigned long prot_bits, flag_bits;
207 _trans(prot, PROT_READ, VM_READ) |
208 _trans(prot, PROT_WRITE, VM_WRITE) |
209 _trans(prot, PROT_EXEC, VM_EXEC);
211 _trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN) |
212 _trans(flags, MAP_DENYWRITE, VM_DENYWRITE) |
213 _trans(flags, MAP_EXECUTABLE, VM_EXECUTABLE);
214 return prot_bits | flag_bits;
219 static int browse_rb(rb_node_t * rb_node) {
223 i += browse_rb(rb_node->rb_left);
224 i += browse_rb(rb_node->rb_right);
229 static void validate_mm(struct mm_struct * mm) {
232 struct vm_area_struct * tmp = mm->mmap;
237 if (i != mm->map_count)
238 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
239 i = browse_rb(mm->mm_rb.rb_node);
240 if (i != mm->map_count)
241 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
246 #define validate_mm(mm) do { } while (0)
249 static struct vm_area_struct * find_vma_prepare(struct mm_struct * mm, unsigned long addr,
250 struct vm_area_struct ** pprev,
251 rb_node_t *** rb_link, rb_node_t ** rb_parent)
253 struct vm_area_struct * vma;
254 rb_node_t ** __rb_link, * __rb_parent, * rb_prev;
256 __rb_link = &mm->mm_rb.rb_node;
257 rb_prev = __rb_parent = NULL;
261 struct vm_area_struct *vma_tmp;
263 __rb_parent = *__rb_link;
264 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
266 if (vma_tmp->vm_end > addr) {
268 if (vma_tmp->vm_start <= addr)
270 __rb_link = &__rb_parent->rb_left;
272 rb_prev = __rb_parent;
273 __rb_link = &__rb_parent->rb_right;
279 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
280 *rb_link = __rb_link;
281 *rb_parent = __rb_parent;
285 static inline void __vma_link_list(struct mm_struct * mm, struct vm_area_struct * vma, struct vm_area_struct * prev,
286 rb_node_t * rb_parent)
289 vma->vm_next = prev->vm_next;
294 vma->vm_next = rb_entry(rb_parent, struct vm_area_struct, vm_rb);
300 static inline void __vma_link_rb(struct mm_struct * mm, struct vm_area_struct * vma,
301 rb_node_t ** rb_link, rb_node_t * rb_parent)
303 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
304 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
307 static inline void __vma_link_file(struct vm_area_struct * vma)
313 struct inode * inode = file->f_dentry->d_inode;
314 struct address_space *mapping = inode->i_mapping;
315 struct vm_area_struct **head;
317 if (vma->vm_flags & VM_DENYWRITE)
318 atomic_dec(&inode->i_writecount);
320 head = &mapping->i_mmap;
321 if (vma->vm_flags & VM_SHARED)
322 head = &mapping->i_mmap_shared;
324 /* insert vma into inode's share list */
325 if((vma->vm_next_share = *head) != NULL)
326 (*head)->vm_pprev_share = &vma->vm_next_share;
328 vma->vm_pprev_share = head;
332 static void __vma_link(struct mm_struct * mm, struct vm_area_struct * vma, struct vm_area_struct * prev,
333 rb_node_t ** rb_link, rb_node_t * rb_parent)
335 __vma_link_list(mm, vma, prev, rb_parent);
336 __vma_link_rb(mm, vma, rb_link, rb_parent);
337 __vma_link_file(vma);
340 static inline void vma_link(struct mm_struct * mm, struct vm_area_struct * vma, struct vm_area_struct * prev,
341 rb_node_t ** rb_link, rb_node_t * rb_parent)
343 lock_vma_mappings(vma);
344 spin_lock(&mm->page_table_lock);
345 __vma_link(mm, vma, prev, rb_link, rb_parent);
346 spin_unlock(&mm->page_table_lock);
347 unlock_vma_mappings(vma);
353 static int vma_merge(struct mm_struct * mm, struct vm_area_struct * prev,
354 rb_node_t * rb_parent, unsigned long addr, unsigned long end, unsigned long vm_flags)
356 spinlock_t * lock = &mm->page_table_lock;
358 prev = rb_entry(rb_parent, struct vm_area_struct, vm_rb);
361 if (prev->vm_end == addr && can_vma_merge(prev, vm_flags)) {
362 struct vm_area_struct * next;
366 next = prev->vm_next;
367 if (next && prev->vm_end == next->vm_start && can_vma_merge(next, vm_flags)) {
368 prev->vm_end = next->vm_end;
369 __vma_unlink(mm, next, prev);
373 kmem_cache_free(vm_area_cachep, next);
380 prev = prev->vm_next;
383 if (!can_vma_merge(prev, vm_flags))
385 if (end == prev->vm_start) {
387 prev->vm_start = addr;
396 unsigned long do_mmap_pgoff(struct file * file, unsigned long addr, unsigned long len,
397 unsigned long prot, unsigned long flags, unsigned long pgoff)
399 struct mm_struct * mm = current->mm;
400 struct vm_area_struct * vma, * prev;
401 unsigned int vm_flags;
402 int correct_wcount = 0;
404 rb_node_t ** rb_link, * rb_parent;
407 if (!file->f_op || !file->f_op->mmap)
410 if ((prot & PROT_EXEC) && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
417 len = PAGE_ALIGN(len);
419 if (len > TASK_SIZE || len == 0)
422 /* offset overflow? */
423 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
426 /* Too many mappings? */
427 if (mm->map_count > max_map_count)
430 /* Obtain the address to map to. we verify (or select) it and ensure
431 * that it represents a valid section of the address space.
433 addr = get_unmapped_area(file, addr, len, pgoff, flags);
434 if (addr & ~PAGE_MASK)
437 /* Do simple checking here so the lower-level routines won't have
438 * to. we assume access permissions have been handled by the open
439 * of the memory object, so we don't do any here.
441 vm_flags = calc_vm_flags(prot,flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
443 /* mlock MCL_FUTURE? */
444 if (vm_flags & VM_LOCKED) {
445 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
447 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
452 switch (flags & MAP_TYPE) {
454 if ((prot & PROT_WRITE) && !(file->f_mode & FMODE_WRITE))
457 /* Make sure we don't allow writing to an append-only file.. */
458 if (IS_APPEND(file->f_dentry->d_inode) && (file->f_mode & FMODE_WRITE))
461 /* make sure there are no mandatory locks on the file. */
462 if (locks_verify_locked(file->f_dentry->d_inode))
465 vm_flags |= VM_SHARED | VM_MAYSHARE;
466 if (!(file->f_mode & FMODE_WRITE))
467 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
471 if (!(file->f_mode & FMODE_READ))
479 vm_flags |= VM_SHARED | VM_MAYSHARE;
480 switch (flags & MAP_TYPE) {
484 vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
493 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
494 if (vma && vma->vm_start < addr + len) {
495 if (do_munmap(mm, addr, len))
500 /* Check against address space limit. */
501 if ((mm->total_vm << PAGE_SHIFT) + len
502 > current->rlim[RLIMIT_AS].rlim_cur)
505 /* Private writable mapping? Check memory availability.. */
506 if ((vm_flags & (VM_SHARED | VM_WRITE)) == VM_WRITE &&
507 !(flags & MAP_NORESERVE) &&
508 !vm_enough_memory(len >> PAGE_SHIFT))
511 /* Can we just expand an old anonymous mapping? */
512 if (!file && !(vm_flags & VM_SHARED) && rb_parent)
513 if (vma_merge(mm, prev, rb_parent, addr, addr + len, vm_flags))
516 /* Determine the object being mapped and call the appropriate
517 * specific mapper. the address has already been validated, but
518 * not unmapped, but the maps are removed from the list.
520 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
525 vma->vm_start = addr;
526 vma->vm_end = addr + len;
527 vma->vm_flags = vm_flags;
528 vma->vm_page_prot = protection_map[vm_flags & 0x0f];
530 vma->vm_pgoff = pgoff;
532 vma->vm_private_data = NULL;
537 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
539 if (vm_flags & VM_DENYWRITE) {
540 error = deny_write_access(file);
547 error = file->f_op->mmap(file, vma);
549 goto unmap_and_free_vma;
550 } else if (flags & MAP_SHARED) {
551 error = shmem_zero_setup(vma);
556 /* Can addr have changed??
558 * Answer: Yes, several device drivers can do it in their
559 * f_op->mmap method. -DaveM
561 if (addr != vma->vm_start) {
563 * It is a bit too late to pretend changing the virtual
564 * area of the mapping, we just corrupted userspace
565 * in the do_munmap, so FIXME (not in 2.4 to avoid breaking
568 struct vm_area_struct * stale_vma;
569 /* Since addr changed, we rely on the mmap op to prevent
570 * collisions with existing vmas and just use find_vma_prepare
571 * to update the tree pointers.
573 addr = vma->vm_start;
574 stale_vma = find_vma_prepare(mm, addr, &prev,
575 &rb_link, &rb_parent);
577 * Make sure the lowlevel driver did its job right.
579 if (unlikely(stale_vma && stale_vma->vm_start < vma->vm_end)) {
580 printk(KERN_ERR "buggy mmap operation: [<%p>]\n",
581 file ? file->f_op->mmap : NULL);
586 vma_link(mm, vma, prev, rb_link, rb_parent);
588 atomic_inc(&file->f_dentry->d_inode->i_writecount);
591 mm->total_vm += len >> PAGE_SHIFT;
592 if (vm_flags & VM_LOCKED) {
593 mm->locked_vm += len >> PAGE_SHIFT;
594 make_pages_present(addr, addr + len);
600 atomic_inc(&file->f_dentry->d_inode->i_writecount);
604 /* Undo any partial mapping done by a device driver. */
605 zap_page_range(mm, vma->vm_start, vma->vm_end - vma->vm_start);
607 kmem_cache_free(vm_area_cachep, vma);
611 /* Get an address range which is currently unmapped.
612 * For shmat() with addr=0.
614 * Ugly calling convention alert:
615 * Return value with the low bits set means error value,
617 * if (ret & ~PAGE_MASK)
620 * This function "knows" that -ENOMEM has the bits set.
622 #ifndef HAVE_ARCH_UNMAPPED_AREA
623 static inline unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
625 struct vm_area_struct *vma;
631 addr = PAGE_ALIGN(addr);
632 vma = find_vma(current->mm, addr);
633 if (TASK_SIZE - len >= addr &&
634 (!vma || addr + len <= vma->vm_start))
637 addr = PAGE_ALIGN(TASK_UNMAPPED_BASE);
639 for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
640 /* At this point: (!vma || addr < vma->vm_end). */
641 if (TASK_SIZE - len < addr)
643 if (!vma || addr + len <= vma->vm_start)
649 extern unsigned long arch_get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
652 unsigned long get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
654 if (flags & MAP_FIXED) {
655 if (addr > TASK_SIZE - len || addr >= TASK_SIZE)
657 if (addr & ~PAGE_MASK)
660 /* Ensure a non-privileged process is not trying to map
663 if (addr < mmap_min_addr && !capable(CAP_SYS_RAWIO))
669 if (file && file->f_op && file->f_op->get_unmapped_area)
670 addr = file->f_op->get_unmapped_area(file, addr, len, pgoff, flags);
672 addr = arch_get_unmapped_area(file, addr, len, pgoff, flags);
674 if (addr < mmap_min_addr && !capable(CAP_SYS_RAWIO))
680 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
681 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
683 struct vm_area_struct *vma = NULL;
686 /* Check the cache first. */
687 /* (Cache hit rate is typically around 35%.) */
688 vma = mm->mmap_cache;
689 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
692 rb_node = mm->mm_rb.rb_node;
696 struct vm_area_struct * vma_tmp;
698 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
700 if (vma_tmp->vm_end > addr) {
702 if (vma_tmp->vm_start <= addr)
704 rb_node = rb_node->rb_left;
706 rb_node = rb_node->rb_right;
709 mm->mmap_cache = vma;
715 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
716 struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
717 struct vm_area_struct **pprev)
720 /* Go through the RB tree quickly. */
721 struct vm_area_struct * vma;
722 rb_node_t * rb_node, * rb_last_right, * rb_prev;
724 rb_node = mm->mm_rb.rb_node;
725 rb_last_right = rb_prev = NULL;
729 struct vm_area_struct * vma_tmp;
731 vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
733 if (vma_tmp->vm_end > addr) {
735 rb_prev = rb_last_right;
736 if (vma_tmp->vm_start <= addr)
738 rb_node = rb_node->rb_left;
740 rb_last_right = rb_node;
741 rb_node = rb_node->rb_right;
745 if (vma->vm_rb.rb_left) {
746 rb_prev = vma->vm_rb.rb_left;
747 while (rb_prev->rb_right)
748 rb_prev = rb_prev->rb_right;
752 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
753 if ((rb_prev ? (*pprev)->vm_next : mm->mmap) != vma)
762 struct vm_area_struct * find_extend_vma(struct mm_struct * mm, unsigned long addr)
764 struct vm_area_struct * vma;
768 vma = find_vma(mm,addr);
771 if (vma->vm_start <= addr)
773 if (!(vma->vm_flags & VM_GROWSDOWN))
775 start = vma->vm_start;
776 if (expand_stack(vma, addr))
778 if (vma->vm_flags & VM_LOCKED) {
779 make_pages_present(addr, start);
784 /* Normal function to fix up a mapping
785 * This function is the default for when an area has no specific
786 * function. This may be used as part of a more specific routine.
787 * This function works out what part of an area is affected and
788 * adjusts the mapping information. Since the actual page
789 * manipulation is done in do_mmap(), none need be done here,
790 * though it would probably be more appropriate.
792 * By the time this function is called, the area struct has been
793 * removed from the process mapping list, so it needs to be
794 * reinserted if necessary.
796 * The 4 main cases are:
797 * Unmapping the whole area
798 * Unmapping from the start of the segment to a point in it
799 * Unmapping from an intermediate point to the end
800 * Unmapping between to intermediate points, making a hole.
802 * Case 4 involves the creation of 2 new areas, for each side of
803 * the hole. If possible, we reuse the existing area rather than
804 * allocate a new one, and the return indicates whether the old
807 static struct vm_area_struct * unmap_fixup(struct mm_struct *mm,
808 struct vm_area_struct *area, unsigned long addr, size_t len,
809 struct vm_area_struct *extra)
811 struct vm_area_struct *mpnt;
812 unsigned long end = addr + len;
814 area->vm_mm->total_vm -= len >> PAGE_SHIFT;
815 if (area->vm_flags & VM_LOCKED)
816 area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
818 /* Unmapping the whole area. */
819 if (addr == area->vm_start && end == area->vm_end) {
820 if (area->vm_ops && area->vm_ops->close)
821 area->vm_ops->close(area);
824 kmem_cache_free(vm_area_cachep, area);
828 /* Work out to one of the ends. */
829 if (end == area->vm_end) {
831 * here area isn't visible to the semaphore-less readers
832 * so we don't need to update it under the spinlock.
835 lock_vma_mappings(area);
836 spin_lock(&mm->page_table_lock);
837 } else if (addr == area->vm_start) {
838 area->vm_pgoff += (end - area->vm_start) >> PAGE_SHIFT;
839 /* same locking considerations of the above case */
840 area->vm_start = end;
841 lock_vma_mappings(area);
842 spin_lock(&mm->page_table_lock);
844 /* Unmapping a hole: area->vm_start < addr <= end < area->vm_end */
845 /* Add end mapping -- leave beginning for below */
849 mpnt->vm_mm = area->vm_mm;
850 mpnt->vm_start = end;
851 mpnt->vm_end = area->vm_end;
852 mpnt->vm_page_prot = area->vm_page_prot;
853 mpnt->vm_flags = area->vm_flags;
855 mpnt->vm_ops = area->vm_ops;
856 mpnt->vm_pgoff = area->vm_pgoff + ((end - area->vm_start) >> PAGE_SHIFT);
857 mpnt->vm_file = area->vm_file;
858 mpnt->vm_private_data = area->vm_private_data;
860 get_file(mpnt->vm_file);
861 if (mpnt->vm_ops && mpnt->vm_ops->open)
862 mpnt->vm_ops->open(mpnt);
863 area->vm_end = addr; /* Truncate area */
865 /* Because mpnt->vm_file == area->vm_file this locks
868 lock_vma_mappings(area);
869 spin_lock(&mm->page_table_lock);
870 __insert_vm_struct(mm, mpnt);
873 __insert_vm_struct(mm, area);
874 spin_unlock(&mm->page_table_lock);
875 unlock_vma_mappings(area);
880 * Try to free as many page directory entries as we can,
881 * without having to work very hard at actually scanning
882 * the page tables themselves.
884 * Right now we try to free page tables if we have a nice
885 * PGDIR-aligned area that got free'd up. We could be more
886 * granular if we want to, but this is fast and simple,
887 * and covers the bad cases.
889 * "prev", if it exists, points to a vma before the one
890 * we just free'd - but there's no telling how much before.
892 static void free_pgtables(struct mm_struct * mm, struct vm_area_struct *prev,
893 unsigned long start, unsigned long end)
895 unsigned long first = start & PGDIR_MASK;
896 unsigned long last = end + PGDIR_SIZE - 1;
897 unsigned long start_index, end_index;
903 if (prev->vm_end > start) {
904 if (last > prev->vm_start)
905 last = prev->vm_start;
910 struct vm_area_struct *next = prev->vm_next;
913 if (next->vm_start < start) {
917 if (last > next->vm_start)
918 last = next->vm_start;
920 if (prev->vm_end > first)
921 first = prev->vm_end + PGDIR_SIZE - 1;
928 * If the PGD bits are not consecutive in the virtual address, the
929 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
931 start_index = pgd_index(first);
932 end_index = pgd_index(last);
933 if (end_index > start_index) {
934 clear_page_tables(mm, start_index, end_index - start_index);
935 flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
939 /* Munmap is split into 2 main parts -- this part which finds
940 * what needs doing, and the areas themselves, which do the
941 * work. This now handles partial unmappings.
942 * Jeremy Fitzhardine <jeremy@sw.oz.au>
944 int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
946 struct vm_area_struct *mpnt, *prev, **npp, *free, *extra;
948 if ((addr & ~PAGE_MASK) || addr >= TASK_SIZE || len > TASK_SIZE-addr)
951 if ((len = PAGE_ALIGN(len)) == 0)
954 /* Check if this memory area is ok - put it on the temporary
955 * list if so.. The checks here are pretty simple --
956 * every area affected in some way (by any overlap) is put
957 * on the list. If nothing is put on, nothing is affected.
959 mpnt = find_vma_prev(mm, addr, &prev);
962 /* we have addr < mpnt->vm_end */
964 if (mpnt->vm_start >= addr+len)
967 /* If we'll make "hole", check the vm areas limit */
968 if ((mpnt->vm_start < addr && mpnt->vm_end > addr+len)
969 && mm->map_count >= max_map_count)
973 * We may need one additional vma to fix up the mappings ...
974 * and this is the last chance for an easy error exit.
976 extra = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
980 npp = (prev ? &prev->vm_next : &mm->mmap);
982 spin_lock(&mm->page_table_lock);
983 for ( ; mpnt && mpnt->vm_start < addr+len; mpnt = *npp) {
984 *npp = mpnt->vm_next;
985 mpnt->vm_next = free;
987 rb_erase(&mpnt->vm_rb, &mm->mm_rb);
989 mm->mmap_cache = NULL; /* Kill the cache. */
990 spin_unlock(&mm->page_table_lock);
992 /* Ok - we have the memory areas we should free on the 'free' list,
993 * so release them, and unmap the page range..
994 * If the one of the segments is only being partially unmapped,
995 * it will put new vm_area_struct(s) into the address space.
996 * In that case we have to be careful with VM_DENYWRITE.
998 while ((mpnt = free) != NULL) {
999 unsigned long st, end, size;
1000 struct file *file = NULL;
1002 free = free->vm_next;
1004 st = addr < mpnt->vm_start ? mpnt->vm_start : addr;
1006 end = end > mpnt->vm_end ? mpnt->vm_end : end;
1009 if (mpnt->vm_flags & VM_DENYWRITE &&
1010 (st != mpnt->vm_start || end != mpnt->vm_end) &&
1011 (file = mpnt->vm_file) != NULL) {
1012 atomic_dec(&file->f_dentry->d_inode->i_writecount);
1014 remove_shared_vm_struct(mpnt);
1017 zap_page_range(mm, st, size);
1020 * Fix the mapping, and free the old area if it wasn't reused.
1022 extra = unmap_fixup(mm, mpnt, st, size, extra);
1024 atomic_inc(&file->f_dentry->d_inode->i_writecount);
1028 /* Release the extra vma struct if it wasn't used */
1030 kmem_cache_free(vm_area_cachep, extra);
1032 free_pgtables(mm, prev, addr, addr+len);
1037 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1040 struct mm_struct *mm = current->mm;
1042 down_write(&mm->mmap_sem);
1043 ret = do_munmap(mm, addr, len);
1044 up_write(&mm->mmap_sem);
1049 static inline void verify_mmap_write_lock_held(struct mm_struct *mm)
1051 if (down_read_trylock(&mm->mmap_sem)) {
1053 up_read(&mm->mmap_sem);
1058 * this is really a simplified "do_mmap". it only handles
1059 * anonymous maps. eventually we may be able to do some
1060 * brk-specific accounting here.
1062 unsigned long do_brk(unsigned long addr, unsigned long len)
1064 struct mm_struct * mm = current->mm;
1065 struct vm_area_struct * vma, * prev;
1066 unsigned long flags;
1067 rb_node_t ** rb_link, * rb_parent;
1069 len = PAGE_ALIGN(len);
1073 if ((addr + len) > TASK_SIZE || (addr + len) < addr)
1076 if (addr < mmap_min_addr && !capable(CAP_SYS_RAWIO))
1082 if (mm->def_flags & VM_LOCKED) {
1083 unsigned long locked = mm->locked_vm << PAGE_SHIFT;
1085 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
1090 * mm->mmap_sem is required to protect against another thread
1091 * changing the mappings while we sleep (on kmalloc for one).
1093 verify_mmap_write_lock_held(mm);
1096 * Clear old maps. this also does some error checking for us
1099 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1100 if (vma && vma->vm_start < addr + len) {
1101 if (do_munmap(mm, addr, len))
1106 /* Check against address space limits *after* clearing old maps... */
1107 if ((mm->total_vm << PAGE_SHIFT) + len
1108 > current->rlim[RLIMIT_AS].rlim_cur)
1111 if (mm->map_count > max_map_count)
1114 if (!vm_enough_memory(len >> PAGE_SHIFT))
1117 flags = VM_DATA_DEFAULT_FLAGS | mm->def_flags;
1119 /* Can we just expand an old anonymous mapping? */
1120 if (rb_parent && vma_merge(mm, prev, rb_parent, addr, addr + len, flags))
1124 * create a vma struct for an anonymous mapping
1126 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
1131 vma->vm_start = addr;
1132 vma->vm_end = addr + len;
1133 vma->vm_flags = flags;
1134 vma->vm_page_prot = protection_map[flags & 0x0f];
1137 vma->vm_file = NULL;
1138 vma->vm_private_data = NULL;
1140 vma_link(mm, vma, prev, rb_link, rb_parent);
1143 mm->total_vm += len >> PAGE_SHIFT;
1144 if (flags & VM_LOCKED) {
1145 mm->locked_vm += len >> PAGE_SHIFT;
1146 make_pages_present(addr, addr + len);
1151 /* Build the RB tree corresponding to the VMA list. */
1152 void build_mmap_rb(struct mm_struct * mm)
1154 struct vm_area_struct * vma;
1155 rb_node_t ** rb_link, * rb_parent;
1157 mm->mm_rb = RB_ROOT;
1158 rb_link = &mm->mm_rb.rb_node;
1160 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1161 __vma_link_rb(mm, vma, rb_link, rb_parent);
1162 rb_parent = &vma->vm_rb;
1163 rb_link = &rb_parent->rb_right;
1167 /* Release all mmaps. */
1168 void exit_mmap(struct mm_struct * mm)
1170 struct vm_area_struct * mpnt;
1172 release_segments(mm);
1173 spin_lock(&mm->page_table_lock);
1175 mm->mmap = mm->mmap_cache = NULL;
1176 mm->mm_rb = RB_ROOT;
1178 spin_unlock(&mm->page_table_lock);
1184 struct vm_area_struct * next = mpnt->vm_next;
1185 unsigned long start = mpnt->vm_start;
1186 unsigned long end = mpnt->vm_end;
1187 unsigned long size = end - start;
1190 if (mpnt->vm_ops->close)
1191 mpnt->vm_ops->close(mpnt);
1194 remove_shared_vm_struct(mpnt);
1195 zap_page_range(mm, start, size);
1197 fput(mpnt->vm_file);
1198 kmem_cache_free(vm_area_cachep, mpnt);
1202 /* This is just debugging */
1206 clear_page_tables(mm, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1211 /* Insert vm structure into process list sorted by address
1212 * and into the inode's i_mmap ring. If vm_file is non-NULL
1213 * then the i_shared_lock must be held here.
1215 void __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1217 struct vm_area_struct * __vma, * prev;
1218 rb_node_t ** rb_link, * rb_parent;
1220 __vma = find_vma_prepare(mm, vma->vm_start, &prev, &rb_link, &rb_parent);
1221 if (__vma && __vma->vm_start < vma->vm_end)
1223 __vma_link(mm, vma, prev, rb_link, rb_parent);
1228 int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
1230 struct vm_area_struct * __vma, * prev;
1231 rb_node_t ** rb_link, * rb_parent;
1233 __vma = find_vma_prepare(mm, vma->vm_start, &prev, &rb_link, &rb_parent);
1234 if (__vma && __vma->vm_start < vma->vm_end)
1236 vma_link(mm, vma, prev, rb_link, rb_parent);