4 * (C) Copyright 1996 Linus Torvalds
6 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
7 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
11 #include <linux/hugetlb.h>
12 #include <linux/shm.h>
13 #include <linux/ksm.h>
14 #include <linux/mman.h>
15 #include <linux/swap.h>
16 #include <linux/capability.h>
18 #include <linux/swapops.h>
19 #include <linux/highmem.h>
20 #include <linux/security.h>
21 #include <linux/syscalls.h>
22 #include <linux/mmu_notifier.h>
23 #include <linux/sched/sysctl.h>
24 #include <linux/uaccess.h>
25 #include <linux/mm-arch-hooks.h>
27 #include <asm/cacheflush.h>
28 #include <asm/tlbflush.h>
32 static pmd_t
*get_old_pmd(struct mm_struct
*mm
, unsigned long addr
)
38 pgd
= pgd_offset(mm
, addr
);
39 if (pgd_none_or_clear_bad(pgd
))
42 pud
= pud_offset(pgd
, addr
);
43 if (pud_none_or_clear_bad(pud
))
46 pmd
= pmd_offset(pud
, addr
);
53 static pmd_t
*alloc_new_pmd(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
60 pgd
= pgd_offset(mm
, addr
);
61 pud
= pud_alloc(mm
, pgd
, addr
);
65 pmd
= pmd_alloc(mm
, pud
, addr
);
69 VM_BUG_ON(pmd_trans_huge(*pmd
));
74 static pte_t
move_soft_dirty_pte(pte_t pte
)
77 * Set soft dirty bit so we can notice
78 * in userspace the ptes were moved.
80 #ifdef CONFIG_MEM_SOFT_DIRTY
82 pte
= pte_mksoft_dirty(pte
);
83 else if (is_swap_pte(pte
))
84 pte
= pte_swp_mksoft_dirty(pte
);
89 static void move_ptes(struct vm_area_struct
*vma
, pmd_t
*old_pmd
,
90 unsigned long old_addr
, unsigned long old_end
,
91 struct vm_area_struct
*new_vma
, pmd_t
*new_pmd
,
92 unsigned long new_addr
, bool need_rmap_locks
)
94 struct address_space
*mapping
= NULL
;
95 struct anon_vma
*anon_vma
= NULL
;
96 struct mm_struct
*mm
= vma
->vm_mm
;
97 pte_t
*old_pte
, *new_pte
, pte
;
98 spinlock_t
*old_ptl
, *new_ptl
;
99 bool force_flush
= false;
100 unsigned long len
= old_end
- old_addr
;
103 * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
104 * locks to ensure that rmap will always observe either the old or the
105 * new ptes. This is the easiest way to avoid races with
106 * truncate_pagecache(), page migration, etc...
108 * When need_rmap_locks is false, we use other ways to avoid
111 * - During exec() shift_arg_pages(), we use a specially tagged vma
112 * which rmap call sites look for using is_vma_temporary_stack().
114 * - During mremap(), new_vma is often known to be placed after vma
115 * in rmap traversal order. This ensures rmap will always observe
116 * either the old pte, or the new pte, or both (the page table locks
117 * serialize access to individual ptes, but only rmap traversal
118 * order guarantees that we won't miss both the old and new ptes).
120 if (need_rmap_locks
) {
122 mapping
= vma
->vm_file
->f_mapping
;
123 i_mmap_lock_write(mapping
);
126 anon_vma
= vma
->anon_vma
;
127 anon_vma_lock_write(anon_vma
);
132 * We don't have to worry about the ordering of src and dst
133 * pte locks because exclusive mmap_sem prevents deadlock.
135 old_pte
= pte_offset_map_lock(mm
, old_pmd
, old_addr
, &old_ptl
);
136 new_pte
= pte_offset_map(new_pmd
, new_addr
);
137 new_ptl
= pte_lockptr(mm
, new_pmd
);
138 if (new_ptl
!= old_ptl
)
139 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
140 flush_tlb_batched_pending(vma
->vm_mm
);
141 arch_enter_lazy_mmu_mode();
143 for (; old_addr
< old_end
; old_pte
++, old_addr
+= PAGE_SIZE
,
144 new_pte
++, new_addr
+= PAGE_SIZE
) {
145 if (pte_none(*old_pte
))
147 pte
= ptep_get_and_clear(mm
, old_addr
, old_pte
);
149 * If we are remapping a valid PTE, make sure
150 * to flush TLB before we drop the PTL for the PTE.
152 * NOTE! Both old and new PTL matter: the old one
153 * for racing with page_mkclean(), the new one to
154 * make sure the physical page stays valid until
155 * the TLB entry for the old mapping has been
158 if (pte_present(pte
))
160 pte
= move_pte(pte
, new_vma
->vm_page_prot
, old_addr
, new_addr
);
161 pte
= move_soft_dirty_pte(pte
);
162 set_pte_at(mm
, new_addr
, new_pte
, pte
);
165 arch_leave_lazy_mmu_mode();
167 flush_tlb_range(vma
, old_end
- len
, old_end
);
168 if (new_ptl
!= old_ptl
)
169 spin_unlock(new_ptl
);
170 pte_unmap(new_pte
- 1);
171 pte_unmap_unlock(old_pte
- 1, old_ptl
);
173 anon_vma_unlock_write(anon_vma
);
175 i_mmap_unlock_write(mapping
);
178 #define LATENCY_LIMIT (64 * PAGE_SIZE)
180 unsigned long move_page_tables(struct vm_area_struct
*vma
,
181 unsigned long old_addr
, struct vm_area_struct
*new_vma
,
182 unsigned long new_addr
, unsigned long len
,
183 bool need_rmap_locks
)
185 unsigned long extent
, next
, old_end
;
186 pmd_t
*old_pmd
, *new_pmd
;
187 unsigned long mmun_start
; /* For mmu_notifiers */
188 unsigned long mmun_end
; /* For mmu_notifiers */
190 old_end
= old_addr
+ len
;
191 flush_cache_range(vma
, old_addr
, old_end
);
193 mmun_start
= old_addr
;
195 mmu_notifier_invalidate_range_start(vma
->vm_mm
, mmun_start
, mmun_end
);
197 for (; old_addr
< old_end
; old_addr
+= extent
, new_addr
+= extent
) {
199 next
= (old_addr
+ PMD_SIZE
) & PMD_MASK
;
200 /* even if next overflowed, extent below will be ok */
201 extent
= next
- old_addr
;
202 if (extent
> old_end
- old_addr
)
203 extent
= old_end
- old_addr
;
204 old_pmd
= get_old_pmd(vma
->vm_mm
, old_addr
);
207 new_pmd
= alloc_new_pmd(vma
->vm_mm
, vma
, new_addr
);
210 if (pmd_trans_huge(*old_pmd
)) {
212 if (extent
== HPAGE_PMD_SIZE
) {
213 VM_BUG_ON_VMA(vma
->vm_file
|| !vma
->anon_vma
,
215 /* See comment in move_ptes() */
217 anon_vma_lock_write(vma
->anon_vma
);
218 err
= move_huge_pmd(vma
, new_vma
, old_addr
,
222 anon_vma_unlock_write(vma
->anon_vma
);
227 split_huge_page_pmd(vma
, old_addr
, old_pmd
);
229 VM_BUG_ON(pmd_trans_huge(*old_pmd
));
231 if (pmd_none(*new_pmd
) && __pte_alloc(new_vma
->vm_mm
, new_vma
,
234 next
= (new_addr
+ PMD_SIZE
) & PMD_MASK
;
235 if (extent
> next
- new_addr
)
236 extent
= next
- new_addr
;
237 if (extent
> LATENCY_LIMIT
)
238 extent
= LATENCY_LIMIT
;
239 move_ptes(vma
, old_pmd
, old_addr
, old_addr
+ extent
,
240 new_vma
, new_pmd
, new_addr
, need_rmap_locks
);
243 mmu_notifier_invalidate_range_end(vma
->vm_mm
, mmun_start
, mmun_end
);
245 return len
+ old_addr
- old_end
; /* how much done */
248 static unsigned long move_vma(struct vm_area_struct
*vma
,
249 unsigned long old_addr
, unsigned long old_len
,
250 unsigned long new_len
, unsigned long new_addr
, bool *locked
)
252 struct mm_struct
*mm
= vma
->vm_mm
;
253 struct vm_area_struct
*new_vma
;
254 unsigned long vm_flags
= vma
->vm_flags
;
255 unsigned long new_pgoff
;
256 unsigned long moved_len
;
257 unsigned long excess
= 0;
258 unsigned long hiwater_vm
;
261 bool need_rmap_locks
;
264 * We'd prefer to avoid failure later on in do_munmap:
265 * which may split one vma into three before unmapping.
267 if (mm
->map_count
>= sysctl_max_map_count
- 3)
271 * Advise KSM to break any KSM pages in the area to be moved:
272 * it would be confusing if they were to turn up at the new
273 * location, where they happen to coincide with different KSM
274 * pages recently unmapped. But leave vma->vm_flags as it was,
275 * so KSM can come around to merge on vma and new_vma afterwards.
277 err
= ksm_madvise(vma
, old_addr
, old_addr
+ old_len
,
278 MADV_UNMERGEABLE
, &vm_flags
);
282 new_pgoff
= vma
->vm_pgoff
+ ((old_addr
- vma
->vm_start
) >> PAGE_SHIFT
);
283 new_vma
= copy_vma(&vma
, new_addr
, new_len
, new_pgoff
,
288 moved_len
= move_page_tables(vma
, old_addr
, new_vma
, new_addr
, old_len
,
290 if (moved_len
< old_len
) {
292 } else if (vma
->vm_ops
&& vma
->vm_ops
->mremap
) {
293 err
= vma
->vm_ops
->mremap(new_vma
);
298 * On error, move entries back from new area to old,
299 * which will succeed since page tables still there,
300 * and then proceed to unmap new area instead of old.
302 move_page_tables(new_vma
, new_addr
, vma
, old_addr
, moved_len
,
309 arch_remap(mm
, old_addr
, old_addr
+ old_len
,
310 new_addr
, new_addr
+ new_len
);
313 /* Conceal VM_ACCOUNT so old reservation is not undone */
314 if (vm_flags
& VM_ACCOUNT
) {
315 vma
->vm_flags
&= ~VM_ACCOUNT
;
316 excess
= vma
->vm_end
- vma
->vm_start
- old_len
;
317 if (old_addr
> vma
->vm_start
&&
318 old_addr
+ old_len
< vma
->vm_end
)
323 * If we failed to move page tables we still do total_vm increment
324 * since do_munmap() will decrement it by old_len == new_len.
326 * Since total_vm is about to be raised artificially high for a
327 * moment, we need to restore high watermark afterwards: if stats
328 * are taken meanwhile, total_vm and hiwater_vm appear too high.
329 * If this were a serious issue, we'd add a flag to do_munmap().
331 hiwater_vm
= mm
->hiwater_vm
;
332 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, new_len
>>PAGE_SHIFT
);
334 if (do_munmap(mm
, old_addr
, old_len
) < 0) {
335 /* OOM: unable to split vma, just get accounts right */
336 vm_unacct_memory(excess
>> PAGE_SHIFT
);
339 mm
->hiwater_vm
= hiwater_vm
;
341 /* Restore VM_ACCOUNT if one or two pieces of vma left */
343 vma
->vm_flags
|= VM_ACCOUNT
;
345 vma
->vm_next
->vm_flags
|= VM_ACCOUNT
;
348 if (vm_flags
& VM_LOCKED
) {
349 mm
->locked_vm
+= new_len
>> PAGE_SHIFT
;
356 static struct vm_area_struct
*vma_to_resize(unsigned long addr
,
357 unsigned long old_len
, unsigned long new_len
, unsigned long *p
)
359 struct mm_struct
*mm
= current
->mm
;
360 struct vm_area_struct
*vma
= find_vma(mm
, addr
);
363 if (!vma
|| vma
->vm_start
> addr
)
364 return ERR_PTR(-EFAULT
);
366 if (is_vm_hugetlb_page(vma
))
367 return ERR_PTR(-EINVAL
);
369 /* We can't remap across vm area boundaries */
370 if (old_len
> vma
->vm_end
- addr
)
371 return ERR_PTR(-EFAULT
);
373 if (new_len
== old_len
)
376 /* Need to be careful about a growing mapping */
377 pgoff
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
378 pgoff
+= vma
->vm_pgoff
;
379 if (pgoff
+ (new_len
>> PAGE_SHIFT
) < pgoff
)
380 return ERR_PTR(-EINVAL
);
382 if (vma
->vm_flags
& (VM_DONTEXPAND
| VM_PFNMAP
))
383 return ERR_PTR(-EFAULT
);
385 if (vma
->vm_flags
& VM_LOCKED
) {
386 unsigned long locked
, lock_limit
;
387 locked
= mm
->locked_vm
<< PAGE_SHIFT
;
388 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
389 locked
+= new_len
- old_len
;
390 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
391 return ERR_PTR(-EAGAIN
);
394 if (!may_expand_vm(mm
, (new_len
- old_len
) >> PAGE_SHIFT
))
395 return ERR_PTR(-ENOMEM
);
397 if (vma
->vm_flags
& VM_ACCOUNT
) {
398 unsigned long charged
= (new_len
- old_len
) >> PAGE_SHIFT
;
399 if (security_vm_enough_memory_mm(mm
, charged
))
400 return ERR_PTR(-ENOMEM
);
407 static unsigned long mremap_to(unsigned long addr
, unsigned long old_len
,
408 unsigned long new_addr
, unsigned long new_len
, bool *locked
)
410 struct mm_struct
*mm
= current
->mm
;
411 struct vm_area_struct
*vma
;
412 unsigned long ret
= -EINVAL
;
413 unsigned long charged
= 0;
414 unsigned long map_flags
;
416 if (offset_in_page(new_addr
))
419 if (new_len
> TASK_SIZE
|| new_addr
> TASK_SIZE
- new_len
)
422 /* Ensure the old/new locations do not overlap */
423 if (addr
+ old_len
> new_addr
&& new_addr
+ new_len
> addr
)
426 ret
= do_munmap(mm
, new_addr
, new_len
);
430 if (old_len
>= new_len
) {
431 ret
= do_munmap(mm
, addr
+new_len
, old_len
- new_len
);
432 if (ret
&& old_len
!= new_len
)
437 vma
= vma_to_resize(addr
, old_len
, new_len
, &charged
);
443 map_flags
= MAP_FIXED
;
444 if (vma
->vm_flags
& VM_MAYSHARE
)
445 map_flags
|= MAP_SHARED
;
447 ret
= get_unmapped_area(vma
->vm_file
, new_addr
, new_len
, vma
->vm_pgoff
+
448 ((addr
- vma
->vm_start
) >> PAGE_SHIFT
),
450 if (offset_in_page(ret
))
453 ret
= move_vma(vma
, addr
, old_len
, new_len
, new_addr
, locked
);
454 if (!(offset_in_page(ret
)))
457 vm_unacct_memory(charged
);
463 static int vma_expandable(struct vm_area_struct
*vma
, unsigned long delta
)
465 unsigned long end
= vma
->vm_end
+ delta
;
466 if (end
< vma
->vm_end
) /* overflow */
468 if (vma
->vm_next
&& vma
->vm_next
->vm_start
< end
) /* intersection */
470 if (get_unmapped_area(NULL
, vma
->vm_start
, end
- vma
->vm_start
,
471 0, MAP_FIXED
) & ~PAGE_MASK
)
477 * Expand (or shrink) an existing mapping, potentially moving it at the
478 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
480 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
481 * This option implies MREMAP_MAYMOVE.
483 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
484 unsigned long, new_len
, unsigned long, flags
,
485 unsigned long, new_addr
)
487 struct mm_struct
*mm
= current
->mm
;
488 struct vm_area_struct
*vma
;
489 unsigned long ret
= -EINVAL
;
490 unsigned long charged
= 0;
493 if (flags
& ~(MREMAP_FIXED
| MREMAP_MAYMOVE
))
496 if (flags
& MREMAP_FIXED
&& !(flags
& MREMAP_MAYMOVE
))
499 if (offset_in_page(addr
))
502 old_len
= PAGE_ALIGN(old_len
);
503 new_len
= PAGE_ALIGN(new_len
);
506 * We allow a zero old-len as a special case
507 * for DOS-emu "duplicate shm area" thing. But
508 * a zero new-len is nonsensical.
513 down_write(¤t
->mm
->mmap_sem
);
515 if (flags
& MREMAP_FIXED
) {
516 ret
= mremap_to(addr
, old_len
, new_addr
, new_len
,
522 * Always allow a shrinking remap: that just unmaps
523 * the unnecessary pages..
524 * do_munmap does all the needed commit accounting
526 if (old_len
>= new_len
) {
527 ret
= do_munmap(mm
, addr
+new_len
, old_len
- new_len
);
528 if (ret
&& old_len
!= new_len
)
535 * Ok, we need to grow..
537 vma
= vma_to_resize(addr
, old_len
, new_len
, &charged
);
543 /* old_len exactly to the end of the area..
545 if (old_len
== vma
->vm_end
- addr
) {
546 /* can we just expand the current mapping? */
547 if (vma_expandable(vma
, new_len
- old_len
)) {
548 int pages
= (new_len
- old_len
) >> PAGE_SHIFT
;
550 if (vma_adjust(vma
, vma
->vm_start
, addr
+ new_len
,
551 vma
->vm_pgoff
, NULL
)) {
556 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, pages
);
557 if (vma
->vm_flags
& VM_LOCKED
) {
558 mm
->locked_vm
+= pages
;
568 * We weren't able to just expand or shrink the area,
569 * we need to create a new one and move it..
572 if (flags
& MREMAP_MAYMOVE
) {
573 unsigned long map_flags
= 0;
574 if (vma
->vm_flags
& VM_MAYSHARE
)
575 map_flags
|= MAP_SHARED
;
577 new_addr
= get_unmapped_area(vma
->vm_file
, 0, new_len
,
579 ((addr
- vma
->vm_start
) >> PAGE_SHIFT
),
581 if (offset_in_page(new_addr
)) {
586 ret
= move_vma(vma
, addr
, old_len
, new_len
, new_addr
, &locked
);
589 if (offset_in_page(ret
)) {
590 vm_unacct_memory(charged
);
593 up_write(¤t
->mm
->mmap_sem
);
594 if (locked
&& new_len
> old_len
)
595 mm_populate(new_addr
+ old_len
, new_len
- old_len
);