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/init.h>
15 #include <linux/file.h>
17 #include <linux/personality.h>
18 #include <linux/security.h>
19 #include <linux/hugetlb.h>
20 #include <linux/profile.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgalloc.h>
27 * WARNING: the debugging will use recursive algorithms so never enable this
28 * unless you know what you are doing.
32 /* description of effects of mapping type and prot in current implementation.
33 * this is due to the limited x86 page protection hardware. The expected
34 * behavior is in parens:
37 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
38 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
39 * w: (no) no w: (no) no w: (yes) yes w: (no) no
40 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
42 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
43 * w: (no) no w: (no) no w: (copy) copy w: (no) no
44 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
47 pgprot_t protection_map
[16] = {
48 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
49 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
52 int sysctl_overcommit_memory
= 0; /* default is heuristic overcommit */
53 int sysctl_overcommit_ratio
= 50; /* default is 50% */
54 atomic_t vm_committed_space
= ATOMIC_INIT(0);
57 * Check that a process has enough memory to allocate a new virtual
58 * mapping. 1 means there is enough memory for the allocation to
59 * succeed and 0 implies there is not.
61 * We currently support three overcommit policies, which are set via the
62 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-acounting
64 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
65 * Additional code 2002 Jul 20 by Robert Love.
67 extern atomic_t slab_reclaim_pages
;
68 int vm_enough_memory(long pages
)
70 unsigned long free
, allowed
;
72 vm_acct_memory(pages
);
75 * Sometimes we want to use more memory than we have
77 if (sysctl_overcommit_memory
== 1)
80 if (sysctl_overcommit_memory
== 0) {
81 free
= get_page_cache_size();
82 free
+= nr_free_pages();
83 free
+= nr_swap_pages
;
86 * Any slabs which are created with the
87 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
88 * which are reclaimable, under pressure. The dentry
89 * cache and most inode caches should fall into this
91 free
+= atomic_read(&slab_reclaim_pages
);
94 * Leave the last 3% for root
96 if (!capable(CAP_SYS_ADMIN
))
101 vm_unacct_memory(pages
);
105 allowed
= totalram_pages
* sysctl_overcommit_ratio
/ 100;
106 allowed
+= total_swap_pages
;
108 if (atomic_read(&vm_committed_space
) < allowed
)
111 vm_unacct_memory(pages
);
117 * Requires inode->i_mapping->i_shared_sem
120 __remove_shared_vm_struct(struct vm_area_struct
*vma
, struct inode
*inode
)
123 if (vma
->vm_flags
& VM_DENYWRITE
)
124 atomic_inc(&inode
->i_writecount
);
125 list_del_init(&vma
->shared
);
130 * Remove one vm structure from the inode's i_mapping address space.
132 static void remove_shared_vm_struct(struct vm_area_struct
*vma
)
134 struct file
*file
= vma
->vm_file
;
137 struct inode
*inode
= file
->f_dentry
->d_inode
;
139 down(&inode
->i_mapping
->i_shared_sem
);
140 __remove_shared_vm_struct(vma
, inode
);
141 up(&inode
->i_mapping
->i_shared_sem
);
146 * sys_brk() for the most part doesn't need the global kernel
147 * lock, except when an application is doing something nasty
148 * like trying to un-brk an area that has already been mapped
149 * to a regular file. in this case, the unmapping will need
150 * to invoke file system routines that need the global lock.
152 asmlinkage
unsigned long sys_brk(unsigned long brk
)
154 unsigned long rlim
, retval
;
155 unsigned long newbrk
, oldbrk
;
156 struct mm_struct
*mm
= current
->mm
;
158 down_write(&mm
->mmap_sem
);
160 if (brk
< mm
->end_code
)
162 newbrk
= PAGE_ALIGN(brk
);
163 oldbrk
= PAGE_ALIGN(mm
->brk
);
164 if (oldbrk
== newbrk
)
167 /* Always allow shrinking brk. */
168 if (brk
<= mm
->brk
) {
169 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
174 /* Check against rlimit.. */
175 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
176 if (rlim
< RLIM_INFINITY
&& brk
- mm
->start_data
> rlim
)
179 /* Check against existing mmap mappings. */
180 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
183 /* Ok, looks good - let it rip. */
184 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
190 up_write(&mm
->mmap_sem
);
194 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
195 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
198 static inline unsigned long
199 calc_vm_flags(unsigned long prot
, unsigned long flags
)
201 #define _trans(x,bit1,bit2) \
202 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
204 unsigned long prot_bits
, flag_bits
;
206 _trans(prot
, PROT_READ
, VM_READ
) |
207 _trans(prot
, PROT_WRITE
, VM_WRITE
) |
208 _trans(prot
, PROT_EXEC
, VM_EXEC
);
210 _trans(flags
, MAP_GROWSDOWN
, VM_GROWSDOWN
) |
211 _trans(flags
, MAP_DENYWRITE
, VM_DENYWRITE
) |
212 _trans(flags
, MAP_EXECUTABLE
, VM_EXECUTABLE
);
213 return prot_bits
| flag_bits
;
218 static int browse_rb(struct rb_node
* rb_node
) {
222 i
+= browse_rb(rb_node
->rb_left
);
223 i
+= browse_rb(rb_node
->rb_right
);
228 static void validate_mm(struct mm_struct
* mm
) {
231 struct vm_area_struct
* tmp
= mm
->mmap
;
236 if (i
!= mm
->map_count
)
237 printk("map_count %d vm_next %d\n", mm
->map_count
, i
), bug
= 1;
238 i
= browse_rb(mm
->mm_rb
.rb_node
);
239 if (i
!= mm
->map_count
)
240 printk("map_count %d rb %d\n", mm
->map_count
, i
), bug
= 1;
245 #define validate_mm(mm) do { } while (0)
248 static struct vm_area_struct
*
249 find_vma_prepare(struct mm_struct
*mm
, unsigned long addr
,
250 struct vm_area_struct
**pprev
, struct rb_node
***rb_link
,
251 struct rb_node
** rb_parent
)
253 struct vm_area_struct
* vma
;
254 struct rb_node
** __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
;
286 __vma_link_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
287 struct vm_area_struct
*prev
, struct rb_node
*rb_parent
)
290 vma
->vm_next
= prev
->vm_next
;
295 vma
->vm_next
= rb_entry(rb_parent
,
296 struct vm_area_struct
, vm_rb
);
302 static void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
303 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
305 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
306 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
309 static inline void __vma_link_file(struct vm_area_struct
*vma
)
315 struct inode
* inode
= file
->f_dentry
->d_inode
;
316 struct address_space
*mapping
= inode
->i_mapping
;
318 if (vma
->vm_flags
& VM_DENYWRITE
)
319 atomic_dec(&inode
->i_writecount
);
321 if (vma
->vm_flags
& VM_SHARED
)
322 list_add_tail(&vma
->shared
, &mapping
->i_mmap_shared
);
324 list_add_tail(&vma
->shared
, &mapping
->i_mmap
);
329 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
330 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
331 struct rb_node
*rb_parent
)
333 __vma_link_list(mm
, vma
, prev
, rb_parent
);
334 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
335 __vma_link_file(vma
);
338 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
339 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
340 struct rb_node
*rb_parent
)
342 struct address_space
*mapping
= NULL
;
345 mapping
= vma
->vm_file
->f_dentry
->d_inode
->i_mapping
;
348 down(&mapping
->i_shared_sem
);
349 spin_lock(&mm
->page_table_lock
);
350 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
351 spin_unlock(&mm
->page_table_lock
);
353 up(&mapping
->i_shared_sem
);
355 mark_mm_hugetlb(mm
, vma
);
361 * If the vma has a ->close operation then the driver probably needs to release
362 * per-vma resources, so we don't attempt to merge those.
364 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
366 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
367 struct file
*file
, unsigned long vm_flags
)
369 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
371 if (vma
->vm_file
!= file
)
373 if (vma
->vm_flags
!= vm_flags
)
375 if (vma
->vm_private_data
)
381 * Return true if we can merge this (vm_flags,file,vm_pgoff,size)
382 * in front of (at a lower virtual address and file offset than) the vma.
384 * We don't check here for the merged mmap wrapping around the end of pagecache
385 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
386 * wrap, nor mmaps which cover the final page at index -1UL.
389 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
390 struct file
*file
, unsigned long vm_pgoff
, unsigned long size
)
392 if (is_mergeable_vma(vma
, file
, vm_flags
)) {
394 return 1; /* anon mapping */
395 if (vma
->vm_pgoff
== vm_pgoff
+ size
)
402 * Return true if we can merge this (vm_flags,file,vm_pgoff)
403 * beyond (at a higher virtual address and file offset than) the vma.
406 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
407 struct file
*file
, unsigned long vm_pgoff
)
409 if (is_mergeable_vma(vma
, file
, vm_flags
)) {
410 unsigned long vma_size
;
413 return 1; /* anon mapping */
415 vma_size
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
416 if (vma
->vm_pgoff
+ vma_size
== vm_pgoff
)
423 * Given a new mapping request (addr,end,vm_flags,file,pgoff), figure out
424 * whether that can be merged with its predecessor or its successor. Or
425 * both (it neatly fills a hole).
427 static int vma_merge(struct mm_struct
*mm
, struct vm_area_struct
*prev
,
428 struct rb_node
*rb_parent
, unsigned long addr
,
429 unsigned long end
, unsigned long vm_flags
,
430 struct file
*file
, unsigned long pgoff
)
432 spinlock_t
* lock
= &mm
->page_table_lock
;
435 * We later require that vma->vm_flags == vm_flags, so this tests
436 * vma->vm_flags & VM_SPECIAL, too.
438 if (vm_flags
& VM_SPECIAL
)
442 prev
= rb_entry(rb_parent
, struct vm_area_struct
, vm_rb
);
447 * Can it merge with the predecessor?
449 if (prev
->vm_end
== addr
&&
450 is_mergeable_vma(prev
, file
, vm_flags
) &&
451 can_vma_merge_after(prev
, vm_flags
, file
, pgoff
)) {
452 struct vm_area_struct
*next
;
453 struct inode
*inode
= file
? file
->f_dentry
->d_inode
: NULL
;
456 if (unlikely(file
&& prev
->vm_next
&&
457 prev
->vm_next
->vm_file
== file
)) {
458 down(&inode
->i_mapping
->i_shared_sem
);
465 * OK, it did. Can we now merge in the successor as well?
467 next
= prev
->vm_next
;
468 if (next
&& prev
->vm_end
== next
->vm_start
&&
469 can_vma_merge_before(next
, vm_flags
, file
,
470 pgoff
, (end
- addr
) >> PAGE_SHIFT
)) {
471 prev
->vm_end
= next
->vm_end
;
472 __vma_unlink(mm
, next
, prev
);
473 __remove_shared_vm_struct(next
, inode
);
476 up(&inode
->i_mapping
->i_shared_sem
);
481 kmem_cache_free(vm_area_cachep
, next
);
486 up(&inode
->i_mapping
->i_shared_sem
);
491 * Can this new request be merged in front of prev->vm_next?
493 prev
= prev
->vm_next
;
496 if (!can_vma_merge_before(prev
, vm_flags
, file
,
497 pgoff
, (end
- addr
) >> PAGE_SHIFT
))
499 if (end
== prev
->vm_start
) {
501 prev
->vm_start
= addr
;
502 prev
->vm_pgoff
-= (end
- addr
) >> PAGE_SHIFT
;
512 * The caller must hold down_write(current->mm->mmap_sem).
515 unsigned long do_mmap_pgoff(struct file
* file
, unsigned long addr
,
516 unsigned long len
, unsigned long prot
,
517 unsigned long flags
, unsigned long pgoff
)
519 struct mm_struct
* mm
= current
->mm
;
520 struct vm_area_struct
* vma
, * prev
;
522 unsigned int vm_flags
;
523 int correct_wcount
= 0;
525 struct rb_node
** rb_link
, * rb_parent
;
526 unsigned long charged
= 0;
528 if (file
&& (!file
->f_op
|| !file
->f_op
->mmap
))
537 len
= PAGE_ALIGN(len
);
539 /* offset overflow? */
540 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
543 /* Too many mappings? */
544 if (mm
->map_count
> MAX_MAP_COUNT
)
547 /* Obtain the address to map to. we verify (or select) it and ensure
548 * that it represents a valid section of the address space.
550 addr
= get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
551 if (addr
& ~PAGE_MASK
)
554 /* Do simple checking here so the lower-level routines won't have
555 * to. we assume access permissions have been handled by the open
556 * of the memory object, so we don't do any here.
558 vm_flags
= calc_vm_flags(prot
,flags
) | mm
->def_flags
|
559 VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
561 if (flags
& MAP_LOCKED
) {
562 if (!capable(CAP_IPC_LOCK
))
564 vm_flags
|= VM_LOCKED
;
566 /* mlock MCL_FUTURE? */
567 if (vm_flags
& VM_LOCKED
) {
568 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
570 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
574 inode
= file
? file
->f_dentry
->d_inode
: NULL
;
577 switch (flags
& MAP_TYPE
) {
579 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
583 * Make sure we don't allow writing to an append-only
586 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
590 * Make sure there are no mandatory locks on the file.
592 if (locks_verify_locked(inode
))
595 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
596 if (!(file
->f_mode
& FMODE_WRITE
))
597 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
601 if (!(file
->f_mode
& FMODE_READ
))
609 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
610 switch (flags
& MAP_TYPE
) {
614 vm_flags
&= ~(VM_SHARED
| VM_MAYSHARE
);
621 error
= security_file_mmap(file
, prot
, flags
);
628 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
629 if (vma
&& vma
->vm_start
< addr
+ len
) {
630 if (do_munmap(mm
, addr
, len
))
635 /* Check against address space limit. */
636 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
637 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
640 if (!(flags
& MAP_NORESERVE
) || sysctl_overcommit_memory
> 1) {
641 if (vm_flags
& VM_SHARED
) {
642 /* Check memory availability in shmem_file_setup? */
643 vm_flags
|= VM_ACCOUNT
;
644 } else if (vm_flags
& VM_WRITE
) {
646 * Private writable mapping: check memory availability
648 charged
= len
>> PAGE_SHIFT
;
649 if (!vm_enough_memory(charged
))
651 vm_flags
|= VM_ACCOUNT
;
655 /* Can we just expand an old anonymous mapping? */
656 if (!file
&& !(vm_flags
& VM_SHARED
) && rb_parent
)
657 if (vma_merge(mm
, prev
, rb_parent
, addr
, addr
+ len
,
662 * Determine the object being mapped and call the appropriate
663 * specific mapper. the address has already been validated, but
664 * not unmapped, but the maps are removed from the list.
666 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
672 vma
->vm_start
= addr
;
673 vma
->vm_end
= addr
+ len
;
674 vma
->vm_flags
= vm_flags
;
675 vma
->vm_page_prot
= protection_map
[vm_flags
& 0x0f];
677 vma
->vm_pgoff
= pgoff
;
679 vma
->vm_private_data
= NULL
;
681 INIT_LIST_HEAD(&vma
->shared
);
685 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
687 if (vm_flags
& VM_DENYWRITE
) {
688 error
= deny_write_access(file
);
695 error
= file
->f_op
->mmap(file
, vma
);
697 goto unmap_and_free_vma
;
698 } else if (vm_flags
& VM_SHARED
) {
699 error
= shmem_zero_setup(vma
);
704 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
705 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
706 * that memory reservation must be checked; but that reservation
707 * belongs to shared memory object, not to vma: so now clear it.
709 if ((vm_flags
& (VM_SHARED
|VM_ACCOUNT
)) == (VM_SHARED
|VM_ACCOUNT
))
710 vma
->vm_flags
&= ~VM_ACCOUNT
;
712 /* Can addr have changed??
714 * Answer: Yes, several device drivers can do it in their
715 * f_op->mmap method. -DaveM
717 addr
= vma
->vm_start
;
719 if (!file
|| !rb_parent
|| !vma_merge(mm
, prev
, rb_parent
, addr
,
720 addr
+ len
, vma
->vm_flags
, file
, pgoff
)) {
721 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
723 atomic_inc(&inode
->i_writecount
);
727 atomic_inc(&inode
->i_writecount
);
730 kmem_cache_free(vm_area_cachep
, vma
);
733 mm
->total_vm
+= len
>> PAGE_SHIFT
;
734 if (vm_flags
& VM_LOCKED
) {
735 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
736 make_pages_present(addr
, addr
+ len
);
738 if (flags
& MAP_POPULATE
) {
739 up_write(&mm
->mmap_sem
);
740 sys_remap_file_pages(addr
, len
, prot
,
741 pgoff
, flags
& MAP_NONBLOCK
);
742 down_write(&mm
->mmap_sem
);
748 atomic_inc(&inode
->i_writecount
);
752 /* Undo any partial mapping done by a device driver. */
753 zap_page_range(vma
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
755 kmem_cache_free(vm_area_cachep
, vma
);
758 vm_unacct_memory(charged
);
762 /* Get an address range which is currently unmapped.
763 * For shmat() with addr=0.
765 * Ugly calling convention alert:
766 * Return value with the low bits set means error value,
768 * if (ret & ~PAGE_MASK)
771 * This function "knows" that -ENOMEM has the bits set.
773 #ifndef HAVE_ARCH_UNMAPPED_AREA
774 static inline unsigned long
775 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
776 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
778 struct mm_struct
*mm
= current
->mm
;
779 struct vm_area_struct
*vma
;
780 unsigned long start_addr
;
786 addr
= PAGE_ALIGN(addr
);
787 vma
= find_vma(mm
, addr
);
788 if (TASK_SIZE
- len
>= addr
&&
789 (!vma
|| addr
+ len
<= vma
->vm_start
))
792 start_addr
= addr
= mm
->free_area_cache
;
795 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
796 /* At this point: (!vma || addr < vma->vm_end). */
797 if (TASK_SIZE
- len
< addr
) {
799 * Start a new search - just in case we missed
802 if (start_addr
!= TASK_UNMAPPED_BASE
) {
803 start_addr
= addr
= TASK_UNMAPPED_BASE
;
808 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
810 * Remember the place where we stopped the search:
812 mm
->free_area_cache
= addr
+ len
;
820 arch_get_unmapped_area(struct file
*, unsigned long, unsigned long,
821 unsigned long, unsigned long);
825 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
826 unsigned long pgoff
, unsigned long flags
)
828 if (flags
& MAP_FIXED
) {
831 if (addr
> TASK_SIZE
- len
)
833 if (addr
& ~PAGE_MASK
)
835 if (file
&& is_file_hugepages(file
)) {
837 * Make sure that addr and length are properly aligned.
839 ret
= is_aligned_hugepage_range(addr
, len
);
842 * Ensure that a normal request is not falling in a
843 * reserved hugepage range. For some archs like IA-64,
844 * there is a separate region for hugepages.
846 ret
= is_hugepage_only_range(addr
, len
);
853 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
854 return file
->f_op
->get_unmapped_area(file
, addr
, len
,
857 return arch_get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
860 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
861 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
863 struct vm_area_struct
*vma
= NULL
;
866 /* Check the cache first. */
867 /* (Cache hit rate is typically around 35%.) */
868 vma
= mm
->mmap_cache
;
869 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
870 struct rb_node
* rb_node
;
872 rb_node
= mm
->mm_rb
.rb_node
;
876 struct vm_area_struct
* vma_tmp
;
878 vma_tmp
= rb_entry(rb_node
,
879 struct vm_area_struct
, vm_rb
);
881 if (vma_tmp
->vm_end
> addr
) {
883 if (vma_tmp
->vm_start
<= addr
)
885 rb_node
= rb_node
->rb_left
;
887 rb_node
= rb_node
->rb_right
;
890 mm
->mmap_cache
= vma
;
896 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
897 struct vm_area_struct
*
898 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
899 struct vm_area_struct
**pprev
)
901 struct vm_area_struct
*vma
= NULL
, *prev
= NULL
;
902 struct rb_node
* rb_node
;
906 /* Guard against addr being lower than the first VMA */
909 /* Go through the RB tree quickly. */
910 rb_node
= mm
->mm_rb
.rb_node
;
913 struct vm_area_struct
*vma_tmp
;
914 vma_tmp
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
916 if (addr
< vma_tmp
->vm_end
) {
917 rb_node
= rb_node
->rb_left
;
920 if (!prev
->vm_next
|| (addr
< prev
->vm_next
->vm_end
))
922 rb_node
= rb_node
->rb_right
;
928 return prev
? prev
->vm_next
: vma
;
931 #ifdef CONFIG_STACK_GROWSUP
933 * vma is the first one with address > vma->vm_end. Have to extend vma.
935 int expand_stack(struct vm_area_struct
* vma
, unsigned long address
)
939 if (!(vma
->vm_flags
& VM_GROWSUP
))
943 * vma->vm_start/vm_end cannot change under us because the caller
944 * is required to hold the mmap_sem in read mode. We need to get
945 * the spinlock only before relocating the vma range ourself.
947 address
+= 4 + PAGE_SIZE
- 1;
948 address
&= PAGE_MASK
;
949 spin_lock(&vma
->vm_mm
->page_table_lock
);
950 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
953 if (!vm_enough_memory(grow
)) {
954 spin_unlock(&vma
->vm_mm
->page_table_lock
);
958 if (address
- vma
->vm_start
> current
->rlim
[RLIMIT_STACK
].rlim_cur
||
959 ((vma
->vm_mm
->total_vm
+ grow
) << PAGE_SHIFT
) >
960 current
->rlim
[RLIMIT_AS
].rlim_cur
) {
961 spin_unlock(&vma
->vm_mm
->page_table_lock
);
962 vm_unacct_memory(grow
);
965 vma
->vm_end
= address
;
966 vma
->vm_mm
->total_vm
+= grow
;
967 if (vma
->vm_flags
& VM_LOCKED
)
968 vma
->vm_mm
->locked_vm
+= grow
;
969 spin_unlock(&vma
->vm_mm
->page_table_lock
);
973 struct vm_area_struct
*
974 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
976 struct vm_area_struct
*vma
, *prev
;
979 vma
= find_vma_prev(mm
, addr
, &prev
);
980 if (vma
&& (vma
->vm_start
<= addr
))
982 if (!prev
|| expand_stack(prev
, addr
))
984 if (prev
->vm_flags
& VM_LOCKED
) {
985 make_pages_present(addr
, prev
->vm_end
);
991 * vma is the first one with address < vma->vm_start. Have to extend vma.
993 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
998 * vma->vm_start/vm_end cannot change under us because the caller
999 * is required to hold the mmap_sem in read mode. We need to get
1000 * the spinlock only before relocating the vma range ourself.
1002 address
&= PAGE_MASK
;
1003 spin_lock(&vma
->vm_mm
->page_table_lock
);
1004 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
1007 if (!vm_enough_memory(grow
)) {
1008 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1012 if (vma
->vm_end
- address
> current
->rlim
[RLIMIT_STACK
].rlim_cur
||
1013 ((vma
->vm_mm
->total_vm
+ grow
) << PAGE_SHIFT
) >
1014 current
->rlim
[RLIMIT_AS
].rlim_cur
) {
1015 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1016 vm_unacct_memory(grow
);
1019 vma
->vm_start
= address
;
1020 vma
->vm_pgoff
-= grow
;
1021 vma
->vm_mm
->total_vm
+= grow
;
1022 if (vma
->vm_flags
& VM_LOCKED
)
1023 vma
->vm_mm
->locked_vm
+= grow
;
1024 spin_unlock(&vma
->vm_mm
->page_table_lock
);
1028 struct vm_area_struct
*
1029 find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
1031 struct vm_area_struct
* vma
;
1032 unsigned long start
;
1035 vma
= find_vma(mm
,addr
);
1038 if (vma
->vm_start
<= addr
)
1040 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
1042 start
= vma
->vm_start
;
1043 if (expand_stack(vma
, addr
))
1045 if (vma
->vm_flags
& VM_LOCKED
) {
1046 make_pages_present(addr
, start
);
1053 * Try to free as many page directory entries as we can,
1054 * without having to work very hard at actually scanning
1055 * the page tables themselves.
1057 * Right now we try to free page tables if we have a nice
1058 * PGDIR-aligned area that got free'd up. We could be more
1059 * granular if we want to, but this is fast and simple,
1060 * and covers the bad cases.
1062 * "prev", if it exists, points to a vma before the one
1063 * we just free'd - but there's no telling how much before.
1065 static void free_pgtables(struct mmu_gather
*tlb
, struct vm_area_struct
*prev
,
1066 unsigned long start
, unsigned long end
)
1068 unsigned long first
= start
& PGDIR_MASK
;
1069 unsigned long last
= end
+ PGDIR_SIZE
- 1;
1070 unsigned long start_index
, end_index
;
1071 struct mm_struct
*mm
= tlb
->mm
;
1077 if (prev
->vm_end
> start
) {
1078 if (last
> prev
->vm_start
)
1079 last
= prev
->vm_start
;
1084 struct vm_area_struct
*next
= prev
->vm_next
;
1087 if (next
->vm_start
< start
) {
1091 if (last
> next
->vm_start
)
1092 last
= next
->vm_start
;
1094 if (prev
->vm_end
> first
)
1095 first
= prev
->vm_end
+ PGDIR_SIZE
- 1;
1099 if (last
< first
) /* for arches with discontiguous pgd indices */
1102 * If the PGD bits are not consecutive in the virtual address, the
1103 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1105 start_index
= pgd_index(first
);
1106 if (start_index
< FIRST_USER_PGD_NR
)
1107 start_index
= FIRST_USER_PGD_NR
;
1108 end_index
= pgd_index(last
);
1109 if (end_index
> start_index
) {
1110 clear_page_tables(tlb
, start_index
, end_index
- start_index
);
1111 flush_tlb_pgtables(mm
, first
& PGDIR_MASK
, last
& PGDIR_MASK
);
1115 /* Normal function to fix up a mapping
1116 * This function is the default for when an area has no specific
1117 * function. This may be used as part of a more specific routine.
1119 * By the time this function is called, the area struct has been
1120 * removed from the process mapping list.
1122 static void unmap_vma(struct mm_struct
*mm
, struct vm_area_struct
*area
)
1124 size_t len
= area
->vm_end
- area
->vm_start
;
1126 area
->vm_mm
->total_vm
-= len
>> PAGE_SHIFT
;
1127 if (area
->vm_flags
& VM_LOCKED
)
1128 area
->vm_mm
->locked_vm
-= len
>> PAGE_SHIFT
;
1130 * Is this a new hole at the lowest possible address?
1132 if (area
->vm_start
>= TASK_UNMAPPED_BASE
&&
1133 area
->vm_start
< area
->vm_mm
->free_area_cache
)
1134 area
->vm_mm
->free_area_cache
= area
->vm_start
;
1136 remove_shared_vm_struct(area
);
1138 if (area
->vm_ops
&& area
->vm_ops
->close
)
1139 area
->vm_ops
->close(area
);
1141 fput(area
->vm_file
);
1142 kmem_cache_free(vm_area_cachep
, area
);
1146 * Update the VMA and inode share lists.
1148 * Ok - we have the memory areas we should free on the 'free' list,
1149 * so release them, and do the vma updates.
1151 static void unmap_vma_list(struct mm_struct
*mm
,
1152 struct vm_area_struct
*mpnt
)
1155 struct vm_area_struct
*next
= mpnt
->vm_next
;
1156 unmap_vma(mm
, mpnt
);
1158 } while (mpnt
!= NULL
);
1163 * Get rid of page table information in the indicated region.
1165 * Called with the page table lock held.
1167 static void unmap_region(struct mm_struct
*mm
,
1168 struct vm_area_struct
*vma
,
1169 struct vm_area_struct
*prev
,
1170 unsigned long start
,
1173 struct mmu_gather
*tlb
;
1174 unsigned long nr_accounted
= 0;
1177 tlb
= tlb_gather_mmu(mm
, 0);
1178 unmap_vmas(&tlb
, mm
, vma
, start
, end
, &nr_accounted
);
1179 vm_unacct_memory(nr_accounted
);
1180 free_pgtables(tlb
, prev
, start
, end
);
1181 tlb_finish_mmu(tlb
, start
, end
);
1185 * Create a list of vma's touched by the unmap, removing them from the mm's
1186 * vma list as we go..
1188 * Called with the page_table_lock held.
1191 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1192 struct vm_area_struct
*prev
, unsigned long end
)
1194 struct vm_area_struct
**insertion_point
;
1195 struct vm_area_struct
*tail_vma
= NULL
;
1197 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
1199 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
1203 } while (vma
&& vma
->vm_start
< end
);
1204 *insertion_point
= vma
;
1205 tail_vma
->vm_next
= NULL
;
1206 mm
->mmap_cache
= NULL
; /* Kill the cache. */
1210 * Split a vma into two pieces at address 'addr', a new vma is allocated
1211 * either for the first part or the the tail.
1213 int split_vma(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
1214 unsigned long addr
, int new_below
)
1216 struct vm_area_struct
*new;
1218 if (mm
->map_count
>= MAX_MAP_COUNT
)
1221 new = kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1225 /* most fields are the same, copy all, and then fixup */
1228 INIT_LIST_HEAD(&new->shared
);
1232 vma
->vm_start
= addr
;
1233 vma
->vm_pgoff
+= ((addr
- new->vm_start
) >> PAGE_SHIFT
);
1236 new->vm_start
= addr
;
1237 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
1241 get_file(new->vm_file
);
1243 if (new->vm_ops
&& new->vm_ops
->open
)
1244 new->vm_ops
->open(new);
1246 insert_vm_struct(mm
, new);
1250 /* Munmap is split into 2 main parts -- this part which finds
1251 * what needs doing, and the areas themselves, which do the
1252 * work. This now handles partial unmappings.
1253 * Jeremy Fitzhardinge <jeremy@goop.org>
1255 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1258 struct vm_area_struct
*mpnt
, *prev
, *last
;
1260 if ((start
& ~PAGE_MASK
) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
1263 if ((len
= PAGE_ALIGN(len
)) == 0)
1266 /* Find the first overlapping VMA */
1267 mpnt
= find_vma_prev(mm
, start
, &prev
);
1270 /* we have start < mpnt->vm_end */
1272 if (is_vm_hugetlb_page(mpnt
)) {
1273 int ret
= is_aligned_hugepage_range(start
, len
);
1279 /* if it doesn't overlap, we have nothing.. */
1281 if (mpnt
->vm_start
>= end
)
1284 /* Something will probably happen, so notify. */
1285 if (mpnt
->vm_file
&& (mpnt
->vm_flags
& VM_EXEC
))
1286 profile_exec_unmap(mm
);
1289 * If we need to split any vma, do it now to save pain later.
1291 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1292 * unmapped vm_area_struct will remain in use: so lower split_vma
1293 * places tmp vma above, and higher split_vma places tmp vma below.
1295 if (start
> mpnt
->vm_start
) {
1296 if (split_vma(mm
, mpnt
, start
, 0))
1301 /* Does it split the last one? */
1302 last
= find_vma(mm
, end
);
1303 if (last
&& end
> last
->vm_start
) {
1304 if (split_vma(mm
, last
, end
, 1))
1307 mpnt
= prev
? prev
->vm_next
: mm
->mmap
;
1310 * Remove the vma's, and unmap the actual pages
1312 spin_lock(&mm
->page_table_lock
);
1313 detach_vmas_to_be_unmapped(mm
, mpnt
, prev
, end
);
1314 unmap_region(mm
, mpnt
, prev
, start
, end
);
1315 spin_unlock(&mm
->page_table_lock
);
1317 /* Fix up all other VM information */
1318 unmap_vma_list(mm
, mpnt
);
1323 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
1326 struct mm_struct
*mm
= current
->mm
;
1328 down_write(&mm
->mmap_sem
);
1329 ret
= do_munmap(mm
, addr
, len
);
1330 up_write(&mm
->mmap_sem
);
1335 * this is really a simplified "do_mmap". it only handles
1336 * anonymous maps. eventually we may be able to do some
1337 * brk-specific accounting here.
1339 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1341 struct mm_struct
* mm
= current
->mm
;
1342 struct vm_area_struct
* vma
, * prev
;
1343 unsigned long flags
;
1344 struct rb_node
** rb_link
, * rb_parent
;
1346 len
= PAGE_ALIGN(len
);
1353 if (mm
->def_flags
& VM_LOCKED
) {
1354 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
1356 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
1361 * Clear old maps. this also does some error checking for us
1364 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
1365 if (vma
&& vma
->vm_start
< addr
+ len
) {
1366 if (do_munmap(mm
, addr
, len
))
1371 /* Check against address space limits *after* clearing old maps... */
1372 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
1373 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
1376 if (mm
->map_count
> MAX_MAP_COUNT
)
1379 if (!vm_enough_memory(len
>> PAGE_SHIFT
))
1382 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
1384 /* Can we just expand an old anonymous mapping? */
1385 if (rb_parent
&& vma_merge(mm
, prev
, rb_parent
, addr
, addr
+ len
,
1390 * create a vma struct for an anonymous mapping
1392 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1394 vm_unacct_memory(len
>> PAGE_SHIFT
);
1399 vma
->vm_start
= addr
;
1400 vma
->vm_end
= addr
+ len
;
1401 vma
->vm_flags
= flags
;
1402 vma
->vm_page_prot
= protection_map
[flags
& 0x0f];
1405 vma
->vm_file
= NULL
;
1406 vma
->vm_private_data
= NULL
;
1407 INIT_LIST_HEAD(&vma
->shared
);
1409 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1412 mm
->total_vm
+= len
>> PAGE_SHIFT
;
1413 if (flags
& VM_LOCKED
) {
1414 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
1415 make_pages_present(addr
, addr
+ len
);
1420 /* Build the RB tree corresponding to the VMA list. */
1421 void build_mmap_rb(struct mm_struct
* mm
)
1423 struct vm_area_struct
* vma
;
1424 struct rb_node
** rb_link
, * rb_parent
;
1426 mm
->mm_rb
= RB_ROOT
;
1427 rb_link
= &mm
->mm_rb
.rb_node
;
1429 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1430 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
1431 rb_parent
= &vma
->vm_rb
;
1432 rb_link
= &rb_parent
->rb_right
;
1436 /* Release all mmaps. */
1437 void exit_mmap(struct mm_struct
*mm
)
1439 struct mmu_gather
*tlb
;
1440 struct vm_area_struct
*vma
;
1441 unsigned long nr_accounted
= 0;
1443 profile_exit_mmap(mm
);
1447 spin_lock(&mm
->page_table_lock
);
1449 tlb
= tlb_gather_mmu(mm
, 1);
1451 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1452 mm
->map_count
-= unmap_vmas(&tlb
, mm
, mm
->mmap
, 0,
1453 ~0UL, &nr_accounted
);
1454 vm_unacct_memory(nr_accounted
);
1455 BUG_ON(mm
->map_count
); /* This is just debugging */
1456 clear_page_tables(tlb
, FIRST_USER_PGD_NR
, USER_PTRS_PER_PGD
);
1457 tlb_finish_mmu(tlb
, 0, MM_VM_SIZE(mm
));
1460 mm
->mmap
= mm
->mmap_cache
= NULL
;
1461 mm
->mm_rb
= RB_ROOT
;
1466 spin_unlock(&mm
->page_table_lock
);
1469 * Walk the list again, actually closing and freeing it
1470 * without holding any MM locks.
1473 struct vm_area_struct
*next
= vma
->vm_next
;
1474 remove_shared_vm_struct(vma
);
1476 if (vma
->vm_ops
->close
)
1477 vma
->vm_ops
->close(vma
);
1481 kmem_cache_free(vm_area_cachep
, vma
);
1486 /* Insert vm structure into process list sorted by address
1487 * and into the inode's i_mmap ring. If vm_file is non-NULL
1488 * then i_shared_sem is taken here.
1490 void insert_vm_struct(struct mm_struct
* mm
, struct vm_area_struct
* vma
)
1492 struct vm_area_struct
* __vma
, * prev
;
1493 struct rb_node
** rb_link
, * rb_parent
;
1495 __vma
= find_vma_prepare(mm
,vma
->vm_start
,&prev
,&rb_link
,&rb_parent
);
1496 if (__vma
&& __vma
->vm_start
< vma
->vm_end
)
1498 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);