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>
20 #include <asm/uaccess.h>
21 #include <asm/pgalloc.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
25 static pte_t *get_one_pte_map_nested(struct mm_struct *mm, unsigned long addr)
31 pgd = pgd_offset(mm, addr);
40 pmd = pmd_offset(pgd, addr);
49 pte = pte_offset_map_nested(pmd, addr);
51 pte_unmap_nested(pte);
58 static inline int page_table_present(struct mm_struct *mm, unsigned long addr)
63 pgd = pgd_offset(mm, addr);
66 pmd = pmd_offset(pgd, addr);
67 return pmd_present(*pmd);
70 static inline pte_t *alloc_one_pte_map(struct mm_struct *mm, unsigned long addr)
75 pmd = pmd_alloc(mm, pgd_offset(mm, addr), addr);
77 pte = pte_alloc_map(mm, pmd, addr);
82 copy_one_pte(struct vm_area_struct *vma, unsigned long old_addr,
83 pte_t *src, pte_t *dst)
87 struct page *page = NULL;
89 if (pte_present(*src))
90 page = pte_page(*src);
92 if (!pte_none(*src)) {
93 pte = ptep_clear_flush(vma, old_addr, src);
95 /* No dest? We must put it back. */
105 move_one_page(struct vm_area_struct *vma, unsigned long old_addr,
106 unsigned long new_addr)
108 struct mm_struct *mm = vma->vm_mm;
112 spin_lock(&mm->page_table_lock);
113 src = get_one_pte_map_nested(mm, old_addr);
116 * Look to see whether alloc_one_pte_map needs to perform a
117 * memory allocation. If it does then we need to drop the
120 if (!page_table_present(mm, new_addr)) {
121 pte_unmap_nested(src);
124 dst = alloc_one_pte_map(mm, new_addr);
126 src = get_one_pte_map_nested(mm, old_addr);
128 * Since alloc_one_pte_map can drop and re-acquire
129 * page_table_lock, we should re-check the src entry...
132 error = copy_one_pte(vma, old_addr, src, dst);
133 pte_unmap_nested(src);
137 spin_unlock(&mm->page_table_lock);
141 static int move_page_tables(struct vm_area_struct *vma,
142 unsigned long new_addr, unsigned long old_addr, unsigned long len)
144 unsigned long offset = len;
146 flush_cache_range(vma, old_addr, old_addr + len);
149 * This is not the clever way to do this, but we're taking the
150 * easy way out on the assumption that most remappings will be
151 * only a few pages.. This also makes error recovery easier.
155 if (move_one_page(vma, old_addr + offset, new_addr + offset))
161 * Ok, the move failed because we didn't have enough pages for
162 * the new page table tree. This is unlikely, but we have to
163 * take the possibility into account. In that case we just move
164 * all the pages back (this will work, because we still have
165 * the old page tables)
168 flush_cache_range(vma, new_addr, new_addr + len);
169 while ((offset += PAGE_SIZE) < len)
170 move_one_page(vma, new_addr + offset, old_addr + offset);
171 zap_page_range(vma, new_addr, len);
175 static unsigned long move_vma(struct vm_area_struct *vma,
176 unsigned long addr, unsigned long old_len, unsigned long new_len,
177 unsigned long new_addr)
179 #if VMA_MERGING_FIXUP
180 struct mm_struct *mm = vma->vm_mm;
181 struct vm_area_struct *new_vma, *next, *prev;
183 struct vm_area_struct *new_vma;
189 #if VMA_MERGING_FIXUP
190 next = find_vma_prev(mm, new_addr, &prev);
192 if (prev && prev->vm_end == new_addr &&
193 mpol_equal(prev->vm_policy, next->vm_policy) &&
194 can_vma_merge(prev, vma->vm_flags) && !vma->vm_file &&
195 !(vma->vm_flags & VM_SHARED)) {
196 spin_lock(&mm->page_table_lock);
197 prev->vm_end = new_addr + new_len;
198 spin_unlock(&mm->page_table_lock);
200 if (next != prev->vm_next)
202 if (prev->vm_end == next->vm_start &&
203 mpol_equal(next->vm_policy, prev->vm_policy) &&
204 can_vma_merge(next, prev->vm_flags)) {
205 spin_lock(&mm->page_table_lock);
206 prev->vm_end = next->vm_end;
207 __vma_unlink(mm, next, prev);
208 spin_unlock(&mm->page_table_lock);
212 mpol_free(next->vm_policy);
213 kmem_cache_free(vm_area_cachep, next);
215 } else if (next->vm_start == new_addr + new_len &&
216 can_vma_merge(next, vma->vm_flags) &&
217 mpol_equal(next->vm_policy, vma->vm_policy) &&
218 !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
219 spin_lock(&mm->page_table_lock);
220 next->vm_start = new_addr;
221 spin_unlock(&mm->page_table_lock);
225 prev = find_vma(mm, new_addr-1);
226 if (prev && prev->vm_end == new_addr &&
227 mpol_equal(prev->vm_policy, vma->vm_policy) &&
228 can_vma_merge(prev, vma->vm_flags) && !vma->vm_file &&
229 !(vma->vm_flags & VM_SHARED)) {
230 spin_lock(&mm->page_table_lock);
231 prev->vm_end = new_addr + new_len;
232 spin_unlock(&mm->page_table_lock);
240 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
246 if (!move_page_tables(vma, new_addr, addr, old_len)) {
247 unsigned long vm_locked = vma->vm_flags & VM_LOCKED;
251 new_vma->vm_policy =mpol_copy(new_vma->vm_policy);
252 if (IS_ERR(new_vma->vm_policy))
254 INIT_VMA_SHARED(new_vma);
255 new_vma->vm_start = new_addr;
256 new_vma->vm_end = new_addr+new_len;
257 new_vma->vm_pgoff += (addr-vma->vm_start) >> PAGE_SHIFT;
258 if (new_vma->vm_file)
259 get_file(new_vma->vm_file);
260 if (new_vma->vm_ops && new_vma->vm_ops->open)
261 new_vma->vm_ops->open(new_vma);
262 insert_vm_struct(current->mm, new_vma);
265 /* Conceal VM_ACCOUNT so old reservation is not undone */
266 if (vma->vm_flags & VM_ACCOUNT) {
267 vma->vm_flags &= ~VM_ACCOUNT;
268 if (addr > vma->vm_start) {
269 if (addr + old_len < vma->vm_end)
271 } else if (addr + old_len == vma->vm_end)
272 vma = NULL; /* it will be removed */
274 vma = NULL; /* nothing more to do */
276 do_munmap(current->mm, addr, old_len);
278 /* Restore VM_ACCOUNT if one or two pieces of vma left */
280 vma->vm_flags |= VM_ACCOUNT;
282 vma->vm_next->vm_flags |= VM_ACCOUNT;
285 current->mm->total_vm += new_len >> PAGE_SHIFT;
287 current->mm->locked_vm += new_len >> PAGE_SHIFT;
288 if (new_len > old_len)
289 make_pages_present(new_addr + old_len,
296 kmem_cache_free(vm_area_cachep, new_vma);
302 * Expand (or shrink) an existing mapping, potentially moving it at the
303 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
305 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
306 * This option implies MREMAP_MAYMOVE.
308 unsigned long do_mremap(unsigned long addr,
309 unsigned long old_len, unsigned long new_len,
310 unsigned long flags, unsigned long new_addr)
312 struct vm_area_struct *vma;
313 struct address_space *mapping = NULL;
314 struct prio_tree_root *root = NULL;
315 unsigned long ret = -EINVAL;
316 unsigned long charged = 0;
318 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
321 if (addr & ~PAGE_MASK)
324 old_len = PAGE_ALIGN(old_len);
325 new_len = PAGE_ALIGN(new_len);
328 * We allow a zero old-len as a special case
329 * for DOS-emu "duplicate shm area" thing. But
330 * a zero new-len is nonsensical.
335 /* new_addr is only valid if MREMAP_FIXED is specified */
336 if (flags & MREMAP_FIXED) {
337 if (new_addr & ~PAGE_MASK)
339 if (!(flags & MREMAP_MAYMOVE))
342 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
345 /* Check if the location we're moving into overlaps the
346 * old location at all, and fail if it does.
348 if ((new_addr <= addr) && (new_addr+new_len) > addr)
351 if ((addr <= new_addr) && (addr+old_len) > new_addr)
354 ret = do_munmap(current->mm, new_addr, new_len);
360 * Always allow a shrinking remap: that just unmaps
361 * the unnecessary pages..
362 * do_munmap does all the needed commit accounting
364 if (old_len >= new_len) {
365 ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
366 if (ret && old_len != new_len)
369 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
375 * Ok, we need to grow.. or relocate.
378 vma = find_vma(current->mm, addr);
379 if (!vma || vma->vm_start > addr)
381 if (is_vm_hugetlb_page(vma)) {
385 /* We can't remap across vm area boundaries */
386 if (old_len > vma->vm_end - addr)
388 if (vma->vm_flags & VM_DONTEXPAND) {
389 if (new_len > old_len)
392 if (vma->vm_flags & VM_LOCKED) {
393 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
394 locked += new_len - old_len;
396 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
400 if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
401 > current->rlim[RLIMIT_AS].rlim_cur)
404 if (vma->vm_flags & VM_ACCOUNT) {
405 charged = (new_len - old_len) >> PAGE_SHIFT;
406 if (security_vm_enough_memory(charged))
410 /* old_len exactly to the end of the area..
411 * And we're not relocating the area.
413 if (old_len == vma->vm_end - addr &&
414 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
415 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
416 unsigned long max_addr = TASK_SIZE;
418 max_addr = vma->vm_next->vm_start;
419 /* can we just expand the current mapping? */
420 if (max_addr - addr >= new_len) {
421 int pages = (new_len - old_len) >> PAGE_SHIFT;
424 mapping = vma->vm_file->f_mapping;
425 if (vma->vm_flags & VM_SHARED) {
426 if (likely(!(vma->vm_flags & VM_NONLINEAR)))
427 root = &mapping->i_mmap_shared;
430 root = &mapping->i_mmap;
431 down(&mapping->i_shared_sem);
434 spin_lock(&vma->vm_mm->page_table_lock);
435 __vma_modify(root, vma, vma->vm_start,
436 addr + new_len, vma->vm_pgoff);
437 spin_unlock(&vma->vm_mm->page_table_lock);
440 up(&mapping->i_shared_sem);
442 current->mm->total_vm += pages;
443 if (vma->vm_flags & VM_LOCKED) {
444 current->mm->locked_vm += pages;
445 make_pages_present(addr + old_len,
454 * We weren't able to just expand or shrink the area,
455 * we need to create a new one and move it..
458 if (flags & MREMAP_MAYMOVE) {
459 if (!(flags & MREMAP_FIXED)) {
460 unsigned long map_flags = 0;
461 if (vma->vm_flags & VM_MAYSHARE)
462 map_flags |= MAP_SHARED;
464 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
465 vma->vm_pgoff, map_flags);
467 if (new_addr & ~PAGE_MASK)
470 ret = move_vma(vma, addr, old_len, new_len, new_addr);
473 if (ret & ~PAGE_MASK)
474 vm_unacct_memory(charged);
479 asmlinkage unsigned long sys_mremap(unsigned long addr,
480 unsigned long old_len, unsigned long new_len,
481 unsigned long flags, unsigned long new_addr)
485 down_write(¤t->mm->mmap_sem);
486 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
487 up_write(¤t->mm->mmap_sem);