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/shmem_fs.h>
29 #include <linux/profile.h>
30 #include <linux/export.h>
31 #include <linux/mount.h>
32 #include <linux/mempolicy.h>
33 #include <linux/rmap.h>
34 #include <linux/mmu_notifier.h>
35 #include <linux/mmdebug.h>
36 #include <linux/perf_event.h>
37 #include <linux/audit.h>
38 #include <linux/khugepaged.h>
39 #include <linux/uprobes.h>
40 #include <linux/rbtree_augmented.h>
41 #include <linux/notifier.h>
42 #include <linux/memory.h>
43 #include <linux/printk.h>
44 #include <linux/userfaultfd_k.h>
45 #include <linux/moduleparam.h>
46 #include <linux/pkeys.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
51 #include <asm/mmu_context.h>
55 #ifndef arch_mmap_check
56 #define arch_mmap_check(addr, len, flags) (0)
59 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
60 const int mmap_rnd_bits_min
= CONFIG_ARCH_MMAP_RND_BITS_MIN
;
61 const int mmap_rnd_bits_max
= CONFIG_ARCH_MMAP_RND_BITS_MAX
;
62 int mmap_rnd_bits __read_mostly
= CONFIG_ARCH_MMAP_RND_BITS
;
64 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
65 const int mmap_rnd_compat_bits_min
= CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN
;
66 const int mmap_rnd_compat_bits_max
= CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX
;
67 int mmap_rnd_compat_bits __read_mostly
= CONFIG_ARCH_MMAP_RND_COMPAT_BITS
;
70 static bool ignore_rlimit_data
;
71 core_param(ignore_rlimit_data
, ignore_rlimit_data
, bool, 0644);
73 static void unmap_region(struct mm_struct
*mm
,
74 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
75 unsigned long start
, unsigned long end
);
77 /* description of effects of mapping type and prot in current implementation.
78 * this is due to the limited x86 page protection hardware. The expected
79 * behavior is in parens:
82 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
83 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
84 * w: (no) no w: (no) no w: (yes) yes w: (no) no
85 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
87 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
88 * w: (no) no w: (no) no w: (copy) copy w: (no) no
89 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
92 pgprot_t protection_map
[16] = {
93 __P000
, __P001
, __P010
, __P011
, __P100
, __P101
, __P110
, __P111
,
94 __S000
, __S001
, __S010
, __S011
, __S100
, __S101
, __S110
, __S111
97 pgprot_t
vm_get_page_prot(unsigned long vm_flags
)
99 return __pgprot(pgprot_val(protection_map
[vm_flags
&
100 (VM_READ
|VM_WRITE
|VM_EXEC
|VM_SHARED
)]) |
101 pgprot_val(arch_vm_get_page_prot(vm_flags
)));
103 EXPORT_SYMBOL(vm_get_page_prot
);
105 static pgprot_t
vm_pgprot_modify(pgprot_t oldprot
, unsigned long vm_flags
)
107 return pgprot_modify(oldprot
, vm_get_page_prot(vm_flags
));
110 /* Update vma->vm_page_prot to reflect vma->vm_flags. */
111 void vma_set_page_prot(struct vm_area_struct
*vma
)
113 unsigned long vm_flags
= vma
->vm_flags
;
115 vma
->vm_page_prot
= vm_pgprot_modify(vma
->vm_page_prot
, vm_flags
);
116 if (vma_wants_writenotify(vma
)) {
117 vm_flags
&= ~VM_SHARED
;
118 vma
->vm_page_prot
= vm_pgprot_modify(vma
->vm_page_prot
,
124 * Requires inode->i_mapping->i_mmap_rwsem
126 static void __remove_shared_vm_struct(struct vm_area_struct
*vma
,
127 struct file
*file
, struct address_space
*mapping
)
129 if (vma
->vm_flags
& VM_DENYWRITE
)
130 atomic_inc(&file_inode(file
)->i_writecount
);
131 if (vma
->vm_flags
& VM_SHARED
)
132 mapping_unmap_writable(mapping
);
134 flush_dcache_mmap_lock(mapping
);
135 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
136 flush_dcache_mmap_unlock(mapping
);
140 * Unlink a file-based vm structure from its interval tree, to hide
141 * vma from rmap and vmtruncate before freeing its page tables.
143 void unlink_file_vma(struct vm_area_struct
*vma
)
145 struct file
*file
= vma
->vm_file
;
148 struct address_space
*mapping
= file
->f_mapping
;
149 i_mmap_lock_write(mapping
);
150 __remove_shared_vm_struct(vma
, file
, mapping
);
151 i_mmap_unlock_write(mapping
);
156 * Close a vm structure and free it, returning the next.
158 static struct vm_area_struct
*remove_vma(struct vm_area_struct
*vma
)
160 struct vm_area_struct
*next
= vma
->vm_next
;
163 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
164 vma
->vm_ops
->close(vma
);
167 mpol_put(vma_policy(vma
));
168 kmem_cache_free(vm_area_cachep
, vma
);
172 static int do_brk(unsigned long addr
, unsigned long len
);
174 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
176 unsigned long retval
;
177 unsigned long newbrk
, oldbrk
;
178 struct mm_struct
*mm
= current
->mm
;
179 unsigned long min_brk
;
182 if (down_write_killable(&mm
->mmap_sem
))
185 #ifdef CONFIG_COMPAT_BRK
187 * CONFIG_COMPAT_BRK can still be overridden by setting
188 * randomize_va_space to 2, which will still cause mm->start_brk
189 * to be arbitrarily shifted
191 if (current
->brk_randomized
)
192 min_brk
= mm
->start_brk
;
194 min_brk
= mm
->end_data
;
196 min_brk
= mm
->start_brk
;
202 * Check against rlimit here. If this check is done later after the test
203 * of oldbrk with newbrk then it can escape the test and let the data
204 * segment grow beyond its set limit the in case where the limit is
205 * not page aligned -Ram Gupta
207 if (check_data_rlimit(rlimit(RLIMIT_DATA
), brk
, mm
->start_brk
,
208 mm
->end_data
, mm
->start_data
))
211 newbrk
= PAGE_ALIGN(brk
);
212 oldbrk
= PAGE_ALIGN(mm
->brk
);
213 if (oldbrk
== newbrk
)
216 /* Always allow shrinking brk. */
217 if (brk
<= mm
->brk
) {
218 if (!do_munmap(mm
, newbrk
, oldbrk
-newbrk
))
223 /* Check against existing mmap mappings. */
224 if (find_vma_intersection(mm
, oldbrk
, newbrk
+PAGE_SIZE
))
227 /* Ok, looks good - let it rip. */
228 if (do_brk(oldbrk
, newbrk
-oldbrk
) < 0)
233 populate
= newbrk
> oldbrk
&& (mm
->def_flags
& VM_LOCKED
) != 0;
234 up_write(&mm
->mmap_sem
);
236 mm_populate(oldbrk
, newbrk
- oldbrk
);
241 up_write(&mm
->mmap_sem
);
245 static long vma_compute_subtree_gap(struct vm_area_struct
*vma
)
247 unsigned long max
, subtree_gap
;
250 max
-= vma
->vm_prev
->vm_end
;
251 if (vma
->vm_rb
.rb_left
) {
252 subtree_gap
= rb_entry(vma
->vm_rb
.rb_left
,
253 struct vm_area_struct
, vm_rb
)->rb_subtree_gap
;
254 if (subtree_gap
> max
)
257 if (vma
->vm_rb
.rb_right
) {
258 subtree_gap
= rb_entry(vma
->vm_rb
.rb_right
,
259 struct vm_area_struct
, vm_rb
)->rb_subtree_gap
;
260 if (subtree_gap
> max
)
266 #ifdef CONFIG_DEBUG_VM_RB
267 static int browse_rb(struct mm_struct
*mm
)
269 struct rb_root
*root
= &mm
->mm_rb
;
270 int i
= 0, j
, bug
= 0;
271 struct rb_node
*nd
, *pn
= NULL
;
272 unsigned long prev
= 0, pend
= 0;
274 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
275 struct vm_area_struct
*vma
;
276 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
277 if (vma
->vm_start
< prev
) {
278 pr_emerg("vm_start %lx < prev %lx\n",
279 vma
->vm_start
, prev
);
282 if (vma
->vm_start
< pend
) {
283 pr_emerg("vm_start %lx < pend %lx\n",
284 vma
->vm_start
, pend
);
287 if (vma
->vm_start
> vma
->vm_end
) {
288 pr_emerg("vm_start %lx > vm_end %lx\n",
289 vma
->vm_start
, vma
->vm_end
);
292 spin_lock(&mm
->page_table_lock
);
293 if (vma
->rb_subtree_gap
!= vma_compute_subtree_gap(vma
)) {
294 pr_emerg("free gap %lx, correct %lx\n",
296 vma_compute_subtree_gap(vma
));
299 spin_unlock(&mm
->page_table_lock
);
302 prev
= vma
->vm_start
;
306 for (nd
= pn
; nd
; nd
= rb_prev(nd
))
309 pr_emerg("backwards %d, forwards %d\n", j
, i
);
315 static void validate_mm_rb(struct rb_root
*root
, struct vm_area_struct
*ignore
)
319 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
320 struct vm_area_struct
*vma
;
321 vma
= rb_entry(nd
, struct vm_area_struct
, vm_rb
);
322 VM_BUG_ON_VMA(vma
!= ignore
&&
323 vma
->rb_subtree_gap
!= vma_compute_subtree_gap(vma
),
328 static void validate_mm(struct mm_struct
*mm
)
332 unsigned long highest_address
= 0;
333 struct vm_area_struct
*vma
= mm
->mmap
;
336 struct anon_vma
*anon_vma
= vma
->anon_vma
;
337 struct anon_vma_chain
*avc
;
340 anon_vma_lock_read(anon_vma
);
341 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
342 anon_vma_interval_tree_verify(avc
);
343 anon_vma_unlock_read(anon_vma
);
346 highest_address
= vma
->vm_end
;
350 if (i
!= mm
->map_count
) {
351 pr_emerg("map_count %d vm_next %d\n", mm
->map_count
, i
);
354 if (highest_address
!= mm
->highest_vm_end
) {
355 pr_emerg("mm->highest_vm_end %lx, found %lx\n",
356 mm
->highest_vm_end
, highest_address
);
360 if (i
!= mm
->map_count
) {
362 pr_emerg("map_count %d rb %d\n", mm
->map_count
, i
);
365 VM_BUG_ON_MM(bug
, mm
);
368 #define validate_mm_rb(root, ignore) do { } while (0)
369 #define validate_mm(mm) do { } while (0)
372 RB_DECLARE_CALLBACKS(static, vma_gap_callbacks
, struct vm_area_struct
, vm_rb
,
373 unsigned long, rb_subtree_gap
, vma_compute_subtree_gap
)
376 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
377 * vma->vm_prev->vm_end values changed, without modifying the vma's position
380 static void vma_gap_update(struct vm_area_struct
*vma
)
383 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
384 * function that does exacltly what we want.
386 vma_gap_callbacks_propagate(&vma
->vm_rb
, NULL
);
389 static inline void vma_rb_insert(struct vm_area_struct
*vma
,
390 struct rb_root
*root
)
392 /* All rb_subtree_gap values must be consistent prior to insertion */
393 validate_mm_rb(root
, NULL
);
395 rb_insert_augmented(&vma
->vm_rb
, root
, &vma_gap_callbacks
);
398 static void vma_rb_erase(struct vm_area_struct
*vma
, struct rb_root
*root
)
401 * All rb_subtree_gap values must be consistent prior to erase,
402 * with the possible exception of the vma being erased.
404 validate_mm_rb(root
, vma
);
407 * Note rb_erase_augmented is a fairly large inline function,
408 * so make sure we instantiate it only once with our desired
409 * augmented rbtree callbacks.
411 rb_erase_augmented(&vma
->vm_rb
, root
, &vma_gap_callbacks
);
415 * vma has some anon_vma assigned, and is already inserted on that
416 * anon_vma's interval trees.
418 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
419 * vma must be removed from the anon_vma's interval trees using
420 * anon_vma_interval_tree_pre_update_vma().
422 * After the update, the vma will be reinserted using
423 * anon_vma_interval_tree_post_update_vma().
425 * The entire update must be protected by exclusive mmap_sem and by
426 * the root anon_vma's mutex.
429 anon_vma_interval_tree_pre_update_vma(struct vm_area_struct
*vma
)
431 struct anon_vma_chain
*avc
;
433 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
434 anon_vma_interval_tree_remove(avc
, &avc
->anon_vma
->rb_root
);
438 anon_vma_interval_tree_post_update_vma(struct vm_area_struct
*vma
)
440 struct anon_vma_chain
*avc
;
442 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
443 anon_vma_interval_tree_insert(avc
, &avc
->anon_vma
->rb_root
);
446 static int find_vma_links(struct mm_struct
*mm
, unsigned long addr
,
447 unsigned long end
, struct vm_area_struct
**pprev
,
448 struct rb_node
***rb_link
, struct rb_node
**rb_parent
)
450 struct rb_node
**__rb_link
, *__rb_parent
, *rb_prev
;
452 __rb_link
= &mm
->mm_rb
.rb_node
;
453 rb_prev
= __rb_parent
= NULL
;
456 struct vm_area_struct
*vma_tmp
;
458 __rb_parent
= *__rb_link
;
459 vma_tmp
= rb_entry(__rb_parent
, struct vm_area_struct
, vm_rb
);
461 if (vma_tmp
->vm_end
> addr
) {
462 /* Fail if an existing vma overlaps the area */
463 if (vma_tmp
->vm_start
< end
)
465 __rb_link
= &__rb_parent
->rb_left
;
467 rb_prev
= __rb_parent
;
468 __rb_link
= &__rb_parent
->rb_right
;
474 *pprev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
475 *rb_link
= __rb_link
;
476 *rb_parent
= __rb_parent
;
480 static unsigned long count_vma_pages_range(struct mm_struct
*mm
,
481 unsigned long addr
, unsigned long end
)
483 unsigned long nr_pages
= 0;
484 struct vm_area_struct
*vma
;
486 /* Find first overlaping mapping */
487 vma
= find_vma_intersection(mm
, addr
, end
);
491 nr_pages
= (min(end
, vma
->vm_end
) -
492 max(addr
, vma
->vm_start
)) >> PAGE_SHIFT
;
494 /* Iterate over the rest of the overlaps */
495 for (vma
= vma
->vm_next
; vma
; vma
= vma
->vm_next
) {
496 unsigned long overlap_len
;
498 if (vma
->vm_start
> end
)
501 overlap_len
= min(end
, vma
->vm_end
) - vma
->vm_start
;
502 nr_pages
+= overlap_len
>> PAGE_SHIFT
;
508 void __vma_link_rb(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
509 struct rb_node
**rb_link
, struct rb_node
*rb_parent
)
511 /* Update tracking information for the gap following the new vma. */
513 vma_gap_update(vma
->vm_next
);
515 mm
->highest_vm_end
= vma
->vm_end
;
518 * vma->vm_prev wasn't known when we followed the rbtree to find the
519 * correct insertion point for that vma. As a result, we could not
520 * update the vma vm_rb parents rb_subtree_gap values on the way down.
521 * So, we first insert the vma with a zero rb_subtree_gap value
522 * (to be consistent with what we did on the way down), and then
523 * immediately update the gap to the correct value. Finally we
524 * rebalance the rbtree after all augmented values have been set.
526 rb_link_node(&vma
->vm_rb
, rb_parent
, rb_link
);
527 vma
->rb_subtree_gap
= 0;
529 vma_rb_insert(vma
, &mm
->mm_rb
);
532 static void __vma_link_file(struct vm_area_struct
*vma
)
538 struct address_space
*mapping
= file
->f_mapping
;
540 if (vma
->vm_flags
& VM_DENYWRITE
)
541 atomic_dec(&file_inode(file
)->i_writecount
);
542 if (vma
->vm_flags
& VM_SHARED
)
543 atomic_inc(&mapping
->i_mmap_writable
);
545 flush_dcache_mmap_lock(mapping
);
546 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
547 flush_dcache_mmap_unlock(mapping
);
552 __vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
553 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
554 struct rb_node
*rb_parent
)
556 __vma_link_list(mm
, vma
, prev
, rb_parent
);
557 __vma_link_rb(mm
, vma
, rb_link
, rb_parent
);
560 static void vma_link(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
561 struct vm_area_struct
*prev
, struct rb_node
**rb_link
,
562 struct rb_node
*rb_parent
)
564 struct address_space
*mapping
= NULL
;
567 mapping
= vma
->vm_file
->f_mapping
;
568 i_mmap_lock_write(mapping
);
571 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
572 __vma_link_file(vma
);
575 i_mmap_unlock_write(mapping
);
582 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
583 * mm's list and rbtree. It has already been inserted into the interval tree.
585 static void __insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
587 struct vm_area_struct
*prev
;
588 struct rb_node
**rb_link
, *rb_parent
;
590 if (find_vma_links(mm
, vma
->vm_start
, vma
->vm_end
,
591 &prev
, &rb_link
, &rb_parent
))
593 __vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
598 __vma_unlink(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
599 struct vm_area_struct
*prev
)
601 struct vm_area_struct
*next
;
603 vma_rb_erase(vma
, &mm
->mm_rb
);
604 prev
->vm_next
= next
= vma
->vm_next
;
606 next
->vm_prev
= prev
;
609 vmacache_invalidate(mm
);
613 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
614 * is already present in an i_mmap tree without adjusting the tree.
615 * The following helper function should be used when such adjustments
616 * are necessary. The "insert" vma (if any) is to be inserted
617 * before we drop the necessary locks.
619 int vma_adjust(struct vm_area_struct
*vma
, unsigned long start
,
620 unsigned long end
, pgoff_t pgoff
, struct vm_area_struct
*insert
)
622 struct mm_struct
*mm
= vma
->vm_mm
;
623 struct vm_area_struct
*next
= vma
->vm_next
;
624 struct vm_area_struct
*importer
= NULL
;
625 struct address_space
*mapping
= NULL
;
626 struct rb_root
*root
= NULL
;
627 struct anon_vma
*anon_vma
= NULL
;
628 struct file
*file
= vma
->vm_file
;
629 bool start_changed
= false, end_changed
= false;
630 long adjust_next
= 0;
633 if (next
&& !insert
) {
634 struct vm_area_struct
*exporter
= NULL
;
636 if (end
>= next
->vm_end
) {
638 * vma expands, overlapping all the next, and
639 * perhaps the one after too (mprotect case 6).
641 again
: remove_next
= 1 + (end
> next
->vm_end
);
645 } else if (end
> next
->vm_start
) {
647 * vma expands, overlapping part of the next:
648 * mprotect case 5 shifting the boundary up.
650 adjust_next
= (end
- next
->vm_start
) >> PAGE_SHIFT
;
653 } else if (end
< vma
->vm_end
) {
655 * vma shrinks, and !insert tells it's not
656 * split_vma inserting another: so it must be
657 * mprotect case 4 shifting the boundary down.
659 adjust_next
= -((vma
->vm_end
- end
) >> PAGE_SHIFT
);
665 * Easily overlooked: when mprotect shifts the boundary,
666 * make sure the expanding vma has anon_vma set if the
667 * shrinking vma had, to cover any anon pages imported.
669 if (exporter
&& exporter
->anon_vma
&& !importer
->anon_vma
) {
672 importer
->anon_vma
= exporter
->anon_vma
;
673 error
= anon_vma_clone(importer
, exporter
);
679 vma_adjust_trans_huge(vma
, start
, end
, adjust_next
);
682 mapping
= file
->f_mapping
;
683 root
= &mapping
->i_mmap
;
684 uprobe_munmap(vma
, vma
->vm_start
, vma
->vm_end
);
687 uprobe_munmap(next
, next
->vm_start
, next
->vm_end
);
689 i_mmap_lock_write(mapping
);
692 * Put into interval tree now, so instantiated pages
693 * are visible to arm/parisc __flush_dcache_page
694 * throughout; but we cannot insert into address
695 * space until vma start or end is updated.
697 __vma_link_file(insert
);
701 anon_vma
= vma
->anon_vma
;
702 if (!anon_vma
&& adjust_next
)
703 anon_vma
= next
->anon_vma
;
705 VM_BUG_ON_VMA(adjust_next
&& next
->anon_vma
&&
706 anon_vma
!= next
->anon_vma
, next
);
707 anon_vma_lock_write(anon_vma
);
708 anon_vma_interval_tree_pre_update_vma(vma
);
710 anon_vma_interval_tree_pre_update_vma(next
);
714 flush_dcache_mmap_lock(mapping
);
715 vma_interval_tree_remove(vma
, root
);
717 vma_interval_tree_remove(next
, root
);
720 if (start
!= vma
->vm_start
) {
721 vma
->vm_start
= start
;
722 start_changed
= true;
724 if (end
!= vma
->vm_end
) {
728 vma
->vm_pgoff
= pgoff
;
730 next
->vm_start
+= adjust_next
<< PAGE_SHIFT
;
731 next
->vm_pgoff
+= adjust_next
;
736 vma_interval_tree_insert(next
, root
);
737 vma_interval_tree_insert(vma
, root
);
738 flush_dcache_mmap_unlock(mapping
);
743 * vma_merge has merged next into vma, and needs
744 * us to remove next before dropping the locks.
746 __vma_unlink(mm
, next
, vma
);
748 __remove_shared_vm_struct(next
, file
, mapping
);
751 * split_vma has split insert from vma, and needs
752 * us to insert it before dropping the locks
753 * (it may either follow vma or precede it).
755 __insert_vm_struct(mm
, insert
);
761 mm
->highest_vm_end
= end
;
762 else if (!adjust_next
)
763 vma_gap_update(next
);
768 anon_vma_interval_tree_post_update_vma(vma
);
770 anon_vma_interval_tree_post_update_vma(next
);
771 anon_vma_unlock_write(anon_vma
);
774 i_mmap_unlock_write(mapping
);
785 uprobe_munmap(next
, next
->vm_start
, next
->vm_end
);
789 anon_vma_merge(vma
, next
);
791 mpol_put(vma_policy(next
));
792 kmem_cache_free(vm_area_cachep
, next
);
794 * In mprotect's case 6 (see comments on vma_merge),
795 * we must remove another next too. It would clutter
796 * up the code too much to do both in one go.
799 if (remove_next
== 2)
802 vma_gap_update(next
);
804 mm
->highest_vm_end
= end
;
815 * If the vma has a ->close operation then the driver probably needs to release
816 * per-vma resources, so we don't attempt to merge those.
818 static inline int is_mergeable_vma(struct vm_area_struct
*vma
,
819 struct file
*file
, unsigned long vm_flags
,
820 struct vm_userfaultfd_ctx vm_userfaultfd_ctx
)
823 * VM_SOFTDIRTY should not prevent from VMA merging, if we
824 * match the flags but dirty bit -- the caller should mark
825 * merged VMA as dirty. If dirty bit won't be excluded from
826 * comparison, we increase pressue on the memory system forcing
827 * the kernel to generate new VMAs when old one could be
830 if ((vma
->vm_flags
^ vm_flags
) & ~VM_SOFTDIRTY
)
832 if (vma
->vm_file
!= file
)
834 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
836 if (!is_mergeable_vm_userfaultfd_ctx(vma
, vm_userfaultfd_ctx
))
841 static inline int is_mergeable_anon_vma(struct anon_vma
*anon_vma1
,
842 struct anon_vma
*anon_vma2
,
843 struct vm_area_struct
*vma
)
846 * The list_is_singular() test is to avoid merging VMA cloned from
847 * parents. This can improve scalability caused by anon_vma lock.
849 if ((!anon_vma1
|| !anon_vma2
) && (!vma
||
850 list_is_singular(&vma
->anon_vma_chain
)))
852 return anon_vma1
== anon_vma2
;
856 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
857 * in front of (at a lower virtual address and file offset than) the vma.
859 * We cannot merge two vmas if they have differently assigned (non-NULL)
860 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
862 * We don't check here for the merged mmap wrapping around the end of pagecache
863 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
864 * wrap, nor mmaps which cover the final page at index -1UL.
867 can_vma_merge_before(struct vm_area_struct
*vma
, unsigned long vm_flags
,
868 struct anon_vma
*anon_vma
, struct file
*file
,
870 struct vm_userfaultfd_ctx vm_userfaultfd_ctx
)
872 if (is_mergeable_vma(vma
, file
, vm_flags
, vm_userfaultfd_ctx
) &&
873 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
, vma
)) {
874 if (vma
->vm_pgoff
== vm_pgoff
)
881 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
882 * beyond (at a higher virtual address and file offset than) the vma.
884 * We cannot merge two vmas if they have differently assigned (non-NULL)
885 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
888 can_vma_merge_after(struct vm_area_struct
*vma
, unsigned long vm_flags
,
889 struct anon_vma
*anon_vma
, struct file
*file
,
891 struct vm_userfaultfd_ctx vm_userfaultfd_ctx
)
893 if (is_mergeable_vma(vma
, file
, vm_flags
, vm_userfaultfd_ctx
) &&
894 is_mergeable_anon_vma(anon_vma
, vma
->anon_vma
, vma
)) {
896 vm_pglen
= vma_pages(vma
);
897 if (vma
->vm_pgoff
+ vm_pglen
== vm_pgoff
)
904 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
905 * whether that can be merged with its predecessor or its successor.
906 * Or both (it neatly fills a hole).
908 * In most cases - when called for mmap, brk or mremap - [addr,end) is
909 * certain not to be mapped by the time vma_merge is called; but when
910 * called for mprotect, it is certain to be already mapped (either at
911 * an offset within prev, or at the start of next), and the flags of
912 * this area are about to be changed to vm_flags - and the no-change
913 * case has already been eliminated.
915 * The following mprotect cases have to be considered, where AAAA is
916 * the area passed down from mprotect_fixup, never extending beyond one
917 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
919 * AAAA AAAA AAAA AAAA
920 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
921 * cannot merge might become might become might become
922 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
923 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
924 * mremap move: PPPPNNNNNNNN 8
926 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
927 * might become case 1 below case 2 below case 3 below
929 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
930 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
932 struct vm_area_struct
*vma_merge(struct mm_struct
*mm
,
933 struct vm_area_struct
*prev
, unsigned long addr
,
934 unsigned long end
, unsigned long vm_flags
,
935 struct anon_vma
*anon_vma
, struct file
*file
,
936 pgoff_t pgoff
, struct mempolicy
*policy
,
937 struct vm_userfaultfd_ctx vm_userfaultfd_ctx
)
939 pgoff_t pglen
= (end
- addr
) >> PAGE_SHIFT
;
940 struct vm_area_struct
*area
, *next
;
944 * We later require that vma->vm_flags == vm_flags,
945 * so this tests vma->vm_flags & VM_SPECIAL, too.
947 if (vm_flags
& VM_SPECIAL
)
951 next
= prev
->vm_next
;
955 if (next
&& next
->vm_end
== end
) /* cases 6, 7, 8 */
956 next
= next
->vm_next
;
959 * Can it merge with the predecessor?
961 if (prev
&& prev
->vm_end
== addr
&&
962 mpol_equal(vma_policy(prev
), policy
) &&
963 can_vma_merge_after(prev
, vm_flags
,
964 anon_vma
, file
, pgoff
,
965 vm_userfaultfd_ctx
)) {
967 * OK, it can. Can we now merge in the successor as well?
969 if (next
&& end
== next
->vm_start
&&
970 mpol_equal(policy
, vma_policy(next
)) &&
971 can_vma_merge_before(next
, vm_flags
,
974 vm_userfaultfd_ctx
) &&
975 is_mergeable_anon_vma(prev
->anon_vma
,
976 next
->anon_vma
, NULL
)) {
978 err
= vma_adjust(prev
, prev
->vm_start
,
979 next
->vm_end
, prev
->vm_pgoff
, NULL
);
980 } else /* cases 2, 5, 7 */
981 err
= vma_adjust(prev
, prev
->vm_start
,
982 end
, prev
->vm_pgoff
, NULL
);
985 khugepaged_enter_vma_merge(prev
, vm_flags
);
990 * Can this new request be merged in front of next?
992 if (next
&& end
== next
->vm_start
&&
993 mpol_equal(policy
, vma_policy(next
)) &&
994 can_vma_merge_before(next
, vm_flags
,
995 anon_vma
, file
, pgoff
+pglen
,
996 vm_userfaultfd_ctx
)) {
997 if (prev
&& addr
< prev
->vm_end
) /* case 4 */
998 err
= vma_adjust(prev
, prev
->vm_start
,
999 addr
, prev
->vm_pgoff
, NULL
);
1000 else /* cases 3, 8 */
1001 err
= vma_adjust(area
, addr
, next
->vm_end
,
1002 next
->vm_pgoff
- pglen
, NULL
);
1005 khugepaged_enter_vma_merge(area
, vm_flags
);
1013 * Rough compatbility check to quickly see if it's even worth looking
1014 * at sharing an anon_vma.
1016 * They need to have the same vm_file, and the flags can only differ
1017 * in things that mprotect may change.
1019 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1020 * we can merge the two vma's. For example, we refuse to merge a vma if
1021 * there is a vm_ops->close() function, because that indicates that the
1022 * driver is doing some kind of reference counting. But that doesn't
1023 * really matter for the anon_vma sharing case.
1025 static int anon_vma_compatible(struct vm_area_struct
*a
, struct vm_area_struct
*b
)
1027 return a
->vm_end
== b
->vm_start
&&
1028 mpol_equal(vma_policy(a
), vma_policy(b
)) &&
1029 a
->vm_file
== b
->vm_file
&&
1030 !((a
->vm_flags
^ b
->vm_flags
) & ~(VM_READ
|VM_WRITE
|VM_EXEC
|VM_SOFTDIRTY
)) &&
1031 b
->vm_pgoff
== a
->vm_pgoff
+ ((b
->vm_start
- a
->vm_start
) >> PAGE_SHIFT
);
1035 * Do some basic sanity checking to see if we can re-use the anon_vma
1036 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1037 * the same as 'old', the other will be the new one that is trying
1038 * to share the anon_vma.
1040 * NOTE! This runs with mm_sem held for reading, so it is possible that
1041 * the anon_vma of 'old' is concurrently in the process of being set up
1042 * by another page fault trying to merge _that_. But that's ok: if it
1043 * is being set up, that automatically means that it will be a singleton
1044 * acceptable for merging, so we can do all of this optimistically. But
1045 * we do that READ_ONCE() to make sure that we never re-load the pointer.
1047 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1048 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1049 * is to return an anon_vma that is "complex" due to having gone through
1052 * We also make sure that the two vma's are compatible (adjacent,
1053 * and with the same memory policies). That's all stable, even with just
1054 * a read lock on the mm_sem.
1056 static struct anon_vma
*reusable_anon_vma(struct vm_area_struct
*old
, struct vm_area_struct
*a
, struct vm_area_struct
*b
)
1058 if (anon_vma_compatible(a
, b
)) {
1059 struct anon_vma
*anon_vma
= READ_ONCE(old
->anon_vma
);
1061 if (anon_vma
&& list_is_singular(&old
->anon_vma_chain
))
1068 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1069 * neighbouring vmas for a suitable anon_vma, before it goes off
1070 * to allocate a new anon_vma. It checks because a repetitive
1071 * sequence of mprotects and faults may otherwise lead to distinct
1072 * anon_vmas being allocated, preventing vma merge in subsequent
1075 struct anon_vma
*find_mergeable_anon_vma(struct vm_area_struct
*vma
)
1077 struct anon_vma
*anon_vma
;
1078 struct vm_area_struct
*near
;
1080 near
= vma
->vm_next
;
1084 anon_vma
= reusable_anon_vma(near
, vma
, near
);
1088 near
= vma
->vm_prev
;
1092 anon_vma
= reusable_anon_vma(near
, near
, vma
);
1097 * There's no absolute need to look only at touching neighbours:
1098 * we could search further afield for "compatible" anon_vmas.
1099 * But it would probably just be a waste of time searching,
1100 * or lead to too many vmas hanging off the same anon_vma.
1101 * We're trying to allow mprotect remerging later on,
1102 * not trying to minimize memory used for anon_vmas.
1108 * If a hint addr is less than mmap_min_addr change hint to be as
1109 * low as possible but still greater than mmap_min_addr
1111 static inline unsigned long round_hint_to_min(unsigned long hint
)
1114 if (((void *)hint
!= NULL
) &&
1115 (hint
< mmap_min_addr
))
1116 return PAGE_ALIGN(mmap_min_addr
);
1120 static inline int mlock_future_check(struct mm_struct
*mm
,
1121 unsigned long flags
,
1124 unsigned long locked
, lock_limit
;
1126 /* mlock MCL_FUTURE? */
1127 if (flags
& VM_LOCKED
) {
1128 locked
= len
>> PAGE_SHIFT
;
1129 locked
+= mm
->locked_vm
;
1130 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
1131 lock_limit
>>= PAGE_SHIFT
;
1132 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
1139 * The caller must hold down_write(¤t->mm->mmap_sem).
1141 unsigned long do_mmap(struct file
*file
, unsigned long addr
,
1142 unsigned long len
, unsigned long prot
,
1143 unsigned long flags
, vm_flags_t vm_flags
,
1144 unsigned long pgoff
, unsigned long *populate
)
1146 struct mm_struct
*mm
= current
->mm
;
1155 * Does the application expect PROT_READ to imply PROT_EXEC?
1157 * (the exception is when the underlying filesystem is noexec
1158 * mounted, in which case we dont add PROT_EXEC.)
1160 if ((prot
& PROT_READ
) && (current
->personality
& READ_IMPLIES_EXEC
))
1161 if (!(file
&& path_noexec(&file
->f_path
)))
1164 if (!(flags
& MAP_FIXED
))
1165 addr
= round_hint_to_min(addr
);
1167 /* Careful about overflows.. */
1168 len
= PAGE_ALIGN(len
);
1172 /* offset overflow? */
1173 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
1176 /* Too many mappings? */
1177 if (mm
->map_count
> sysctl_max_map_count
)
1180 /* Obtain the address to map to. we verify (or select) it and ensure
1181 * that it represents a valid section of the address space.
1183 addr
= get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
1184 if (offset_in_page(addr
))
1187 if (prot
== PROT_EXEC
) {
1188 pkey
= execute_only_pkey(mm
);
1193 /* Do simple checking here so the lower-level routines won't have
1194 * to. we assume access permissions have been handled by the open
1195 * of the memory object, so we don't do any here.
1197 vm_flags
|= calc_vm_prot_bits(prot
, pkey
) | calc_vm_flag_bits(flags
) |
1198 mm
->def_flags
| VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1200 if (flags
& MAP_LOCKED
)
1201 if (!can_do_mlock())
1204 if (mlock_future_check(mm
, vm_flags
, len
))
1208 struct inode
*inode
= file_inode(file
);
1210 switch (flags
& MAP_TYPE
) {
1212 if ((prot
&PROT_WRITE
) && !(file
->f_mode
&FMODE_WRITE
))
1216 * Make sure we don't allow writing to an append-only
1219 if (IS_APPEND(inode
) && (file
->f_mode
& FMODE_WRITE
))
1223 * Make sure there are no mandatory locks on the file.
1225 if (locks_verify_locked(file
))
1228 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
1229 if (!(file
->f_mode
& FMODE_WRITE
))
1230 vm_flags
&= ~(VM_MAYWRITE
| VM_SHARED
);
1234 if (!(file
->f_mode
& FMODE_READ
))
1236 if (path_noexec(&file
->f_path
)) {
1237 if (vm_flags
& VM_EXEC
)
1239 vm_flags
&= ~VM_MAYEXEC
;
1242 if (!file
->f_op
->mmap
)
1244 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1252 switch (flags
& MAP_TYPE
) {
1254 if (vm_flags
& (VM_GROWSDOWN
|VM_GROWSUP
))
1260 vm_flags
|= VM_SHARED
| VM_MAYSHARE
;
1264 * Set pgoff according to addr for anon_vma.
1266 pgoff
= addr
>> PAGE_SHIFT
;
1274 * Set 'VM_NORESERVE' if we should not account for the
1275 * memory use of this mapping.
1277 if (flags
& MAP_NORESERVE
) {
1278 /* We honor MAP_NORESERVE if allowed to overcommit */
1279 if (sysctl_overcommit_memory
!= OVERCOMMIT_NEVER
)
1280 vm_flags
|= VM_NORESERVE
;
1282 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1283 if (file
&& is_file_hugepages(file
))
1284 vm_flags
|= VM_NORESERVE
;
1287 addr
= mmap_region(file
, addr
, len
, vm_flags
, pgoff
);
1288 if (!IS_ERR_VALUE(addr
) &&
1289 ((vm_flags
& VM_LOCKED
) ||
1290 (flags
& (MAP_POPULATE
| MAP_NONBLOCK
)) == MAP_POPULATE
))
1295 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1296 unsigned long, prot
, unsigned long, flags
,
1297 unsigned long, fd
, unsigned long, pgoff
)
1299 struct file
*file
= NULL
;
1300 unsigned long retval
;
1302 if (!(flags
& MAP_ANONYMOUS
)) {
1303 audit_mmap_fd(fd
, flags
);
1307 if (is_file_hugepages(file
))
1308 len
= ALIGN(len
, huge_page_size(hstate_file(file
)));
1310 if (unlikely(flags
& MAP_HUGETLB
&& !is_file_hugepages(file
)))
1312 } else if (flags
& MAP_HUGETLB
) {
1313 struct user_struct
*user
= NULL
;
1316 hs
= hstate_sizelog((flags
>> MAP_HUGE_SHIFT
) & SHM_HUGE_MASK
);
1320 len
= ALIGN(len
, huge_page_size(hs
));
1322 * VM_NORESERVE is used because the reservations will be
1323 * taken when vm_ops->mmap() is called
1324 * A dummy user value is used because we are not locking
1325 * memory so no accounting is necessary
1327 file
= hugetlb_file_setup(HUGETLB_ANON_FILE
, len
,
1329 &user
, HUGETLB_ANONHUGE_INODE
,
1330 (flags
>> MAP_HUGE_SHIFT
) & MAP_HUGE_MASK
);
1332 return PTR_ERR(file
);
1335 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1337 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1344 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1345 struct mmap_arg_struct
{
1349 unsigned long flags
;
1351 unsigned long offset
;
1354 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1356 struct mmap_arg_struct a
;
1358 if (copy_from_user(&a
, arg
, sizeof(a
)))
1360 if (offset_in_page(a
.offset
))
1363 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1364 a
.offset
>> PAGE_SHIFT
);
1366 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1369 * Some shared mappigns will want the pages marked read-only
1370 * to track write events. If so, we'll downgrade vm_page_prot
1371 * to the private version (using protection_map[] without the
1374 int vma_wants_writenotify(struct vm_area_struct
*vma
)
1376 vm_flags_t vm_flags
= vma
->vm_flags
;
1377 const struct vm_operations_struct
*vm_ops
= vma
->vm_ops
;
1379 /* If it was private or non-writable, the write bit is already clear */
1380 if ((vm_flags
& (VM_WRITE
|VM_SHARED
)) != ((VM_WRITE
|VM_SHARED
)))
1383 /* The backer wishes to know when pages are first written to? */
1384 if (vm_ops
&& (vm_ops
->page_mkwrite
|| vm_ops
->pfn_mkwrite
))
1387 /* The open routine did something to the protections that pgprot_modify
1388 * won't preserve? */
1389 if (pgprot_val(vma
->vm_page_prot
) !=
1390 pgprot_val(vm_pgprot_modify(vma
->vm_page_prot
, vm_flags
)))
1393 /* Do we need to track softdirty? */
1394 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY
) && !(vm_flags
& VM_SOFTDIRTY
))
1397 /* Specialty mapping? */
1398 if (vm_flags
& VM_PFNMAP
)
1401 /* Can the mapping track the dirty pages? */
1402 return vma
->vm_file
&& vma
->vm_file
->f_mapping
&&
1403 mapping_cap_account_dirty(vma
->vm_file
->f_mapping
);
1407 * We account for memory if it's a private writeable mapping,
1408 * not hugepages and VM_NORESERVE wasn't set.
1410 static inline int accountable_mapping(struct file
*file
, vm_flags_t vm_flags
)
1413 * hugetlb has its own accounting separate from the core VM
1414 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1416 if (file
&& is_file_hugepages(file
))
1419 return (vm_flags
& (VM_NORESERVE
| VM_SHARED
| VM_WRITE
)) == VM_WRITE
;
1422 unsigned long mmap_region(struct file
*file
, unsigned long addr
,
1423 unsigned long len
, vm_flags_t vm_flags
, unsigned long pgoff
)
1425 struct mm_struct
*mm
= current
->mm
;
1426 struct vm_area_struct
*vma
, *prev
;
1428 struct rb_node
**rb_link
, *rb_parent
;
1429 unsigned long charged
= 0;
1431 /* Check against address space limit. */
1432 if (!may_expand_vm(mm
, vm_flags
, len
>> PAGE_SHIFT
)) {
1433 unsigned long nr_pages
;
1436 * MAP_FIXED may remove pages of mappings that intersects with
1437 * requested mapping. Account for the pages it would unmap.
1439 nr_pages
= count_vma_pages_range(mm
, addr
, addr
+ len
);
1441 if (!may_expand_vm(mm
, vm_flags
,
1442 (len
>> PAGE_SHIFT
) - nr_pages
))
1446 /* Clear old maps */
1447 while (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
,
1449 if (do_munmap(mm
, addr
, len
))
1454 * Private writable mapping: check memory availability
1456 if (accountable_mapping(file
, vm_flags
)) {
1457 charged
= len
>> PAGE_SHIFT
;
1458 if (security_vm_enough_memory_mm(mm
, charged
))
1460 vm_flags
|= VM_ACCOUNT
;
1464 * Can we just expand an old mapping?
1466 vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vm_flags
,
1467 NULL
, file
, pgoff
, NULL
, NULL_VM_UFFD_CTX
);
1472 * Determine the object being mapped and call the appropriate
1473 * specific mapper. the address has already been validated, but
1474 * not unmapped, but the maps are removed from the list.
1476 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1483 vma
->vm_start
= addr
;
1484 vma
->vm_end
= addr
+ len
;
1485 vma
->vm_flags
= vm_flags
;
1486 vma
->vm_page_prot
= vm_get_page_prot(vm_flags
);
1487 vma
->vm_pgoff
= pgoff
;
1488 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1491 if (vm_flags
& VM_DENYWRITE
) {
1492 error
= deny_write_access(file
);
1496 if (vm_flags
& VM_SHARED
) {
1497 error
= mapping_map_writable(file
->f_mapping
);
1499 goto allow_write_and_free_vma
;
1502 /* ->mmap() can change vma->vm_file, but must guarantee that
1503 * vma_link() below can deny write-access if VM_DENYWRITE is set
1504 * and map writably if VM_SHARED is set. This usually means the
1505 * new file must not have been exposed to user-space, yet.
1507 vma
->vm_file
= get_file(file
);
1508 error
= file
->f_op
->mmap(file
, vma
);
1510 goto unmap_and_free_vma
;
1512 /* Can addr have changed??
1514 * Answer: Yes, several device drivers can do it in their
1515 * f_op->mmap method. -DaveM
1516 * Bug: If addr is changed, prev, rb_link, rb_parent should
1517 * be updated for vma_link()
1519 WARN_ON_ONCE(addr
!= vma
->vm_start
);
1521 addr
= vma
->vm_start
;
1522 vm_flags
= vma
->vm_flags
;
1523 } else if (vm_flags
& VM_SHARED
) {
1524 error
= shmem_zero_setup(vma
);
1529 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
1530 /* Once vma denies write, undo our temporary denial count */
1532 if (vm_flags
& VM_SHARED
)
1533 mapping_unmap_writable(file
->f_mapping
);
1534 if (vm_flags
& VM_DENYWRITE
)
1535 allow_write_access(file
);
1537 file
= vma
->vm_file
;
1539 perf_event_mmap(vma
);
1541 vm_stat_account(mm
, vm_flags
, len
>> PAGE_SHIFT
);
1542 if (vm_flags
& VM_LOCKED
) {
1543 if (!((vm_flags
& VM_SPECIAL
) || is_vm_hugetlb_page(vma
) ||
1544 vma
== get_gate_vma(current
->mm
)))
1545 mm
->locked_vm
+= (len
>> PAGE_SHIFT
);
1547 vma
->vm_flags
&= VM_LOCKED_CLEAR_MASK
;
1554 * New (or expanded) vma always get soft dirty status.
1555 * Otherwise user-space soft-dirty page tracker won't
1556 * be able to distinguish situation when vma area unmapped,
1557 * then new mapped in-place (which must be aimed as
1558 * a completely new data area).
1560 vma
->vm_flags
|= VM_SOFTDIRTY
;
1562 vma_set_page_prot(vma
);
1567 vma
->vm_file
= NULL
;
1570 /* Undo any partial mapping done by a device driver. */
1571 unmap_region(mm
, vma
, prev
, vma
->vm_start
, vma
->vm_end
);
1573 if (vm_flags
& VM_SHARED
)
1574 mapping_unmap_writable(file
->f_mapping
);
1575 allow_write_and_free_vma
:
1576 if (vm_flags
& VM_DENYWRITE
)
1577 allow_write_access(file
);
1579 kmem_cache_free(vm_area_cachep
, vma
);
1582 vm_unacct_memory(charged
);
1586 unsigned long unmapped_area(struct vm_unmapped_area_info
*info
)
1589 * We implement the search by looking for an rbtree node that
1590 * immediately follows a suitable gap. That is,
1591 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1592 * - gap_end = vma->vm_start >= info->low_limit + length;
1593 * - gap_end - gap_start >= length
1596 struct mm_struct
*mm
= current
->mm
;
1597 struct vm_area_struct
*vma
;
1598 unsigned long length
, low_limit
, high_limit
, gap_start
, gap_end
;
1600 /* Adjust search length to account for worst case alignment overhead */
1601 length
= info
->length
+ info
->align_mask
;
1602 if (length
< info
->length
)
1605 /* Adjust search limits by the desired length */
1606 if (info
->high_limit
< length
)
1608 high_limit
= info
->high_limit
- length
;
1610 if (info
->low_limit
> high_limit
)
1612 low_limit
= info
->low_limit
+ length
;
1614 /* Check if rbtree root looks promising */
1615 if (RB_EMPTY_ROOT(&mm
->mm_rb
))
1617 vma
= rb_entry(mm
->mm_rb
.rb_node
, struct vm_area_struct
, vm_rb
);
1618 if (vma
->rb_subtree_gap
< length
)
1622 /* Visit left subtree if it looks promising */
1623 gap_end
= vma
->vm_start
;
1624 if (gap_end
>= low_limit
&& vma
->vm_rb
.rb_left
) {
1625 struct vm_area_struct
*left
=
1626 rb_entry(vma
->vm_rb
.rb_left
,
1627 struct vm_area_struct
, vm_rb
);
1628 if (left
->rb_subtree_gap
>= length
) {
1634 gap_start
= vma
->vm_prev
? vma
->vm_prev
->vm_end
: 0;
1636 /* Check if current node has a suitable gap */
1637 if (gap_start
> high_limit
)
1639 if (gap_end
>= low_limit
&& gap_end
- gap_start
>= length
)
1642 /* Visit right subtree if it looks promising */
1643 if (vma
->vm_rb
.rb_right
) {
1644 struct vm_area_struct
*right
=
1645 rb_entry(vma
->vm_rb
.rb_right
,
1646 struct vm_area_struct
, vm_rb
);
1647 if (right
->rb_subtree_gap
>= length
) {
1653 /* Go back up the rbtree to find next candidate node */
1655 struct rb_node
*prev
= &vma
->vm_rb
;
1656 if (!rb_parent(prev
))
1658 vma
= rb_entry(rb_parent(prev
),
1659 struct vm_area_struct
, vm_rb
);
1660 if (prev
== vma
->vm_rb
.rb_left
) {
1661 gap_start
= vma
->vm_prev
->vm_end
;
1662 gap_end
= vma
->vm_start
;
1669 /* Check highest gap, which does not precede any rbtree node */
1670 gap_start
= mm
->highest_vm_end
;
1671 gap_end
= ULONG_MAX
; /* Only for VM_BUG_ON below */
1672 if (gap_start
> high_limit
)
1676 /* We found a suitable gap. Clip it with the original low_limit. */
1677 if (gap_start
< info
->low_limit
)
1678 gap_start
= info
->low_limit
;
1680 /* Adjust gap address to the desired alignment */
1681 gap_start
+= (info
->align_offset
- gap_start
) & info
->align_mask
;
1683 VM_BUG_ON(gap_start
+ info
->length
> info
->high_limit
);
1684 VM_BUG_ON(gap_start
+ info
->length
> gap_end
);
1688 unsigned long unmapped_area_topdown(struct vm_unmapped_area_info
*info
)
1690 struct mm_struct
*mm
= current
->mm
;
1691 struct vm_area_struct
*vma
;
1692 unsigned long length
, low_limit
, high_limit
, gap_start
, gap_end
;
1694 /* Adjust search length to account for worst case alignment overhead */
1695 length
= info
->length
+ info
->align_mask
;
1696 if (length
< info
->length
)
1700 * Adjust search limits by the desired length.
1701 * See implementation comment at top of unmapped_area().
1703 gap_end
= info
->high_limit
;
1704 if (gap_end
< length
)
1706 high_limit
= gap_end
- length
;
1708 if (info
->low_limit
> high_limit
)
1710 low_limit
= info
->low_limit
+ length
;
1712 /* Check highest gap, which does not precede any rbtree node */
1713 gap_start
= mm
->highest_vm_end
;
1714 if (gap_start
<= high_limit
)
1717 /* Check if rbtree root looks promising */
1718 if (RB_EMPTY_ROOT(&mm
->mm_rb
))
1720 vma
= rb_entry(mm
->mm_rb
.rb_node
, struct vm_area_struct
, vm_rb
);
1721 if (vma
->rb_subtree_gap
< length
)
1725 /* Visit right subtree if it looks promising */
1726 gap_start
= vma
->vm_prev
? vma
->vm_prev
->vm_end
: 0;
1727 if (gap_start
<= high_limit
&& vma
->vm_rb
.rb_right
) {
1728 struct vm_area_struct
*right
=
1729 rb_entry(vma
->vm_rb
.rb_right
,
1730 struct vm_area_struct
, vm_rb
);
1731 if (right
->rb_subtree_gap
>= length
) {
1738 /* Check if current node has a suitable gap */
1739 gap_end
= vma
->vm_start
;
1740 if (gap_end
< low_limit
)
1742 if (gap_start
<= high_limit
&& gap_end
- gap_start
>= length
)
1745 /* Visit left subtree if it looks promising */
1746 if (vma
->vm_rb
.rb_left
) {
1747 struct vm_area_struct
*left
=
1748 rb_entry(vma
->vm_rb
.rb_left
,
1749 struct vm_area_struct
, vm_rb
);
1750 if (left
->rb_subtree_gap
>= length
) {
1756 /* Go back up the rbtree to find next candidate node */
1758 struct rb_node
*prev
= &vma
->vm_rb
;
1759 if (!rb_parent(prev
))
1761 vma
= rb_entry(rb_parent(prev
),
1762 struct vm_area_struct
, vm_rb
);
1763 if (prev
== vma
->vm_rb
.rb_right
) {
1764 gap_start
= vma
->vm_prev
?
1765 vma
->vm_prev
->vm_end
: 0;
1772 /* We found a suitable gap. Clip it with the original high_limit. */
1773 if (gap_end
> info
->high_limit
)
1774 gap_end
= info
->high_limit
;
1777 /* Compute highest gap address at the desired alignment */
1778 gap_end
-= info
->length
;
1779 gap_end
-= (gap_end
- info
->align_offset
) & info
->align_mask
;
1781 VM_BUG_ON(gap_end
< info
->low_limit
);
1782 VM_BUG_ON(gap_end
< gap_start
);
1786 /* Get an address range which is currently unmapped.
1787 * For shmat() with addr=0.
1789 * Ugly calling convention alert:
1790 * Return value with the low bits set means error value,
1792 * if (ret & ~PAGE_MASK)
1795 * This function "knows" that -ENOMEM has the bits set.
1797 #ifndef HAVE_ARCH_UNMAPPED_AREA
1799 arch_get_unmapped_area(struct file
*filp
, unsigned long addr
,
1800 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1802 struct mm_struct
*mm
= current
->mm
;
1803 struct vm_area_struct
*vma
;
1804 struct vm_unmapped_area_info info
;
1806 if (len
> TASK_SIZE
- mmap_min_addr
)
1809 if (flags
& MAP_FIXED
)
1813 addr
= PAGE_ALIGN(addr
);
1814 vma
= find_vma(mm
, addr
);
1815 if (TASK_SIZE
- len
>= addr
&& addr
>= mmap_min_addr
&&
1816 (!vma
|| addr
+ len
<= vma
->vm_start
))
1822 info
.low_limit
= mm
->mmap_base
;
1823 info
.high_limit
= TASK_SIZE
;
1824 info
.align_mask
= 0;
1825 return vm_unmapped_area(&info
);
1830 * This mmap-allocator allocates new areas top-down from below the
1831 * stack's low limit (the base):
1833 #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1835 arch_get_unmapped_area_topdown(struct file
*filp
, const unsigned long addr0
,
1836 const unsigned long len
, const unsigned long pgoff
,
1837 const unsigned long flags
)
1839 struct vm_area_struct
*vma
;
1840 struct mm_struct
*mm
= current
->mm
;
1841 unsigned long addr
= addr0
;
1842 struct vm_unmapped_area_info info
;
1844 /* requested length too big for entire address space */
1845 if (len
> TASK_SIZE
- mmap_min_addr
)
1848 if (flags
& MAP_FIXED
)
1851 /* requesting a specific address */
1853 addr
= PAGE_ALIGN(addr
);
1854 vma
= find_vma(mm
, addr
);
1855 if (TASK_SIZE
- len
>= addr
&& addr
>= mmap_min_addr
&&
1856 (!vma
|| addr
+ len
<= vma
->vm_start
))
1860 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
1862 info
.low_limit
= max(PAGE_SIZE
, mmap_min_addr
);
1863 info
.high_limit
= mm
->mmap_base
;
1864 info
.align_mask
= 0;
1865 addr
= vm_unmapped_area(&info
);
1868 * A failed mmap() very likely causes application failure,
1869 * so fall back to the bottom-up function here. This scenario
1870 * can happen with large stack limits and large mmap()
1873 if (offset_in_page(addr
)) {
1874 VM_BUG_ON(addr
!= -ENOMEM
);
1876 info
.low_limit
= TASK_UNMAPPED_BASE
;
1877 info
.high_limit
= TASK_SIZE
;
1878 addr
= vm_unmapped_area(&info
);
1886 get_unmapped_area(struct file
*file
, unsigned long addr
, unsigned long len
,
1887 unsigned long pgoff
, unsigned long flags
)
1889 unsigned long (*get_area
)(struct file
*, unsigned long,
1890 unsigned long, unsigned long, unsigned long);
1892 unsigned long error
= arch_mmap_check(addr
, len
, flags
);
1896 /* Careful about overflows.. */
1897 if (len
> TASK_SIZE
)
1900 get_area
= current
->mm
->get_unmapped_area
;
1902 if (file
->f_op
->get_unmapped_area
)
1903 get_area
= file
->f_op
->get_unmapped_area
;
1904 } else if (flags
& MAP_SHARED
) {
1906 * mmap_region() will call shmem_zero_setup() to create a file,
1907 * so use shmem's get_unmapped_area in case it can be huge.
1908 * do_mmap_pgoff() will clear pgoff, so match alignment.
1911 get_area
= shmem_get_unmapped_area
;
1914 addr
= get_area(file
, addr
, len
, pgoff
, flags
);
1915 if (IS_ERR_VALUE(addr
))
1918 if (addr
> TASK_SIZE
- len
)
1920 if (offset_in_page(addr
))
1923 error
= security_mmap_addr(addr
);
1924 return error
? error
: addr
;
1927 EXPORT_SYMBOL(get_unmapped_area
);
1929 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
1930 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
1932 struct rb_node
*rb_node
;
1933 struct vm_area_struct
*vma
;
1935 /* Check the cache first. */
1936 vma
= vmacache_find(mm
, addr
);
1940 rb_node
= mm
->mm_rb
.rb_node
;
1943 struct vm_area_struct
*tmp
;
1945 tmp
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
1947 if (tmp
->vm_end
> addr
) {
1949 if (tmp
->vm_start
<= addr
)
1951 rb_node
= rb_node
->rb_left
;
1953 rb_node
= rb_node
->rb_right
;
1957 vmacache_update(addr
, vma
);
1961 EXPORT_SYMBOL(find_vma
);
1964 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
1966 struct vm_area_struct
*
1967 find_vma_prev(struct mm_struct
*mm
, unsigned long addr
,
1968 struct vm_area_struct
**pprev
)
1970 struct vm_area_struct
*vma
;
1972 vma
= find_vma(mm
, addr
);
1974 *pprev
= vma
->vm_prev
;
1976 struct rb_node
*rb_node
= mm
->mm_rb
.rb_node
;
1979 *pprev
= rb_entry(rb_node
, struct vm_area_struct
, vm_rb
);
1980 rb_node
= rb_node
->rb_right
;
1987 * Verify that the stack growth is acceptable and
1988 * update accounting. This is shared with both the
1989 * grow-up and grow-down cases.
1991 static int acct_stack_growth(struct vm_area_struct
*vma
, unsigned long size
, unsigned long grow
)
1993 struct mm_struct
*mm
= vma
->vm_mm
;
1994 struct rlimit
*rlim
= current
->signal
->rlim
;
1995 unsigned long new_start
, actual_size
;
1997 /* address space limit tests */
1998 if (!may_expand_vm(mm
, vma
->vm_flags
, grow
))
2001 /* Stack limit test */
2003 if (size
&& (vma
->vm_flags
& (VM_GROWSUP
| VM_GROWSDOWN
)))
2004 actual_size
-= PAGE_SIZE
;
2005 if (actual_size
> READ_ONCE(rlim
[RLIMIT_STACK
].rlim_cur
))
2008 /* mlock limit tests */
2009 if (vma
->vm_flags
& VM_LOCKED
) {
2010 unsigned long locked
;
2011 unsigned long limit
;
2012 locked
= mm
->locked_vm
+ grow
;
2013 limit
= READ_ONCE(rlim
[RLIMIT_MEMLOCK
].rlim_cur
);
2014 limit
>>= PAGE_SHIFT
;
2015 if (locked
> limit
&& !capable(CAP_IPC_LOCK
))
2019 /* Check to ensure the stack will not grow into a hugetlb-only region */
2020 new_start
= (vma
->vm_flags
& VM_GROWSUP
) ? vma
->vm_start
:
2022 if (is_hugepage_only_range(vma
->vm_mm
, new_start
, size
))
2026 * Overcommit.. This must be the final test, as it will
2027 * update security statistics.
2029 if (security_vm_enough_memory_mm(mm
, grow
))
2035 #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
2037 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2038 * vma is the last one with address > vma->vm_end. Have to extend vma.
2040 int expand_upwards(struct vm_area_struct
*vma
, unsigned long address
)
2042 struct mm_struct
*mm
= vma
->vm_mm
;
2045 if (!(vma
->vm_flags
& VM_GROWSUP
))
2048 /* Guard against wrapping around to address 0. */
2049 if (address
< PAGE_ALIGN(address
+4))
2050 address
= PAGE_ALIGN(address
+4);
2054 /* We must make sure the anon_vma is allocated. */
2055 if (unlikely(anon_vma_prepare(vma
)))
2059 * vma->vm_start/vm_end cannot change under us because the caller
2060 * is required to hold the mmap_sem in read mode. We need the
2061 * anon_vma lock to serialize against concurrent expand_stacks.
2063 anon_vma_lock_write(vma
->anon_vma
);
2065 /* Somebody else might have raced and expanded it already */
2066 if (address
> vma
->vm_end
) {
2067 unsigned long size
, grow
;
2069 size
= address
- vma
->vm_start
;
2070 grow
= (address
- vma
->vm_end
) >> PAGE_SHIFT
;
2073 if (vma
->vm_pgoff
+ (size
>> PAGE_SHIFT
) >= vma
->vm_pgoff
) {
2074 error
= acct_stack_growth(vma
, size
, grow
);
2077 * vma_gap_update() doesn't support concurrent
2078 * updates, but we only hold a shared mmap_sem
2079 * lock here, so we need to protect against
2080 * concurrent vma expansions.
2081 * anon_vma_lock_write() doesn't help here, as
2082 * we don't guarantee that all growable vmas
2083 * in a mm share the same root anon vma.
2084 * So, we reuse mm->page_table_lock to guard
2085 * against concurrent vma expansions.
2087 spin_lock(&mm
->page_table_lock
);
2088 if (vma
->vm_flags
& VM_LOCKED
)
2089 mm
->locked_vm
+= grow
;
2090 vm_stat_account(mm
, vma
->vm_flags
, grow
);
2091 anon_vma_interval_tree_pre_update_vma(vma
);
2092 vma
->vm_end
= address
;
2093 anon_vma_interval_tree_post_update_vma(vma
);
2095 vma_gap_update(vma
->vm_next
);
2097 mm
->highest_vm_end
= address
;
2098 spin_unlock(&mm
->page_table_lock
);
2100 perf_event_mmap(vma
);
2104 anon_vma_unlock_write(vma
->anon_vma
);
2105 khugepaged_enter_vma_merge(vma
, vma
->vm_flags
);
2109 #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2112 * vma is the first one with address < vma->vm_start. Have to extend vma.
2114 int expand_downwards(struct vm_area_struct
*vma
,
2115 unsigned long address
)
2117 struct mm_struct
*mm
= vma
->vm_mm
;
2120 address
&= PAGE_MASK
;
2121 error
= security_mmap_addr(address
);
2125 /* We must make sure the anon_vma is allocated. */
2126 if (unlikely(anon_vma_prepare(vma
)))
2130 * vma->vm_start/vm_end cannot change under us because the caller
2131 * is required to hold the mmap_sem in read mode. We need the
2132 * anon_vma lock to serialize against concurrent expand_stacks.
2134 anon_vma_lock_write(vma
->anon_vma
);
2136 /* Somebody else might have raced and expanded it already */
2137 if (address
< vma
->vm_start
) {
2138 unsigned long size
, grow
;
2140 size
= vma
->vm_end
- address
;
2141 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
2144 if (grow
<= vma
->vm_pgoff
) {
2145 error
= acct_stack_growth(vma
, size
, grow
);
2148 * vma_gap_update() doesn't support concurrent
2149 * updates, but we only hold a shared mmap_sem
2150 * lock here, so we need to protect against
2151 * concurrent vma expansions.
2152 * anon_vma_lock_write() doesn't help here, as
2153 * we don't guarantee that all growable vmas
2154 * in a mm share the same root anon vma.
2155 * So, we reuse mm->page_table_lock to guard
2156 * against concurrent vma expansions.
2158 spin_lock(&mm
->page_table_lock
);
2159 if (vma
->vm_flags
& VM_LOCKED
)
2160 mm
->locked_vm
+= grow
;
2161 vm_stat_account(mm
, vma
->vm_flags
, grow
);
2162 anon_vma_interval_tree_pre_update_vma(vma
);
2163 vma
->vm_start
= address
;
2164 vma
->vm_pgoff
-= grow
;
2165 anon_vma_interval_tree_post_update_vma(vma
);
2166 vma_gap_update(vma
);
2167 spin_unlock(&mm
->page_table_lock
);
2169 perf_event_mmap(vma
);
2173 anon_vma_unlock_write(vma
->anon_vma
);
2174 khugepaged_enter_vma_merge(vma
, vma
->vm_flags
);
2180 * Note how expand_stack() refuses to expand the stack all the way to
2181 * abut the next virtual mapping, *unless* that mapping itself is also
2182 * a stack mapping. We want to leave room for a guard page, after all
2183 * (the guard page itself is not added here, that is done by the
2184 * actual page faulting logic)
2186 * This matches the behavior of the guard page logic (see mm/memory.c:
2187 * check_stack_guard_page()), which only allows the guard page to be
2188 * removed under these circumstances.
2190 #ifdef CONFIG_STACK_GROWSUP
2191 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
2193 struct vm_area_struct
*next
;
2195 address
&= PAGE_MASK
;
2196 next
= vma
->vm_next
;
2197 if (next
&& next
->vm_start
== address
+ PAGE_SIZE
) {
2198 if (!(next
->vm_flags
& VM_GROWSUP
))
2201 return expand_upwards(vma
, address
);
2204 struct vm_area_struct
*
2205 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
2207 struct vm_area_struct
*vma
, *prev
;
2210 vma
= find_vma_prev(mm
, addr
, &prev
);
2211 if (vma
&& (vma
->vm_start
<= addr
))
2213 if (!prev
|| expand_stack(prev
, addr
))
2215 if (prev
->vm_flags
& VM_LOCKED
)
2216 populate_vma_page_range(prev
, addr
, prev
->vm_end
, NULL
);
2220 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
2222 struct vm_area_struct
*prev
;
2224 address
&= PAGE_MASK
;
2225 prev
= vma
->vm_prev
;
2226 if (prev
&& prev
->vm_end
== address
) {
2227 if (!(prev
->vm_flags
& VM_GROWSDOWN
))
2230 return expand_downwards(vma
, address
);
2233 struct vm_area_struct
*
2234 find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
2236 struct vm_area_struct
*vma
;
2237 unsigned long start
;
2240 vma
= find_vma(mm
, addr
);
2243 if (vma
->vm_start
<= addr
)
2245 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
2247 start
= vma
->vm_start
;
2248 if (expand_stack(vma
, addr
))
2250 if (vma
->vm_flags
& VM_LOCKED
)
2251 populate_vma_page_range(vma
, addr
, start
, NULL
);
2256 EXPORT_SYMBOL_GPL(find_extend_vma
);
2259 * Ok - we have the memory areas we should free on the vma list,
2260 * so release them, and do the vma updates.
2262 * Called with the mm semaphore held.
2264 static void remove_vma_list(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
2266 unsigned long nr_accounted
= 0;
2268 /* Update high watermark before we lower total_vm */
2269 update_hiwater_vm(mm
);
2271 long nrpages
= vma_pages(vma
);
2273 if (vma
->vm_flags
& VM_ACCOUNT
)
2274 nr_accounted
+= nrpages
;
2275 vm_stat_account(mm
, vma
->vm_flags
, -nrpages
);
2276 vma
= remove_vma(vma
);
2278 vm_unacct_memory(nr_accounted
);
2283 * Get rid of page table information in the indicated region.
2285 * Called with the mm semaphore held.
2287 static void unmap_region(struct mm_struct
*mm
,
2288 struct vm_area_struct
*vma
, struct vm_area_struct
*prev
,
2289 unsigned long start
, unsigned long end
)
2291 struct vm_area_struct
*next
= prev
? prev
->vm_next
: mm
->mmap
;
2292 struct mmu_gather tlb
;
2295 tlb_gather_mmu(&tlb
, mm
, start
, end
);
2296 update_hiwater_rss(mm
);
2297 unmap_vmas(&tlb
, vma
, start
, end
);
2298 free_pgtables(&tlb
, vma
, prev
? prev
->vm_end
: FIRST_USER_ADDRESS
,
2299 next
? next
->vm_start
: USER_PGTABLES_CEILING
);
2300 tlb_finish_mmu(&tlb
, start
, end
);
2304 * Create a list of vma's touched by the unmap, removing them from the mm's
2305 * vma list as we go..
2308 detach_vmas_to_be_unmapped(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2309 struct vm_area_struct
*prev
, unsigned long end
)
2311 struct vm_area_struct
**insertion_point
;
2312 struct vm_area_struct
*tail_vma
= NULL
;
2314 insertion_point
= (prev
? &prev
->vm_next
: &mm
->mmap
);
2315 vma
->vm_prev
= NULL
;
2317 vma_rb_erase(vma
, &mm
->mm_rb
);
2321 } while (vma
&& vma
->vm_start
< end
);
2322 *insertion_point
= vma
;
2324 vma
->vm_prev
= prev
;
2325 vma_gap_update(vma
);
2327 mm
->highest_vm_end
= prev
? prev
->vm_end
: 0;
2328 tail_vma
->vm_next
= NULL
;
2330 /* Kill the cache */
2331 vmacache_invalidate(mm
);
2335 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2336 * munmap path where it doesn't make sense to fail.
2338 static int __split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2339 unsigned long addr
, int new_below
)
2341 struct vm_area_struct
*new;
2344 if (is_vm_hugetlb_page(vma
) && (addr
&
2345 ~(huge_page_mask(hstate_vma(vma
)))))
2348 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
2352 /* most fields are the same, copy all, and then fixup */
2355 INIT_LIST_HEAD(&new->anon_vma_chain
);
2360 new->vm_start
= addr
;
2361 new->vm_pgoff
+= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
);
2364 err
= vma_dup_policy(vma
, new);
2368 err
= anon_vma_clone(new, vma
);
2373 get_file(new->vm_file
);
2375 if (new->vm_ops
&& new->vm_ops
->open
)
2376 new->vm_ops
->open(new);
2379 err
= vma_adjust(vma
, addr
, vma
->vm_end
, vma
->vm_pgoff
+
2380 ((addr
- new->vm_start
) >> PAGE_SHIFT
), new);
2382 err
= vma_adjust(vma
, vma
->vm_start
, addr
, vma
->vm_pgoff
, new);
2388 /* Clean everything up if vma_adjust failed. */
2389 if (new->vm_ops
&& new->vm_ops
->close
)
2390 new->vm_ops
->close(new);
2393 unlink_anon_vmas(new);
2395 mpol_put(vma_policy(new));
2397 kmem_cache_free(vm_area_cachep
, new);
2402 * Split a vma into two pieces at address 'addr', a new vma is allocated
2403 * either for the first part or the tail.
2405 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2406 unsigned long addr
, int new_below
)
2408 if (mm
->map_count
>= sysctl_max_map_count
)
2411 return __split_vma(mm
, vma
, addr
, new_below
);
2414 /* Munmap is split into 2 main parts -- this part which finds
2415 * what needs doing, and the areas themselves, which do the
2416 * work. This now handles partial unmappings.
2417 * Jeremy Fitzhardinge <jeremy@goop.org>
2419 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
2422 struct vm_area_struct
*vma
, *prev
, *last
;
2424 if ((offset_in_page(start
)) || start
> TASK_SIZE
|| len
> TASK_SIZE
-start
)
2427 len
= PAGE_ALIGN(len
);
2431 /* Find the first overlapping VMA */
2432 vma
= find_vma(mm
, start
);
2435 prev
= vma
->vm_prev
;
2436 /* we have start < vma->vm_end */
2438 /* if it doesn't overlap, we have nothing.. */
2440 if (vma
->vm_start
>= end
)
2444 * If we need to split any vma, do it now to save pain later.
2446 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2447 * unmapped vm_area_struct will remain in use: so lower split_vma
2448 * places tmp vma above, and higher split_vma places tmp vma below.
2450 if (start
> vma
->vm_start
) {
2454 * Make sure that map_count on return from munmap() will
2455 * not exceed its limit; but let map_count go just above
2456 * its limit temporarily, to help free resources as expected.
2458 if (end
< vma
->vm_end
&& mm
->map_count
>= sysctl_max_map_count
)
2461 error
= __split_vma(mm
, vma
, start
, 0);
2467 /* Does it split the last one? */
2468 last
= find_vma(mm
, end
);
2469 if (last
&& end
> last
->vm_start
) {
2470 int error
= __split_vma(mm
, last
, end
, 1);
2474 vma
= prev
? prev
->vm_next
: mm
->mmap
;
2477 * unlock any mlock()ed ranges before detaching vmas
2479 if (mm
->locked_vm
) {
2480 struct vm_area_struct
*tmp
= vma
;
2481 while (tmp
&& tmp
->vm_start
< end
) {
2482 if (tmp
->vm_flags
& VM_LOCKED
) {
2483 mm
->locked_vm
-= vma_pages(tmp
);
2484 munlock_vma_pages_all(tmp
);
2491 * Remove the vma's, and unmap the actual pages
2493 detach_vmas_to_be_unmapped(mm
, vma
, prev
, end
);
2494 unmap_region(mm
, vma
, prev
, start
, end
);
2496 arch_unmap(mm
, vma
, start
, end
);
2498 /* Fix up all other VM information */
2499 remove_vma_list(mm
, vma
);
2504 int vm_munmap(unsigned long start
, size_t len
)
2507 struct mm_struct
*mm
= current
->mm
;
2509 if (down_write_killable(&mm
->mmap_sem
))
2512 ret
= do_munmap(mm
, start
, len
);
2513 up_write(&mm
->mmap_sem
);
2516 EXPORT_SYMBOL(vm_munmap
);
2518 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
2521 struct mm_struct
*mm
= current
->mm
;
2523 profile_munmap(addr
);
2524 if (down_write_killable(&mm
->mmap_sem
))
2526 ret
= do_munmap(mm
, addr
, len
);
2527 up_write(&mm
->mmap_sem
);
2533 * Emulation of deprecated remap_file_pages() syscall.
2535 SYSCALL_DEFINE5(remap_file_pages
, unsigned long, start
, unsigned long, size
,
2536 unsigned long, prot
, unsigned long, pgoff
, unsigned long, flags
)
2539 struct mm_struct
*mm
= current
->mm
;
2540 struct vm_area_struct
*vma
;
2541 unsigned long populate
= 0;
2542 unsigned long ret
= -EINVAL
;
2545 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
2546 current
->comm
, current
->pid
);
2550 start
= start
& PAGE_MASK
;
2551 size
= size
& PAGE_MASK
;
2553 if (start
+ size
<= start
)
2556 /* Does pgoff wrap? */
2557 if (pgoff
+ (size
>> PAGE_SHIFT
) < pgoff
)
2560 if (down_write_killable(&mm
->mmap_sem
))
2563 vma
= find_vma(mm
, start
);
2565 if (!vma
|| !(vma
->vm_flags
& VM_SHARED
))
2568 if (start
< vma
->vm_start
)
2571 if (start
+ size
> vma
->vm_end
) {
2572 struct vm_area_struct
*next
;
2574 for (next
= vma
->vm_next
; next
; next
= next
->vm_next
) {
2575 /* hole between vmas ? */
2576 if (next
->vm_start
!= next
->vm_prev
->vm_end
)
2579 if (next
->vm_file
!= vma
->vm_file
)
2582 if (next
->vm_flags
!= vma
->vm_flags
)
2585 if (start
+ size
<= next
->vm_end
)
2593 prot
|= vma
->vm_flags
& VM_READ
? PROT_READ
: 0;
2594 prot
|= vma
->vm_flags
& VM_WRITE
? PROT_WRITE
: 0;
2595 prot
|= vma
->vm_flags
& VM_EXEC
? PROT_EXEC
: 0;
2597 flags
&= MAP_NONBLOCK
;
2598 flags
|= MAP_SHARED
| MAP_FIXED
| MAP_POPULATE
;
2599 if (vma
->vm_flags
& VM_LOCKED
) {
2600 struct vm_area_struct
*tmp
;
2601 flags
|= MAP_LOCKED
;
2603 /* drop PG_Mlocked flag for over-mapped range */
2604 for (tmp
= vma
; tmp
->vm_start
>= start
+ size
;
2605 tmp
= tmp
->vm_next
) {
2607 * Split pmd and munlock page on the border
2610 vma_adjust_trans_huge(tmp
, start
, start
+ size
, 0);
2612 munlock_vma_pages_range(tmp
,
2613 max(tmp
->vm_start
, start
),
2614 min(tmp
->vm_end
, start
+ size
));
2618 file
= get_file(vma
->vm_file
);
2619 ret
= do_mmap_pgoff(vma
->vm_file
, start
, size
,
2620 prot
, flags
, pgoff
, &populate
);
2623 up_write(&mm
->mmap_sem
);
2625 mm_populate(ret
, populate
);
2626 if (!IS_ERR_VALUE(ret
))
2631 static inline void verify_mm_writelocked(struct mm_struct
*mm
)
2633 #ifdef CONFIG_DEBUG_VM
2634 if (unlikely(down_read_trylock(&mm
->mmap_sem
))) {
2636 up_read(&mm
->mmap_sem
);
2642 * this is really a simplified "do_mmap". it only handles
2643 * anonymous maps. eventually we may be able to do some
2644 * brk-specific accounting here.
2646 static int do_brk(unsigned long addr
, unsigned long len
)
2648 struct mm_struct
*mm
= current
->mm
;
2649 struct vm_area_struct
*vma
, *prev
;
2650 unsigned long flags
;
2651 struct rb_node
**rb_link
, *rb_parent
;
2652 pgoff_t pgoff
= addr
>> PAGE_SHIFT
;
2655 len
= PAGE_ALIGN(len
);
2659 flags
= VM_DATA_DEFAULT_FLAGS
| VM_ACCOUNT
| mm
->def_flags
;
2661 error
= get_unmapped_area(NULL
, addr
, len
, 0, MAP_FIXED
);
2662 if (offset_in_page(error
))
2665 error
= mlock_future_check(mm
, mm
->def_flags
, len
);
2670 * mm->mmap_sem is required to protect against another thread
2671 * changing the mappings in case we sleep.
2673 verify_mm_writelocked(mm
);
2676 * Clear old maps. this also does some error checking for us
2678 while (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
,
2680 if (do_munmap(mm
, addr
, len
))
2684 /* Check against address space limits *after* clearing old maps... */
2685 if (!may_expand_vm(mm
, flags
, len
>> PAGE_SHIFT
))
2688 if (mm
->map_count
> sysctl_max_map_count
)
2691 if (security_vm_enough_memory_mm(mm
, len
>> PAGE_SHIFT
))
2694 /* Can we just expand an old private anonymous mapping? */
2695 vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, flags
,
2696 NULL
, NULL
, pgoff
, NULL
, NULL_VM_UFFD_CTX
);
2701 * create a vma struct for an anonymous mapping
2703 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
2705 vm_unacct_memory(len
>> PAGE_SHIFT
);
2709 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
2711 vma
->vm_start
= addr
;
2712 vma
->vm_end
= addr
+ len
;
2713 vma
->vm_pgoff
= pgoff
;
2714 vma
->vm_flags
= flags
;
2715 vma
->vm_page_prot
= vm_get_page_prot(flags
);
2716 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2718 perf_event_mmap(vma
);
2719 mm
->total_vm
+= len
>> PAGE_SHIFT
;
2720 mm
->data_vm
+= len
>> PAGE_SHIFT
;
2721 if (flags
& VM_LOCKED
)
2722 mm
->locked_vm
+= (len
>> PAGE_SHIFT
);
2723 vma
->vm_flags
|= VM_SOFTDIRTY
;
2727 int vm_brk(unsigned long addr
, unsigned long len
)
2729 struct mm_struct
*mm
= current
->mm
;
2733 if (down_write_killable(&mm
->mmap_sem
))
2736 ret
= do_brk(addr
, len
);
2737 populate
= ((mm
->def_flags
& VM_LOCKED
) != 0);
2738 up_write(&mm
->mmap_sem
);
2739 if (populate
&& !ret
)
2740 mm_populate(addr
, len
);
2743 EXPORT_SYMBOL(vm_brk
);
2745 /* Release all mmaps. */
2746 void exit_mmap(struct mm_struct
*mm
)
2748 struct mmu_gather tlb
;
2749 struct vm_area_struct
*vma
;
2750 unsigned long nr_accounted
= 0;
2752 /* mm's last user has gone, and its about to be pulled down */
2753 mmu_notifier_release(mm
);
2755 if (mm
->locked_vm
) {
2758 if (vma
->vm_flags
& VM_LOCKED
)
2759 munlock_vma_pages_all(vma
);
2767 if (!vma
) /* Can happen if dup_mmap() received an OOM */
2772 tlb_gather_mmu(&tlb
, mm
, 0, -1);
2773 /* update_hiwater_rss(mm) here? but nobody should be looking */
2774 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2775 unmap_vmas(&tlb
, vma
, 0, -1);
2777 free_pgtables(&tlb
, vma
, FIRST_USER_ADDRESS
, USER_PGTABLES_CEILING
);
2778 tlb_finish_mmu(&tlb
, 0, -1);
2781 * Walk the list again, actually closing and freeing it,
2782 * with preemption enabled, without holding any MM locks.
2785 if (vma
->vm_flags
& VM_ACCOUNT
)
2786 nr_accounted
+= vma_pages(vma
);
2787 vma
= remove_vma(vma
);
2789 vm_unacct_memory(nr_accounted
);
2792 /* Insert vm structure into process list sorted by address
2793 * and into the inode's i_mmap tree. If vm_file is non-NULL
2794 * then i_mmap_rwsem is taken here.
2796 int insert_vm_struct(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
2798 struct vm_area_struct
*prev
;
2799 struct rb_node
**rb_link
, *rb_parent
;
2801 if (find_vma_links(mm
, vma
->vm_start
, vma
->vm_end
,
2802 &prev
, &rb_link
, &rb_parent
))
2804 if ((vma
->vm_flags
& VM_ACCOUNT
) &&
2805 security_vm_enough_memory_mm(mm
, vma_pages(vma
)))
2809 * The vm_pgoff of a purely anonymous vma should be irrelevant
2810 * until its first write fault, when page's anon_vma and index
2811 * are set. But now set the vm_pgoff it will almost certainly
2812 * end up with (unless mremap moves it elsewhere before that
2813 * first wfault), so /proc/pid/maps tells a consistent story.
2815 * By setting it to reflect the virtual start address of the
2816 * vma, merges and splits can happen in a seamless way, just
2817 * using the existing file pgoff checks and manipulations.
2818 * Similarly in do_mmap_pgoff and in do_brk.
2820 if (vma_is_anonymous(vma
)) {
2821 BUG_ON(vma
->anon_vma
);
2822 vma
->vm_pgoff
= vma
->vm_start
>> PAGE_SHIFT
;
2825 vma_link(mm
, vma
, prev
, rb_link
, rb_parent
);
2830 * Copy the vma structure to a new location in the same mm,
2831 * prior to moving page table entries, to effect an mremap move.
2833 struct vm_area_struct
*copy_vma(struct vm_area_struct
**vmap
,
2834 unsigned long addr
, unsigned long len
, pgoff_t pgoff
,
2835 bool *need_rmap_locks
)
2837 struct vm_area_struct
*vma
= *vmap
;
2838 unsigned long vma_start
= vma
->vm_start
;
2839 struct mm_struct
*mm
= vma
->vm_mm
;
2840 struct vm_area_struct
*new_vma
, *prev
;
2841 struct rb_node
**rb_link
, *rb_parent
;
2842 bool faulted_in_anon_vma
= true;
2845 * If anonymous vma has not yet been faulted, update new pgoff
2846 * to match new location, to increase its chance of merging.
2848 if (unlikely(vma_is_anonymous(vma
) && !vma
->anon_vma
)) {
2849 pgoff
= addr
>> PAGE_SHIFT
;
2850 faulted_in_anon_vma
= false;
2853 if (find_vma_links(mm
, addr
, addr
+ len
, &prev
, &rb_link
, &rb_parent
))
2854 return NULL
; /* should never get here */
2855 new_vma
= vma_merge(mm
, prev
, addr
, addr
+ len
, vma
->vm_flags
,
2856 vma
->anon_vma
, vma
->vm_file
, pgoff
, vma_policy(vma
),
2857 vma
->vm_userfaultfd_ctx
);
2860 * Source vma may have been merged into new_vma
2862 if (unlikely(vma_start
>= new_vma
->vm_start
&&
2863 vma_start
< new_vma
->vm_end
)) {
2865 * The only way we can get a vma_merge with
2866 * self during an mremap is if the vma hasn't
2867 * been faulted in yet and we were allowed to
2868 * reset the dst vma->vm_pgoff to the
2869 * destination address of the mremap to allow
2870 * the merge to happen. mremap must change the
2871 * vm_pgoff linearity between src and dst vmas
2872 * (in turn preventing a vma_merge) to be
2873 * safe. It is only safe to keep the vm_pgoff
2874 * linear if there are no pages mapped yet.
2876 VM_BUG_ON_VMA(faulted_in_anon_vma
, new_vma
);
2877 *vmap
= vma
= new_vma
;
2879 *need_rmap_locks
= (new_vma
->vm_pgoff
<= vma
->vm_pgoff
);
2881 new_vma
= kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
2885 new_vma
->vm_start
= addr
;
2886 new_vma
->vm_end
= addr
+ len
;
2887 new_vma
->vm_pgoff
= pgoff
;
2888 if (vma_dup_policy(vma
, new_vma
))
2890 INIT_LIST_HEAD(&new_vma
->anon_vma_chain
);
2891 if (anon_vma_clone(new_vma
, vma
))
2892 goto out_free_mempol
;
2893 if (new_vma
->vm_file
)
2894 get_file(new_vma
->vm_file
);
2895 if (new_vma
->vm_ops
&& new_vma
->vm_ops
->open
)
2896 new_vma
->vm_ops
->open(new_vma
);
2897 vma_link(mm
, new_vma
, prev
, rb_link
, rb_parent
);
2898 *need_rmap_locks
= false;
2903 mpol_put(vma_policy(new_vma
));
2905 kmem_cache_free(vm_area_cachep
, new_vma
);
2911 * Return true if the calling process may expand its vm space by the passed
2914 bool may_expand_vm(struct mm_struct
*mm
, vm_flags_t flags
, unsigned long npages
)
2916 if (mm
->total_vm
+ npages
> rlimit(RLIMIT_AS
) >> PAGE_SHIFT
)
2919 if (is_data_mapping(flags
) &&
2920 mm
->data_vm
+ npages
> rlimit(RLIMIT_DATA
) >> PAGE_SHIFT
) {
2921 /* Workaround for Valgrind */
2922 if (rlimit(RLIMIT_DATA
) == 0 &&
2923 mm
->data_vm
+ npages
<= rlimit_max(RLIMIT_DATA
) >> PAGE_SHIFT
)
2925 if (!ignore_rlimit_data
) {
2926 pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits or use boot option ignore_rlimit_data.\n",
2927 current
->comm
, current
->pid
,
2928 (mm
->data_vm
+ npages
) << PAGE_SHIFT
,
2929 rlimit(RLIMIT_DATA
));
2937 void vm_stat_account(struct mm_struct
*mm
, vm_flags_t flags
, long npages
)
2939 mm
->total_vm
+= npages
;
2941 if (is_exec_mapping(flags
))
2942 mm
->exec_vm
+= npages
;
2943 else if (is_stack_mapping(flags
))
2944 mm
->stack_vm
+= npages
;
2945 else if (is_data_mapping(flags
))
2946 mm
->data_vm
+= npages
;
2949 static int special_mapping_fault(struct vm_area_struct
*vma
,
2950 struct vm_fault
*vmf
);
2953 * Having a close hook prevents vma merging regardless of flags.
2955 static void special_mapping_close(struct vm_area_struct
*vma
)
2959 static const char *special_mapping_name(struct vm_area_struct
*vma
)
2961 return ((struct vm_special_mapping
*)vma
->vm_private_data
)->name
;
2964 static int special_mapping_mremap(struct vm_area_struct
*new_vma
)
2966 struct vm_special_mapping
*sm
= new_vma
->vm_private_data
;
2969 return sm
->mremap(sm
, new_vma
);
2973 static const struct vm_operations_struct special_mapping_vmops
= {
2974 .close
= special_mapping_close
,
2975 .fault
= special_mapping_fault
,
2976 .mremap
= special_mapping_mremap
,
2977 .name
= special_mapping_name
,
2980 static const struct vm_operations_struct legacy_special_mapping_vmops
= {
2981 .close
= special_mapping_close
,
2982 .fault
= special_mapping_fault
,
2985 static int special_mapping_fault(struct vm_area_struct
*vma
,
2986 struct vm_fault
*vmf
)
2989 struct page
**pages
;
2991 if (vma
->vm_ops
== &legacy_special_mapping_vmops
) {
2992 pages
= vma
->vm_private_data
;
2994 struct vm_special_mapping
*sm
= vma
->vm_private_data
;
2997 return sm
->fault(sm
, vma
, vmf
);
3002 for (pgoff
= vmf
->pgoff
; pgoff
&& *pages
; ++pages
)
3006 struct page
*page
= *pages
;
3012 return VM_FAULT_SIGBUS
;
3015 static struct vm_area_struct
*__install_special_mapping(
3016 struct mm_struct
*mm
,
3017 unsigned long addr
, unsigned long len
,
3018 unsigned long vm_flags
, void *priv
,
3019 const struct vm_operations_struct
*ops
)
3022 struct vm_area_struct
*vma
;
3024 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
3025 if (unlikely(vma
== NULL
))
3026 return ERR_PTR(-ENOMEM
);
3028 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
3030 vma
->vm_start
= addr
;
3031 vma
->vm_end
= addr
+ len
;
3033 vma
->vm_flags
= vm_flags
| mm
->def_flags
| VM_DONTEXPAND
| VM_SOFTDIRTY
;
3034 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
3037 vma
->vm_private_data
= priv
;
3039 ret
= insert_vm_struct(mm
, vma
);
3043 vm_stat_account(mm
, vma
->vm_flags
, len
>> PAGE_SHIFT
);
3045 perf_event_mmap(vma
);
3050 kmem_cache_free(vm_area_cachep
, vma
);
3051 return ERR_PTR(ret
);
3055 * Called with mm->mmap_sem held for writing.
3056 * Insert a new vma covering the given region, with the given flags.
3057 * Its pages are supplied by the given array of struct page *.
3058 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3059 * The region past the last page supplied will always produce SIGBUS.
3060 * The array pointer and the pages it points to are assumed to stay alive
3061 * for as long as this mapping might exist.
3063 struct vm_area_struct
*_install_special_mapping(
3064 struct mm_struct
*mm
,
3065 unsigned long addr
, unsigned long len
,
3066 unsigned long vm_flags
, const struct vm_special_mapping
*spec
)
3068 return __install_special_mapping(mm
, addr
, len
, vm_flags
, (void *)spec
,
3069 &special_mapping_vmops
);
3072 int install_special_mapping(struct mm_struct
*mm
,
3073 unsigned long addr
, unsigned long len
,
3074 unsigned long vm_flags
, struct page
**pages
)
3076 struct vm_area_struct
*vma
= __install_special_mapping(
3077 mm
, addr
, len
, vm_flags
, (void *)pages
,
3078 &legacy_special_mapping_vmops
);
3080 return PTR_ERR_OR_ZERO(vma
);
3083 static DEFINE_MUTEX(mm_all_locks_mutex
);
3085 static void vm_lock_anon_vma(struct mm_struct
*mm
, struct anon_vma
*anon_vma
)
3087 if (!test_bit(0, (unsigned long *) &anon_vma
->root
->rb_root
.rb_node
)) {
3089 * The LSB of head.next can't change from under us
3090 * because we hold the mm_all_locks_mutex.
3092 down_write_nest_lock(&anon_vma
->root
->rwsem
, &mm
->mmap_sem
);
3094 * We can safely modify head.next after taking the
3095 * anon_vma->root->rwsem. If some other vma in this mm shares
3096 * the same anon_vma we won't take it again.
3098 * No need of atomic instructions here, head.next
3099 * can't change from under us thanks to the
3100 * anon_vma->root->rwsem.
3102 if (__test_and_set_bit(0, (unsigned long *)
3103 &anon_vma
->root
->rb_root
.rb_node
))
3108 static void vm_lock_mapping(struct mm_struct
*mm
, struct address_space
*mapping
)
3110 if (!test_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
)) {
3112 * AS_MM_ALL_LOCKS can't change from under us because
3113 * we hold the mm_all_locks_mutex.
3115 * Operations on ->flags have to be atomic because
3116 * even if AS_MM_ALL_LOCKS is stable thanks to the
3117 * mm_all_locks_mutex, there may be other cpus
3118 * changing other bitflags in parallel to us.
3120 if (test_and_set_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
))
3122 down_write_nest_lock(&mapping
->i_mmap_rwsem
, &mm
->mmap_sem
);
3127 * This operation locks against the VM for all pte/vma/mm related
3128 * operations that could ever happen on a certain mm. This includes
3129 * vmtruncate, try_to_unmap, and all page faults.
3131 * The caller must take the mmap_sem in write mode before calling
3132 * mm_take_all_locks(). The caller isn't allowed to release the
3133 * mmap_sem until mm_drop_all_locks() returns.
3135 * mmap_sem in write mode is required in order to block all operations
3136 * that could modify pagetables and free pages without need of
3137 * altering the vma layout. It's also needed in write mode to avoid new
3138 * anon_vmas to be associated with existing vmas.
3140 * A single task can't take more than one mm_take_all_locks() in a row
3141 * or it would deadlock.
3143 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
3144 * mapping->flags avoid to take the same lock twice, if more than one
3145 * vma in this mm is backed by the same anon_vma or address_space.
3147 * We take locks in following order, accordingly to comment at beginning
3149 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3151 * - all i_mmap_rwsem locks;
3152 * - all anon_vma->rwseml
3154 * We can take all locks within these types randomly because the VM code
3155 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3156 * mm_all_locks_mutex.
3158 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3159 * that may have to take thousand of locks.
3161 * mm_take_all_locks() can fail if it's interrupted by signals.
3163 int mm_take_all_locks(struct mm_struct
*mm
)
3165 struct vm_area_struct
*vma
;
3166 struct anon_vma_chain
*avc
;
3168 BUG_ON(down_read_trylock(&mm
->mmap_sem
));
3170 mutex_lock(&mm_all_locks_mutex
);
3172 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3173 if (signal_pending(current
))
3175 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
&&
3176 is_vm_hugetlb_page(vma
))
3177 vm_lock_mapping(mm
, vma
->vm_file
->f_mapping
);
3180 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3181 if (signal_pending(current
))
3183 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
&&
3184 !is_vm_hugetlb_page(vma
))
3185 vm_lock_mapping(mm
, vma
->vm_file
->f_mapping
);
3188 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3189 if (signal_pending(current
))
3192 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
3193 vm_lock_anon_vma(mm
, avc
->anon_vma
);
3199 mm_drop_all_locks(mm
);
3203 static void vm_unlock_anon_vma(struct anon_vma
*anon_vma
)
3205 if (test_bit(0, (unsigned long *) &anon_vma
->root
->rb_root
.rb_node
)) {
3207 * The LSB of head.next can't change to 0 from under
3208 * us because we hold the mm_all_locks_mutex.
3210 * We must however clear the bitflag before unlocking
3211 * the vma so the users using the anon_vma->rb_root will
3212 * never see our bitflag.
3214 * No need of atomic instructions here, head.next
3215 * can't change from under us until we release the
3216 * anon_vma->root->rwsem.
3218 if (!__test_and_clear_bit(0, (unsigned long *)
3219 &anon_vma
->root
->rb_root
.rb_node
))
3221 anon_vma_unlock_write(anon_vma
);
3225 static void vm_unlock_mapping(struct address_space
*mapping
)
3227 if (test_bit(AS_MM_ALL_LOCKS
, &mapping
->flags
)) {
3229 * AS_MM_ALL_LOCKS can't change to 0 from under us
3230 * because we hold the mm_all_locks_mutex.
3232 i_mmap_unlock_write(mapping
);
3233 if (!test_and_clear_bit(AS_MM_ALL_LOCKS
,
3240 * The mmap_sem cannot be released by the caller until
3241 * mm_drop_all_locks() returns.
3243 void mm_drop_all_locks(struct mm_struct
*mm
)
3245 struct vm_area_struct
*vma
;
3246 struct anon_vma_chain
*avc
;
3248 BUG_ON(down_read_trylock(&mm
->mmap_sem
));
3249 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex
));
3251 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
3253 list_for_each_entry(avc
, &vma
->anon_vma_chain
, same_vma
)
3254 vm_unlock_anon_vma(avc
->anon_vma
);
3255 if (vma
->vm_file
&& vma
->vm_file
->f_mapping
)
3256 vm_unlock_mapping(vma
->vm_file
->f_mapping
);
3259 mutex_unlock(&mm_all_locks_mutex
);
3263 * initialise the VMA slab
3265 void __init
mmap_init(void)
3269 ret
= percpu_counter_init(&vm_committed_as
, 0, GFP_KERNEL
);
3274 * Initialise sysctl_user_reserve_kbytes.
3276 * This is intended to prevent a user from starting a single memory hogging
3277 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3280 * The default value is min(3% of free memory, 128MB)
3281 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3283 static int init_user_reserve(void)
3285 unsigned long free_kbytes
;
3287 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
3289 sysctl_user_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 17);
3292 subsys_initcall(init_user_reserve
);
3295 * Initialise sysctl_admin_reserve_kbytes.
3297 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3298 * to log in and kill a memory hogging process.
3300 * Systems with more than 256MB will reserve 8MB, enough to recover
3301 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3302 * only reserve 3% of free pages by default.
3304 static int init_admin_reserve(void)
3306 unsigned long free_kbytes
;
3308 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
3310 sysctl_admin_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 13);
3313 subsys_initcall(init_admin_reserve
);
3316 * Reinititalise user and admin reserves if memory is added or removed.
3318 * The default user reserve max is 128MB, and the default max for the
3319 * admin reserve is 8MB. These are usually, but not always, enough to
3320 * enable recovery from a memory hogging process using login/sshd, a shell,
3321 * and tools like top. It may make sense to increase or even disable the
3322 * reserve depending on the existence of swap or variations in the recovery
3323 * tools. So, the admin may have changed them.
3325 * If memory is added and the reserves have been eliminated or increased above
3326 * the default max, then we'll trust the admin.
3328 * If memory is removed and there isn't enough free memory, then we
3329 * need to reset the reserves.
3331 * Otherwise keep the reserve set by the admin.
3333 static int reserve_mem_notifier(struct notifier_block
*nb
,
3334 unsigned long action
, void *data
)
3336 unsigned long tmp
, free_kbytes
;
3340 /* Default max is 128MB. Leave alone if modified by operator. */
3341 tmp
= sysctl_user_reserve_kbytes
;
3342 if (0 < tmp
&& tmp
< (1UL << 17))
3343 init_user_reserve();
3345 /* Default max is 8MB. Leave alone if modified by operator. */
3346 tmp
= sysctl_admin_reserve_kbytes
;
3347 if (0 < tmp
&& tmp
< (1UL << 13))
3348 init_admin_reserve();
3352 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
3354 if (sysctl_user_reserve_kbytes
> free_kbytes
) {
3355 init_user_reserve();
3356 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3357 sysctl_user_reserve_kbytes
);
3360 if (sysctl_admin_reserve_kbytes
> free_kbytes
) {
3361 init_admin_reserve();
3362 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3363 sysctl_admin_reserve_kbytes
);
3372 static struct notifier_block reserve_mem_nb
= {
3373 .notifier_call
= reserve_mem_notifier
,
3376 static int __meminit
init_reserve_notifier(void)
3378 if (register_hotmemory_notifier(&reserve_mem_nb
))
3379 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
3383 subsys_initcall(init_reserve_notifier
);