4 * (C) Copyright 1996 Linus Torvalds
6 * Address space accounting code <alan@redhat.com>
7 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
11 #include <linux/hugetlb.h>
12 #include <linux/slab.h>
13 #include <linux/shm.h>
14 #include <linux/mman.h>
15 #include <linux/swap.h>
16 #include <linux/capability.h>
18 #include <linux/highmem.h>
19 #include <linux/security.h>
20 #include <linux/syscalls.h>
21 #include <linux/mmu_notifier.h>
23 #include <asm/uaccess.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
27 static pmd_t
*get_old_pmd(struct mm_struct
*mm
, unsigned long addr
)
33 pgd
= pgd_offset(mm
, addr
);
34 if (pgd_none_or_clear_bad(pgd
))
37 pud
= pud_offset(pgd
, addr
);
38 if (pud_none_or_clear_bad(pud
))
41 pmd
= pmd_offset(pud
, addr
);
42 if (pmd_none_or_clear_bad(pmd
))
48 static pmd_t
*alloc_new_pmd(struct mm_struct
*mm
, unsigned long addr
)
54 pgd
= pgd_offset(mm
, addr
);
55 pud
= pud_alloc(mm
, pgd
, addr
);
59 pmd
= pmd_alloc(mm
, pud
, addr
);
63 if (!pmd_present(*pmd
) && __pte_alloc(mm
, pmd
, addr
))
69 static void move_ptes(struct vm_area_struct
*vma
, pmd_t
*old_pmd
,
70 unsigned long old_addr
, unsigned long old_end
,
71 struct vm_area_struct
*new_vma
, pmd_t
*new_pmd
,
72 unsigned long new_addr
)
74 struct address_space
*mapping
= NULL
;
75 struct mm_struct
*mm
= vma
->vm_mm
;
76 pte_t
*old_pte
, *new_pte
, pte
;
77 spinlock_t
*old_ptl
, *new_ptl
;
78 unsigned long old_start
;
81 mmu_notifier_invalidate_range_start(vma
->vm_mm
,
85 * Subtle point from Rajesh Venkatasubramanian: before
86 * moving file-based ptes, we must lock vmtruncate out,
87 * since it might clean the dst vma before the src vma,
88 * and we propagate stale pages into the dst afterward.
90 mapping
= vma
->vm_file
->f_mapping
;
91 spin_lock(&mapping
->i_mmap_lock
);
92 new_vma
->vm_truncate_count
= 0;
96 * We don't have to worry about the ordering of src and dst
97 * pte locks because exclusive mmap_sem prevents deadlock.
99 old_pte
= pte_offset_map_lock(mm
, old_pmd
, old_addr
, &old_ptl
);
100 new_pte
= pte_offset_map_nested(new_pmd
, new_addr
);
101 new_ptl
= pte_lockptr(mm
, new_pmd
);
102 if (new_ptl
!= old_ptl
)
103 spin_lock_nested(new_ptl
, SINGLE_DEPTH_NESTING
);
104 arch_enter_lazy_mmu_mode();
106 for (; old_addr
< old_end
; old_pte
++, old_addr
+= PAGE_SIZE
,
107 new_pte
++, new_addr
+= PAGE_SIZE
) {
108 if (pte_none(*old_pte
))
110 pte
= ptep_clear_flush(vma
, old_addr
, old_pte
);
111 pte
= move_pte(pte
, new_vma
->vm_page_prot
, old_addr
, new_addr
);
112 set_pte_at(mm
, new_addr
, new_pte
, pte
);
115 arch_leave_lazy_mmu_mode();
116 if (new_ptl
!= old_ptl
)
117 spin_unlock(new_ptl
);
118 pte_unmap_nested(new_pte
- 1);
119 pte_unmap_unlock(old_pte
- 1, old_ptl
);
121 spin_unlock(&mapping
->i_mmap_lock
);
122 mmu_notifier_invalidate_range_end(vma
->vm_mm
, old_start
, old_end
);
125 #define LATENCY_LIMIT (64 * PAGE_SIZE)
127 unsigned long move_page_tables(struct vm_area_struct
*vma
,
128 unsigned long old_addr
, struct vm_area_struct
*new_vma
,
129 unsigned long new_addr
, unsigned long len
)
131 unsigned long extent
, next
, old_end
;
132 pmd_t
*old_pmd
, *new_pmd
;
134 old_end
= old_addr
+ len
;
135 flush_cache_range(vma
, old_addr
, old_end
);
137 for (; old_addr
< old_end
; old_addr
+= extent
, new_addr
+= extent
) {
139 next
= (old_addr
+ PMD_SIZE
) & PMD_MASK
;
140 if (next
- 1 > old_end
)
142 extent
= next
- old_addr
;
143 old_pmd
= get_old_pmd(vma
->vm_mm
, old_addr
);
146 new_pmd
= alloc_new_pmd(vma
->vm_mm
, new_addr
);
149 next
= (new_addr
+ PMD_SIZE
) & PMD_MASK
;
150 if (extent
> next
- new_addr
)
151 extent
= next
- new_addr
;
152 if (extent
> LATENCY_LIMIT
)
153 extent
= LATENCY_LIMIT
;
154 move_ptes(vma
, old_pmd
, old_addr
, old_addr
+ extent
,
155 new_vma
, new_pmd
, new_addr
);
158 return len
+ old_addr
- old_end
; /* how much done */
161 static unsigned long move_vma(struct vm_area_struct
*vma
,
162 unsigned long old_addr
, unsigned long old_len
,
163 unsigned long new_len
, unsigned long new_addr
)
165 struct mm_struct
*mm
= vma
->vm_mm
;
166 struct vm_area_struct
*new_vma
;
167 unsigned long vm_flags
= vma
->vm_flags
;
168 unsigned long new_pgoff
;
169 unsigned long moved_len
;
170 unsigned long excess
= 0;
171 unsigned long hiwater_vm
;
175 * We'd prefer to avoid failure later on in do_munmap:
176 * which may split one vma into three before unmapping.
178 if (mm
->map_count
>= sysctl_max_map_count
- 3)
181 new_pgoff
= vma
->vm_pgoff
+ ((old_addr
- vma
->vm_start
) >> PAGE_SHIFT
);
182 new_vma
= copy_vma(&vma
, new_addr
, new_len
, new_pgoff
);
186 moved_len
= move_page_tables(vma
, old_addr
, new_vma
, new_addr
, old_len
);
187 if (moved_len
< old_len
) {
189 * On error, move entries back from new area to old,
190 * which will succeed since page tables still there,
191 * and then proceed to unmap new area instead of old.
193 move_page_tables(new_vma
, new_addr
, vma
, old_addr
, moved_len
);
200 /* Conceal VM_ACCOUNT so old reservation is not undone */
201 if (vm_flags
& VM_ACCOUNT
) {
202 vma
->vm_flags
&= ~VM_ACCOUNT
;
203 excess
= vma
->vm_end
- vma
->vm_start
- old_len
;
204 if (old_addr
> vma
->vm_start
&&
205 old_addr
+ old_len
< vma
->vm_end
)
210 * If we failed to move page tables we still do total_vm increment
211 * since do_munmap() will decrement it by old_len == new_len.
213 * Since total_vm is about to be raised artificially high for a
214 * moment, we need to restore high watermark afterwards: if stats
215 * are taken meanwhile, total_vm and hiwater_vm appear too high.
216 * If this were a serious issue, we'd add a flag to do_munmap().
218 hiwater_vm
= mm
->hiwater_vm
;
219 mm
->total_vm
+= new_len
>> PAGE_SHIFT
;
220 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, new_len
>>PAGE_SHIFT
);
222 if (do_munmap(mm
, old_addr
, old_len
) < 0) {
223 /* OOM: unable to split vma, just get accounts right */
224 vm_unacct_memory(excess
>> PAGE_SHIFT
);
227 mm
->hiwater_vm
= hiwater_vm
;
229 /* Restore VM_ACCOUNT if one or two pieces of vma left */
231 vma
->vm_flags
|= VM_ACCOUNT
;
233 vma
->vm_next
->vm_flags
|= VM_ACCOUNT
;
236 if (vm_flags
& VM_LOCKED
) {
237 mm
->locked_vm
+= new_len
>> PAGE_SHIFT
;
238 if (new_len
> old_len
)
239 make_pages_present(new_addr
+ old_len
,
247 * Expand (or shrink) an existing mapping, potentially moving it at the
248 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
250 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
251 * This option implies MREMAP_MAYMOVE.
253 unsigned long do_mremap(unsigned long addr
,
254 unsigned long old_len
, unsigned long new_len
,
255 unsigned long flags
, unsigned long new_addr
)
257 struct mm_struct
*mm
= current
->mm
;
258 struct vm_area_struct
*vma
;
259 unsigned long ret
= -EINVAL
;
260 unsigned long charged
= 0;
262 if (flags
& ~(MREMAP_FIXED
| MREMAP_MAYMOVE
))
265 if (addr
& ~PAGE_MASK
)
268 old_len
= PAGE_ALIGN(old_len
);
269 new_len
= PAGE_ALIGN(new_len
);
272 * We allow a zero old-len as a special case
273 * for DOS-emu "duplicate shm area" thing. But
274 * a zero new-len is nonsensical.
279 /* new_addr is only valid if MREMAP_FIXED is specified */
280 if (flags
& MREMAP_FIXED
) {
281 if (new_addr
& ~PAGE_MASK
)
283 if (!(flags
& MREMAP_MAYMOVE
))
286 if (new_len
> TASK_SIZE
|| new_addr
> TASK_SIZE
- new_len
)
289 /* Check if the location we're moving into overlaps the
290 * old location at all, and fail if it does.
292 if ((new_addr
<= addr
) && (new_addr
+new_len
) > addr
)
295 if ((addr
<= new_addr
) && (addr
+old_len
) > new_addr
)
298 ret
= security_file_mmap(NULL
, 0, 0, 0, new_addr
, 1);
302 ret
= do_munmap(mm
, new_addr
, new_len
);
308 * Always allow a shrinking remap: that just unmaps
309 * the unnecessary pages..
310 * do_munmap does all the needed commit accounting
312 if (old_len
>= new_len
) {
313 ret
= do_munmap(mm
, addr
+new_len
, old_len
- new_len
);
314 if (ret
&& old_len
!= new_len
)
317 if (!(flags
& MREMAP_FIXED
) || (new_addr
== addr
))
323 * Ok, we need to grow.. or relocate.
326 vma
= find_vma(mm
, addr
);
327 if (!vma
|| vma
->vm_start
> addr
)
329 if (is_vm_hugetlb_page(vma
)) {
333 /* We can't remap across vm area boundaries */
334 if (old_len
> vma
->vm_end
- addr
)
337 /* Need to be careful about a growing mapping */
338 if (new_len
> old_len
) {
341 if (vma
->vm_flags
& (VM_DONTEXPAND
| VM_PFNMAP
))
343 pgoff
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
344 pgoff
+= vma
->vm_pgoff
;
345 if (pgoff
+ (new_len
>> PAGE_SHIFT
) < pgoff
) {
351 if (vma
->vm_flags
& VM_LOCKED
) {
352 unsigned long locked
, lock_limit
;
353 locked
= mm
->locked_vm
<< PAGE_SHIFT
;
354 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
355 locked
+= new_len
- old_len
;
357 if (locked
> lock_limit
&& !capable(CAP_IPC_LOCK
))
360 if (!may_expand_vm(mm
, (new_len
- old_len
) >> PAGE_SHIFT
)) {
365 if (vma
->vm_flags
& VM_ACCOUNT
) {
366 charged
= (new_len
- old_len
) >> PAGE_SHIFT
;
367 if (security_vm_enough_memory(charged
))
371 /* old_len exactly to the end of the area..
372 * And we're not relocating the area.
374 if (old_len
== vma
->vm_end
- addr
&&
375 !((flags
& MREMAP_FIXED
) && (addr
!= new_addr
)) &&
376 (old_len
!= new_len
|| !(flags
& MREMAP_MAYMOVE
))) {
377 unsigned long max_addr
= TASK_SIZE
;
379 max_addr
= vma
->vm_next
->vm_start
;
380 /* can we just expand the current mapping? */
381 if (max_addr
- addr
>= new_len
) {
382 int pages
= (new_len
- old_len
) >> PAGE_SHIFT
;
384 vma_adjust(vma
, vma
->vm_start
,
385 addr
+ new_len
, vma
->vm_pgoff
, NULL
);
387 mm
->total_vm
+= pages
;
388 vm_stat_account(mm
, vma
->vm_flags
, vma
->vm_file
, pages
);
389 if (vma
->vm_flags
& VM_LOCKED
) {
390 mm
->locked_vm
+= pages
;
391 make_pages_present(addr
+ old_len
,
400 * We weren't able to just expand or shrink the area,
401 * we need to create a new one and move it..
404 if (flags
& MREMAP_MAYMOVE
) {
405 if (!(flags
& MREMAP_FIXED
)) {
406 unsigned long map_flags
= 0;
407 if (vma
->vm_flags
& VM_MAYSHARE
)
408 map_flags
|= MAP_SHARED
;
410 new_addr
= get_unmapped_area(vma
->vm_file
, 0, new_len
,
411 vma
->vm_pgoff
, map_flags
);
412 if (new_addr
& ~PAGE_MASK
) {
417 ret
= security_file_mmap(NULL
, 0, 0, 0, new_addr
, 1);
421 ret
= move_vma(vma
, addr
, old_len
, new_len
, new_addr
);
424 if (ret
& ~PAGE_MASK
)
425 vm_unacct_memory(charged
);
430 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
431 unsigned long, new_len
, unsigned long, flags
,
432 unsigned long, new_addr
)
436 down_write(¤t
->mm
->mmap_sem
);
437 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
438 up_write(¤t
->mm
->mmap_sem
);