aboutsummaryrefslogtreecommitdiffstats
path: root/mm/nommu.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/nommu.c')
-rw-r--r--mm/nommu.c1054
1 files changed, 723 insertions, 331 deletions
diff --git a/mm/nommu.c b/mm/nommu.c
index 7695dc850785..2fcf47d449b4 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -6,11 +6,11 @@
6 * 6 *
7 * See Documentation/nommu-mmap.txt 7 * See Documentation/nommu-mmap.txt
8 * 8 *
9 * Copyright (c) 2004-2005 David Howells <dhowells@redhat.com> 9 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
10 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com> 10 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org> 11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com> 12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
13 * Copyright (c) 2007 Paul Mundt <lethal@linux-sh.org> 13 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
14 */ 14 */
15 15
16#include <linux/module.h> 16#include <linux/module.h>
@@ -33,6 +33,28 @@
33#include <asm/uaccess.h> 33#include <asm/uaccess.h>
34#include <asm/tlb.h> 34#include <asm/tlb.h>
35#include <asm/tlbflush.h> 35#include <asm/tlbflush.h>
36#include "internal.h"
37
38static inline __attribute__((format(printf, 1, 2)))
39void no_printk(const char *fmt, ...)
40{
41}
42
43#if 0
44#define kenter(FMT, ...) \
45 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
46#define kleave(FMT, ...) \
47 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
48#define kdebug(FMT, ...) \
49 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
50#else
51#define kenter(FMT, ...) \
52 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
53#define kleave(FMT, ...) \
54 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
55#define kdebug(FMT, ...) \
56 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
57#endif
36 58
37#include "internal.h" 59#include "internal.h"
38 60
@@ -40,19 +62,22 @@ void *high_memory;
40struct page *mem_map; 62struct page *mem_map;
41unsigned long max_mapnr; 63unsigned long max_mapnr;
42unsigned long num_physpages; 64unsigned long num_physpages;
43unsigned long askedalloc, realalloc;
44atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0); 65atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
45int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */ 66int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
46int sysctl_overcommit_ratio = 50; /* default is 50% */ 67int sysctl_overcommit_ratio = 50; /* default is 50% */
47int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; 68int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
69int sysctl_nr_trim_pages = 1; /* page trimming behaviour */
48int heap_stack_gap = 0; 70int heap_stack_gap = 0;
49 71
72atomic_t mmap_pages_allocated;
73
50EXPORT_SYMBOL(mem_map); 74EXPORT_SYMBOL(mem_map);
51EXPORT_SYMBOL(num_physpages); 75EXPORT_SYMBOL(num_physpages);
52 76
53/* list of shareable VMAs */ 77/* list of mapped, potentially shareable regions */
54struct rb_root nommu_vma_tree = RB_ROOT; 78static struct kmem_cache *vm_region_jar;
55DECLARE_RWSEM(nommu_vma_sem); 79struct rb_root nommu_region_tree = RB_ROOT;
80DECLARE_RWSEM(nommu_region_sem);
56 81
57struct vm_operations_struct generic_file_vm_ops = { 82struct vm_operations_struct generic_file_vm_ops = {
58}; 83};
@@ -86,7 +111,7 @@ do_expand:
86 i_size_write(inode, offset); 111 i_size_write(inode, offset);
87 112
88out_truncate: 113out_truncate:
89 if (inode->i_op && inode->i_op->truncate) 114 if (inode->i_op->truncate)
90 inode->i_op->truncate(inode); 115 inode->i_op->truncate(inode);
91 return 0; 116 return 0;
92out_sig: 117out_sig:
@@ -124,6 +149,20 @@ unsigned int kobjsize(const void *objp)
124 return ksize(objp); 149 return ksize(objp);
125 150
126 /* 151 /*
152 * If it's not a compound page, see if we have a matching VMA
153 * region. This test is intentionally done in reverse order,
154 * so if there's no VMA, we still fall through and hand back
155 * PAGE_SIZE for 0-order pages.
156 */
157 if (!PageCompound(page)) {
158 struct vm_area_struct *vma;
159
160 vma = find_vma(current->mm, (unsigned long)objp);
161 if (vma)
162 return vma->vm_end - vma->vm_start;
163 }
164
165 /*
127 * The ksize() function is only guaranteed to work for pointers 166 * The ksize() function is only guaranteed to work for pointers
128 * returned by kmalloc(). So handle arbitrary pointers here. 167 * returned by kmalloc(). So handle arbitrary pointers here.
129 */ 168 */
@@ -355,6 +394,24 @@ void vunmap(const void *addr)
355} 394}
356EXPORT_SYMBOL(vunmap); 395EXPORT_SYMBOL(vunmap);
357 396
397void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
398{
399 BUG();
400 return NULL;
401}
402EXPORT_SYMBOL(vm_map_ram);
403
404void vm_unmap_ram(const void *mem, unsigned int count)
405{
406 BUG();
407}
408EXPORT_SYMBOL(vm_unmap_ram);
409
410void vm_unmap_aliases(void)
411{
412}
413EXPORT_SYMBOL_GPL(vm_unmap_aliases);
414
358/* 415/*
359 * Implement a stub for vmalloc_sync_all() if the architecture chose not to 416 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
360 * have one. 417 * have one.
@@ -377,7 +434,7 @@ EXPORT_SYMBOL(vm_insert_page);
377 * to a regular file. in this case, the unmapping will need 434 * to a regular file. in this case, the unmapping will need
378 * to invoke file system routines that need the global lock. 435 * to invoke file system routines that need the global lock.
379 */ 436 */
380asmlinkage unsigned long sys_brk(unsigned long brk) 437SYSCALL_DEFINE1(brk, unsigned long, brk)
381{ 438{
382 struct mm_struct *mm = current->mm; 439 struct mm_struct *mm = current->mm;
383 440
@@ -401,129 +458,178 @@ asmlinkage unsigned long sys_brk(unsigned long brk)
401 return mm->brk = brk; 458 return mm->brk = brk;
402} 459}
403 460
404#ifdef DEBUG 461/*
405static void show_process_blocks(void) 462 * initialise the VMA and region record slabs
463 */
464void __init mmap_init(void)
406{ 465{
407 struct vm_list_struct *vml; 466 vm_region_jar = kmem_cache_create("vm_region_jar",
408 467 sizeof(struct vm_region), 0,
409 printk("Process blocks %d:", current->pid); 468 SLAB_PANIC, NULL);
410 469 vm_area_cachep = kmem_cache_create("vm_area_struct",
411 for (vml = &current->mm->context.vmlist; vml; vml = vml->next) { 470 sizeof(struct vm_area_struct), 0,
412 printk(" %p: %p", vml, vml->vma); 471 SLAB_PANIC, NULL);
413 if (vml->vma)
414 printk(" (%d @%lx #%d)",
415 kobjsize((void *) vml->vma->vm_start),
416 vml->vma->vm_start,
417 atomic_read(&vml->vma->vm_usage));
418 printk(vml->next ? " ->" : ".\n");
419 }
420} 472}
421#endif /* DEBUG */
422 473
423/* 474/*
424 * add a VMA into a process's mm_struct in the appropriate place in the list 475 * validate the region tree
425 * - should be called with mm->mmap_sem held writelocked 476 * - the caller must hold the region lock
426 */ 477 */
427static void add_vma_to_mm(struct mm_struct *mm, struct vm_list_struct *vml) 478#ifdef CONFIG_DEBUG_NOMMU_REGIONS
479static noinline void validate_nommu_regions(void)
428{ 480{
429 struct vm_list_struct **ppv; 481 struct vm_region *region, *last;
430 482 struct rb_node *p, *lastp;
431 for (ppv = &current->mm->context.vmlist; *ppv; ppv = &(*ppv)->next) 483
432 if ((*ppv)->vma->vm_start > vml->vma->vm_start) 484 lastp = rb_first(&nommu_region_tree);
433 break; 485 if (!lastp)
434 486 return;
435 vml->next = *ppv; 487
436 *ppv = vml; 488 last = rb_entry(lastp, struct vm_region, vm_rb);
489 if (unlikely(last->vm_end <= last->vm_start))
490 BUG();
491 if (unlikely(last->vm_top < last->vm_end))
492 BUG();
493
494 while ((p = rb_next(lastp))) {
495 region = rb_entry(p, struct vm_region, vm_rb);
496 last = rb_entry(lastp, struct vm_region, vm_rb);
497
498 if (unlikely(region->vm_end <= region->vm_start))
499 BUG();
500 if (unlikely(region->vm_top < region->vm_end))
501 BUG();
502 if (unlikely(region->vm_start < last->vm_top))
503 BUG();
504
505 lastp = p;
506 }
437} 507}
508#else
509#define validate_nommu_regions() do {} while(0)
510#endif
438 511
439/* 512/*
440 * look up the first VMA in which addr resides, NULL if none 513 * add a region into the global tree
441 * - should be called with mm->mmap_sem at least held readlocked
442 */ 514 */
443struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) 515static void add_nommu_region(struct vm_region *region)
444{ 516{
445 struct vm_list_struct *loop, *vml; 517 struct vm_region *pregion;
518 struct rb_node **p, *parent;
446 519
447 /* search the vm_start ordered list */ 520 validate_nommu_regions();
448 vml = NULL; 521
449 for (loop = mm->context.vmlist; loop; loop = loop->next) { 522 BUG_ON(region->vm_start & ~PAGE_MASK);
450 if (loop->vma->vm_start > addr) 523
451 break; 524 parent = NULL;
452 vml = loop; 525 p = &nommu_region_tree.rb_node;
526 while (*p) {
527 parent = *p;
528 pregion = rb_entry(parent, struct vm_region, vm_rb);
529 if (region->vm_start < pregion->vm_start)
530 p = &(*p)->rb_left;
531 else if (region->vm_start > pregion->vm_start)
532 p = &(*p)->rb_right;
533 else if (pregion == region)
534 return;
535 else
536 BUG();
453 } 537 }
454 538
455 if (vml && vml->vma->vm_end > addr) 539 rb_link_node(&region->vm_rb, parent, p);
456 return vml->vma; 540 rb_insert_color(&region->vm_rb, &nommu_region_tree);
457 541
458 return NULL; 542 validate_nommu_regions();
459} 543}
460EXPORT_SYMBOL(find_vma);
461 544
462/* 545/*
463 * find a VMA 546 * delete a region from the global tree
464 * - we don't extend stack VMAs under NOMMU conditions
465 */ 547 */
466struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr) 548static void delete_nommu_region(struct vm_region *region)
467{ 549{
468 return find_vma(mm, addr); 550 BUG_ON(!nommu_region_tree.rb_node);
469}
470 551
471int expand_stack(struct vm_area_struct *vma, unsigned long address) 552 validate_nommu_regions();
472{ 553 rb_erase(&region->vm_rb, &nommu_region_tree);
473 return -ENOMEM; 554 validate_nommu_regions();
474} 555}
475 556
476/* 557/*
477 * look up the first VMA exactly that exactly matches addr 558 * free a contiguous series of pages
478 * - should be called with mm->mmap_sem at least held readlocked
479 */ 559 */
480static inline struct vm_area_struct *find_vma_exact(struct mm_struct *mm, 560static void free_page_series(unsigned long from, unsigned long to)
481 unsigned long addr)
482{ 561{
483 struct vm_list_struct *vml; 562 for (; from < to; from += PAGE_SIZE) {
484 563 struct page *page = virt_to_page(from);
485 /* search the vm_start ordered list */ 564
486 for (vml = mm->context.vmlist; vml; vml = vml->next) { 565 kdebug("- free %lx", from);
487 if (vml->vma->vm_start == addr) 566 atomic_dec(&mmap_pages_allocated);
488 return vml->vma; 567 if (page_count(page) != 1)
489 if (vml->vma->vm_start > addr) 568 kdebug("free page %p [%d]", page, page_count(page));
490 break; 569 put_page(page);
491 } 570 }
492
493 return NULL;
494} 571}
495 572
496/* 573/*
497 * find a VMA in the global tree 574 * release a reference to a region
575 * - the caller must hold the region semaphore, which this releases
576 * - the region may not have been added to the tree yet, in which case vm_top
577 * will equal vm_start
498 */ 578 */
499static inline struct vm_area_struct *find_nommu_vma(unsigned long start) 579static void __put_nommu_region(struct vm_region *region)
580 __releases(nommu_region_sem)
500{ 581{
501 struct vm_area_struct *vma; 582 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
502 struct rb_node *n = nommu_vma_tree.rb_node;
503 583
504 while (n) { 584 BUG_ON(!nommu_region_tree.rb_node);
505 vma = rb_entry(n, struct vm_area_struct, vm_rb);
506 585
507 if (start < vma->vm_start) 586 if (atomic_dec_and_test(&region->vm_usage)) {
508 n = n->rb_left; 587 if (region->vm_top > region->vm_start)
509 else if (start > vma->vm_start) 588 delete_nommu_region(region);
510 n = n->rb_right; 589 up_write(&nommu_region_sem);
511 else 590
512 return vma; 591 if (region->vm_file)
592 fput(region->vm_file);
593
594 /* IO memory and memory shared directly out of the pagecache
595 * from ramfs/tmpfs mustn't be released here */
596 if (region->vm_flags & VM_MAPPED_COPY) {
597 kdebug("free series");
598 free_page_series(region->vm_start, region->vm_top);
599 }
600 kmem_cache_free(vm_region_jar, region);
601 } else {
602 up_write(&nommu_region_sem);
513 } 603 }
604}
514 605
515 return NULL; 606/*
607 * release a reference to a region
608 */
609static void put_nommu_region(struct vm_region *region)
610{
611 down_write(&nommu_region_sem);
612 __put_nommu_region(region);
516} 613}
517 614
518/* 615/*
519 * add a VMA in the global tree 616 * add a VMA into a process's mm_struct in the appropriate place in the list
617 * and tree and add to the address space's page tree also if not an anonymous
618 * page
619 * - should be called with mm->mmap_sem held writelocked
520 */ 620 */
521static void add_nommu_vma(struct vm_area_struct *vma) 621static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
522{ 622{
523 struct vm_area_struct *pvma; 623 struct vm_area_struct *pvma, **pp;
524 struct address_space *mapping; 624 struct address_space *mapping;
525 struct rb_node **p = &nommu_vma_tree.rb_node; 625 struct rb_node **p, *parent;
526 struct rb_node *parent = NULL; 626
627 kenter(",%p", vma);
628
629 BUG_ON(!vma->vm_region);
630
631 mm->map_count++;
632 vma->vm_mm = mm;
527 633
528 /* add the VMA to the mapping */ 634 /* add the VMA to the mapping */
529 if (vma->vm_file) { 635 if (vma->vm_file) {
@@ -534,42 +640,62 @@ static void add_nommu_vma(struct vm_area_struct *vma)
534 flush_dcache_mmap_unlock(mapping); 640 flush_dcache_mmap_unlock(mapping);
535 } 641 }
536 642
537 /* add the VMA to the master list */ 643 /* add the VMA to the tree */
644 parent = NULL;
645 p = &mm->mm_rb.rb_node;
538 while (*p) { 646 while (*p) {
539 parent = *p; 647 parent = *p;
540 pvma = rb_entry(parent, struct vm_area_struct, vm_rb); 648 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
541 649
542 if (vma->vm_start < pvma->vm_start) { 650 /* sort by: start addr, end addr, VMA struct addr in that order
651 * (the latter is necessary as we may get identical VMAs) */
652 if (vma->vm_start < pvma->vm_start)
543 p = &(*p)->rb_left; 653 p = &(*p)->rb_left;
544 } 654 else if (vma->vm_start > pvma->vm_start)
545 else if (vma->vm_start > pvma->vm_start) {
546 p = &(*p)->rb_right; 655 p = &(*p)->rb_right;
547 } 656 else if (vma->vm_end < pvma->vm_end)
548 else { 657 p = &(*p)->rb_left;
549 /* mappings are at the same address - this can only 658 else if (vma->vm_end > pvma->vm_end)
550 * happen for shared-mem chardevs and shared file 659 p = &(*p)->rb_right;
551 * mappings backed by ramfs/tmpfs */ 660 else if (vma < pvma)
552 BUG_ON(!(pvma->vm_flags & VM_SHARED)); 661 p = &(*p)->rb_left;
553 662 else if (vma > pvma)
554 if (vma < pvma) 663 p = &(*p)->rb_right;
555 p = &(*p)->rb_left; 664 else
556 else if (vma > pvma) 665 BUG();
557 p = &(*p)->rb_right;
558 else
559 BUG();
560 }
561 } 666 }
562 667
563 rb_link_node(&vma->vm_rb, parent, p); 668 rb_link_node(&vma->vm_rb, parent, p);
564 rb_insert_color(&vma->vm_rb, &nommu_vma_tree); 669 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
670
671 /* add VMA to the VMA list also */
672 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
673 if (pvma->vm_start > vma->vm_start)
674 break;
675 if (pvma->vm_start < vma->vm_start)
676 continue;
677 if (pvma->vm_end < vma->vm_end)
678 break;
679 }
680
681 vma->vm_next = *pp;
682 *pp = vma;
565} 683}
566 684
567/* 685/*
568 * delete a VMA from the global list 686 * delete a VMA from its owning mm_struct and address space
569 */ 687 */
570static void delete_nommu_vma(struct vm_area_struct *vma) 688static void delete_vma_from_mm(struct vm_area_struct *vma)
571{ 689{
690 struct vm_area_struct **pp;
572 struct address_space *mapping; 691 struct address_space *mapping;
692 struct mm_struct *mm = vma->vm_mm;
693
694 kenter("%p", vma);
695
696 mm->map_count--;
697 if (mm->mmap_cache == vma)
698 mm->mmap_cache = NULL;
573 699
574 /* remove the VMA from the mapping */ 700 /* remove the VMA from the mapping */
575 if (vma->vm_file) { 701 if (vma->vm_file) {
@@ -580,8 +706,115 @@ static void delete_nommu_vma(struct vm_area_struct *vma)
580 flush_dcache_mmap_unlock(mapping); 706 flush_dcache_mmap_unlock(mapping);
581 } 707 }
582 708
583 /* remove from the master list */ 709 /* remove from the MM's tree and list */
584 rb_erase(&vma->vm_rb, &nommu_vma_tree); 710 rb_erase(&vma->vm_rb, &mm->mm_rb);
711 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
712 if (*pp == vma) {
713 *pp = vma->vm_next;
714 break;
715 }
716 }
717
718 vma->vm_mm = NULL;
719}
720
721/*
722 * destroy a VMA record
723 */
724static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
725{
726 kenter("%p", vma);
727 if (vma->vm_ops && vma->vm_ops->close)
728 vma->vm_ops->close(vma);
729 if (vma->vm_file) {
730 fput(vma->vm_file);
731 if (vma->vm_flags & VM_EXECUTABLE)
732 removed_exe_file_vma(mm);
733 }
734 put_nommu_region(vma->vm_region);
735 kmem_cache_free(vm_area_cachep, vma);
736}
737
738/*
739 * look up the first VMA in which addr resides, NULL if none
740 * - should be called with mm->mmap_sem at least held readlocked
741 */
742struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
743{
744 struct vm_area_struct *vma;
745 struct rb_node *n = mm->mm_rb.rb_node;
746
747 /* check the cache first */
748 vma = mm->mmap_cache;
749 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
750 return vma;
751
752 /* trawl the tree (there may be multiple mappings in which addr
753 * resides) */
754 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
755 vma = rb_entry(n, struct vm_area_struct, vm_rb);
756 if (vma->vm_start > addr)
757 return NULL;
758 if (vma->vm_end > addr) {
759 mm->mmap_cache = vma;
760 return vma;
761 }
762 }
763
764 return NULL;
765}
766EXPORT_SYMBOL(find_vma);
767
768/*
769 * find a VMA
770 * - we don't extend stack VMAs under NOMMU conditions
771 */
772struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
773{
774 return find_vma(mm, addr);
775}
776
777/*
778 * expand a stack to a given address
779 * - not supported under NOMMU conditions
780 */
781int expand_stack(struct vm_area_struct *vma, unsigned long address)
782{
783 return -ENOMEM;
784}
785
786/*
787 * look up the first VMA exactly that exactly matches addr
788 * - should be called with mm->mmap_sem at least held readlocked
789 */
790static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
791 unsigned long addr,
792 unsigned long len)
793{
794 struct vm_area_struct *vma;
795 struct rb_node *n = mm->mm_rb.rb_node;
796 unsigned long end = addr + len;
797
798 /* check the cache first */
799 vma = mm->mmap_cache;
800 if (vma && vma->vm_start == addr && vma->vm_end == end)
801 return vma;
802
803 /* trawl the tree (there may be multiple mappings in which addr
804 * resides) */
805 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
806 vma = rb_entry(n, struct vm_area_struct, vm_rb);
807 if (vma->vm_start < addr)
808 continue;
809 if (vma->vm_start > addr)
810 return NULL;
811 if (vma->vm_end == end) {
812 mm->mmap_cache = vma;
813 return vma;
814 }
815 }
816
817 return NULL;
585} 818}
586 819
587/* 820/*
@@ -596,7 +829,7 @@ static int validate_mmap_request(struct file *file,
596 unsigned long pgoff, 829 unsigned long pgoff,
597 unsigned long *_capabilities) 830 unsigned long *_capabilities)
598{ 831{
599 unsigned long capabilities; 832 unsigned long capabilities, rlen;
600 unsigned long reqprot = prot; 833 unsigned long reqprot = prot;
601 int ret; 834 int ret;
602 835
@@ -616,12 +849,12 @@ static int validate_mmap_request(struct file *file,
616 return -EINVAL; 849 return -EINVAL;
617 850
618 /* Careful about overflows.. */ 851 /* Careful about overflows.. */
619 len = PAGE_ALIGN(len); 852 rlen = PAGE_ALIGN(len);
620 if (!len || len > TASK_SIZE) 853 if (!rlen || rlen > TASK_SIZE)
621 return -ENOMEM; 854 return -ENOMEM;
622 855
623 /* offset overflow? */ 856 /* offset overflow? */
624 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) 857 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
625 return -EOVERFLOW; 858 return -EOVERFLOW;
626 859
627 if (file) { 860 if (file) {
@@ -795,13 +1028,18 @@ static unsigned long determine_vm_flags(struct file *file,
795} 1028}
796 1029
797/* 1030/*
798 * set up a shared mapping on a file 1031 * set up a shared mapping on a file (the driver or filesystem provides and
1032 * pins the storage)
799 */ 1033 */
800static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len) 1034static int do_mmap_shared_file(struct vm_area_struct *vma)
801{ 1035{
802 int ret; 1036 int ret;
803 1037
804 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1038 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1039 if (ret == 0) {
1040 vma->vm_region->vm_top = vma->vm_region->vm_end;
1041 return ret;
1042 }
805 if (ret != -ENOSYS) 1043 if (ret != -ENOSYS)
806 return ret; 1044 return ret;
807 1045
@@ -815,10 +1053,14 @@ static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
815/* 1053/*
816 * set up a private mapping or an anonymous shared mapping 1054 * set up a private mapping or an anonymous shared mapping
817 */ 1055 */
818static int do_mmap_private(struct vm_area_struct *vma, unsigned long len) 1056static int do_mmap_private(struct vm_area_struct *vma,
1057 struct vm_region *region,
1058 unsigned long len)
819{ 1059{
1060 struct page *pages;
1061 unsigned long total, point, n, rlen;
820 void *base; 1062 void *base;
821 int ret; 1063 int ret, order;
822 1064
823 /* invoke the file's mapping function so that it can keep track of 1065 /* invoke the file's mapping function so that it can keep track of
824 * shared mappings on devices or memory 1066 * shared mappings on devices or memory
@@ -826,34 +1068,63 @@ static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
826 */ 1068 */
827 if (vma->vm_file) { 1069 if (vma->vm_file) {
828 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma); 1070 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
829 if (ret != -ENOSYS) { 1071 if (ret == 0) {
830 /* shouldn't return success if we're not sharing */ 1072 /* shouldn't return success if we're not sharing */
831 BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE)); 1073 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
832 return ret; /* success or a real error */ 1074 vma->vm_region->vm_top = vma->vm_region->vm_end;
1075 return ret;
833 } 1076 }
1077 if (ret != -ENOSYS)
1078 return ret;
834 1079
835 /* getting an ENOSYS error indicates that direct mmap isn't 1080 /* getting an ENOSYS error indicates that direct mmap isn't
836 * possible (as opposed to tried but failed) so we'll try to 1081 * possible (as opposed to tried but failed) so we'll try to
837 * make a private copy of the data and map that instead */ 1082 * make a private copy of the data and map that instead */
838 } 1083 }
839 1084
1085 rlen = PAGE_ALIGN(len);
1086
840 /* allocate some memory to hold the mapping 1087 /* allocate some memory to hold the mapping
841 * - note that this may not return a page-aligned address if the object 1088 * - note that this may not return a page-aligned address if the object
842 * we're allocating is smaller than a page 1089 * we're allocating is smaller than a page
843 */ 1090 */
844 base = kmalloc(len, GFP_KERNEL|__GFP_COMP); 1091 order = get_order(rlen);
845 if (!base) 1092 kdebug("alloc order %d for %lx", order, len);
1093
1094 pages = alloc_pages(GFP_KERNEL, order);
1095 if (!pages)
846 goto enomem; 1096 goto enomem;
847 1097
848 vma->vm_start = (unsigned long) base; 1098 total = 1 << order;
849 vma->vm_end = vma->vm_start + len; 1099 atomic_add(total, &mmap_pages_allocated);
850 vma->vm_flags |= VM_MAPPED_COPY; 1100
1101 point = rlen >> PAGE_SHIFT;
1102
1103 /* we allocated a power-of-2 sized page set, so we may want to trim off
1104 * the excess */
1105 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1106 while (total > point) {
1107 order = ilog2(total - point);
1108 n = 1 << order;
1109 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1110 atomic_sub(n, &mmap_pages_allocated);
1111 total -= n;
1112 set_page_refcounted(pages + total);
1113 __free_pages(pages + total, order);
1114 }
1115 }
851 1116
852#ifdef WARN_ON_SLACK 1117 for (point = 1; point < total; point++)
853 if (len + WARN_ON_SLACK <= kobjsize(result)) 1118 set_page_refcounted(&pages[point]);
854 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n", 1119
855 len, current->pid, kobjsize(result) - len); 1120 base = page_address(pages);
856#endif 1121 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1122 region->vm_start = (unsigned long) base;
1123 region->vm_end = region->vm_start + rlen;
1124 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1125
1126 vma->vm_start = region->vm_start;
1127 vma->vm_end = region->vm_start + len;
857 1128
858 if (vma->vm_file) { 1129 if (vma->vm_file) {
859 /* read the contents of a file into the copy */ 1130 /* read the contents of a file into the copy */
@@ -865,31 +1136,33 @@ static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
865 1136
866 old_fs = get_fs(); 1137 old_fs = get_fs();
867 set_fs(KERNEL_DS); 1138 set_fs(KERNEL_DS);
868 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos); 1139 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
869 set_fs(old_fs); 1140 set_fs(old_fs);
870 1141
871 if (ret < 0) 1142 if (ret < 0)
872 goto error_free; 1143 goto error_free;
873 1144
874 /* clear the last little bit */ 1145 /* clear the last little bit */
875 if (ret < len) 1146 if (ret < rlen)
876 memset(base + ret, 0, len - ret); 1147 memset(base + ret, 0, rlen - ret);
877 1148
878 } else { 1149 } else {
879 /* if it's an anonymous mapping, then just clear it */ 1150 /* if it's an anonymous mapping, then just clear it */
880 memset(base, 0, len); 1151 memset(base, 0, rlen);
881 } 1152 }
882 1153
883 return 0; 1154 return 0;
884 1155
885error_free: 1156error_free:
886 kfree(base); 1157 free_page_series(region->vm_start, region->vm_end);
887 vma->vm_start = 0; 1158 region->vm_start = vma->vm_start = 0;
1159 region->vm_end = vma->vm_end = 0;
1160 region->vm_top = 0;
888 return ret; 1161 return ret;
889 1162
890enomem: 1163enomem:
891 printk("Allocation of length %lu from process %d failed\n", 1164 printk("Allocation of length %lu from process %d (%s) failed\n",
892 len, current->pid); 1165 len, current->pid, current->comm);
893 show_free_areas(); 1166 show_free_areas();
894 return -ENOMEM; 1167 return -ENOMEM;
895} 1168}
@@ -904,13 +1177,14 @@ unsigned long do_mmap_pgoff(struct file *file,
904 unsigned long flags, 1177 unsigned long flags,
905 unsigned long pgoff) 1178 unsigned long pgoff)
906{ 1179{
907 struct vm_list_struct *vml = NULL; 1180 struct vm_area_struct *vma;
908 struct vm_area_struct *vma = NULL; 1181 struct vm_region *region;
909 struct rb_node *rb; 1182 struct rb_node *rb;
910 unsigned long capabilities, vm_flags; 1183 unsigned long capabilities, vm_flags, result;
911 void *result;
912 int ret; 1184 int ret;
913 1185
1186 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1187
914 if (!(flags & MAP_FIXED)) 1188 if (!(flags & MAP_FIXED))
915 addr = round_hint_to_min(addr); 1189 addr = round_hint_to_min(addr);
916 1190
@@ -918,73 +1192,120 @@ unsigned long do_mmap_pgoff(struct file *file,
918 * mapping */ 1192 * mapping */
919 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff, 1193 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
920 &capabilities); 1194 &capabilities);
921 if (ret < 0) 1195 if (ret < 0) {
1196 kleave(" = %d [val]", ret);
922 return ret; 1197 return ret;
1198 }
923 1199
924 /* we've determined that we can make the mapping, now translate what we 1200 /* we've determined that we can make the mapping, now translate what we
925 * now know into VMA flags */ 1201 * now know into VMA flags */
926 vm_flags = determine_vm_flags(file, prot, flags, capabilities); 1202 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
927 1203
928 /* we're going to need to record the mapping if it works */ 1204 /* we're going to need to record the mapping */
929 vml = kzalloc(sizeof(struct vm_list_struct), GFP_KERNEL); 1205 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
930 if (!vml) 1206 if (!region)
931 goto error_getting_vml; 1207 goto error_getting_region;
1208
1209 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1210 if (!vma)
1211 goto error_getting_vma;
1212
1213 atomic_set(&region->vm_usage, 1);
1214 region->vm_flags = vm_flags;
1215 region->vm_pgoff = pgoff;
1216
1217 INIT_LIST_HEAD(&vma->anon_vma_node);
1218 vma->vm_flags = vm_flags;
1219 vma->vm_pgoff = pgoff;
1220
1221 if (file) {
1222 region->vm_file = file;
1223 get_file(file);
1224 vma->vm_file = file;
1225 get_file(file);
1226 if (vm_flags & VM_EXECUTABLE) {
1227 added_exe_file_vma(current->mm);
1228 vma->vm_mm = current->mm;
1229 }
1230 }
932 1231
933 down_write(&nommu_vma_sem); 1232 down_write(&nommu_region_sem);
934 1233
935 /* if we want to share, we need to check for VMAs created by other 1234 /* if we want to share, we need to check for regions created by other
936 * mmap() calls that overlap with our proposed mapping 1235 * mmap() calls that overlap with our proposed mapping
937 * - we can only share with an exact match on most regular files 1236 * - we can only share with a superset match on most regular files
938 * - shared mappings on character devices and memory backed files are 1237 * - shared mappings on character devices and memory backed files are
939 * permitted to overlap inexactly as far as we are concerned for in 1238 * permitted to overlap inexactly as far as we are concerned for in
940 * these cases, sharing is handled in the driver or filesystem rather 1239 * these cases, sharing is handled in the driver or filesystem rather
941 * than here 1240 * than here
942 */ 1241 */
943 if (vm_flags & VM_MAYSHARE) { 1242 if (vm_flags & VM_MAYSHARE) {
944 unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1243 struct vm_region *pregion;
945 unsigned long vmpglen; 1244 unsigned long pglen, rpglen, pgend, rpgend, start;
946 1245
947 /* suppress VMA sharing for shared regions */ 1246 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
948 if (vm_flags & VM_SHARED && 1247 pgend = pgoff + pglen;
949 capabilities & BDI_CAP_MAP_DIRECT)
950 goto dont_share_VMAs;
951 1248
952 for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) { 1249 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
953 vma = rb_entry(rb, struct vm_area_struct, vm_rb); 1250 pregion = rb_entry(rb, struct vm_region, vm_rb);
954 1251
955 if (!(vma->vm_flags & VM_MAYSHARE)) 1252 if (!(pregion->vm_flags & VM_MAYSHARE))
956 continue; 1253 continue;
957 1254
958 /* search for overlapping mappings on the same file */ 1255 /* search for overlapping mappings on the same file */
959 if (vma->vm_file->f_path.dentry->d_inode != file->f_path.dentry->d_inode) 1256 if (pregion->vm_file->f_path.dentry->d_inode !=
1257 file->f_path.dentry->d_inode)
960 continue; 1258 continue;
961 1259
962 if (vma->vm_pgoff >= pgoff + pglen) 1260 if (pregion->vm_pgoff >= pgend)
963 continue; 1261 continue;
964 1262
965 vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1; 1263 rpglen = pregion->vm_end - pregion->vm_start;
966 vmpglen >>= PAGE_SHIFT; 1264 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
967 if (pgoff >= vma->vm_pgoff + vmpglen) 1265 rpgend = pregion->vm_pgoff + rpglen;
1266 if (pgoff >= rpgend)
968 continue; 1267 continue;
969 1268
970 /* handle inexactly overlapping matches between mappings */ 1269 /* handle inexactly overlapping matches between
971 if (vma->vm_pgoff != pgoff || vmpglen != pglen) { 1270 * mappings */
1271 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1272 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1273 /* new mapping is not a subset of the region */
972 if (!(capabilities & BDI_CAP_MAP_DIRECT)) 1274 if (!(capabilities & BDI_CAP_MAP_DIRECT))
973 goto sharing_violation; 1275 goto sharing_violation;
974 continue; 1276 continue;
975 } 1277 }
976 1278
977 /* we've found a VMA we can share */ 1279 /* we've found a region we can share */
978 atomic_inc(&vma->vm_usage); 1280 atomic_inc(&pregion->vm_usage);
979 1281 vma->vm_region = pregion;
980 vml->vma = vma; 1282 start = pregion->vm_start;
981 result = (void *) vma->vm_start; 1283 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
982 goto shared; 1284 vma->vm_start = start;
1285 vma->vm_end = start + len;
1286
1287 if (pregion->vm_flags & VM_MAPPED_COPY) {
1288 kdebug("share copy");
1289 vma->vm_flags |= VM_MAPPED_COPY;
1290 } else {
1291 kdebug("share mmap");
1292 ret = do_mmap_shared_file(vma);
1293 if (ret < 0) {
1294 vma->vm_region = NULL;
1295 vma->vm_start = 0;
1296 vma->vm_end = 0;
1297 atomic_dec(&pregion->vm_usage);
1298 pregion = NULL;
1299 goto error_just_free;
1300 }
1301 }
1302 fput(region->vm_file);
1303 kmem_cache_free(vm_region_jar, region);
1304 region = pregion;
1305 result = start;
1306 goto share;
983 } 1307 }
984 1308
985 dont_share_VMAs:
986 vma = NULL;
987
988 /* obtain the address at which to make a shared mapping 1309 /* obtain the address at which to make a shared mapping
989 * - this is the hook for quasi-memory character devices to 1310 * - this is the hook for quasi-memory character devices to
990 * tell us the location of a shared mapping 1311 * tell us the location of a shared mapping
@@ -995,113 +1316,93 @@ unsigned long do_mmap_pgoff(struct file *file,
995 if (IS_ERR((void *) addr)) { 1316 if (IS_ERR((void *) addr)) {
996 ret = addr; 1317 ret = addr;
997 if (ret != (unsigned long) -ENOSYS) 1318 if (ret != (unsigned long) -ENOSYS)
998 goto error; 1319 goto error_just_free;
999 1320
1000 /* the driver refused to tell us where to site 1321 /* the driver refused to tell us where to site
1001 * the mapping so we'll have to attempt to copy 1322 * the mapping so we'll have to attempt to copy
1002 * it */ 1323 * it */
1003 ret = (unsigned long) -ENODEV; 1324 ret = (unsigned long) -ENODEV;
1004 if (!(capabilities & BDI_CAP_MAP_COPY)) 1325 if (!(capabilities & BDI_CAP_MAP_COPY))
1005 goto error; 1326 goto error_just_free;
1006 1327
1007 capabilities &= ~BDI_CAP_MAP_DIRECT; 1328 capabilities &= ~BDI_CAP_MAP_DIRECT;
1329 } else {
1330 vma->vm_start = region->vm_start = addr;
1331 vma->vm_end = region->vm_end = addr + len;
1008 } 1332 }
1009 } 1333 }
1010 } 1334 }
1011 1335
1012 /* we're going to need a VMA struct as well */ 1336 vma->vm_region = region;
1013 vma = kzalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
1014 if (!vma)
1015 goto error_getting_vma;
1016
1017 INIT_LIST_HEAD(&vma->anon_vma_node);
1018 atomic_set(&vma->vm_usage, 1);
1019 if (file) {
1020 get_file(file);
1021 if (vm_flags & VM_EXECUTABLE) {
1022 added_exe_file_vma(current->mm);
1023 vma->vm_mm = current->mm;
1024 }
1025 }
1026 vma->vm_file = file;
1027 vma->vm_flags = vm_flags;
1028 vma->vm_start = addr;
1029 vma->vm_end = addr + len;
1030 vma->vm_pgoff = pgoff;
1031
1032 vml->vma = vma;
1033 1337
1034 /* set up the mapping */ 1338 /* set up the mapping */
1035 if (file && vma->vm_flags & VM_SHARED) 1339 if (file && vma->vm_flags & VM_SHARED)
1036 ret = do_mmap_shared_file(vma, len); 1340 ret = do_mmap_shared_file(vma);
1037 else 1341 else
1038 ret = do_mmap_private(vma, len); 1342 ret = do_mmap_private(vma, region, len);
1039 if (ret < 0) 1343 if (ret < 0)
1040 goto error; 1344 goto error_put_region;
1041
1042 /* okay... we have a mapping; now we have to register it */
1043 result = (void *) vma->vm_start;
1044 1345
1045 if (vma->vm_flags & VM_MAPPED_COPY) { 1346 add_nommu_region(region);
1046 realalloc += kobjsize(result);
1047 askedalloc += len;
1048 }
1049 1347
1050 realalloc += kobjsize(vma); 1348 /* okay... we have a mapping; now we have to register it */
1051 askedalloc += sizeof(*vma); 1349 result = vma->vm_start;
1052 1350
1053 current->mm->total_vm += len >> PAGE_SHIFT; 1351 current->mm->total_vm += len >> PAGE_SHIFT;
1054 1352
1055 add_nommu_vma(vma); 1353share:
1056 1354 add_vma_to_mm(current->mm, vma);
1057 shared:
1058 realalloc += kobjsize(vml);
1059 askedalloc += sizeof(*vml);
1060 1355
1061 add_vma_to_mm(current->mm, vml); 1356 up_write(&nommu_region_sem);
1062
1063 up_write(&nommu_vma_sem);
1064 1357
1065 if (prot & PROT_EXEC) 1358 if (prot & PROT_EXEC)
1066 flush_icache_range((unsigned long) result, 1359 flush_icache_range(result, result + len);
1067 (unsigned long) result + len);
1068 1360
1069#ifdef DEBUG 1361 kleave(" = %lx", result);
1070 printk("do_mmap:\n"); 1362 return result;
1071 show_process_blocks();
1072#endif
1073 1363
1074 return (unsigned long) result; 1364error_put_region:
1075 1365 __put_nommu_region(region);
1076 error:
1077 up_write(&nommu_vma_sem);
1078 kfree(vml);
1079 if (vma) { 1366 if (vma) {
1080 if (vma->vm_file) { 1367 if (vma->vm_file) {
1081 fput(vma->vm_file); 1368 fput(vma->vm_file);
1082 if (vma->vm_flags & VM_EXECUTABLE) 1369 if (vma->vm_flags & VM_EXECUTABLE)
1083 removed_exe_file_vma(vma->vm_mm); 1370 removed_exe_file_vma(vma->vm_mm);
1084 } 1371 }
1085 kfree(vma); 1372 kmem_cache_free(vm_area_cachep, vma);
1086 } 1373 }
1374 kleave(" = %d [pr]", ret);
1087 return ret; 1375 return ret;
1088 1376
1089 sharing_violation: 1377error_just_free:
1090 up_write(&nommu_vma_sem); 1378 up_write(&nommu_region_sem);
1091 printk("Attempt to share mismatched mappings\n"); 1379error:
1092 kfree(vml); 1380 fput(region->vm_file);
1093 return -EINVAL; 1381 kmem_cache_free(vm_region_jar, region);
1382 fput(vma->vm_file);
1383 if (vma->vm_flags & VM_EXECUTABLE)
1384 removed_exe_file_vma(vma->vm_mm);
1385 kmem_cache_free(vm_area_cachep, vma);
1386 kleave(" = %d", ret);
1387 return ret;
1094 1388
1095 error_getting_vma: 1389sharing_violation:
1096 up_write(&nommu_vma_sem); 1390 up_write(&nommu_region_sem);
1097 kfree(vml); 1391 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1098 printk("Allocation of vma for %lu byte allocation from process %d failed\n", 1392 ret = -EINVAL;
1393 goto error;
1394
1395error_getting_vma:
1396 kmem_cache_free(vm_region_jar, region);
1397 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1398 " from process %d failed\n",
1099 len, current->pid); 1399 len, current->pid);
1100 show_free_areas(); 1400 show_free_areas();
1101 return -ENOMEM; 1401 return -ENOMEM;
1102 1402
1103 error_getting_vml: 1403error_getting_region:
1104 printk("Allocation of vml for %lu byte allocation from process %d failed\n", 1404 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1405 " from process %d failed\n",
1105 len, current->pid); 1406 len, current->pid);
1106 show_free_areas(); 1407 show_free_areas();
1107 return -ENOMEM; 1408 return -ENOMEM;
@@ -1109,90 +1410,188 @@ unsigned long do_mmap_pgoff(struct file *file,
1109EXPORT_SYMBOL(do_mmap_pgoff); 1410EXPORT_SYMBOL(do_mmap_pgoff);
1110 1411
1111/* 1412/*
1112 * handle mapping disposal for uClinux 1413 * split a vma into two pieces at address 'addr', a new vma is allocated either
1414 * for the first part or the tail.
1113 */ 1415 */
1114static void put_vma(struct mm_struct *mm, struct vm_area_struct *vma) 1416int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1417 unsigned long addr, int new_below)
1115{ 1418{
1116 if (vma) { 1419 struct vm_area_struct *new;
1117 down_write(&nommu_vma_sem); 1420 struct vm_region *region;
1421 unsigned long npages;
1118 1422
1119 if (atomic_dec_and_test(&vma->vm_usage)) { 1423 kenter("");
1120 delete_nommu_vma(vma);
1121 1424
1122 if (vma->vm_ops && vma->vm_ops->close) 1425 /* we're only permitted to split anonymous regions that have a single
1123 vma->vm_ops->close(vma); 1426 * owner */
1427 if (vma->vm_file ||
1428 atomic_read(&vma->vm_region->vm_usage) != 1)
1429 return -ENOMEM;
1124 1430
1125 /* IO memory and memory shared directly out of the pagecache from 1431 if (mm->map_count >= sysctl_max_map_count)
1126 * ramfs/tmpfs mustn't be released here */ 1432 return -ENOMEM;
1127 if (vma->vm_flags & VM_MAPPED_COPY) {
1128 realalloc -= kobjsize((void *) vma->vm_start);
1129 askedalloc -= vma->vm_end - vma->vm_start;
1130 kfree((void *) vma->vm_start);
1131 }
1132 1433
1133 realalloc -= kobjsize(vma); 1434 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1134 askedalloc -= sizeof(*vma); 1435 if (!region)
1436 return -ENOMEM;
1135 1437
1136 if (vma->vm_file) { 1438 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1137 fput(vma->vm_file); 1439 if (!new) {
1138 if (vma->vm_flags & VM_EXECUTABLE) 1440 kmem_cache_free(vm_region_jar, region);
1139 removed_exe_file_vma(mm); 1441 return -ENOMEM;
1140 } 1442 }
1141 kfree(vma); 1443
1142 } 1444 /* most fields are the same, copy all, and then fixup */
1445 *new = *vma;
1446 *region = *vma->vm_region;
1447 new->vm_region = region;
1448
1449 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1143 1450
1144 up_write(&nommu_vma_sem); 1451 if (new_below) {
1452 region->vm_top = region->vm_end = new->vm_end = addr;
1453 } else {
1454 region->vm_start = new->vm_start = addr;
1455 region->vm_pgoff = new->vm_pgoff += npages;
1145 } 1456 }
1457
1458 if (new->vm_ops && new->vm_ops->open)
1459 new->vm_ops->open(new);
1460
1461 delete_vma_from_mm(vma);
1462 down_write(&nommu_region_sem);
1463 delete_nommu_region(vma->vm_region);
1464 if (new_below) {
1465 vma->vm_region->vm_start = vma->vm_start = addr;
1466 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1467 } else {
1468 vma->vm_region->vm_end = vma->vm_end = addr;
1469 vma->vm_region->vm_top = addr;
1470 }
1471 add_nommu_region(vma->vm_region);
1472 add_nommu_region(new->vm_region);
1473 up_write(&nommu_region_sem);
1474 add_vma_to_mm(mm, vma);
1475 add_vma_to_mm(mm, new);
1476 return 0;
1146} 1477}
1147 1478
1148/* 1479/*
1149 * release a mapping 1480 * shrink a VMA by removing the specified chunk from either the beginning or
1150 * - under NOMMU conditions the parameters must match exactly to the mapping to 1481 * the end
1151 * be removed
1152 */ 1482 */
1153int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len) 1483static int shrink_vma(struct mm_struct *mm,
1484 struct vm_area_struct *vma,
1485 unsigned long from, unsigned long to)
1154{ 1486{
1155 struct vm_list_struct *vml, **parent; 1487 struct vm_region *region;
1156 unsigned long end = addr + len;
1157 1488
1158#ifdef DEBUG 1489 kenter("");
1159 printk("do_munmap:\n");
1160#endif
1161 1490
1162 for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next) { 1491 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1163 if ((*parent)->vma->vm_start > addr) 1492 * and list */
1164 break; 1493 delete_vma_from_mm(vma);
1165 if ((*parent)->vma->vm_start == addr && 1494 if (from > vma->vm_start)
1166 ((len == 0) || ((*parent)->vma->vm_end == end))) 1495 vma->vm_end = from;
1167 goto found; 1496 else
1497 vma->vm_start = to;
1498 add_vma_to_mm(mm, vma);
1499
1500 /* cut the backing region down to size */
1501 region = vma->vm_region;
1502 BUG_ON(atomic_read(&region->vm_usage) != 1);
1503
1504 down_write(&nommu_region_sem);
1505 delete_nommu_region(region);
1506 if (from > region->vm_start) {
1507 to = region->vm_top;
1508 region->vm_top = region->vm_end = from;
1509 } else {
1510 region->vm_start = to;
1168 } 1511 }
1512 add_nommu_region(region);
1513 up_write(&nommu_region_sem);
1169 1514
1170 printk("munmap of non-mmaped memory by process %d (%s): %p\n", 1515 free_page_series(from, to);
1171 current->pid, current->comm, (void *) addr); 1516 return 0;
1172 return -EINVAL; 1517}
1173 1518
1174 found: 1519/*
1175 vml = *parent; 1520 * release a mapping
1521 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1522 * VMA, though it need not cover the whole VMA
1523 */
1524int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1525{
1526 struct vm_area_struct *vma;
1527 struct rb_node *rb;
1528 unsigned long end = start + len;
1529 int ret;
1176 1530
1177 put_vma(mm, vml->vma); 1531 kenter(",%lx,%zx", start, len);
1178 1532
1179 *parent = vml->next; 1533 if (len == 0)
1180 realalloc -= kobjsize(vml); 1534 return -EINVAL;
1181 askedalloc -= sizeof(*vml);
1182 kfree(vml);
1183 1535
1184 update_hiwater_vm(mm); 1536 /* find the first potentially overlapping VMA */
1185 mm->total_vm -= len >> PAGE_SHIFT; 1537 vma = find_vma(mm, start);
1538 if (!vma) {
1539 printk(KERN_WARNING
1540 "munmap of memory not mmapped by process %d (%s):"
1541 " 0x%lx-0x%lx\n",
1542 current->pid, current->comm, start, start + len - 1);
1543 return -EINVAL;
1544 }
1186 1545
1187#ifdef DEBUG 1546 /* we're allowed to split an anonymous VMA but not a file-backed one */
1188 show_process_blocks(); 1547 if (vma->vm_file) {
1189#endif 1548 do {
1549 if (start > vma->vm_start) {
1550 kleave(" = -EINVAL [miss]");
1551 return -EINVAL;
1552 }
1553 if (end == vma->vm_end)
1554 goto erase_whole_vma;
1555 rb = rb_next(&vma->vm_rb);
1556 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1557 } while (rb);
1558 kleave(" = -EINVAL [split file]");
1559 return -EINVAL;
1560 } else {
1561 /* the chunk must be a subset of the VMA found */
1562 if (start == vma->vm_start && end == vma->vm_end)
1563 goto erase_whole_vma;
1564 if (start < vma->vm_start || end > vma->vm_end) {
1565 kleave(" = -EINVAL [superset]");
1566 return -EINVAL;
1567 }
1568 if (start & ~PAGE_MASK) {
1569 kleave(" = -EINVAL [unaligned start]");
1570 return -EINVAL;
1571 }
1572 if (end != vma->vm_end && end & ~PAGE_MASK) {
1573 kleave(" = -EINVAL [unaligned split]");
1574 return -EINVAL;
1575 }
1576 if (start != vma->vm_start && end != vma->vm_end) {
1577 ret = split_vma(mm, vma, start, 1);
1578 if (ret < 0) {
1579 kleave(" = %d [split]", ret);
1580 return ret;
1581 }
1582 }
1583 return shrink_vma(mm, vma, start, end);
1584 }
1190 1585
1586erase_whole_vma:
1587 delete_vma_from_mm(vma);
1588 delete_vma(mm, vma);
1589 kleave(" = 0");
1191 return 0; 1590 return 0;
1192} 1591}
1193EXPORT_SYMBOL(do_munmap); 1592EXPORT_SYMBOL(do_munmap);
1194 1593
1195asmlinkage long sys_munmap(unsigned long addr, size_t len) 1594SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1196{ 1595{
1197 int ret; 1596 int ret;
1198 struct mm_struct *mm = current->mm; 1597 struct mm_struct *mm = current->mm;
@@ -1204,32 +1603,26 @@ asmlinkage long sys_munmap(unsigned long addr, size_t len)
1204} 1603}
1205 1604
1206/* 1605/*
1207 * Release all mappings 1606 * release all the mappings made in a process's VM space
1208 */ 1607 */
1209void exit_mmap(struct mm_struct * mm) 1608void exit_mmap(struct mm_struct *mm)
1210{ 1609{
1211 struct vm_list_struct *tmp; 1610 struct vm_area_struct *vma;
1212
1213 if (mm) {
1214#ifdef DEBUG
1215 printk("Exit_mmap:\n");
1216#endif
1217 1611
1218 mm->total_vm = 0; 1612 if (!mm)
1613 return;
1219 1614
1220 while ((tmp = mm->context.vmlist)) { 1615 kenter("");
1221 mm->context.vmlist = tmp->next;
1222 put_vma(mm, tmp->vma);
1223 1616
1224 realalloc -= kobjsize(tmp); 1617 mm->total_vm = 0;
1225 askedalloc -= sizeof(*tmp);
1226 kfree(tmp);
1227 }
1228 1618
1229#ifdef DEBUG 1619 while ((vma = mm->mmap)) {
1230 show_process_blocks(); 1620 mm->mmap = vma->vm_next;
1231#endif 1621 delete_vma_from_mm(vma);
1622 delete_vma(mm, vma);
1232 } 1623 }
1624
1625 kleave("");
1233} 1626}
1234 1627
1235unsigned long do_brk(unsigned long addr, unsigned long len) 1628unsigned long do_brk(unsigned long addr, unsigned long len)
@@ -1242,8 +1635,8 @@ unsigned long do_brk(unsigned long addr, unsigned long len)
1242 * time (controlled by the MREMAP_MAYMOVE flag and available VM space) 1635 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1243 * 1636 *
1244 * under NOMMU conditions, we only permit changing a mapping's size, and only 1637 * under NOMMU conditions, we only permit changing a mapping's size, and only
1245 * as long as it stays within the hole allocated by the kmalloc() call in 1638 * as long as it stays within the region allocated by do_mmap_private() and the
1246 * do_mmap_pgoff() and the block is not shareable 1639 * block is not shareable
1247 * 1640 *
1248 * MREMAP_FIXED is not supported under NOMMU conditions 1641 * MREMAP_FIXED is not supported under NOMMU conditions
1249 */ 1642 */
@@ -1254,13 +1647,16 @@ unsigned long do_mremap(unsigned long addr,
1254 struct vm_area_struct *vma; 1647 struct vm_area_struct *vma;
1255 1648
1256 /* insanity checks first */ 1649 /* insanity checks first */
1257 if (new_len == 0) 1650 if (old_len == 0 || new_len == 0)
1258 return (unsigned long) -EINVAL; 1651 return (unsigned long) -EINVAL;
1259 1652
1653 if (addr & ~PAGE_MASK)
1654 return -EINVAL;
1655
1260 if (flags & MREMAP_FIXED && new_addr != addr) 1656 if (flags & MREMAP_FIXED && new_addr != addr)
1261 return (unsigned long) -EINVAL; 1657 return (unsigned long) -EINVAL;
1262 1658
1263 vma = find_vma_exact(current->mm, addr); 1659 vma = find_vma_exact(current->mm, addr, old_len);
1264 if (!vma) 1660 if (!vma)
1265 return (unsigned long) -EINVAL; 1661 return (unsigned long) -EINVAL;
1266 1662
@@ -1270,22 +1666,18 @@ unsigned long do_mremap(unsigned long addr,
1270 if (vma->vm_flags & VM_MAYSHARE) 1666 if (vma->vm_flags & VM_MAYSHARE)
1271 return (unsigned long) -EPERM; 1667 return (unsigned long) -EPERM;
1272 1668
1273 if (new_len > kobjsize((void *) addr)) 1669 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1274 return (unsigned long) -ENOMEM; 1670 return (unsigned long) -ENOMEM;
1275 1671
1276 /* all checks complete - do it */ 1672 /* all checks complete - do it */
1277 vma->vm_end = vma->vm_start + new_len; 1673 vma->vm_end = vma->vm_start + new_len;
1278
1279 askedalloc -= old_len;
1280 askedalloc += new_len;
1281
1282 return vma->vm_start; 1674 return vma->vm_start;
1283} 1675}
1284EXPORT_SYMBOL(do_mremap); 1676EXPORT_SYMBOL(do_mremap);
1285 1677
1286asmlinkage unsigned long sys_mremap(unsigned long addr, 1678SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1287 unsigned long old_len, unsigned long new_len, 1679 unsigned long, new_len, unsigned long, flags,
1288 unsigned long flags, unsigned long new_addr) 1680 unsigned long, new_addr)
1289{ 1681{
1290 unsigned long ret; 1682 unsigned long ret;
1291 1683