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/rmap-locking.h>
19 #include <linux/security.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgalloc.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 inline int page_table_present(struct mm_struct *mm, unsigned long addr)
64 pgd = pgd_offset(mm, addr);
67 pmd = pmd_offset(pgd, addr);
68 return pmd_present(*pmd);
71 static inline pte_t *alloc_one_pte_map(struct mm_struct *mm, unsigned long addr)
76 pmd = pmd_alloc(mm, pgd_offset(mm, addr), addr);
78 pte = pte_alloc_map(mm, pmd, addr);
83 copy_one_pte(struct vm_area_struct *vma, unsigned long old_addr,
84 pte_t *src, pte_t *dst, struct pte_chain **pte_chainp)
86 pte_t pte = ptep_clear_flush(vma, old_addr, src);
89 if (pte_present(pte)) {
90 unsigned long pfn = pte_pfn(pte);
92 struct page *page = pfn_to_page(pfn);
93 page_remove_rmap(page, src);
94 *pte_chainp = page_add_rmap(page, dst, *pte_chainp);
100 move_one_page(struct vm_area_struct *vma, unsigned long old_addr,
101 unsigned long new_addr)
103 struct mm_struct *mm = vma->vm_mm;
106 struct pte_chain *pte_chain;
108 pte_chain = pte_chain_alloc(GFP_KERNEL);
113 spin_lock(&mm->page_table_lock);
114 src = get_one_pte_map_nested(mm, old_addr);
117 * Look to see whether alloc_one_pte_map needs to perform a
118 * memory allocation. If it does then we need to drop the
121 if (!page_table_present(mm, new_addr)) {
122 pte_unmap_nested(src);
125 dst = alloc_one_pte_map(mm, new_addr);
127 src = get_one_pte_map_nested(mm, old_addr);
129 * Since alloc_one_pte_map can drop and re-acquire
130 * page_table_lock, we should re-check the src entry...
134 copy_one_pte(vma, old_addr, src,
138 pte_unmap_nested(src);
142 spin_unlock(&mm->page_table_lock);
143 pte_chain_free(pte_chain);
148 static int move_page_tables(struct vm_area_struct *vma,
149 unsigned long new_addr, unsigned long old_addr, unsigned long len)
151 unsigned long offset = len;
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.
162 if (move_one_page(vma, old_addr + offset, new_addr + offset))
168 * Ok, the move failed because we didn't have enough pages for
169 * the new page table tree. This is unlikely, but we have to
170 * take the possibility into account. In that case we just move
171 * all the pages back (this will work, because we still have
172 * the old page tables)
175 flush_cache_range(vma, new_addr, new_addr + len);
176 while ((offset += PAGE_SIZE) < len)
177 move_one_page(vma, new_addr + offset, old_addr + offset);
178 zap_page_range(vma, new_addr, len);
182 static unsigned long move_vma(struct vm_area_struct *vma,
183 unsigned long addr, unsigned long old_len, unsigned long new_len,
184 unsigned long new_addr)
186 struct mm_struct *mm = vma->vm_mm;
187 struct vm_area_struct *new_vma, *next, *prev;
192 next = find_vma_prev(mm, new_addr, &prev);
194 if (prev && prev->vm_end == new_addr &&
195 can_vma_merge(prev, vma->vm_flags) && !vma->vm_file &&
196 !(vma->vm_flags & VM_SHARED)) {
197 spin_lock(&mm->page_table_lock);
198 prev->vm_end = new_addr + new_len;
199 spin_unlock(&mm->page_table_lock);
201 if (next != prev->vm_next)
203 if (prev->vm_end == next->vm_start &&
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 kmem_cache_free(vm_area_cachep, next);
214 } else if (next->vm_start == new_addr + new_len &&
215 can_vma_merge(next, vma->vm_flags) &&
216 !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
217 spin_lock(&mm->page_table_lock);
218 next->vm_start = new_addr;
219 spin_unlock(&mm->page_table_lock);
223 prev = find_vma(mm, new_addr-1);
224 if (prev && prev->vm_end == new_addr &&
225 can_vma_merge(prev, vma->vm_flags) && !vma->vm_file &&
226 !(vma->vm_flags & VM_SHARED)) {
227 spin_lock(&mm->page_table_lock);
228 prev->vm_end = new_addr + new_len;
229 spin_unlock(&mm->page_table_lock);
236 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
242 if (!move_page_tables(vma, new_addr, addr, old_len)) {
243 unsigned long vm_locked = vma->vm_flags & VM_LOCKED;
247 INIT_LIST_HEAD(&new_vma->shared);
248 new_vma->vm_start = new_addr;
249 new_vma->vm_end = new_addr+new_len;
250 new_vma->vm_pgoff += (addr-vma->vm_start) >> PAGE_SHIFT;
251 if (new_vma->vm_file)
252 get_file(new_vma->vm_file);
253 if (new_vma->vm_ops && new_vma->vm_ops->open)
254 new_vma->vm_ops->open(new_vma);
255 insert_vm_struct(current->mm, new_vma);
258 /* Conceal VM_ACCOUNT so old reservation is not undone */
259 if (vma->vm_flags & VM_ACCOUNT) {
260 vma->vm_flags &= ~VM_ACCOUNT;
261 if (addr > vma->vm_start) {
262 if (addr + old_len < vma->vm_end)
264 } else if (addr + old_len == vma->vm_end)
265 vma = NULL; /* it will be removed */
267 vma = NULL; /* nothing more to do */
269 do_munmap(current->mm, addr, old_len);
271 /* Restore VM_ACCOUNT if one or two pieces of vma left */
273 vma->vm_flags |= VM_ACCOUNT;
275 vma->vm_next->vm_flags |= VM_ACCOUNT;
278 current->mm->total_vm += new_len >> PAGE_SHIFT;
280 current->mm->locked_vm += new_len >> PAGE_SHIFT;
281 if (new_len > old_len)
282 make_pages_present(new_addr + old_len,
288 kmem_cache_free(vm_area_cachep, new_vma);
294 * Expand (or shrink) an existing mapping, potentially moving it at the
295 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
297 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
298 * This option implies MREMAP_MAYMOVE.
300 unsigned long do_mremap(unsigned long addr,
301 unsigned long old_len, unsigned long new_len,
302 unsigned long flags, unsigned long new_addr)
304 struct vm_area_struct *vma;
305 unsigned long ret = -EINVAL;
306 unsigned long charged = 0;
308 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
311 if (addr & ~PAGE_MASK)
314 old_len = PAGE_ALIGN(old_len);
315 new_len = PAGE_ALIGN(new_len);
318 * We allow a zero old-len as a special case
319 * for DOS-emu "duplicate shm area" thing. But
320 * a zero new-len is nonsensical.
325 /* new_addr is only valid if MREMAP_FIXED is specified */
326 if (flags & MREMAP_FIXED) {
327 if (new_addr & ~PAGE_MASK)
329 if (!(flags & MREMAP_MAYMOVE))
332 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
335 /* Check if the location we're moving into overlaps the
336 * old location at all, and fail if it does.
338 if ((new_addr <= addr) && (new_addr+new_len) > addr)
341 if ((addr <= new_addr) && (addr+old_len) > new_addr)
344 ret = do_munmap(current->mm, new_addr, new_len);
350 * Always allow a shrinking remap: that just unmaps
351 * the unnecessary pages..
352 * do_munmap does all the needed commit accounting
354 if (old_len >= new_len) {
355 ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
356 if (ret && old_len != new_len)
359 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
365 * Ok, we need to grow.. or relocate.
368 vma = find_vma(current->mm, addr);
369 if (!vma || vma->vm_start > addr)
371 if (is_vm_hugetlb_page(vma)) {
375 /* We can't remap across vm area boundaries */
376 if (old_len > vma->vm_end - addr)
378 if (vma->vm_flags & VM_DONTEXPAND) {
379 if (new_len > old_len)
382 if (vma->vm_flags & VM_LOCKED) {
383 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
384 locked += new_len - old_len;
386 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
390 if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
391 > current->rlim[RLIMIT_AS].rlim_cur)
394 if (vma->vm_flags & VM_ACCOUNT) {
395 charged = (new_len - old_len) >> PAGE_SHIFT;
396 if (security_vm_enough_memory(charged))
400 /* old_len exactly to the end of the area..
401 * And we're not relocating the area.
403 if (old_len == vma->vm_end - addr &&
404 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
405 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
406 unsigned long max_addr = TASK_SIZE;
408 max_addr = vma->vm_next->vm_start;
409 /* can we just expand the current mapping? */
410 if (max_addr - addr >= new_len) {
411 int pages = (new_len - old_len) >> PAGE_SHIFT;
412 spin_lock(&vma->vm_mm->page_table_lock);
413 vma->vm_end = addr + new_len;
414 spin_unlock(&vma->vm_mm->page_table_lock);
415 current->mm->total_vm += pages;
416 if (vma->vm_flags & VM_LOCKED) {
417 current->mm->locked_vm += pages;
418 make_pages_present(addr + old_len,
427 * We weren't able to just expand or shrink the area,
428 * we need to create a new one and move it..
431 if (flags & MREMAP_MAYMOVE) {
432 if (!(flags & MREMAP_FIXED)) {
433 unsigned long map_flags = 0;
434 if (vma->vm_flags & VM_MAYSHARE)
435 map_flags |= MAP_SHARED;
437 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
438 vma->vm_pgoff, map_flags);
440 if (new_addr & ~PAGE_MASK)
443 ret = move_vma(vma, addr, old_len, new_len, new_addr);
446 if (ret & ~PAGE_MASK)
447 vm_unacct_memory(charged);
452 asmlinkage unsigned long sys_mremap(unsigned long addr,
453 unsigned long old_len, unsigned long new_len,
454 unsigned long flags, unsigned long new_addr)
458 down_write(¤t->mm->mmap_sem);
459 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
460 up_write(¤t->mm->mmap_sem);