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>
21 #include <linux/module.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgalloc.h>
28 * WARNING: the debugging will use recursive algorithms so never enable this
29 * unless you know what you are doing.
33 /* description of effects of mapping type and prot in current implementation.
34 * this is due to the limited x86 page protection hardware. The expected
35 * behavior is in parens:
38 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
39 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
40 * w: (no) no w: (no) no w: (yes) yes w: (no) no
41 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
43 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
44 * w: (no) no w: (no) no w: (copy) copy w: (no) no
45 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
48 pgprot_t protection_map
[16] = {
49 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
50 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
53 int sysctl_overcommit_memory
= 0; /* default is heuristic overcommit */
54 int sysctl_overcommit_ratio
= 50; /* default is 50% */
55 atomic_t vm_committed_space
= ATOMIC_INIT(0);
57 EXPORT_SYMBOL(sysctl_overcommit_memory
);
58 EXPORT_SYMBOL(sysctl_overcommit_ratio
);
59 EXPORT_SYMBOL(vm_committed_space
);
62 * Requires inode->i_mapping->i_shared_sem
65 __remove_shared_vm_struct(struct vm_area_struct
*vma
, struct inode
*inode
)
68 if (vma
->vm_flags
& VM_DENYWRITE
)
69 atomic_inc(&inode
->i_writecount
);
70 list_del_init(&vma
->shared
);
75 * Remove one vm structure from the inode's i_mapping address space.
77 static void remove_shared_vm_struct(struct vm_area_struct
*vma
)
79 struct file
*file
= vma
->vm_file
;
82 struct inode
*inode
= file
->f_dentry
->d_inode
;
84 down(&inode
->i_mapping
->i_shared_sem
);
85 __remove_shared_vm_struct(vma
, inode
);
86 up(&inode
->i_mapping
->i_shared_sem
);
91 * sys_brk() for the most part doesn't need the global kernel
92 * lock, except when an application is doing something nasty
93 * like trying to un-brk an area that has already been mapped
94 * to a regular file. in this case, the unmapping will need
95 * to invoke file system routines that need the global lock.
97 asmlinkage
unsigned long sys_brk(unsigned long brk
)
99 unsigned long rlim
, retval
;
100 unsigned long newbrk
, oldbrk
;
101 struct mm_struct
*mm
= current
->mm
;
103 down_write(&mm
->mmap_sem
);
105 if (brk
< mm
->end_code
)
107 newbrk
= PAGE_ALIGN(brk
);
108 oldbrk
= PAGE_ALIGN(mm
->brk
);
109 if (oldbrk
== newbrk
)
112 /* Always allow shrinking brk. */
113 if (brk
<= mm
->brk
) {
114 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
119 /* Check against rlimit.. */
120 rlim
= current
->rlim
[RLIMIT_DATA
].rlim_cur
;
121 if (rlim
< RLIM_INFINITY
&& brk
- mm
->start_data
> rlim
)
124 /* Check against existing mmap mappings. */
125 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
128 /* Ok, looks good - let it rip. */
129 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
135 up_write(&mm
->mmap_sem
);
139 /* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
140 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
143 static inline unsigned long
144 calc_vm_flags(unsigned long prot
, unsigned long flags
)
146 #define _trans(x,bit1,bit2) \
147 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
149 unsigned long prot_bits
, flag_bits
;
151 _trans(prot
, PROT_READ
, VM_READ
) |
152 _trans(prot
, PROT_WRITE
, VM_WRITE
) |
153 _trans(prot
, PROT_EXEC
, VM_EXEC
);
155 _trans(flags
, MAP_GROWSDOWN
, VM_GROWSDOWN
) |
156 _trans(flags
, MAP_DENYWRITE
, VM_DENYWRITE
) |
157 _trans(flags
, MAP_EXECUTABLE
, VM_EXECUTABLE
);
158 return prot_bits
| flag_bits
;
163 static int browse_rb(struct rb_node
* rb_node
) {
167 i
+= browse_rb(rb_node
->rb_left
);
168 i
+= browse_rb(rb_node
->rb_right
);
173 static void validate_mm(struct mm_struct
* mm
) {
176 struct vm_area_struct
* tmp
= mm
->mmap
;
181 if (i
!= mm
->map_count
)
182 printk("map_count %d vm_next %d\n", mm
->map_count
, i
), bug
= 1;
183 i
= browse_rb(mm
->mm_rb
.rb_node
);
184 if (i
!= mm
->map_count
)
185 printk("map_count %d rb %d\n", mm
->map_count
, i
), bug
= 1;
190 #define validate_mm(mm) do { } while (0)
193 static struct vm_area_struct
*
194 find_vma_prepare(struct mm_struct
*mm
, unsigned long addr
,
195 struct vm_area_struct
**pprev
, struct rb_node
***rb_link
,
196 struct rb_node
** rb_parent
)
198 struct vm_area_struct
* vma
;
199 struct rb_node
** __rb_link
, * __rb_parent
, * rb_prev
;
201 __rb_link
= &mm
->mm_rb
.rb_node
;
202 rb_prev
= __rb_parent
= NULL
;
206 struct vm_area_struct
*vma_tmp
;
208 __rb_parent
= *__rb_link
;
209 vma_tmp
= rb_entry(__rb_parent
, struct vm_area_struct
, vm_rb
);
211 if (vma_tmp
->vm_end
> addr
) {
213 if (vma_tmp
->vm_start
<= addr
)
215 __rb_link
= &__rb_parent
->rb_left
;
217 rb_prev
= __rb_parent
;
218 __rb_link
= &__rb_parent
->rb_right
;
224 *pprev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
225 *rb_link
= __rb_link
;
226 *rb_parent
= __rb_parent
;
231 __vma_link_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
232 struct vm_area_struct
*prev
, struct rb_node
*rb_parent
)
235 vma
->vm_next
= prev
->vm_next
;
240 vma
->vm_next
= rb_entry(rb_parent
,
241 struct vm_area_struct
, vm_rb
);
247 static void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
248 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
250 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
251 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
254 static inline void __vma_link_file(struct vm_area_struct
*vma
)
260 struct inode
* inode
= file
->f_dentry
->d_inode
;
261 struct address_space
*mapping
= inode
->i_mapping
;
263 if (vma
->vm_flags
& VM_DENYWRITE
)
264 atomic_dec(&inode
->i_writecount
);
266 if (vma
->vm_flags
& VM_SHARED
)
267 list_add_tail(&vma
->shared
, &mapping
->i_mmap_shared
);
269 list_add_tail(&vma
->shared
, &mapping
->i_mmap
);
274 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
275 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
276 struct rb_node
*rb_parent
)
278 __vma_link_list(mm
, vma
, prev
, rb_parent
);
279 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
280 __vma_link_file(vma
);
283 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
284 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
285 struct rb_node
*rb_parent
)
287 struct address_space
*mapping
= NULL
;
290 mapping
= vma
->vm_file
->f_dentry
->d_inode
->i_mapping
;
293 down(&mapping
->i_shared_sem
);
294 spin_lock(&mm
->page_table_lock
);
295 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
296 spin_unlock(&mm
->page_table_lock
);
298 up(&mapping
->i_shared_sem
);
300 mark_mm_hugetlb(mm
, vma
);
306 * If the vma has a ->close operation then the driver probably needs to release
307 * per-vma resources, so we don't attempt to merge those.
309 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
311 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
312 struct file
*file
, unsigned long vm_flags
)
314 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
316 if (vma
->vm_file
!= file
)
318 if (vma
->vm_flags
!= vm_flags
)
320 if (vma
->vm_private_data
)
326 * Return true if we can merge this (vm_flags,file,vm_pgoff,size)
327 * in front of (at a lower virtual address and file offset than) the vma.
329 * We don't check here for the merged mmap wrapping around the end of pagecache
330 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
331 * wrap, nor mmaps which cover the final page at index -1UL.
334 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
335 struct file
*file
, unsigned long vm_pgoff
, unsigned long size
)
337 if (is_mergeable_vma(vma
, file
, vm_flags
)) {
339 return 1; /* anon mapping */
340 if (vma
->vm_pgoff
== vm_pgoff
+ size
)
347 * Return true if we can merge this (vm_flags,file,vm_pgoff)
348 * beyond (at a higher virtual address and file offset than) the vma.
351 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
352 struct file
*file
, unsigned long vm_pgoff
)
354 if (is_mergeable_vma(vma
, file
, vm_flags
)) {
355 unsigned long vma_size
;
358 return 1; /* anon mapping */
360 vma_size
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
361 if (vma
->vm_pgoff
+ vma_size
== vm_pgoff
)
368 * Given a new mapping request (addr,end,vm_flags,file,pgoff), figure out
369 * whether that can be merged with its predecessor or its successor. Or
370 * both (it neatly fills a hole).
372 static int vma_merge(struct mm_struct
*mm
, struct vm_area_struct
*prev
,
373 struct rb_node
*rb_parent
, unsigned long addr
,
374 unsigned long end
, unsigned long vm_flags
,
375 struct file
*file
, unsigned long pgoff
)
377 spinlock_t
* lock
= &mm
->page_table_lock
;
380 * We later require that vma->vm_flags == vm_flags, so this tests
381 * vma->vm_flags & VM_SPECIAL, too.
383 if (vm_flags
& VM_SPECIAL
)
387 prev
= rb_entry(rb_parent
, struct vm_area_struct
, vm_rb
);
392 * Can it merge with the predecessor?
394 if (prev
->vm_end
== addr
&&
395 is_mergeable_vma(prev
, file
, vm_flags
) &&
396 can_vma_merge_after(prev
, vm_flags
, file
, pgoff
)) {
397 struct vm_area_struct
*next
;
398 struct inode
*inode
= file
? file
->f_dentry
->d_inode
: NULL
;
401 if (unlikely(file
&& prev
->vm_next
&&
402 prev
->vm_next
->vm_file
== file
)) {
403 down(&inode
->i_mapping
->i_shared_sem
);
410 * OK, it did. Can we now merge in the successor as well?
412 next
= prev
->vm_next
;
413 if (next
&& prev
->vm_end
== next
->vm_start
&&
414 can_vma_merge_before(next
, vm_flags
, file
,
415 pgoff
, (end
- addr
) >> PAGE_SHIFT
)) {
416 prev
->vm_end
= next
->vm_end
;
417 __vma_unlink(mm
, next
, prev
);
418 __remove_shared_vm_struct(next
, inode
);
421 up(&inode
->i_mapping
->i_shared_sem
);
426 kmem_cache_free(vm_area_cachep
, next
);
431 up(&inode
->i_mapping
->i_shared_sem
);
436 * Can this new request be merged in front of prev->vm_next?
438 prev
= prev
->vm_next
;
441 if (!can_vma_merge_before(prev
, vm_flags
, file
,
442 pgoff
, (end
- addr
) >> PAGE_SHIFT
))
444 if (end
== prev
->vm_start
) {
446 prev
->vm_start
= addr
;
447 prev
->vm_pgoff
-= (end
- addr
) >> PAGE_SHIFT
;
457 * The caller must hold down_write(current->mm->mmap_sem).
460 unsigned long do_mmap_pgoff(struct file
* file
, unsigned long addr
,
461 unsigned long len
, unsigned long prot
,
462 unsigned long flags
, unsigned long pgoff
)
464 struct mm_struct
* mm
= current
->mm
;
465 struct vm_area_struct
* vma
, * prev
;
467 unsigned int vm_flags
;
468 int correct_wcount
= 0;
470 struct rb_node
** rb_link
, * rb_parent
;
471 unsigned long charged
= 0;
473 if (file
&& (!file
->f_op
|| !file
->f_op
->mmap
))
482 len
= PAGE_ALIGN(len
);
484 /* offset overflow? */
485 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
488 /* Too many mappings? */
489 if (mm
->map_count
> MAX_MAP_COUNT
)
492 /* Obtain the address to map to. we verify (or select) it and ensure
493 * that it represents a valid section of the address space.
495 addr
= get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
496 if (addr
& ~PAGE_MASK
)
499 /* Do simple checking here so the lower-level routines won't have
500 * to. we assume access permissions have been handled by the open
501 * of the memory object, so we don't do any here.
503 vm_flags
= calc_vm_flags(prot
,flags
) | mm
->def_flags
|
504 VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
506 if (flags
& MAP_LOCKED
) {
507 if (!capable(CAP_IPC_LOCK
))
509 vm_flags
|= VM_LOCKED
;
511 /* mlock MCL_FUTURE? */
512 if (vm_flags
& VM_LOCKED
) {
513 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
515 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
519 inode
= file
? file
->f_dentry
->d_inode
: NULL
;
522 switch (flags
& MAP_TYPE
) {
524 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
528 * Make sure we don't allow writing to an append-only
531 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
535 * Make sure there are no mandatory locks on the file.
537 if (locks_verify_locked(inode
))
540 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
541 if (!(file
->f_mode
& FMODE_WRITE
))
542 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
546 if (!(file
->f_mode
& FMODE_READ
))
554 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
555 switch (flags
& MAP_TYPE
) {
559 vm_flags
&= ~(VM_SHARED
| VM_MAYSHARE
);
566 error
= security_file_mmap(file
, prot
, flags
);
573 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
574 if (vma
&& vma
->vm_start
< addr
+ len
) {
575 if (do_munmap(mm
, addr
, len
))
580 /* Check against address space limit. */
581 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
582 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
585 if (!(flags
& MAP_NORESERVE
) || sysctl_overcommit_memory
> 1) {
586 if (vm_flags
& VM_SHARED
) {
587 /* Check memory availability in shmem_file_setup? */
588 vm_flags
|= VM_ACCOUNT
;
589 } else if (vm_flags
& VM_WRITE
) {
591 * Private writable mapping: check memory availability
593 charged
= len
>> PAGE_SHIFT
;
594 if (security_vm_enough_memory(charged
))
596 vm_flags
|= VM_ACCOUNT
;
600 /* Can we just expand an old anonymous mapping? */
601 if (!file
&& !(vm_flags
& VM_SHARED
) && rb_parent
)
602 if (vma_merge(mm
, prev
, rb_parent
, addr
, addr
+ len
,
607 * Determine the object being mapped and call the appropriate
608 * specific mapper. the address has already been validated, but
609 * not unmapped, but the maps are removed from the list.
611 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
617 vma
->vm_start
= addr
;
618 vma
->vm_end
= addr
+ len
;
619 vma
->vm_flags
= vm_flags
;
620 vma
->vm_page_prot
= protection_map
[vm_flags
& 0x0f];
622 vma
->vm_pgoff
= pgoff
;
624 vma
->vm_private_data
= NULL
;
626 INIT_LIST_HEAD(&vma
->shared
);
630 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
632 if (vm_flags
& VM_DENYWRITE
) {
633 error
= deny_write_access(file
);
640 error
= file
->f_op
->mmap(file
, vma
);
642 goto unmap_and_free_vma
;
643 } else if (vm_flags
& VM_SHARED
) {
644 error
= shmem_zero_setup(vma
);
649 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
650 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
651 * that memory reservation must be checked; but that reservation
652 * belongs to shared memory object, not to vma: so now clear it.
654 if ((vm_flags
& (VM_SHARED
|VM_ACCOUNT
)) == (VM_SHARED
|VM_ACCOUNT
))
655 vma
->vm_flags
&= ~VM_ACCOUNT
;
657 /* Can addr have changed??
659 * Answer: Yes, several device drivers can do it in their
660 * f_op->mmap method. -DaveM
662 addr
= vma
->vm_start
;
664 if (!file
|| !rb_parent
|| !vma_merge(mm
, prev
, rb_parent
, addr
,
665 addr
+ len
, vma
->vm_flags
, file
, pgoff
)) {
666 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
668 atomic_inc(&inode
->i_writecount
);
672 atomic_inc(&inode
->i_writecount
);
675 kmem_cache_free(vm_area_cachep
, vma
);
678 mm
->total_vm
+= len
>> PAGE_SHIFT
;
679 if (vm_flags
& VM_LOCKED
) {
680 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
681 make_pages_present(addr
, addr
+ len
);
683 if (flags
& MAP_POPULATE
) {
684 up_write(&mm
->mmap_sem
);
685 sys_remap_file_pages(addr
, len
, prot
,
686 pgoff
, flags
& MAP_NONBLOCK
);
687 down_write(&mm
->mmap_sem
);
693 atomic_inc(&inode
->i_writecount
);
697 /* Undo any partial mapping done by a device driver. */
698 zap_page_range(vma
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
700 kmem_cache_free(vm_area_cachep
, vma
);
703 vm_unacct_memory(charged
);
707 /* Get an address range which is currently unmapped.
708 * For shmat() with addr=0.
710 * Ugly calling convention alert:
711 * Return value with the low bits set means error value,
713 * if (ret & ~PAGE_MASK)
716 * This function "knows" that -ENOMEM has the bits set.
718 #ifndef HAVE_ARCH_UNMAPPED_AREA
719 static inline unsigned long
720 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
721 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
723 struct mm_struct
*mm
= current
->mm
;
724 struct vm_area_struct
*vma
;
725 unsigned long start_addr
;
731 addr
= PAGE_ALIGN(addr
);
732 vma
= find_vma(mm
, addr
);
733 if (TASK_SIZE
- len
>= addr
&&
734 (!vma
|| addr
+ len
<= vma
->vm_start
))
737 start_addr
= addr
= mm
->free_area_cache
;
740 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
741 /* At this point: (!vma || addr < vma->vm_end). */
742 if (TASK_SIZE
- len
< addr
) {
744 * Start a new search - just in case we missed
747 if (start_addr
!= TASK_UNMAPPED_BASE
) {
748 start_addr
= addr
= TASK_UNMAPPED_BASE
;
753 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
755 * Remember the place where we stopped the search:
757 mm
->free_area_cache
= addr
+ len
;
765 arch_get_unmapped_area(struct file
*, unsigned long, unsigned long,
766 unsigned long, unsigned long);
770 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
771 unsigned long pgoff
, unsigned long flags
)
773 if (flags
& MAP_FIXED
) {
776 if (addr
> TASK_SIZE
- len
)
778 if (addr
& ~PAGE_MASK
)
780 if (file
&& is_file_hugepages(file
)) {
782 * Make sure that addr and length are properly aligned.
784 ret
= is_aligned_hugepage_range(addr
, len
);
787 * Ensure that a normal request is not falling in a
788 * reserved hugepage range. For some archs like IA-64,
789 * there is a separate region for hugepages.
791 ret
= is_hugepage_only_range(addr
, len
);
798 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
799 return file
->f_op
->get_unmapped_area(file
, addr
, len
,
802 return arch_get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
805 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
806 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
808 struct vm_area_struct
*vma
= NULL
;
811 /* Check the cache first. */
812 /* (Cache hit rate is typically around 35%.) */
813 vma
= mm
->mmap_cache
;
814 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
815 struct rb_node
* rb_node
;
817 rb_node
= mm
->mm_rb
.rb_node
;
821 struct vm_area_struct
* vma_tmp
;
823 vma_tmp
= rb_entry(rb_node
,
824 struct vm_area_struct
, vm_rb
);
826 if (vma_tmp
->vm_end
> addr
) {
828 if (vma_tmp
->vm_start
<= addr
)
830 rb_node
= rb_node
->rb_left
;
832 rb_node
= rb_node
->rb_right
;
835 mm
->mmap_cache
= vma
;
841 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
842 struct vm_area_struct
*
843 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
844 struct vm_area_struct
**pprev
)
846 struct vm_area_struct
*vma
= NULL
, *prev
= NULL
;
847 struct rb_node
* rb_node
;
851 /* Guard against addr being lower than the first VMA */
854 /* Go through the RB tree quickly. */
855 rb_node
= mm
->mm_rb
.rb_node
;
858 struct vm_area_struct
*vma_tmp
;
859 vma_tmp
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
861 if (addr
< vma_tmp
->vm_end
) {
862 rb_node
= rb_node
->rb_left
;
865 if (!prev
->vm_next
|| (addr
< prev
->vm_next
->vm_end
))
867 rb_node
= rb_node
->rb_right
;
873 return prev
? prev
->vm_next
: vma
;
876 #ifdef CONFIG_STACK_GROWSUP
878 * vma is the first one with address > vma->vm_end. Have to extend vma.
880 int expand_stack(struct vm_area_struct
* vma
, unsigned long address
)
884 if (!(vma
->vm_flags
& VM_GROWSUP
))
888 * vma->vm_start/vm_end cannot change under us because the caller
889 * is required to hold the mmap_sem in read mode. We need to get
890 * the spinlock only before relocating the vma range ourself.
892 address
+= 4 + PAGE_SIZE
- 1;
893 address
&= PAGE_MASK
;
894 spin_lock(&vma
->vm_mm
->page_table_lock
);
895 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
898 if (security_vm_enough_memory(grow
)) {
899 spin_unlock(&vma
->vm_mm
->page_table_lock
);
903 if (address
- vma
->vm_start
> current
->rlim
[RLIMIT_STACK
].rlim_cur
||
904 ((vma
->vm_mm
->total_vm
+ grow
) << PAGE_SHIFT
) >
905 current
->rlim
[RLIMIT_AS
].rlim_cur
) {
906 spin_unlock(&vma
->vm_mm
->page_table_lock
);
907 vm_unacct_memory(grow
);
910 vma
->vm_end
= address
;
911 vma
->vm_mm
->total_vm
+= grow
;
912 if (vma
->vm_flags
& VM_LOCKED
)
913 vma
->vm_mm
->locked_vm
+= grow
;
914 spin_unlock(&vma
->vm_mm
->page_table_lock
);
918 struct vm_area_struct
*
919 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
921 struct vm_area_struct
*vma
, *prev
;
924 vma
= find_vma_prev(mm
, addr
, &prev
);
925 if (vma
&& (vma
->vm_start
<= addr
))
927 if (!prev
|| expand_stack(prev
, addr
))
929 if (prev
->vm_flags
& VM_LOCKED
) {
930 make_pages_present(addr
, prev
->vm_end
);
936 * vma is the first one with address < vma->vm_start. Have to extend vma.
938 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
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
&= PAGE_MASK
;
948 spin_lock(&vma
->vm_mm
->page_table_lock
);
949 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
952 if (security_vm_enough_memory(grow
)) {
953 spin_unlock(&vma
->vm_mm
->page_table_lock
);
957 if (vma
->vm_end
- address
> current
->rlim
[RLIMIT_STACK
].rlim_cur
||
958 ((vma
->vm_mm
->total_vm
+ grow
) << PAGE_SHIFT
) >
959 current
->rlim
[RLIMIT_AS
].rlim_cur
) {
960 spin_unlock(&vma
->vm_mm
->page_table_lock
);
961 vm_unacct_memory(grow
);
964 vma
->vm_start
= address
;
965 vma
->vm_pgoff
-= grow
;
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
;
980 vma
= find_vma(mm
,addr
);
983 if (vma
->vm_start
<= addr
)
985 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
987 start
= vma
->vm_start
;
988 if (expand_stack(vma
, addr
))
990 if (vma
->vm_flags
& VM_LOCKED
) {
991 make_pages_present(addr
, start
);
998 * Try to free as many page directory entries as we can,
999 * without having to work very hard at actually scanning
1000 * the page tables themselves.
1002 * Right now we try to free page tables if we have a nice
1003 * PGDIR-aligned area that got free'd up. We could be more
1004 * granular if we want to, but this is fast and simple,
1005 * and covers the bad cases.
1007 * "prev", if it exists, points to a vma before the one
1008 * we just free'd - but there's no telling how much before.
1010 static void free_pgtables(struct mmu_gather
*tlb
, struct vm_area_struct
*prev
,
1011 unsigned long start
, unsigned long end
)
1013 unsigned long first
= start
& PGDIR_MASK
;
1014 unsigned long last
= end
+ PGDIR_SIZE
- 1;
1015 unsigned long start_index
, end_index
;
1016 struct mm_struct
*mm
= tlb
->mm
;
1022 if (prev
->vm_end
> start
) {
1023 if (last
> prev
->vm_start
)
1024 last
= prev
->vm_start
;
1029 struct vm_area_struct
*next
= prev
->vm_next
;
1032 if (next
->vm_start
< start
) {
1036 if (last
> next
->vm_start
)
1037 last
= next
->vm_start
;
1039 if (prev
->vm_end
> first
)
1040 first
= prev
->vm_end
+ PGDIR_SIZE
- 1;
1044 if (last
< first
) /* for arches with discontiguous pgd indices */
1047 * If the PGD bits are not consecutive in the virtual address, the
1048 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
1050 start_index
= pgd_index(first
);
1051 if (start_index
< FIRST_USER_PGD_NR
)
1052 start_index
= FIRST_USER_PGD_NR
;
1053 end_index
= pgd_index(last
);
1054 if (end_index
> start_index
) {
1055 clear_page_tables(tlb
, start_index
, end_index
- start_index
);
1056 flush_tlb_pgtables(mm
, first
& PGDIR_MASK
, last
& PGDIR_MASK
);
1060 /* Normal function to fix up a mapping
1061 * This function is the default for when an area has no specific
1062 * function. This may be used as part of a more specific routine.
1064 * By the time this function is called, the area struct has been
1065 * removed from the process mapping list.
1067 static void unmap_vma(struct mm_struct
*mm
, struct vm_area_struct
*area
)
1069 size_t len
= area
->vm_end
- area
->vm_start
;
1071 area
->vm_mm
->total_vm
-= len
>> PAGE_SHIFT
;
1072 if (area
->vm_flags
& VM_LOCKED
)
1073 area
->vm_mm
->locked_vm
-= len
>> PAGE_SHIFT
;
1075 * Is this a new hole at the lowest possible address?
1077 if (area
->vm_start
>= TASK_UNMAPPED_BASE
&&
1078 area
->vm_start
< area
->vm_mm
->free_area_cache
)
1079 area
->vm_mm
->free_area_cache
= area
->vm_start
;
1081 remove_shared_vm_struct(area
);
1083 if (area
->vm_ops
&& area
->vm_ops
->close
)
1084 area
->vm_ops
->close(area
);
1086 fput(area
->vm_file
);
1087 kmem_cache_free(vm_area_cachep
, area
);
1091 * Update the VMA and inode share lists.
1093 * Ok - we have the memory areas we should free on the 'free' list,
1094 * so release them, and do the vma updates.
1096 static void unmap_vma_list(struct mm_struct
*mm
,
1097 struct vm_area_struct
*mpnt
)
1100 struct vm_area_struct
*next
= mpnt
->vm_next
;
1101 unmap_vma(mm
, mpnt
);
1103 } while (mpnt
!= NULL
);
1108 * Get rid of page table information in the indicated region.
1110 * Called with the page table lock held.
1112 static void unmap_region(struct mm_struct
*mm
,
1113 struct vm_area_struct
*vma
,
1114 struct vm_area_struct
*prev
,
1115 unsigned long start
,
1118 struct mmu_gather
*tlb
;
1119 unsigned long nr_accounted
= 0;
1122 tlb
= tlb_gather_mmu(mm
, 0);
1123 unmap_vmas(&tlb
, mm
, vma
, start
, end
, &nr_accounted
);
1124 vm_unacct_memory(nr_accounted
);
1125 free_pgtables(tlb
, prev
, start
, end
);
1126 tlb_finish_mmu(tlb
, start
, end
);
1130 * Create a list of vma's touched by the unmap, removing them from the mm's
1131 * vma list as we go..
1133 * Called with the page_table_lock held.
1136 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1137 struct vm_area_struct
*prev
, unsigned long end
)
1139 struct vm_area_struct
**insertion_point
;
1140 struct vm_area_struct
*tail_vma
= NULL
;
1142 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
1144 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
1148 } while (vma
&& vma
->vm_start
< end
);
1149 *insertion_point
= vma
;
1150 tail_vma
->vm_next
= NULL
;
1151 mm
->mmap_cache
= NULL
; /* Kill the cache. */
1155 * Split a vma into two pieces at address 'addr', a new vma is allocated
1156 * either for the first part or the the tail.
1158 int split_vma(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
1159 unsigned long addr
, int new_below
)
1161 struct vm_area_struct
*new;
1163 if (mm
->map_count
>= MAX_MAP_COUNT
)
1166 new = kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1170 /* most fields are the same, copy all, and then fixup */
1173 INIT_LIST_HEAD(&new->shared
);
1177 vma
->vm_start
= addr
;
1178 vma
->vm_pgoff
+= ((addr
- new->vm_start
) >> PAGE_SHIFT
);
1181 new->vm_start
= addr
;
1182 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
1186 get_file(new->vm_file
);
1188 if (new->vm_ops
&& new->vm_ops
->open
)
1189 new->vm_ops
->open(new);
1191 insert_vm_struct(mm
, new);
1195 /* Munmap is split into 2 main parts -- this part which finds
1196 * what needs doing, and the areas themselves, which do the
1197 * work. This now handles partial unmappings.
1198 * Jeremy Fitzhardinge <jeremy@goop.org>
1200 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1203 struct vm_area_struct
*mpnt
, *prev
, *last
;
1205 if ((start
& ~PAGE_MASK
) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
1208 if ((len
= PAGE_ALIGN(len
)) == 0)
1211 /* Find the first overlapping VMA */
1212 mpnt
= find_vma_prev(mm
, start
, &prev
);
1215 /* we have start < mpnt->vm_end */
1217 if (is_vm_hugetlb_page(mpnt
)) {
1218 int ret
= is_aligned_hugepage_range(start
, len
);
1224 /* if it doesn't overlap, we have nothing.. */
1226 if (mpnt
->vm_start
>= end
)
1229 /* Something will probably happen, so notify. */
1230 if (mpnt
->vm_file
&& (mpnt
->vm_flags
& VM_EXEC
))
1231 profile_exec_unmap(mm
);
1234 * If we need to split any vma, do it now to save pain later.
1236 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1237 * unmapped vm_area_struct will remain in use: so lower split_vma
1238 * places tmp vma above, and higher split_vma places tmp vma below.
1240 if (start
> mpnt
->vm_start
) {
1241 if (split_vma(mm
, mpnt
, start
, 0))
1246 /* Does it split the last one? */
1247 last
= find_vma(mm
, end
);
1248 if (last
&& end
> last
->vm_start
) {
1249 if (split_vma(mm
, last
, end
, 1))
1252 mpnt
= prev
? prev
->vm_next
: mm
->mmap
;
1255 * Remove the vma's, and unmap the actual pages
1257 spin_lock(&mm
->page_table_lock
);
1258 detach_vmas_to_be_unmapped(mm
, mpnt
, prev
, end
);
1259 unmap_region(mm
, mpnt
, prev
, start
, end
);
1260 spin_unlock(&mm
->page_table_lock
);
1262 /* Fix up all other VM information */
1263 unmap_vma_list(mm
, mpnt
);
1268 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
1271 struct mm_struct
*mm
= current
->mm
;
1273 down_write(&mm
->mmap_sem
);
1274 ret
= do_munmap(mm
, addr
, len
);
1275 up_write(&mm
->mmap_sem
);
1280 * this is really a simplified "do_mmap". it only handles
1281 * anonymous maps. eventually we may be able to do some
1282 * brk-specific accounting here.
1284 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1286 struct mm_struct
* mm
= current
->mm
;
1287 struct vm_area_struct
* vma
, * prev
;
1288 unsigned long flags
;
1289 struct rb_node
** rb_link
, * rb_parent
;
1291 len
= PAGE_ALIGN(len
);
1298 if (mm
->def_flags
& VM_LOCKED
) {
1299 unsigned long locked
= mm
->locked_vm
<< PAGE_SHIFT
;
1301 if (locked
> current
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
)
1306 * Clear old maps. this also does some error checking for us
1309 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
1310 if (vma
&& vma
->vm_start
< addr
+ len
) {
1311 if (do_munmap(mm
, addr
, len
))
1316 /* Check against address space limits *after* clearing old maps... */
1317 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
1318 > current
->rlim
[RLIMIT_AS
].rlim_cur
)
1321 if (mm
->map_count
> MAX_MAP_COUNT
)
1324 if (security_vm_enough_memory(len
>> PAGE_SHIFT
))
1327 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
1329 /* Can we just expand an old anonymous mapping? */
1330 if (rb_parent
&& vma_merge(mm
, prev
, rb_parent
, addr
, addr
+ len
,
1335 * create a vma struct for an anonymous mapping
1337 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1339 vm_unacct_memory(len
>> PAGE_SHIFT
);
1344 vma
->vm_start
= addr
;
1345 vma
->vm_end
= addr
+ len
;
1346 vma
->vm_flags
= flags
;
1347 vma
->vm_page_prot
= protection_map
[flags
& 0x0f];
1350 vma
->vm_file
= NULL
;
1351 vma
->vm_private_data
= NULL
;
1352 INIT_LIST_HEAD(&vma
->shared
);
1354 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1357 mm
->total_vm
+= len
>> PAGE_SHIFT
;
1358 if (flags
& VM_LOCKED
) {
1359 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
1360 make_pages_present(addr
, addr
+ len
);
1365 /* Build the RB tree corresponding to the VMA list. */
1366 void build_mmap_rb(struct mm_struct
* mm
)
1368 struct vm_area_struct
* vma
;
1369 struct rb_node
** rb_link
, * rb_parent
;
1371 mm
->mm_rb
= RB_ROOT
;
1372 rb_link
= &mm
->mm_rb
.rb_node
;
1374 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
1375 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
1376 rb_parent
= &vma
->vm_rb
;
1377 rb_link
= &rb_parent
->rb_right
;
1381 /* Release all mmaps. */
1382 void exit_mmap(struct mm_struct
*mm
)
1384 struct mmu_gather
*tlb
;
1385 struct vm_area_struct
*vma
;
1386 unsigned long nr_accounted
= 0;
1388 profile_exit_mmap(mm
);
1392 spin_lock(&mm
->page_table_lock
);
1394 tlb
= tlb_gather_mmu(mm
, 1);
1396 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1397 mm
->map_count
-= unmap_vmas(&tlb
, mm
, mm
->mmap
, 0,
1398 ~0UL, &nr_accounted
);
1399 vm_unacct_memory(nr_accounted
);
1400 BUG_ON(mm
->map_count
); /* This is just debugging */
1401 clear_page_tables(tlb
, FIRST_USER_PGD_NR
, USER_PTRS_PER_PGD
);
1402 tlb_finish_mmu(tlb
, 0, MM_VM_SIZE(mm
));
1405 mm
->mmap
= mm
->mmap_cache
= NULL
;
1406 mm
->mm_rb
= RB_ROOT
;
1411 spin_unlock(&mm
->page_table_lock
);
1414 * Walk the list again, actually closing and freeing it
1415 * without holding any MM locks.
1418 struct vm_area_struct
*next
= vma
->vm_next
;
1419 remove_shared_vm_struct(vma
);
1421 if (vma
->vm_ops
->close
)
1422 vma
->vm_ops
->close(vma
);
1426 kmem_cache_free(vm_area_cachep
, vma
);
1431 /* Insert vm structure into process list sorted by address
1432 * and into the inode's i_mmap ring. If vm_file is non-NULL
1433 * then i_shared_sem is taken here.
1435 void insert_vm_struct(struct mm_struct
* mm
, struct vm_area_struct
* vma
)
1437 struct vm_area_struct
* __vma
, * prev
;
1438 struct rb_node
** rb_link
, * rb_parent
;
1440 __vma
= find_vma_prepare(mm
,vma
->vm_start
,&prev
,&rb_link
,&rb_parent
);
1441 if (__vma
&& __vma
->vm_start
< vma
->vm_end
)
1443 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);