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/uaccess.h>
24 #include <linux/mm-arch-hooks.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
31 static pmd_t
*get_old_pmd(struct mm_struct
*mm
, unsigned long addr
)
37 pgd
= pgd_offset(mm
, addr
);
38 if (pgd_none_or_clear_bad(pgd
))
41 pud
= pud_offset(pgd
, addr
);
42 if (pud_none_or_clear_bad(pud
))
45 pmd
= pmd_offset(pud
, addr
);
52 static pmd_t
*alloc_new_pmd(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
59 pgd
= pgd_offset(mm
, addr
);
60 pud
= pud_alloc(mm
, pgd
, addr
);
64 pmd
= pmd_alloc(mm
, pud
, addr
);
68 VM_BUG_ON(pmd_trans_huge(*pmd
));
73 static void take_rmap_locks(struct vm_area_struct
*vma
)
76 i_mmap_lock_write(vma
->vm_file
->f_mapping
);
78 anon_vma_lock_write(vma
->anon_vma
);
81 static void drop_rmap_locks(struct vm_area_struct
*vma
)
84 anon_vma_unlock_write(vma
->anon_vma
);
86 i_mmap_unlock_write(vma
->vm_file
->f_mapping
);
89 static pte_t
move_soft_dirty_pte(pte_t pte
)
92 * Set soft dirty bit so we can notice
93 * in userspace the ptes were moved.
95 #ifdef CONFIG_MEM_SOFT_DIRTY
97 pte
= pte_mksoft_dirty(pte
);
98 else if (is_swap_pte(pte
))
99 pte
= pte_swp_mksoft_dirty(pte
);
104 static void move_ptes(struct vm_area_struct
*vma
, pmd_t
*old_pmd
,
105 unsigned long old_addr
, unsigned long old_end
,
106 struct vm_area_struct
*new_vma
, pmd_t
*new_pmd
,
107 unsigned long new_addr
, bool need_rmap_locks
)
109 struct mm_struct
*mm
= vma
->vm_mm
;
110 pte_t
*old_pte
, *new_pte
, pte
;
111 spinlock_t
*old_ptl
, *new_ptl
;
112 bool force_flush
= false;
113 unsigned long len
= old_end
- old_addr
;
116 * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
117 * locks to ensure that rmap will always observe either the old or the
118 * new ptes. This is the easiest way to avoid races with
119 * truncate_pagecache(), page migration, etc...
121 * When need_rmap_locks is false, we use other ways to avoid
124 * - During exec() shift_arg_pages(), we use a specially tagged vma
125 * which rmap call sites look for using is_vma_temporary_stack().
127 * - During mremap(), new_vma is often known to be placed after vma
128 * in rmap traversal order. This ensures rmap will always observe
129 * either the old pte, or the new pte, or both (the page table locks
130 * serialize access to individual ptes, but only rmap traversal
131 * order guarantees that we won't miss both the old and new ptes).
134 take_rmap_locks(vma
);
137 * We don't have to worry about the ordering of src and dst
138 * pte locks because exclusive mmap_sem prevents deadlock.
140 old_pte
= pte_offset_map_lock(mm
, old_pmd
, old_addr
, &old_ptl
);
141 new_pte
= pte_offset_map(new_pmd
, new_addr
);
142 new_ptl
= pte_lockptr(mm
, new_pmd
);
143 if (new_ptl
!= old_ptl
)
144 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
145 flush_tlb_batched_pending(vma
->vm_mm
);
146 arch_enter_lazy_mmu_mode();
148 for (; old_addr
< old_end
; old_pte
++, old_addr
+= PAGE_SIZE
,
149 new_pte
++, new_addr
+= PAGE_SIZE
) {
150 if (pte_none(*old_pte
))
153 pte
= ptep_get_and_clear(mm
, old_addr
, old_pte
);
155 * If we are remapping a valid PTE, make sure
156 * to flush TLB before we drop the PTL for the
159 * NOTE! Both old and new PTL matter: the old one
160 * for racing with page_mkclean(), the new one to
161 * make sure the physical page stays valid until
162 * the TLB entry for the old mapping has been
165 if (pte_present(pte
))
167 pte
= move_pte(pte
, new_vma
->vm_page_prot
, old_addr
, new_addr
);
168 pte
= move_soft_dirty_pte(pte
);
169 set_pte_at(mm
, new_addr
, new_pte
, pte
);
172 arch_leave_lazy_mmu_mode();
174 flush_tlb_range(vma
, old_end
- len
, old_end
);
175 if (new_ptl
!= old_ptl
)
176 spin_unlock(new_ptl
);
177 pte_unmap(new_pte
- 1);
178 pte_unmap_unlock(old_pte
- 1, old_ptl
);
180 drop_rmap_locks(vma
);
183 #define LATENCY_LIMIT (64 * PAGE_SIZE)
185 unsigned long move_page_tables(struct vm_area_struct
*vma
,
186 unsigned long old_addr
, struct vm_area_struct
*new_vma
,
187 unsigned long new_addr
, unsigned long len
,
188 bool need_rmap_locks
)
190 unsigned long extent
, next
, old_end
;
191 pmd_t
*old_pmd
, *new_pmd
;
192 unsigned long mmun_start
; /* For mmu_notifiers */
193 unsigned long mmun_end
; /* For mmu_notifiers */
195 old_end
= old_addr
+ len
;
196 flush_cache_range(vma
, old_addr
, old_end
);
198 mmun_start
= old_addr
;
200 mmu_notifier_invalidate_range_start(vma
->vm_mm
, mmun_start
, mmun_end
);
202 for (; old_addr
< old_end
; old_addr
+= extent
, new_addr
+= extent
) {
204 next
= (old_addr
+ PMD_SIZE
) & PMD_MASK
;
205 /* even if next overflowed, extent below will be ok */
206 extent
= next
- old_addr
;
207 if (extent
> old_end
- old_addr
)
208 extent
= old_end
- old_addr
;
209 old_pmd
= get_old_pmd(vma
->vm_mm
, old_addr
);
212 new_pmd
= alloc_new_pmd(vma
->vm_mm
, vma
, new_addr
);
215 if (pmd_trans_huge(*old_pmd
) || pmd_devmap(*old_pmd
)) {
216 if (extent
== HPAGE_PMD_SIZE
) {
218 /* See comment in move_ptes() */
220 take_rmap_locks(vma
);
221 moved
= move_huge_pmd(vma
, old_addr
, new_addr
,
222 old_end
, old_pmd
, new_pmd
);
224 drop_rmap_locks(vma
);
228 split_huge_pmd(vma
, old_pmd
, old_addr
);
229 if (pmd_trans_unstable(old_pmd
))
232 if (pte_alloc(new_vma
->vm_mm
, new_pmd
, new_addr
))
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
, new_vma
,
240 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
, new_len
>> PAGE_SHIFT
);
334 /* Tell pfnmap has moved from this vma */
335 if (unlikely(vma
->vm_flags
& VM_PFNMAP
))
336 untrack_pfn_moved(vma
);
338 if (do_munmap(mm
, old_addr
, old_len
) < 0) {
339 /* OOM: unable to split vma, just get accounts right */
340 vm_unacct_memory(excess
>> PAGE_SHIFT
);
343 mm
->hiwater_vm
= hiwater_vm
;
345 /* Restore VM_ACCOUNT if one or two pieces of vma left */
347 vma
->vm_flags
|= VM_ACCOUNT
;
349 vma
->vm_next
->vm_flags
|= VM_ACCOUNT
;
352 if (vm_flags
& VM_LOCKED
) {
353 mm
->locked_vm
+= new_len
>> PAGE_SHIFT
;
360 static struct vm_area_struct
*vma_to_resize(unsigned long addr
,
361 unsigned long old_len
, unsigned long new_len
, unsigned long *p
)
363 struct mm_struct
*mm
= current
->mm
;
364 struct vm_area_struct
*vma
= find_vma(mm
, addr
);
367 if (!vma
|| vma
->vm_start
> addr
)
368 return ERR_PTR(-EFAULT
);
370 if (is_vm_hugetlb_page(vma
))
371 return ERR_PTR(-EINVAL
);
373 /* We can't remap across vm area boundaries */
374 if (old_len
> vma
->vm_end
- addr
)
375 return ERR_PTR(-EFAULT
);
377 if (new_len
== old_len
)
380 /* Need to be careful about a growing mapping */
381 pgoff
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
382 pgoff
+= vma
->vm_pgoff
;
383 if (pgoff
+ (new_len
>> PAGE_SHIFT
) < pgoff
)
384 return ERR_PTR(-EINVAL
);
386 if (vma
->vm_flags
& (VM_DONTEXPAND
| VM_PFNMAP
))
387 return ERR_PTR(-EFAULT
);
389 if (vma
->vm_flags
& VM_LOCKED
) {
390 unsigned long locked
, lock_limit
;
391 locked
= mm
->locked_vm
<< PAGE_SHIFT
;
392 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
393 locked
+= new_len
- old_len
;
394 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
395 return ERR_PTR(-EAGAIN
);
398 if (!may_expand_vm(mm
, vma
->vm_flags
,
399 (new_len
- old_len
) >> PAGE_SHIFT
))
400 return ERR_PTR(-ENOMEM
);
402 if (vma
->vm_flags
& VM_ACCOUNT
) {
403 unsigned long charged
= (new_len
- old_len
) >> PAGE_SHIFT
;
404 if (security_vm_enough_memory_mm(mm
, charged
))
405 return ERR_PTR(-ENOMEM
);
412 static unsigned long mremap_to(unsigned long addr
, unsigned long old_len
,
413 unsigned long new_addr
, unsigned long new_len
, bool *locked
)
415 struct mm_struct
*mm
= current
->mm
;
416 struct vm_area_struct
*vma
;
417 unsigned long ret
= -EINVAL
;
418 unsigned long charged
= 0;
419 unsigned long map_flags
;
421 if (offset_in_page(new_addr
))
424 if (new_len
> TASK_SIZE
|| new_addr
> TASK_SIZE
- new_len
)
427 /* Ensure the old/new locations do not overlap */
428 if (addr
+ old_len
> new_addr
&& new_addr
+ new_len
> addr
)
431 ret
= do_munmap(mm
, new_addr
, new_len
);
435 if (old_len
>= new_len
) {
436 ret
= do_munmap(mm
, addr
+new_len
, old_len
- new_len
);
437 if (ret
&& old_len
!= new_len
)
442 vma
= vma_to_resize(addr
, old_len
, new_len
, &charged
);
448 map_flags
= MAP_FIXED
;
449 if (vma
->vm_flags
& VM_MAYSHARE
)
450 map_flags
|= MAP_SHARED
;
452 ret
= get_unmapped_area(vma
->vm_file
, new_addr
, new_len
, vma
->vm_pgoff
+
453 ((addr
- vma
->vm_start
) >> PAGE_SHIFT
),
455 if (offset_in_page(ret
))
458 ret
= move_vma(vma
, addr
, old_len
, new_len
, new_addr
, locked
);
459 if (!(offset_in_page(ret
)))
462 vm_unacct_memory(charged
);
468 static int vma_expandable(struct vm_area_struct
*vma
, unsigned long delta
)
470 unsigned long end
= vma
->vm_end
+ delta
;
471 if (end
< vma
->vm_end
) /* overflow */
473 if (vma
->vm_next
&& vma
->vm_next
->vm_start
< end
) /* intersection */
475 if (get_unmapped_area(NULL
, vma
->vm_start
, end
- vma
->vm_start
,
476 0, MAP_FIXED
) & ~PAGE_MASK
)
482 * Expand (or shrink) an existing mapping, potentially moving it at the
483 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
485 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
486 * This option implies MREMAP_MAYMOVE.
488 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
489 unsigned long, new_len
, unsigned long, flags
,
490 unsigned long, new_addr
)
492 struct mm_struct
*mm
= current
->mm
;
493 struct vm_area_struct
*vma
;
494 unsigned long ret
= -EINVAL
;
495 unsigned long charged
= 0;
498 if (flags
& ~(MREMAP_FIXED
| MREMAP_MAYMOVE
))
501 if (flags
& MREMAP_FIXED
&& !(flags
& MREMAP_MAYMOVE
))
504 if (offset_in_page(addr
))
507 old_len
= PAGE_ALIGN(old_len
);
508 new_len
= PAGE_ALIGN(new_len
);
511 * We allow a zero old-len as a special case
512 * for DOS-emu "duplicate shm area" thing. But
513 * a zero new-len is nonsensical.
518 if (down_write_killable(¤t
->mm
->mmap_sem
))
521 if (flags
& MREMAP_FIXED
) {
522 ret
= mremap_to(addr
, old_len
, new_addr
, new_len
,
528 * Always allow a shrinking remap: that just unmaps
529 * the unnecessary pages..
530 * do_munmap does all the needed commit accounting
532 if (old_len
>= new_len
) {
533 ret
= do_munmap(mm
, addr
+new_len
, old_len
- new_len
);
534 if (ret
&& old_len
!= new_len
)
541 * Ok, we need to grow..
543 vma
= vma_to_resize(addr
, old_len
, new_len
, &charged
);
549 /* old_len exactly to the end of the area..
551 if (old_len
== vma
->vm_end
- addr
) {
552 /* can we just expand the current mapping? */
553 if (vma_expandable(vma
, new_len
- old_len
)) {
554 int pages
= (new_len
- old_len
) >> PAGE_SHIFT
;
556 if (vma_adjust(vma
, vma
->vm_start
, addr
+ new_len
,
557 vma
->vm_pgoff
, NULL
)) {
562 vm_stat_account(mm
, vma
->vm_flags
, pages
);
563 if (vma
->vm_flags
& VM_LOCKED
) {
564 mm
->locked_vm
+= pages
;
574 * We weren't able to just expand or shrink the area,
575 * we need to create a new one and move it..
578 if (flags
& MREMAP_MAYMOVE
) {
579 unsigned long map_flags
= 0;
580 if (vma
->vm_flags
& VM_MAYSHARE
)
581 map_flags
|= MAP_SHARED
;
583 new_addr
= get_unmapped_area(vma
->vm_file
, 0, new_len
,
585 ((addr
- vma
->vm_start
) >> PAGE_SHIFT
),
587 if (offset_in_page(new_addr
)) {
592 ret
= move_vma(vma
, addr
, old_len
, new_len
, new_addr
, &locked
);
595 if (offset_in_page(ret
)) {
596 vm_unacct_memory(charged
);
599 up_write(¤t
->mm
->mmap_sem
);
600 if (locked
&& new_len
> old_len
)
601 mm_populate(new_addr
+ old_len
, new_len
- old_len
);