6 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/backing-dev.h>
15 #include <linux/vmacache.h>
16 #include <linux/shm.h>
17 #include <linux/mman.h>
18 #include <linux/pagemap.h>
19 #include <linux/swap.h>
20 #include <linux/syscalls.h>
21 #include <linux/capability.h>
22 #include <linux/init.h>
23 #include <linux/file.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/hugetlb.h>
28 #include <linux/profile.h>
29 #include <linux/export.h>
30 #include <linux/mount.h>
31 #include <linux/mempolicy.h>
32 #include <linux/rmap.h>
33 #include <linux/mmu_notifier.h>
34 #include <linux/perf_event.h>
35 #include <linux/audit.h>
36 #include <linux/khugepaged.h>
37 #include <linux/uprobes.h>
38 #include <linux/rbtree_augmented.h>
39 #include <linux/sched/sysctl.h>
40 #include <linux/notifier.h>
41 #include <linux/memory.h>
42 #include <linux/printk.h>
44 #include <asm/uaccess.h>
45 #include <asm/cacheflush.h>
47 #include <asm/mmu_context.h>
51 #ifndef arch_mmap_check
52 #define arch_mmap_check(addr, len, flags) (0)
55 #ifndef arch_rebalance_pgtables
56 #define arch_rebalance_pgtables(addr, len) (addr)
59 static void unmap_region(struct mm_struct
*mm
,
60 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
61 unsigned long start
, unsigned long end
);
63 /* description of effects of mapping type and prot in current implementation.
64 * this is due to the limited x86 page protection hardware. The expected
65 * behavior is in parens:
68 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
69 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
70 * w: (no) no w: (no) no w: (yes) yes w: (no) no
71 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
73 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
74 * w: (no) no w: (no) no w: (copy) copy w: (no) no
75 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
78 pgprot_t protection_map
[16] = {
79 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
80 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
83 pgprot_t
vm_get_page_prot(unsigned long vm_flags
)
85 return __pgprot(pgprot_val(protection_map
[vm_flags
&
86 (VM_READ
|VM_WRITE
|VM_EXEC
|VM_SHARED
)]) |
87 pgprot_val(arch_vm_get_page_prot(vm_flags
)));
89 EXPORT_SYMBOL(vm_get_page_prot
);
91 int sysctl_overcommit_memory __read_mostly
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
92 int sysctl_overcommit_ratio __read_mostly
= 50; /* default is 50% */
93 unsigned long sysctl_overcommit_kbytes __read_mostly
;
94 int sysctl_max_map_count __read_mostly
= DEFAULT_MAX_MAP_COUNT
;
95 unsigned long sysctl_user_reserve_kbytes __read_mostly
= 1UL << 17; /* 128MB */
96 unsigned long sysctl_admin_reserve_kbytes __read_mostly
= 1UL << 13; /* 8MB */
98 * Make sure vm_committed_as in one cacheline and not cacheline shared with
99 * other variables. It can be updated by several CPUs frequently.
101 struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp
;
104 * The global memory commitment made in the system can be a metric
105 * that can be used to drive ballooning decisions when Linux is hosted
106 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
107 * balancing memory across competing virtual machines that are hosted.
108 * Several metrics drive this policy engine including the guest reported
111 unsigned long vm_memory_committed(void)
113 return percpu_counter_read_positive(&vm_committed_as
);
115 EXPORT_SYMBOL_GPL(vm_memory_committed
);
118 * Check that a process has enough memory to allocate a new virtual
119 * mapping. 0 means there is enough memory for the allocation to
120 * succeed and -ENOMEM implies there is not.
122 * We currently support three overcommit policies, which are set via the
123 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
125 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
126 * Additional code 2002 Jul 20 by Robert Love.
128 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
130 * Note this is a helper function intended to be used by LSMs which
131 * wish to use this logic.
133 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
135 long free
, allowed
, reserve
;
137 vm_acct_memory(pages
);
140 * Sometimes we want to use more memory than we have
142 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
145 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
146 free
= global_page_state(NR_FREE_PAGES
);
147 free
+= global_page_state(NR_FILE_PAGES
);
150 * shmem pages shouldn't be counted as free in this
151 * case, they can't be purged, only swapped out, and
152 * that won't affect the overall amount of available
153 * memory in the system.
155 free
-= global_page_state(NR_SHMEM
);
157 free
+= get_nr_swap_pages();
160 * Any slabs which are created with the
161 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
162 * which are reclaimable, under pressure. The dentry
163 * cache and most inode caches should fall into this
165 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
168 * Leave reserved pages. The pages are not for anonymous pages.
170 if (free
<= totalreserve_pages
)
173 free
-= totalreserve_pages
;
176 * Reserve some for root
179 free
-= sysctl_admin_reserve_kbytes
>> (PAGE_SHIFT
- 10);
187 allowed
= vm_commit_limit();
189 * Reserve some for root
192 allowed
-= sysctl_admin_reserve_kbytes
>> (PAGE_SHIFT
- 10);
195 * Don't let a single process grow so big a user can't recover
198 reserve
= sysctl_user_reserve_kbytes
>> (PAGE_SHIFT
- 10);
199 allowed
-= min_t(long, mm
->total_vm
/ 32, reserve
);
202 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
205 vm_unacct_memory(pages
);
211 * Requires inode->i_mapping->i_mmap_mutex
213 static void __remove_shared_vm_struct(struct vm_area_struct
*vma
,
214 struct file
*file
, struct address_space
*mapping
)
216 if (vma
->vm_flags
& VM_DENYWRITE
)
217 atomic_inc(&file_inode(file
)->i_writecount
);
218 if (vma
->vm_flags
& VM_SHARED
)
219 mapping
->i_mmap_writable
--;
221 flush_dcache_mmap_lock(mapping
);
222 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
223 flush_dcache_mmap_unlock(mapping
);
227 * Unlink a file-based vm structure from its interval tree, to hide
228 * vma from rmap and vmtruncate before freeing its page tables.
230 void unlink_file_vma(struct vm_area_struct
*vma
)
232 struct file
*file
= vma
->vm_file
;
235 struct address_space
*mapping
= file
->f_mapping
;
236 mutex_lock(&mapping
->i_mmap_mutex
);
237 __remove_shared_vm_struct(vma
, file
, mapping
);
238 mutex_unlock(&mapping
->i_mmap_mutex
);
243 * Close a vm structure and free it, returning the next.
245 static struct vm_area_struct
*remove_vma(struct vm_area_struct
*vma
)
247 struct vm_area_struct
*next
= vma
->vm_next
;
250 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
251 vma
->vm_ops
->close(vma
);
254 mpol_put(vma_policy(vma
));
255 kmem_cache_free(vm_area_cachep
, vma
);
259 static unsigned long do_brk(unsigned long addr
, unsigned long len
);
261 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
263 unsigned long rlim
, retval
;
264 unsigned long newbrk
, oldbrk
;
265 struct mm_struct
*mm
= current
->mm
;
266 struct vm_area_struct
*next
;
267 unsigned long min_brk
;
270 down_write(&mm
->mmap_sem
);
272 #ifdef CONFIG_COMPAT_BRK
274 * CONFIG_COMPAT_BRK can still be overridden by setting
275 * randomize_va_space to 2, which will still cause mm->start_brk
276 * to be arbitrarily shifted
278 if (current
->brk_randomized
)
279 min_brk
= mm
->start_brk
;
281 min_brk
= mm
->end_data
;
283 min_brk
= mm
->start_brk
;
289 * Check against rlimit here. If this check is done later after the test
290 * of oldbrk with newbrk then it can escape the test and let the data
291 * segment grow beyond its set limit the in case where the limit is
292 * not page aligned -Ram Gupta
294 rlim
= rlimit(RLIMIT_DATA
);
295 if (rlim
< RLIM_INFINITY
&& (brk
- mm
->start_brk
) +
296 (mm
->end_data
- mm
->start_data
) > rlim
)
299 newbrk
= PAGE_ALIGN(brk
);
300 oldbrk
= PAGE_ALIGN(mm
->brk
);
301 if (oldbrk
== newbrk
)
304 /* Always allow shrinking brk. */
305 if (brk
<= mm
->brk
) {
306 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
311 /* Check against existing mmap mappings. */
312 next
= find_vma(mm
, oldbrk
);
313 if (next
&& newbrk
+ PAGE_SIZE
> vm_start_gap(next
))
316 /* Ok, looks good - let it rip. */
317 if (do_brk(oldbrk
, newbrk
-oldbrk
) != oldbrk
)
322 populate
= newbrk
> oldbrk
&& (mm
->def_flags
& VM_LOCKED
) != 0;
323 up_write(&mm
->mmap_sem
);
325 mm_populate(oldbrk
, newbrk
- oldbrk
);
330 up_write(&mm
->mmap_sem
);
334 static long vma_compute_subtree_gap(struct vm_area_struct
*vma
)
336 unsigned long max
, prev_end
, subtree_gap
;
339 * Note: in the rare case of a VM_GROWSDOWN above a VM_GROWSUP, we
340 * allow two stack_guard_gaps between them here, and when choosing
341 * an unmapped area; whereas when expanding we only require one.
342 * That's a little inconsistent, but keeps the code here simpler.
344 max
= vm_start_gap(vma
);
346 prev_end
= vm_end_gap(vma
->vm_prev
);
352 if (vma
->vm_rb
.rb_left
) {
353 subtree_gap
= rb_entry(vma
->vm_rb
.rb_left
,
354 struct vm_area_struct
, vm_rb
)->rb_subtree_gap
;
355 if (subtree_gap
> max
)
358 if (vma
->vm_rb
.rb_right
) {
359 subtree_gap
= rb_entry(vma
->vm_rb
.rb_right
,
360 struct vm_area_struct
, vm_rb
)->rb_subtree_gap
;
361 if (subtree_gap
> max
)
367 #ifdef CONFIG_DEBUG_VM_RB
368 static int browse_rb(struct rb_root
*root
)
370 int i
= 0, j
, bug
= 0;
371 struct rb_node
*nd
, *pn
= NULL
;
372 unsigned long prev
= 0, pend
= 0;
374 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
375 struct vm_area_struct
*vma
;
376 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
377 if (vma
->vm_start
< prev
) {
378 pr_info("vm_start %lx prev %lx\n", vma
->vm_start
, prev
);
381 if (vma
->vm_start
< pend
) {
382 pr_info("vm_start %lx pend %lx\n", vma
->vm_start
, pend
);
385 if (vma
->vm_start
> vma
->vm_end
) {
386 pr_info("vm_end %lx < vm_start %lx\n",
387 vma
->vm_end
, vma
->vm_start
);
390 if (vma
->rb_subtree_gap
!= vma_compute_subtree_gap(vma
)) {
391 pr_info("free gap %lx, correct %lx\n",
393 vma_compute_subtree_gap(vma
));
398 prev
= vma
->vm_start
;
402 for (nd
= pn
; nd
; nd
= rb_prev(nd
))
405 pr_info("backwards %d, forwards %d\n", j
, i
);
411 static void validate_mm_rb(struct rb_root
*root
, struct vm_area_struct
*ignore
)
415 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
416 struct vm_area_struct
*vma
;
417 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
418 BUG_ON(vma
!= ignore
&&
419 vma
->rb_subtree_gap
!= vma_compute_subtree_gap(vma
));
423 static void validate_mm(struct mm_struct
*mm
)
427 unsigned long highest_address
= 0;
428 struct vm_area_struct
*vma
= mm
->mmap
;
430 struct anon_vma
*anon_vma
= vma
->anon_vma
;
431 struct anon_vma_chain
*avc
;
434 anon_vma_lock_read(anon_vma
);
435 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
436 anon_vma_interval_tree_verify(avc
);
437 anon_vma_unlock_read(anon_vma
);
440 highest_address
= vm_end_gap(vma
);
444 if (i
!= mm
->map_count
) {
445 pr_info("map_count %d vm_next %d\n", mm
->map_count
, i
);
448 if (highest_address
!= mm
->highest_vm_end
) {
449 pr_info("mm->highest_vm_end %lx, found %lx\n",
450 mm
->highest_vm_end
, highest_address
);
453 i
= browse_rb(&mm
->mm_rb
);
454 if (i
!= mm
->map_count
) {
455 pr_info("map_count %d rb %d\n", mm
->map_count
, i
);
461 #define validate_mm_rb(root, ignore) do { } while (0)
462 #define validate_mm(mm) do { } while (0)
465 RB_DECLARE_CALLBACKS(static, vma_gap_callbacks
, struct vm_area_struct
, vm_rb
,
466 unsigned long, rb_subtree_gap
, vma_compute_subtree_gap
)
469 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
470 * vma->vm_prev->vm_end values changed, without modifying the vma's position
473 static void vma_gap_update(struct vm_area_struct
*vma
)
476 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
477 * function that does exacltly what we want.
479 vma_gap_callbacks_propagate(&vma
->vm_rb
, NULL
);
482 static inline void vma_rb_insert(struct vm_area_struct
*vma
,
483 struct rb_root
*root
)
485 /* All rb_subtree_gap values must be consistent prior to insertion */
486 validate_mm_rb(root
, NULL
);
488 rb_insert_augmented(&vma
->vm_rb
, root
, &vma_gap_callbacks
);
491 static void vma_rb_erase(struct vm_area_struct
*vma
, struct rb_root
*root
)
494 * All rb_subtree_gap values must be consistent prior to erase,
495 * with the possible exception of the vma being erased.
497 validate_mm_rb(root
, vma
);
500 * Note rb_erase_augmented is a fairly large inline function,
501 * so make sure we instantiate it only once with our desired
502 * augmented rbtree callbacks.
504 rb_erase_augmented(&vma
->vm_rb
, root
, &vma_gap_callbacks
);
508 * vma has some anon_vma assigned, and is already inserted on that
509 * anon_vma's interval trees.
511 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
512 * vma must be removed from the anon_vma's interval trees using
513 * anon_vma_interval_tree_pre_update_vma().
515 * After the update, the vma will be reinserted using
516 * anon_vma_interval_tree_post_update_vma().
518 * The entire update must be protected by exclusive mmap_sem and by
519 * the root anon_vma's mutex.
522 anon_vma_interval_tree_pre_update_vma(struct vm_area_struct
*vma
)
524 struct anon_vma_chain
*avc
;
526 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
527 anon_vma_interval_tree_remove(avc
, &avc
->anon_vma
->rb_root
);
531 anon_vma_interval_tree_post_update_vma(struct vm_area_struct
*vma
)
533 struct anon_vma_chain
*avc
;
535 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
536 anon_vma_interval_tree_insert(avc
, &avc
->anon_vma
->rb_root
);
539 static int find_vma_links(struct mm_struct
*mm
, unsigned long addr
,
540 unsigned long end
, struct vm_area_struct
**pprev
,
541 struct rb_node
***rb_link
, struct rb_node
**rb_parent
)
543 struct rb_node
**__rb_link
, *__rb_parent
, *rb_prev
;
545 __rb_link
= &mm
->mm_rb
.rb_node
;
546 rb_prev
= __rb_parent
= NULL
;
549 struct vm_area_struct
*vma_tmp
;
551 __rb_parent
= *__rb_link
;
552 vma_tmp
= rb_entry(__rb_parent
, struct vm_area_struct
, vm_rb
);
554 if (vma_tmp
->vm_end
> addr
) {
555 /* Fail if an existing vma overlaps the area */
556 if (vma_tmp
->vm_start
< end
)
558 __rb_link
= &__rb_parent
->rb_left
;
560 rb_prev
= __rb_parent
;
561 __rb_link
= &__rb_parent
->rb_right
;
567 *pprev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
568 *rb_link
= __rb_link
;
569 *rb_parent
= __rb_parent
;
573 static unsigned long count_vma_pages_range(struct mm_struct
*mm
,
574 unsigned long addr
, unsigned long end
)
576 unsigned long nr_pages
= 0;
577 struct vm_area_struct
*vma
;
579 /* Find first overlaping mapping */
580 vma
= find_vma_intersection(mm
, addr
, end
);
584 nr_pages
= (min(end
, vma
->vm_end
) -
585 max(addr
, vma
->vm_start
)) >> PAGE_SHIFT
;
587 /* Iterate over the rest of the overlaps */
588 for (vma
= vma
->vm_next
; vma
; vma
= vma
->vm_next
) {
589 unsigned long overlap_len
;
591 if (vma
->vm_start
> end
)
594 overlap_len
= min(end
, vma
->vm_end
) - vma
->vm_start
;
595 nr_pages
+= overlap_len
>> PAGE_SHIFT
;
601 void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
602 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
604 /* Update tracking information for the gap following the new vma. */
606 vma_gap_update(vma
->vm_next
);
608 mm
->highest_vm_end
= vm_end_gap(vma
);
611 * vma->vm_prev wasn't known when we followed the rbtree to find the
612 * correct insertion point for that vma. As a result, we could not
613 * update the vma vm_rb parents rb_subtree_gap values on the way down.
614 * So, we first insert the vma with a zero rb_subtree_gap value
615 * (to be consistent with what we did on the way down), and then
616 * immediately update the gap to the correct value. Finally we
617 * rebalance the rbtree after all augmented values have been set.
619 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
620 vma
->rb_subtree_gap
= 0;
622 vma_rb_insert(vma
, &mm
->mm_rb
);
625 static void __vma_link_file(struct vm_area_struct
*vma
)
631 struct address_space
*mapping
= file
->f_mapping
;
633 if (vma
->vm_flags
& VM_DENYWRITE
)
634 atomic_dec(&file_inode(file
)->i_writecount
);
635 if (vma
->vm_flags
& VM_SHARED
)
636 mapping
->i_mmap_writable
++;
638 flush_dcache_mmap_lock(mapping
);
639 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
640 flush_dcache_mmap_unlock(mapping
);
645 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
646 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
647 struct rb_node
*rb_parent
)
649 __vma_link_list(mm
, vma
, prev
, rb_parent
);
650 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
653 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
654 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
655 struct rb_node
*rb_parent
)
657 struct address_space
*mapping
= NULL
;
660 mapping
= vma
->vm_file
->f_mapping
;
661 mutex_lock(&mapping
->i_mmap_mutex
);
664 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
665 __vma_link_file(vma
);
668 mutex_unlock(&mapping
->i_mmap_mutex
);
675 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
676 * mm's list and rbtree. It has already been inserted into the interval tree.
678 static void __insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
680 struct vm_area_struct
*prev
;
681 struct rb_node
**rb_link
, *rb_parent
;
683 if (find_vma_links(mm
, vma
->vm_start
, vma
->vm_end
,
684 &prev
, &rb_link
, &rb_parent
))
686 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
691 __vma_unlink(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
692 struct vm_area_struct
*prev
)
694 struct vm_area_struct
*next
;
696 vma_rb_erase(vma
, &mm
->mm_rb
);
697 prev
->vm_next
= next
= vma
->vm_next
;
699 next
->vm_prev
= prev
;
702 vmacache_invalidate(mm
);
706 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
707 * is already present in an i_mmap tree without adjusting the tree.
708 * The following helper function should be used when such adjustments
709 * are necessary. The "insert" vma (if any) is to be inserted
710 * before we drop the necessary locks.
712 int vma_adjust(struct vm_area_struct
*vma
, unsigned long start
,
713 unsigned long end
, pgoff_t pgoff
, struct vm_area_struct
*insert
)
715 struct mm_struct
*mm
= vma
->vm_mm
;
716 struct vm_area_struct
*next
= vma
->vm_next
;
717 struct vm_area_struct
*importer
= NULL
;
718 struct address_space
*mapping
= NULL
;
719 struct rb_root
*root
= NULL
;
720 struct anon_vma
*anon_vma
= NULL
;
721 struct file
*file
= vma
->vm_file
;
722 bool start_changed
= false, end_changed
= false;
723 long adjust_next
= 0;
726 if (next
&& !insert
) {
727 struct vm_area_struct
*exporter
= NULL
;
729 if (end
>= next
->vm_end
) {
731 * vma expands, overlapping all the next, and
732 * perhaps the one after too (mprotect case 6).
734 again
: remove_next
= 1 + (end
> next
->vm_end
);
738 } else if (end
> next
->vm_start
) {
740 * vma expands, overlapping part of the next:
741 * mprotect case 5 shifting the boundary up.
743 adjust_next
= (end
- next
->vm_start
) >> PAGE_SHIFT
;
746 } else if (end
< vma
->vm_end
) {
748 * vma shrinks, and !insert tells it's not
749 * split_vma inserting another: so it must be
750 * mprotect case 4 shifting the boundary down.
752 adjust_next
= - ((vma
->vm_end
- end
) >> PAGE_SHIFT
);
758 * Easily overlooked: when mprotect shifts the boundary,
759 * make sure the expanding vma has anon_vma set if the
760 * shrinking vma had, to cover any anon pages imported.
762 if (exporter
&& exporter
->anon_vma
&& !importer
->anon_vma
) {
765 importer
->anon_vma
= exporter
->anon_vma
;
766 error
= anon_vma_clone(importer
, exporter
);
773 mapping
= file
->f_mapping
;
774 root
= &mapping
->i_mmap
;
775 uprobe_munmap(vma
, vma
->vm_start
, vma
->vm_end
);
778 uprobe_munmap(next
, next
->vm_start
, next
->vm_end
);
780 mutex_lock(&mapping
->i_mmap_mutex
);
783 * Put into interval tree now, so instantiated pages
784 * are visible to arm/parisc __flush_dcache_page
785 * throughout; but we cannot insert into address
786 * space until vma start or end is updated.
788 __vma_link_file(insert
);
792 vma_adjust_trans_huge(vma
, start
, end
, adjust_next
);
794 anon_vma
= vma
->anon_vma
;
795 if (!anon_vma
&& adjust_next
)
796 anon_vma
= next
->anon_vma
;
798 VM_BUG_ON(adjust_next
&& next
->anon_vma
&&
799 anon_vma
!= next
->anon_vma
);
800 anon_vma_lock_write(anon_vma
);
801 anon_vma_interval_tree_pre_update_vma(vma
);
803 anon_vma_interval_tree_pre_update_vma(next
);
807 flush_dcache_mmap_lock(mapping
);
808 vma_interval_tree_remove(vma
, root
);
810 vma_interval_tree_remove(next
, root
);
813 if (start
!= vma
->vm_start
) {
814 vma
->vm_start
= start
;
815 start_changed
= true;
817 if (end
!= vma
->vm_end
) {
821 vma
->vm_pgoff
= pgoff
;
823 next
->vm_start
+= adjust_next
<< PAGE_SHIFT
;
824 next
->vm_pgoff
+= adjust_next
;
829 vma_interval_tree_insert(next
, root
);
830 vma_interval_tree_insert(vma
, root
);
831 flush_dcache_mmap_unlock(mapping
);
836 * vma_merge has merged next into vma, and needs
837 * us to remove next before dropping the locks.
839 __vma_unlink(mm
, next
, vma
);
841 __remove_shared_vm_struct(next
, file
, mapping
);
844 * split_vma has split insert from vma, and needs
845 * us to insert it before dropping the locks
846 * (it may either follow vma or precede it).
848 __insert_vm_struct(mm
, insert
);
854 mm
->highest_vm_end
= vm_end_gap(vma
);
855 else if (!adjust_next
)
856 vma_gap_update(next
);
861 anon_vma_interval_tree_post_update_vma(vma
);
863 anon_vma_interval_tree_post_update_vma(next
);
864 anon_vma_unlock_write(anon_vma
);
867 mutex_unlock(&mapping
->i_mmap_mutex
);
878 uprobe_munmap(next
, next
->vm_start
, next
->vm_end
);
882 anon_vma_merge(vma
, next
);
884 mpol_put(vma_policy(next
));
885 kmem_cache_free(vm_area_cachep
, next
);
887 * In mprotect's case 6 (see comments on vma_merge),
888 * we must remove another next too. It would clutter
889 * up the code too much to do both in one go.
892 if (remove_next
== 2)
895 vma_gap_update(next
);
897 VM_WARN_ON(mm
->highest_vm_end
!= vm_end_gap(vma
));
908 * If the vma has a ->close operation then the driver probably needs to release
909 * per-vma resources, so we don't attempt to merge those.
911 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
912 struct file
*file
, unsigned long vm_flags
)
915 * VM_SOFTDIRTY should not prevent from VMA merging, if we
916 * match the flags but dirty bit -- the caller should mark
917 * merged VMA as dirty. If dirty bit won't be excluded from
918 * comparison, we increase pressue on the memory system forcing
919 * the kernel to generate new VMAs when old one could be
922 if ((vma
->vm_flags
^ vm_flags
) & ~VM_SOFTDIRTY
)
924 if (vma
->vm_file
!= file
)
926 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
931 static inline int is_mergeable_anon_vma(struct anon_vma
*anon_vma1
,
932 struct anon_vma
*anon_vma2
,
933 struct vm_area_struct
*vma
)
936 * The list_is_singular() test is to avoid merging VMA cloned from
937 * parents. This can improve scalability caused by anon_vma lock.
939 if ((!anon_vma1
|| !anon_vma2
) && (!vma
||
940 list_is_singular(&vma
->anon_vma_chain
)))
942 return anon_vma1
== anon_vma2
;
946 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
947 * in front of (at a lower virtual address and file offset than) the vma.
949 * We cannot merge two vmas if they have differently assigned (non-NULL)
950 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
952 * We don't check here for the merged mmap wrapping around the end of pagecache
953 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
954 * wrap, nor mmaps which cover the final page at index -1UL.
957 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
958 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
960 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
961 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
, vma
)) {
962 if (vma
->vm_pgoff
== vm_pgoff
)
969 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
970 * beyond (at a higher virtual address and file offset than) the vma.
972 * We cannot merge two vmas if they have differently assigned (non-NULL)
973 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
976 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
977 struct anon_vma
*anon_vma
, struct file
*file
, pgoff_t vm_pgoff
)
979 if (is_mergeable_vma(vma
, file
, vm_flags
) &&
980 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
, vma
)) {
982 vm_pglen
= vma_pages(vma
);
983 if (vma
->vm_pgoff
+ vm_pglen
== vm_pgoff
)
990 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
991 * whether that can be merged with its predecessor or its successor.
992 * Or both (it neatly fills a hole).
994 * In most cases - when called for mmap, brk or mremap - [addr,end) is
995 * certain not to be mapped by the time vma_merge is called; but when
996 * called for mprotect, it is certain to be already mapped (either at
997 * an offset within prev, or at the start of next), and the flags of
998 * this area are about to be changed to vm_flags - and the no-change
999 * case has already been eliminated.
1001 * The following mprotect cases have to be considered, where AAAA is
1002 * the area passed down from mprotect_fixup, never extending beyond one
1003 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
1005 * AAAA AAAA AAAA AAAA
1006 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
1007 * cannot merge might become might become might become
1008 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
1009 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
1010 * mremap move: PPPPNNNNNNNN 8
1012 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
1013 * might become case 1 below case 2 below case 3 below
1015 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
1016 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
1018 struct vm_area_struct
*vma_merge(struct mm_struct
*mm
,
1019 struct vm_area_struct
*prev
, unsigned long addr
,
1020 unsigned long end
, unsigned long vm_flags
,
1021 struct anon_vma
*anon_vma
, struct file
*file
,
1022 pgoff_t pgoff
, struct mempolicy
*policy
)
1024 pgoff_t pglen
= (end
- addr
) >> PAGE_SHIFT
;
1025 struct vm_area_struct
*area
, *next
;
1029 * We later require that vma->vm_flags == vm_flags,
1030 * so this tests vma->vm_flags & VM_SPECIAL, too.
1032 if (vm_flags
& VM_SPECIAL
)
1036 next
= prev
->vm_next
;
1040 if (next
&& next
->vm_end
== end
) /* cases 6, 7, 8 */
1041 next
= next
->vm_next
;
1044 * Can it merge with the predecessor?
1046 if (prev
&& prev
->vm_end
== addr
&&
1047 mpol_equal(vma_policy(prev
), policy
) &&
1048 can_vma_merge_after(prev
, vm_flags
,
1049 anon_vma
, file
, pgoff
)) {
1051 * OK, it can. Can we now merge in the successor as well?
1053 if (next
&& end
== next
->vm_start
&&
1054 mpol_equal(policy
, vma_policy(next
)) &&
1055 can_vma_merge_before(next
, vm_flags
,
1056 anon_vma
, file
, pgoff
+pglen
) &&
1057 is_mergeable_anon_vma(prev
->anon_vma
,
1058 next
->anon_vma
, NULL
)) {
1060 err
= vma_adjust(prev
, prev
->vm_start
,
1061 next
->vm_end
, prev
->vm_pgoff
, NULL
);
1062 } else /* cases 2, 5, 7 */
1063 err
= vma_adjust(prev
, prev
->vm_start
,
1064 end
, prev
->vm_pgoff
, NULL
);
1067 khugepaged_enter_vma_merge(prev
, vm_flags
);
1072 * Can this new request be merged in front of next?
1074 if (next
&& end
== next
->vm_start
&&
1075 mpol_equal(policy
, vma_policy(next
)) &&
1076 can_vma_merge_before(next
, vm_flags
,
1077 anon_vma
, file
, pgoff
+pglen
)) {
1078 if (prev
&& addr
< prev
->vm_end
) /* case 4 */
1079 err
= vma_adjust(prev
, prev
->vm_start
,
1080 addr
, prev
->vm_pgoff
, NULL
);
1081 else /* cases 3, 8 */
1082 err
= vma_adjust(area
, addr
, next
->vm_end
,
1083 next
->vm_pgoff
- pglen
, NULL
);
1086 khugepaged_enter_vma_merge(area
, vm_flags
);
1094 * Rough compatbility check to quickly see if it's even worth looking
1095 * at sharing an anon_vma.
1097 * They need to have the same vm_file, and the flags can only differ
1098 * in things that mprotect may change.
1100 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1101 * we can merge the two vma's. For example, we refuse to merge a vma if
1102 * there is a vm_ops->close() function, because that indicates that the
1103 * driver is doing some kind of reference counting. But that doesn't
1104 * really matter for the anon_vma sharing case.
1106 static int anon_vma_compatible(struct vm_area_struct
*a
, struct vm_area_struct
*b
)
1108 return a
->vm_end
== b
->vm_start
&&
1109 mpol_equal(vma_policy(a
), vma_policy(b
)) &&
1110 a
->vm_file
== b
->vm_file
&&
1111 !((a
->vm_flags
^ b
->vm_flags
) & ~(VM_READ
|VM_WRITE
|VM_EXEC
|VM_SOFTDIRTY
)) &&
1112 b
->vm_pgoff
== a
->vm_pgoff
+ ((b
->vm_start
- a
->vm_start
) >> PAGE_SHIFT
);
1116 * Do some basic sanity checking to see if we can re-use the anon_vma
1117 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1118 * the same as 'old', the other will be the new one that is trying
1119 * to share the anon_vma.
1121 * NOTE! This runs with mm_sem held for reading, so it is possible that
1122 * the anon_vma of 'old' is concurrently in the process of being set up
1123 * by another page fault trying to merge _that_. But that's ok: if it
1124 * is being set up, that automatically means that it will be a singleton
1125 * acceptable for merging, so we can do all of this optimistically. But
1126 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
1128 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1129 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1130 * is to return an anon_vma that is "complex" due to having gone through
1133 * We also make sure that the two vma's are compatible (adjacent,
1134 * and with the same memory policies). That's all stable, even with just
1135 * a read lock on the mm_sem.
1137 static struct anon_vma
*reusable_anon_vma(struct vm_area_struct
*old
, struct vm_area_struct
*a
, struct vm_area_struct
*b
)
1139 if (anon_vma_compatible(a
, b
)) {
1140 struct anon_vma
*anon_vma
= ACCESS_ONCE(old
->anon_vma
);
1142 if (anon_vma
&& list_is_singular(&old
->anon_vma_chain
))
1149 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1150 * neighbouring vmas for a suitable anon_vma, before it goes off
1151 * to allocate a new anon_vma. It checks because a repetitive
1152 * sequence of mprotects and faults may otherwise lead to distinct
1153 * anon_vmas being allocated, preventing vma merge in subsequent
1156 struct anon_vma
*find_mergeable_anon_vma(struct vm_area_struct
*vma
)
1158 struct anon_vma
*anon_vma
;
1159 struct vm_area_struct
*near
;
1161 near
= vma
->vm_next
;
1165 anon_vma
= reusable_anon_vma(near
, vma
, near
);
1169 near
= vma
->vm_prev
;
1173 anon_vma
= reusable_anon_vma(near
, near
, vma
);
1178 * There's no absolute need to look only at touching neighbours:
1179 * we could search further afield for "compatible" anon_vmas.
1180 * But it would probably just be a waste of time searching,
1181 * or lead to too many vmas hanging off the same anon_vma.
1182 * We're trying to allow mprotect remerging later on,
1183 * not trying to minimize memory used for anon_vmas.
1188 #ifdef CONFIG_PROC_FS
1189 void vm_stat_account(struct mm_struct
*mm
, unsigned long flags
,
1190 struct file
*file
, long pages
)
1192 const unsigned long stack_flags
1193 = VM_STACK_FLAGS
& (VM_GROWSUP
|VM_GROWSDOWN
);
1195 mm
->total_vm
+= pages
;
1198 mm
->shared_vm
+= pages
;
1199 if ((flags
& (VM_EXEC
|VM_WRITE
)) == VM_EXEC
)
1200 mm
->exec_vm
+= pages
;
1201 } else if (flags
& stack_flags
)
1202 mm
->stack_vm
+= pages
;
1204 #endif /* CONFIG_PROC_FS */
1207 * If a hint addr is less than mmap_min_addr change hint to be as
1208 * low as possible but still greater than mmap_min_addr
1210 static inline unsigned long round_hint_to_min(unsigned long hint
)
1213 if (((void *)hint
!= NULL
) &&
1214 (hint
< mmap_min_addr
))
1215 return PAGE_ALIGN(mmap_min_addr
);
1219 static inline int mlock_future_check(struct mm_struct
*mm
,
1220 unsigned long flags
,
1223 unsigned long locked
, lock_limit
;
1225 /* mlock MCL_FUTURE? */
1226 if (flags
& VM_LOCKED
) {
1227 locked
= len
>> PAGE_SHIFT
;
1228 locked
+= mm
->locked_vm
;
1229 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
1230 lock_limit
>>= PAGE_SHIFT
;
1231 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
1237 static inline u64
file_mmap_size_max(struct file
*file
, struct inode
*inode
)
1239 if (S_ISREG(inode
->i_mode
))
1240 return MAX_LFS_FILESIZE
;
1242 if (S_ISBLK(inode
->i_mode
))
1243 return MAX_LFS_FILESIZE
;
1245 /* Special "we do even unsigned file positions" case */
1246 if (file
->f_mode
& FMODE_UNSIGNED_OFFSET
)
1249 /* Yes, random drivers might want more. But I'm tired of buggy drivers */
1253 static inline bool file_mmap_ok(struct file
*file
, struct inode
*inode
,
1254 unsigned long pgoff
, unsigned long len
)
1256 u64 maxsize
= file_mmap_size_max(file
, inode
);
1258 if (maxsize
&& len
> maxsize
)
1261 if (pgoff
> maxsize
>> PAGE_SHIFT
)
1267 * The caller must hold down_write(¤t->mm->mmap_sem).
1270 unsigned long do_mmap_pgoff(struct file
*file
, unsigned long addr
,
1271 unsigned long len
, unsigned long prot
,
1272 unsigned long flags
, unsigned long pgoff
,
1273 unsigned long *populate
)
1275 struct mm_struct
* mm
= current
->mm
;
1276 vm_flags_t vm_flags
;
1281 * Does the application expect PROT_READ to imply PROT_EXEC?
1283 * (the exception is when the underlying filesystem is noexec
1284 * mounted, in which case we dont add PROT_EXEC.)
1286 if ((prot
& PROT_READ
) && (current
->personality
& READ_IMPLIES_EXEC
))
1287 if (!(file
&& path_noexec(&file
->f_path
)))
1293 if (!(flags
& MAP_FIXED
))
1294 addr
= round_hint_to_min(addr
);
1296 /* Careful about overflows.. */
1297 len
= PAGE_ALIGN(len
);
1301 /* offset overflow? */
1302 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
1305 /* Too many mappings? */
1306 if (mm
->map_count
> sysctl_max_map_count
)
1309 /* Obtain the address to map to. we verify (or select) it and ensure
1310 * that it represents a valid section of the address space.
1312 addr
= get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
1313 if (addr
& ~PAGE_MASK
)
1316 /* Do simple checking here so the lower-level routines won't have
1317 * to. we assume access permissions have been handled by the open
1318 * of the memory object, so we don't do any here.
1320 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
) |
1321 mm
->def_flags
| VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1323 if (flags
& MAP_LOCKED
)
1324 if (!can_do_mlock())
1327 if (mlock_future_check(mm
, vm_flags
, len
))
1331 struct inode
*inode
= file_inode(file
);
1333 if (!file_mmap_ok(file
, inode
, pgoff
, len
))
1336 switch (flags
& MAP_TYPE
) {
1338 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
1342 * Make sure we don't allow writing to an append-only
1345 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
1349 * Make sure there are no mandatory locks on the file.
1351 if (locks_verify_locked(file
))
1354 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
1355 if (!(file
->f_mode
& FMODE_WRITE
))
1356 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
1360 if (!(file
->f_mode
& FMODE_READ
))
1362 if (path_noexec(&file
->f_path
)) {
1363 if (vm_flags
& VM_EXEC
)
1365 vm_flags
&= ~VM_MAYEXEC
;
1368 if (!file
->f_op
->mmap
)
1370 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1378 switch (flags
& MAP_TYPE
) {
1380 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1386 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
1390 * Set pgoff according to addr for anon_vma.
1392 pgoff
= addr
>> PAGE_SHIFT
;
1400 * Set 'VM_NORESERVE' if we should not account for the
1401 * memory use of this mapping.
1403 if (flags
& MAP_NORESERVE
) {
1404 /* We honor MAP_NORESERVE if allowed to overcommit */
1405 if (sysctl_overcommit_memory
!= OVERCOMMIT_NEVER
)
1406 vm_flags
|= VM_NORESERVE
;
1408 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1409 if (file
&& is_file_hugepages(file
))
1410 vm_flags
|= VM_NORESERVE
;
1413 addr
= mmap_region(file
, addr
, len
, vm_flags
, pgoff
);
1414 if (!IS_ERR_VALUE(addr
) &&
1415 ((vm_flags
& VM_LOCKED
) ||
1416 (flags
& (MAP_POPULATE
| MAP_NONBLOCK
)) == MAP_POPULATE
))
1421 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1422 unsigned long, prot
, unsigned long, flags
,
1423 unsigned long, fd
, unsigned long, pgoff
)
1425 struct file
*file
= NULL
;
1426 unsigned long retval
= -EBADF
;
1428 if (!(flags
& MAP_ANONYMOUS
)) {
1429 audit_mmap_fd(fd
, flags
);
1433 if (is_file_hugepages(file
))
1434 len
= ALIGN(len
, huge_page_size(hstate_file(file
)));
1436 if (unlikely(flags
& MAP_HUGETLB
&& !is_file_hugepages(file
)))
1438 } else if (flags
& MAP_HUGETLB
) {
1439 struct user_struct
*user
= NULL
;
1442 hs
= hstate_sizelog((flags
>> MAP_HUGE_SHIFT
) & SHM_HUGE_MASK
);
1446 len
= ALIGN(len
, huge_page_size(hs
));
1448 * VM_NORESERVE is used because the reservations will be
1449 * taken when vm_ops->mmap() is called
1450 * A dummy user value is used because we are not locking
1451 * memory so no accounting is necessary
1453 file
= hugetlb_file_setup(HUGETLB_ANON_FILE
, len
,
1455 &user
, HUGETLB_ANONHUGE_INODE
,
1456 (flags
>> MAP_HUGE_SHIFT
) & MAP_HUGE_MASK
);
1458 return PTR_ERR(file
);
1461 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1463 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1471 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1472 struct mmap_arg_struct
{
1476 unsigned long flags
;
1478 unsigned long offset
;
1481 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1483 struct mmap_arg_struct a
;
1485 if (copy_from_user(&a
, arg
, sizeof(a
)))
1487 if (a
.offset
& ~PAGE_MASK
)
1490 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1491 a
.offset
>> PAGE_SHIFT
);
1493 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1496 * Some shared mappigns will want the pages marked read-only
1497 * to track write events. If so, we'll downgrade vm_page_prot
1498 * to the private version (using protection_map[] without the
1501 int vma_wants_writenotify(struct vm_area_struct
*vma
)
1503 vm_flags_t vm_flags
= vma
->vm_flags
;
1505 /* If it was private or non-writable, the write bit is already clear */
1506 if ((vm_flags
& (VM_WRITE
|VM_SHARED
)) != ((VM_WRITE
|VM_SHARED
)))
1509 /* The backer wishes to know when pages are first written to? */
1510 if (vma
->vm_ops
&& vma
->vm_ops
->page_mkwrite
)
1513 /* The open routine did something to the protections already? */
1514 if (pgprot_val(vma
->vm_page_prot
) !=
1515 pgprot_val(vm_get_page_prot(vm_flags
)))
1518 /* Specialty mapping? */
1519 if (vm_flags
& VM_PFNMAP
)
1522 /* Can the mapping track the dirty pages? */
1523 return vma
->vm_file
&& vma
->vm_file
->f_mapping
&&
1524 mapping_cap_account_dirty(vma
->vm_file
->f_mapping
);
1528 * We account for memory if it's a private writeable mapping,
1529 * not hugepages and VM_NORESERVE wasn't set.
1531 static inline int accountable_mapping(struct file
*file
, vm_flags_t vm_flags
)
1534 * hugetlb has its own accounting separate from the core VM
1535 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1537 if (file
&& is_file_hugepages(file
))
1540 return (vm_flags
& (VM_NORESERVE
| VM_SHARED
| VM_WRITE
)) == VM_WRITE
;
1543 unsigned long mmap_region(struct file
*file
, unsigned long addr
,
1544 unsigned long len
, vm_flags_t vm_flags
, unsigned long pgoff
)
1546 struct mm_struct
*mm
= current
->mm
;
1547 struct vm_area_struct
*vma
, *prev
;
1549 struct rb_node
**rb_link
, *rb_parent
;
1550 unsigned long charged
= 0;
1552 /* Check against address space limit. */
1553 if (!may_expand_vm(mm
, len
>> PAGE_SHIFT
)) {
1554 unsigned long nr_pages
;
1557 * MAP_FIXED may remove pages of mappings that intersects with
1558 * requested mapping. Account for the pages it would unmap.
1560 if (!(vm_flags
& MAP_FIXED
))
1563 nr_pages
= count_vma_pages_range(mm
, addr
, addr
+ len
);
1565 if (!may_expand_vm(mm
, (len
>> PAGE_SHIFT
) - nr_pages
))
1569 /* Clear old maps */
1572 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
)) {
1573 if (do_munmap(mm
, addr
, len
))
1579 * Private writable mapping: check memory availability
1581 if (accountable_mapping(file
, vm_flags
)) {
1582 charged
= len
>> PAGE_SHIFT
;
1583 if (security_vm_enough_memory_mm(mm
, charged
))
1585 vm_flags
|= VM_ACCOUNT
;
1589 * Can we just expand an old mapping?
1591 vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vm_flags
, NULL
, file
, pgoff
, NULL
);
1596 * Determine the object being mapped and call the appropriate
1597 * specific mapper. the address has already been validated, but
1598 * not unmapped, but the maps are removed from the list.
1600 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1607 vma
->vm_start
= addr
;
1608 vma
->vm_end
= addr
+ len
;
1609 vma
->vm_flags
= vm_flags
;
1610 vma
->vm_page_prot
= vm_get_page_prot(vm_flags
);
1611 vma
->vm_pgoff
= pgoff
;
1612 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1615 if (vm_flags
& VM_DENYWRITE
) {
1616 error
= deny_write_access(file
);
1620 vma
->vm_file
= get_file(file
);
1621 error
= file
->f_op
->mmap(file
, vma
);
1623 goto unmap_and_free_vma
;
1625 /* Can addr have changed??
1627 * Answer: Yes, several device drivers can do it in their
1628 * f_op->mmap method. -DaveM
1629 * Bug: If addr is changed, prev, rb_link, rb_parent should
1630 * be updated for vma_link()
1632 WARN_ON_ONCE(addr
!= vma
->vm_start
);
1634 addr
= vma
->vm_start
;
1635 vm_flags
= vma
->vm_flags
;
1636 } else if (vm_flags
& VM_SHARED
) {
1637 error
= shmem_zero_setup(vma
);
1642 if (vma_wants_writenotify(vma
)) {
1643 pgprot_t pprot
= vma
->vm_page_prot
;
1645 /* Can vma->vm_page_prot have changed??
1647 * Answer: Yes, drivers may have changed it in their
1648 * f_op->mmap method.
1650 * Ensures that vmas marked as uncached stay that way.
1652 vma
->vm_page_prot
= vm_get_page_prot(vm_flags
& ~VM_SHARED
);
1653 if (pgprot_val(pprot
) == pgprot_val(pgprot_noncached(pprot
)))
1654 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1657 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1658 /* Once vma denies write, undo our temporary denial count */
1659 if (vm_flags
& VM_DENYWRITE
)
1660 allow_write_access(file
);
1661 file
= vma
->vm_file
;
1663 perf_event_mmap(vma
);
1665 vm_stat_account(mm
, vm_flags
, file
, len
>> PAGE_SHIFT
);
1666 if (vm_flags
& VM_LOCKED
) {
1667 if (!((vm_flags
& VM_SPECIAL
) || is_vm_hugetlb_page(vma
) ||
1668 vma
== get_gate_vma(current
->mm
)))
1669 mm
->locked_vm
+= (len
>> PAGE_SHIFT
);
1671 vma
->vm_flags
&= ~VM_LOCKED
;
1678 * New (or expanded) vma always get soft dirty status.
1679 * Otherwise user-space soft-dirty page tracker won't
1680 * be able to distinguish situation when vma area unmapped,
1681 * then new mapped in-place (which must be aimed as
1682 * a completely new data area).
1684 vma
->vm_flags
|= VM_SOFTDIRTY
;
1689 if (vm_flags
& VM_DENYWRITE
)
1690 allow_write_access(file
);
1691 vma
->vm_file
= NULL
;
1694 /* Undo any partial mapping done by a device driver. */
1695 unmap_region(mm
, vma
, prev
, vma
->vm_start
, vma
->vm_end
);
1698 kmem_cache_free(vm_area_cachep
, vma
);
1701 vm_unacct_memory(charged
);
1705 unsigned long unmapped_area(struct vm_unmapped_area_info
*info
)
1708 * We implement the search by looking for an rbtree node that
1709 * immediately follows a suitable gap. That is,
1710 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1711 * - gap_end = vma->vm_start >= info->low_limit + length;
1712 * - gap_end - gap_start >= length
1715 struct mm_struct
*mm
= current
->mm
;
1716 struct vm_area_struct
*vma
;
1717 unsigned long length
, low_limit
, high_limit
, gap_start
, gap_end
;
1719 /* Adjust search length to account for worst case alignment overhead */
1720 length
= info
->length
+ info
->align_mask
;
1721 if (length
< info
->length
)
1724 /* Adjust search limits by the desired length */
1725 if (info
->high_limit
< length
)
1727 high_limit
= info
->high_limit
- length
;
1729 if (info
->low_limit
> high_limit
)
1731 low_limit
= info
->low_limit
+ length
;
1733 /* Check if rbtree root looks promising */
1734 if (RB_EMPTY_ROOT(&mm
->mm_rb
))
1736 vma
= rb_entry(mm
->mm_rb
.rb_node
, struct vm_area_struct
, vm_rb
);
1737 if (vma
->rb_subtree_gap
< length
)
1741 /* Visit left subtree if it looks promising */
1742 gap_end
= vm_start_gap(vma
);
1743 if (gap_end
>= low_limit
&& vma
->vm_rb
.rb_left
) {
1744 struct vm_area_struct
*left
=
1745 rb_entry(vma
->vm_rb
.rb_left
,
1746 struct vm_area_struct
, vm_rb
);
1747 if (left
->rb_subtree_gap
>= length
) {
1753 gap_start
= vma
->vm_prev
? vm_end_gap(vma
->vm_prev
) : 0;
1755 /* Check if current node has a suitable gap */
1756 if (gap_start
> high_limit
)
1758 if (gap_end
>= low_limit
&&
1759 gap_end
> gap_start
&& gap_end
- gap_start
>= length
)
1762 /* Visit right subtree if it looks promising */
1763 if (vma
->vm_rb
.rb_right
) {
1764 struct vm_area_struct
*right
=
1765 rb_entry(vma
->vm_rb
.rb_right
,
1766 struct vm_area_struct
, vm_rb
);
1767 if (right
->rb_subtree_gap
>= length
) {
1773 /* Go back up the rbtree to find next candidate node */
1775 struct rb_node
*prev
= &vma
->vm_rb
;
1776 if (!rb_parent(prev
))
1778 vma
= rb_entry(rb_parent(prev
),
1779 struct vm_area_struct
, vm_rb
);
1780 if (prev
== vma
->vm_rb
.rb_left
) {
1781 gap_start
= vm_end_gap(vma
->vm_prev
);
1782 gap_end
= vm_start_gap(vma
);
1789 /* Check highest gap, which does not precede any rbtree node */
1790 gap_start
= mm
->highest_vm_end
;
1791 gap_end
= ULONG_MAX
; /* Only for VM_BUG_ON below */
1792 if (gap_start
> high_limit
)
1796 /* We found a suitable gap. Clip it with the original low_limit. */
1797 if (gap_start
< info
->low_limit
)
1798 gap_start
= info
->low_limit
;
1800 /* Adjust gap address to the desired alignment */
1801 gap_start
+= (info
->align_offset
- gap_start
) & info
->align_mask
;
1803 VM_BUG_ON(gap_start
+ info
->length
> info
->high_limit
);
1804 VM_BUG_ON(gap_start
+ info
->length
> gap_end
);
1808 unsigned long unmapped_area_topdown(struct vm_unmapped_area_info
*info
)
1810 struct mm_struct
*mm
= current
->mm
;
1811 struct vm_area_struct
*vma
;
1812 unsigned long length
, low_limit
, high_limit
, gap_start
, gap_end
;
1814 /* Adjust search length to account for worst case alignment overhead */
1815 length
= info
->length
+ info
->align_mask
;
1816 if (length
< info
->length
)
1820 * Adjust search limits by the desired length.
1821 * See implementation comment at top of unmapped_area().
1823 gap_end
= info
->high_limit
;
1824 if (gap_end
< length
)
1826 high_limit
= gap_end
- length
;
1828 if (info
->low_limit
> high_limit
)
1830 low_limit
= info
->low_limit
+ length
;
1832 /* Check highest gap, which does not precede any rbtree node */
1833 gap_start
= mm
->highest_vm_end
;
1834 if (gap_start
<= high_limit
)
1837 /* Check if rbtree root looks promising */
1838 if (RB_EMPTY_ROOT(&mm
->mm_rb
))
1840 vma
= rb_entry(mm
->mm_rb
.rb_node
, struct vm_area_struct
, vm_rb
);
1841 if (vma
->rb_subtree_gap
< length
)
1845 /* Visit right subtree if it looks promising */
1846 gap_start
= vma
->vm_prev
? vm_end_gap(vma
->vm_prev
) : 0;
1847 if (gap_start
<= high_limit
&& vma
->vm_rb
.rb_right
) {
1848 struct vm_area_struct
*right
=
1849 rb_entry(vma
->vm_rb
.rb_right
,
1850 struct vm_area_struct
, vm_rb
);
1851 if (right
->rb_subtree_gap
>= length
) {
1858 /* Check if current node has a suitable gap */
1859 gap_end
= vm_start_gap(vma
);
1860 if (gap_end
< low_limit
)
1862 if (gap_start
<= high_limit
&&
1863 gap_end
> gap_start
&& gap_end
- gap_start
>= length
)
1866 /* Visit left subtree if it looks promising */
1867 if (vma
->vm_rb
.rb_left
) {
1868 struct vm_area_struct
*left
=
1869 rb_entry(vma
->vm_rb
.rb_left
,
1870 struct vm_area_struct
, vm_rb
);
1871 if (left
->rb_subtree_gap
>= length
) {
1877 /* Go back up the rbtree to find next candidate node */
1879 struct rb_node
*prev
= &vma
->vm_rb
;
1880 if (!rb_parent(prev
))
1882 vma
= rb_entry(rb_parent(prev
),
1883 struct vm_area_struct
, vm_rb
);
1884 if (prev
== vma
->vm_rb
.rb_right
) {
1885 gap_start
= vma
->vm_prev
?
1886 vm_end_gap(vma
->vm_prev
) : 0;
1893 /* We found a suitable gap. Clip it with the original high_limit. */
1894 if (gap_end
> info
->high_limit
)
1895 gap_end
= info
->high_limit
;
1898 /* Compute highest gap address at the desired alignment */
1899 gap_end
-= info
->length
;
1900 gap_end
-= (gap_end
- info
->align_offset
) & info
->align_mask
;
1902 VM_BUG_ON(gap_end
< info
->low_limit
);
1903 VM_BUG_ON(gap_end
< gap_start
);
1907 /* Get an address range which is currently unmapped.
1908 * For shmat() with addr=0.
1910 * Ugly calling convention alert:
1911 * Return value with the low bits set means error value,
1913 * if (ret & ~PAGE_MASK)
1916 * This function "knows" that -ENOMEM has the bits set.
1918 #ifndef HAVE_ARCH_UNMAPPED_AREA
1920 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
1921 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1923 struct mm_struct
*mm
= current
->mm
;
1924 struct vm_area_struct
*vma
, *prev
;
1925 struct vm_unmapped_area_info info
;
1927 if (len
> TASK_SIZE
- mmap_min_addr
)
1930 if (flags
& MAP_FIXED
)
1934 addr
= PAGE_ALIGN(addr
);
1935 vma
= find_vma_prev(mm
, addr
, &prev
);
1936 if (TASK_SIZE
- len
>= addr
&& addr
>= mmap_min_addr
&&
1937 (!vma
|| addr
+ len
<= vm_start_gap(vma
)) &&
1938 (!prev
|| addr
>= vm_end_gap(prev
)))
1944 info
.low_limit
= mm
->mmap_base
;
1945 info
.high_limit
= TASK_SIZE
;
1946 info
.align_mask
= 0;
1947 return vm_unmapped_area(&info
);
1952 * This mmap-allocator allocates new areas top-down from below the
1953 * stack's low limit (the base):
1955 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1957 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
1958 const unsigned long len
, const unsigned long pgoff
,
1959 const unsigned long flags
)
1961 struct vm_area_struct
*vma
, *prev
;
1962 struct mm_struct
*mm
= current
->mm
;
1963 unsigned long addr
= addr0
;
1964 struct vm_unmapped_area_info info
;
1966 /* requested length too big for entire address space */
1967 if (len
> TASK_SIZE
- mmap_min_addr
)
1970 if (flags
& MAP_FIXED
)
1973 /* requesting a specific address */
1975 addr
= PAGE_ALIGN(addr
);
1976 vma
= find_vma_prev(mm
, addr
, &prev
);
1977 if (TASK_SIZE
- len
>= addr
&& addr
>= mmap_min_addr
&&
1978 (!vma
|| addr
+ len
<= vm_start_gap(vma
)) &&
1979 (!prev
|| addr
>= vm_end_gap(prev
)))
1983 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
1985 info
.low_limit
= max(PAGE_SIZE
, mmap_min_addr
);
1986 info
.high_limit
= mm
->mmap_base
;
1987 info
.align_mask
= 0;
1988 addr
= vm_unmapped_area(&info
);
1991 * A failed mmap() very likely causes application failure,
1992 * so fall back to the bottom-up function here. This scenario
1993 * can happen with large stack limits and large mmap()
1996 if (addr
& ~PAGE_MASK
) {
1997 VM_BUG_ON(addr
!= -ENOMEM
);
1999 info
.low_limit
= TASK_UNMAPPED_BASE
;
2000 info
.high_limit
= TASK_SIZE
;
2001 addr
= vm_unmapped_area(&info
);
2009 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
2010 unsigned long pgoff
, unsigned long flags
)
2012 unsigned long (*get_area
)(struct file
*, unsigned long,
2013 unsigned long, unsigned long, unsigned long);
2015 unsigned long error
= arch_mmap_check(addr
, len
, flags
);
2019 /* Careful about overflows.. */
2020 if (len
> TASK_SIZE
)
2023 get_area
= current
->mm
->get_unmapped_area
;
2024 if (file
&& file
->f_op
->get_unmapped_area
)
2025 get_area
= file
->f_op
->get_unmapped_area
;
2026 addr
= get_area(file
, addr
, len
, pgoff
, flags
);
2027 if (IS_ERR_VALUE(addr
))
2030 if (addr
> TASK_SIZE
- len
)
2032 if (addr
& ~PAGE_MASK
)
2035 addr
= arch_rebalance_pgtables(addr
, len
);
2036 error
= security_mmap_addr(addr
);
2037 return error
? error
: addr
;
2040 EXPORT_SYMBOL(get_unmapped_area
);
2042 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
2043 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
2045 struct rb_node
*rb_node
;
2046 struct vm_area_struct
*vma
;
2048 /* Check the cache first. */
2049 vma
= vmacache_find(mm
, addr
);
2053 rb_node
= mm
->mm_rb
.rb_node
;
2057 struct vm_area_struct
*tmp
;
2059 tmp
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
2061 if (tmp
->vm_end
> addr
) {
2063 if (tmp
->vm_start
<= addr
)
2065 rb_node
= rb_node
->rb_left
;
2067 rb_node
= rb_node
->rb_right
;
2071 vmacache_update(addr
, vma
);
2075 EXPORT_SYMBOL(find_vma
);
2078 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
2080 struct vm_area_struct
*
2081 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
2082 struct vm_area_struct
**pprev
)
2084 struct vm_area_struct
*vma
;
2086 vma
= find_vma(mm
, addr
);
2088 *pprev
= vma
->vm_prev
;
2090 struct rb_node
*rb_node
= mm
->mm_rb
.rb_node
;
2093 *pprev
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
2094 rb_node
= rb_node
->rb_right
;
2101 * Verify that the stack growth is acceptable and
2102 * update accounting. This is shared with both the
2103 * grow-up and grow-down cases.
2105 static int acct_stack_growth(struct vm_area_struct
*vma
,
2106 unsigned long size
, unsigned long grow
)
2108 struct mm_struct
*mm
= vma
->vm_mm
;
2109 struct rlimit
*rlim
= current
->signal
->rlim
;
2110 unsigned long new_start
;
2112 /* address space limit tests */
2113 if (!may_expand_vm(mm
, grow
))
2116 /* Stack limit test */
2117 if (size
> ACCESS_ONCE(rlim
[RLIMIT_STACK
].rlim_cur
))
2120 /* mlock limit tests */
2121 if (vma
->vm_flags
& VM_LOCKED
) {
2122 unsigned long locked
;
2123 unsigned long limit
;
2124 locked
= mm
->locked_vm
+ grow
;
2125 limit
= ACCESS_ONCE(rlim
[RLIMIT_MEMLOCK
].rlim_cur
);
2126 limit
>>= PAGE_SHIFT
;
2127 if (locked
> limit
&& !capable(CAP_IPC_LOCK
))
2131 /* Check to ensure the stack will not grow into a hugetlb-only region */
2132 new_start
= (vma
->vm_flags
& VM_GROWSUP
) ? vma
->vm_start
:
2134 if (is_hugepage_only_range(vma
->vm_mm
, new_start
, size
))
2138 * Overcommit.. This must be the final test, as it will
2139 * update security statistics.
2141 if (security_vm_enough_memory_mm(mm
, grow
))
2144 /* Ok, everything looks good - let it rip */
2145 if (vma
->vm_flags
& VM_LOCKED
)
2146 mm
->locked_vm
+= grow
;
2147 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, grow
);
2151 #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
2153 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2154 * vma is the last one with address > vma->vm_end. Have to extend vma.
2156 int expand_upwards(struct vm_area_struct
*vma
, unsigned long address
)
2158 struct vm_area_struct
*next
;
2159 unsigned long gap_addr
;
2162 if (!(vma
->vm_flags
& VM_GROWSUP
))
2165 /* Guard against exceeding limits of the address space. */
2166 address
&= PAGE_MASK
;
2167 if (address
>= (TASK_SIZE
& PAGE_MASK
))
2169 address
+= PAGE_SIZE
;
2171 /* Enforce stack_guard_gap */
2172 gap_addr
= address
+ stack_guard_gap
;
2174 /* Guard against overflow */
2175 if (gap_addr
< address
|| gap_addr
> TASK_SIZE
)
2176 gap_addr
= TASK_SIZE
;
2178 next
= vma
->vm_next
;
2179 if (next
&& next
->vm_start
< gap_addr
&&
2180 (next
->vm_flags
& (VM_WRITE
|VM_READ
|VM_EXEC
))) {
2181 if (!(next
->vm_flags
& VM_GROWSUP
))
2183 /* Check that both stack segments have the same anon_vma? */
2186 /* We must make sure the anon_vma is allocated. */
2187 if (unlikely(anon_vma_prepare(vma
)))
2191 * vma->vm_start/vm_end cannot change under us because the caller
2192 * is required to hold the mmap_sem in read mode. We need the
2193 * anon_vma lock to serialize against concurrent expand_stacks.
2195 anon_vma_lock_write(vma
->anon_vma
);
2197 /* Somebody else might have raced and expanded it already */
2198 if (address
> vma
->vm_end
) {
2199 unsigned long size
, grow
;
2201 size
= address
- vma
->vm_start
;
2202 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
2205 if (vma
->vm_pgoff
+ (size
>> PAGE_SHIFT
) >= vma
->vm_pgoff
) {
2206 error
= acct_stack_growth(vma
, size
, grow
);
2209 * vma_gap_update() doesn't support concurrent
2210 * updates, but we only hold a shared mmap_sem
2211 * lock here, so we need to protect against
2212 * concurrent vma expansions.
2213 * anon_vma_lock_write() doesn't help here, as
2214 * we don't guarantee that all growable vmas
2215 * in a mm share the same root anon vma.
2216 * So, we reuse mm->page_table_lock to guard
2217 * against concurrent vma expansions.
2219 spin_lock(&vma
->vm_mm
->page_table_lock
);
2220 anon_vma_interval_tree_pre_update_vma(vma
);
2221 vma
->vm_end
= address
;
2222 anon_vma_interval_tree_post_update_vma(vma
);
2224 vma_gap_update(vma
->vm_next
);
2226 vma
->vm_mm
->highest_vm_end
= vm_end_gap(vma
);
2227 spin_unlock(&vma
->vm_mm
->page_table_lock
);
2229 perf_event_mmap(vma
);
2233 anon_vma_unlock_write(vma
->anon_vma
);
2234 khugepaged_enter_vma_merge(vma
, vma
->vm_flags
);
2235 validate_mm(vma
->vm_mm
);
2238 #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2241 * vma is the first one with address < vma->vm_start. Have to extend vma.
2243 int expand_downwards(struct vm_area_struct
*vma
,
2244 unsigned long address
)
2246 struct vm_area_struct
*prev
;
2247 unsigned long gap_addr
;
2250 address
&= PAGE_MASK
;
2251 error
= security_mmap_addr(address
);
2255 /* Enforce stack_guard_gap */
2256 gap_addr
= address
- stack_guard_gap
;
2257 if (gap_addr
> address
)
2259 prev
= vma
->vm_prev
;
2260 if (prev
&& prev
->vm_end
> gap_addr
&&
2261 (prev
->vm_flags
& (VM_WRITE
|VM_READ
|VM_EXEC
))) {
2262 if (!(prev
->vm_flags
& VM_GROWSDOWN
))
2264 /* Check that both stack segments have the same anon_vma? */
2267 /* We must make sure the anon_vma is allocated. */
2268 if (unlikely(anon_vma_prepare(vma
)))
2272 * vma->vm_start/vm_end cannot change under us because the caller
2273 * is required to hold the mmap_sem in read mode. We need the
2274 * anon_vma lock to serialize against concurrent expand_stacks.
2276 anon_vma_lock_write(vma
->anon_vma
);
2278 /* Somebody else might have raced and expanded it already */
2279 if (address
< vma
->vm_start
) {
2280 unsigned long size
, grow
;
2282 size
= vma
->vm_end
- address
;
2283 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
2286 if (grow
<= vma
->vm_pgoff
) {
2287 error
= acct_stack_growth(vma
, size
, grow
);
2290 * vma_gap_update() doesn't support concurrent
2291 * updates, but we only hold a shared mmap_sem
2292 * lock here, so we need to protect against
2293 * concurrent vma expansions.
2294 * anon_vma_lock_write() doesn't help here, as
2295 * we don't guarantee that all growable vmas
2296 * in a mm share the same root anon vma.
2297 * So, we reuse mm->page_table_lock to guard
2298 * against concurrent vma expansions.
2300 spin_lock(&vma
->vm_mm
->page_table_lock
);
2301 anon_vma_interval_tree_pre_update_vma(vma
);
2302 vma
->vm_start
= address
;
2303 vma
->vm_pgoff
-= grow
;
2304 anon_vma_interval_tree_post_update_vma(vma
);
2305 vma_gap_update(vma
);
2306 spin_unlock(&vma
->vm_mm
->page_table_lock
);
2308 perf_event_mmap(vma
);
2312 anon_vma_unlock_write(vma
->anon_vma
);
2313 khugepaged_enter_vma_merge(vma
, vma
->vm_flags
);
2314 validate_mm(vma
->vm_mm
);
2318 /* enforced gap between the expanding stack and other mappings. */
2319 unsigned long stack_guard_gap
= 256UL<<PAGE_SHIFT
;
2321 static int __init
cmdline_parse_stack_guard_gap(char *p
)
2326 val
= simple_strtoul(p
, &endptr
, 10);
2328 stack_guard_gap
= val
<< PAGE_SHIFT
;
2332 __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap
);
2334 #ifdef CONFIG_STACK_GROWSUP
2335 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
2337 return expand_upwards(vma
, address
);
2340 struct vm_area_struct
*
2341 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
2343 struct vm_area_struct
*vma
, *prev
;
2346 vma
= find_vma_prev(mm
, addr
, &prev
);
2347 if (vma
&& (vma
->vm_start
<= addr
))
2349 if (!prev
|| expand_stack(prev
, addr
))
2351 if (prev
->vm_flags
& VM_LOCKED
)
2352 __mlock_vma_pages_range(prev
, addr
, prev
->vm_end
, NULL
);
2356 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
2358 return expand_downwards(vma
, address
);
2361 struct vm_area_struct
*
2362 find_extend_vma(struct mm_struct
* mm
, unsigned long addr
)
2364 struct vm_area_struct
* vma
;
2365 unsigned long start
;
2368 vma
= find_vma(mm
,addr
);
2371 if (vma
->vm_start
<= addr
)
2373 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
2375 start
= vma
->vm_start
;
2376 if (expand_stack(vma
, addr
))
2378 if (vma
->vm_flags
& VM_LOCKED
)
2379 __mlock_vma_pages_range(vma
, addr
, start
, NULL
);
2385 * Ok - we have the memory areas we should free on the vma list,
2386 * so release them, and do the vma updates.
2388 * Called with the mm semaphore held.
2390 static void remove_vma_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
2392 unsigned long nr_accounted
= 0;
2394 /* Update high watermark before we lower total_vm */
2395 update_hiwater_vm(mm
);
2397 long nrpages
= vma_pages(vma
);
2399 if (vma
->vm_flags
& VM_ACCOUNT
)
2400 nr_accounted
+= nrpages
;
2401 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, -nrpages
);
2402 vma
= remove_vma(vma
);
2404 vm_unacct_memory(nr_accounted
);
2409 * Get rid of page table information in the indicated region.
2411 * Called with the mm semaphore held.
2413 static void unmap_region(struct mm_struct
*mm
,
2414 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
2415 unsigned long start
, unsigned long end
)
2417 struct vm_area_struct
*next
= prev
? prev
->vm_next
: mm
->mmap
;
2418 struct mmu_gather tlb
;
2421 tlb_gather_mmu(&tlb
, mm
, start
, end
);
2422 update_hiwater_rss(mm
);
2423 unmap_vmas(&tlb
, vma
, start
, end
);
2424 free_pgtables(&tlb
, vma
, prev
? prev
->vm_end
: FIRST_USER_ADDRESS
,
2425 next
? next
->vm_start
: USER_PGTABLES_CEILING
);
2426 tlb_finish_mmu(&tlb
, start
, end
);
2430 * Create a list of vma's touched by the unmap, removing them from the mm's
2431 * vma list as we go..
2434 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2435 struct vm_area_struct
*prev
, unsigned long end
)
2437 struct vm_area_struct
**insertion_point
;
2438 struct vm_area_struct
*tail_vma
= NULL
;
2440 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
2441 vma
->vm_prev
= NULL
;
2443 vma_rb_erase(vma
, &mm
->mm_rb
);
2447 } while (vma
&& vma
->vm_start
< end
);
2448 *insertion_point
= vma
;
2450 vma
->vm_prev
= prev
;
2451 vma_gap_update(vma
);
2453 mm
->highest_vm_end
= prev
? vm_end_gap(prev
) : 0;
2454 tail_vma
->vm_next
= NULL
;
2456 /* Kill the cache */
2457 vmacache_invalidate(mm
);
2461 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2462 * munmap path where it doesn't make sense to fail.
2464 static int __split_vma(struct mm_struct
* mm
, struct vm_area_struct
* vma
,
2465 unsigned long addr
, int new_below
)
2467 struct vm_area_struct
*new;
2470 if (is_vm_hugetlb_page(vma
) && (addr
&
2471 ~(huge_page_mask(hstate_vma(vma
)))))
2474 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
2478 /* most fields are the same, copy all, and then fixup */
2481 INIT_LIST_HEAD(&new->anon_vma_chain
);
2486 new->vm_start
= addr
;
2487 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
2490 err
= vma_dup_policy(vma
, new);
2494 err
= anon_vma_clone(new, vma
);
2499 get_file(new->vm_file
);
2501 if (new->vm_ops
&& new->vm_ops
->open
)
2502 new->vm_ops
->open(new);
2505 err
= vma_adjust(vma
, addr
, vma
->vm_end
, vma
->vm_pgoff
+
2506 ((addr
- new->vm_start
) >> PAGE_SHIFT
), new);
2508 err
= vma_adjust(vma
, vma
->vm_start
, addr
, vma
->vm_pgoff
, new);
2514 /* Clean everything up if vma_adjust failed. */
2515 if (new->vm_ops
&& new->vm_ops
->close
)
2516 new->vm_ops
->close(new);
2519 unlink_anon_vmas(new);
2521 mpol_put(vma_policy(new));
2523 kmem_cache_free(vm_area_cachep
, new);
2529 * Split a vma into two pieces at address 'addr', a new vma is allocated
2530 * either for the first part or the tail.
2532 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2533 unsigned long addr
, int new_below
)
2535 if (mm
->map_count
>= sysctl_max_map_count
)
2538 return __split_vma(mm
, vma
, addr
, new_below
);
2541 /* Munmap is split into 2 main parts -- this part which finds
2542 * what needs doing, and the areas themselves, which do the
2543 * work. This now handles partial unmappings.
2544 * Jeremy Fitzhardinge <jeremy@goop.org>
2546 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
2549 struct vm_area_struct
*vma
, *prev
, *last
;
2551 if ((start
& ~PAGE_MASK
) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
2554 if ((len
= PAGE_ALIGN(len
)) == 0)
2557 /* Find the first overlapping VMA */
2558 vma
= find_vma(mm
, start
);
2561 prev
= vma
->vm_prev
;
2562 /* we have start < vma->vm_end */
2564 /* if it doesn't overlap, we have nothing.. */
2566 if (vma
->vm_start
>= end
)
2570 * If we need to split any vma, do it now to save pain later.
2572 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2573 * unmapped vm_area_struct will remain in use: so lower split_vma
2574 * places tmp vma above, and higher split_vma places tmp vma below.
2576 if (start
> vma
->vm_start
) {
2580 * Make sure that map_count on return from munmap() will
2581 * not exceed its limit; but let map_count go just above
2582 * its limit temporarily, to help free resources as expected.
2584 if (end
< vma
->vm_end
&& mm
->map_count
>= sysctl_max_map_count
)
2587 error
= __split_vma(mm
, vma
, start
, 0);
2593 /* Does it split the last one? */
2594 last
= find_vma(mm
, end
);
2595 if (last
&& end
> last
->vm_start
) {
2596 int error
= __split_vma(mm
, last
, end
, 1);
2600 vma
= prev
? prev
->vm_next
: mm
->mmap
;
2603 * unlock any mlock()ed ranges before detaching vmas
2605 if (mm
->locked_vm
) {
2606 struct vm_area_struct
*tmp
= vma
;
2607 while (tmp
&& tmp
->vm_start
< end
) {
2608 if (tmp
->vm_flags
& VM_LOCKED
) {
2609 mm
->locked_vm
-= vma_pages(tmp
);
2610 munlock_vma_pages_all(tmp
);
2617 * Remove the vma's, and unmap the actual pages
2619 detach_vmas_to_be_unmapped(mm
, vma
, prev
, end
);
2620 unmap_region(mm
, vma
, prev
, start
, end
);
2622 /* Fix up all other VM information */
2623 remove_vma_list(mm
, vma
);
2628 int vm_munmap(unsigned long start
, size_t len
)
2631 struct mm_struct
*mm
= current
->mm
;
2633 down_write(&mm
->mmap_sem
);
2634 ret
= do_munmap(mm
, start
, len
);
2635 up_write(&mm
->mmap_sem
);
2638 EXPORT_SYMBOL(vm_munmap
);
2640 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
2642 profile_munmap(addr
);
2643 return vm_munmap(addr
, len
);
2648 * Emulation of deprecated remap_file_pages() syscall.
2650 SYSCALL_DEFINE5(remap_file_pages
, unsigned long, start
, unsigned long, size
,
2651 unsigned long, prot
, unsigned long, pgoff
, unsigned long, flags
)
2654 struct mm_struct
*mm
= current
->mm
;
2655 struct vm_area_struct
*vma
;
2656 unsigned long populate
= 0;
2657 unsigned long ret
= -EINVAL
;
2660 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
2661 "See Documentation/vm/remap_file_pages.txt.\n",
2662 current
->comm
, current
->pid
);
2666 start
= start
& PAGE_MASK
;
2667 size
= size
& PAGE_MASK
;
2669 if (start
+ size
<= start
)
2672 /* Does pgoff wrap? */
2673 if (pgoff
+ (size
>> PAGE_SHIFT
) < pgoff
)
2676 down_write(&mm
->mmap_sem
);
2677 vma
= find_vma(mm
, start
);
2679 if (!vma
|| !(vma
->vm_flags
& VM_SHARED
))
2682 if (start
< vma
->vm_start
)
2685 if (start
+ size
> vma
->vm_end
) {
2686 struct vm_area_struct
*next
;
2688 for (next
= vma
->vm_next
; next
; next
= next
->vm_next
) {
2689 /* hole between vmas ? */
2690 if (next
->vm_start
!= next
->vm_prev
->vm_end
)
2693 if (next
->vm_file
!= vma
->vm_file
)
2696 if (next
->vm_flags
!= vma
->vm_flags
)
2699 if (start
+ size
<= next
->vm_end
)
2707 prot
|= vma
->vm_flags
& VM_READ
? PROT_READ
: 0;
2708 prot
|= vma
->vm_flags
& VM_WRITE
? PROT_WRITE
: 0;
2709 prot
|= vma
->vm_flags
& VM_EXEC
? PROT_EXEC
: 0;
2711 flags
&= MAP_NONBLOCK
;
2712 flags
|= MAP_SHARED
| MAP_FIXED
| MAP_POPULATE
;
2713 if (vma
->vm_flags
& VM_LOCKED
) {
2714 struct vm_area_struct
*tmp
;
2715 flags
|= MAP_LOCKED
;
2717 /* drop PG_Mlocked flag for over-mapped range */
2718 for (tmp
= vma
; tmp
->vm_start
>= start
+ size
;
2719 tmp
= tmp
->vm_next
) {
2720 munlock_vma_pages_range(tmp
,
2721 max(tmp
->vm_start
, start
),
2722 min(tmp
->vm_end
, start
+ size
));
2726 file
= get_file(vma
->vm_file
);
2727 ret
= do_mmap_pgoff(vma
->vm_file
, start
, size
,
2728 prot
, flags
, pgoff
, &populate
);
2731 up_write(&mm
->mmap_sem
);
2733 mm_populate(ret
, populate
);
2734 if (!IS_ERR_VALUE(ret
))
2739 static inline void verify_mm_writelocked(struct mm_struct
*mm
)
2741 #ifdef CONFIG_DEBUG_VM
2742 if (unlikely(down_read_trylock(&mm
->mmap_sem
))) {
2744 up_read(&mm
->mmap_sem
);
2750 * this is really a simplified "do_mmap". it only handles
2751 * anonymous maps. eventually we may be able to do some
2752 * brk-specific accounting here.
2754 static unsigned long do_brk(unsigned long addr
, unsigned long len
)
2756 struct mm_struct
* mm
= current
->mm
;
2757 struct vm_area_struct
* vma
, * prev
;
2758 unsigned long flags
;
2759 struct rb_node
** rb_link
, * rb_parent
;
2760 pgoff_t pgoff
= addr
>> PAGE_SHIFT
;
2763 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
2765 error
= get_unmapped_area(NULL
, addr
, len
, 0, MAP_FIXED
);
2766 if (error
& ~PAGE_MASK
)
2769 error
= mlock_future_check(mm
, mm
->def_flags
, len
);
2774 * mm->mmap_sem is required to protect against another thread
2775 * changing the mappings in case we sleep.
2777 verify_mm_writelocked(mm
);
2780 * Clear old maps. this also does some error checking for us
2783 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
)) {
2784 if (do_munmap(mm
, addr
, len
))
2789 /* Check against address space limits *after* clearing old maps... */
2790 if (!may_expand_vm(mm
, len
>> PAGE_SHIFT
))
2793 if (mm
->map_count
> sysctl_max_map_count
)
2796 if (security_vm_enough_memory_mm(mm
, len
>> PAGE_SHIFT
))
2799 /* Can we just expand an old private anonymous mapping? */
2800 vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, flags
,
2801 NULL
, NULL
, pgoff
, NULL
);
2806 * create a vma struct for an anonymous mapping
2808 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
2810 vm_unacct_memory(len
>> PAGE_SHIFT
);
2814 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
2816 vma
->vm_start
= addr
;
2817 vma
->vm_end
= addr
+ len
;
2818 vma
->vm_pgoff
= pgoff
;
2819 vma
->vm_flags
= flags
;
2820 vma
->vm_page_prot
= vm_get_page_prot(flags
);
2821 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2823 perf_event_mmap(vma
);
2824 mm
->total_vm
+= len
>> PAGE_SHIFT
;
2825 if (flags
& VM_LOCKED
)
2826 mm
->locked_vm
+= (len
>> PAGE_SHIFT
);
2827 vma
->vm_flags
|= VM_SOFTDIRTY
;
2831 unsigned long vm_brk(unsigned long addr
, unsigned long request
)
2833 struct mm_struct
*mm
= current
->mm
;
2838 len
= PAGE_ALIGN(request
);
2844 down_write(&mm
->mmap_sem
);
2845 ret
= do_brk(addr
, len
);
2846 populate
= ((mm
->def_flags
& VM_LOCKED
) != 0);
2847 up_write(&mm
->mmap_sem
);
2849 mm_populate(addr
, len
);
2852 EXPORT_SYMBOL(vm_brk
);
2854 /* Release all mmaps. */
2855 void exit_mmap(struct mm_struct
*mm
)
2857 struct mmu_gather tlb
;
2858 struct vm_area_struct
*vma
;
2859 unsigned long nr_accounted
= 0;
2861 /* mm's last user has gone, and its about to be pulled down */
2862 mmu_notifier_release(mm
);
2864 if (mm
->locked_vm
) {
2867 if (vma
->vm_flags
& VM_LOCKED
)
2868 munlock_vma_pages_all(vma
);
2876 if (!vma
) /* Can happen if dup_mmap() received an OOM */
2881 tlb_gather_mmu(&tlb
, mm
, 0, -1);
2882 /* update_hiwater_rss(mm) here? but nobody should be looking */
2883 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2884 unmap_vmas(&tlb
, vma
, 0, -1);
2886 free_pgtables(&tlb
, vma
, FIRST_USER_ADDRESS
, USER_PGTABLES_CEILING
);
2887 tlb_finish_mmu(&tlb
, 0, -1);
2890 * Walk the list again, actually closing and freeing it,
2891 * with preemption enabled, without holding any MM locks.
2894 if (vma
->vm_flags
& VM_ACCOUNT
)
2895 nr_accounted
+= vma_pages(vma
);
2896 vma
= remove_vma(vma
);
2898 vm_unacct_memory(nr_accounted
);
2900 WARN_ON(atomic_long_read(&mm
->nr_ptes
) >
2901 (FIRST_USER_ADDRESS
+PMD_SIZE
-1)>>PMD_SHIFT
);
2904 /* Insert vm structure into process list sorted by address
2905 * and into the inode's i_mmap tree. If vm_file is non-NULL
2906 * then i_mmap_mutex is taken here.
2908 int insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
2910 struct vm_area_struct
*prev
;
2911 struct rb_node
**rb_link
, *rb_parent
;
2914 * The vm_pgoff of a purely anonymous vma should be irrelevant
2915 * until its first write fault, when page's anon_vma and index
2916 * are set. But now set the vm_pgoff it will almost certainly
2917 * end up with (unless mremap moves it elsewhere before that
2918 * first wfault), so /proc/pid/maps tells a consistent story.
2920 * By setting it to reflect the virtual start address of the
2921 * vma, merges and splits can happen in a seamless way, just
2922 * using the existing file pgoff checks and manipulations.
2923 * Similarly in do_mmap_pgoff and in do_brk.
2925 if (!vma
->vm_file
) {
2926 BUG_ON(vma
->anon_vma
);
2927 vma
->vm_pgoff
= vma
->vm_start
>> PAGE_SHIFT
;
2929 if (find_vma_links(mm
, vma
->vm_start
, vma
->vm_end
,
2930 &prev
, &rb_link
, &rb_parent
))
2932 if ((vma
->vm_flags
& VM_ACCOUNT
) &&
2933 security_vm_enough_memory_mm(mm
, vma_pages(vma
)))
2936 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2941 * Copy the vma structure to a new location in the same mm,
2942 * prior to moving page table entries, to effect an mremap move.
2944 struct vm_area_struct
*copy_vma(struct vm_area_struct
**vmap
,
2945 unsigned long addr
, unsigned long len
, pgoff_t pgoff
,
2946 bool *need_rmap_locks
)
2948 struct vm_area_struct
*vma
= *vmap
;
2949 unsigned long vma_start
= vma
->vm_start
;
2950 struct mm_struct
*mm
= vma
->vm_mm
;
2951 struct vm_area_struct
*new_vma
, *prev
;
2952 struct rb_node
**rb_link
, *rb_parent
;
2953 bool faulted_in_anon_vma
= true;
2956 * If anonymous vma has not yet been faulted, update new pgoff
2957 * to match new location, to increase its chance of merging.
2959 if (unlikely(!vma
->vm_file
&& !vma
->anon_vma
)) {
2960 pgoff
= addr
>> PAGE_SHIFT
;
2961 faulted_in_anon_vma
= false;
2964 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
))
2965 return NULL
; /* should never get here */
2966 new_vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vma
->vm_flags
,
2967 vma
->anon_vma
, vma
->vm_file
, pgoff
, vma_policy(vma
));
2970 * Source vma may have been merged into new_vma
2972 if (unlikely(vma_start
>= new_vma
->vm_start
&&
2973 vma_start
< new_vma
->vm_end
)) {
2975 * The only way we can get a vma_merge with
2976 * self during an mremap is if the vma hasn't
2977 * been faulted in yet and we were allowed to
2978 * reset the dst vma->vm_pgoff to the
2979 * destination address of the mremap to allow
2980 * the merge to happen. mremap must change the
2981 * vm_pgoff linearity between src and dst vmas
2982 * (in turn preventing a vma_merge) to be
2983 * safe. It is only safe to keep the vm_pgoff
2984 * linear if there are no pages mapped yet.
2986 VM_BUG_ON(faulted_in_anon_vma
);
2987 *vmap
= vma
= new_vma
;
2989 *need_rmap_locks
= (new_vma
->vm_pgoff
<= vma
->vm_pgoff
);
2991 new_vma
= kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
2994 new_vma
->vm_start
= addr
;
2995 new_vma
->vm_end
= addr
+ len
;
2996 new_vma
->vm_pgoff
= pgoff
;
2997 if (vma_dup_policy(vma
, new_vma
))
2999 INIT_LIST_HEAD(&new_vma
->anon_vma_chain
);
3000 if (anon_vma_clone(new_vma
, vma
))
3001 goto out_free_mempol
;
3002 if (new_vma
->vm_file
)
3003 get_file(new_vma
->vm_file
);
3004 if (new_vma
->vm_ops
&& new_vma
->vm_ops
->open
)
3005 new_vma
->vm_ops
->open(new_vma
);
3006 vma_link(mm
, new_vma
, prev
, rb_link
, rb_parent
);
3007 *need_rmap_locks
= false;
3013 mpol_put(vma_policy(new_vma
));
3015 kmem_cache_free(vm_area_cachep
, new_vma
);
3020 * Return true if the calling process may expand its vm space by the passed
3023 int may_expand_vm(struct mm_struct
*mm
, unsigned long npages
)
3025 unsigned long cur
= mm
->total_vm
; /* pages */
3028 lim
= rlimit(RLIMIT_AS
) >> PAGE_SHIFT
;
3030 if (cur
+ npages
> lim
)
3035 static int special_mapping_fault(struct vm_area_struct
*vma
,
3036 struct vm_fault
*vmf
);
3039 * Having a close hook prevents vma merging regardless of flags.
3041 static void special_mapping_close(struct vm_area_struct
*vma
)
3045 static const char *special_mapping_name(struct vm_area_struct
*vma
)
3047 return ((struct vm_special_mapping
*)vma
->vm_private_data
)->name
;
3050 static const struct vm_operations_struct special_mapping_vmops
= {
3051 .close
= special_mapping_close
,
3052 .fault
= special_mapping_fault
,
3053 .name
= special_mapping_name
,
3056 static const struct vm_operations_struct legacy_special_mapping_vmops
= {
3057 .close
= special_mapping_close
,
3058 .fault
= special_mapping_fault
,
3061 static int special_mapping_fault(struct vm_area_struct
*vma
,
3062 struct vm_fault
*vmf
)
3065 struct page
**pages
;
3068 * special mappings have no vm_file, and in that case, the mm
3069 * uses vm_pgoff internally. So we have to subtract it from here.
3070 * We are allowed to do this because we are the mm; do not copy
3071 * this code into drivers!
3073 pgoff
= vmf
->pgoff
- vma
->vm_pgoff
;
3075 if (vma
->vm_ops
== &legacy_special_mapping_vmops
)
3076 pages
= vma
->vm_private_data
;
3078 pages
= ((struct vm_special_mapping
*)vma
->vm_private_data
)->
3081 for (; pgoff
&& *pages
; ++pages
)
3085 struct page
*page
= *pages
;
3091 return VM_FAULT_SIGBUS
;
3094 static struct vm_area_struct
*__install_special_mapping(
3095 struct mm_struct
*mm
,
3096 unsigned long addr
, unsigned long len
,
3097 unsigned long vm_flags
, const struct vm_operations_struct
*ops
,
3101 struct vm_area_struct
*vma
;
3103 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
3104 if (unlikely(vma
== NULL
))
3105 return ERR_PTR(-ENOMEM
);
3107 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
3109 vma
->vm_start
= addr
;
3110 vma
->vm_end
= addr
+ len
;
3112 vma
->vm_flags
= vm_flags
| mm
->def_flags
| VM_DONTEXPAND
| VM_SOFTDIRTY
;
3113 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
3116 vma
->vm_private_data
= priv
;
3118 ret
= insert_vm_struct(mm
, vma
);
3122 mm
->total_vm
+= len
>> PAGE_SHIFT
;
3124 perf_event_mmap(vma
);
3129 kmem_cache_free(vm_area_cachep
, vma
);
3130 return ERR_PTR(ret
);
3134 * Called with mm->mmap_sem held for writing.
3135 * Insert a new vma covering the given region, with the given flags.
3136 * Its pages are supplied by the given array of struct page *.
3137 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3138 * The region past the last page supplied will always produce SIGBUS.
3139 * The array pointer and the pages it points to are assumed to stay alive
3140 * for as long as this mapping might exist.
3142 struct vm_area_struct
*_install_special_mapping(
3143 struct mm_struct
*mm
,
3144 unsigned long addr
, unsigned long len
,
3145 unsigned long vm_flags
, const struct vm_special_mapping
*spec
)
3147 return __install_special_mapping(mm
, addr
, len
, vm_flags
,
3148 &special_mapping_vmops
, (void *)spec
);
3151 int install_special_mapping(struct mm_struct
*mm
,
3152 unsigned long addr
, unsigned long len
,
3153 unsigned long vm_flags
, struct page
**pages
)
3155 struct vm_area_struct
*vma
= __install_special_mapping(
3156 mm
, addr
, len
, vm_flags
, &legacy_special_mapping_vmops
,
3159 return PTR_ERR_OR_ZERO(vma
);
3162 static DEFINE_MUTEX(mm_all_locks_mutex
);
3164 static void vm_lock_anon_vma(struct mm_struct
*mm
, struct anon_vma
*anon_vma
)
3166 if (!test_bit(0, (unsigned long *) &anon_vma
->root
->rb_root
.rb_node
)) {
3168 * The LSB of head.next can't change from under us
3169 * because we hold the mm_all_locks_mutex.
3171 down_write_nest_lock(&anon_vma
->root
->rwsem
, &mm
->mmap_sem
);
3173 * We can safely modify head.next after taking the
3174 * anon_vma->root->rwsem. If some other vma in this mm shares
3175 * the same anon_vma we won't take it again.
3177 * No need of atomic instructions here, head.next
3178 * can't change from under us thanks to the
3179 * anon_vma->root->rwsem.
3181 if (__test_and_set_bit(0, (unsigned long *)
3182 &anon_vma
->root
->rb_root
.rb_node
))
3187 static void vm_lock_mapping(struct mm_struct
*mm
, struct address_space
*mapping
)
3189 if (!test_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
)) {
3191 * AS_MM_ALL_LOCKS can't change from under us because
3192 * we hold the mm_all_locks_mutex.
3194 * Operations on ->flags have to be atomic because
3195 * even if AS_MM_ALL_LOCKS is stable thanks to the
3196 * mm_all_locks_mutex, there may be other cpus
3197 * changing other bitflags in parallel to us.
3199 if (test_and_set_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
))
3201 mutex_lock_nest_lock(&mapping
->i_mmap_mutex
, &mm
->mmap_sem
);
3206 * This operation locks against the VM for all pte/vma/mm related
3207 * operations that could ever happen on a certain mm. This includes
3208 * vmtruncate, try_to_unmap, and all page faults.
3210 * The caller must take the mmap_sem in write mode before calling
3211 * mm_take_all_locks(). The caller isn't allowed to release the
3212 * mmap_sem until mm_drop_all_locks() returns.
3214 * mmap_sem in write mode is required in order to block all operations
3215 * that could modify pagetables and free pages without need of
3216 * altering the vma layout. It's also needed in write mode to avoid new
3217 * anon_vmas to be associated with existing vmas.
3219 * A single task can't take more than one mm_take_all_locks() in a row
3220 * or it would deadlock.
3222 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
3223 * mapping->flags avoid to take the same lock twice, if more than one
3224 * vma in this mm is backed by the same anon_vma or address_space.
3226 * We can take all the locks in random order because the VM code
3227 * taking i_mmap_mutex or anon_vma->rwsem outside the mmap_sem never
3228 * takes more than one of them in a row. Secondly we're protected
3229 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
3231 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3232 * that may have to take thousand of locks.
3234 * mm_take_all_locks() can fail if it's interrupted by signals.
3236 int mm_take_all_locks(struct mm_struct
*mm
)
3238 struct vm_area_struct
*vma
;
3239 struct anon_vma_chain
*avc
;
3241 BUG_ON(down_read_trylock(&mm
->mmap_sem
));
3243 mutex_lock(&mm_all_locks_mutex
);
3245 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3246 if (signal_pending(current
))
3248 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
)
3249 vm_lock_mapping(mm
, vma
->vm_file
->f_mapping
);
3252 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3253 if (signal_pending(current
))
3256 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
3257 vm_lock_anon_vma(mm
, avc
->anon_vma
);
3263 mm_drop_all_locks(mm
);
3267 static void vm_unlock_anon_vma(struct anon_vma
*anon_vma
)
3269 if (test_bit(0, (unsigned long *) &anon_vma
->root
->rb_root
.rb_node
)) {
3271 * The LSB of head.next can't change to 0 from under
3272 * us because we hold the mm_all_locks_mutex.
3274 * We must however clear the bitflag before unlocking
3275 * the vma so the users using the anon_vma->rb_root will
3276 * never see our bitflag.
3278 * No need of atomic instructions here, head.next
3279 * can't change from under us until we release the
3280 * anon_vma->root->rwsem.
3282 if (!__test_and_clear_bit(0, (unsigned long *)
3283 &anon_vma
->root
->rb_root
.rb_node
))
3285 anon_vma_unlock_write(anon_vma
);
3289 static void vm_unlock_mapping(struct address_space
*mapping
)
3291 if (test_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
)) {
3293 * AS_MM_ALL_LOCKS can't change to 0 from under us
3294 * because we hold the mm_all_locks_mutex.
3296 mutex_unlock(&mapping
->i_mmap_mutex
);
3297 if (!test_and_clear_bit(AS_MM_ALL_LOCKS
,
3304 * The mmap_sem cannot be released by the caller until
3305 * mm_drop_all_locks() returns.
3307 void mm_drop_all_locks(struct mm_struct
*mm
)
3309 struct vm_area_struct
*vma
;
3310 struct anon_vma_chain
*avc
;
3312 BUG_ON(down_read_trylock(&mm
->mmap_sem
));
3313 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex
));
3315 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3317 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
3318 vm_unlock_anon_vma(avc
->anon_vma
);
3319 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
)
3320 vm_unlock_mapping(vma
->vm_file
->f_mapping
);
3323 mutex_unlock(&mm_all_locks_mutex
);
3327 * initialise the VMA slab
3329 void __init
mmap_init(void)
3333 ret
= percpu_counter_init(&vm_committed_as
, 0);
3338 * Initialise sysctl_user_reserve_kbytes.
3340 * This is intended to prevent a user from starting a single memory hogging
3341 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3344 * The default value is min(3% of free memory, 128MB)
3345 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3347 static int init_user_reserve(void)
3349 unsigned long free_kbytes
;
3351 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
3353 sysctl_user_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 17);
3356 subsys_initcall(init_user_reserve
);
3359 * Initialise sysctl_admin_reserve_kbytes.
3361 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3362 * to log in and kill a memory hogging process.
3364 * Systems with more than 256MB will reserve 8MB, enough to recover
3365 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3366 * only reserve 3% of free pages by default.
3368 static int init_admin_reserve(void)
3370 unsigned long free_kbytes
;
3372 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
3374 sysctl_admin_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 13);
3377 subsys_initcall(init_admin_reserve
);
3380 * Reinititalise user and admin reserves if memory is added or removed.
3382 * The default user reserve max is 128MB, and the default max for the
3383 * admin reserve is 8MB. These are usually, but not always, enough to
3384 * enable recovery from a memory hogging process using login/sshd, a shell,
3385 * and tools like top. It may make sense to increase or even disable the
3386 * reserve depending on the existence of swap or variations in the recovery
3387 * tools. So, the admin may have changed them.
3389 * If memory is added and the reserves have been eliminated or increased above
3390 * the default max, then we'll trust the admin.
3392 * If memory is removed and there isn't enough free memory, then we
3393 * need to reset the reserves.
3395 * Otherwise keep the reserve set by the admin.
3397 static int reserve_mem_notifier(struct notifier_block
*nb
,
3398 unsigned long action
, void *data
)
3400 unsigned long tmp
, free_kbytes
;
3404 /* Default max is 128MB. Leave alone if modified by operator. */
3405 tmp
= sysctl_user_reserve_kbytes
;
3406 if (0 < tmp
&& tmp
< (1UL << 17))
3407 init_user_reserve();
3409 /* Default max is 8MB. Leave alone if modified by operator. */
3410 tmp
= sysctl_admin_reserve_kbytes
;
3411 if (0 < tmp
&& tmp
< (1UL << 13))
3412 init_admin_reserve();
3416 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
3418 if (sysctl_user_reserve_kbytes
> free_kbytes
) {
3419 init_user_reserve();
3420 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3421 sysctl_user_reserve_kbytes
);
3424 if (sysctl_admin_reserve_kbytes
> free_kbytes
) {
3425 init_admin_reserve();
3426 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3427 sysctl_admin_reserve_kbytes
);
3436 static struct notifier_block reserve_mem_nb
= {
3437 .notifier_call
= reserve_mem_notifier
,
3440 static int __meminit
init_reserve_notifier(void)
3442 if (register_hotmemory_notifier(&reserve_mem_nb
))
3443 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
3447 subsys_initcall(init_reserve_notifier
);