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/vs_memory.h>
20 #include <linux/syscalls.h>
22 #include <asm/uaccess.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
26 static pte_t *get_one_pte_map_nested(struct mm_struct *mm, unsigned long addr)
32 pgd = pgd_offset(mm, addr);
41 pmd = pmd_offset(pgd, addr);
50 pte = pte_offset_map_nested(pmd, addr);
52 pte_unmap_nested(pte);
59 static pte_t *get_one_pte_map(struct mm_struct *mm, unsigned long addr)
64 pgd = pgd_offset(mm, addr);
67 pmd = pmd_offset(pgd, addr);
68 if (!pmd_present(*pmd))
70 return pte_offset_map(pmd, addr);
73 static inline pte_t *alloc_one_pte_map(struct mm_struct *mm, unsigned long addr)
78 pmd = pmd_alloc(mm, pgd_offset(mm, addr), addr);
80 pte = pte_alloc_map(mm, pmd, addr);
85 move_one_page(struct vm_area_struct *vma, unsigned long old_addr,
86 unsigned long new_addr)
88 struct address_space *mapping = NULL;
89 struct mm_struct *mm = vma->vm_mm;
95 * Subtle point from Rajesh Venkatasubramanian: before
96 * moving file-based ptes, we must lock vmtruncate out,
97 * since it might clean the dst vma before the src vma,
98 * and we propagate stale pages into the dst afterward.
100 mapping = vma->vm_file->f_mapping;
101 spin_lock(&mapping->i_mmap_lock);
103 spin_lock(&mm->page_table_lock);
105 src = get_one_pte_map_nested(mm, old_addr);
108 * Look to see whether alloc_one_pte_map needs to perform a
109 * memory allocation. If it does then we need to drop the
112 dst = get_one_pte_map(mm, new_addr);
113 if (unlikely(!dst)) {
114 pte_unmap_nested(src);
116 spin_unlock(&mapping->i_mmap_lock);
117 dst = alloc_one_pte_map(mm, new_addr);
118 if (mapping && !spin_trylock(&mapping->i_mmap_lock)) {
119 spin_unlock(&mm->page_table_lock);
120 spin_lock(&mapping->i_mmap_lock);
121 spin_lock(&mm->page_table_lock);
123 src = get_one_pte_map_nested(mm, old_addr);
126 * Since alloc_one_pte_map can drop and re-acquire
127 * page_table_lock, we should re-check the src entry...
132 pte = ptep_clear_flush(vma, old_addr, src);
136 pte_unmap_nested(src);
141 spin_unlock(&mm->page_table_lock);
143 spin_unlock(&mapping->i_mmap_lock);
147 static unsigned long move_page_tables(struct vm_area_struct *vma,
148 unsigned long new_addr, unsigned long old_addr,
151 unsigned long offset;
153 flush_cache_range(vma, old_addr, old_addr + len);
156 * This is not the clever way to do this, but we're taking the
157 * easy way out on the assumption that most remappings will be
158 * only a few pages.. This also makes error recovery easier.
160 for (offset = 0; offset < len; offset += PAGE_SIZE) {
161 if (move_one_page(vma, old_addr+offset, new_addr+offset) < 0)
168 static unsigned long move_vma(struct vm_area_struct *vma,
169 unsigned long old_addr, unsigned long old_len,
170 unsigned long new_len, unsigned long new_addr)
172 struct mm_struct *mm = vma->vm_mm;
173 struct vm_area_struct *new_vma;
174 unsigned long vm_flags = vma->vm_flags;
175 unsigned long new_pgoff;
176 unsigned long moved_len;
177 unsigned long excess = 0;
181 * We'd prefer to avoid failure later on in do_munmap:
182 * which may split one vma into three before unmapping.
184 if (mm->map_count >= sysctl_max_map_count - 3)
187 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
188 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
192 moved_len = move_page_tables(vma, new_addr, old_addr, old_len);
193 if (moved_len < old_len) {
195 * On error, move entries back from new area to old,
196 * which will succeed since page tables still there,
197 * and then proceed to unmap new area instead of old.
199 move_page_tables(new_vma, old_addr, new_addr, moved_len);
206 /* Conceal VM_ACCOUNT so old reservation is not undone */
207 if (vm_flags & VM_ACCOUNT) {
208 vma->vm_flags &= ~VM_ACCOUNT;
209 excess = vma->vm_end - vma->vm_start - old_len;
210 if (old_addr > vma->vm_start &&
211 old_addr + old_len < vma->vm_end)
215 if (do_munmap(mm, old_addr, old_len) < 0) {
216 /* OOM: unable to split vma, just get accounts right */
217 vm_unacct_memory(excess >> PAGE_SHIFT);
221 /* Restore VM_ACCOUNT if one or two pieces of vma left */
223 vma->vm_flags |= VM_ACCOUNT;
225 vma->vm_next->vm_flags |= VM_ACCOUNT;
228 // mm->total_vm += new_len >> PAGE_SHIFT;
229 vx_vmpages_add(mm, new_len >> PAGE_SHIFT);
230 __vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
231 if (vm_flags & VM_LOCKED) {
232 // mm->locked_vm += new_len >> PAGE_SHIFT;
233 vx_vmlocked_add(mm, new_len >> PAGE_SHIFT);
234 if (new_len > old_len)
235 make_pages_present(new_addr + old_len,
243 * Expand (or shrink) an existing mapping, potentially moving it at the
244 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
246 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
247 * This option implies MREMAP_MAYMOVE.
249 unsigned long do_mremap(unsigned long addr,
250 unsigned long old_len, unsigned long new_len,
251 unsigned long flags, unsigned long new_addr)
253 struct vm_area_struct *vma;
254 unsigned long ret = -EINVAL;
255 unsigned long charged = 0;
257 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
260 if (addr & ~PAGE_MASK)
263 old_len = PAGE_ALIGN(old_len);
264 new_len = PAGE_ALIGN(new_len);
267 * We allow a zero old-len as a special case
268 * for DOS-emu "duplicate shm area" thing. But
269 * a zero new-len is nonsensical.
274 /* new_addr is only valid if MREMAP_FIXED is specified */
275 if (flags & MREMAP_FIXED) {
276 if (new_addr & ~PAGE_MASK)
278 if (!(flags & MREMAP_MAYMOVE))
281 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
284 /* Check if the location we're moving into overlaps the
285 * old location at all, and fail if it does.
287 if ((new_addr <= addr) && (new_addr+new_len) > addr)
290 if ((addr <= new_addr) && (addr+old_len) > new_addr)
293 ret = do_munmap(current->mm, new_addr, new_len);
299 * Always allow a shrinking remap: that just unmaps
300 * the unnecessary pages..
301 * do_munmap does all the needed commit accounting
303 if (old_len >= new_len) {
304 ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
305 if (ret && old_len != new_len)
308 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
314 * Ok, we need to grow.. or relocate.
317 vma = find_vma(current->mm, addr);
318 if (!vma || vma->vm_start > addr)
320 if (is_vm_hugetlb_page(vma)) {
324 /* We can't remap across vm area boundaries */
325 if (old_len > vma->vm_end - addr)
327 if (vma->vm_flags & VM_DONTEXPAND) {
328 if (new_len > old_len)
331 if (vma->vm_flags & VM_LOCKED) {
332 unsigned long locked, lock_limit;
333 locked = current->mm->locked_vm << PAGE_SHIFT;
334 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
335 locked += new_len - old_len;
337 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
340 if (!vx_vmlocked_avail(current->mm,
341 (new_len - old_len) >> PAGE_SHIFT))
345 if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
346 > current->signal->rlim[RLIMIT_AS].rlim_cur)
348 /* check context space, maybe only Private writable mapping? */
349 if (!vx_vmpages_avail(current->mm, (new_len - old_len) >> PAGE_SHIFT))
352 if (vma->vm_flags & VM_ACCOUNT) {
353 charged = (new_len - old_len) >> PAGE_SHIFT;
354 if (security_vm_enough_memory(charged))
358 /* old_len exactly to the end of the area..
359 * And we're not relocating the area.
361 if (old_len == vma->vm_end - addr &&
362 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
363 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
364 unsigned long max_addr = TASK_SIZE;
366 max_addr = vma->vm_next->vm_start;
367 /* can we just expand the current mapping? */
368 if (max_addr - addr >= new_len) {
369 int pages = (new_len - old_len) >> PAGE_SHIFT;
371 vma_adjust(vma, vma->vm_start,
372 addr + new_len, vma->vm_pgoff, NULL);
374 // current->mm->total_vm += pages;
375 vx_vmpages_add(current->mm, pages);
376 __vm_stat_account(vma->vm_mm, vma->vm_flags,
377 vma->vm_file, pages);
378 if (vma->vm_flags & VM_LOCKED) {
379 // current->mm->locked_vm += pages;
380 vx_vmlocked_add(vma->vm_mm, pages);
381 make_pages_present(addr + old_len,
390 * We weren't able to just expand or shrink the area,
391 * we need to create a new one and move it..
394 if (flags & MREMAP_MAYMOVE) {
395 if (!(flags & MREMAP_FIXED)) {
396 unsigned long map_flags = 0;
397 if (vma->vm_flags & VM_MAYSHARE)
398 map_flags |= MAP_SHARED;
400 new_addr = get_unmapped_area_prot(vma->vm_file, 0, new_len,
401 vma->vm_pgoff, map_flags, vma->vm_flags & VM_EXEC);
403 if (new_addr & ~PAGE_MASK)
406 ret = move_vma(vma, addr, old_len, new_len, new_addr);
409 if (ret & ~PAGE_MASK)
410 vm_unacct_memory(charged);
415 asmlinkage unsigned long sys_mremap(unsigned long addr,
416 unsigned long old_len, unsigned long new_len,
417 unsigned long flags, unsigned long new_addr)
421 down_write(¤t->mm->mmap_sem);
422 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
423 up_write(¤t->mm->mmap_sem);