6 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
9 #include <linux/slab.h>
10 #include <linux/backing-dev.h>
12 #include <linux/shm.h>
13 #include <linux/mman.h>
14 #include <linux/pagemap.h>
15 #include <linux/swap.h>
16 #include <linux/syscalls.h>
17 #include <linux/capability.h>
18 #include <linux/init.h>
19 #include <linux/file.h>
21 #include <linux/personality.h>
22 #include <linux/security.h>
23 #include <linux/hugetlb.h>
24 #include <linux/profile.h>
25 #include <linux/export.h>
26 #include <linux/mount.h>
27 #include <linux/mempolicy.h>
28 #include <linux/rmap.h>
29 #include <linux/mmu_notifier.h>
30 #include <linux/perf_event.h>
31 #include <linux/audit.h>
32 #include <linux/khugepaged.h>
33 #include <linux/uprobes.h>
34 #include <linux/rbtree_augmented.h>
36 #include <asm/uaccess.h>
37 #include <asm/cacheflush.h>
39 #include <asm/mmu_context.h>
43 #ifndef arch_mmap_check
44 #define arch_mmap_check(addr, len, flags) (0)
47 #ifndef arch_rebalance_pgtables
48 #define arch_rebalance_pgtables(addr, len) (addr)
51 static void unmap_region(struct mm_struct
*mm
,
52 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
53 unsigned long start
, unsigned long end
);
55 /* description of effects of mapping type and prot in current implementation.
56 * this is due to the limited x86 page protection hardware. The expected
57 * behavior is in parens:
60 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
61 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
62 * w: (no) no w: (no) no w: (yes) yes w: (no) no
63 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
65 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
66 * w: (no) no w: (no) no w: (copy) copy w: (no) no
67 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
70 pgprot_t protection_map
[16] = {
71 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
72 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
75 pgprot_t
vm_get_page_prot(unsigned long vm_flags
)
77 return __pgprot(pgprot_val(protection_map
[vm_flags
&
78 (VM_READ
|VM_WRITE
|VM_EXEC
|VM_SHARED
)]) |
79 pgprot_val(arch_vm_get_page_prot(vm_flags
)));
81 EXPORT_SYMBOL(vm_get_page_prot
);
83 int sysctl_overcommit_memory __read_mostly
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
84 int sysctl_overcommit_ratio __read_mostly
= 50; /* default is 50% */
85 int sysctl_max_map_count __read_mostly
= DEFAULT_MAX_MAP_COUNT
;
87 * Make sure vm_committed_as in one cacheline and not cacheline shared with
88 * other variables. It can be updated by several CPUs frequently.
90 struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp
;
93 * Check that a process has enough memory to allocate a new virtual
94 * mapping. 0 means there is enough memory for the allocation to
95 * succeed and -ENOMEM implies there is not.
97 * We currently support three overcommit policies, which are set via the
98 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
100 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
101 * Additional code 2002 Jul 20 by Robert Love.
103 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
105 * Note this is a helper function intended to be used by LSMs which
106 * wish to use this logic.
108 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
110 unsigned long free
, allowed
;
112 vm_acct_memory(pages
);
115 * Sometimes we want to use more memory than we have
117 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
120 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
121 free
= global_page_state(NR_FREE_PAGES
);
122 free
+= global_page_state(NR_FILE_PAGES
);
125 * shmem pages shouldn't be counted as free in this
126 * case, they can't be purged, only swapped out, and
127 * that won't affect the overall amount of available
128 * memory in the system.
130 free
-= global_page_state(NR_SHMEM
);
132 free
+= nr_swap_pages
;
135 * Any slabs which are created with the
136 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
137 * which are reclaimable, under pressure. The dentry
138 * cache and most inode caches should fall into this
140 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
143 * Leave reserved pages. The pages are not for anonymous pages.
145 if (free
<= totalreserve_pages
)
148 free
-= totalreserve_pages
;
151 * Leave the last 3% for root
162 allowed
= (totalram_pages
- hugetlb_total_pages())
163 * sysctl_overcommit_ratio
/ 100;
165 * Leave the last 3% for root
168 allowed
-= allowed
/ 32;
169 allowed
+= total_swap_pages
;
171 /* Don't let a single process grow too big:
172 leave 3% of the size of this process for other processes */
174 allowed
-= mm
->total_vm
/ 32;
176 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
179 vm_unacct_memory(pages
);
185 * Requires inode->i_mapping->i_mmap_mutex
187 static void __remove_shared_vm_struct(struct vm_area_struct
*vma
,
188 struct file
*file
, struct address_space
*mapping
)
190 if (vma
->vm_flags
& VM_DENYWRITE
)
191 atomic_inc(&file
->f_path
.dentry
->d_inode
->i_writecount
);
192 if (vma
->vm_flags
& VM_SHARED
)
193 mapping
->i_mmap_writable
--;
195 flush_dcache_mmap_lock(mapping
);
196 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
))
197 list_del_init(&vma
->shared
.nonlinear
);
199 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
200 flush_dcache_mmap_unlock(mapping
);
204 * Unlink a file-based vm structure from its interval tree, to hide
205 * vma from rmap and vmtruncate before freeing its page tables.
207 void unlink_file_vma(struct vm_area_struct
*vma
)
209 struct file
*file
= vma
->vm_file
;
212 struct address_space
*mapping
= file
->f_mapping
;
213 mutex_lock(&mapping
->i_mmap_mutex
);
214 __remove_shared_vm_struct(vma
, file
, mapping
);
215 mutex_unlock(&mapping
->i_mmap_mutex
);
220 * Close a vm structure and free it, returning the next.
222 static struct vm_area_struct
*remove_vma(struct vm_area_struct
*vma
)
224 struct vm_area_struct
*next
= vma
->vm_next
;
227 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
228 vma
->vm_ops
->close(vma
);
231 mpol_put(vma_policy(vma
));
232 kmem_cache_free(vm_area_cachep
, vma
);
236 static unsigned long do_brk(unsigned long addr
, unsigned long len
);
238 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
240 unsigned long rlim
, retval
;
241 unsigned long newbrk
, oldbrk
;
242 struct mm_struct
*mm
= current
->mm
;
243 unsigned long min_brk
;
245 down_write(&mm
->mmap_sem
);
247 #ifdef CONFIG_COMPAT_BRK
249 * CONFIG_COMPAT_BRK can still be overridden by setting
250 * randomize_va_space to 2, which will still cause mm->start_brk
251 * to be arbitrarily shifted
253 if (current
->brk_randomized
)
254 min_brk
= mm
->start_brk
;
256 min_brk
= mm
->end_data
;
258 min_brk
= mm
->start_brk
;
264 * Check against rlimit here. If this check is done later after the test
265 * of oldbrk with newbrk then it can escape the test and let the data
266 * segment grow beyond its set limit the in case where the limit is
267 * not page aligned -Ram Gupta
269 rlim
= rlimit(RLIMIT_DATA
);
270 if (rlim
< RLIM_INFINITY
&& (brk
- mm
->start_brk
) +
271 (mm
->end_data
- mm
->start_data
) > rlim
)
274 newbrk
= PAGE_ALIGN(brk
);
275 oldbrk
= PAGE_ALIGN(mm
->brk
);
276 if (oldbrk
== newbrk
)
279 /* Always allow shrinking brk. */
280 if (brk
<= mm
->brk
) {
281 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
286 /* Check against existing mmap mappings. */
287 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
290 /* Ok, looks good - let it rip. */
291 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
297 up_write(&mm
->mmap_sem
);
301 static long vma_compute_subtree_gap(struct vm_area_struct
*vma
)
303 unsigned long max
, subtree_gap
;
306 max
-= vma
->vm_prev
->vm_end
;
307 if (vma
->vm_rb
.rb_left
) {
308 subtree_gap
= rb_entry(vma
->vm_rb
.rb_left
,
309 struct vm_area_struct
, vm_rb
)->rb_subtree_gap
;
310 if (subtree_gap
> max
)
313 if (vma
->vm_rb
.rb_right
) {
314 subtree_gap
= rb_entry(vma
->vm_rb
.rb_right
,
315 struct vm_area_struct
, vm_rb
)->rb_subtree_gap
;
316 if (subtree_gap
> max
)
322 #ifdef CONFIG_DEBUG_VM_RB
323 static int browse_rb(struct rb_root
*root
)
325 int i
= 0, j
, bug
= 0;
326 struct rb_node
*nd
, *pn
= NULL
;
327 unsigned long prev
= 0, pend
= 0;
329 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
330 struct vm_area_struct
*vma
;
331 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
332 if (vma
->vm_start
< prev
) {
333 printk("vm_start %lx prev %lx\n", vma
->vm_start
, prev
);
336 if (vma
->vm_start
< pend
) {
337 printk("vm_start %lx pend %lx\n", vma
->vm_start
, pend
);
340 if (vma
->vm_start
> vma
->vm_end
) {
341 printk("vm_end %lx < vm_start %lx\n",
342 vma
->vm_end
, vma
->vm_start
);
345 if (vma
->rb_subtree_gap
!= vma_compute_subtree_gap(vma
)) {
346 printk("free gap %lx, correct %lx\n",
348 vma_compute_subtree_gap(vma
));
353 prev
= vma
->vm_start
;
357 for (nd
= pn
; nd
; nd
= rb_prev(nd
))
360 printk("backwards %d, forwards %d\n", j
, i
);
366 static void validate_mm_rb(struct rb_root
*root
, struct vm_area_struct
*ignore
)
370 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
371 struct vm_area_struct
*vma
;
372 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
373 BUG_ON(vma
!= ignore
&&
374 vma
->rb_subtree_gap
!= vma_compute_subtree_gap(vma
));
378 void validate_mm(struct mm_struct
*mm
)
382 unsigned long highest_address
= 0;
383 struct vm_area_struct
*vma
= mm
->mmap
;
385 struct anon_vma_chain
*avc
;
386 vma_lock_anon_vma(vma
);
387 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
388 anon_vma_interval_tree_verify(avc
);
389 vma_unlock_anon_vma(vma
);
390 highest_address
= vma
->vm_end
;
394 if (i
!= mm
->map_count
) {
395 printk("map_count %d vm_next %d\n", mm
->map_count
, i
);
398 if (highest_address
!= mm
->highest_vm_end
) {
399 printk("mm->highest_vm_end %lx, found %lx\n",
400 mm
->highest_vm_end
, highest_address
);
403 i
= browse_rb(&mm
->mm_rb
);
404 if (i
!= mm
->map_count
) {
405 printk("map_count %d rb %d\n", mm
->map_count
, i
);
411 #define validate_mm_rb(root, ignore) do { } while (0)
412 #define validate_mm(mm) do { } while (0)
415 RB_DECLARE_CALLBACKS(static, vma_gap_callbacks
, struct vm_area_struct
, vm_rb
,
416 unsigned long, rb_subtree_gap
, vma_compute_subtree_gap
)
419 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
420 * vma->vm_prev->vm_end values changed, without modifying the vma's position
423 static void vma_gap_update(struct vm_area_struct
*vma
)
426 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
427 * function that does exacltly what we want.
429 vma_gap_callbacks_propagate(&vma
->vm_rb
, NULL
);
432 static inline void vma_rb_insert(struct vm_area_struct
*vma
,
433 struct rb_root
*root
)
435 /* All rb_subtree_gap values must be consistent prior to insertion */
436 validate_mm_rb(root
, NULL
);
438 rb_insert_augmented(&vma
->vm_rb
, root
, &vma_gap_callbacks
);
441 static void vma_rb_erase(struct vm_area_struct
*vma
, struct rb_root
*root
)
444 * All rb_subtree_gap values must be consistent prior to erase,
445 * with the possible exception of the vma being erased.
447 validate_mm_rb(root
, vma
);
450 * Note rb_erase_augmented is a fairly large inline function,
451 * so make sure we instantiate it only once with our desired
452 * augmented rbtree callbacks.
454 rb_erase_augmented(&vma
->vm_rb
, root
, &vma_gap_callbacks
);
458 * vma has some anon_vma assigned, and is already inserted on that
459 * anon_vma's interval trees.
461 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
462 * vma must be removed from the anon_vma's interval trees using
463 * anon_vma_interval_tree_pre_update_vma().
465 * After the update, the vma will be reinserted using
466 * anon_vma_interval_tree_post_update_vma().
468 * The entire update must be protected by exclusive mmap_sem and by
469 * the root anon_vma's mutex.
472 anon_vma_interval_tree_pre_update_vma(struct vm_area_struct
*vma
)
474 struct anon_vma_chain
*avc
;
476 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
477 anon_vma_interval_tree_remove(avc
, &avc
->anon_vma
->rb_root
);
481 anon_vma_interval_tree_post_update_vma(struct vm_area_struct
*vma
)
483 struct anon_vma_chain
*avc
;
485 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
486 anon_vma_interval_tree_insert(avc
, &avc
->anon_vma
->rb_root
);
489 static int find_vma_links(struct mm_struct
*mm
, unsigned long addr
,
490 unsigned long end
, struct vm_area_struct
**pprev
,
491 struct rb_node
***rb_link
, struct rb_node
**rb_parent
)
493 struct rb_node
**__rb_link
, *__rb_parent
, *rb_prev
;
495 __rb_link
= &mm
->mm_rb
.rb_node
;
496 rb_prev
= __rb_parent
= NULL
;
499 struct vm_area_struct
*vma_tmp
;
501 __rb_parent
= *__rb_link
;
502 vma_tmp
= rb_entry(__rb_parent
, struct vm_area_struct
, vm_rb
);
504 if (vma_tmp
->vm_end
> addr
) {
505 /* Fail if an existing vma overlaps the area */
506 if (vma_tmp
->vm_start
< end
)
508 __rb_link
= &__rb_parent
->rb_left
;
510 rb_prev
= __rb_parent
;
511 __rb_link
= &__rb_parent
->rb_right
;
517 *pprev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
518 *rb_link
= __rb_link
;
519 *rb_parent
= __rb_parent
;
523 void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
524 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
526 /* Update tracking information for the gap following the new vma. */
528 vma_gap_update(vma
->vm_next
);
530 mm
->highest_vm_end
= vma
->vm_end
;
533 * vma->vm_prev wasn't known when we followed the rbtree to find the
534 * correct insertion point for that vma. As a result, we could not
535 * update the vma vm_rb parents rb_subtree_gap values on the way down.
536 * So, we first insert the vma with a zero rb_subtree_gap value
537 * (to be consistent with what we did on the way down), and then
538 * immediately update the gap to the correct value. Finally we
539 * rebalance the rbtree after all augmented values have been set.
541 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
542 vma
->rb_subtree_gap
= 0;
544 vma_rb_insert(vma
, &mm
->mm_rb
);
547 static void __vma_link_file(struct vm_area_struct
*vma
)
553 struct address_space
*mapping
= file
->f_mapping
;
555 if (vma
->vm_flags
& VM_DENYWRITE
)
556 atomic_dec(&file
->f_path
.dentry
->d_inode
->i_writecount
);
557 if (vma
->vm_flags
& VM_SHARED
)
558 mapping
->i_mmap_writable
++;
560 flush_dcache_mmap_lock(mapping
);
561 if (unlikely(vma
->vm_flags
& VM_NONLINEAR
))
562 vma_nonlinear_insert(vma
, &mapping
->i_mmap_nonlinear
);
564 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
565 flush_dcache_mmap_unlock(mapping
);
570 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
571 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
572 struct rb_node
*rb_parent
)
574 __vma_link_list(mm
, vma
, prev
, rb_parent
);
575 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
578 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
579 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
580 struct rb_node
*rb_parent
)
582 struct address_space
*mapping
= NULL
;
585 mapping
= vma
->vm_file
->f_mapping
;
588 mutex_lock(&mapping
->i_mmap_mutex
);
590 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
591 __vma_link_file(vma
);
594 mutex_unlock(&mapping
->i_mmap_mutex
);
601 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
602 * mm's list and rbtree. It has already been inserted into the interval tree.
604 static void __insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
606 struct vm_area_struct
*prev
;
607 struct rb_node
**rb_link
, *rb_parent
;
609 if (find_vma_links(mm
, vma
->vm_start
, vma
->vm_end
,
610 &prev
, &rb_link
, &rb_parent
))
612 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
617 __vma_unlink(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
618 struct vm_area_struct
*prev
)
620 struct vm_area_struct
*next
;
622 vma_rb_erase(vma
, &mm
->mm_rb
);
623 prev
->vm_next
= next
= vma
->vm_next
;
625 next
->vm_prev
= prev
;
626 if (mm
->mmap_cache
== vma
)
627 mm
->mmap_cache
= prev
;
631 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
632 * is already present in an i_mmap tree without adjusting the tree.
633 * The following helper function should be used when such adjustments
634 * are necessary. The "insert" vma (if any) is to be inserted
635 * before we drop the necessary locks.
637 int vma_adjust(struct vm_area_struct
*vma
, unsigned long start
,
638 unsigned long end
, pgoff_t pgoff
, struct vm_area_struct
*insert
)
640 struct mm_struct
*mm
= vma
->vm_mm
;
641 struct vm_area_struct
*next
= vma
->vm_next
;
642 struct vm_area_struct
*importer
= NULL
;
643 struct address_space
*mapping
= NULL
;
644 struct rb_root
*root
= NULL
;
645 struct anon_vma
*anon_vma
= NULL
;
646 struct file
*file
= vma
->vm_file
;
647 bool start_changed
= false, end_changed
= false;
648 long adjust_next
= 0;
651 if (next
&& !insert
) {
652 struct vm_area_struct
*exporter
= NULL
;
654 if (end
>= next
->vm_end
) {
656 * vma expands, overlapping all the next, and
657 * perhaps the one after too (mprotect case 6).
659 again
: remove_next
= 1 + (end
> next
->vm_end
);
663 } else if (end
> next
->vm_start
) {
665 * vma expands, overlapping part of the next:
666 * mprotect case 5 shifting the boundary up.
668 adjust_next
= (end
- next
->vm_start
) >> PAGE_SHIFT
;
671 } else if (end
< vma
->vm_end
) {
673 * vma shrinks, and !insert tells it's not
674 * split_vma inserting another: so it must be
675 * mprotect case 4 shifting the boundary down.
677 adjust_next
= - ((vma
->vm_end
- end
) >> PAGE_SHIFT
);
683 * Easily overlooked: when mprotect shifts the boundary,
684 * make sure the expanding vma has anon_vma set if the
685 * shrinking vma had, to cover any anon pages imported.
687 if (exporter
&& exporter
->anon_vma
&& !importer
->anon_vma
) {
688 if (anon_vma_clone(importer
, exporter
))
690 importer
->anon_vma
= exporter
->anon_vma
;
695 mapping
= file
->f_mapping
;
696 if (!(vma
->vm_flags
& VM_NONLINEAR
)) {
697 root
= &mapping
->i_mmap
;
698 uprobe_munmap(vma
, vma
->vm_start
, vma
->vm_end
);
701 uprobe_munmap(next
, next
->vm_start
,
705 mutex_lock(&mapping
->i_mmap_mutex
);
708 * Put into interval tree now, so instantiated pages
709 * are visible to arm/parisc __flush_dcache_page
710 * throughout; but we cannot insert into address
711 * space until vma start or end is updated.
713 __vma_link_file(insert
);
717 vma_adjust_trans_huge(vma
, start
, end
, adjust_next
);
719 anon_vma
= vma
->anon_vma
;
720 if (!anon_vma
&& adjust_next
)
721 anon_vma
= next
->anon_vma
;
723 VM_BUG_ON(adjust_next
&& next
->anon_vma
&&
724 anon_vma
!= next
->anon_vma
);
725 anon_vma_lock(anon_vma
);
726 anon_vma_interval_tree_pre_update_vma(vma
);
728 anon_vma_interval_tree_pre_update_vma(next
);
732 flush_dcache_mmap_lock(mapping
);
733 vma_interval_tree_remove(vma
, root
);
735 vma_interval_tree_remove(next
, root
);
738 if (start
!= vma
->vm_start
) {
739 vma
->vm_start
= start
;
740 start_changed
= true;
742 if (end
!= vma
->vm_end
) {
746 vma
->vm_pgoff
= pgoff
;
748 next
->vm_start
+= adjust_next
<< PAGE_SHIFT
;
749 next
->vm_pgoff
+= adjust_next
;
754 vma_interval_tree_insert(next
, root
);
755 vma_interval_tree_insert(vma
, root
);
756 flush_dcache_mmap_unlock(mapping
);
761 * vma_merge has merged next into vma, and needs
762 * us to remove next before dropping the locks.
764 __vma_unlink(mm
, next
, vma
);
766 __remove_shared_vm_struct(next
, file
, mapping
);
769 * split_vma has split insert from vma, and needs
770 * us to insert it before dropping the locks
771 * (it may either follow vma or precede it).
773 __insert_vm_struct(mm
, insert
);
779 mm
->highest_vm_end
= end
;
780 else if (!adjust_next
)
781 vma_gap_update(next
);
786 anon_vma_interval_tree_post_update_vma(vma
);
788 anon_vma_interval_tree_post_update_vma(next
);
789 anon_vma_unlock(anon_vma
);
792 mutex_unlock(&mapping
->i_mmap_mutex
);
803 uprobe_munmap(next
, next
->vm_start
, next
->vm_end
);
807 anon_vma_merge(vma
, next
);
809 mpol_put(vma_policy(next
));
810 kmem_cache_free(vm_area_cachep
, next
);
812 * In mprotect's case 6 (see comments on vma_merge),
813 * we must remove another next too. It would clutter
814 * up the code too much to do both in one go.
817 if (remove_next
== 2)
820 vma_gap_update(next
);
822 mm
->highest_vm_end
= end
;
833 * If the vma has a ->close operation then the driver probably needs to release
834 * per-vma resources, so we don't attempt to merge those.
836 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
837 struct file
*file
, unsigned long vm_flags
)
839 if (vma
->vm_flags
^ vm_flags
)
841 if (vma
->vm_file
!= file
)
843 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
848 static inline int is_mergeable_anon_vma(struct anon_vma
*anon_vma1
,
849 struct anon_vma
*anon_vma2
,
850 struct vm_area_struct
*vma
)
853 * The list_is_singular() test is to avoid merging VMA cloned from
854 * parents. This can improve scalability caused by anon_vma lock.
856 if ((!anon_vma1
|| !anon_vma2
) && (!vma
||
857 list_is_singular(&vma
->anon_vma_chain
)))
859 return anon_vma1
== anon_vma2
;
863 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
864 * in front of (at a lower virtual address and file offset than) the vma.
866 * We cannot merge two vmas if they have differently assigned (non-NULL)
867 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
869 * We don't check here for the merged mmap wrapping around the end of pagecache
870 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
871 * wrap, nor mmaps which cover the final page at index -1UL.
874 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
875 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
877 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
878 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
, vma
)) {
879 if (vma
->vm_pgoff
== vm_pgoff
)
886 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
887 * beyond (at a higher virtual address and file offset than) the vma.
889 * We cannot merge two vmas if they have differently assigned (non-NULL)
890 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
893 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
894 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
896 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
897 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
, vma
)) {
899 vm_pglen
= (vma
->vm_end
- vma
->vm_start
) >> PAGE_SHIFT
;
900 if (vma
->vm_pgoff
+ vm_pglen
== vm_pgoff
)
907 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
908 * whether that can be merged with its predecessor or its successor.
909 * Or both (it neatly fills a hole).
911 * In most cases - when called for mmap, brk or mremap - [addr,end) is
912 * certain not to be mapped by the time vma_merge is called; but when
913 * called for mprotect, it is certain to be already mapped (either at
914 * an offset within prev, or at the start of next), and the flags of
915 * this area are about to be changed to vm_flags - and the no-change
916 * case has already been eliminated.
918 * The following mprotect cases have to be considered, where AAAA is
919 * the area passed down from mprotect_fixup, never extending beyond one
920 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
922 * AAAA AAAA AAAA AAAA
923 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
924 * cannot merge might become might become might become
925 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
926 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
927 * mremap move: PPPPNNNNNNNN 8
929 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
930 * might become case 1 below case 2 below case 3 below
932 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
933 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
935 struct vm_area_struct
*vma_merge(struct mm_struct
*mm
,
936 struct vm_area_struct
*prev
, unsigned long addr
,
937 unsigned long end
, unsigned long vm_flags
,
938 struct anon_vma
*anon_vma
, struct file
*file
,
939 pgoff_t pgoff
, struct mempolicy
*policy
)
941 pgoff_t pglen
= (end
- addr
) >> PAGE_SHIFT
;
942 struct vm_area_struct
*area
, *next
;
946 * We later require that vma->vm_flags == vm_flags,
947 * so this tests vma->vm_flags & VM_SPECIAL, too.
949 if (vm_flags
& VM_SPECIAL
)
953 next
= prev
->vm_next
;
957 if (next
&& next
->vm_end
== end
) /* cases 6, 7, 8 */
958 next
= next
->vm_next
;
961 * Can it merge with the predecessor?
963 if (prev
&& prev
->vm_end
== addr
&&
964 mpol_equal(vma_policy(prev
), policy
) &&
965 can_vma_merge_after(prev
, vm_flags
,
966 anon_vma
, file
, pgoff
)) {
968 * OK, it can. Can we now merge in the successor as well?
970 if (next
&& end
== next
->vm_start
&&
971 mpol_equal(policy
, vma_policy(next
)) &&
972 can_vma_merge_before(next
, vm_flags
,
973 anon_vma
, file
, pgoff
+pglen
) &&
974 is_mergeable_anon_vma(prev
->anon_vma
,
975 next
->anon_vma
, NULL
)) {
977 err
= vma_adjust(prev
, prev
->vm_start
,
978 next
->vm_end
, prev
->vm_pgoff
, NULL
);
979 } else /* cases 2, 5, 7 */
980 err
= vma_adjust(prev
, prev
->vm_start
,
981 end
, prev
->vm_pgoff
, NULL
);
984 khugepaged_enter_vma_merge(prev
);
989 * Can this new request be merged in front of next?
991 if (next
&& end
== next
->vm_start
&&
992 mpol_equal(policy
, vma_policy(next
)) &&
993 can_vma_merge_before(next
, vm_flags
,
994 anon_vma
, file
, pgoff
+pglen
)) {
995 if (prev
&& addr
< prev
->vm_end
) /* case 4 */
996 err
= vma_adjust(prev
, prev
->vm_start
,
997 addr
, prev
->vm_pgoff
, NULL
);
998 else /* cases 3, 8 */
999 err
= vma_adjust(area
, addr
, next
->vm_end
,
1000 next
->vm_pgoff
- pglen
, NULL
);
1003 khugepaged_enter_vma_merge(area
);
1011 * Rough compatbility check to quickly see if it's even worth looking
1012 * at sharing an anon_vma.
1014 * They need to have the same vm_file, and the flags can only differ
1015 * in things that mprotect may change.
1017 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1018 * we can merge the two vma's. For example, we refuse to merge a vma if
1019 * there is a vm_ops->close() function, because that indicates that the
1020 * driver is doing some kind of reference counting. But that doesn't
1021 * really matter for the anon_vma sharing case.
1023 static int anon_vma_compatible(struct vm_area_struct
*a
, struct vm_area_struct
*b
)
1025 return a
->vm_end
== b
->vm_start
&&
1026 mpol_equal(vma_policy(a
), vma_policy(b
)) &&
1027 a
->vm_file
== b
->vm_file
&&
1028 !((a
->vm_flags
^ b
->vm_flags
) & ~(VM_READ
|VM_WRITE
|VM_EXEC
)) &&
1029 b
->vm_pgoff
== a
->vm_pgoff
+ ((b
->vm_start
- a
->vm_start
) >> PAGE_SHIFT
);
1033 * Do some basic sanity checking to see if we can re-use the anon_vma
1034 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1035 * the same as 'old', the other will be the new one that is trying
1036 * to share the anon_vma.
1038 * NOTE! This runs with mm_sem held for reading, so it is possible that
1039 * the anon_vma of 'old' is concurrently in the process of being set up
1040 * by another page fault trying to merge _that_. But that's ok: if it
1041 * is being set up, that automatically means that it will be a singleton
1042 * acceptable for merging, so we can do all of this optimistically. But
1043 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
1045 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1046 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1047 * is to return an anon_vma that is "complex" due to having gone through
1050 * We also make sure that the two vma's are compatible (adjacent,
1051 * and with the same memory policies). That's all stable, even with just
1052 * a read lock on the mm_sem.
1054 static struct anon_vma
*reusable_anon_vma(struct vm_area_struct
*old
, struct vm_area_struct
*a
, struct vm_area_struct
*b
)
1056 if (anon_vma_compatible(a
, b
)) {
1057 struct anon_vma
*anon_vma
= ACCESS_ONCE(old
->anon_vma
);
1059 if (anon_vma
&& list_is_singular(&old
->anon_vma_chain
))
1066 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1067 * neighbouring vmas for a suitable anon_vma, before it goes off
1068 * to allocate a new anon_vma. It checks because a repetitive
1069 * sequence of mprotects and faults may otherwise lead to distinct
1070 * anon_vmas being allocated, preventing vma merge in subsequent
1073 struct anon_vma
*find_mergeable_anon_vma(struct vm_area_struct
*vma
)
1075 struct anon_vma
*anon_vma
;
1076 struct vm_area_struct
*near
;
1078 near
= vma
->vm_next
;
1082 anon_vma
= reusable_anon_vma(near
, vma
, near
);
1086 near
= vma
->vm_prev
;
1090 anon_vma
= reusable_anon_vma(near
, near
, vma
);
1095 * There's no absolute need to look only at touching neighbours:
1096 * we could search further afield for "compatible" anon_vmas.
1097 * But it would probably just be a waste of time searching,
1098 * or lead to too many vmas hanging off the same anon_vma.
1099 * We're trying to allow mprotect remerging later on,
1100 * not trying to minimize memory used for anon_vmas.
1105 #ifdef CONFIG_PROC_FS
1106 void vm_stat_account(struct mm_struct
*mm
, unsigned long flags
,
1107 struct file
*file
, long pages
)
1109 const unsigned long stack_flags
1110 = VM_STACK_FLAGS
& (VM_GROWSUP
|VM_GROWSDOWN
);
1112 mm
->total_vm
+= pages
;
1115 mm
->shared_vm
+= pages
;
1116 if ((flags
& (VM_EXEC
|VM_WRITE
)) == VM_EXEC
)
1117 mm
->exec_vm
+= pages
;
1118 } else if (flags
& stack_flags
)
1119 mm
->stack_vm
+= pages
;
1121 #endif /* CONFIG_PROC_FS */
1124 * If a hint addr is less than mmap_min_addr change hint to be as
1125 * low as possible but still greater than mmap_min_addr
1127 static inline unsigned long round_hint_to_min(unsigned long hint
)
1130 if (((void *)hint
!= NULL
) &&
1131 (hint
< mmap_min_addr
))
1132 return PAGE_ALIGN(mmap_min_addr
);
1137 * The caller must hold down_write(¤t->mm->mmap_sem).
1140 unsigned long do_mmap_pgoff(struct file
*file
, unsigned long addr
,
1141 unsigned long len
, unsigned long prot
,
1142 unsigned long flags
, unsigned long pgoff
)
1144 struct mm_struct
* mm
= current
->mm
;
1145 struct inode
*inode
;
1146 vm_flags_t vm_flags
;
1149 * Does the application expect PROT_READ to imply PROT_EXEC?
1151 * (the exception is when the underlying filesystem is noexec
1152 * mounted, in which case we dont add PROT_EXEC.)
1154 if ((prot
& PROT_READ
) && (current
->personality
& READ_IMPLIES_EXEC
))
1155 if (!(file
&& (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
)))
1161 if (!(flags
& MAP_FIXED
))
1162 addr
= round_hint_to_min(addr
);
1164 /* Careful about overflows.. */
1165 len
= PAGE_ALIGN(len
);
1169 /* offset overflow? */
1170 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
1173 /* Too many mappings? */
1174 if (mm
->map_count
> sysctl_max_map_count
)
1177 /* Obtain the address to map to. we verify (or select) it and ensure
1178 * that it represents a valid section of the address space.
1180 addr
= get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
1181 if (addr
& ~PAGE_MASK
)
1184 /* Do simple checking here so the lower-level routines won't have
1185 * to. we assume access permissions have been handled by the open
1186 * of the memory object, so we don't do any here.
1188 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
) |
1189 mm
->def_flags
| VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1191 if (flags
& MAP_LOCKED
)
1192 if (!can_do_mlock())
1195 /* mlock MCL_FUTURE? */
1196 if (vm_flags
& VM_LOCKED
) {
1197 unsigned long locked
, lock_limit
;
1198 locked
= len
>> PAGE_SHIFT
;
1199 locked
+= mm
->locked_vm
;
1200 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
1201 lock_limit
>>= PAGE_SHIFT
;
1202 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
1206 inode
= file
? file
->f_path
.dentry
->d_inode
: NULL
;
1209 switch (flags
& MAP_TYPE
) {
1211 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
1215 * Make sure we don't allow writing to an append-only
1218 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
1222 * Make sure there are no mandatory locks on the file.
1224 if (locks_verify_locked(inode
))
1227 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
1228 if (!(file
->f_mode
& FMODE_WRITE
))
1229 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
1233 if (!(file
->f_mode
& FMODE_READ
))
1235 if (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
) {
1236 if (vm_flags
& VM_EXEC
)
1238 vm_flags
&= ~VM_MAYEXEC
;
1241 if (!file
->f_op
|| !file
->f_op
->mmap
)
1249 switch (flags
& MAP_TYPE
) {
1255 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
1259 * Set pgoff according to addr for anon_vma.
1261 pgoff
= addr
>> PAGE_SHIFT
;
1268 return mmap_region(file
, addr
, len
, flags
, vm_flags
, pgoff
);
1271 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1272 unsigned long, prot
, unsigned long, flags
,
1273 unsigned long, fd
, unsigned long, pgoff
)
1275 struct file
*file
= NULL
;
1276 unsigned long retval
= -EBADF
;
1278 if (!(flags
& MAP_ANONYMOUS
)) {
1279 audit_mmap_fd(fd
, flags
);
1280 if (unlikely(flags
& MAP_HUGETLB
))
1285 } else if (flags
& MAP_HUGETLB
) {
1286 struct user_struct
*user
= NULL
;
1288 * VM_NORESERVE is used because the reservations will be
1289 * taken when vm_ops->mmap() is called
1290 * A dummy user value is used because we are not locking
1291 * memory so no accounting is necessary
1293 file
= hugetlb_file_setup(HUGETLB_ANON_FILE
, addr
, len
,
1295 &user
, HUGETLB_ANONHUGE_INODE
,
1296 (flags
>> MAP_HUGE_SHIFT
) & MAP_HUGE_MASK
);
1298 return PTR_ERR(file
);
1301 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1303 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1310 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1311 struct mmap_arg_struct
{
1315 unsigned long flags
;
1317 unsigned long offset
;
1320 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1322 struct mmap_arg_struct a
;
1324 if (copy_from_user(&a
, arg
, sizeof(a
)))
1326 if (a
.offset
& ~PAGE_MASK
)
1329 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1330 a
.offset
>> PAGE_SHIFT
);
1332 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1335 * Some shared mappigns will want the pages marked read-only
1336 * to track write events. If so, we'll downgrade vm_page_prot
1337 * to the private version (using protection_map[] without the
1340 int vma_wants_writenotify(struct vm_area_struct
*vma
)
1342 vm_flags_t vm_flags
= vma
->vm_flags
;
1344 /* If it was private or non-writable, the write bit is already clear */
1345 if ((vm_flags
& (VM_WRITE
|VM_SHARED
)) != ((VM_WRITE
|VM_SHARED
)))
1348 /* The backer wishes to know when pages are first written to? */
1349 if (vma
->vm_ops
&& vma
->vm_ops
->page_mkwrite
)
1352 /* The open routine did something to the protections already? */
1353 if (pgprot_val(vma
->vm_page_prot
) !=
1354 pgprot_val(vm_get_page_prot(vm_flags
)))
1357 /* Specialty mapping? */
1358 if (vm_flags
& VM_PFNMAP
)
1361 /* Can the mapping track the dirty pages? */
1362 return vma
->vm_file
&& vma
->vm_file
->f_mapping
&&
1363 mapping_cap_account_dirty(vma
->vm_file
->f_mapping
);
1367 * We account for memory if it's a private writeable mapping,
1368 * not hugepages and VM_NORESERVE wasn't set.
1370 static inline int accountable_mapping(struct file
*file
, vm_flags_t vm_flags
)
1373 * hugetlb has its own accounting separate from the core VM
1374 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1376 if (file
&& is_file_hugepages(file
))
1379 return (vm_flags
& (VM_NORESERVE
| VM_SHARED
| VM_WRITE
)) == VM_WRITE
;
1382 unsigned long mmap_region(struct file
*file
, unsigned long addr
,
1383 unsigned long len
, unsigned long flags
,
1384 vm_flags_t vm_flags
, unsigned long pgoff
)
1386 struct mm_struct
*mm
= current
->mm
;
1387 struct vm_area_struct
*vma
, *prev
;
1388 int correct_wcount
= 0;
1390 struct rb_node
**rb_link
, *rb_parent
;
1391 unsigned long charged
= 0;
1392 struct inode
*inode
= file
? file
->f_path
.dentry
->d_inode
: NULL
;
1394 /* Clear old maps */
1397 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
)) {
1398 if (do_munmap(mm
, addr
, len
))
1403 /* Check against address space limit. */
1404 if (!may_expand_vm(mm
, len
>> PAGE_SHIFT
))
1408 * Set 'VM_NORESERVE' if we should not account for the
1409 * memory use of this mapping.
1411 if ((flags
& MAP_NORESERVE
)) {
1412 /* We honor MAP_NORESERVE if allowed to overcommit */
1413 if (sysctl_overcommit_memory
!= OVERCOMMIT_NEVER
)
1414 vm_flags
|= VM_NORESERVE
;
1416 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1417 if (file
&& is_file_hugepages(file
))
1418 vm_flags
|= VM_NORESERVE
;
1422 * Private writable mapping: check memory availability
1424 if (accountable_mapping(file
, vm_flags
)) {
1425 charged
= len
>> PAGE_SHIFT
;
1426 if (security_vm_enough_memory_mm(mm
, charged
))
1428 vm_flags
|= VM_ACCOUNT
;
1432 * Can we just expand an old mapping?
1434 vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vm_flags
, NULL
, file
, pgoff
, NULL
);
1439 * Determine the object being mapped and call the appropriate
1440 * specific mapper. the address has already been validated, but
1441 * not unmapped, but the maps are removed from the list.
1443 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1450 vma
->vm_start
= addr
;
1451 vma
->vm_end
= addr
+ len
;
1452 vma
->vm_flags
= vm_flags
;
1453 vma
->vm_page_prot
= vm_get_page_prot(vm_flags
);
1454 vma
->vm_pgoff
= pgoff
;
1455 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1457 error
= -EINVAL
; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1460 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1462 if (vm_flags
& VM_DENYWRITE
) {
1463 error
= deny_write_access(file
);
1468 vma
->vm_file
= get_file(file
);
1469 error
= file
->f_op
->mmap(file
, vma
);
1471 goto unmap_and_free_vma
;
1473 /* Can addr have changed??
1475 * Answer: Yes, several device drivers can do it in their
1476 * f_op->mmap method. -DaveM
1478 addr
= vma
->vm_start
;
1479 pgoff
= vma
->vm_pgoff
;
1480 vm_flags
= vma
->vm_flags
;
1481 } else if (vm_flags
& VM_SHARED
) {
1482 if (unlikely(vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
)))
1484 error
= shmem_zero_setup(vma
);
1489 if (vma_wants_writenotify(vma
)) {
1490 pgprot_t pprot
= vma
->vm_page_prot
;
1492 /* Can vma->vm_page_prot have changed??
1494 * Answer: Yes, drivers may have changed it in their
1495 * f_op->mmap method.
1497 * Ensures that vmas marked as uncached stay that way.
1499 vma
->vm_page_prot
= vm_get_page_prot(vm_flags
& ~VM_SHARED
);
1500 if (pgprot_val(pprot
) == pgprot_val(pgprot_noncached(pprot
)))
1501 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1504 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1505 file
= vma
->vm_file
;
1507 /* Once vma denies write, undo our temporary denial count */
1509 atomic_inc(&inode
->i_writecount
);
1511 perf_event_mmap(vma
);
1513 vm_stat_account(mm
, vm_flags
, file
, len
>> PAGE_SHIFT
);
1514 if (vm_flags
& VM_LOCKED
) {
1515 if (!mlock_vma_pages_range(vma
, addr
, addr
+ len
))
1516 mm
->locked_vm
+= (len
>> PAGE_SHIFT
);
1517 } else if ((flags
& MAP_POPULATE
) && !(flags
& MAP_NONBLOCK
))
1518 make_pages_present(addr
, addr
+ len
);
1527 atomic_inc(&inode
->i_writecount
);
1528 vma
->vm_file
= NULL
;
1531 /* Undo any partial mapping done by a device driver. */
1532 unmap_region(mm
, vma
, prev
, vma
->vm_start
, vma
->vm_end
);
1535 kmem_cache_free(vm_area_cachep
, vma
);
1538 vm_unacct_memory(charged
);
1542 /* Get an address range which is currently unmapped.
1543 * For shmat() with addr=0.
1545 * Ugly calling convention alert:
1546 * Return value with the low bits set means error value,
1548 * if (ret & ~PAGE_MASK)
1551 * This function "knows" that -ENOMEM has the bits set.
1553 #ifndef HAVE_ARCH_UNMAPPED_AREA
1555 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
1556 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1558 struct mm_struct
*mm
= current
->mm
;
1559 struct vm_area_struct
*vma
;
1560 unsigned long start_addr
;
1562 if (len
> TASK_SIZE
)
1565 if (flags
& MAP_FIXED
)
1569 addr
= PAGE_ALIGN(addr
);
1570 vma
= find_vma(mm
, addr
);
1571 if (TASK_SIZE
- len
>= addr
&&
1572 (!vma
|| addr
+ len
<= vma
->vm_start
))
1575 if (len
> mm
->cached_hole_size
) {
1576 start_addr
= addr
= mm
->free_area_cache
;
1578 start_addr
= addr
= TASK_UNMAPPED_BASE
;
1579 mm
->cached_hole_size
= 0;
1583 for (vma
= find_vma(mm
, addr
); ; vma
= vma
->vm_next
) {
1584 /* At this point: (!vma || addr < vma->vm_end). */
1585 if (TASK_SIZE
- len
< addr
) {
1587 * Start a new search - just in case we missed
1590 if (start_addr
!= TASK_UNMAPPED_BASE
) {
1591 addr
= TASK_UNMAPPED_BASE
;
1593 mm
->cached_hole_size
= 0;
1598 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
1600 * Remember the place where we stopped the search:
1602 mm
->free_area_cache
= addr
+ len
;
1605 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
1606 mm
->cached_hole_size
= vma
->vm_start
- addr
;
1612 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1615 * Is this a new hole at the lowest possible address?
1617 if (addr
>= TASK_UNMAPPED_BASE
&& addr
< mm
->free_area_cache
)
1618 mm
->free_area_cache
= addr
;
1622 * This mmap-allocator allocates new areas top-down from below the
1623 * stack's low limit (the base):
1625 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1627 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
1628 const unsigned long len
, const unsigned long pgoff
,
1629 const unsigned long flags
)
1631 struct vm_area_struct
*vma
;
1632 struct mm_struct
*mm
= current
->mm
;
1633 unsigned long addr
= addr0
, start_addr
;
1635 /* requested length too big for entire address space */
1636 if (len
> TASK_SIZE
)
1639 if (flags
& MAP_FIXED
)
1642 /* requesting a specific address */
1644 addr
= PAGE_ALIGN(addr
);
1645 vma
= find_vma(mm
, addr
);
1646 if (TASK_SIZE
- len
>= addr
&&
1647 (!vma
|| addr
+ len
<= vma
->vm_start
))
1651 /* check if free_area_cache is useful for us */
1652 if (len
<= mm
->cached_hole_size
) {
1653 mm
->cached_hole_size
= 0;
1654 mm
->free_area_cache
= mm
->mmap_base
;
1658 /* either no address requested or can't fit in requested address hole */
1659 start_addr
= addr
= mm
->free_area_cache
;
1667 * Lookup failure means no vma is above this address,
1668 * else if new region fits below vma->vm_start,
1669 * return with success:
1671 vma
= find_vma(mm
, addr
);
1672 if (!vma
|| addr
+len
<= vma
->vm_start
)
1673 /* remember the address as a hint for next time */
1674 return (mm
->free_area_cache
= addr
);
1676 /* remember the largest hole we saw so far */
1677 if (addr
+ mm
->cached_hole_size
< vma
->vm_start
)
1678 mm
->cached_hole_size
= vma
->vm_start
- addr
;
1680 /* try just below the current vma->vm_start */
1681 addr
= vma
->vm_start
-len
;
1682 } while (len
< vma
->vm_start
);
1686 * if hint left us with no space for the requested
1687 * mapping then try again:
1689 * Note: this is different with the case of bottomup
1690 * which does the fully line-search, but we use find_vma
1691 * here that causes some holes skipped.
1693 if (start_addr
!= mm
->mmap_base
) {
1694 mm
->free_area_cache
= mm
->mmap_base
;
1695 mm
->cached_hole_size
= 0;
1700 * A failed mmap() very likely causes application failure,
1701 * so fall back to the bottom-up function here. This scenario
1702 * can happen with large stack limits and large mmap()
1705 mm
->cached_hole_size
= ~0UL;
1706 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
1707 addr
= arch_get_unmapped_area(filp
, addr0
, len
, pgoff
, flags
);
1709 * Restore the topdown base:
1711 mm
->free_area_cache
= mm
->mmap_base
;
1712 mm
->cached_hole_size
= ~0UL;
1718 void arch_unmap_area_topdown(struct mm_struct
*mm
, unsigned long addr
)
1721 * Is this a new hole at the highest possible address?
1723 if (addr
> mm
->free_area_cache
)
1724 mm
->free_area_cache
= addr
;
1726 /* dont allow allocations above current base */
1727 if (mm
->free_area_cache
> mm
->mmap_base
)
1728 mm
->free_area_cache
= mm
->mmap_base
;
1732 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
1733 unsigned long pgoff
, unsigned long flags
)
1735 unsigned long (*get_area
)(struct file
*, unsigned long,
1736 unsigned long, unsigned long, unsigned long);
1738 unsigned long error
= arch_mmap_check(addr
, len
, flags
);
1742 /* Careful about overflows.. */
1743 if (len
> TASK_SIZE
)
1746 get_area
= current
->mm
->get_unmapped_area
;
1747 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
1748 get_area
= file
->f_op
->get_unmapped_area
;
1749 addr
= get_area(file
, addr
, len
, pgoff
, flags
);
1750 if (IS_ERR_VALUE(addr
))
1753 if (addr
> TASK_SIZE
- len
)
1755 if (addr
& ~PAGE_MASK
)
1758 addr
= arch_rebalance_pgtables(addr
, len
);
1759 error
= security_mmap_addr(addr
);
1760 return error
? error
: addr
;
1763 EXPORT_SYMBOL(get_unmapped_area
);
1765 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1766 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
1768 struct vm_area_struct
*vma
= NULL
;
1770 if (WARN_ON_ONCE(!mm
)) /* Remove this in linux-3.6 */
1773 /* Check the cache first. */
1774 /* (Cache hit rate is typically around 35%.) */
1775 vma
= mm
->mmap_cache
;
1776 if (!(vma
&& vma
->vm_end
> addr
&& vma
->vm_start
<= addr
)) {
1777 struct rb_node
*rb_node
;
1779 rb_node
= mm
->mm_rb
.rb_node
;
1783 struct vm_area_struct
*vma_tmp
;
1785 vma_tmp
= rb_entry(rb_node
,
1786 struct vm_area_struct
, vm_rb
);
1788 if (vma_tmp
->vm_end
> addr
) {
1790 if (vma_tmp
->vm_start
<= addr
)
1792 rb_node
= rb_node
->rb_left
;
1794 rb_node
= rb_node
->rb_right
;
1797 mm
->mmap_cache
= vma
;
1802 EXPORT_SYMBOL(find_vma
);
1805 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
1807 struct vm_area_struct
*
1808 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
1809 struct vm_area_struct
**pprev
)
1811 struct vm_area_struct
*vma
;
1813 vma
= find_vma(mm
, addr
);
1815 *pprev
= vma
->vm_prev
;
1817 struct rb_node
*rb_node
= mm
->mm_rb
.rb_node
;
1820 *pprev
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
1821 rb_node
= rb_node
->rb_right
;
1828 * Verify that the stack growth is acceptable and
1829 * update accounting. This is shared with both the
1830 * grow-up and grow-down cases.
1832 static int acct_stack_growth(struct vm_area_struct
*vma
, unsigned long size
, unsigned long grow
)
1834 struct mm_struct
*mm
= vma
->vm_mm
;
1835 struct rlimit
*rlim
= current
->signal
->rlim
;
1836 unsigned long new_start
;
1838 /* address space limit tests */
1839 if (!may_expand_vm(mm
, grow
))
1842 /* Stack limit test */
1843 if (size
> ACCESS_ONCE(rlim
[RLIMIT_STACK
].rlim_cur
))
1846 /* mlock limit tests */
1847 if (vma
->vm_flags
& VM_LOCKED
) {
1848 unsigned long locked
;
1849 unsigned long limit
;
1850 locked
= mm
->locked_vm
+ grow
;
1851 limit
= ACCESS_ONCE(rlim
[RLIMIT_MEMLOCK
].rlim_cur
);
1852 limit
>>= PAGE_SHIFT
;
1853 if (locked
> limit
&& !capable(CAP_IPC_LOCK
))
1857 /* Check to ensure the stack will not grow into a hugetlb-only region */
1858 new_start
= (vma
->vm_flags
& VM_GROWSUP
) ? vma
->vm_start
:
1860 if (is_hugepage_only_range(vma
->vm_mm
, new_start
, size
))
1864 * Overcommit.. This must be the final test, as it will
1865 * update security statistics.
1867 if (security_vm_enough_memory_mm(mm
, grow
))
1870 /* Ok, everything looks good - let it rip */
1871 if (vma
->vm_flags
& VM_LOCKED
)
1872 mm
->locked_vm
+= grow
;
1873 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, grow
);
1877 #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
1879 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1880 * vma is the last one with address > vma->vm_end. Have to extend vma.
1882 int expand_upwards(struct vm_area_struct
*vma
, unsigned long address
)
1886 if (!(vma
->vm_flags
& VM_GROWSUP
))
1890 * We must make sure the anon_vma is allocated
1891 * so that the anon_vma locking is not a noop.
1893 if (unlikely(anon_vma_prepare(vma
)))
1895 vma_lock_anon_vma(vma
);
1898 * vma->vm_start/vm_end cannot change under us because the caller
1899 * is required to hold the mmap_sem in read mode. We need the
1900 * anon_vma lock to serialize against concurrent expand_stacks.
1901 * Also guard against wrapping around to address 0.
1903 if (address
< PAGE_ALIGN(address
+4))
1904 address
= PAGE_ALIGN(address
+4);
1906 vma_unlock_anon_vma(vma
);
1911 /* Somebody else might have raced and expanded it already */
1912 if (address
> vma
->vm_end
) {
1913 unsigned long size
, grow
;
1915 size
= address
- vma
->vm_start
;
1916 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
1919 if (vma
->vm_pgoff
+ (size
>> PAGE_SHIFT
) >= vma
->vm_pgoff
) {
1920 error
= acct_stack_growth(vma
, size
, grow
);
1922 anon_vma_interval_tree_pre_update_vma(vma
);
1923 vma
->vm_end
= address
;
1924 anon_vma_interval_tree_post_update_vma(vma
);
1926 vma_gap_update(vma
->vm_next
);
1928 vma
->vm_mm
->highest_vm_end
= address
;
1929 perf_event_mmap(vma
);
1933 vma_unlock_anon_vma(vma
);
1934 khugepaged_enter_vma_merge(vma
);
1935 validate_mm(vma
->vm_mm
);
1938 #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1941 * vma is the first one with address < vma->vm_start. Have to extend vma.
1943 int expand_downwards(struct vm_area_struct
*vma
,
1944 unsigned long address
)
1949 * We must make sure the anon_vma is allocated
1950 * so that the anon_vma locking is not a noop.
1952 if (unlikely(anon_vma_prepare(vma
)))
1955 address
&= PAGE_MASK
;
1956 error
= security_mmap_addr(address
);
1960 vma_lock_anon_vma(vma
);
1963 * vma->vm_start/vm_end cannot change under us because the caller
1964 * is required to hold the mmap_sem in read mode. We need the
1965 * anon_vma lock to serialize against concurrent expand_stacks.
1968 /* Somebody else might have raced and expanded it already */
1969 if (address
< vma
->vm_start
) {
1970 unsigned long size
, grow
;
1972 size
= vma
->vm_end
- address
;
1973 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
1976 if (grow
<= vma
->vm_pgoff
) {
1977 error
= acct_stack_growth(vma
, size
, grow
);
1979 anon_vma_interval_tree_pre_update_vma(vma
);
1980 vma
->vm_start
= address
;
1981 vma
->vm_pgoff
-= grow
;
1982 anon_vma_interval_tree_post_update_vma(vma
);
1983 vma_gap_update(vma
);
1984 perf_event_mmap(vma
);
1988 vma_unlock_anon_vma(vma
);
1989 khugepaged_enter_vma_merge(vma
);
1990 validate_mm(vma
->vm_mm
);
1994 #ifdef CONFIG_STACK_GROWSUP
1995 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
1997 return expand_upwards(vma
, address
);
2000 struct vm_area_struct
*
2001 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
2003 struct vm_area_struct
*vma
, *prev
;
2006 vma
= find_vma_prev(mm
, addr
, &prev
);
2007 if (vma
&& (vma
->vm_start
<= addr
))
2009 if (!prev
|| expand_stack(prev
, addr
))
2011 if (prev
->vm_flags
& VM_LOCKED
) {
2012 mlock_vma_pages_range(prev
, addr
, prev
->vm_end
);
2017 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
2019 return expand_downwards(vma
, address
);
2022 struct vm_area_struct
*
2023 find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
2025 struct vm_area_struct
* vma
;
2026 unsigned long start
;
2029 vma
= find_vma(mm
,addr
);
2032 if (vma
->vm_start
<= addr
)
2034 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
2036 start
= vma
->vm_start
;
2037 if (expand_stack(vma
, addr
))
2039 if (vma
->vm_flags
& VM_LOCKED
) {
2040 mlock_vma_pages_range(vma
, addr
, start
);
2047 * Ok - we have the memory areas we should free on the vma list,
2048 * so release them, and do the vma updates.
2050 * Called with the mm semaphore held.
2052 static void remove_vma_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
2054 unsigned long nr_accounted
= 0;
2056 /* Update high watermark before we lower total_vm */
2057 update_hiwater_vm(mm
);
2059 long nrpages
= vma_pages(vma
);
2061 if (vma
->vm_flags
& VM_ACCOUNT
)
2062 nr_accounted
+= nrpages
;
2063 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, -nrpages
);
2064 vma
= remove_vma(vma
);
2066 vm_unacct_memory(nr_accounted
);
2071 * Get rid of page table information in the indicated region.
2073 * Called with the mm semaphore held.
2075 static void unmap_region(struct mm_struct
*mm
,
2076 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
2077 unsigned long start
, unsigned long end
)
2079 struct vm_area_struct
*next
= prev
? prev
->vm_next
: mm
->mmap
;
2080 struct mmu_gather tlb
;
2083 tlb_gather_mmu(&tlb
, mm
, 0);
2084 update_hiwater_rss(mm
);
2085 unmap_vmas(&tlb
, vma
, start
, end
);
2086 free_pgtables(&tlb
, vma
, prev
? prev
->vm_end
: FIRST_USER_ADDRESS
,
2087 next
? next
->vm_start
: 0);
2088 tlb_finish_mmu(&tlb
, start
, end
);
2092 * Create a list of vma's touched by the unmap, removing them from the mm's
2093 * vma list as we go..
2096 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2097 struct vm_area_struct
*prev
, unsigned long end
)
2099 struct vm_area_struct
**insertion_point
;
2100 struct vm_area_struct
*tail_vma
= NULL
;
2103 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
2104 vma
->vm_prev
= NULL
;
2106 vma_rb_erase(vma
, &mm
->mm_rb
);
2110 } while (vma
&& vma
->vm_start
< end
);
2111 *insertion_point
= vma
;
2113 vma
->vm_prev
= prev
;
2114 vma_gap_update(vma
);
2116 mm
->highest_vm_end
= prev
? prev
->vm_end
: 0;
2117 tail_vma
->vm_next
= NULL
;
2118 if (mm
->unmap_area
== arch_unmap_area
)
2119 addr
= prev
? prev
->vm_end
: mm
->mmap_base
;
2121 addr
= vma
? vma
->vm_start
: mm
->mmap_base
;
2122 mm
->unmap_area(mm
, addr
);
2123 mm
->mmap_cache
= NULL
; /* Kill the cache. */
2127 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2128 * munmap path where it doesn't make sense to fail.
2130 static int __split_vma(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
2131 unsigned long addr
, int new_below
)
2133 struct mempolicy
*pol
;
2134 struct vm_area_struct
*new;
2137 if (is_vm_hugetlb_page(vma
) && (addr
&
2138 ~(huge_page_mask(hstate_vma(vma
)))))
2141 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
2145 /* most fields are the same, copy all, and then fixup */
2148 INIT_LIST_HEAD(&new->anon_vma_chain
);
2153 new->vm_start
= addr
;
2154 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
2157 pol
= mpol_dup(vma_policy(vma
));
2162 vma_set_policy(new, pol
);
2164 if (anon_vma_clone(new, vma
))
2168 get_file(new->vm_file
);
2170 if (new->vm_ops
&& new->vm_ops
->open
)
2171 new->vm_ops
->open(new);
2174 err
= vma_adjust(vma
, addr
, vma
->vm_end
, vma
->vm_pgoff
+
2175 ((addr
- new->vm_start
) >> PAGE_SHIFT
), new);
2177 err
= vma_adjust(vma
, vma
->vm_start
, addr
, vma
->vm_pgoff
, new);
2183 /* Clean everything up if vma_adjust failed. */
2184 if (new->vm_ops
&& new->vm_ops
->close
)
2185 new->vm_ops
->close(new);
2188 unlink_anon_vmas(new);
2192 kmem_cache_free(vm_area_cachep
, new);
2198 * Split a vma into two pieces at address 'addr', a new vma is allocated
2199 * either for the first part or the tail.
2201 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2202 unsigned long addr
, int new_below
)
2204 if (mm
->map_count
>= sysctl_max_map_count
)
2207 return __split_vma(mm
, vma
, addr
, new_below
);
2210 /* Munmap is split into 2 main parts -- this part which finds
2211 * what needs doing, and the areas themselves, which do the
2212 * work. This now handles partial unmappings.
2213 * Jeremy Fitzhardinge <jeremy@goop.org>
2215 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
2218 struct vm_area_struct
*vma
, *prev
, *last
;
2220 if ((start
& ~PAGE_MASK
) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
2223 if ((len
= PAGE_ALIGN(len
)) == 0)
2226 /* Find the first overlapping VMA */
2227 vma
= find_vma(mm
, start
);
2230 prev
= vma
->vm_prev
;
2231 /* we have start < vma->vm_end */
2233 /* if it doesn't overlap, we have nothing.. */
2235 if (vma
->vm_start
>= end
)
2239 * If we need to split any vma, do it now to save pain later.
2241 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2242 * unmapped vm_area_struct will remain in use: so lower split_vma
2243 * places tmp vma above, and higher split_vma places tmp vma below.
2245 if (start
> vma
->vm_start
) {
2249 * Make sure that map_count on return from munmap() will
2250 * not exceed its limit; but let map_count go just above
2251 * its limit temporarily, to help free resources as expected.
2253 if (end
< vma
->vm_end
&& mm
->map_count
>= sysctl_max_map_count
)
2256 error
= __split_vma(mm
, vma
, start
, 0);
2262 /* Does it split the last one? */
2263 last
= find_vma(mm
, end
);
2264 if (last
&& end
> last
->vm_start
) {
2265 int error
= __split_vma(mm
, last
, end
, 1);
2269 vma
= prev
? prev
->vm_next
: mm
->mmap
;
2272 * unlock any mlock()ed ranges before detaching vmas
2274 if (mm
->locked_vm
) {
2275 struct vm_area_struct
*tmp
= vma
;
2276 while (tmp
&& tmp
->vm_start
< end
) {
2277 if (tmp
->vm_flags
& VM_LOCKED
) {
2278 mm
->locked_vm
-= vma_pages(tmp
);
2279 munlock_vma_pages_all(tmp
);
2286 * Remove the vma's, and unmap the actual pages
2288 detach_vmas_to_be_unmapped(mm
, vma
, prev
, end
);
2289 unmap_region(mm
, vma
, prev
, start
, end
);
2291 /* Fix up all other VM information */
2292 remove_vma_list(mm
, vma
);
2297 int vm_munmap(unsigned long start
, size_t len
)
2300 struct mm_struct
*mm
= current
->mm
;
2302 down_write(&mm
->mmap_sem
);
2303 ret
= do_munmap(mm
, start
, len
);
2304 up_write(&mm
->mmap_sem
);
2307 EXPORT_SYMBOL(vm_munmap
);
2309 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
2311 profile_munmap(addr
);
2312 return vm_munmap(addr
, len
);
2315 static inline void verify_mm_writelocked(struct mm_struct
*mm
)
2317 #ifdef CONFIG_DEBUG_VM
2318 if (unlikely(down_read_trylock(&mm
->mmap_sem
))) {
2320 up_read(&mm
->mmap_sem
);
2326 * this is really a simplified "do_mmap". it only handles
2327 * anonymous maps. eventually we may be able to do some
2328 * brk-specific accounting here.
2330 static unsigned long do_brk(unsigned long addr
, unsigned long len
)
2332 struct mm_struct
* mm
= current
->mm
;
2333 struct vm_area_struct
* vma
, * prev
;
2334 unsigned long flags
;
2335 struct rb_node
** rb_link
, * rb_parent
;
2336 pgoff_t pgoff
= addr
>> PAGE_SHIFT
;
2339 len
= PAGE_ALIGN(len
);
2343 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
2345 error
= get_unmapped_area(NULL
, addr
, len
, 0, MAP_FIXED
);
2346 if (error
& ~PAGE_MASK
)
2352 if (mm
->def_flags
& VM_LOCKED
) {
2353 unsigned long locked
, lock_limit
;
2354 locked
= len
>> PAGE_SHIFT
;
2355 locked
+= mm
->locked_vm
;
2356 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
2357 lock_limit
>>= PAGE_SHIFT
;
2358 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
2363 * mm->mmap_sem is required to protect against another thread
2364 * changing the mappings in case we sleep.
2366 verify_mm_writelocked(mm
);
2369 * Clear old maps. this also does some error checking for us
2372 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
)) {
2373 if (do_munmap(mm
, addr
, len
))
2378 /* Check against address space limits *after* clearing old maps... */
2379 if (!may_expand_vm(mm
, len
>> PAGE_SHIFT
))
2382 if (mm
->map_count
> sysctl_max_map_count
)
2385 if (security_vm_enough_memory_mm(mm
, len
>> PAGE_SHIFT
))
2388 /* Can we just expand an old private anonymous mapping? */
2389 vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, flags
,
2390 NULL
, NULL
, pgoff
, NULL
);
2395 * create a vma struct for an anonymous mapping
2397 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
2399 vm_unacct_memory(len
>> PAGE_SHIFT
);
2403 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
2405 vma
->vm_start
= addr
;
2406 vma
->vm_end
= addr
+ len
;
2407 vma
->vm_pgoff
= pgoff
;
2408 vma
->vm_flags
= flags
;
2409 vma
->vm_page_prot
= vm_get_page_prot(flags
);
2410 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2412 perf_event_mmap(vma
);
2413 mm
->total_vm
+= len
>> PAGE_SHIFT
;
2414 if (flags
& VM_LOCKED
) {
2415 if (!mlock_vma_pages_range(vma
, addr
, addr
+ len
))
2416 mm
->locked_vm
+= (len
>> PAGE_SHIFT
);
2421 unsigned long vm_brk(unsigned long addr
, unsigned long len
)
2423 struct mm_struct
*mm
= current
->mm
;
2426 down_write(&mm
->mmap_sem
);
2427 ret
= do_brk(addr
, len
);
2428 up_write(&mm
->mmap_sem
);
2431 EXPORT_SYMBOL(vm_brk
);
2433 /* Release all mmaps. */
2434 void exit_mmap(struct mm_struct
*mm
)
2436 struct mmu_gather tlb
;
2437 struct vm_area_struct
*vma
;
2438 unsigned long nr_accounted
= 0;
2440 /* mm's last user has gone, and its about to be pulled down */
2441 mmu_notifier_release(mm
);
2443 if (mm
->locked_vm
) {
2446 if (vma
->vm_flags
& VM_LOCKED
)
2447 munlock_vma_pages_all(vma
);
2455 if (!vma
) /* Can happen if dup_mmap() received an OOM */
2460 tlb_gather_mmu(&tlb
, mm
, 1);
2461 /* update_hiwater_rss(mm) here? but nobody should be looking */
2462 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2463 unmap_vmas(&tlb
, vma
, 0, -1);
2465 free_pgtables(&tlb
, vma
, FIRST_USER_ADDRESS
, 0);
2466 tlb_finish_mmu(&tlb
, 0, -1);
2469 * Walk the list again, actually closing and freeing it,
2470 * with preemption enabled, without holding any MM locks.
2473 if (vma
->vm_flags
& VM_ACCOUNT
)
2474 nr_accounted
+= vma_pages(vma
);
2475 vma
= remove_vma(vma
);
2477 vm_unacct_memory(nr_accounted
);
2479 WARN_ON(mm
->nr_ptes
> (FIRST_USER_ADDRESS
+PMD_SIZE
-1)>>PMD_SHIFT
);
2482 /* Insert vm structure into process list sorted by address
2483 * and into the inode's i_mmap tree. If vm_file is non-NULL
2484 * then i_mmap_mutex is taken here.
2486 int insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
2488 struct vm_area_struct
*prev
;
2489 struct rb_node
**rb_link
, *rb_parent
;
2492 * The vm_pgoff of a purely anonymous vma should be irrelevant
2493 * until its first write fault, when page's anon_vma and index
2494 * are set. But now set the vm_pgoff it will almost certainly
2495 * end up with (unless mremap moves it elsewhere before that
2496 * first wfault), so /proc/pid/maps tells a consistent story.
2498 * By setting it to reflect the virtual start address of the
2499 * vma, merges and splits can happen in a seamless way, just
2500 * using the existing file pgoff checks and manipulations.
2501 * Similarly in do_mmap_pgoff and in do_brk.
2503 if (!vma
->vm_file
) {
2504 BUG_ON(vma
->anon_vma
);
2505 vma
->vm_pgoff
= vma
->vm_start
>> PAGE_SHIFT
;
2507 if (find_vma_links(mm
, vma
->vm_start
, vma
->vm_end
,
2508 &prev
, &rb_link
, &rb_parent
))
2510 if ((vma
->vm_flags
& VM_ACCOUNT
) &&
2511 security_vm_enough_memory_mm(mm
, vma_pages(vma
)))
2514 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2519 * Copy the vma structure to a new location in the same mm,
2520 * prior to moving page table entries, to effect an mremap move.
2522 struct vm_area_struct
*copy_vma(struct vm_area_struct
**vmap
,
2523 unsigned long addr
, unsigned long len
, pgoff_t pgoff
,
2524 bool *need_rmap_locks
)
2526 struct vm_area_struct
*vma
= *vmap
;
2527 unsigned long vma_start
= vma
->vm_start
;
2528 struct mm_struct
*mm
= vma
->vm_mm
;
2529 struct vm_area_struct
*new_vma
, *prev
;
2530 struct rb_node
**rb_link
, *rb_parent
;
2531 struct mempolicy
*pol
;
2532 bool faulted_in_anon_vma
= true;
2535 * If anonymous vma has not yet been faulted, update new pgoff
2536 * to match new location, to increase its chance of merging.
2538 if (unlikely(!vma
->vm_file
&& !vma
->anon_vma
)) {
2539 pgoff
= addr
>> PAGE_SHIFT
;
2540 faulted_in_anon_vma
= false;
2543 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
))
2544 return NULL
; /* should never get here */
2545 new_vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vma
->vm_flags
,
2546 vma
->anon_vma
, vma
->vm_file
, pgoff
, vma_policy(vma
));
2549 * Source vma may have been merged into new_vma
2551 if (unlikely(vma_start
>= new_vma
->vm_start
&&
2552 vma_start
< new_vma
->vm_end
)) {
2554 * The only way we can get a vma_merge with
2555 * self during an mremap is if the vma hasn't
2556 * been faulted in yet and we were allowed to
2557 * reset the dst vma->vm_pgoff to the
2558 * destination address of the mremap to allow
2559 * the merge to happen. mremap must change the
2560 * vm_pgoff linearity between src and dst vmas
2561 * (in turn preventing a vma_merge) to be
2562 * safe. It is only safe to keep the vm_pgoff
2563 * linear if there are no pages mapped yet.
2565 VM_BUG_ON(faulted_in_anon_vma
);
2566 *vmap
= vma
= new_vma
;
2568 *need_rmap_locks
= (new_vma
->vm_pgoff
<= vma
->vm_pgoff
);
2570 new_vma
= kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
2573 new_vma
->vm_start
= addr
;
2574 new_vma
->vm_end
= addr
+ len
;
2575 new_vma
->vm_pgoff
= pgoff
;
2576 pol
= mpol_dup(vma_policy(vma
));
2579 vma_set_policy(new_vma
, pol
);
2580 INIT_LIST_HEAD(&new_vma
->anon_vma_chain
);
2581 if (anon_vma_clone(new_vma
, vma
))
2582 goto out_free_mempol
;
2583 if (new_vma
->vm_file
)
2584 get_file(new_vma
->vm_file
);
2585 if (new_vma
->vm_ops
&& new_vma
->vm_ops
->open
)
2586 new_vma
->vm_ops
->open(new_vma
);
2587 vma_link(mm
, new_vma
, prev
, rb_link
, rb_parent
);
2588 *need_rmap_locks
= false;
2596 kmem_cache_free(vm_area_cachep
, new_vma
);
2601 * Return true if the calling process may expand its vm space by the passed
2604 int may_expand_vm(struct mm_struct
*mm
, unsigned long npages
)
2606 unsigned long cur
= mm
->total_vm
; /* pages */
2609 lim
= rlimit(RLIMIT_AS
) >> PAGE_SHIFT
;
2611 if (cur
+ npages
> lim
)
2617 static int special_mapping_fault(struct vm_area_struct
*vma
,
2618 struct vm_fault
*vmf
)
2621 struct page
**pages
;
2624 * special mappings have no vm_file, and in that case, the mm
2625 * uses vm_pgoff internally. So we have to subtract it from here.
2626 * We are allowed to do this because we are the mm; do not copy
2627 * this code into drivers!
2629 pgoff
= vmf
->pgoff
- vma
->vm_pgoff
;
2631 for (pages
= vma
->vm_private_data
; pgoff
&& *pages
; ++pages
)
2635 struct page
*page
= *pages
;
2641 return VM_FAULT_SIGBUS
;
2645 * Having a close hook prevents vma merging regardless of flags.
2647 static void special_mapping_close(struct vm_area_struct
*vma
)
2651 static const struct vm_operations_struct special_mapping_vmops
= {
2652 .close
= special_mapping_close
,
2653 .fault
= special_mapping_fault
,
2657 * Called with mm->mmap_sem held for writing.
2658 * Insert a new vma covering the given region, with the given flags.
2659 * Its pages are supplied by the given array of struct page *.
2660 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2661 * The region past the last page supplied will always produce SIGBUS.
2662 * The array pointer and the pages it points to are assumed to stay alive
2663 * for as long as this mapping might exist.
2665 int install_special_mapping(struct mm_struct
*mm
,
2666 unsigned long addr
, unsigned long len
,
2667 unsigned long vm_flags
, struct page
**pages
)
2670 struct vm_area_struct
*vma
;
2672 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
2673 if (unlikely(vma
== NULL
))
2676 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
2678 vma
->vm_start
= addr
;
2679 vma
->vm_end
= addr
+ len
;
2681 vma
->vm_flags
= vm_flags
| mm
->def_flags
| VM_DONTEXPAND
;
2682 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
2684 vma
->vm_ops
= &special_mapping_vmops
;
2685 vma
->vm_private_data
= pages
;
2687 ret
= insert_vm_struct(mm
, vma
);
2691 mm
->total_vm
+= len
>> PAGE_SHIFT
;
2693 perf_event_mmap(vma
);
2698 kmem_cache_free(vm_area_cachep
, vma
);
2702 static DEFINE_MUTEX(mm_all_locks_mutex
);
2704 static void vm_lock_anon_vma(struct mm_struct
*mm
, struct anon_vma
*anon_vma
)
2706 if (!test_bit(0, (unsigned long *) &anon_vma
->root
->rb_root
.rb_node
)) {
2708 * The LSB of head.next can't change from under us
2709 * because we hold the mm_all_locks_mutex.
2711 mutex_lock_nest_lock(&anon_vma
->root
->mutex
, &mm
->mmap_sem
);
2713 * We can safely modify head.next after taking the
2714 * anon_vma->root->mutex. If some other vma in this mm shares
2715 * the same anon_vma we won't take it again.
2717 * No need of atomic instructions here, head.next
2718 * can't change from under us thanks to the
2719 * anon_vma->root->mutex.
2721 if (__test_and_set_bit(0, (unsigned long *)
2722 &anon_vma
->root
->rb_root
.rb_node
))
2727 static void vm_lock_mapping(struct mm_struct
*mm
, struct address_space
*mapping
)
2729 if (!test_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
)) {
2731 * AS_MM_ALL_LOCKS can't change from under us because
2732 * we hold the mm_all_locks_mutex.
2734 * Operations on ->flags have to be atomic because
2735 * even if AS_MM_ALL_LOCKS is stable thanks to the
2736 * mm_all_locks_mutex, there may be other cpus
2737 * changing other bitflags in parallel to us.
2739 if (test_and_set_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
))
2741 mutex_lock_nest_lock(&mapping
->i_mmap_mutex
, &mm
->mmap_sem
);
2746 * This operation locks against the VM for all pte/vma/mm related
2747 * operations that could ever happen on a certain mm. This includes
2748 * vmtruncate, try_to_unmap, and all page faults.
2750 * The caller must take the mmap_sem in write mode before calling
2751 * mm_take_all_locks(). The caller isn't allowed to release the
2752 * mmap_sem until mm_drop_all_locks() returns.
2754 * mmap_sem in write mode is required in order to block all operations
2755 * that could modify pagetables and free pages without need of
2756 * altering the vma layout (for example populate_range() with
2757 * nonlinear vmas). It's also needed in write mode to avoid new
2758 * anon_vmas to be associated with existing vmas.
2760 * A single task can't take more than one mm_take_all_locks() in a row
2761 * or it would deadlock.
2763 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
2764 * mapping->flags avoid to take the same lock twice, if more than one
2765 * vma in this mm is backed by the same anon_vma or address_space.
2767 * We can take all the locks in random order because the VM code
2768 * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
2769 * takes more than one of them in a row. Secondly we're protected
2770 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2772 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2773 * that may have to take thousand of locks.
2775 * mm_take_all_locks() can fail if it's interrupted by signals.
2777 int mm_take_all_locks(struct mm_struct
*mm
)
2779 struct vm_area_struct
*vma
;
2780 struct anon_vma_chain
*avc
;
2782 BUG_ON(down_read_trylock(&mm
->mmap_sem
));
2784 mutex_lock(&mm_all_locks_mutex
);
2786 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
2787 if (signal_pending(current
))
2789 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
)
2790 vm_lock_mapping(mm
, vma
->vm_file
->f_mapping
);
2793 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
2794 if (signal_pending(current
))
2797 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
2798 vm_lock_anon_vma(mm
, avc
->anon_vma
);
2804 mm_drop_all_locks(mm
);
2808 static void vm_unlock_anon_vma(struct anon_vma
*anon_vma
)
2810 if (test_bit(0, (unsigned long *) &anon_vma
->root
->rb_root
.rb_node
)) {
2812 * The LSB of head.next can't change to 0 from under
2813 * us because we hold the mm_all_locks_mutex.
2815 * We must however clear the bitflag before unlocking
2816 * the vma so the users using the anon_vma->rb_root will
2817 * never see our bitflag.
2819 * No need of atomic instructions here, head.next
2820 * can't change from under us until we release the
2821 * anon_vma->root->mutex.
2823 if (!__test_and_clear_bit(0, (unsigned long *)
2824 &anon_vma
->root
->rb_root
.rb_node
))
2826 anon_vma_unlock(anon_vma
);
2830 static void vm_unlock_mapping(struct address_space
*mapping
)
2832 if (test_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
)) {
2834 * AS_MM_ALL_LOCKS can't change to 0 from under us
2835 * because we hold the mm_all_locks_mutex.
2837 mutex_unlock(&mapping
->i_mmap_mutex
);
2838 if (!test_and_clear_bit(AS_MM_ALL_LOCKS
,
2845 * The mmap_sem cannot be released by the caller until
2846 * mm_drop_all_locks() returns.
2848 void mm_drop_all_locks(struct mm_struct
*mm
)
2850 struct vm_area_struct
*vma
;
2851 struct anon_vma_chain
*avc
;
2853 BUG_ON(down_read_trylock(&mm
->mmap_sem
));
2854 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex
));
2856 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
2858 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
2859 vm_unlock_anon_vma(avc
->anon_vma
);
2860 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
)
2861 vm_unlock_mapping(vma
->vm_file
->f_mapping
);
2864 mutex_unlock(&mm_all_locks_mutex
);
2868 * initialise the VMA slab
2870 void __init
mmap_init(void)
2874 ret
= percpu_counter_init(&vm_committed_as
, 0);