1 /* 2 * linux/mm/nommu.c 3 * 4 * Replacement code for mm functions to support CPU's that don't 5 * have any form of memory management unit (thus no virtual memory). 6 * 7 * See Documentation/nommu-mmap.txt 8 * 9 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com> 10 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com> 11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org> 12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com> 13 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org> 14 */ 15 16 #include <linux/module.h> 17 #include <linux/mm.h> 18 #include <linux/mman.h> 19 #include <linux/swap.h> 20 #include <linux/file.h> 21 #include <linux/highmem.h> 22 #include <linux/pagemap.h> 23 #include <linux/slab.h> 24 #include <linux/vmalloc.h> 25 #include <linux/tracehook.h> 26 #include <linux/blkdev.h> 27 #include <linux/backing-dev.h> 28 #include <linux/mount.h> 29 #include <linux/personality.h> 30 #include <linux/security.h> 31 #include <linux/syscalls.h> 32 33 #include <asm/uaccess.h> 34 #include <asm/tlb.h> 35 #include <asm/tlbflush.h> 36 #include <asm/mmu_context.h> 37 #include "internal.h" 38 39 #if 0 40 #define kenter(FMT, ...) \ 41 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) 42 #define kleave(FMT, ...) \ 43 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) 44 #define kdebug(FMT, ...) \ 45 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__) 46 #else 47 #define kenter(FMT, ...) \ 48 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__) 49 #define kleave(FMT, ...) \ 50 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__) 51 #define kdebug(FMT, ...) \ 52 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__) 53 #endif 54 55 void *high_memory; 56 struct page *mem_map; 57 unsigned long max_mapnr; 58 unsigned long num_physpages; 59 unsigned long highest_memmap_pfn; 60 struct percpu_counter vm_committed_as; 61 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */ 62 int sysctl_overcommit_ratio = 50; /* default is 50% */ 63 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; 64 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS; 65 int heap_stack_gap = 0; 66 67 atomic_long_t mmap_pages_allocated; 68 69 EXPORT_SYMBOL(mem_map); 70 EXPORT_SYMBOL(num_physpages); 71 72 /* list of mapped, potentially shareable regions */ 73 static struct kmem_cache *vm_region_jar; 74 struct rb_root nommu_region_tree = RB_ROOT; 75 DECLARE_RWSEM(nommu_region_sem); 76 77 const struct vm_operations_struct generic_file_vm_ops = { 78 }; 79 80 /* 81 * Return the total memory allocated for this pointer, not 82 * just what the caller asked for. 83 * 84 * Doesn't have to be accurate, i.e. may have races. 85 */ 86 unsigned int kobjsize(const void *objp) 87 { 88 struct page *page; 89 90 /* 91 * If the object we have should not have ksize performed on it, 92 * return size of 0 93 */ 94 if (!objp || !virt_addr_valid(objp)) 95 return 0; 96 97 page = virt_to_head_page(objp); 98 99 /* 100 * If the allocator sets PageSlab, we know the pointer came from 101 * kmalloc(). 102 */ 103 if (PageSlab(page)) 104 return ksize(objp); 105 106 /* 107 * If it's not a compound page, see if we have a matching VMA 108 * region. This test is intentionally done in reverse order, 109 * so if there's no VMA, we still fall through and hand back 110 * PAGE_SIZE for 0-order pages. 111 */ 112 if (!PageCompound(page)) { 113 struct vm_area_struct *vma; 114 115 vma = find_vma(current->mm, (unsigned long)objp); 116 if (vma) 117 return vma->vm_end - vma->vm_start; 118 } 119 120 /* 121 * The ksize() function is only guaranteed to work for pointers 122 * returned by kmalloc(). So handle arbitrary pointers here. 123 */ 124 return PAGE_SIZE << compound_order(page); 125 } 126 127 int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 128 unsigned long start, int nr_pages, unsigned int foll_flags, 129 struct page **pages, struct vm_area_struct **vmas) 130 { 131 struct vm_area_struct *vma; 132 unsigned long vm_flags; 133 int i; 134 135 /* calculate required read or write permissions. 136 * If FOLL_FORCE is set, we only require the "MAY" flags. 137 */ 138 vm_flags = (foll_flags & FOLL_WRITE) ? 139 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); 140 vm_flags &= (foll_flags & FOLL_FORCE) ? 141 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); 142 143 for (i = 0; i < nr_pages; i++) { 144 vma = find_vma(mm, start); 145 if (!vma) 146 goto finish_or_fault; 147 148 /* protect what we can, including chardevs */ 149 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) || 150 !(vm_flags & vma->vm_flags)) 151 goto finish_or_fault; 152 153 if (pages) { 154 pages[i] = virt_to_page(start); 155 if (pages[i]) 156 page_cache_get(pages[i]); 157 } 158 if (vmas) 159 vmas[i] = vma; 160 start = (start + PAGE_SIZE) & PAGE_MASK; 161 } 162 163 return i; 164 165 finish_or_fault: 166 return i ? : -EFAULT; 167 } 168 169 /* 170 * get a list of pages in an address range belonging to the specified process 171 * and indicate the VMA that covers each page 172 * - this is potentially dodgy as we may end incrementing the page count of a 173 * slab page or a secondary page from a compound page 174 * - don't permit access to VMAs that don't support it, such as I/O mappings 175 */ 176 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, 177 unsigned long start, int nr_pages, int write, int force, 178 struct page **pages, struct vm_area_struct **vmas) 179 { 180 int flags = 0; 181 182 if (write) 183 flags |= FOLL_WRITE; 184 if (force) 185 flags |= FOLL_FORCE; 186 187 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas); 188 } 189 EXPORT_SYMBOL(get_user_pages); 190 191 /** 192 * follow_pfn - look up PFN at a user virtual address 193 * @vma: memory mapping 194 * @address: user virtual address 195 * @pfn: location to store found PFN 196 * 197 * Only IO mappings and raw PFN mappings are allowed. 198 * 199 * Returns zero and the pfn at @pfn on success, -ve otherwise. 200 */ 201 int follow_pfn(struct vm_area_struct *vma, unsigned long address, 202 unsigned long *pfn) 203 { 204 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) 205 return -EINVAL; 206 207 *pfn = address >> PAGE_SHIFT; 208 return 0; 209 } 210 EXPORT_SYMBOL(follow_pfn); 211 212 DEFINE_RWLOCK(vmlist_lock); 213 struct vm_struct *vmlist; 214 215 void vfree(const void *addr) 216 { 217 kfree(addr); 218 } 219 EXPORT_SYMBOL(vfree); 220 221 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) 222 { 223 /* 224 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc() 225 * returns only a logical address. 226 */ 227 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM); 228 } 229 EXPORT_SYMBOL(__vmalloc); 230 231 void *vmalloc_user(unsigned long size) 232 { 233 void *ret; 234 235 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, 236 PAGE_KERNEL); 237 if (ret) { 238 struct vm_area_struct *vma; 239 240 down_write(¤t->mm->mmap_sem); 241 vma = find_vma(current->mm, (unsigned long)ret); 242 if (vma) 243 vma->vm_flags |= VM_USERMAP; 244 up_write(¤t->mm->mmap_sem); 245 } 246 247 return ret; 248 } 249 EXPORT_SYMBOL(vmalloc_user); 250 251 struct page *vmalloc_to_page(const void *addr) 252 { 253 return virt_to_page(addr); 254 } 255 EXPORT_SYMBOL(vmalloc_to_page); 256 257 unsigned long vmalloc_to_pfn(const void *addr) 258 { 259 return page_to_pfn(virt_to_page(addr)); 260 } 261 EXPORT_SYMBOL(vmalloc_to_pfn); 262 263 long vread(char *buf, char *addr, unsigned long count) 264 { 265 memcpy(buf, addr, count); 266 return count; 267 } 268 269 long vwrite(char *buf, char *addr, unsigned long count) 270 { 271 /* Don't allow overflow */ 272 if ((unsigned long) addr + count < count) 273 count = -(unsigned long) addr; 274 275 memcpy(addr, buf, count); 276 return(count); 277 } 278 279 /* 280 * vmalloc - allocate virtually continguos memory 281 * 282 * @size: allocation size 283 * 284 * Allocate enough pages to cover @size from the page level 285 * allocator and map them into continguos kernel virtual space. 286 * 287 * For tight control over page level allocator and protection flags 288 * use __vmalloc() instead. 289 */ 290 void *vmalloc(unsigned long size) 291 { 292 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL); 293 } 294 EXPORT_SYMBOL(vmalloc); 295 296 void *vmalloc_node(unsigned long size, int node) 297 { 298 return vmalloc(size); 299 } 300 EXPORT_SYMBOL(vmalloc_node); 301 302 #ifndef PAGE_KERNEL_EXEC 303 # define PAGE_KERNEL_EXEC PAGE_KERNEL 304 #endif 305 306 /** 307 * vmalloc_exec - allocate virtually contiguous, executable memory 308 * @size: allocation size 309 * 310 * Kernel-internal function to allocate enough pages to cover @size 311 * the page level allocator and map them into contiguous and 312 * executable kernel virtual space. 313 * 314 * For tight control over page level allocator and protection flags 315 * use __vmalloc() instead. 316 */ 317 318 void *vmalloc_exec(unsigned long size) 319 { 320 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC); 321 } 322 323 /** 324 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) 325 * @size: allocation size 326 * 327 * Allocate enough 32bit PA addressable pages to cover @size from the 328 * page level allocator and map them into continguos kernel virtual space. 329 */ 330 void *vmalloc_32(unsigned long size) 331 { 332 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL); 333 } 334 EXPORT_SYMBOL(vmalloc_32); 335 336 /** 337 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory 338 * @size: allocation size 339 * 340 * The resulting memory area is 32bit addressable and zeroed so it can be 341 * mapped to userspace without leaking data. 342 * 343 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to 344 * remap_vmalloc_range() are permissible. 345 */ 346 void *vmalloc_32_user(unsigned long size) 347 { 348 /* 349 * We'll have to sort out the ZONE_DMA bits for 64-bit, 350 * but for now this can simply use vmalloc_user() directly. 351 */ 352 return vmalloc_user(size); 353 } 354 EXPORT_SYMBOL(vmalloc_32_user); 355 356 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot) 357 { 358 BUG(); 359 return NULL; 360 } 361 EXPORT_SYMBOL(vmap); 362 363 void vunmap(const void *addr) 364 { 365 BUG(); 366 } 367 EXPORT_SYMBOL(vunmap); 368 369 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) 370 { 371 BUG(); 372 return NULL; 373 } 374 EXPORT_SYMBOL(vm_map_ram); 375 376 void vm_unmap_ram(const void *mem, unsigned int count) 377 { 378 BUG(); 379 } 380 EXPORT_SYMBOL(vm_unmap_ram); 381 382 void vm_unmap_aliases(void) 383 { 384 } 385 EXPORT_SYMBOL_GPL(vm_unmap_aliases); 386 387 /* 388 * Implement a stub for vmalloc_sync_all() if the architecture chose not to 389 * have one. 390 */ 391 void __attribute__((weak)) vmalloc_sync_all(void) 392 { 393 } 394 395 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, 396 struct page *page) 397 { 398 return -EINVAL; 399 } 400 EXPORT_SYMBOL(vm_insert_page); 401 402 /* 403 * sys_brk() for the most part doesn't need the global kernel 404 * lock, except when an application is doing something nasty 405 * like trying to un-brk an area that has already been mapped 406 * to a regular file. in this case, the unmapping will need 407 * to invoke file system routines that need the global lock. 408 */ 409 SYSCALL_DEFINE1(brk, unsigned long, brk) 410 { 411 struct mm_struct *mm = current->mm; 412 413 if (brk < mm->start_brk || brk > mm->context.end_brk) 414 return mm->brk; 415 416 if (mm->brk == brk) 417 return mm->brk; 418 419 /* 420 * Always allow shrinking brk 421 */ 422 if (brk <= mm->brk) { 423 mm->brk = brk; 424 return brk; 425 } 426 427 /* 428 * Ok, looks good - let it rip. 429 */ 430 flush_icache_range(mm->brk, brk); 431 return mm->brk = brk; 432 } 433 434 /* 435 * initialise the VMA and region record slabs 436 */ 437 void __init mmap_init(void) 438 { 439 int ret; 440 441 ret = percpu_counter_init(&vm_committed_as, 0); 442 VM_BUG_ON(ret); 443 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC); 444 } 445 446 /* 447 * validate the region tree 448 * - the caller must hold the region lock 449 */ 450 #ifdef CONFIG_DEBUG_NOMMU_REGIONS 451 static noinline void validate_nommu_regions(void) 452 { 453 struct vm_region *region, *last; 454 struct rb_node *p, *lastp; 455 456 lastp = rb_first(&nommu_region_tree); 457 if (!lastp) 458 return; 459 460 last = rb_entry(lastp, struct vm_region, vm_rb); 461 BUG_ON(unlikely(last->vm_end <= last->vm_start)); 462 BUG_ON(unlikely(last->vm_top < last->vm_end)); 463 464 while ((p = rb_next(lastp))) { 465 region = rb_entry(p, struct vm_region, vm_rb); 466 last = rb_entry(lastp, struct vm_region, vm_rb); 467 468 BUG_ON(unlikely(region->vm_end <= region->vm_start)); 469 BUG_ON(unlikely(region->vm_top < region->vm_end)); 470 BUG_ON(unlikely(region->vm_start < last->vm_top)); 471 472 lastp = p; 473 } 474 } 475 #else 476 static void validate_nommu_regions(void) 477 { 478 } 479 #endif 480 481 /* 482 * add a region into the global tree 483 */ 484 static void add_nommu_region(struct vm_region *region) 485 { 486 struct vm_region *pregion; 487 struct rb_node **p, *parent; 488 489 validate_nommu_regions(); 490 491 parent = NULL; 492 p = &nommu_region_tree.rb_node; 493 while (*p) { 494 parent = *p; 495 pregion = rb_entry(parent, struct vm_region, vm_rb); 496 if (region->vm_start < pregion->vm_start) 497 p = &(*p)->rb_left; 498 else if (region->vm_start > pregion->vm_start) 499 p = &(*p)->rb_right; 500 else if (pregion == region) 501 return; 502 else 503 BUG(); 504 } 505 506 rb_link_node(®ion->vm_rb, parent, p); 507 rb_insert_color(®ion->vm_rb, &nommu_region_tree); 508 509 validate_nommu_regions(); 510 } 511 512 /* 513 * delete a region from the global tree 514 */ 515 static void delete_nommu_region(struct vm_region *region) 516 { 517 BUG_ON(!nommu_region_tree.rb_node); 518 519 validate_nommu_regions(); 520 rb_erase(®ion->vm_rb, &nommu_region_tree); 521 validate_nommu_regions(); 522 } 523 524 /* 525 * free a contiguous series of pages 526 */ 527 static void free_page_series(unsigned long from, unsigned long to) 528 { 529 for (; from < to; from += PAGE_SIZE) { 530 struct page *page = virt_to_page(from); 531 532 kdebug("- free %lx", from); 533 atomic_long_dec(&mmap_pages_allocated); 534 if (page_count(page) != 1) 535 kdebug("free page %p: refcount not one: %d", 536 page, page_count(page)); 537 put_page(page); 538 } 539 } 540 541 /* 542 * release a reference to a region 543 * - the caller must hold the region semaphore for writing, which this releases 544 * - the region may not have been added to the tree yet, in which case vm_top 545 * will equal vm_start 546 */ 547 static void __put_nommu_region(struct vm_region *region) 548 __releases(nommu_region_sem) 549 { 550 kenter("%p{%d}", region, region->vm_usage); 551 552 BUG_ON(!nommu_region_tree.rb_node); 553 554 if (--region->vm_usage == 0) { 555 if (region->vm_top > region->vm_start) 556 delete_nommu_region(region); 557 up_write(&nommu_region_sem); 558 559 if (region->vm_file) 560 fput(region->vm_file); 561 562 /* IO memory and memory shared directly out of the pagecache 563 * from ramfs/tmpfs mustn't be released here */ 564 if (region->vm_flags & VM_MAPPED_COPY) { 565 kdebug("free series"); 566 free_page_series(region->vm_start, region->vm_top); 567 } 568 kmem_cache_free(vm_region_jar, region); 569 } else { 570 up_write(&nommu_region_sem); 571 } 572 } 573 574 /* 575 * release a reference to a region 576 */ 577 static void put_nommu_region(struct vm_region *region) 578 { 579 down_write(&nommu_region_sem); 580 __put_nommu_region(region); 581 } 582 583 /* 584 * update protection on a vma 585 */ 586 static void protect_vma(struct vm_area_struct *vma, unsigned long flags) 587 { 588 #ifdef CONFIG_MPU 589 struct mm_struct *mm = vma->vm_mm; 590 long start = vma->vm_start & PAGE_MASK; 591 while (start < vma->vm_end) { 592 protect_page(mm, start, flags); 593 start += PAGE_SIZE; 594 } 595 update_protections(mm); 596 #endif 597 } 598 599 /* 600 * add a VMA into a process's mm_struct in the appropriate place in the list 601 * and tree and add to the address space's page tree also if not an anonymous 602 * page 603 * - should be called with mm->mmap_sem held writelocked 604 */ 605 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma) 606 { 607 struct vm_area_struct *pvma, **pp; 608 struct address_space *mapping; 609 struct rb_node **p, *parent; 610 611 kenter(",%p", vma); 612 613 BUG_ON(!vma->vm_region); 614 615 mm->map_count++; 616 vma->vm_mm = mm; 617 618 protect_vma(vma, vma->vm_flags); 619 620 /* add the VMA to the mapping */ 621 if (vma->vm_file) { 622 mapping = vma->vm_file->f_mapping; 623 624 flush_dcache_mmap_lock(mapping); 625 vma_prio_tree_insert(vma, &mapping->i_mmap); 626 flush_dcache_mmap_unlock(mapping); 627 } 628 629 /* add the VMA to the tree */ 630 parent = NULL; 631 p = &mm->mm_rb.rb_node; 632 while (*p) { 633 parent = *p; 634 pvma = rb_entry(parent, struct vm_area_struct, vm_rb); 635 636 /* sort by: start addr, end addr, VMA struct addr in that order 637 * (the latter is necessary as we may get identical VMAs) */ 638 if (vma->vm_start < pvma->vm_start) 639 p = &(*p)->rb_left; 640 else if (vma->vm_start > pvma->vm_start) 641 p = &(*p)->rb_right; 642 else if (vma->vm_end < pvma->vm_end) 643 p = &(*p)->rb_left; 644 else if (vma->vm_end > pvma->vm_end) 645 p = &(*p)->rb_right; 646 else if (vma < pvma) 647 p = &(*p)->rb_left; 648 else if (vma > pvma) 649 p = &(*p)->rb_right; 650 else 651 BUG(); 652 } 653 654 rb_link_node(&vma->vm_rb, parent, p); 655 rb_insert_color(&vma->vm_rb, &mm->mm_rb); 656 657 /* add VMA to the VMA list also */ 658 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) { 659 if (pvma->vm_start > vma->vm_start) 660 break; 661 if (pvma->vm_start < vma->vm_start) 662 continue; 663 if (pvma->vm_end < vma->vm_end) 664 break; 665 } 666 667 vma->vm_next = *pp; 668 *pp = vma; 669 } 670 671 /* 672 * delete a VMA from its owning mm_struct and address space 673 */ 674 static void delete_vma_from_mm(struct vm_area_struct *vma) 675 { 676 struct vm_area_struct **pp; 677 struct address_space *mapping; 678 struct mm_struct *mm = vma->vm_mm; 679 680 kenter("%p", vma); 681 682 protect_vma(vma, 0); 683 684 mm->map_count--; 685 if (mm->mmap_cache == vma) 686 mm->mmap_cache = NULL; 687 688 /* remove the VMA from the mapping */ 689 if (vma->vm_file) { 690 mapping = vma->vm_file->f_mapping; 691 692 flush_dcache_mmap_lock(mapping); 693 vma_prio_tree_remove(vma, &mapping->i_mmap); 694 flush_dcache_mmap_unlock(mapping); 695 } 696 697 /* remove from the MM's tree and list */ 698 rb_erase(&vma->vm_rb, &mm->mm_rb); 699 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) { 700 if (*pp == vma) { 701 *pp = vma->vm_next; 702 break; 703 } 704 } 705 706 vma->vm_mm = NULL; 707 } 708 709 /* 710 * destroy a VMA record 711 */ 712 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) 713 { 714 kenter("%p", vma); 715 if (vma->vm_ops && vma->vm_ops->close) 716 vma->vm_ops->close(vma); 717 if (vma->vm_file) { 718 fput(vma->vm_file); 719 if (vma->vm_flags & VM_EXECUTABLE) 720 removed_exe_file_vma(mm); 721 } 722 put_nommu_region(vma->vm_region); 723 kmem_cache_free(vm_area_cachep, vma); 724 } 725 726 /* 727 * look up the first VMA in which addr resides, NULL if none 728 * - should be called with mm->mmap_sem at least held readlocked 729 */ 730 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 731 { 732 struct vm_area_struct *vma; 733 struct rb_node *n = mm->mm_rb.rb_node; 734 735 /* check the cache first */ 736 vma = mm->mmap_cache; 737 if (vma && vma->vm_start <= addr && vma->vm_end > addr) 738 return vma; 739 740 /* trawl the tree (there may be multiple mappings in which addr 741 * resides) */ 742 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) { 743 vma = rb_entry(n, struct vm_area_struct, vm_rb); 744 if (vma->vm_start > addr) 745 return NULL; 746 if (vma->vm_end > addr) { 747 mm->mmap_cache = vma; 748 return vma; 749 } 750 } 751 752 return NULL; 753 } 754 EXPORT_SYMBOL(find_vma); 755 756 /* 757 * find a VMA 758 * - we don't extend stack VMAs under NOMMU conditions 759 */ 760 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 761 { 762 return find_vma(mm, addr); 763 } 764 765 /* 766 * expand a stack to a given address 767 * - not supported under NOMMU conditions 768 */ 769 int expand_stack(struct vm_area_struct *vma, unsigned long address) 770 { 771 return -ENOMEM; 772 } 773 774 /* 775 * look up the first VMA exactly that exactly matches addr 776 * - should be called with mm->mmap_sem at least held readlocked 777 */ 778 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 779 unsigned long addr, 780 unsigned long len) 781 { 782 struct vm_area_struct *vma; 783 struct rb_node *n = mm->mm_rb.rb_node; 784 unsigned long end = addr + len; 785 786 /* check the cache first */ 787 vma = mm->mmap_cache; 788 if (vma && vma->vm_start == addr && vma->vm_end == end) 789 return vma; 790 791 /* trawl the tree (there may be multiple mappings in which addr 792 * resides) */ 793 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) { 794 vma = rb_entry(n, struct vm_area_struct, vm_rb); 795 if (vma->vm_start < addr) 796 continue; 797 if (vma->vm_start > addr) 798 return NULL; 799 if (vma->vm_end == end) { 800 mm->mmap_cache = vma; 801 return vma; 802 } 803 } 804 805 return NULL; 806 } 807 808 /* 809 * determine whether a mapping should be permitted and, if so, what sort of 810 * mapping we're capable of supporting 811 */ 812 static int validate_mmap_request(struct file *file, 813 unsigned long addr, 814 unsigned long len, 815 unsigned long prot, 816 unsigned long flags, 817 unsigned long pgoff, 818 unsigned long *_capabilities) 819 { 820 unsigned long capabilities, rlen; 821 unsigned long reqprot = prot; 822 int ret; 823 824 /* do the simple checks first */ 825 if (flags & MAP_FIXED) { 826 printk(KERN_DEBUG 827 "%d: Can't do fixed-address/overlay mmap of RAM\n", 828 current->pid); 829 return -EINVAL; 830 } 831 832 if ((flags & MAP_TYPE) != MAP_PRIVATE && 833 (flags & MAP_TYPE) != MAP_SHARED) 834 return -EINVAL; 835 836 if (!len) 837 return -EINVAL; 838 839 /* Careful about overflows.. */ 840 rlen = PAGE_ALIGN(len); 841 if (!rlen || rlen > TASK_SIZE) 842 return -ENOMEM; 843 844 /* offset overflow? */ 845 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff) 846 return -EOVERFLOW; 847 848 if (file) { 849 /* validate file mapping requests */ 850 struct address_space *mapping; 851 852 /* files must support mmap */ 853 if (!file->f_op || !file->f_op->mmap) 854 return -ENODEV; 855 856 /* work out if what we've got could possibly be shared 857 * - we support chardevs that provide their own "memory" 858 * - we support files/blockdevs that are memory backed 859 */ 860 mapping = file->f_mapping; 861 if (!mapping) 862 mapping = file->f_path.dentry->d_inode->i_mapping; 863 864 capabilities = 0; 865 if (mapping && mapping->backing_dev_info) 866 capabilities = mapping->backing_dev_info->capabilities; 867 868 if (!capabilities) { 869 /* no explicit capabilities set, so assume some 870 * defaults */ 871 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) { 872 case S_IFREG: 873 case S_IFBLK: 874 capabilities = BDI_CAP_MAP_COPY; 875 break; 876 877 case S_IFCHR: 878 capabilities = 879 BDI_CAP_MAP_DIRECT | 880 BDI_CAP_READ_MAP | 881 BDI_CAP_WRITE_MAP; 882 break; 883 884 default: 885 return -EINVAL; 886 } 887 } 888 889 /* eliminate any capabilities that we can't support on this 890 * device */ 891 if (!file->f_op->get_unmapped_area) 892 capabilities &= ~BDI_CAP_MAP_DIRECT; 893 if (!file->f_op->read) 894 capabilities &= ~BDI_CAP_MAP_COPY; 895 896 /* The file shall have been opened with read permission. */ 897 if (!(file->f_mode & FMODE_READ)) 898 return -EACCES; 899 900 if (flags & MAP_SHARED) { 901 /* do checks for writing, appending and locking */ 902 if ((prot & PROT_WRITE) && 903 !(file->f_mode & FMODE_WRITE)) 904 return -EACCES; 905 906 if (IS_APPEND(file->f_path.dentry->d_inode) && 907 (file->f_mode & FMODE_WRITE)) 908 return -EACCES; 909 910 if (locks_verify_locked(file->f_path.dentry->d_inode)) 911 return -EAGAIN; 912 913 if (!(capabilities & BDI_CAP_MAP_DIRECT)) 914 return -ENODEV; 915 916 /* we mustn't privatise shared mappings */ 917 capabilities &= ~BDI_CAP_MAP_COPY; 918 } 919 else { 920 /* we're going to read the file into private memory we 921 * allocate */ 922 if (!(capabilities & BDI_CAP_MAP_COPY)) 923 return -ENODEV; 924 925 /* we don't permit a private writable mapping to be 926 * shared with the backing device */ 927 if (prot & PROT_WRITE) 928 capabilities &= ~BDI_CAP_MAP_DIRECT; 929 } 930 931 if (capabilities & BDI_CAP_MAP_DIRECT) { 932 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) || 933 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) || 934 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP)) 935 ) { 936 capabilities &= ~BDI_CAP_MAP_DIRECT; 937 if (flags & MAP_SHARED) { 938 printk(KERN_WARNING 939 "MAP_SHARED not completely supported on !MMU\n"); 940 return -EINVAL; 941 } 942 } 943 } 944 945 /* handle executable mappings and implied executable 946 * mappings */ 947 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { 948 if (prot & PROT_EXEC) 949 return -EPERM; 950 } 951 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { 952 /* handle implication of PROT_EXEC by PROT_READ */ 953 if (current->personality & READ_IMPLIES_EXEC) { 954 if (capabilities & BDI_CAP_EXEC_MAP) 955 prot |= PROT_EXEC; 956 } 957 } 958 else if ((prot & PROT_READ) && 959 (prot & PROT_EXEC) && 960 !(capabilities & BDI_CAP_EXEC_MAP) 961 ) { 962 /* backing file is not executable, try to copy */ 963 capabilities &= ~BDI_CAP_MAP_DIRECT; 964 } 965 } 966 else { 967 /* anonymous mappings are always memory backed and can be 968 * privately mapped 969 */ 970 capabilities = BDI_CAP_MAP_COPY; 971 972 /* handle PROT_EXEC implication by PROT_READ */ 973 if ((prot & PROT_READ) && 974 (current->personality & READ_IMPLIES_EXEC)) 975 prot |= PROT_EXEC; 976 } 977 978 /* allow the security API to have its say */ 979 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0); 980 if (ret < 0) 981 return ret; 982 983 /* looks okay */ 984 *_capabilities = capabilities; 985 return 0; 986 } 987 988 /* 989 * we've determined that we can make the mapping, now translate what we 990 * now know into VMA flags 991 */ 992 static unsigned long determine_vm_flags(struct file *file, 993 unsigned long prot, 994 unsigned long flags, 995 unsigned long capabilities) 996 { 997 unsigned long vm_flags; 998 999 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags); 1000 /* vm_flags |= mm->def_flags; */ 1001 1002 if (!(capabilities & BDI_CAP_MAP_DIRECT)) { 1003 /* attempt to share read-only copies of mapped file chunks */ 1004 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 1005 if (file && !(prot & PROT_WRITE)) 1006 vm_flags |= VM_MAYSHARE; 1007 } else { 1008 /* overlay a shareable mapping on the backing device or inode 1009 * if possible - used for chardevs, ramfs/tmpfs/shmfs and 1010 * romfs/cramfs */ 1011 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS); 1012 if (flags & MAP_SHARED) 1013 vm_flags |= VM_SHARED; 1014 } 1015 1016 /* refuse to let anyone share private mappings with this process if 1017 * it's being traced - otherwise breakpoints set in it may interfere 1018 * with another untraced process 1019 */ 1020 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current)) 1021 vm_flags &= ~VM_MAYSHARE; 1022 1023 return vm_flags; 1024 } 1025 1026 /* 1027 * set up a shared mapping on a file (the driver or filesystem provides and 1028 * pins the storage) 1029 */ 1030 static int do_mmap_shared_file(struct vm_area_struct *vma) 1031 { 1032 int ret; 1033 1034 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1035 if (ret == 0) { 1036 vma->vm_region->vm_top = vma->vm_region->vm_end; 1037 return 0; 1038 } 1039 if (ret != -ENOSYS) 1040 return ret; 1041 1042 /* getting -ENOSYS indicates that direct mmap isn't possible (as 1043 * opposed to tried but failed) so we can only give a suitable error as 1044 * it's not possible to make a private copy if MAP_SHARED was given */ 1045 return -ENODEV; 1046 } 1047 1048 /* 1049 * set up a private mapping or an anonymous shared mapping 1050 */ 1051 static int do_mmap_private(struct vm_area_struct *vma, 1052 struct vm_region *region, 1053 unsigned long len, 1054 unsigned long capabilities) 1055 { 1056 struct page *pages; 1057 unsigned long total, point, n, rlen; 1058 void *base; 1059 int ret, order; 1060 1061 /* invoke the file's mapping function so that it can keep track of 1062 * shared mappings on devices or memory 1063 * - VM_MAYSHARE will be set if it may attempt to share 1064 */ 1065 if (capabilities & BDI_CAP_MAP_DIRECT) { 1066 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1067 if (ret == 0) { 1068 /* shouldn't return success if we're not sharing */ 1069 BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); 1070 vma->vm_region->vm_top = vma->vm_region->vm_end; 1071 return 0; 1072 } 1073 if (ret != -ENOSYS) 1074 return ret; 1075 1076 /* getting an ENOSYS error indicates that direct mmap isn't 1077 * possible (as opposed to tried but failed) so we'll try to 1078 * make a private copy of the data and map that instead */ 1079 } 1080 1081 rlen = PAGE_ALIGN(len); 1082 1083 /* allocate some memory to hold the mapping 1084 * - note that this may not return a page-aligned address if the object 1085 * we're allocating is smaller than a page 1086 */ 1087 order = get_order(rlen); 1088 kdebug("alloc order %d for %lx", order, len); 1089 1090 pages = alloc_pages(GFP_KERNEL, order); 1091 if (!pages) 1092 goto enomem; 1093 1094 total = 1 << order; 1095 atomic_long_add(total, &mmap_pages_allocated); 1096 1097 point = rlen >> PAGE_SHIFT; 1098 1099 /* we allocated a power-of-2 sized page set, so we may want to trim off 1100 * the excess */ 1101 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) { 1102 while (total > point) { 1103 order = ilog2(total - point); 1104 n = 1 << order; 1105 kdebug("shave %lu/%lu @%lu", n, total - point, total); 1106 atomic_long_sub(n, &mmap_pages_allocated); 1107 total -= n; 1108 set_page_refcounted(pages + total); 1109 __free_pages(pages + total, order); 1110 } 1111 } 1112 1113 for (point = 1; point < total; point++) 1114 set_page_refcounted(&pages[point]); 1115 1116 base = page_address(pages); 1117 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY; 1118 region->vm_start = (unsigned long) base; 1119 region->vm_end = region->vm_start + rlen; 1120 region->vm_top = region->vm_start + (total << PAGE_SHIFT); 1121 1122 vma->vm_start = region->vm_start; 1123 vma->vm_end = region->vm_start + len; 1124 1125 if (vma->vm_file) { 1126 /* read the contents of a file into the copy */ 1127 mm_segment_t old_fs; 1128 loff_t fpos; 1129 1130 fpos = vma->vm_pgoff; 1131 fpos <<= PAGE_SHIFT; 1132 1133 old_fs = get_fs(); 1134 set_fs(KERNEL_DS); 1135 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos); 1136 set_fs(old_fs); 1137 1138 if (ret < 0) 1139 goto error_free; 1140 1141 /* clear the last little bit */ 1142 if (ret < rlen) 1143 memset(base + ret, 0, rlen - ret); 1144 1145 } 1146 1147 return 0; 1148 1149 error_free: 1150 free_page_series(region->vm_start, region->vm_end); 1151 region->vm_start = vma->vm_start = 0; 1152 region->vm_end = vma->vm_end = 0; 1153 region->vm_top = 0; 1154 return ret; 1155 1156 enomem: 1157 printk("Allocation of length %lu from process %d (%s) failed\n", 1158 len, current->pid, current->comm); 1159 show_free_areas(); 1160 return -ENOMEM; 1161 } 1162 1163 /* 1164 * handle mapping creation for uClinux 1165 */ 1166 unsigned long do_mmap_pgoff(struct file *file, 1167 unsigned long addr, 1168 unsigned long len, 1169 unsigned long prot, 1170 unsigned long flags, 1171 unsigned long pgoff) 1172 { 1173 struct vm_area_struct *vma; 1174 struct vm_region *region; 1175 struct rb_node *rb; 1176 unsigned long capabilities, vm_flags, result; 1177 int ret; 1178 1179 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff); 1180 1181 /* decide whether we should attempt the mapping, and if so what sort of 1182 * mapping */ 1183 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 1184 &capabilities); 1185 if (ret < 0) { 1186 kleave(" = %d [val]", ret); 1187 return ret; 1188 } 1189 1190 /* we ignore the address hint */ 1191 addr = 0; 1192 1193 /* we've determined that we can make the mapping, now translate what we 1194 * now know into VMA flags */ 1195 vm_flags = determine_vm_flags(file, prot, flags, capabilities); 1196 1197 /* we're going to need to record the mapping */ 1198 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL); 1199 if (!region) 1200 goto error_getting_region; 1201 1202 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); 1203 if (!vma) 1204 goto error_getting_vma; 1205 1206 region->vm_usage = 1; 1207 region->vm_flags = vm_flags; 1208 region->vm_pgoff = pgoff; 1209 1210 INIT_LIST_HEAD(&vma->anon_vma_chain); 1211 vma->vm_flags = vm_flags; 1212 vma->vm_pgoff = pgoff; 1213 1214 if (file) { 1215 region->vm_file = file; 1216 get_file(file); 1217 vma->vm_file = file; 1218 get_file(file); 1219 if (vm_flags & VM_EXECUTABLE) { 1220 added_exe_file_vma(current->mm); 1221 vma->vm_mm = current->mm; 1222 } 1223 } 1224 1225 down_write(&nommu_region_sem); 1226 1227 /* if we want to share, we need to check for regions created by other 1228 * mmap() calls that overlap with our proposed mapping 1229 * - we can only share with a superset match on most regular files 1230 * - shared mappings on character devices and memory backed files are 1231 * permitted to overlap inexactly as far as we are concerned for in 1232 * these cases, sharing is handled in the driver or filesystem rather 1233 * than here 1234 */ 1235 if (vm_flags & VM_MAYSHARE) { 1236 struct vm_region *pregion; 1237 unsigned long pglen, rpglen, pgend, rpgend, start; 1238 1239 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1240 pgend = pgoff + pglen; 1241 1242 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) { 1243 pregion = rb_entry(rb, struct vm_region, vm_rb); 1244 1245 if (!(pregion->vm_flags & VM_MAYSHARE)) 1246 continue; 1247 1248 /* search for overlapping mappings on the same file */ 1249 if (pregion->vm_file->f_path.dentry->d_inode != 1250 file->f_path.dentry->d_inode) 1251 continue; 1252 1253 if (pregion->vm_pgoff >= pgend) 1254 continue; 1255 1256 rpglen = pregion->vm_end - pregion->vm_start; 1257 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT; 1258 rpgend = pregion->vm_pgoff + rpglen; 1259 if (pgoff >= rpgend) 1260 continue; 1261 1262 /* handle inexactly overlapping matches between 1263 * mappings */ 1264 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) && 1265 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) { 1266 /* new mapping is not a subset of the region */ 1267 if (!(capabilities & BDI_CAP_MAP_DIRECT)) 1268 goto sharing_violation; 1269 continue; 1270 } 1271 1272 /* we've found a region we can share */ 1273 pregion->vm_usage++; 1274 vma->vm_region = pregion; 1275 start = pregion->vm_start; 1276 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT; 1277 vma->vm_start = start; 1278 vma->vm_end = start + len; 1279 1280 if (pregion->vm_flags & VM_MAPPED_COPY) { 1281 kdebug("share copy"); 1282 vma->vm_flags |= VM_MAPPED_COPY; 1283 } else { 1284 kdebug("share mmap"); 1285 ret = do_mmap_shared_file(vma); 1286 if (ret < 0) { 1287 vma->vm_region = NULL; 1288 vma->vm_start = 0; 1289 vma->vm_end = 0; 1290 pregion->vm_usage--; 1291 pregion = NULL; 1292 goto error_just_free; 1293 } 1294 } 1295 fput(region->vm_file); 1296 kmem_cache_free(vm_region_jar, region); 1297 region = pregion; 1298 result = start; 1299 goto share; 1300 } 1301 1302 /* obtain the address at which to make a shared mapping 1303 * - this is the hook for quasi-memory character devices to 1304 * tell us the location of a shared mapping 1305 */ 1306 if (capabilities & BDI_CAP_MAP_DIRECT) { 1307 addr = file->f_op->get_unmapped_area(file, addr, len, 1308 pgoff, flags); 1309 if (IS_ERR((void *) addr)) { 1310 ret = addr; 1311 if (ret != (unsigned long) -ENOSYS) 1312 goto error_just_free; 1313 1314 /* the driver refused to tell us where to site 1315 * the mapping so we'll have to attempt to copy 1316 * it */ 1317 ret = (unsigned long) -ENODEV; 1318 if (!(capabilities & BDI_CAP_MAP_COPY)) 1319 goto error_just_free; 1320 1321 capabilities &= ~BDI_CAP_MAP_DIRECT; 1322 } else { 1323 vma->vm_start = region->vm_start = addr; 1324 vma->vm_end = region->vm_end = addr + len; 1325 } 1326 } 1327 } 1328 1329 vma->vm_region = region; 1330 1331 /* set up the mapping 1332 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set 1333 */ 1334 if (file && vma->vm_flags & VM_SHARED) 1335 ret = do_mmap_shared_file(vma); 1336 else 1337 ret = do_mmap_private(vma, region, len, capabilities); 1338 if (ret < 0) 1339 goto error_just_free; 1340 add_nommu_region(region); 1341 1342 /* clear anonymous mappings that don't ask for uninitialized data */ 1343 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED)) 1344 memset((void *)region->vm_start, 0, 1345 region->vm_end - region->vm_start); 1346 1347 /* okay... we have a mapping; now we have to register it */ 1348 result = vma->vm_start; 1349 1350 current->mm->total_vm += len >> PAGE_SHIFT; 1351 1352 share: 1353 add_vma_to_mm(current->mm, vma); 1354 1355 /* we flush the region from the icache only when the first executable 1356 * mapping of it is made */ 1357 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) { 1358 flush_icache_range(region->vm_start, region->vm_end); 1359 region->vm_icache_flushed = true; 1360 } 1361 1362 up_write(&nommu_region_sem); 1363 1364 kleave(" = %lx", result); 1365 return result; 1366 1367 error_just_free: 1368 up_write(&nommu_region_sem); 1369 error: 1370 if (region->vm_file) 1371 fput(region->vm_file); 1372 kmem_cache_free(vm_region_jar, region); 1373 if (vma->vm_file) 1374 fput(vma->vm_file); 1375 if (vma->vm_flags & VM_EXECUTABLE) 1376 removed_exe_file_vma(vma->vm_mm); 1377 kmem_cache_free(vm_area_cachep, vma); 1378 kleave(" = %d", ret); 1379 return ret; 1380 1381 sharing_violation: 1382 up_write(&nommu_region_sem); 1383 printk(KERN_WARNING "Attempt to share mismatched mappings\n"); 1384 ret = -EINVAL; 1385 goto error; 1386 1387 error_getting_vma: 1388 kmem_cache_free(vm_region_jar, region); 1389 printk(KERN_WARNING "Allocation of vma for %lu byte allocation" 1390 " from process %d failed\n", 1391 len, current->pid); 1392 show_free_areas(); 1393 return -ENOMEM; 1394 1395 error_getting_region: 1396 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation" 1397 " from process %d failed\n", 1398 len, current->pid); 1399 show_free_areas(); 1400 return -ENOMEM; 1401 } 1402 EXPORT_SYMBOL(do_mmap_pgoff); 1403 1404 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, 1405 unsigned long, prot, unsigned long, flags, 1406 unsigned long, fd, unsigned long, pgoff) 1407 { 1408 struct file *file = NULL; 1409 unsigned long retval = -EBADF; 1410 1411 if (!(flags & MAP_ANONYMOUS)) { 1412 file = fget(fd); 1413 if (!file) 1414 goto out; 1415 } 1416 1417 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 1418 1419 down_write(¤t->mm->mmap_sem); 1420 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); 1421 up_write(¤t->mm->mmap_sem); 1422 1423 if (file) 1424 fput(file); 1425 out: 1426 return retval; 1427 } 1428 1429 #ifdef __ARCH_WANT_SYS_OLD_MMAP 1430 struct mmap_arg_struct { 1431 unsigned long addr; 1432 unsigned long len; 1433 unsigned long prot; 1434 unsigned long flags; 1435 unsigned long fd; 1436 unsigned long offset; 1437 }; 1438 1439 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) 1440 { 1441 struct mmap_arg_struct a; 1442 1443 if (copy_from_user(&a, arg, sizeof(a))) 1444 return -EFAULT; 1445 if (a.offset & ~PAGE_MASK) 1446 return -EINVAL; 1447 1448 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, 1449 a.offset >> PAGE_SHIFT); 1450 } 1451 #endif /* __ARCH_WANT_SYS_OLD_MMAP */ 1452 1453 /* 1454 * split a vma into two pieces at address 'addr', a new vma is allocated either 1455 * for the first part or the tail. 1456 */ 1457 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, 1458 unsigned long addr, int new_below) 1459 { 1460 struct vm_area_struct *new; 1461 struct vm_region *region; 1462 unsigned long npages; 1463 1464 kenter(""); 1465 1466 /* we're only permitted to split anonymous regions (these should have 1467 * only a single usage on the region) */ 1468 if (vma->vm_file) 1469 return -ENOMEM; 1470 1471 if (mm->map_count >= sysctl_max_map_count) 1472 return -ENOMEM; 1473 1474 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL); 1475 if (!region) 1476 return -ENOMEM; 1477 1478 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); 1479 if (!new) { 1480 kmem_cache_free(vm_region_jar, region); 1481 return -ENOMEM; 1482 } 1483 1484 /* most fields are the same, copy all, and then fixup */ 1485 *new = *vma; 1486 *region = *vma->vm_region; 1487 new->vm_region = region; 1488 1489 npages = (addr - vma->vm_start) >> PAGE_SHIFT; 1490 1491 if (new_below) { 1492 region->vm_top = region->vm_end = new->vm_end = addr; 1493 } else { 1494 region->vm_start = new->vm_start = addr; 1495 region->vm_pgoff = new->vm_pgoff += npages; 1496 } 1497 1498 if (new->vm_ops && new->vm_ops->open) 1499 new->vm_ops->open(new); 1500 1501 delete_vma_from_mm(vma); 1502 down_write(&nommu_region_sem); 1503 delete_nommu_region(vma->vm_region); 1504 if (new_below) { 1505 vma->vm_region->vm_start = vma->vm_start = addr; 1506 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages; 1507 } else { 1508 vma->vm_region->vm_end = vma->vm_end = addr; 1509 vma->vm_region->vm_top = addr; 1510 } 1511 add_nommu_region(vma->vm_region); 1512 add_nommu_region(new->vm_region); 1513 up_write(&nommu_region_sem); 1514 add_vma_to_mm(mm, vma); 1515 add_vma_to_mm(mm, new); 1516 return 0; 1517 } 1518 1519 /* 1520 * shrink a VMA by removing the specified chunk from either the beginning or 1521 * the end 1522 */ 1523 static int shrink_vma(struct mm_struct *mm, 1524 struct vm_area_struct *vma, 1525 unsigned long from, unsigned long to) 1526 { 1527 struct vm_region *region; 1528 1529 kenter(""); 1530 1531 /* adjust the VMA's pointers, which may reposition it in the MM's tree 1532 * and list */ 1533 delete_vma_from_mm(vma); 1534 if (from > vma->vm_start) 1535 vma->vm_end = from; 1536 else 1537 vma->vm_start = to; 1538 add_vma_to_mm(mm, vma); 1539 1540 /* cut the backing region down to size */ 1541 region = vma->vm_region; 1542 BUG_ON(region->vm_usage != 1); 1543 1544 down_write(&nommu_region_sem); 1545 delete_nommu_region(region); 1546 if (from > region->vm_start) { 1547 to = region->vm_top; 1548 region->vm_top = region->vm_end = from; 1549 } else { 1550 region->vm_start = to; 1551 } 1552 add_nommu_region(region); 1553 up_write(&nommu_region_sem); 1554 1555 free_page_series(from, to); 1556 return 0; 1557 } 1558 1559 /* 1560 * release a mapping 1561 * - under NOMMU conditions the chunk to be unmapped must be backed by a single 1562 * VMA, though it need not cover the whole VMA 1563 */ 1564 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) 1565 { 1566 struct vm_area_struct *vma; 1567 struct rb_node *rb; 1568 unsigned long end = start + len; 1569 int ret; 1570 1571 kenter(",%lx,%zx", start, len); 1572 1573 if (len == 0) 1574 return -EINVAL; 1575 1576 /* find the first potentially overlapping VMA */ 1577 vma = find_vma(mm, start); 1578 if (!vma) { 1579 static int limit = 0; 1580 if (limit < 5) { 1581 printk(KERN_WARNING 1582 "munmap of memory not mmapped by process %d" 1583 " (%s): 0x%lx-0x%lx\n", 1584 current->pid, current->comm, 1585 start, start + len - 1); 1586 limit++; 1587 } 1588 return -EINVAL; 1589 } 1590 1591 /* we're allowed to split an anonymous VMA but not a file-backed one */ 1592 if (vma->vm_file) { 1593 do { 1594 if (start > vma->vm_start) { 1595 kleave(" = -EINVAL [miss]"); 1596 return -EINVAL; 1597 } 1598 if (end == vma->vm_end) 1599 goto erase_whole_vma; 1600 rb = rb_next(&vma->vm_rb); 1601 vma = rb_entry(rb, struct vm_area_struct, vm_rb); 1602 } while (rb); 1603 kleave(" = -EINVAL [split file]"); 1604 return -EINVAL; 1605 } else { 1606 /* the chunk must be a subset of the VMA found */ 1607 if (start == vma->vm_start && end == vma->vm_end) 1608 goto erase_whole_vma; 1609 if (start < vma->vm_start || end > vma->vm_end) { 1610 kleave(" = -EINVAL [superset]"); 1611 return -EINVAL; 1612 } 1613 if (start & ~PAGE_MASK) { 1614 kleave(" = -EINVAL [unaligned start]"); 1615 return -EINVAL; 1616 } 1617 if (end != vma->vm_end && end & ~PAGE_MASK) { 1618 kleave(" = -EINVAL [unaligned split]"); 1619 return -EINVAL; 1620 } 1621 if (start != vma->vm_start && end != vma->vm_end) { 1622 ret = split_vma(mm, vma, start, 1); 1623 if (ret < 0) { 1624 kleave(" = %d [split]", ret); 1625 return ret; 1626 } 1627 } 1628 return shrink_vma(mm, vma, start, end); 1629 } 1630 1631 erase_whole_vma: 1632 delete_vma_from_mm(vma); 1633 delete_vma(mm, vma); 1634 kleave(" = 0"); 1635 return 0; 1636 } 1637 EXPORT_SYMBOL(do_munmap); 1638 1639 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) 1640 { 1641 int ret; 1642 struct mm_struct *mm = current->mm; 1643 1644 down_write(&mm->mmap_sem); 1645 ret = do_munmap(mm, addr, len); 1646 up_write(&mm->mmap_sem); 1647 return ret; 1648 } 1649 1650 /* 1651 * release all the mappings made in a process's VM space 1652 */ 1653 void exit_mmap(struct mm_struct *mm) 1654 { 1655 struct vm_area_struct *vma; 1656 1657 if (!mm) 1658 return; 1659 1660 kenter(""); 1661 1662 mm->total_vm = 0; 1663 1664 while ((vma = mm->mmap)) { 1665 mm->mmap = vma->vm_next; 1666 delete_vma_from_mm(vma); 1667 delete_vma(mm, vma); 1668 } 1669 1670 kleave(""); 1671 } 1672 1673 unsigned long do_brk(unsigned long addr, unsigned long len) 1674 { 1675 return -ENOMEM; 1676 } 1677 1678 /* 1679 * expand (or shrink) an existing mapping, potentially moving it at the same 1680 * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 1681 * 1682 * under NOMMU conditions, we only permit changing a mapping's size, and only 1683 * as long as it stays within the region allocated by do_mmap_private() and the 1684 * block is not shareable 1685 * 1686 * MREMAP_FIXED is not supported under NOMMU conditions 1687 */ 1688 unsigned long do_mremap(unsigned long addr, 1689 unsigned long old_len, unsigned long new_len, 1690 unsigned long flags, unsigned long new_addr) 1691 { 1692 struct vm_area_struct *vma; 1693 1694 /* insanity checks first */ 1695 if (old_len == 0 || new_len == 0) 1696 return (unsigned long) -EINVAL; 1697 1698 if (addr & ~PAGE_MASK) 1699 return -EINVAL; 1700 1701 if (flags & MREMAP_FIXED && new_addr != addr) 1702 return (unsigned long) -EINVAL; 1703 1704 vma = find_vma_exact(current->mm, addr, old_len); 1705 if (!vma) 1706 return (unsigned long) -EINVAL; 1707 1708 if (vma->vm_end != vma->vm_start + old_len) 1709 return (unsigned long) -EFAULT; 1710 1711 if (vma->vm_flags & VM_MAYSHARE) 1712 return (unsigned long) -EPERM; 1713 1714 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start) 1715 return (unsigned long) -ENOMEM; 1716 1717 /* all checks complete - do it */ 1718 vma->vm_end = vma->vm_start + new_len; 1719 return vma->vm_start; 1720 } 1721 EXPORT_SYMBOL(do_mremap); 1722 1723 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, 1724 unsigned long, new_len, unsigned long, flags, 1725 unsigned long, new_addr) 1726 { 1727 unsigned long ret; 1728 1729 down_write(¤t->mm->mmap_sem); 1730 ret = do_mremap(addr, old_len, new_len, flags, new_addr); 1731 up_write(¤t->mm->mmap_sem); 1732 return ret; 1733 } 1734 1735 struct page *follow_page(struct vm_area_struct *vma, unsigned long address, 1736 unsigned int foll_flags) 1737 { 1738 return NULL; 1739 } 1740 1741 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from, 1742 unsigned long to, unsigned long size, pgprot_t prot) 1743 { 1744 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT; 1745 return 0; 1746 } 1747 EXPORT_SYMBOL(remap_pfn_range); 1748 1749 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 1750 unsigned long pgoff) 1751 { 1752 unsigned int size = vma->vm_end - vma->vm_start; 1753 1754 if (!(vma->vm_flags & VM_USERMAP)) 1755 return -EINVAL; 1756 1757 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT)); 1758 vma->vm_end = vma->vm_start + size; 1759 1760 return 0; 1761 } 1762 EXPORT_SYMBOL(remap_vmalloc_range); 1763 1764 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) 1765 { 1766 } 1767 1768 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr, 1769 unsigned long len, unsigned long pgoff, unsigned long flags) 1770 { 1771 return -ENOMEM; 1772 } 1773 1774 void arch_unmap_area(struct mm_struct *mm, unsigned long addr) 1775 { 1776 } 1777 1778 void unmap_mapping_range(struct address_space *mapping, 1779 loff_t const holebegin, loff_t const holelen, 1780 int even_cows) 1781 { 1782 } 1783 EXPORT_SYMBOL(unmap_mapping_range); 1784 1785 /* 1786 * Check that a process has enough memory to allocate a new virtual 1787 * mapping. 0 means there is enough memory for the allocation to 1788 * succeed and -ENOMEM implies there is not. 1789 * 1790 * We currently support three overcommit policies, which are set via the 1791 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting 1792 * 1793 * Strict overcommit modes added 2002 Feb 26 by Alan Cox. 1794 * Additional code 2002 Jul 20 by Robert Love. 1795 * 1796 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. 1797 * 1798 * Note this is a helper function intended to be used by LSMs which 1799 * wish to use this logic. 1800 */ 1801 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) 1802 { 1803 unsigned long free, allowed; 1804 1805 vm_acct_memory(pages); 1806 1807 /* 1808 * Sometimes we want to use more memory than we have 1809 */ 1810 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) 1811 return 0; 1812 1813 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { 1814 unsigned long n; 1815 1816 free = global_page_state(NR_FILE_PAGES); 1817 free += nr_swap_pages; 1818 1819 /* 1820 * Any slabs which are created with the 1821 * SLAB_RECLAIM_ACCOUNT flag claim to have contents 1822 * which are reclaimable, under pressure. The dentry 1823 * cache and most inode caches should fall into this 1824 */ 1825 free += global_page_state(NR_SLAB_RECLAIMABLE); 1826 1827 /* 1828 * Leave the last 3% for root 1829 */ 1830 if (!cap_sys_admin) 1831 free -= free / 32; 1832 1833 if (free > pages) 1834 return 0; 1835 1836 /* 1837 * nr_free_pages() is very expensive on large systems, 1838 * only call if we're about to fail. 1839 */ 1840 n = nr_free_pages(); 1841 1842 /* 1843 * Leave reserved pages. The pages are not for anonymous pages. 1844 */ 1845 if (n <= totalreserve_pages) 1846 goto error; 1847 else 1848 n -= totalreserve_pages; 1849 1850 /* 1851 * Leave the last 3% for root 1852 */ 1853 if (!cap_sys_admin) 1854 n -= n / 32; 1855 free += n; 1856 1857 if (free > pages) 1858 return 0; 1859 1860 goto error; 1861 } 1862 1863 allowed = totalram_pages * sysctl_overcommit_ratio / 100; 1864 /* 1865 * Leave the last 3% for root 1866 */ 1867 if (!cap_sys_admin) 1868 allowed -= allowed / 32; 1869 allowed += total_swap_pages; 1870 1871 /* Don't let a single process grow too big: 1872 leave 3% of the size of this process for other processes */ 1873 if (mm) 1874 allowed -= mm->total_vm / 32; 1875 1876 if (percpu_counter_read_positive(&vm_committed_as) < allowed) 1877 return 0; 1878 1879 error: 1880 vm_unacct_memory(pages); 1881 1882 return -ENOMEM; 1883 } 1884 1885 int in_gate_area_no_task(unsigned long addr) 1886 { 1887 return 0; 1888 } 1889 1890 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 1891 { 1892 BUG(); 1893 return 0; 1894 } 1895 EXPORT_SYMBOL(filemap_fault); 1896 1897 /* 1898 * Access another process' address space. 1899 * - source/target buffer must be kernel space 1900 */ 1901 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write) 1902 { 1903 struct vm_area_struct *vma; 1904 struct mm_struct *mm; 1905 1906 if (addr + len < addr) 1907 return 0; 1908 1909 mm = get_task_mm(tsk); 1910 if (!mm) 1911 return 0; 1912 1913 down_read(&mm->mmap_sem); 1914 1915 /* the access must start within one of the target process's mappings */ 1916 vma = find_vma(mm, addr); 1917 if (vma) { 1918 /* don't overrun this mapping */ 1919 if (addr + len >= vma->vm_end) 1920 len = vma->vm_end - addr; 1921 1922 /* only read or write mappings where it is permitted */ 1923 if (write && vma->vm_flags & VM_MAYWRITE) 1924 copy_to_user_page(vma, NULL, addr, 1925 (void *) addr, buf, len); 1926 else if (!write && vma->vm_flags & VM_MAYREAD) 1927 copy_from_user_page(vma, NULL, addr, 1928 buf, (void *) addr, len); 1929 else 1930 len = 0; 1931 } else { 1932 len = 0; 1933 } 1934 1935 up_read(&mm->mmap_sem); 1936 mmput(mm); 1937 return len; 1938 } 1939 1940 /** 1941 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode 1942 * @inode: The inode to check 1943 * @size: The current filesize of the inode 1944 * @newsize: The proposed filesize of the inode 1945 * 1946 * Check the shared mappings on an inode on behalf of a shrinking truncate to 1947 * make sure that that any outstanding VMAs aren't broken and then shrink the 1948 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't 1949 * automatically grant mappings that are too large. 1950 */ 1951 int nommu_shrink_inode_mappings(struct inode *inode, size_t size, 1952 size_t newsize) 1953 { 1954 struct vm_area_struct *vma; 1955 struct prio_tree_iter iter; 1956 struct vm_region *region; 1957 pgoff_t low, high; 1958 size_t r_size, r_top; 1959 1960 low = newsize >> PAGE_SHIFT; 1961 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 1962 1963 down_write(&nommu_region_sem); 1964 1965 /* search for VMAs that fall within the dead zone */ 1966 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap, 1967 low, high) { 1968 /* found one - only interested if it's shared out of the page 1969 * cache */ 1970 if (vma->vm_flags & VM_SHARED) { 1971 up_write(&nommu_region_sem); 1972 return -ETXTBSY; /* not quite true, but near enough */ 1973 } 1974 } 1975 1976 /* reduce any regions that overlap the dead zone - if in existence, 1977 * these will be pointed to by VMAs that don't overlap the dead zone 1978 * 1979 * we don't check for any regions that start beyond the EOF as there 1980 * shouldn't be any 1981 */ 1982 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap, 1983 0, ULONG_MAX) { 1984 if (!(vma->vm_flags & VM_SHARED)) 1985 continue; 1986 1987 region = vma->vm_region; 1988 r_size = region->vm_top - region->vm_start; 1989 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size; 1990 1991 if (r_top > newsize) { 1992 region->vm_top -= r_top - newsize; 1993 if (region->vm_end > region->vm_top) 1994 region->vm_end = region->vm_top; 1995 } 1996 } 1997 1998 up_write(&nommu_region_sem); 1999 return 0; 2000 } 2001