6 * Address space accounting code <alan@redhat.com>
9 #include <linux/slab.h>
11 #include <linux/shm.h>
12 #include <linux/mman.h>
13 #include <linux/pagemap.h>
14 #include <linux/swap.h>
15 #include <linux/syscalls.h>
16 #include <linux/init.h>
17 #include <linux/file.h>
19 #include <linux/personality.h>
20 #include <linux/security.h>
21 #include <linux/hugetlb.h>
22 #include <linux/profile.h>
23 #include <linux/module.h>
24 #include <linux/mount.h>
25 #include <linux/mempolicy.h>
26 #include <linux/rmap.h>
28 #include <asm/uaccess.h>
29 #include <asm/cacheflush.h>
33 * WARNING: the debugging will use recursive algorithms so never enable this
34 * unless you know what you are doing.
38 /* description of effects of mapping type and prot in current implementation.
39 * this is due to the limited x86 page protection hardware. The expected
40 * behavior is in parens:
43 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
44 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
45 * w: (no) no w: (no) no w: (yes) yes w: (no) no
46 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
48 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
49 * w: (no) no w: (no) no w: (copy) copy w: (no) no
50 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
53 pgprot_t protection_map
[16] = {
54 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
55 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
58 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
59 int sysctl_overcommit_ratio
= 50; /* default is 50% */
60 int sysctl_max_map_count
= DEFAULT_MAX_MAP_COUNT
;
61 atomic_t vm_committed_space
= ATOMIC_INIT(0);
64 * Check that a process has enough memory to allocate a new virtual
65 * mapping. 0 means there is enough memory for the allocation to
66 * succeed and -ENOMEM implies there is not.
68 * We currently support three overcommit policies, which are set via the
69 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
71 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
72 * Additional code 2002 Jul 20 by Robert Love.
74 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
76 * Note this is a helper function intended to be used by LSMs which
77 * wish to use this logic.
79 int __vm_enough_memory(long pages
, int cap_sys_admin
)
81 unsigned long free
, allowed
;
83 vm_acct_memory(pages
);
86 * Sometimes we want to use more memory than we have
88 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
91 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
94 free
= get_page_cache_size();
95 free
+= nr_swap_pages
;
98 * Any slabs which are created with the
99 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
100 * which are reclaimable, under pressure. The dentry
101 * cache and most inode caches should fall into this
103 free
+= atomic_read(&slab_reclaim_pages
);
106 * Leave the last 3% for root
115 * nr_free_pages() is very expensive on large systems,
116 * only call if we're about to fail.
125 vm_unacct_memory(pages
);
129 allowed
= (totalram_pages
- hugetlb_total_pages())
130 * sysctl_overcommit_ratio
/ 100;
132 * Leave the last 3% for root
135 allowed
-= allowed
/ 32;
136 allowed
+= total_swap_pages
;
138 /* Don't let a single process grow too big:
139 leave 3% of the size of this process for other processes */
140 allowed
-= current
->mm
->total_vm
/ 32;
142 if (atomic_read(&vm_committed_space
) < allowed
)
145 vm_unacct_memory(pages
);
150 EXPORT_SYMBOL(sysctl_overcommit_memory
);
151 EXPORT_SYMBOL(sysctl_overcommit_ratio
);
152 EXPORT_SYMBOL(sysctl_max_map_count
);
153 EXPORT_SYMBOL(vm_committed_space
);
154 EXPORT_SYMBOL(__vm_enough_memory
);
157 * Requires inode->i_mapping->i_mmap_lock
159 static void __remove_shared_vm_struct(struct vm_area_struct
*vma
,
160 struct file
*file
, struct address_space
*mapping
)
162 if (vma
->vm_flags
& VM_DENYWRITE
)
163 atomic_inc(&file
->f_dentry
->d_inode
->i_writecount
);
164 if (vma
->vm_flags
& VM_SHARED
)
165 mapping
->i_mmap_writable
--;
167 flush_dcache_mmap_lock(mapping
);
168 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
))
169 list_del_init(&vma
->shared
.vm_set
.list
);
171 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
172 flush_dcache_mmap_unlock(mapping
);
176 * Remove one vm structure and free it.
178 static void remove_vm_struct(struct vm_area_struct
*vma
)
180 struct file
*file
= vma
->vm_file
;
184 struct address_space
*mapping
= file
->f_mapping
;
185 spin_lock(&mapping
->i_mmap_lock
);
186 __remove_shared_vm_struct(vma
, file
, mapping
);
187 spin_unlock(&mapping
->i_mmap_lock
);
189 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
190 vma
->vm_ops
->close(vma
);
193 anon_vma_unlink(vma
);
194 mpol_free(vma_policy(vma
));
195 kmem_cache_free(vm_area_cachep
, vma
);
199 * sys_brk() for the most part doesn't need the global kernel
200 * lock, except when an application is doing something nasty
201 * like trying to un-brk an area that has already been mapped
202 * to a regular file. in this case, the unmapping will need
203 * to invoke file system routines that need the global lock.
205 asmlinkage
unsigned long sys_brk(unsigned long brk
)
207 unsigned long rlim
, retval
;
208 unsigned long newbrk
, oldbrk
;
209 struct mm_struct
*mm
= current
->mm
;
211 down_write(&mm
->mmap_sem
);
213 if (brk
< mm
->end_code
)
215 newbrk
= PAGE_ALIGN(brk
);
216 oldbrk
= PAGE_ALIGN(mm
->brk
);
217 if (oldbrk
== newbrk
)
220 /* Always allow shrinking brk. */
221 if (brk
<= mm
->brk
) {
222 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
227 /* Check against rlimit.. */
228 rlim
= current
->signal
->rlim
[RLIMIT_DATA
].rlim_cur
;
229 if (rlim
< RLIM_INFINITY
&& brk
- mm
->start_data
> rlim
)
232 /* Check against existing mmap mappings. */
233 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
236 /* Ok, looks good - let it rip. */
237 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
243 up_write(&mm
->mmap_sem
);
248 static int browse_rb(struct rb_root
*root
)
251 struct rb_node
*nd
, *pn
= NULL
;
252 unsigned long prev
= 0, pend
= 0;
254 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
255 struct vm_area_struct
*vma
;
256 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
257 if (vma
->vm_start
< prev
)
258 printk("vm_start %lx prev %lx\n", vma
->vm_start
, prev
), i
= -1;
259 if (vma
->vm_start
< pend
)
260 printk("vm_start %lx pend %lx\n", vma
->vm_start
, pend
);
261 if (vma
->vm_start
> vma
->vm_end
)
262 printk("vm_end %lx < vm_start %lx\n", vma
->vm_end
, vma
->vm_start
);
267 for (nd
= pn
; nd
; nd
= rb_prev(nd
)) {
271 printk("backwards %d, forwards %d\n", j
, i
), i
= 0;
275 void validate_mm(struct mm_struct
*mm
)
279 struct vm_area_struct
*tmp
= mm
->mmap
;
284 if (i
!= mm
->map_count
)
285 printk("map_count %d vm_next %d\n", mm
->map_count
, i
), bug
= 1;
286 i
= browse_rb(&mm
->mm_rb
);
287 if (i
!= mm
->map_count
)
288 printk("map_count %d rb %d\n", mm
->map_count
, i
), bug
= 1;
293 #define validate_mm(mm) do { } while (0)
296 static struct vm_area_struct
*
297 find_vma_prepare(struct mm_struct
*mm
, unsigned long addr
,
298 struct vm_area_struct
**pprev
, struct rb_node
***rb_link
,
299 struct rb_node
** rb_parent
)
301 struct vm_area_struct
* vma
;
302 struct rb_node
** __rb_link
, * __rb_parent
, * rb_prev
;
304 __rb_link
= &mm
->mm_rb
.rb_node
;
305 rb_prev
= __rb_parent
= NULL
;
309 struct vm_area_struct
*vma_tmp
;
311 __rb_parent
= *__rb_link
;
312 vma_tmp
= rb_entry(__rb_parent
, struct vm_area_struct
, vm_rb
);
314 if (vma_tmp
->vm_end
> addr
) {
316 if (vma_tmp
->vm_start
<= addr
)
318 __rb_link
= &__rb_parent
->rb_left
;
320 rb_prev
= __rb_parent
;
321 __rb_link
= &__rb_parent
->rb_right
;
327 *pprev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
328 *rb_link
= __rb_link
;
329 *rb_parent
= __rb_parent
;
334 __vma_link_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
335 struct vm_area_struct
*prev
, struct rb_node
*rb_parent
)
338 vma
->vm_next
= prev
->vm_next
;
343 vma
->vm_next
= rb_entry(rb_parent
,
344 struct vm_area_struct
, vm_rb
);
350 void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
351 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
353 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
354 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
357 static inline void __vma_link_file(struct vm_area_struct
*vma
)
363 struct address_space
*mapping
= file
->f_mapping
;
365 if (vma
->vm_flags
& VM_DENYWRITE
)
366 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
367 if (vma
->vm_flags
& VM_SHARED
)
368 mapping
->i_mmap_writable
++;
370 flush_dcache_mmap_lock(mapping
);
371 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
))
372 vma_nonlinear_insert(vma
, &mapping
->i_mmap_nonlinear
);
374 vma_prio_tree_insert(vma
, &mapping
->i_mmap
);
375 flush_dcache_mmap_unlock(mapping
);
380 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
381 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
382 struct rb_node
*rb_parent
)
384 __vma_link_list(mm
, vma
, prev
, rb_parent
);
385 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
386 __anon_vma_link(vma
);
389 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
390 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
391 struct rb_node
*rb_parent
)
393 struct address_space
*mapping
= NULL
;
396 mapping
= vma
->vm_file
->f_mapping
;
399 spin_lock(&mapping
->i_mmap_lock
);
400 vma
->vm_truncate_count
= mapping
->truncate_count
;
404 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
405 __vma_link_file(vma
);
407 anon_vma_unlock(vma
);
409 spin_unlock(&mapping
->i_mmap_lock
);
416 * Helper for vma_adjust in the split_vma insert case:
417 * insert vm structure into list and rbtree and anon_vma,
418 * but it has already been inserted into prio_tree earlier.
421 __insert_vm_struct(struct mm_struct
* mm
, struct vm_area_struct
* vma
)
423 struct vm_area_struct
* __vma
, * prev
;
424 struct rb_node
** rb_link
, * rb_parent
;
426 __vma
= find_vma_prepare(mm
, vma
->vm_start
,&prev
, &rb_link
, &rb_parent
);
427 if (__vma
&& __vma
->vm_start
< vma
->vm_end
)
429 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
434 __vma_unlink(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
435 struct vm_area_struct
*prev
)
437 prev
->vm_next
= vma
->vm_next
;
438 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
439 if (mm
->mmap_cache
== vma
)
440 mm
->mmap_cache
= prev
;
444 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
445 * is already present in an i_mmap tree without adjusting the tree.
446 * The following helper function should be used when such adjustments
447 * are necessary. The "insert" vma (if any) is to be inserted
448 * before we drop the necessary locks.
450 void vma_adjust(struct vm_area_struct
*vma
, unsigned long start
,
451 unsigned long end
, pgoff_t pgoff
, struct vm_area_struct
*insert
)
453 struct mm_struct
*mm
= vma
->vm_mm
;
454 struct vm_area_struct
*next
= vma
->vm_next
;
455 struct vm_area_struct
*importer
= NULL
;
456 struct address_space
*mapping
= NULL
;
457 struct prio_tree_root
*root
= NULL
;
458 struct file
*file
= vma
->vm_file
;
459 struct anon_vma
*anon_vma
= NULL
;
460 long adjust_next
= 0;
463 if (next
&& !insert
) {
464 if (end
>= next
->vm_end
) {
466 * vma expands, overlapping all the next, and
467 * perhaps the one after too (mprotect case 6).
469 again
: remove_next
= 1 + (end
> next
->vm_end
);
471 anon_vma
= next
->anon_vma
;
473 } else if (end
> next
->vm_start
) {
475 * vma expands, overlapping part of the next:
476 * mprotect case 5 shifting the boundary up.
478 adjust_next
= (end
- next
->vm_start
) >> PAGE_SHIFT
;
479 anon_vma
= next
->anon_vma
;
481 } else if (end
< vma
->vm_end
) {
483 * vma shrinks, and !insert tells it's not
484 * split_vma inserting another: so it must be
485 * mprotect case 4 shifting the boundary down.
487 adjust_next
= - ((vma
->vm_end
- end
) >> PAGE_SHIFT
);
488 anon_vma
= next
->anon_vma
;
494 mapping
= file
->f_mapping
;
495 if (!(vma
->vm_flags
& VM_NONLINEAR
))
496 root
= &mapping
->i_mmap
;
497 spin_lock(&mapping
->i_mmap_lock
);
499 vma
->vm_truncate_count
!= next
->vm_truncate_count
) {
501 * unmap_mapping_range might be in progress:
502 * ensure that the expanding vma is rescanned.
504 importer
->vm_truncate_count
= 0;
507 insert
->vm_truncate_count
= vma
->vm_truncate_count
;
509 * Put into prio_tree now, so instantiated pages
510 * are visible to arm/parisc __flush_dcache_page
511 * throughout; but we cannot insert into address
512 * space until vma start or end is updated.
514 __vma_link_file(insert
);
519 * When changing only vma->vm_end, we don't really need
520 * anon_vma lock: but is that case worth optimizing out?
523 anon_vma
= vma
->anon_vma
;
525 spin_lock(&anon_vma
->lock
);
527 * Easily overlooked: when mprotect shifts the boundary,
528 * make sure the expanding vma has anon_vma set if the
529 * shrinking vma had, to cover any anon pages imported.
531 if (importer
&& !importer
->anon_vma
) {
532 importer
->anon_vma
= anon_vma
;
533 __anon_vma_link(importer
);
538 flush_dcache_mmap_lock(mapping
);
539 vma_prio_tree_remove(vma
, root
);
541 vma_prio_tree_remove(next
, root
);
544 vma
->vm_start
= start
;
546 vma
->vm_pgoff
= pgoff
;
548 next
->vm_start
+= adjust_next
<< PAGE_SHIFT
;
549 next
->vm_pgoff
+= adjust_next
;
554 vma_prio_tree_insert(next
, root
);
555 vma_prio_tree_insert(vma
, root
);
556 flush_dcache_mmap_unlock(mapping
);
561 * vma_merge has merged next into vma, and needs
562 * us to remove next before dropping the locks.
564 __vma_unlink(mm
, next
, vma
);
566 __remove_shared_vm_struct(next
, file
, mapping
);
568 __anon_vma_merge(vma
, next
);
571 * split_vma has split insert from vma, and needs
572 * us to insert it before dropping the locks
573 * (it may either follow vma or precede it).
575 __insert_vm_struct(mm
, insert
);
579 spin_unlock(&anon_vma
->lock
);
581 spin_unlock(&mapping
->i_mmap_lock
);
587 mpol_free(vma_policy(next
));
588 kmem_cache_free(vm_area_cachep
, next
);
590 * In mprotect's case 6 (see comments on vma_merge),
591 * we must remove another next too. It would clutter
592 * up the code too much to do both in one go.
594 if (remove_next
== 2) {
604 * If the vma has a ->close operation then the driver probably needs to release
605 * per-vma resources, so we don't attempt to merge those.
607 #define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)
609 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
610 struct file
*file
, unsigned long vm_flags
)
612 if (vma
->vm_flags
!= vm_flags
)
614 if (vma
->vm_file
!= file
)
616 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
621 static inline int is_mergeable_anon_vma(struct anon_vma
*anon_vma1
,
622 struct anon_vma
*anon_vma2
)
624 return !anon_vma1
|| !anon_vma2
|| (anon_vma1
== anon_vma2
);
628 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
629 * in front of (at a lower virtual address and file offset than) the vma.
631 * We cannot merge two vmas if they have differently assigned (non-NULL)
632 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
634 * We don't check here for the merged mmap wrapping around the end of pagecache
635 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
636 * wrap, nor mmaps which cover the final page at index -1UL.
639 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
640 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
642 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
643 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
)) {
644 if (vma
->vm_pgoff
== vm_pgoff
)
651 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
652 * beyond (at a higher virtual address and file offset than) the vma.
654 * We cannot merge two vmas if they have differently assigned (non-NULL)
655 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
658 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
659 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
661 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
662 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
)) {
664 vm_pglen
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
665 if (vma
->vm_pgoff
+ vm_pglen
== vm_pgoff
)
672 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
673 * whether that can be merged with its predecessor or its successor.
674 * Or both (it neatly fills a hole).
676 * In most cases - when called for mmap, brk or mremap - [addr,end) is
677 * certain not to be mapped by the time vma_merge is called; but when
678 * called for mprotect, it is certain to be already mapped (either at
679 * an offset within prev, or at the start of next), and the flags of
680 * this area are about to be changed to vm_flags - and the no-change
681 * case has already been eliminated.
683 * The following mprotect cases have to be considered, where AAAA is
684 * the area passed down from mprotect_fixup, never extending beyond one
685 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
687 * AAAA AAAA AAAA AAAA
688 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
689 * cannot merge might become might become might become
690 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
691 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
692 * mremap move: PPPPNNNNNNNN 8
694 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
695 * might become case 1 below case 2 below case 3 below
697 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
698 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
700 struct vm_area_struct
*vma_merge(struct mm_struct
*mm
,
701 struct vm_area_struct
*prev
, unsigned long addr
,
702 unsigned long end
, unsigned long vm_flags
,
703 struct anon_vma
*anon_vma
, struct file
*file
,
704 pgoff_t pgoff
, struct mempolicy
*policy
)
706 pgoff_t pglen
= (end
- addr
) >> PAGE_SHIFT
;
707 struct vm_area_struct
*area
, *next
;
710 * We later require that vma->vm_flags == vm_flags,
711 * so this tests vma->vm_flags & VM_SPECIAL, too.
713 if (vm_flags
& VM_SPECIAL
)
717 next
= prev
->vm_next
;
721 if (next
&& next
->vm_end
== end
) /* cases 6, 7, 8 */
722 next
= next
->vm_next
;
725 * Can it merge with the predecessor?
727 if (prev
&& prev
->vm_end
== addr
&&
728 mpol_equal(vma_policy(prev
), policy
) &&
729 can_vma_merge_after(prev
, vm_flags
,
730 anon_vma
, file
, pgoff
)) {
732 * OK, it can. Can we now merge in the successor as well?
734 if (next
&& end
== next
->vm_start
&&
735 mpol_equal(policy
, vma_policy(next
)) &&
736 can_vma_merge_before(next
, vm_flags
,
737 anon_vma
, file
, pgoff
+pglen
) &&
738 is_mergeable_anon_vma(prev
->anon_vma
,
741 vma_adjust(prev
, prev
->vm_start
,
742 next
->vm_end
, prev
->vm_pgoff
, NULL
);
743 } else /* cases 2, 5, 7 */
744 vma_adjust(prev
, prev
->vm_start
,
745 end
, prev
->vm_pgoff
, NULL
);
750 * Can this new request be merged in front of next?
752 if (next
&& end
== next
->vm_start
&&
753 mpol_equal(policy
, vma_policy(next
)) &&
754 can_vma_merge_before(next
, vm_flags
,
755 anon_vma
, file
, pgoff
+pglen
)) {
756 if (prev
&& addr
< prev
->vm_end
) /* case 4 */
757 vma_adjust(prev
, prev
->vm_start
,
758 addr
, prev
->vm_pgoff
, NULL
);
759 else /* cases 3, 8 */
760 vma_adjust(area
, addr
, next
->vm_end
,
761 next
->vm_pgoff
- pglen
, NULL
);
769 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
770 * neighbouring vmas for a suitable anon_vma, before it goes off
771 * to allocate a new anon_vma. It checks because a repetitive
772 * sequence of mprotects and faults may otherwise lead to distinct
773 * anon_vmas being allocated, preventing vma merge in subsequent
776 struct anon_vma
*find_mergeable_anon_vma(struct vm_area_struct
*vma
)
778 struct vm_area_struct
*near
;
779 unsigned long vm_flags
;
786 * Since only mprotect tries to remerge vmas, match flags
787 * which might be mprotected into each other later on.
788 * Neither mlock nor madvise tries to remerge at present,
789 * so leave their flags as obstructing a merge.
791 vm_flags
= vma
->vm_flags
& ~(VM_READ
|VM_WRITE
|VM_EXEC
);
792 vm_flags
|= near
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
);
794 if (near
->anon_vma
&& vma
->vm_end
== near
->vm_start
&&
795 mpol_equal(vma_policy(vma
), vma_policy(near
)) &&
796 can_vma_merge_before(near
, vm_flags
,
797 NULL
, vma
->vm_file
, vma
->vm_pgoff
+
798 ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
)))
799 return near
->anon_vma
;
802 * It is potentially slow to have to call find_vma_prev here.
803 * But it's only on the first write fault on the vma, not
804 * every time, and we could devise a way to avoid it later
805 * (e.g. stash info in next's anon_vma_node when assigning
806 * an anon_vma, or when trying vma_merge). Another time.
808 if (find_vma_prev(vma
->vm_mm
, vma
->vm_start
, &near
) != vma
)
813 vm_flags
= vma
->vm_flags
& ~(VM_READ
|VM_WRITE
|VM_EXEC
);
814 vm_flags
|= near
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
);
816 if (near
->anon_vma
&& near
->vm_end
== vma
->vm_start
&&
817 mpol_equal(vma_policy(near
), vma_policy(vma
)) &&
818 can_vma_merge_after(near
, vm_flags
,
819 NULL
, vma
->vm_file
, vma
->vm_pgoff
))
820 return near
->anon_vma
;
823 * There's no absolute need to look only at touching neighbours:
824 * we could search further afield for "compatible" anon_vmas.
825 * But it would probably just be a waste of time searching,
826 * or lead to too many vmas hanging off the same anon_vma.
827 * We're trying to allow mprotect remerging later on,
828 * not trying to minimize memory used for anon_vmas.
833 #ifdef CONFIG_PROC_FS
834 void __vm_stat_account(struct mm_struct
*mm
, unsigned long flags
,
835 struct file
*file
, long pages
)
837 const unsigned long stack_flags
838 = VM_STACK_FLAGS
& (VM_GROWSUP
|VM_GROWSDOWN
);
840 #ifdef CONFIG_HUGETLB
841 if (flags
& VM_HUGETLB
) {
842 if (!(flags
& VM_DONTCOPY
))
843 mm
->shared_vm
+= pages
;
846 #endif /* CONFIG_HUGETLB */
849 mm
->shared_vm
+= pages
;
850 if ((flags
& (VM_EXEC
|VM_WRITE
)) == VM_EXEC
)
851 mm
->exec_vm
+= pages
;
852 } else if (flags
& stack_flags
)
853 mm
->stack_vm
+= pages
;
854 if (flags
& (VM_RESERVED
|VM_IO
))
855 mm
->reserved_vm
+= pages
;
857 #endif /* CONFIG_PROC_FS */
860 * The caller must hold down_write(current->mm->mmap_sem).
863 unsigned long do_mmap_pgoff(struct file
* file
, unsigned long addr
,
864 unsigned long len
, unsigned long prot
,
865 unsigned long flags
, unsigned long pgoff
)
867 struct mm_struct
* mm
= current
->mm
;
868 struct vm_area_struct
* vma
, * prev
;
870 unsigned int vm_flags
;
871 int correct_wcount
= 0;
873 struct rb_node
** rb_link
, * rb_parent
;
875 unsigned long charged
= 0, reqprot
= prot
;
878 if (is_file_hugepages(file
))
881 if (!file
->f_op
|| !file
->f_op
->mmap
)
884 if ((prot
& PROT_EXEC
) &&
885 (file
->f_vfsmnt
->mnt_flags
& MNT_NOEXEC
))
889 * Does the application expect PROT_READ to imply PROT_EXEC?
891 * (the exception is when the underlying filesystem is noexec
892 * mounted, in which case we dont add PROT_EXEC.)
894 if ((prot
& PROT_READ
) && (current
->personality
& READ_IMPLIES_EXEC
))
895 if (!(file
&& (file
->f_vfsmnt
->mnt_flags
& MNT_NOEXEC
)))
901 /* Careful about overflows.. */
902 len
= PAGE_ALIGN(len
);
903 if (!len
|| len
> TASK_SIZE
)
906 /* offset overflow? */
907 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
910 /* Too many mappings? */
911 if (mm
->map_count
> sysctl_max_map_count
)
914 /* Obtain the address to map to. we verify (or select) it and ensure
915 * that it represents a valid section of the address space.
917 addr
= get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
918 if (addr
& ~PAGE_MASK
)
921 /* Do simple checking here so the lower-level routines won't have
922 * to. we assume access permissions have been handled by the open
923 * of the memory object, so we don't do any here.
925 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
) |
926 mm
->def_flags
| VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
928 if (flags
& MAP_LOCKED
) {
931 vm_flags
|= VM_LOCKED
;
933 /* mlock MCL_FUTURE? */
934 if (vm_flags
& VM_LOCKED
) {
935 unsigned long locked
, lock_limit
;
936 locked
= mm
->locked_vm
<< PAGE_SHIFT
;
937 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
939 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
943 inode
= file
? file
->f_dentry
->d_inode
: NULL
;
946 switch (flags
& MAP_TYPE
) {
948 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
952 * Make sure we don't allow writing to an append-only
955 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
959 * Make sure there are no mandatory locks on the file.
961 if (locks_verify_locked(inode
))
964 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
965 if (!(file
->f_mode
& FMODE_WRITE
))
966 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
970 if (!(file
->f_mode
& FMODE_READ
))
978 switch (flags
& MAP_TYPE
) {
980 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
984 * Set pgoff according to addr for anon_vma.
986 pgoff
= addr
>> PAGE_SHIFT
;
993 error
= security_file_mmap(file
, reqprot
, prot
, flags
);
1000 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
1001 if (vma
&& vma
->vm_start
< addr
+ len
) {
1002 if (do_munmap(mm
, addr
, len
))
1007 /* Check against address space limit. */
1008 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
1009 > current
->signal
->rlim
[RLIMIT_AS
].rlim_cur
)
1012 if (accountable
&& (!(flags
& MAP_NORESERVE
) ||
1013 sysctl_overcommit_memory
== OVERCOMMIT_NEVER
)) {
1014 if (vm_flags
& VM_SHARED
) {
1015 /* Check memory availability in shmem_file_setup? */
1016 vm_flags
|= VM_ACCOUNT
;
1017 } else if (vm_flags
& VM_WRITE
) {
1019 * Private writable mapping: check memory availability
1021 charged
= len
>> PAGE_SHIFT
;
1022 if (security_vm_enough_memory(charged
))
1024 vm_flags
|= VM_ACCOUNT
;
1029 * Can we just expand an old private anonymous mapping?
1030 * The VM_SHARED test is necessary because shmem_zero_setup
1031 * will create the file object for a shared anonymous map below.
1033 if (!file
&& !(vm_flags
& VM_SHARED
) &&
1034 vma_merge(mm
, prev
, addr
, addr
+ len
, vm_flags
,
1035 NULL
, NULL
, pgoff
, NULL
))
1039 * Determine the object being mapped and call the appropriate
1040 * specific mapper. the address has already been validated, but
1041 * not unmapped, but the maps are removed from the list.
1043 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1048 memset(vma
, 0, sizeof(*vma
));
1051 vma
->vm_start
= addr
;
1052 vma
->vm_end
= addr
+ len
;
1053 vma
->vm_flags
= vm_flags
;
1054 vma
->vm_page_prot
= protection_map
[vm_flags
& 0x0f];
1055 vma
->vm_pgoff
= pgoff
;
1059 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1061 if (vm_flags
& VM_DENYWRITE
) {
1062 error
= deny_write_access(file
);
1067 vma
->vm_file
= file
;
1069 error
= file
->f_op
->mmap(file
, vma
);
1071 goto unmap_and_free_vma
;
1072 } else if (vm_flags
& VM_SHARED
) {
1073 error
= shmem_zero_setup(vma
);
1078 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1079 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1080 * that memory reservation must be checked; but that reservation
1081 * belongs to shared memory object, not to vma: so now clear it.
1083 if ((vm_flags
& (VM_SHARED
|VM_ACCOUNT
)) == (VM_SHARED
|VM_ACCOUNT
))
1084 vma
->vm_flags
&= ~VM_ACCOUNT
;
1086 /* Can addr have changed??
1088 * Answer: Yes, several device drivers can do it in their
1089 * f_op->mmap method. -DaveM
1091 addr
= vma
->vm_start
;
1092 pgoff
= vma
->vm_pgoff
;
1093 vm_flags
= vma
->vm_flags
;
1095 if (!file
|| !vma_merge(mm
, prev
, addr
, vma
->vm_end
,
1096 vma
->vm_flags
, NULL
, file
, pgoff
, vma_policy(vma
))) {
1097 file
= vma
->vm_file
;
1098 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1100 atomic_inc(&inode
->i_writecount
);
1104 atomic_inc(&inode
->i_writecount
);
1107 mpol_free(vma_policy(vma
));
1108 kmem_cache_free(vm_area_cachep
, vma
);
1111 mm
->total_vm
+= len
>> PAGE_SHIFT
;
1112 __vm_stat_account(mm
, vm_flags
, file
, len
>> PAGE_SHIFT
);
1113 if (vm_flags
& VM_LOCKED
) {
1114 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
1115 make_pages_present(addr
, addr
+ len
);
1117 if (flags
& MAP_POPULATE
) {
1118 up_write(&mm
->mmap_sem
);
1119 sys_remap_file_pages(addr
, len
, 0,
1120 pgoff
, flags
& MAP_NONBLOCK
);
1121 down_write(&mm
->mmap_sem
);
1127 atomic_inc(&inode
->i_writecount
);
1128 vma
->vm_file
= NULL
;
1131 /* Undo any partial mapping done by a device driver. */
1132 zap_page_range(vma
, vma
->vm_start
, vma
->vm_end
- vma
->vm_start
, NULL
);
1134 kmem_cache_free(vm_area_cachep
, vma
);
1137 vm_unacct_memory(charged
);
1141 EXPORT_SYMBOL(do_mmap_pgoff
);
1143 /* Get an address range which is currently unmapped.
1144 * For shmat() with addr=0.
1146 * Ugly calling convention alert:
1147 * Return value with the low bits set means error value,
1149 * if (ret & ~PAGE_MASK)
1152 * This function "knows" that -ENOMEM has the bits set.
1154 #ifndef HAVE_ARCH_UNMAPPED_AREA
1156 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
1157 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1159 struct mm_struct
*mm
= current
->mm
;
1160 struct vm_area_struct
*vma
;
1161 unsigned long start_addr
;
1163 if (len
> TASK_SIZE
)
1167 addr
= PAGE_ALIGN(addr
);
1168 vma
= find_vma(mm
, addr
);
1169 if (TASK_SIZE
- len
>= addr
&&
1170 (!vma
|| addr
+ len
<= vma
->vm_start
))
1173 start_addr
= addr
= mm
->free_area_cache
;
1176 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
1177 /* At this point: (!vma || addr < vma->vm_end). */
1178 if (TASK_SIZE
- len
< addr
) {
1180 * Start a new search - just in case we missed
1183 if (start_addr
!= TASK_UNMAPPED_BASE
) {
1184 start_addr
= addr
= TASK_UNMAPPED_BASE
;
1189 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
1191 * Remember the place where we stopped the search:
1193 mm
->free_area_cache
= addr
+ len
;
1201 void arch_unmap_area(struct vm_area_struct
*area
)
1204 * Is this a new hole at the lowest possible address?
1206 if (area
->vm_start
>= TASK_UNMAPPED_BASE
&&
1207 area
->vm_start
< area
->vm_mm
->free_area_cache
)
1208 area
->vm_mm
->free_area_cache
= area
->vm_start
;
1212 * This mmap-allocator allocates new areas top-down from below the
1213 * stack's low limit (the base):
1215 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1217 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
1218 const unsigned long len
, const unsigned long pgoff
,
1219 const unsigned long flags
)
1221 struct vm_area_struct
*vma
;
1222 struct mm_struct
*mm
= current
->mm
;
1223 unsigned long addr
= addr0
;
1225 /* requested length too big for entire address space */
1226 if (len
> TASK_SIZE
)
1229 /* requesting a specific address */
1231 addr
= PAGE_ALIGN(addr
);
1232 vma
= find_vma(mm
, addr
);
1233 if (TASK_SIZE
- len
>= addr
&&
1234 (!vma
|| addr
+ len
<= vma
->vm_start
))
1238 /* either no address requested or can't fit in requested address hole */
1239 addr
= mm
->free_area_cache
;
1241 /* make sure it can fit in the remaining address space */
1243 vma
= find_vma(mm
, addr
-len
);
1244 if (!vma
|| addr
<= vma
->vm_start
)
1245 /* remember the address as a hint for next time */
1246 return (mm
->free_area_cache
= addr
-len
);
1249 addr
= mm
->mmap_base
-len
;
1253 * Lookup failure means no vma is above this address,
1254 * else if new region fits below vma->vm_start,
1255 * return with success:
1257 vma
= find_vma(mm
, addr
);
1258 if (!vma
|| addr
+len
<= vma
->vm_start
)
1259 /* remember the address as a hint for next time */
1260 return (mm
->free_area_cache
= addr
);
1262 /* try just below the current vma->vm_start */
1263 addr
= vma
->vm_start
-len
;
1264 } while (len
<= vma
->vm_start
);
1267 * A failed mmap() very likely causes application failure,
1268 * so fall back to the bottom-up function here. This scenario
1269 * can happen with large stack limits and large mmap()
1272 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
1273 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
1275 * Restore the topdown base:
1277 mm
->free_area_cache
= mm
->mmap_base
;
1283 void arch_unmap_area_topdown(struct vm_area_struct
*area
)
1286 * Is this a new hole at the highest possible address?
1288 if (area
->vm_end
> area
->vm_mm
->free_area_cache
)
1289 area
->vm_mm
->free_area_cache
= area
->vm_end
;
1291 /* dont allow allocations above current base */
1292 if (area
->vm_mm
->free_area_cache
> area
->vm_mm
->mmap_base
)
1293 area
->vm_mm
->free_area_cache
= area
->vm_mm
->mmap_base
;
1297 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
1298 unsigned long pgoff
, unsigned long flags
)
1300 if (flags
& MAP_FIXED
) {
1303 if (addr
> TASK_SIZE
- len
)
1305 if (addr
& ~PAGE_MASK
)
1307 if (file
&& is_file_hugepages(file
)) {
1309 * Check if the given range is hugepage aligned, and
1310 * can be made suitable for hugepages.
1312 ret
= prepare_hugepage_range(addr
, len
);
1315 * Ensure that a normal request is not falling in a
1316 * reserved hugepage range. For some archs like IA-64,
1317 * there is a separate region for hugepages.
1319 ret
= is_hugepage_only_range(current
->mm
, addr
, len
);
1326 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
1327 return file
->f_op
->get_unmapped_area(file
, addr
, len
,
1330 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
1333 EXPORT_SYMBOL(get_unmapped_area
);
1335 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1336 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
1338 struct vm_area_struct
*vma
= NULL
;
1341 /* Check the cache first. */
1342 /* (Cache hit rate is typically around 35%.) */
1343 vma
= mm
->mmap_cache
;
1344 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
1345 struct rb_node
* rb_node
;
1347 rb_node
= mm
->mm_rb
.rb_node
;
1351 struct vm_area_struct
* vma_tmp
;
1353 vma_tmp
= rb_entry(rb_node
,
1354 struct vm_area_struct
, vm_rb
);
1356 if (vma_tmp
->vm_end
> addr
) {
1358 if (vma_tmp
->vm_start
<= addr
)
1360 rb_node
= rb_node
->rb_left
;
1362 rb_node
= rb_node
->rb_right
;
1365 mm
->mmap_cache
= vma
;
1371 EXPORT_SYMBOL(find_vma
);
1373 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1374 struct vm_area_struct
*
1375 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
1376 struct vm_area_struct
**pprev
)
1378 struct vm_area_struct
*vma
= NULL
, *prev
= NULL
;
1379 struct rb_node
* rb_node
;
1383 /* Guard against addr being lower than the first VMA */
1386 /* Go through the RB tree quickly. */
1387 rb_node
= mm
->mm_rb
.rb_node
;
1390 struct vm_area_struct
*vma_tmp
;
1391 vma_tmp
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
1393 if (addr
< vma_tmp
->vm_end
) {
1394 rb_node
= rb_node
->rb_left
;
1397 if (!prev
->vm_next
|| (addr
< prev
->vm_next
->vm_end
))
1399 rb_node
= rb_node
->rb_right
;
1405 return prev
? prev
->vm_next
: vma
;
1409 * Verify that the stack growth is acceptable and
1410 * update accounting. This is shared with both the
1411 * grow-up and grow-down cases.
1413 static int acct_stack_growth(struct vm_area_struct
* vma
, unsigned long size
, unsigned long grow
)
1415 struct mm_struct
*mm
= vma
->vm_mm
;
1416 struct rlimit
*rlim
= current
->signal
->rlim
;
1418 /* address space limit tests */
1419 if (mm
->total_vm
+ grow
> rlim
[RLIMIT_AS
].rlim_cur
>> PAGE_SHIFT
)
1422 /* Stack limit test */
1423 if (size
> rlim
[RLIMIT_STACK
].rlim_cur
)
1426 /* mlock limit tests */
1427 if (vma
->vm_flags
& VM_LOCKED
) {
1428 unsigned long locked
;
1429 unsigned long limit
;
1430 locked
= mm
->locked_vm
+ grow
;
1431 limit
= rlim
[RLIMIT_MEMLOCK
].rlim_cur
>> PAGE_SHIFT
;
1432 if (locked
> limit
&& !capable(CAP_IPC_LOCK
))
1437 * Overcommit.. This must be the final test, as it will
1438 * update security statistics.
1440 if (security_vm_enough_memory(grow
))
1443 /* Ok, everything looks good - let it rip */
1444 mm
->total_vm
+= grow
;
1445 if (vma
->vm_flags
& VM_LOCKED
)
1446 mm
->locked_vm
+= grow
;
1447 __vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, grow
);
1451 #ifdef CONFIG_STACK_GROWSUP
1453 * vma is the first one with address > vma->vm_end. Have to extend vma.
1455 int expand_stack(struct vm_area_struct
* vma
, unsigned long address
)
1459 if (!(vma
->vm_flags
& VM_GROWSUP
))
1463 * We must make sure the anon_vma is allocated
1464 * so that the anon_vma locking is not a noop.
1466 if (unlikely(anon_vma_prepare(vma
)))
1471 * vma->vm_start/vm_end cannot change under us because the caller
1472 * is required to hold the mmap_sem in read mode. We need the
1473 * anon_vma lock to serialize against concurrent expand_stacks.
1475 address
+= 4 + PAGE_SIZE
- 1;
1476 address
&= PAGE_MASK
;
1479 /* Somebody else might have raced and expanded it already */
1480 if (address
> vma
->vm_end
) {
1481 unsigned long size
, grow
;
1483 size
= address
- vma
->vm_start
;
1484 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
1486 error
= acct_stack_growth(vma
, size
, grow
);
1488 vma
->vm_end
= address
;
1490 anon_vma_unlock(vma
);
1494 struct vm_area_struct
*
1495 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
1497 struct vm_area_struct
*vma
, *prev
;
1500 vma
= find_vma_prev(mm
, addr
, &prev
);
1501 if (vma
&& (vma
->vm_start
<= addr
))
1503 if (!prev
|| expand_stack(prev
, addr
))
1505 if (prev
->vm_flags
& VM_LOCKED
) {
1506 make_pages_present(addr
, prev
->vm_end
);
1512 * vma is the first one with address < vma->vm_start. Have to extend vma.
1514 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
1519 * We must make sure the anon_vma is allocated
1520 * so that the anon_vma locking is not a noop.
1522 if (unlikely(anon_vma_prepare(vma
)))
1527 * vma->vm_start/vm_end cannot change under us because the caller
1528 * is required to hold the mmap_sem in read mode. We need the
1529 * anon_vma lock to serialize against concurrent expand_stacks.
1531 address
&= PAGE_MASK
;
1534 /* Somebody else might have raced and expanded it already */
1535 if (address
< vma
->vm_start
) {
1536 unsigned long size
, grow
;
1538 size
= vma
->vm_end
- address
;
1539 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
1541 error
= acct_stack_growth(vma
, size
, grow
);
1543 vma
->vm_start
= address
;
1544 vma
->vm_pgoff
-= grow
;
1547 anon_vma_unlock(vma
);
1551 struct vm_area_struct
*
1552 find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
1554 struct vm_area_struct
* vma
;
1555 unsigned long start
;
1558 vma
= find_vma(mm
,addr
);
1561 if (vma
->vm_start
<= addr
)
1563 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
1565 start
= vma
->vm_start
;
1566 if (expand_stack(vma
, addr
))
1568 if (vma
->vm_flags
& VM_LOCKED
) {
1569 make_pages_present(addr
, start
);
1576 * Try to free as many page directory entries as we can,
1577 * without having to work very hard at actually scanning
1578 * the page tables themselves.
1580 * Right now we try to free page tables if we have a nice
1581 * PGDIR-aligned area that got free'd up. We could be more
1582 * granular if we want to, but this is fast and simple,
1583 * and covers the bad cases.
1585 * "prev", if it exists, points to a vma before the one
1586 * we just free'd - but there's no telling how much before.
1588 static void free_pgtables(struct mmu_gather
*tlb
, struct vm_area_struct
*prev
,
1589 unsigned long start
, unsigned long end
)
1591 unsigned long first
= start
& PGDIR_MASK
;
1592 unsigned long last
= end
+ PGDIR_SIZE
- 1;
1593 struct mm_struct
*mm
= tlb
->mm
;
1595 if (last
> MM_VM_SIZE(mm
) || last
< end
)
1596 last
= MM_VM_SIZE(mm
);
1602 if (prev
->vm_end
> start
) {
1603 if (last
> prev
->vm_start
)
1604 last
= prev
->vm_start
;
1609 struct vm_area_struct
*next
= prev
->vm_next
;
1612 if (next
->vm_start
< start
) {
1616 if (last
> next
->vm_start
)
1617 last
= next
->vm_start
;
1619 if (prev
->vm_end
> first
)
1620 first
= prev
->vm_end
;
1624 if (last
< first
) /* for arches with discontiguous pgd indices */
1626 if (first
< FIRST_USER_PGD_NR
* PGDIR_SIZE
)
1627 first
= FIRST_USER_PGD_NR
* PGDIR_SIZE
;
1628 /* No point trying to free anything if we're in the same pte page */
1629 if ((first
& PMD_MASK
) < (last
& PMD_MASK
)) {
1630 clear_page_range(tlb
, first
, last
);
1631 flush_tlb_pgtables(mm
, first
, last
);
1635 /* Normal function to fix up a mapping
1636 * This function is the default for when an area has no specific
1637 * function. This may be used as part of a more specific routine.
1639 * By the time this function is called, the area struct has been
1640 * removed from the process mapping list.
1642 static void unmap_vma(struct mm_struct
*mm
, struct vm_area_struct
*area
)
1644 size_t len
= area
->vm_end
- area
->vm_start
;
1646 area
->vm_mm
->total_vm
-= len
>> PAGE_SHIFT
;
1647 if (area
->vm_flags
& VM_LOCKED
)
1648 area
->vm_mm
->locked_vm
-= len
>> PAGE_SHIFT
;
1649 vm_stat_unaccount(area
);
1650 area
->vm_mm
->unmap_area(area
);
1651 remove_vm_struct(area
);
1655 * Update the VMA and inode share lists.
1657 * Ok - we have the memory areas we should free on the 'free' list,
1658 * so release them, and do the vma updates.
1660 static void unmap_vma_list(struct mm_struct
*mm
,
1661 struct vm_area_struct
*mpnt
)
1664 struct vm_area_struct
*next
= mpnt
->vm_next
;
1665 unmap_vma(mm
, mpnt
);
1667 } while (mpnt
!= NULL
);
1672 * Get rid of page table information in the indicated region.
1674 * Called with the page table lock held.
1676 static void unmap_region(struct mm_struct
*mm
,
1677 struct vm_area_struct
*vma
,
1678 struct vm_area_struct
*prev
,
1679 unsigned long start
,
1682 struct mmu_gather
*tlb
;
1683 unsigned long nr_accounted
= 0;
1686 tlb
= tlb_gather_mmu(mm
, 0);
1687 unmap_vmas(&tlb
, mm
, vma
, start
, end
, &nr_accounted
, NULL
);
1688 vm_unacct_memory(nr_accounted
);
1690 if (is_hugepage_only_range(mm
, start
, end
- start
))
1691 hugetlb_free_pgtables(tlb
, prev
, start
, end
);
1693 free_pgtables(tlb
, prev
, start
, end
);
1694 tlb_finish_mmu(tlb
, start
, end
);
1698 * Create a list of vma's touched by the unmap, removing them from the mm's
1699 * vma list as we go..
1702 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1703 struct vm_area_struct
*prev
, unsigned long end
)
1705 struct vm_area_struct
**insertion_point
;
1706 struct vm_area_struct
*tail_vma
= NULL
;
1708 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
1710 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
1714 } while (vma
&& vma
->vm_start
< end
);
1715 *insertion_point
= vma
;
1716 tail_vma
->vm_next
= NULL
;
1717 mm
->mmap_cache
= NULL
; /* Kill the cache. */
1721 * Split a vma into two pieces at address 'addr', a new vma is allocated
1722 * either for the first part or the the tail.
1724 int split_vma(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
1725 unsigned long addr
, int new_below
)
1727 struct mempolicy
*pol
;
1728 struct vm_area_struct
*new;
1730 if (is_vm_hugetlb_page(vma
) && (addr
& ~HPAGE_MASK
))
1733 if (mm
->map_count
>= sysctl_max_map_count
)
1736 new = kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1740 /* most fields are the same, copy all, and then fixup */
1746 new->vm_start
= addr
;
1747 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
1750 pol
= mpol_copy(vma_policy(vma
));
1752 kmem_cache_free(vm_area_cachep
, new);
1753 return PTR_ERR(pol
);
1755 vma_set_policy(new, pol
);
1758 get_file(new->vm_file
);
1760 if (new->vm_ops
&& new->vm_ops
->open
)
1761 new->vm_ops
->open(new);
1764 vma_adjust(vma
, addr
, vma
->vm_end
, vma
->vm_pgoff
+
1765 ((addr
- new->vm_start
) >> PAGE_SHIFT
), new);
1767 vma_adjust(vma
, vma
->vm_start
, addr
, vma
->vm_pgoff
, new);
1772 /* Munmap is split into 2 main parts -- this part which finds
1773 * what needs doing, and the areas themselves, which do the
1774 * work. This now handles partial unmappings.
1775 * Jeremy Fitzhardinge <jeremy@goop.org>
1777 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1780 struct vm_area_struct
*mpnt
, *prev
, *last
;
1782 if ((start
& ~PAGE_MASK
) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
1785 if ((len
= PAGE_ALIGN(len
)) == 0)
1788 /* Find the first overlapping VMA */
1789 mpnt
= find_vma_prev(mm
, start
, &prev
);
1792 /* we have start < mpnt->vm_end */
1794 /* if it doesn't overlap, we have nothing.. */
1796 if (mpnt
->vm_start
>= end
)
1800 * If we need to split any vma, do it now to save pain later.
1802 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1803 * unmapped vm_area_struct will remain in use: so lower split_vma
1804 * places tmp vma above, and higher split_vma places tmp vma below.
1806 if (start
> mpnt
->vm_start
) {
1807 int error
= split_vma(mm
, mpnt
, start
, 0);
1813 /* Does it split the last one? */
1814 last
= find_vma(mm
, end
);
1815 if (last
&& end
> last
->vm_start
) {
1816 int error
= split_vma(mm
, last
, end
, 1);
1820 mpnt
= prev
? prev
->vm_next
: mm
->mmap
;
1823 * Remove the vma's, and unmap the actual pages
1825 detach_vmas_to_be_unmapped(mm
, mpnt
, prev
, end
);
1826 spin_lock(&mm
->page_table_lock
);
1827 unmap_region(mm
, mpnt
, prev
, start
, end
);
1828 spin_unlock(&mm
->page_table_lock
);
1830 /* Fix up all other VM information */
1831 unmap_vma_list(mm
, mpnt
);
1836 EXPORT_SYMBOL(do_munmap
);
1838 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
1841 struct mm_struct
*mm
= current
->mm
;
1843 profile_munmap(addr
);
1845 down_write(&mm
->mmap_sem
);
1846 ret
= do_munmap(mm
, addr
, len
);
1847 up_write(&mm
->mmap_sem
);
1851 static inline void verify_mm_writelocked(struct mm_struct
*mm
)
1853 #ifdef CONFIG_DEBUG_KERNEL
1854 if (unlikely(down_read_trylock(&mm
->mmap_sem
))) {
1856 up_read(&mm
->mmap_sem
);
1862 * this is really a simplified "do_mmap". it only handles
1863 * anonymous maps. eventually we may be able to do some
1864 * brk-specific accounting here.
1866 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1868 struct mm_struct
* mm
= current
->mm
;
1869 struct vm_area_struct
* vma
, * prev
;
1870 unsigned long flags
;
1871 struct rb_node
** rb_link
, * rb_parent
;
1872 pgoff_t pgoff
= addr
>> PAGE_SHIFT
;
1874 len
= PAGE_ALIGN(len
);
1878 if ((addr
+ len
) > TASK_SIZE
|| (addr
+ len
) < addr
)
1884 if (mm
->def_flags
& VM_LOCKED
) {
1885 unsigned long locked
, lock_limit
;
1886 locked
= mm
->locked_vm
<< PAGE_SHIFT
;
1887 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1889 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
1894 * mm->mmap_sem is required to protect against another thread
1895 * changing the mappings in case we sleep.
1897 verify_mm_writelocked(mm
);
1900 * Clear old maps. this also does some error checking for us
1903 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
1904 if (vma
&& vma
->vm_start
< addr
+ len
) {
1905 if (do_munmap(mm
, addr
, len
))
1910 /* Check against address space limits *after* clearing old maps... */
1911 if ((mm
->total_vm
<< PAGE_SHIFT
) + len
1912 > current
->signal
->rlim
[RLIMIT_AS
].rlim_cur
)
1915 if (mm
->map_count
> sysctl_max_map_count
)
1918 if (security_vm_enough_memory(len
>> PAGE_SHIFT
))
1921 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
1923 /* Can we just expand an old private anonymous mapping? */
1924 if (vma_merge(mm
, prev
, addr
, addr
+ len
, flags
,
1925 NULL
, NULL
, pgoff
, NULL
))
1929 * create a vma struct for an anonymous mapping
1931 vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1933 vm_unacct_memory(len
>> PAGE_SHIFT
);
1936 memset(vma
, 0, sizeof(*vma
));
1939 vma
->vm_start
= addr
;
1940 vma
->vm_end
= addr
+ len
;
1941 vma
->vm_pgoff
= pgoff
;
1942 vma
->vm_flags
= flags
;
1943 vma
->vm_page_prot
= protection_map
[flags
& 0x0f];
1944 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1946 mm
->total_vm
+= len
>> PAGE_SHIFT
;
1947 if (flags
& VM_LOCKED
) {
1948 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
1949 make_pages_present(addr
, addr
+ len
);
1954 EXPORT_SYMBOL(do_brk
);
1956 /* Release all mmaps. */
1957 void exit_mmap(struct mm_struct
*mm
)
1959 struct mmu_gather
*tlb
;
1960 struct vm_area_struct
*vma
;
1961 unsigned long nr_accounted
= 0;
1965 spin_lock(&mm
->page_table_lock
);
1967 tlb
= tlb_gather_mmu(mm
, 1);
1969 /* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1970 mm
->map_count
-= unmap_vmas(&tlb
, mm
, mm
->mmap
, 0,
1971 ~0UL, &nr_accounted
, NULL
);
1972 vm_unacct_memory(nr_accounted
);
1973 BUG_ON(mm
->map_count
); /* This is just debugging */
1974 clear_page_range(tlb
, FIRST_USER_PGD_NR
* PGDIR_SIZE
, MM_VM_SIZE(mm
));
1976 tlb_finish_mmu(tlb
, 0, MM_VM_SIZE(mm
));
1979 mm
->mmap
= mm
->mmap_cache
= NULL
;
1980 mm
->mm_rb
= RB_ROOT
;
1981 set_mm_counter(mm
, rss
, 0);
1985 spin_unlock(&mm
->page_table_lock
);
1988 * Walk the list again, actually closing and freeing it
1989 * without holding any MM locks.
1992 struct vm_area_struct
*next
= vma
->vm_next
;
1993 remove_vm_struct(vma
);
1998 /* Insert vm structure into process list sorted by address
1999 * and into the inode's i_mmap tree. If vm_file is non-NULL
2000 * then i_mmap_lock is taken here.
2002 int insert_vm_struct(struct mm_struct
* mm
, struct vm_area_struct
* vma
)
2004 struct vm_area_struct
* __vma
, * prev
;
2005 struct rb_node
** rb_link
, * rb_parent
;
2008 * The vm_pgoff of a purely anonymous vma should be irrelevant
2009 * until its first write fault, when page's anon_vma and index
2010 * are set. But now set the vm_pgoff it will almost certainly
2011 * end up with (unless mremap moves it elsewhere before that
2012 * first wfault), so /proc/pid/maps tells a consistent story.
2014 * By setting it to reflect the virtual start address of the
2015 * vma, merges and splits can happen in a seamless way, just
2016 * using the existing file pgoff checks and manipulations.
2017 * Similarly in do_mmap_pgoff and in do_brk.
2019 if (!vma
->vm_file
) {
2020 BUG_ON(vma
->anon_vma
);
2021 vma
->vm_pgoff
= vma
->vm_start
>> PAGE_SHIFT
;
2023 __vma
= find_vma_prepare(mm
,vma
->vm_start
,&prev
,&rb_link
,&rb_parent
);
2024 if (__vma
&& __vma
->vm_start
< vma
->vm_end
)
2026 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2031 * Copy the vma structure to a new location in the same mm,
2032 * prior to moving page table entries, to effect an mremap move.
2034 struct vm_area_struct
*copy_vma(struct vm_area_struct
**vmap
,
2035 unsigned long addr
, unsigned long len
, pgoff_t pgoff
)
2037 struct vm_area_struct
*vma
= *vmap
;
2038 unsigned long vma_start
= vma
->vm_start
;
2039 struct mm_struct
*mm
= vma
->vm_mm
;
2040 struct vm_area_struct
*new_vma
, *prev
;
2041 struct rb_node
**rb_link
, *rb_parent
;
2042 struct mempolicy
*pol
;
2045 * If anonymous vma has not yet been faulted, update new pgoff
2046 * to match new location, to increase its chance of merging.
2048 if (!vma
->vm_file
&& !vma
->anon_vma
)
2049 pgoff
= addr
>> PAGE_SHIFT
;
2051 find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
2052 new_vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vma
->vm_flags
,
2053 vma
->anon_vma
, vma
->vm_file
, pgoff
, vma_policy(vma
));
2056 * Source vma may have been merged into new_vma
2058 if (vma_start
>= new_vma
->vm_start
&&
2059 vma_start
< new_vma
->vm_end
)
2062 new_vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
2065 pol
= mpol_copy(vma_policy(vma
));
2067 kmem_cache_free(vm_area_cachep
, new_vma
);
2070 vma_set_policy(new_vma
, pol
);
2071 new_vma
->vm_start
= addr
;
2072 new_vma
->vm_end
= addr
+ len
;
2073 new_vma
->vm_pgoff
= pgoff
;
2074 if (new_vma
->vm_file
)
2075 get_file(new_vma
->vm_file
);
2076 if (new_vma
->vm_ops
&& new_vma
->vm_ops
->open
)
2077 new_vma
->vm_ops
->open(new_vma
);
2078 vma_link(mm
, new_vma
, prev
, rb_link
, rb_parent
);