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/capability.h>
17 #include <linux/init.h>
18 #include <linux/file.h>
20 #include <linux/personality.h>
21 #include <linux/security.h>
22 #include <linux/hugetlb.h>
23 #include <linux/profile.h>
24 #include <linux/module.h>
25 #include <linux/mount.h>
26 #include <linux/mempolicy.h>
27 #include <linux/rmap.h>
29 #include <asm/uaccess.h>
30 #include <asm/cacheflush.h>
33 static void unmap_region(struct mm_struct
*mm
,
34 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
35 unsigned long start
, unsigned long end
);
38 * WARNING: the debugging will use recursive algorithms so never enable this
39 * unless you know what you are doing.
43 /* description of effects of mapping type and prot in current implementation.
44 * this is due to the limited x86 page protection hardware. The expected
45 * behavior is in parens:
48 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
49 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
50 * w: (no) no w: (no) no w: (yes) yes w: (no) no
51 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
53 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
54 * w: (no) no w: (no) no w: (copy) copy w: (no) no
55 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
58 pgprot_t protection_map
[16] = {
59 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
60 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
63 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
64 int sysctl_overcommit_ratio
= 50; /* default is 50% */
65 int sysctl_max_map_count __read_mostly
= DEFAULT_MAX_MAP_COUNT
;
66 atomic_t vm_committed_space
= ATOMIC_INIT(0);
69 * Check that a process has enough memory to allocate a new virtual
70 * mapping. 0 means there is enough memory for the allocation to
71 * succeed and -ENOMEM implies there is not.
73 * We currently support three overcommit policies, which are set via the
74 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
76 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
77 * Additional code 2002 Jul 20 by Robert Love.
79 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
81 * Note this is a helper function intended to be used by LSMs which
82 * wish to use this logic.
84 int __vm_enough_memory(long pages
, int cap_sys_admin
)
86 unsigned long free
, allowed
;
88 vm_acct_memory(pages
);
91 * Sometimes we want to use more memory than we have
93 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
96 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
99 free
= get_page_cache_size();
100 free
+= nr_swap_pages
;
103 * Any slabs which are created with the
104 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
105 * which are reclaimable, under pressure. The dentry
106 * cache and most inode caches should fall into this
108 free
+= atomic_read(&slab_reclaim_pages
);
111 * Leave the last 3% for root
120 * nr_free_pages() is very expensive on large systems,
121 * only call if we're about to fail.
130 vm_unacct_memory(pages
);
134 allowed
= (totalram_pages
- hugetlb_total_pages())
135 * sysctl_overcommit_ratio
/ 100;
137 * Leave the last 3% for root
140 allowed
-= allowed
/ 32;
141 allowed
+= total_swap_pages
;
143 /* Don't let a single process grow too big:
144 leave 3% of the size of this process for other processes */
145 allowed
-= current
->mm
->total_vm
/ 32;
148 * cast `allowed' as a signed long because vm_committed_space
149 * sometimes has a negative value
151 if (atomic_read(&vm_committed_space
) < (long)allowed
)
154 vm_unacct_memory(pages
);
159 EXPORT_SYMBOL(__vm_enough_memory
);
162 * Requires inode->i_mapping->i_mmap_lock
164 static void __remove_shared_vm_struct(struct vm_area_struct
*vma
,
165 struct file
*file
, struct address_space
*mapping
)
167 if (vma
->vm_flags
& VM_DENYWRITE
)
168 atomic_inc(&file
->f_dentry
->d_inode
->i_writecount
);
169 if (vma
->vm_flags
& VM_SHARED
)
170 mapping
->i_mmap_writable
--;
172 flush_dcache_mmap_lock(mapping
);
173 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
))
174 list_del_init(&vma
->shared
.vm_set
.list
);
176 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
177 flush_dcache_mmap_unlock(mapping
);
181 * Unlink a file-based vm structure from its prio_tree, to hide
182 * vma from rmap and vmtruncate before freeing its page tables.
184 void unlink_file_vma(struct vm_area_struct
*vma
)
186 struct file
*file
= vma
->vm_file
;
189 struct address_space
*mapping
= file
->f_mapping
;
190 spin_lock(&mapping
->i_mmap_lock
);
191 __remove_shared_vm_struct(vma
, file
, mapping
);
192 spin_unlock(&mapping
->i_mmap_lock
);
197 * Close a vm structure and free it, returning the next.
199 static struct vm_area_struct
*remove_vma(struct vm_area_struct
*vma
)
201 struct vm_area_struct
*next
= vma
->vm_next
;
204 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
205 vma
->vm_ops
->close(vma
);
208 mpol_free(vma_policy(vma
));
209 kmem_cache_free(vm_area_cachep
, vma
);
213 asmlinkage
unsigned long sys_brk(unsigned long brk
)
215 unsigned long rlim
, retval
;
216 unsigned long newbrk
, oldbrk
;
217 struct mm_struct
*mm
= current
->mm
;
219 down_write(&mm
->mmap_sem
);
221 if (brk
< mm
->end_code
)
223 newbrk
= PAGE_ALIGN(brk
);
224 oldbrk
= PAGE_ALIGN(mm
->brk
);
225 if (oldbrk
== newbrk
)
228 /* Always allow shrinking brk. */
229 if (brk
<= mm
->brk
) {
230 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
235 /* Check against rlimit.. */
236 rlim
= current
->signal
->rlim
[RLIMIT_DATA
].rlim_cur
;
237 if (rlim
< RLIM_INFINITY
&& brk
- mm
->start_data
> rlim
)
240 /* Check against existing mmap mappings. */
241 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
244 /* Ok, looks good - let it rip. */
245 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
251 up_write(&mm
->mmap_sem
);
256 static int browse_rb(struct rb_root
*root
)
259 struct rb_node
*nd
, *pn
= NULL
;
260 unsigned long prev
= 0, pend
= 0;
262 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
263 struct vm_area_struct
*vma
;
264 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
265 if (vma
->vm_start
< prev
)
266 printk("vm_start %lx prev %lx\n", vma
->vm_start
, prev
), i
= -1;
267 if (vma
->vm_start
< pend
)
268 printk("vm_start %lx pend %lx\n", vma
->vm_start
, pend
);
269 if (vma
->vm_start
> vma
->vm_end
)
270 printk("vm_end %lx < vm_start %lx\n", vma
->vm_end
, vma
->vm_start
);
275 for (nd
= pn
; nd
; nd
= rb_prev(nd
)) {
279 printk("backwards %d, forwards %d\n", j
, i
), i
= 0;
283 void validate_mm(struct mm_struct
*mm
)
287 struct vm_area_struct
*tmp
= mm
->mmap
;
292 if (i
!= mm
->map_count
)
293 printk("map_count %d vm_next %d\n", mm
->map_count
, i
), bug
= 1;
294 i
= browse_rb(&mm
->mm_rb
);
295 if (i
!= mm
->map_count
)
296 printk("map_count %d rb %d\n", mm
->map_count
, i
), bug
= 1;
301 #define validate_mm(mm) do { } while (0)
304 static struct vm_area_struct
*
305 find_vma_prepare(struct mm_struct
*mm
, unsigned long addr
,
306 struct vm_area_struct
**pprev
, struct rb_node
***rb_link
,
307 struct rb_node
** rb_parent
)
309 struct vm_area_struct
* vma
;
310 struct rb_node
** __rb_link
, * __rb_parent
, * rb_prev
;
312 __rb_link
= &mm
->mm_rb
.rb_node
;
313 rb_prev
= __rb_parent
= NULL
;
317 struct vm_area_struct
*vma_tmp
;
319 __rb_parent
= *__rb_link
;
320 vma_tmp
= rb_entry(__rb_parent
, struct vm_area_struct
, vm_rb
);
322 if (vma_tmp
->vm_end
> addr
) {
324 if (vma_tmp
->vm_start
<= addr
)
326 __rb_link
= &__rb_parent
->rb_left
;
328 rb_prev
= __rb_parent
;
329 __rb_link
= &__rb_parent
->rb_right
;
335 *pprev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
336 *rb_link
= __rb_link
;
337 *rb_parent
= __rb_parent
;
342 __vma_link_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
343 struct vm_area_struct
*prev
, struct rb_node
*rb_parent
)
346 vma
->vm_next
= prev
->vm_next
;
351 vma
->vm_next
= rb_entry(rb_parent
,
352 struct vm_area_struct
, vm_rb
);
358 void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
359 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
361 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
362 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
365 static inline void __vma_link_file(struct vm_area_struct
*vma
)
371 struct address_space
*mapping
= file
->f_mapping
;
373 if (vma
->vm_flags
& VM_DENYWRITE
)
374 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
375 if (vma
->vm_flags
& VM_SHARED
)
376 mapping
->i_mmap_writable
++;
378 flush_dcache_mmap_lock(mapping
);
379 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
))
380 vma_nonlinear_insert(vma
, &mapping
->i_mmap_nonlinear
);
382 vma_prio_tree_insert(vma
, &mapping
->i_mmap
);
383 flush_dcache_mmap_unlock(mapping
);
388 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
389 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
390 struct rb_node
*rb_parent
)
392 __vma_link_list(mm
, vma
, prev
, rb_parent
);
393 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
394 __anon_vma_link(vma
);
397 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
398 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
399 struct rb_node
*rb_parent
)
401 struct address_space
*mapping
= NULL
;
404 mapping
= vma
->vm_file
->f_mapping
;
407 spin_lock(&mapping
->i_mmap_lock
);
408 vma
->vm_truncate_count
= mapping
->truncate_count
;
412 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
413 __vma_link_file(vma
);
415 anon_vma_unlock(vma
);
417 spin_unlock(&mapping
->i_mmap_lock
);
424 * Helper for vma_adjust in the split_vma insert case:
425 * insert vm structure into list and rbtree and anon_vma,
426 * but it has already been inserted into prio_tree earlier.
429 __insert_vm_struct(struct mm_struct
* mm
, struct vm_area_struct
* vma
)
431 struct vm_area_struct
* __vma
, * prev
;
432 struct rb_node
** rb_link
, * rb_parent
;
434 __vma
= find_vma_prepare(mm
, vma
->vm_start
,&prev
, &rb_link
, &rb_parent
);
435 if (__vma
&& __vma
->vm_start
< vma
->vm_end
)
437 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
442 __vma_unlink(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
443 struct vm_area_struct
*prev
)
445 prev
->vm_next
= vma
->vm_next
;
446 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
447 if (mm
->mmap_cache
== vma
)
448 mm
->mmap_cache
= prev
;
452 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
453 * is already present in an i_mmap tree without adjusting the tree.
454 * The following helper function should be used when such adjustments
455 * are necessary. The "insert" vma (if any) is to be inserted
456 * before we drop the necessary locks.
458 void vma_adjust(struct vm_area_struct
*vma
, unsigned long start
,
459 unsigned long end
, pgoff_t pgoff
, struct vm_area_struct
*insert
)
461 struct mm_struct
*mm
= vma
->vm_mm
;
462 struct vm_area_struct
*next
= vma
->vm_next
;
463 struct vm_area_struct
*importer
= NULL
;
464 struct address_space
*mapping
= NULL
;
465 struct prio_tree_root
*root
= NULL
;
466 struct file
*file
= vma
->vm_file
;
467 struct anon_vma
*anon_vma
= NULL
;
468 long adjust_next
= 0;
471 if (next
&& !insert
) {
472 if (end
>= next
->vm_end
) {
474 * vma expands, overlapping all the next, and
475 * perhaps the one after too (mprotect case 6).
477 again
: remove_next
= 1 + (end
> next
->vm_end
);
479 anon_vma
= next
->anon_vma
;
481 } else if (end
> next
->vm_start
) {
483 * vma expands, overlapping part of the next:
484 * mprotect case 5 shifting the boundary up.
486 adjust_next
= (end
- next
->vm_start
) >> PAGE_SHIFT
;
487 anon_vma
= next
->anon_vma
;
489 } else if (end
< vma
->vm_end
) {
491 * vma shrinks, and !insert tells it's not
492 * split_vma inserting another: so it must be
493 * mprotect case 4 shifting the boundary down.
495 adjust_next
= - ((vma
->vm_end
- end
) >> PAGE_SHIFT
);
496 anon_vma
= next
->anon_vma
;
502 mapping
= file
->f_mapping
;
503 if (!(vma
->vm_flags
& VM_NONLINEAR
))
504 root
= &mapping
->i_mmap
;
505 spin_lock(&mapping
->i_mmap_lock
);
507 vma
->vm_truncate_count
!= next
->vm_truncate_count
) {
509 * unmap_mapping_range might be in progress:
510 * ensure that the expanding vma is rescanned.
512 importer
->vm_truncate_count
= 0;
515 insert
->vm_truncate_count
= vma
->vm_truncate_count
;
517 * Put into prio_tree now, so instantiated pages
518 * are visible to arm/parisc __flush_dcache_page
519 * throughout; but we cannot insert into address
520 * space until vma start or end is updated.
522 __vma_link_file(insert
);
527 * When changing only vma->vm_end, we don't really need
528 * anon_vma lock: but is that case worth optimizing out?
531 anon_vma
= vma
->anon_vma
;
533 spin_lock(&anon_vma
->lock
);
535 * Easily overlooked: when mprotect shifts the boundary,
536 * make sure the expanding vma has anon_vma set if the
537 * shrinking vma had, to cover any anon pages imported.
539 if (importer
&& !importer
->anon_vma
) {
540 importer
->anon_vma
= anon_vma
;
541 __anon_vma_link(importer
);
546 flush_dcache_mmap_lock(mapping
);
547 vma_prio_tree_remove(vma
, root
);
549 vma_prio_tree_remove(next
, root
);
552 vma
->vm_start
= start
;
554 vma
->vm_pgoff
= pgoff
;
556 next
->vm_start
+= adjust_next
<< PAGE_SHIFT
;
557 next
->vm_pgoff
+= adjust_next
;
562 vma_prio_tree_insert(next
, root
);
563 vma_prio_tree_insert(vma
, root
);
564 flush_dcache_mmap_unlock(mapping
);
569 * vma_merge has merged next into vma, and needs
570 * us to remove next before dropping the locks.
572 __vma_unlink(mm
, next
, vma
);
574 __remove_shared_vm_struct(next
, file
, mapping
);
576 __anon_vma_merge(vma
, next
);
579 * split_vma has split insert from vma, and needs
580 * us to insert it before dropping the locks
581 * (it may either follow vma or precede it).
583 __insert_vm_struct(mm
, insert
);
587 spin_unlock(&anon_vma
->lock
);
589 spin_unlock(&mapping
->i_mmap_lock
);
595 mpol_free(vma_policy(next
));
596 kmem_cache_free(vm_area_cachep
, next
);
598 * In mprotect's case 6 (see comments on vma_merge),
599 * we must remove another next too. It would clutter
600 * up the code too much to do both in one go.
602 if (remove_next
== 2) {
612 * If the vma has a ->close operation then the driver probably needs to release
613 * per-vma resources, so we don't attempt to merge those.
615 #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP)
617 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
618 struct file
*file
, unsigned long vm_flags
)
620 if (vma
->vm_flags
!= vm_flags
)
622 if (vma
->vm_file
!= file
)
624 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
629 static inline int is_mergeable_anon_vma(struct anon_vma
*anon_vma1
,
630 struct anon_vma
*anon_vma2
)
632 return !anon_vma1
|| !anon_vma2
|| (anon_vma1
== anon_vma2
);
636 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
637 * in front of (at a lower virtual address and file offset than) the vma.
639 * We cannot merge two vmas if they have differently assigned (non-NULL)
640 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
642 * We don't check here for the merged mmap wrapping around the end of pagecache
643 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
644 * wrap, nor mmaps which cover the final page at index -1UL.
647 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
648 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
650 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
651 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
)) {
652 if (vma
->vm_pgoff
== vm_pgoff
)
659 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
660 * beyond (at a higher virtual address and file offset than) the vma.
662 * We cannot merge two vmas if they have differently assigned (non-NULL)
663 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
666 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
667 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
669 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
670 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
)) {
672 vm_pglen
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
673 if (vma
->vm_pgoff
+ vm_pglen
== vm_pgoff
)
680 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
681 * whether that can be merged with its predecessor or its successor.
682 * Or both (it neatly fills a hole).
684 * In most cases - when called for mmap, brk or mremap - [addr,end) is
685 * certain not to be mapped by the time vma_merge is called; but when
686 * called for mprotect, it is certain to be already mapped (either at
687 * an offset within prev, or at the start of next), and the flags of
688 * this area are about to be changed to vm_flags - and the no-change
689 * case has already been eliminated.
691 * The following mprotect cases have to be considered, where AAAA is
692 * the area passed down from mprotect_fixup, never extending beyond one
693 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
695 * AAAA AAAA AAAA AAAA
696 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
697 * cannot merge might become might become might become
698 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
699 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
700 * mremap move: PPPPNNNNNNNN 8
702 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
703 * might become case 1 below case 2 below case 3 below
705 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
706 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
708 struct vm_area_struct
*vma_merge(struct mm_struct
*mm
,
709 struct vm_area_struct
*prev
, unsigned long addr
,
710 unsigned long end
, unsigned long vm_flags
,
711 struct anon_vma
*anon_vma
, struct file
*file
,
712 pgoff_t pgoff
, struct mempolicy
*policy
)
714 pgoff_t pglen
= (end
- addr
) >> PAGE_SHIFT
;
715 struct vm_area_struct
*area
, *next
;
718 * We later require that vma->vm_flags == vm_flags,
719 * so this tests vma->vm_flags & VM_SPECIAL, too.
721 if (vm_flags
& VM_SPECIAL
)
725 next
= prev
->vm_next
;
729 if (next
&& next
->vm_end
== end
) /* cases 6, 7, 8 */
730 next
= next
->vm_next
;
733 * Can it merge with the predecessor?
735 if (prev
&& prev
->vm_end
== addr
&&
736 mpol_equal(vma_policy(prev
), policy
) &&
737 can_vma_merge_after(prev
, vm_flags
,
738 anon_vma
, file
, pgoff
)) {
740 * OK, it can. Can we now merge in the successor as well?
742 if (next
&& end
== next
->vm_start
&&
743 mpol_equal(policy
, vma_policy(next
)) &&
744 can_vma_merge_before(next
, vm_flags
,
745 anon_vma
, file
, pgoff
+pglen
) &&
746 is_mergeable_anon_vma(prev
->anon_vma
,
749 vma_adjust(prev
, prev
->vm_start
,
750 next
->vm_end
, prev
->vm_pgoff
, NULL
);
751 } else /* cases 2, 5, 7 */
752 vma_adjust(prev
, prev
->vm_start
,
753 end
, prev
->vm_pgoff
, NULL
);
758 * Can this new request be merged in front of next?
760 if (next
&& end
== next
->vm_start
&&
761 mpol_equal(policy
, vma_policy(next
)) &&
762 can_vma_merge_before(next
, vm_flags
,
763 anon_vma
, file
, pgoff
+pglen
)) {
764 if (prev
&& addr
< prev
->vm_end
) /* case 4 */
765 vma_adjust(prev
, prev
->vm_start
,
766 addr
, prev
->vm_pgoff
, NULL
);
767 else /* cases 3, 8 */
768 vma_adjust(area
, addr
, next
->vm_end
,
769 next
->vm_pgoff
- pglen
, NULL
);
777 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
778 * neighbouring vmas for a suitable anon_vma, before it goes off
779 * to allocate a new anon_vma. It checks because a repetitive
780 * sequence of mprotects and faults may otherwise lead to distinct
781 * anon_vmas being allocated, preventing vma merge in subsequent
784 struct anon_vma
*find_mergeable_anon_vma(struct vm_area_struct
*vma
)
786 struct vm_area_struct
*near
;
787 unsigned long vm_flags
;
794 * Since only mprotect tries to remerge vmas, match flags
795 * which might be mprotected into each other later on.
796 * Neither mlock nor madvise tries to remerge at present,
797 * so leave their flags as obstructing a merge.
799 vm_flags
= vma
->vm_flags
& ~(VM_READ
|VM_WRITE
|VM_EXEC
);
800 vm_flags
|= near
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
);
802 if (near
->anon_vma
&& vma
->vm_end
== near
->vm_start
&&
803 mpol_equal(vma_policy(vma
), vma_policy(near
)) &&
804 can_vma_merge_before(near
, vm_flags
,
805 NULL
, vma
->vm_file
, vma
->vm_pgoff
+
806 ((vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
)))
807 return near
->anon_vma
;
810 * It is potentially slow to have to call find_vma_prev here.
811 * But it's only on the first write fault on the vma, not
812 * every time, and we could devise a way to avoid it later
813 * (e.g. stash info in next's anon_vma_node when assigning
814 * an anon_vma, or when trying vma_merge). Another time.
816 if (find_vma_prev(vma
->vm_mm
, vma
->vm_start
, &near
) != vma
)
821 vm_flags
= vma
->vm_flags
& ~(VM_READ
|VM_WRITE
|VM_EXEC
);
822 vm_flags
|= near
->vm_flags
& (VM_READ
|VM_WRITE
|VM_EXEC
);
824 if (near
->anon_vma
&& near
->vm_end
== vma
->vm_start
&&
825 mpol_equal(vma_policy(near
), vma_policy(vma
)) &&
826 can_vma_merge_after(near
, vm_flags
,
827 NULL
, vma
->vm_file
, vma
->vm_pgoff
))
828 return near
->anon_vma
;
831 * There's no absolute need to look only at touching neighbours:
832 * we could search further afield for "compatible" anon_vmas.
833 * But it would probably just be a waste of time searching,
834 * or lead to too many vmas hanging off the same anon_vma.
835 * We're trying to allow mprotect remerging later on,
836 * not trying to minimize memory used for anon_vmas.
841 #ifdef CONFIG_PROC_FS
842 void vm_stat_account(struct mm_struct
*mm
, unsigned long flags
,
843 struct file
*file
, long pages
)
845 const unsigned long stack_flags
846 = VM_STACK_FLAGS
& (VM_GROWSUP
|VM_GROWSDOWN
);
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
= len
>> PAGE_SHIFT
;
937 locked
+= mm
->locked_vm
;
938 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
939 lock_limit
>>= PAGE_SHIFT
;
940 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
944 inode
= file
? file
->f_dentry
->d_inode
: NULL
;
947 switch (flags
& MAP_TYPE
) {
949 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
953 * Make sure we don't allow writing to an append-only
956 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
960 * Make sure there are no mandatory locks on the file.
962 if (locks_verify_locked(inode
))
965 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
966 if (!(file
->f_mode
& FMODE_WRITE
))
967 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
971 if (!(file
->f_mode
& FMODE_READ
))
979 switch (flags
& MAP_TYPE
) {
981 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
985 * Set pgoff according to addr for anon_vma.
987 pgoff
= addr
>> PAGE_SHIFT
;
994 error
= security_file_mmap(file
, reqprot
, prot
, flags
);
1001 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
1002 if (vma
&& vma
->vm_start
< addr
+ len
) {
1003 if (do_munmap(mm
, addr
, len
))
1008 /* Check against address space limit. */
1009 if (!may_expand_vm(mm
, len
>> PAGE_SHIFT
))
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_zalloc(vm_area_cachep
, GFP_KERNEL
);
1050 vma
->vm_start
= addr
;
1051 vma
->vm_end
= addr
+ len
;
1052 vma
->vm_flags
= vm_flags
;
1053 vma
->vm_page_prot
= protection_map
[vm_flags
& 0x0f];
1054 vma
->vm_pgoff
= pgoff
;
1058 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1060 if (vm_flags
& VM_DENYWRITE
) {
1061 error
= deny_write_access(file
);
1066 vma
->vm_file
= file
;
1068 error
= file
->f_op
->mmap(file
, vma
);
1070 goto unmap_and_free_vma
;
1071 } else if (vm_flags
& VM_SHARED
) {
1072 error
= shmem_zero_setup(vma
);
1077 /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
1078 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
1079 * that memory reservation must be checked; but that reservation
1080 * belongs to shared memory object, not to vma: so now clear it.
1082 if ((vm_flags
& (VM_SHARED
|VM_ACCOUNT
)) == (VM_SHARED
|VM_ACCOUNT
))
1083 vma
->vm_flags
&= ~VM_ACCOUNT
;
1085 /* Can addr have changed??
1087 * Answer: Yes, several device drivers can do it in their
1088 * f_op->mmap method. -DaveM
1090 addr
= vma
->vm_start
;
1091 pgoff
= vma
->vm_pgoff
;
1092 vm_flags
= vma
->vm_flags
;
1094 if (!file
|| !vma_merge(mm
, prev
, addr
, vma
->vm_end
,
1095 vma
->vm_flags
, NULL
, file
, pgoff
, vma_policy(vma
))) {
1096 file
= vma
->vm_file
;
1097 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1099 atomic_inc(&inode
->i_writecount
);
1103 atomic_inc(&inode
->i_writecount
);
1106 mpol_free(vma_policy(vma
));
1107 kmem_cache_free(vm_area_cachep
, vma
);
1110 mm
->total_vm
+= len
>> PAGE_SHIFT
;
1111 vm_stat_account(mm
, vm_flags
, file
, len
>> PAGE_SHIFT
);
1112 if (vm_flags
& VM_LOCKED
) {
1113 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
1114 make_pages_present(addr
, addr
+ len
);
1116 if (flags
& MAP_POPULATE
) {
1117 up_write(&mm
->mmap_sem
);
1118 sys_remap_file_pages(addr
, len
, 0,
1119 pgoff
, flags
& MAP_NONBLOCK
);
1120 down_write(&mm
->mmap_sem
);
1126 atomic_inc(&inode
->i_writecount
);
1127 vma
->vm_file
= NULL
;
1130 /* Undo any partial mapping done by a device driver. */
1131 unmap_region(mm
, vma
, prev
, vma
->vm_start
, vma
->vm_end
);
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 if (len
> mm
->cached_hole_size
) {
1174 start_addr
= addr
= mm
->free_area_cache
;
1176 start_addr
= addr
= TASK_UNMAPPED_BASE
;
1177 mm
->cached_hole_size
= 0;
1181 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
1182 /* At this point: (!vma || addr < vma->vm_end). */
1183 if (TASK_SIZE
- len
< addr
) {
1185 * Start a new search - just in case we missed
1188 if (start_addr
!= TASK_UNMAPPED_BASE
) {
1189 addr
= TASK_UNMAPPED_BASE
;
1191 mm
->cached_hole_size
= 0;
1196 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
1198 * Remember the place where we stopped the search:
1200 mm
->free_area_cache
= addr
+ len
;
1203 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
1204 mm
->cached_hole_size
= vma
->vm_start
- addr
;
1210 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1213 * Is this a new hole at the lowest possible address?
1215 if (addr
>= TASK_UNMAPPED_BASE
&& addr
< mm
->free_area_cache
) {
1216 mm
->free_area_cache
= addr
;
1217 mm
->cached_hole_size
= ~0UL;
1222 * This mmap-allocator allocates new areas top-down from below the
1223 * stack's low limit (the base):
1225 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1227 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
1228 const unsigned long len
, const unsigned long pgoff
,
1229 const unsigned long flags
)
1231 struct vm_area_struct
*vma
;
1232 struct mm_struct
*mm
= current
->mm
;
1233 unsigned long addr
= addr0
;
1235 /* requested length too big for entire address space */
1236 if (len
> TASK_SIZE
)
1239 /* requesting a specific address */
1241 addr
= PAGE_ALIGN(addr
);
1242 vma
= find_vma(mm
, addr
);
1243 if (TASK_SIZE
- len
>= addr
&&
1244 (!vma
|| addr
+ len
<= vma
->vm_start
))
1248 /* check if free_area_cache is useful for us */
1249 if (len
<= mm
->cached_hole_size
) {
1250 mm
->cached_hole_size
= 0;
1251 mm
->free_area_cache
= mm
->mmap_base
;
1254 /* either no address requested or can't fit in requested address hole */
1255 addr
= mm
->free_area_cache
;
1257 /* make sure it can fit in the remaining address space */
1259 vma
= find_vma(mm
, addr
-len
);
1260 if (!vma
|| addr
<= vma
->vm_start
)
1261 /* remember the address as a hint for next time */
1262 return (mm
->free_area_cache
= addr
-len
);
1265 if (mm
->mmap_base
< len
)
1268 addr
= mm
->mmap_base
-len
;
1272 * Lookup failure means no vma is above this address,
1273 * else if new region fits below vma->vm_start,
1274 * return with success:
1276 vma
= find_vma(mm
, addr
);
1277 if (!vma
|| addr
+len
<= vma
->vm_start
)
1278 /* remember the address as a hint for next time */
1279 return (mm
->free_area_cache
= addr
);
1281 /* remember the largest hole we saw so far */
1282 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
1283 mm
->cached_hole_size
= vma
->vm_start
- addr
;
1285 /* try just below the current vma->vm_start */
1286 addr
= vma
->vm_start
-len
;
1287 } while (len
< vma
->vm_start
);
1291 * A failed mmap() very likely causes application failure,
1292 * so fall back to the bottom-up function here. This scenario
1293 * can happen with large stack limits and large mmap()
1296 mm
->cached_hole_size
= ~0UL;
1297 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
1298 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
1300 * Restore the topdown base:
1302 mm
->free_area_cache
= mm
->mmap_base
;
1303 mm
->cached_hole_size
= ~0UL;
1309 void arch_unmap_area_topdown(struct mm_struct
*mm
, unsigned long addr
)
1312 * Is this a new hole at the highest possible address?
1314 if (addr
> mm
->free_area_cache
)
1315 mm
->free_area_cache
= addr
;
1317 /* dont allow allocations above current base */
1318 if (mm
->free_area_cache
> mm
->mmap_base
)
1319 mm
->free_area_cache
= mm
->mmap_base
;
1323 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
1324 unsigned long pgoff
, unsigned long flags
)
1328 if (!(flags
& MAP_FIXED
)) {
1329 unsigned long (*get_area
)(struct file
*, unsigned long, unsigned long, unsigned long, unsigned long);
1331 get_area
= current
->mm
->get_unmapped_area
;
1332 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
1333 get_area
= file
->f_op
->get_unmapped_area
;
1334 addr
= get_area(file
, addr
, len
, pgoff
, flags
);
1335 if (IS_ERR_VALUE(addr
))
1339 if (addr
> TASK_SIZE
- len
)
1341 if (addr
& ~PAGE_MASK
)
1343 if (file
&& is_file_hugepages(file
)) {
1345 * Check if the given range is hugepage aligned, and
1346 * can be made suitable for hugepages.
1348 ret
= prepare_hugepage_range(addr
, len
);
1351 * Ensure that a normal request is not falling in a
1352 * reserved hugepage range. For some archs like IA-64,
1353 * there is a separate region for hugepages.
1355 ret
= is_hugepage_only_range(current
->mm
, addr
, len
);
1362 EXPORT_SYMBOL(get_unmapped_area
);
1364 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1365 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
1367 struct vm_area_struct
*vma
= NULL
;
1370 /* Check the cache first. */
1371 /* (Cache hit rate is typically around 35%.) */
1372 vma
= mm
->mmap_cache
;
1373 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
1374 struct rb_node
* rb_node
;
1376 rb_node
= mm
->mm_rb
.rb_node
;
1380 struct vm_area_struct
* vma_tmp
;
1382 vma_tmp
= rb_entry(rb_node
,
1383 struct vm_area_struct
, vm_rb
);
1385 if (vma_tmp
->vm_end
> addr
) {
1387 if (vma_tmp
->vm_start
<= addr
)
1389 rb_node
= rb_node
->rb_left
;
1391 rb_node
= rb_node
->rb_right
;
1394 mm
->mmap_cache
= vma
;
1400 EXPORT_SYMBOL(find_vma
);
1402 /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1403 struct vm_area_struct
*
1404 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
1405 struct vm_area_struct
**pprev
)
1407 struct vm_area_struct
*vma
= NULL
, *prev
= NULL
;
1408 struct rb_node
* rb_node
;
1412 /* Guard against addr being lower than the first VMA */
1415 /* Go through the RB tree quickly. */
1416 rb_node
= mm
->mm_rb
.rb_node
;
1419 struct vm_area_struct
*vma_tmp
;
1420 vma_tmp
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
1422 if (addr
< vma_tmp
->vm_end
) {
1423 rb_node
= rb_node
->rb_left
;
1426 if (!prev
->vm_next
|| (addr
< prev
->vm_next
->vm_end
))
1428 rb_node
= rb_node
->rb_right
;
1434 return prev
? prev
->vm_next
: vma
;
1438 * Verify that the stack growth is acceptable and
1439 * update accounting. This is shared with both the
1440 * grow-up and grow-down cases.
1442 static int acct_stack_growth(struct vm_area_struct
* vma
, unsigned long size
, unsigned long grow
)
1444 struct mm_struct
*mm
= vma
->vm_mm
;
1445 struct rlimit
*rlim
= current
->signal
->rlim
;
1447 /* address space limit tests */
1448 if (!may_expand_vm(mm
, grow
))
1451 /* Stack limit test */
1452 if (size
> rlim
[RLIMIT_STACK
].rlim_cur
)
1455 /* mlock limit tests */
1456 if (vma
->vm_flags
& VM_LOCKED
) {
1457 unsigned long locked
;
1458 unsigned long limit
;
1459 locked
= mm
->locked_vm
+ grow
;
1460 limit
= rlim
[RLIMIT_MEMLOCK
].rlim_cur
>> PAGE_SHIFT
;
1461 if (locked
> limit
&& !capable(CAP_IPC_LOCK
))
1466 * Overcommit.. This must be the final test, as it will
1467 * update security statistics.
1469 if (security_vm_enough_memory(grow
))
1472 /* Ok, everything looks good - let it rip */
1473 mm
->total_vm
+= grow
;
1474 if (vma
->vm_flags
& VM_LOCKED
)
1475 mm
->locked_vm
+= grow
;
1476 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, grow
);
1480 #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
1482 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1483 * vma is the last one with address > vma->vm_end. Have to extend vma.
1488 int expand_upwards(struct vm_area_struct
*vma
, unsigned long address
)
1492 if (!(vma
->vm_flags
& VM_GROWSUP
))
1496 * We must make sure the anon_vma is allocated
1497 * so that the anon_vma locking is not a noop.
1499 if (unlikely(anon_vma_prepare(vma
)))
1504 * vma->vm_start/vm_end cannot change under us because the caller
1505 * is required to hold the mmap_sem in read mode. We need the
1506 * anon_vma lock to serialize against concurrent expand_stacks.
1508 address
+= 4 + PAGE_SIZE
- 1;
1509 address
&= PAGE_MASK
;
1512 /* Somebody else might have raced and expanded it already */
1513 if (address
> vma
->vm_end
) {
1514 unsigned long size
, grow
;
1516 size
= address
- vma
->vm_start
;
1517 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
1519 error
= acct_stack_growth(vma
, size
, grow
);
1521 vma
->vm_end
= address
;
1523 anon_vma_unlock(vma
);
1526 #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1528 #ifdef CONFIG_STACK_GROWSUP
1529 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
1531 return expand_upwards(vma
, address
);
1534 struct vm_area_struct
*
1535 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
1537 struct vm_area_struct
*vma
, *prev
;
1540 vma
= find_vma_prev(mm
, addr
, &prev
);
1541 if (vma
&& (vma
->vm_start
<= addr
))
1543 if (!prev
|| expand_stack(prev
, addr
))
1545 if (prev
->vm_flags
& VM_LOCKED
) {
1546 make_pages_present(addr
, prev
->vm_end
);
1552 * vma is the first one with address < vma->vm_start. Have to extend vma.
1554 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
1559 * We must make sure the anon_vma is allocated
1560 * so that the anon_vma locking is not a noop.
1562 if (unlikely(anon_vma_prepare(vma
)))
1567 * vma->vm_start/vm_end cannot change under us because the caller
1568 * is required to hold the mmap_sem in read mode. We need the
1569 * anon_vma lock to serialize against concurrent expand_stacks.
1571 address
&= PAGE_MASK
;
1574 /* Somebody else might have raced and expanded it already */
1575 if (address
< vma
->vm_start
) {
1576 unsigned long size
, grow
;
1578 size
= vma
->vm_end
- address
;
1579 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
1581 error
= acct_stack_growth(vma
, size
, grow
);
1583 vma
->vm_start
= address
;
1584 vma
->vm_pgoff
-= grow
;
1587 anon_vma_unlock(vma
);
1591 struct vm_area_struct
*
1592 find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
1594 struct vm_area_struct
* vma
;
1595 unsigned long start
;
1598 vma
= find_vma(mm
,addr
);
1601 if (vma
->vm_start
<= addr
)
1603 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
1605 start
= vma
->vm_start
;
1606 if (expand_stack(vma
, addr
))
1608 if (vma
->vm_flags
& VM_LOCKED
) {
1609 make_pages_present(addr
, start
);
1616 * Ok - we have the memory areas we should free on the vma list,
1617 * so release them, and do the vma updates.
1619 * Called with the mm semaphore held.
1621 static void remove_vma_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
1623 /* Update high watermark before we lower total_vm */
1624 update_hiwater_vm(mm
);
1626 long nrpages
= vma_pages(vma
);
1628 mm
->total_vm
-= nrpages
;
1629 if (vma
->vm_flags
& VM_LOCKED
)
1630 mm
->locked_vm
-= nrpages
;
1631 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, -nrpages
);
1632 vma
= remove_vma(vma
);
1638 * Get rid of page table information in the indicated region.
1640 * Called with the mm semaphore held.
1642 static void unmap_region(struct mm_struct
*mm
,
1643 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
1644 unsigned long start
, unsigned long end
)
1646 struct vm_area_struct
*next
= prev
? prev
->vm_next
: mm
->mmap
;
1647 struct mmu_gather
*tlb
;
1648 unsigned long nr_accounted
= 0;
1651 tlb
= tlb_gather_mmu(mm
, 0);
1652 update_hiwater_rss(mm
);
1653 unmap_vmas(&tlb
, vma
, start
, end
, &nr_accounted
, NULL
);
1654 vm_unacct_memory(nr_accounted
);
1655 free_pgtables(&tlb
, vma
, prev
? prev
->vm_end
: FIRST_USER_ADDRESS
,
1656 next
? next
->vm_start
: 0);
1657 tlb_finish_mmu(tlb
, start
, end
);
1661 * Create a list of vma's touched by the unmap, removing them from the mm's
1662 * vma list as we go..
1665 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1666 struct vm_area_struct
*prev
, unsigned long end
)
1668 struct vm_area_struct
**insertion_point
;
1669 struct vm_area_struct
*tail_vma
= NULL
;
1672 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
1674 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
1678 } while (vma
&& vma
->vm_start
< end
);
1679 *insertion_point
= vma
;
1680 tail_vma
->vm_next
= NULL
;
1681 if (mm
->unmap_area
== arch_unmap_area
)
1682 addr
= prev
? prev
->vm_end
: mm
->mmap_base
;
1684 addr
= vma
? vma
->vm_start
: mm
->mmap_base
;
1685 mm
->unmap_area(mm
, addr
);
1686 mm
->mmap_cache
= NULL
; /* Kill the cache. */
1690 * Split a vma into two pieces at address 'addr', a new vma is allocated
1691 * either for the first part or the the tail.
1693 int split_vma(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
1694 unsigned long addr
, int new_below
)
1696 struct mempolicy
*pol
;
1697 struct vm_area_struct
*new;
1699 if (is_vm_hugetlb_page(vma
) && (addr
& ~HPAGE_MASK
))
1702 if (mm
->map_count
>= sysctl_max_map_count
)
1705 new = kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
1709 /* most fields are the same, copy all, and then fixup */
1715 new->vm_start
= addr
;
1716 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
1719 pol
= mpol_copy(vma_policy(vma
));
1721 kmem_cache_free(vm_area_cachep
, new);
1722 return PTR_ERR(pol
);
1724 vma_set_policy(new, pol
);
1727 get_file(new->vm_file
);
1729 if (new->vm_ops
&& new->vm_ops
->open
)
1730 new->vm_ops
->open(new);
1733 vma_adjust(vma
, addr
, vma
->vm_end
, vma
->vm_pgoff
+
1734 ((addr
- new->vm_start
) >> PAGE_SHIFT
), new);
1736 vma_adjust(vma
, vma
->vm_start
, addr
, vma
->vm_pgoff
, new);
1741 /* Munmap is split into 2 main parts -- this part which finds
1742 * what needs doing, and the areas themselves, which do the
1743 * work. This now handles partial unmappings.
1744 * Jeremy Fitzhardinge <jeremy@goop.org>
1746 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1749 struct vm_area_struct
*vma
, *prev
, *last
;
1751 if ((start
& ~PAGE_MASK
) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
1754 if ((len
= PAGE_ALIGN(len
)) == 0)
1757 /* Find the first overlapping VMA */
1758 vma
= find_vma_prev(mm
, start
, &prev
);
1761 /* we have start < vma->vm_end */
1763 /* if it doesn't overlap, we have nothing.. */
1765 if (vma
->vm_start
>= end
)
1769 * If we need to split any vma, do it now to save pain later.
1771 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
1772 * unmapped vm_area_struct will remain in use: so lower split_vma
1773 * places tmp vma above, and higher split_vma places tmp vma below.
1775 if (start
> vma
->vm_start
) {
1776 int error
= split_vma(mm
, vma
, start
, 0);
1782 /* Does it split the last one? */
1783 last
= find_vma(mm
, end
);
1784 if (last
&& end
> last
->vm_start
) {
1785 int error
= split_vma(mm
, last
, end
, 1);
1789 vma
= prev
? prev
->vm_next
: mm
->mmap
;
1792 * Remove the vma's, and unmap the actual pages
1794 detach_vmas_to_be_unmapped(mm
, vma
, prev
, end
);
1795 unmap_region(mm
, vma
, prev
, start
, end
);
1797 /* Fix up all other VM information */
1798 remove_vma_list(mm
, vma
);
1803 EXPORT_SYMBOL(do_munmap
);
1805 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
1808 struct mm_struct
*mm
= current
->mm
;
1810 profile_munmap(addr
);
1812 down_write(&mm
->mmap_sem
);
1813 ret
= do_munmap(mm
, addr
, len
);
1814 up_write(&mm
->mmap_sem
);
1818 static inline void verify_mm_writelocked(struct mm_struct
*mm
)
1820 #ifdef CONFIG_DEBUG_VM
1821 if (unlikely(down_read_trylock(&mm
->mmap_sem
))) {
1823 up_read(&mm
->mmap_sem
);
1829 * this is really a simplified "do_mmap". it only handles
1830 * anonymous maps. eventually we may be able to do some
1831 * brk-specific accounting here.
1833 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1835 struct mm_struct
* mm
= current
->mm
;
1836 struct vm_area_struct
* vma
, * prev
;
1837 unsigned long flags
;
1838 struct rb_node
** rb_link
, * rb_parent
;
1839 pgoff_t pgoff
= addr
>> PAGE_SHIFT
;
1841 len
= PAGE_ALIGN(len
);
1845 if ((addr
+ len
) > TASK_SIZE
|| (addr
+ len
) < addr
)
1851 if (mm
->def_flags
& VM_LOCKED
) {
1852 unsigned long locked
, lock_limit
;
1853 locked
= len
>> PAGE_SHIFT
;
1854 locked
+= mm
->locked_vm
;
1855 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1856 lock_limit
>>= PAGE_SHIFT
;
1857 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
1862 * mm->mmap_sem is required to protect against another thread
1863 * changing the mappings in case we sleep.
1865 verify_mm_writelocked(mm
);
1868 * Clear old maps. this also does some error checking for us
1871 vma
= find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
1872 if (vma
&& vma
->vm_start
< addr
+ len
) {
1873 if (do_munmap(mm
, addr
, len
))
1878 /* Check against address space limits *after* clearing old maps... */
1879 if (!may_expand_vm(mm
, len
>> PAGE_SHIFT
))
1882 if (mm
->map_count
> sysctl_max_map_count
)
1885 if (security_vm_enough_memory(len
>> PAGE_SHIFT
))
1888 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
1890 /* Can we just expand an old private anonymous mapping? */
1891 if (vma_merge(mm
, prev
, addr
, addr
+ len
, flags
,
1892 NULL
, NULL
, pgoff
, NULL
))
1896 * create a vma struct for an anonymous mapping
1898 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1900 vm_unacct_memory(len
>> PAGE_SHIFT
);
1905 vma
->vm_start
= addr
;
1906 vma
->vm_end
= addr
+ len
;
1907 vma
->vm_pgoff
= pgoff
;
1908 vma
->vm_flags
= flags
;
1909 vma
->vm_page_prot
= protection_map
[flags
& 0x0f];
1910 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1912 mm
->total_vm
+= len
>> PAGE_SHIFT
;
1913 if (flags
& VM_LOCKED
) {
1914 mm
->locked_vm
+= len
>> PAGE_SHIFT
;
1915 make_pages_present(addr
, addr
+ len
);
1920 EXPORT_SYMBOL(do_brk
);
1922 /* Release all mmaps. */
1923 void exit_mmap(struct mm_struct
*mm
)
1925 struct mmu_gather
*tlb
;
1926 struct vm_area_struct
*vma
= mm
->mmap
;
1927 unsigned long nr_accounted
= 0;
1932 tlb
= tlb_gather_mmu(mm
, 1);
1933 /* Don't update_hiwater_rss(mm) here, do_exit already did */
1934 /* Use -1 here to ensure all VMAs in the mm are unmapped */
1935 end
= unmap_vmas(&tlb
, vma
, 0, -1, &nr_accounted
, NULL
);
1936 vm_unacct_memory(nr_accounted
);
1937 free_pgtables(&tlb
, vma
, FIRST_USER_ADDRESS
, 0);
1938 tlb_finish_mmu(tlb
, 0, end
);
1941 * Walk the list again, actually closing and freeing it,
1942 * with preemption enabled, without holding any MM locks.
1945 vma
= remove_vma(vma
);
1947 BUG_ON(mm
->nr_ptes
> (FIRST_USER_ADDRESS
+PMD_SIZE
-1)>>PMD_SHIFT
);
1950 /* Insert vm structure into process list sorted by address
1951 * and into the inode's i_mmap tree. If vm_file is non-NULL
1952 * then i_mmap_lock is taken here.
1954 int insert_vm_struct(struct mm_struct
* mm
, struct vm_area_struct
* vma
)
1956 struct vm_area_struct
* __vma
, * prev
;
1957 struct rb_node
** rb_link
, * rb_parent
;
1960 * The vm_pgoff of a purely anonymous vma should be irrelevant
1961 * until its first write fault, when page's anon_vma and index
1962 * are set. But now set the vm_pgoff it will almost certainly
1963 * end up with (unless mremap moves it elsewhere before that
1964 * first wfault), so /proc/pid/maps tells a consistent story.
1966 * By setting it to reflect the virtual start address of the
1967 * vma, merges and splits can happen in a seamless way, just
1968 * using the existing file pgoff checks and manipulations.
1969 * Similarly in do_mmap_pgoff and in do_brk.
1971 if (!vma
->vm_file
) {
1972 BUG_ON(vma
->anon_vma
);
1973 vma
->vm_pgoff
= vma
->vm_start
>> PAGE_SHIFT
;
1975 __vma
= find_vma_prepare(mm
,vma
->vm_start
,&prev
,&rb_link
,&rb_parent
);
1976 if (__vma
&& __vma
->vm_start
< vma
->vm_end
)
1978 if ((vma
->vm_flags
& VM_ACCOUNT
) &&
1979 security_vm_enough_memory(vma_pages(vma
)))
1981 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1986 * Copy the vma structure to a new location in the same mm,
1987 * prior to moving page table entries, to effect an mremap move.
1989 struct vm_area_struct
*copy_vma(struct vm_area_struct
**vmap
,
1990 unsigned long addr
, unsigned long len
, pgoff_t pgoff
)
1992 struct vm_area_struct
*vma
= *vmap
;
1993 unsigned long vma_start
= vma
->vm_start
;
1994 struct mm_struct
*mm
= vma
->vm_mm
;
1995 struct vm_area_struct
*new_vma
, *prev
;
1996 struct rb_node
**rb_link
, *rb_parent
;
1997 struct mempolicy
*pol
;
2000 * If anonymous vma has not yet been faulted, update new pgoff
2001 * to match new location, to increase its chance of merging.
2003 if (!vma
->vm_file
&& !vma
->anon_vma
)
2004 pgoff
= addr
>> PAGE_SHIFT
;
2006 find_vma_prepare(mm
, addr
, &prev
, &rb_link
, &rb_parent
);
2007 new_vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vma
->vm_flags
,
2008 vma
->anon_vma
, vma
->vm_file
, pgoff
, vma_policy(vma
));
2011 * Source vma may have been merged into new_vma
2013 if (vma_start
>= new_vma
->vm_start
&&
2014 vma_start
< new_vma
->vm_end
)
2017 new_vma
= kmem_cache_alloc(vm_area_cachep
, SLAB_KERNEL
);
2020 pol
= mpol_copy(vma_policy(vma
));
2022 kmem_cache_free(vm_area_cachep
, new_vma
);
2025 vma_set_policy(new_vma
, pol
);
2026 new_vma
->vm_start
= addr
;
2027 new_vma
->vm_end
= addr
+ len
;
2028 new_vma
->vm_pgoff
= pgoff
;
2029 if (new_vma
->vm_file
)
2030 get_file(new_vma
->vm_file
);
2031 if (new_vma
->vm_ops
&& new_vma
->vm_ops
->open
)
2032 new_vma
->vm_ops
->open(new_vma
);
2033 vma_link(mm
, new_vma
, prev
, rb_link
, rb_parent
);
2040 * Return true if the calling process may expand its vm space by the passed
2043 int may_expand_vm(struct mm_struct
*mm
, unsigned long npages
)
2045 unsigned long cur
= mm
->total_vm
; /* pages */
2048 lim
= current
->signal
->rlim
[RLIMIT_AS
].rlim_cur
>> PAGE_SHIFT
;
2050 if (cur
+ npages
> lim
)