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