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>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/acct.h>
20 #include <linux/syscalls.h>
21 #include <linux/vs_memory.h>
23 #include <asm/uaccess.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
27 static pte_t *get_one_pte_map_nested(struct mm_struct *mm, unsigned long addr)
34 pgd = pgd_offset(mm, addr);
38 pud = pud_offset(pgd, addr);
47 pmd = pmd_offset(pud, addr);
56 pte = pte_offset_map_nested(pmd, addr);
58 pte_unmap_nested(pte);
65 static pte_t *get_one_pte_map(struct mm_struct *mm, unsigned long addr)
71 pgd = pgd_offset(mm, addr);
75 pud = pud_offset(pgd, addr);
78 pmd = pmd_offset(pud, addr);
79 if (!pmd_present(*pmd))
81 return pte_offset_map(pmd, addr);
84 static inline pte_t *alloc_one_pte_map(struct mm_struct *mm, unsigned long addr)
91 pgd = pgd_offset(mm, addr);
93 pud = pud_alloc(mm, pgd, addr);
96 pmd = pmd_alloc(mm, pud, addr);
98 pte = pte_alloc_map(mm, pmd, addr);
103 move_one_page(struct vm_area_struct *vma, unsigned long old_addr,
104 struct vm_area_struct *new_vma, unsigned long new_addr)
106 struct address_space *mapping = NULL;
107 struct mm_struct *mm = vma->vm_mm;
113 * Subtle point from Rajesh Venkatasubramanian: before
114 * moving file-based ptes, we must lock vmtruncate out,
115 * since it might clean the dst vma before the src vma,
116 * and we propagate stale pages into the dst afterward.
118 mapping = vma->vm_file->f_mapping;
119 spin_lock(&mapping->i_mmap_lock);
120 if (new_vma->vm_truncate_count &&
121 new_vma->vm_truncate_count != vma->vm_truncate_count)
122 new_vma->vm_truncate_count = 0;
124 spin_lock(&mm->page_table_lock);
126 src = get_one_pte_map_nested(mm, old_addr);
129 * Look to see whether alloc_one_pte_map needs to perform a
130 * memory allocation. If it does then we need to drop the
133 dst = get_one_pte_map(mm, new_addr);
134 if (unlikely(!dst)) {
135 pte_unmap_nested(src);
137 spin_unlock(&mapping->i_mmap_lock);
138 dst = alloc_one_pte_map(mm, new_addr);
139 if (mapping && !spin_trylock(&mapping->i_mmap_lock)) {
140 spin_unlock(&mm->page_table_lock);
141 spin_lock(&mapping->i_mmap_lock);
142 spin_lock(&mm->page_table_lock);
144 src = get_one_pte_map_nested(mm, old_addr);
147 * Since alloc_one_pte_map can drop and re-acquire
148 * page_table_lock, we should re-check the src entry...
153 pte = ptep_clear_flush(vma, old_addr, src);
157 pte_unmap_nested(src);
162 spin_unlock(&mm->page_table_lock);
164 spin_unlock(&mapping->i_mmap_lock);
168 static unsigned long move_page_tables(struct vm_area_struct *vma,
169 unsigned long old_addr, struct vm_area_struct *new_vma,
170 unsigned long new_addr, unsigned long len)
172 unsigned long offset;
174 flush_cache_range(vma, old_addr, old_addr + len);
177 * This is not the clever way to do this, but we're taking the
178 * easy way out on the assumption that most remappings will be
179 * only a few pages.. This also makes error recovery easier.
181 for (offset = 0; offset < len; offset += PAGE_SIZE) {
182 if (move_one_page(vma, old_addr + offset,
183 new_vma, new_addr + offset) < 0)
190 static unsigned long move_vma(struct vm_area_struct *vma,
191 unsigned long old_addr, unsigned long old_len,
192 unsigned long new_len, unsigned long new_addr)
194 struct mm_struct *mm = vma->vm_mm;
195 struct vm_area_struct *new_vma;
196 unsigned long vm_flags = vma->vm_flags;
197 unsigned long new_pgoff;
198 unsigned long moved_len;
199 unsigned long excess = 0;
203 * We'd prefer to avoid failure later on in do_munmap:
204 * which may split one vma into three before unmapping.
206 if (mm->map_count >= sysctl_max_map_count - 3)
209 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
210 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
214 moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
215 if (moved_len < old_len) {
217 * On error, move entries back from new area to old,
218 * which will succeed since page tables still there,
219 * and then proceed to unmap new area instead of old.
221 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
228 /* Conceal VM_ACCOUNT so old reservation is not undone */
229 if (vm_flags & VM_ACCOUNT) {
230 vma->vm_flags &= ~VM_ACCOUNT;
231 excess = vma->vm_end - vma->vm_start - old_len;
232 if (old_addr > vma->vm_start &&
233 old_addr + old_len < vma->vm_end)
237 if (do_munmap(mm, old_addr, old_len) < 0) {
238 /* OOM: unable to split vma, just get accounts right */
239 vm_unacct_memory(excess >> PAGE_SHIFT);
243 /* Restore VM_ACCOUNT if one or two pieces of vma left */
245 vma->vm_flags |= VM_ACCOUNT;
247 vma->vm_next->vm_flags |= VM_ACCOUNT;
250 // mm->total_vm += new_len >> PAGE_SHIFT;
251 vx_vmpages_add(mm, new_len >> PAGE_SHIFT);
252 __vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
253 if (vm_flags & VM_LOCKED) {
254 // mm->locked_vm += new_len >> PAGE_SHIFT;
255 vx_vmlocked_add(mm, new_len >> PAGE_SHIFT);
256 if (new_len > old_len)
257 make_pages_present(new_addr + old_len,
261 acct_update_integrals();
262 update_mem_hiwater();
268 * Expand (or shrink) an existing mapping, potentially moving it at the
269 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
271 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
272 * This option implies MREMAP_MAYMOVE.
274 unsigned long do_mremap(unsigned long addr,
275 unsigned long old_len, unsigned long new_len,
276 unsigned long flags, unsigned long new_addr)
278 struct vm_area_struct *vma;
279 unsigned long ret = -EINVAL;
280 unsigned long charged = 0;
282 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
285 if (addr & ~PAGE_MASK)
288 old_len = PAGE_ALIGN(old_len);
289 new_len = PAGE_ALIGN(new_len);
292 * We allow a zero old-len as a special case
293 * for DOS-emu "duplicate shm area" thing. But
294 * a zero new-len is nonsensical.
299 /* new_addr is only valid if MREMAP_FIXED is specified */
300 if (flags & MREMAP_FIXED) {
301 if (new_addr & ~PAGE_MASK)
303 if (!(flags & MREMAP_MAYMOVE))
306 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
309 /* Check if the location we're moving into overlaps the
310 * old location at all, and fail if it does.
312 if ((new_addr <= addr) && (new_addr+new_len) > addr)
315 if ((addr <= new_addr) && (addr+old_len) > new_addr)
318 ret = do_munmap(current->mm, new_addr, new_len);
324 * Always allow a shrinking remap: that just unmaps
325 * the unnecessary pages..
326 * do_munmap does all the needed commit accounting
328 if (old_len >= new_len) {
329 ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
330 if (ret && old_len != new_len)
333 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
339 * Ok, we need to grow.. or relocate.
342 vma = find_vma(current->mm, addr);
343 if (!vma || vma->vm_start > addr)
345 if (is_vm_hugetlb_page(vma)) {
349 /* We can't remap across vm area boundaries */
350 if (old_len > vma->vm_end - addr)
352 if (vma->vm_flags & VM_DONTEXPAND) {
353 if (new_len > old_len)
356 if (vma->vm_flags & VM_LOCKED) {
357 unsigned long locked, lock_limit;
358 locked = current->mm->locked_vm << PAGE_SHIFT;
359 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
360 locked += new_len - old_len;
362 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
365 if (!vx_vmlocked_avail(current->mm,
366 (new_len - old_len) >> PAGE_SHIFT))
370 if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
371 > current->signal->rlim[RLIMIT_AS].rlim_cur)
373 /* check context space, maybe only Private writable mapping? */
374 if (!vx_vmpages_avail(current->mm, (new_len - old_len) >> PAGE_SHIFT))
377 if (vma->vm_flags & VM_ACCOUNT) {
378 charged = (new_len - old_len) >> PAGE_SHIFT;
379 if (security_vm_enough_memory(charged))
383 /* old_len exactly to the end of the area..
384 * And we're not relocating the area.
386 if (old_len == vma->vm_end - addr &&
387 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
388 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
389 unsigned long max_addr = TASK_SIZE;
391 max_addr = vma->vm_next->vm_start;
392 /* can we just expand the current mapping? */
393 if (max_addr - addr >= new_len) {
394 int pages = (new_len - old_len) >> PAGE_SHIFT;
396 vma_adjust(vma, vma->vm_start,
397 addr + new_len, vma->vm_pgoff, NULL);
399 // current->mm->total_vm += pages;
400 vx_vmpages_add(current->mm, pages);
401 __vm_stat_account(vma->vm_mm, vma->vm_flags,
402 vma->vm_file, pages);
403 if (vma->vm_flags & VM_LOCKED) {
404 // current->mm->locked_vm += pages;
405 vx_vmlocked_add(vma->vm_mm, pages);
406 make_pages_present(addr + old_len,
409 acct_update_integrals();
410 update_mem_hiwater();
417 * We weren't able to just expand or shrink the area,
418 * we need to create a new one and move it..
421 if (flags & MREMAP_MAYMOVE) {
422 if (!(flags & MREMAP_FIXED)) {
423 unsigned long map_flags = 0;
424 if (vma->vm_flags & VM_MAYSHARE)
425 map_flags |= MAP_SHARED;
427 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
428 vma->vm_pgoff, map_flags);
430 if (new_addr & ~PAGE_MASK)
433 ret = move_vma(vma, addr, old_len, new_len, new_addr);
436 if (ret & ~PAGE_MASK)
437 vm_unacct_memory(charged);
442 asmlinkage unsigned long sys_mremap(unsigned long addr,
443 unsigned long old_len, unsigned long new_len,
444 unsigned long flags, unsigned long new_addr)
448 down_write(¤t->mm->mmap_sem);
449 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
450 up_write(¤t->mm->mmap_sem);