linux-old/mm/mremap.c
<<
>>
Prefs
   1/*
   2 *      linux/mm/remap.c
   3 *
   4 *      (C) Copyright 1996 Linus Torvalds
   5 */
   6
   7#include <linux/slab.h>
   8#include <linux/smp_lock.h>
   9#include <linux/shm.h>
  10#include <linux/mman.h>
  11#include <linux/swap.h>
  12
  13#include <asm/uaccess.h>
  14#include <asm/pgtable.h>
  15
  16extern int vm_enough_memory(long pages);
  17
  18static inline pte_t *get_one_pte(struct mm_struct *mm, unsigned long addr)
  19{
  20        pgd_t * pgd;
  21        pmd_t * pmd;
  22        pte_t * pte = NULL;
  23
  24        pgd = pgd_offset(mm, addr);
  25        if (pgd_none(*pgd))
  26                goto end;
  27        if (pgd_bad(*pgd)) {
  28                printk("move_one_page: bad source pgd (%08lx)\n", pgd_val(*pgd));
  29                pgd_clear(pgd);
  30                goto end;
  31        }
  32
  33        pmd = pmd_offset(pgd, addr);
  34        if (pmd_none(*pmd))
  35                goto end;
  36        if (pmd_bad(*pmd)) {
  37                printk("move_one_page: bad source pmd (%08lx)\n", pmd_val(*pmd));
  38                pmd_clear(pmd);
  39                goto end;
  40        }
  41
  42        pte = pte_offset(pmd, addr);
  43        if (pte_none(*pte))
  44                pte = NULL;
  45end:
  46        return pte;
  47}
  48
  49static inline pte_t *alloc_one_pte(struct mm_struct *mm, unsigned long addr)
  50{
  51        pmd_t * pmd;
  52        pte_t * pte = NULL;
  53
  54        pmd = pmd_alloc(pgd_offset(mm, addr), addr);
  55        if (pmd)
  56                pte = pte_alloc(pmd, addr);
  57        return pte;
  58}
  59
  60static inline int copy_one_pte(pte_t * src, pte_t * dst)
  61{
  62        int error = 0;
  63        pte_t pte = *src;
  64
  65        if (!pte_none(pte)) {
  66                error++;
  67                if (dst) {
  68                        pte_clear(src);
  69                        set_pte(dst, pte);
  70                        error--;
  71                }
  72        }
  73        return error;
  74}
  75
  76static int move_one_page(struct mm_struct *mm, unsigned long old_addr, unsigned long new_addr)
  77{
  78        int error = 0;
  79        pte_t * src;
  80
  81        src = get_one_pte(mm, old_addr);
  82        if (src)
  83                error = copy_one_pte(src, alloc_one_pte(mm, new_addr));
  84        return error;
  85}
  86
  87static int move_page_tables(struct mm_struct * mm,
  88        unsigned long new_addr, unsigned long old_addr, unsigned long len)
  89{
  90        unsigned long offset = len;
  91
  92        flush_cache_range(mm, old_addr, old_addr + len);
  93
  94        /*
  95         * This is not the clever way to do this, but we're taking the
  96         * easy way out on the assumption that most remappings will be
  97         * only a few pages.. This also makes error recovery easier.
  98         */
  99        while (offset) {
 100                offset -= PAGE_SIZE;
 101                if (move_one_page(mm, old_addr + offset, new_addr + offset))
 102                        goto oops_we_failed;
 103        }
 104        flush_tlb_range(mm, old_addr, old_addr + len);
 105        return 0;
 106
 107        /*
 108         * Ok, the move failed because we didn't have enough pages for
 109         * the new page table tree. This is unlikely, but we have to
 110         * take the possibility into account. In that case we just move
 111         * all the pages back (this will work, because we still have
 112         * the old page tables)
 113         */
 114oops_we_failed:
 115        flush_cache_range(mm, new_addr, new_addr + len);
 116        while ((offset += PAGE_SIZE) < len)
 117                move_one_page(mm, new_addr + offset, old_addr + offset);
 118        zap_page_range(mm, new_addr, len);
 119        flush_tlb_range(mm, new_addr, new_addr + len);
 120        return -1;
 121}
 122
 123static inline unsigned long move_vma(struct vm_area_struct * vma,
 124        unsigned long addr, unsigned long old_len, unsigned long new_len)
 125{
 126        struct vm_area_struct * new_vma;
 127
 128        new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
 129        if (new_vma) {
 130                unsigned long new_addr = get_unmapped_area(addr, new_len);
 131
 132                if (new_addr && !move_page_tables(current->mm, new_addr, addr, old_len)) {
 133                        *new_vma = *vma;
 134                        new_vma->vm_start = new_addr;
 135                        new_vma->vm_end = new_addr+new_len;
 136                        new_vma->vm_offset = vma->vm_offset + (addr - vma->vm_start);
 137                        if (new_vma->vm_file)
 138                                new_vma->vm_file->f_count++;
 139                        if (new_vma->vm_ops && new_vma->vm_ops->open)
 140                                new_vma->vm_ops->open(new_vma);
 141                        insert_vm_struct(current->mm, new_vma);
 142                        merge_segments(current->mm, new_vma->vm_start, new_vma->vm_end);
 143                        /* XXX: possible errors masked, mapping might remain */
 144                        do_munmap(addr, old_len);
 145                        current->mm->total_vm += new_len >> PAGE_SHIFT;
 146                        if (new_vma->vm_flags & VM_LOCKED) {
 147                                current->mm->locked_vm += new_len >> PAGE_SHIFT;
 148                                make_pages_present(new_vma->vm_start,
 149                                                   new_vma->vm_end);
 150                        }
 151                        return new_addr;
 152                }
 153                kmem_cache_free(vm_area_cachep, new_vma);
 154        }
 155        return -ENOMEM;
 156}
 157
 158/*
 159 * Expand (or shrink) an existing mapping, potentially moving it at the
 160 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
 161 */
 162asmlinkage unsigned long sys_mremap(unsigned long addr,
 163        unsigned long old_len, unsigned long new_len,
 164        unsigned long flags)
 165{
 166        struct vm_area_struct *vma;
 167        unsigned long ret = -EINVAL;
 168
 169        down(&current->mm->mmap_sem);
 170        lock_kernel();
 171        if (addr & ~PAGE_MASK)
 172                goto out;
 173        old_len = PAGE_ALIGN(old_len);
 174        new_len = PAGE_ALIGN(new_len);
 175
 176        if (old_len > TASK_SIZE || addr > TASK_SIZE - old_len)
 177                goto out;
 178
 179        if (addr >= TASK_SIZE)
 180                goto out;
 181
 182        /*
 183         * Always allow a shrinking remap: that just unmaps
 184         * the unnecessary pages..
 185         */
 186        if (old_len >= new_len) {
 187                ret = do_munmap(addr+new_len, old_len - new_len);
 188                if (!ret || old_len == new_len)
 189                        ret = addr;
 190                goto out;
 191        }
 192
 193        if (new_len > TASK_SIZE || addr > TASK_SIZE - new_len)
 194                goto out;
 195
 196        /*
 197         * Ok, we need to grow..
 198         */
 199        ret = -EFAULT;
 200        vma = find_vma(current->mm, addr);
 201        if (!vma || vma->vm_start > addr)
 202                goto out;
 203        /* We can't remap across vm area boundaries */
 204        if (old_len > vma->vm_end - addr)
 205                goto out;
 206        if (vma->vm_flags & VM_LOCKED) {
 207                unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
 208                locked += new_len - old_len;
 209                ret = -EAGAIN;
 210                if ((current->rlim[RLIMIT_MEMLOCK].rlim_cur < RLIM_INFINITY) &&
 211                   (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur))
 212                        goto out;
 213        }
 214        ret = -ENOMEM;
 215        if ((current->rlim[RLIMIT_AS].rlim_cur < RLIM_INFINITY) &&
 216            ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
 217            > current->rlim[RLIMIT_AS].rlim_cur))
 218                goto out;
 219        /* Private writable mapping? Check memory availability.. */
 220        if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) == VM_WRITE &&
 221            !(flags & MAP_NORESERVE)                             &&
 222            !vm_enough_memory((new_len - old_len) >> PAGE_SHIFT))
 223                goto out;
 224
 225        /* old_len exactly to the end of the area.. */
 226        if (old_len == vma->vm_end - addr &&
 227            (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
 228                unsigned long max_addr = TASK_SIZE;
 229                if (vma->vm_next)
 230                        max_addr = vma->vm_next->vm_start;
 231                /* can we just expand the current mapping? */
 232                if (max_addr - addr >= new_len) {
 233                        int pages = (new_len - old_len) >> PAGE_SHIFT;
 234                        vma->vm_end = addr + new_len;
 235                        current->mm->total_vm += pages;
 236                        if (vma->vm_flags & VM_LOCKED) {
 237                                current->mm->locked_vm += pages;
 238                                make_pages_present(addr + old_len,
 239                                                   addr + new_len);
 240                        }
 241                        ret = addr;
 242                        goto out;
 243                }
 244        }
 245
 246        /*
 247         * We weren't able to just expand or shrink the area,
 248         * we need to create a new one and move it..
 249         */
 250        if (flags & MREMAP_MAYMOVE)
 251                ret = move_vma(vma, addr, old_len, new_len);
 252        else
 253                ret = -ENOMEM;
 254out:
 255        unlock_kernel();
 256        up(&current->mm->mmap_sem);
 257        return ret;
 258}
 259
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.