ia64/linux-2.6.18-xen.hg

annotate mm/mremap.c @ 871:9cbcc9008446

xen/x86: don't initialize cpu_data[]'s apicid field on generic code

Afaict, this is not only redundant with the intialization done in
drivers/xen/core/smpboot.c, but actually results - at least for
secondary CPUs - in the Xen-specific value written to be later
overwritten with whatever the generic code determines (with no
guarantee that the two values are identical).

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 14 10:09:15 2009 +0100 (2009-05-14)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * mm/mremap.c
ian@0 3 *
ian@0 4 * (C) Copyright 1996 Linus Torvalds
ian@0 5 *
ian@0 6 * Address space accounting code <alan@redhat.com>
ian@0 7 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
ian@0 8 */
ian@0 9
ian@0 10 #include <linux/mm.h>
ian@0 11 #include <linux/hugetlb.h>
ian@0 12 #include <linux/slab.h>
ian@0 13 #include <linux/shm.h>
ian@0 14 #include <linux/mman.h>
ian@0 15 #include <linux/swap.h>
ian@0 16 #include <linux/capability.h>
ian@0 17 #include <linux/fs.h>
ian@0 18 #include <linux/highmem.h>
ian@0 19 #include <linux/security.h>
ian@0 20 #include <linux/syscalls.h>
ian@0 21
ian@0 22 #include <asm/uaccess.h>
ian@0 23 #include <asm/cacheflush.h>
ian@0 24 #include <asm/tlbflush.h>
ian@0 25
ian@0 26 static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
ian@0 27 {
ian@0 28 pgd_t *pgd;
ian@0 29 pud_t *pud;
ian@0 30 pmd_t *pmd;
ian@0 31
ian@0 32 pgd = pgd_offset(mm, addr);
ian@0 33 if (pgd_none_or_clear_bad(pgd))
ian@0 34 return NULL;
ian@0 35
ian@0 36 pud = pud_offset(pgd, addr);
ian@0 37 if (pud_none_or_clear_bad(pud))
ian@0 38 return NULL;
ian@0 39
ian@0 40 pmd = pmd_offset(pud, addr);
ian@0 41 if (pmd_none_or_clear_bad(pmd))
ian@0 42 return NULL;
ian@0 43
ian@0 44 return pmd;
ian@0 45 }
ian@0 46
ian@0 47 static pmd_t *alloc_new_pmd(struct mm_struct *mm, unsigned long addr)
ian@0 48 {
ian@0 49 pgd_t *pgd;
ian@0 50 pud_t *pud;
ian@0 51 pmd_t *pmd;
ian@0 52
ian@0 53 pgd = pgd_offset(mm, addr);
ian@0 54 pud = pud_alloc(mm, pgd, addr);
ian@0 55 if (!pud)
ian@0 56 return NULL;
ian@0 57
ian@0 58 pmd = pmd_alloc(mm, pud, addr);
ian@0 59 if (!pmd)
ian@0 60 return NULL;
ian@0 61
ian@0 62 if (!pmd_present(*pmd) && __pte_alloc(mm, pmd, addr))
ian@0 63 return NULL;
ian@0 64
ian@0 65 return pmd;
ian@0 66 }
ian@0 67
ian@0 68 static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
ian@0 69 unsigned long old_addr, unsigned long old_end,
ian@0 70 struct vm_area_struct *new_vma, pmd_t *new_pmd,
ian@0 71 unsigned long new_addr)
ian@0 72 {
ian@0 73 struct address_space *mapping = NULL;
ian@0 74 struct mm_struct *mm = vma->vm_mm;
ian@0 75 pte_t *old_pte, *new_pte, pte;
ian@0 76 spinlock_t *old_ptl, *new_ptl;
ian@0 77
ian@0 78 if (vma->vm_file) {
ian@0 79 /*
ian@0 80 * Subtle point from Rajesh Venkatasubramanian: before
ian@0 81 * moving file-based ptes, we must lock vmtruncate out,
ian@0 82 * since it might clean the dst vma before the src vma,
ian@0 83 * and we propagate stale pages into the dst afterward.
ian@0 84 */
ian@0 85 mapping = vma->vm_file->f_mapping;
ian@0 86 spin_lock(&mapping->i_mmap_lock);
ian@0 87 if (new_vma->vm_truncate_count &&
ian@0 88 new_vma->vm_truncate_count != vma->vm_truncate_count)
ian@0 89 new_vma->vm_truncate_count = 0;
ian@0 90 }
ian@0 91
ian@0 92 /*
ian@0 93 * We don't have to worry about the ordering of src and dst
ian@0 94 * pte locks because exclusive mmap_sem prevents deadlock.
ian@0 95 */
ian@0 96 old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
ian@0 97 new_pte = pte_offset_map_nested(new_pmd, new_addr);
ian@0 98 new_ptl = pte_lockptr(mm, new_pmd);
ian@0 99 if (new_ptl != old_ptl)
ian@0 100 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
ian@0 101
ian@0 102 for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
ian@0 103 new_pte++, new_addr += PAGE_SIZE) {
ian@0 104 if (pte_none(*old_pte))
ian@0 105 continue;
ian@0 106 pte = ptep_clear_flush(vma, old_addr, old_pte);
ian@0 107 /* ZERO_PAGE can be dependant on virtual addr */
ian@0 108 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
ian@0 109 set_pte_at(mm, new_addr, new_pte, pte);
ian@0 110 }
ian@0 111
ian@0 112 if (new_ptl != old_ptl)
ian@0 113 spin_unlock(new_ptl);
ian@0 114 pte_unmap_nested(new_pte - 1);
ian@0 115 pte_unmap_unlock(old_pte - 1, old_ptl);
ian@0 116 if (mapping)
ian@0 117 spin_unlock(&mapping->i_mmap_lock);
ian@0 118 }
ian@0 119
ian@0 120 #define LATENCY_LIMIT (64 * PAGE_SIZE)
ian@0 121
ian@0 122 static unsigned long move_page_tables(struct vm_area_struct *vma,
ian@0 123 unsigned long old_addr, struct vm_area_struct *new_vma,
ian@0 124 unsigned long new_addr, unsigned long len)
ian@0 125 {
ian@0 126 unsigned long extent, next, old_end;
ian@0 127 pmd_t *old_pmd, *new_pmd;
ian@0 128
ian@0 129 old_end = old_addr + len;
ian@0 130 flush_cache_range(vma, old_addr, old_end);
ian@0 131
ian@0 132 for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
ian@0 133 cond_resched();
ian@0 134 next = (old_addr + PMD_SIZE) & PMD_MASK;
ian@0 135 if (next - 1 > old_end)
ian@0 136 next = old_end;
ian@0 137 extent = next - old_addr;
ian@0 138 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
ian@0 139 if (!old_pmd)
ian@0 140 continue;
ian@0 141 new_pmd = alloc_new_pmd(vma->vm_mm, new_addr);
ian@0 142 if (!new_pmd)
ian@0 143 break;
ian@0 144 next = (new_addr + PMD_SIZE) & PMD_MASK;
ian@0 145 if (extent > next - new_addr)
ian@0 146 extent = next - new_addr;
ian@0 147 if (extent > LATENCY_LIMIT)
ian@0 148 extent = LATENCY_LIMIT;
ian@0 149 move_ptes(vma, old_pmd, old_addr, old_addr + extent,
ian@0 150 new_vma, new_pmd, new_addr);
ian@0 151 }
ian@0 152
ian@0 153 return len + old_addr - old_end; /* how much done */
ian@0 154 }
ian@0 155
ian@0 156 static unsigned long move_vma(struct vm_area_struct *vma,
ian@0 157 unsigned long old_addr, unsigned long old_len,
ian@0 158 unsigned long new_len, unsigned long new_addr)
ian@0 159 {
ian@0 160 struct mm_struct *mm = vma->vm_mm;
ian@0 161 struct vm_area_struct *new_vma;
ian@0 162 unsigned long vm_flags = vma->vm_flags;
ian@0 163 unsigned long new_pgoff;
ian@0 164 unsigned long moved_len;
ian@0 165 unsigned long excess = 0;
ian@0 166 unsigned long hiwater_vm;
ian@0 167 int split = 0;
ian@0 168
ian@0 169 /*
ian@0 170 * We'd prefer to avoid failure later on in do_munmap:
ian@0 171 * which may split one vma into three before unmapping.
ian@0 172 */
ian@0 173 if (mm->map_count >= sysctl_max_map_count - 3)
ian@0 174 return -ENOMEM;
ian@0 175
ian@0 176 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
ian@0 177 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
ian@0 178 if (!new_vma)
ian@0 179 return -ENOMEM;
ian@0 180
ian@0 181 moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
ian@0 182 if (moved_len < old_len) {
ian@0 183 /*
ian@0 184 * On error, move entries back from new area to old,
ian@0 185 * which will succeed since page tables still there,
ian@0 186 * and then proceed to unmap new area instead of old.
ian@0 187 */
ian@0 188 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
ian@0 189 vma = new_vma;
ian@0 190 old_len = new_len;
ian@0 191 old_addr = new_addr;
ian@0 192 new_addr = -ENOMEM;
ian@0 193 }
ian@0 194
ian@0 195 /* Conceal VM_ACCOUNT so old reservation is not undone */
ian@0 196 if (vm_flags & VM_ACCOUNT) {
ian@0 197 vma->vm_flags &= ~VM_ACCOUNT;
ian@0 198 excess = vma->vm_end - vma->vm_start - old_len;
ian@0 199 if (old_addr > vma->vm_start &&
ian@0 200 old_addr + old_len < vma->vm_end)
ian@0 201 split = 1;
ian@0 202 }
ian@0 203
ian@0 204 /*
ian@0 205 * If we failed to move page tables we still do total_vm increment
ian@0 206 * since do_munmap() will decrement it by old_len == new_len.
ian@0 207 *
ian@0 208 * Since total_vm is about to be raised artificially high for a
ian@0 209 * moment, we need to restore high watermark afterwards: if stats
ian@0 210 * are taken meanwhile, total_vm and hiwater_vm appear too high.
ian@0 211 * If this were a serious issue, we'd add a flag to do_munmap().
ian@0 212 */
ian@0 213 hiwater_vm = mm->hiwater_vm;
ian@0 214 mm->total_vm += new_len >> PAGE_SHIFT;
ian@0 215 vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
ian@0 216
ian@0 217 if (do_munmap(mm, old_addr, old_len) < 0) {
ian@0 218 /* OOM: unable to split vma, just get accounts right */
ian@0 219 vm_unacct_memory(excess >> PAGE_SHIFT);
ian@0 220 excess = 0;
ian@0 221 }
ian@0 222 mm->hiwater_vm = hiwater_vm;
ian@0 223
ian@0 224 /* Restore VM_ACCOUNT if one or two pieces of vma left */
ian@0 225 if (excess) {
ian@0 226 vma->vm_flags |= VM_ACCOUNT;
ian@0 227 if (split)
ian@0 228 vma->vm_next->vm_flags |= VM_ACCOUNT;
ian@0 229 }
ian@0 230
ian@0 231 if (vm_flags & VM_LOCKED) {
ian@0 232 mm->locked_vm += new_len >> PAGE_SHIFT;
ian@0 233 if (new_len > old_len)
ian@0 234 make_pages_present(new_addr + old_len,
ian@0 235 new_addr + new_len);
ian@0 236 }
ian@0 237
ian@0 238 return new_addr;
ian@0 239 }
ian@0 240
ian@0 241 /*
ian@0 242 * Expand (or shrink) an existing mapping, potentially moving it at the
ian@0 243 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
ian@0 244 *
ian@0 245 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
ian@0 246 * This option implies MREMAP_MAYMOVE.
ian@0 247 */
ian@0 248 unsigned long do_mremap(unsigned long addr,
ian@0 249 unsigned long old_len, unsigned long new_len,
ian@0 250 unsigned long flags, unsigned long new_addr)
ian@0 251 {
ian@0 252 struct mm_struct *mm = current->mm;
ian@0 253 struct vm_area_struct *vma;
ian@0 254 unsigned long ret = -EINVAL;
ian@0 255 unsigned long charged = 0;
ian@0 256
ian@0 257 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
ian@0 258 goto out;
ian@0 259
ian@0 260 if (addr & ~PAGE_MASK)
ian@0 261 goto out;
ian@0 262
ian@0 263 old_len = PAGE_ALIGN(old_len);
ian@0 264 new_len = PAGE_ALIGN(new_len);
ian@0 265
ian@0 266 /*
ian@0 267 * We allow a zero old-len as a special case
ian@0 268 * for DOS-emu "duplicate shm area" thing. But
ian@0 269 * a zero new-len is nonsensical.
ian@0 270 */
ian@0 271 if (!new_len)
ian@0 272 goto out;
ian@0 273
ian@0 274 /* new_addr is only valid if MREMAP_FIXED is specified */
ian@0 275 if (flags & MREMAP_FIXED) {
ian@0 276 if (new_addr & ~PAGE_MASK)
ian@0 277 goto out;
ian@0 278 if (!(flags & MREMAP_MAYMOVE))
ian@0 279 goto out;
ian@0 280
ian@0 281 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
ian@0 282 goto out;
ian@0 283
ian@0 284 /* Check if the location we're moving into overlaps the
ian@0 285 * old location at all, and fail if it does.
ian@0 286 */
ian@0 287 if ((new_addr <= addr) && (new_addr+new_len) > addr)
ian@0 288 goto out;
ian@0 289
ian@0 290 if ((addr <= new_addr) && (addr+old_len) > new_addr)
ian@0 291 goto out;
ian@0 292
ian@0 293 ret = do_munmap(mm, new_addr, new_len);
ian@0 294 if (ret)
ian@0 295 goto out;
ian@0 296 }
ian@0 297
ian@0 298 /*
ian@0 299 * Always allow a shrinking remap: that just unmaps
ian@0 300 * the unnecessary pages..
ian@0 301 * do_munmap does all the needed commit accounting
ian@0 302 */
ian@0 303 if (old_len >= new_len) {
ian@0 304 ret = do_munmap(mm, addr+new_len, old_len - new_len);
ian@0 305 if (ret && old_len != new_len)
ian@0 306 goto out;
ian@0 307 ret = addr;
ian@0 308 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
ian@0 309 goto out;
ian@0 310 old_len = new_len;
ian@0 311 }
ian@0 312
ian@0 313 /*
ian@0 314 * Ok, we need to grow.. or relocate.
ian@0 315 */
ian@0 316 ret = -EFAULT;
ian@0 317 vma = find_vma(mm, addr);
ian@0 318 if (!vma || vma->vm_start > addr)
ian@0 319 goto out;
ian@0 320 if (is_vm_hugetlb_page(vma)) {
ian@0 321 ret = -EINVAL;
ian@0 322 goto out;
ian@0 323 }
ian@0 324 /* We can't remap across vm area boundaries */
ian@0 325 if (old_len > vma->vm_end - addr)
ian@0 326 goto out;
ian@0 327 if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) {
ian@0 328 if (new_len > old_len)
ian@0 329 goto out;
ian@0 330 }
ian@0 331 if (vma->vm_flags & VM_LOCKED) {
ian@0 332 unsigned long locked, lock_limit;
ian@0 333 locked = mm->locked_vm << PAGE_SHIFT;
ian@0 334 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
ian@0 335 locked += new_len - old_len;
ian@0 336 ret = -EAGAIN;
ian@0 337 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
ian@0 338 goto out;
ian@0 339 }
ian@0 340 if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT)) {
ian@0 341 ret = -ENOMEM;
ian@0 342 goto out;
ian@0 343 }
ian@0 344
ian@0 345 if (vma->vm_flags & VM_ACCOUNT) {
ian@0 346 charged = (new_len - old_len) >> PAGE_SHIFT;
ian@0 347 if (security_vm_enough_memory(charged))
ian@0 348 goto out_nc;
ian@0 349 }
ian@0 350
ian@0 351 /* old_len exactly to the end of the area..
ian@0 352 * And we're not relocating the area.
ian@0 353 */
ian@0 354 if (old_len == vma->vm_end - addr &&
ian@0 355 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
ian@0 356 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
ian@0 357 unsigned long max_addr = TASK_SIZE;
ian@0 358 if (vma->vm_next)
ian@0 359 max_addr = vma->vm_next->vm_start;
ian@0 360 /* can we just expand the current mapping? */
ian@0 361 if (max_addr - addr >= new_len) {
ian@0 362 int pages = (new_len - old_len) >> PAGE_SHIFT;
ian@0 363
ian@0 364 vma_adjust(vma, vma->vm_start,
ian@0 365 addr + new_len, vma->vm_pgoff, NULL);
ian@0 366
ian@0 367 mm->total_vm += pages;
ian@0 368 vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
ian@0 369 if (vma->vm_flags & VM_LOCKED) {
ian@0 370 mm->locked_vm += pages;
ian@0 371 make_pages_present(addr + old_len,
ian@0 372 addr + new_len);
ian@0 373 }
ian@0 374 ret = addr;
ian@0 375 goto out;
ian@0 376 }
ian@0 377 }
ian@0 378
ian@0 379 /*
ian@0 380 * We weren't able to just expand or shrink the area,
ian@0 381 * we need to create a new one and move it..
ian@0 382 */
ian@0 383 ret = -ENOMEM;
ian@0 384 if (flags & MREMAP_MAYMOVE) {
ian@0 385 if (!(flags & MREMAP_FIXED)) {
ian@0 386 unsigned long map_flags = 0;
ian@0 387 if (vma->vm_flags & VM_MAYSHARE)
ian@0 388 map_flags |= MAP_SHARED;
ian@0 389
ian@0 390 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
ian@0 391 vma->vm_pgoff, map_flags);
ian@0 392 ret = new_addr;
ian@0 393 if (new_addr & ~PAGE_MASK)
ian@0 394 goto out;
ian@0 395 }
ian@0 396 ret = move_vma(vma, addr, old_len, new_len, new_addr);
ian@0 397 }
ian@0 398 out:
ian@0 399 if (ret & ~PAGE_MASK)
ian@0 400 vm_unacct_memory(charged);
ian@0 401 out_nc:
ian@0 402 return ret;
ian@0 403 }
ian@0 404
ian@0 405 asmlinkage unsigned long sys_mremap(unsigned long addr,
ian@0 406 unsigned long old_len, unsigned long new_len,
ian@0 407 unsigned long flags, unsigned long new_addr)
ian@0 408 {
ian@0 409 unsigned long ret;
ian@0 410
ian@0 411 down_write(&current->mm->mmap_sem);
ian@0 412 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
ian@0 413 up_write(&current->mm->mmap_sem);
ian@0 414 return ret;
ian@0 415 }