KVM: x86 emulator: convert group 4 to new style
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / nommu.c
blob88ff091eb07a23dcaee1e8e6d0cc70927183b89b
1 /*
2 * linux/mm/nommu.c
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).
7 * See Documentation/nommu-mmap.txt
9 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
10 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
13 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
16 #include <linux/module.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/tracehook.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mount.h>
29 #include <linux/personality.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
33 #include <asm/uaccess.h>
34 #include <asm/tlb.h>
35 #include <asm/tlbflush.h>
36 #include <asm/mmu_context.h>
37 #include "internal.h"
39 #if 0
40 #define kenter(FMT, ...) \
41 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
42 #define kleave(FMT, ...) \
43 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
44 #define kdebug(FMT, ...) \
45 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
46 #else
47 #define kenter(FMT, ...) \
48 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
49 #define kleave(FMT, ...) \
50 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
51 #define kdebug(FMT, ...) \
52 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
53 #endif
55 void *high_memory;
56 struct page *mem_map;
57 unsigned long max_mapnr;
58 unsigned long num_physpages;
59 unsigned long highest_memmap_pfn;
60 struct percpu_counter vm_committed_as;
61 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
62 int sysctl_overcommit_ratio = 50; /* default is 50% */
63 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
64 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
65 int heap_stack_gap = 0;
67 atomic_long_t mmap_pages_allocated;
69 EXPORT_SYMBOL(mem_map);
70 EXPORT_SYMBOL(num_physpages);
72 /* list of mapped, potentially shareable regions */
73 static struct kmem_cache *vm_region_jar;
74 struct rb_root nommu_region_tree = RB_ROOT;
75 DECLARE_RWSEM(nommu_region_sem);
77 const struct vm_operations_struct generic_file_vm_ops = {
81 * Return the total memory allocated for this pointer, not
82 * just what the caller asked for.
84 * Doesn't have to be accurate, i.e. may have races.
86 unsigned int kobjsize(const void *objp)
88 struct page *page;
91 * If the object we have should not have ksize performed on it,
92 * return size of 0
94 if (!objp || !virt_addr_valid(objp))
95 return 0;
97 page = virt_to_head_page(objp);
100 * If the allocator sets PageSlab, we know the pointer came from
101 * kmalloc().
103 if (PageSlab(page))
104 return ksize(objp);
107 * If it's not a compound page, see if we have a matching VMA
108 * region. This test is intentionally done in reverse order,
109 * so if there's no VMA, we still fall through and hand back
110 * PAGE_SIZE for 0-order pages.
112 if (!PageCompound(page)) {
113 struct vm_area_struct *vma;
115 vma = find_vma(current->mm, (unsigned long)objp);
116 if (vma)
117 return vma->vm_end - vma->vm_start;
121 * The ksize() function is only guaranteed to work for pointers
122 * returned by kmalloc(). So handle arbitrary pointers here.
124 return PAGE_SIZE << compound_order(page);
127 int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
128 unsigned long start, int nr_pages, unsigned int foll_flags,
129 struct page **pages, struct vm_area_struct **vmas)
131 struct vm_area_struct *vma;
132 unsigned long vm_flags;
133 int i;
135 /* calculate required read or write permissions.
136 * If FOLL_FORCE is set, we only require the "MAY" flags.
138 vm_flags = (foll_flags & FOLL_WRITE) ?
139 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
140 vm_flags &= (foll_flags & FOLL_FORCE) ?
141 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
143 for (i = 0; i < nr_pages; i++) {
144 vma = find_vma(mm, start);
145 if (!vma)
146 goto finish_or_fault;
148 /* protect what we can, including chardevs */
149 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
150 !(vm_flags & vma->vm_flags))
151 goto finish_or_fault;
153 if (pages) {
154 pages[i] = virt_to_page(start);
155 if (pages[i])
156 page_cache_get(pages[i]);
158 if (vmas)
159 vmas[i] = vma;
160 start = (start + PAGE_SIZE) & PAGE_MASK;
163 return i;
165 finish_or_fault:
166 return i ? : -EFAULT;
170 * get a list of pages in an address range belonging to the specified process
171 * and indicate the VMA that covers each page
172 * - this is potentially dodgy as we may end incrementing the page count of a
173 * slab page or a secondary page from a compound page
174 * - don't permit access to VMAs that don't support it, such as I/O mappings
176 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
177 unsigned long start, int nr_pages, int write, int force,
178 struct page **pages, struct vm_area_struct **vmas)
180 int flags = 0;
182 if (write)
183 flags |= FOLL_WRITE;
184 if (force)
185 flags |= FOLL_FORCE;
187 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
189 EXPORT_SYMBOL(get_user_pages);
192 * follow_pfn - look up PFN at a user virtual address
193 * @vma: memory mapping
194 * @address: user virtual address
195 * @pfn: location to store found PFN
197 * Only IO mappings and raw PFN mappings are allowed.
199 * Returns zero and the pfn at @pfn on success, -ve otherwise.
201 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
202 unsigned long *pfn)
204 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
205 return -EINVAL;
207 *pfn = address >> PAGE_SHIFT;
208 return 0;
210 EXPORT_SYMBOL(follow_pfn);
212 DEFINE_RWLOCK(vmlist_lock);
213 struct vm_struct *vmlist;
215 void vfree(const void *addr)
217 kfree(addr);
219 EXPORT_SYMBOL(vfree);
221 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
224 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
225 * returns only a logical address.
227 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
229 EXPORT_SYMBOL(__vmalloc);
231 void *vmalloc_user(unsigned long size)
233 void *ret;
235 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
236 PAGE_KERNEL);
237 if (ret) {
238 struct vm_area_struct *vma;
240 down_write(&current->mm->mmap_sem);
241 vma = find_vma(current->mm, (unsigned long)ret);
242 if (vma)
243 vma->vm_flags |= VM_USERMAP;
244 up_write(&current->mm->mmap_sem);
247 return ret;
249 EXPORT_SYMBOL(vmalloc_user);
251 struct page *vmalloc_to_page(const void *addr)
253 return virt_to_page(addr);
255 EXPORT_SYMBOL(vmalloc_to_page);
257 unsigned long vmalloc_to_pfn(const void *addr)
259 return page_to_pfn(virt_to_page(addr));
261 EXPORT_SYMBOL(vmalloc_to_pfn);
263 long vread(char *buf, char *addr, unsigned long count)
265 memcpy(buf, addr, count);
266 return count;
269 long vwrite(char *buf, char *addr, unsigned long count)
271 /* Don't allow overflow */
272 if ((unsigned long) addr + count < count)
273 count = -(unsigned long) addr;
275 memcpy(addr, buf, count);
276 return(count);
280 * vmalloc - allocate virtually continguos memory
282 * @size: allocation size
284 * Allocate enough pages to cover @size from the page level
285 * allocator and map them into continguos kernel virtual space.
287 * For tight control over page level allocator and protection flags
288 * use __vmalloc() instead.
290 void *vmalloc(unsigned long size)
292 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
294 EXPORT_SYMBOL(vmalloc);
296 void *vmalloc_node(unsigned long size, int node)
298 return vmalloc(size);
300 EXPORT_SYMBOL(vmalloc_node);
302 #ifndef PAGE_KERNEL_EXEC
303 # define PAGE_KERNEL_EXEC PAGE_KERNEL
304 #endif
307 * vmalloc_exec - allocate virtually contiguous, executable memory
308 * @size: allocation size
310 * Kernel-internal function to allocate enough pages to cover @size
311 * the page level allocator and map them into contiguous and
312 * executable kernel virtual space.
314 * For tight control over page level allocator and protection flags
315 * use __vmalloc() instead.
318 void *vmalloc_exec(unsigned long size)
320 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
324 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
325 * @size: allocation size
327 * Allocate enough 32bit PA addressable pages to cover @size from the
328 * page level allocator and map them into continguos kernel virtual space.
330 void *vmalloc_32(unsigned long size)
332 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
334 EXPORT_SYMBOL(vmalloc_32);
337 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
338 * @size: allocation size
340 * The resulting memory area is 32bit addressable and zeroed so it can be
341 * mapped to userspace without leaking data.
343 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
344 * remap_vmalloc_range() are permissible.
346 void *vmalloc_32_user(unsigned long size)
349 * We'll have to sort out the ZONE_DMA bits for 64-bit,
350 * but for now this can simply use vmalloc_user() directly.
352 return vmalloc_user(size);
354 EXPORT_SYMBOL(vmalloc_32_user);
356 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
358 BUG();
359 return NULL;
361 EXPORT_SYMBOL(vmap);
363 void vunmap(const void *addr)
365 BUG();
367 EXPORT_SYMBOL(vunmap);
369 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
371 BUG();
372 return NULL;
374 EXPORT_SYMBOL(vm_map_ram);
376 void vm_unmap_ram(const void *mem, unsigned int count)
378 BUG();
380 EXPORT_SYMBOL(vm_unmap_ram);
382 void vm_unmap_aliases(void)
385 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
388 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
389 * have one.
391 void __attribute__((weak)) vmalloc_sync_all(void)
395 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
396 struct page *page)
398 return -EINVAL;
400 EXPORT_SYMBOL(vm_insert_page);
403 * sys_brk() for the most part doesn't need the global kernel
404 * lock, except when an application is doing something nasty
405 * like trying to un-brk an area that has already been mapped
406 * to a regular file. in this case, the unmapping will need
407 * to invoke file system routines that need the global lock.
409 SYSCALL_DEFINE1(brk, unsigned long, brk)
411 struct mm_struct *mm = current->mm;
413 if (brk < mm->start_brk || brk > mm->context.end_brk)
414 return mm->brk;
416 if (mm->brk == brk)
417 return mm->brk;
420 * Always allow shrinking brk
422 if (brk <= mm->brk) {
423 mm->brk = brk;
424 return brk;
428 * Ok, looks good - let it rip.
430 flush_icache_range(mm->brk, brk);
431 return mm->brk = brk;
435 * initialise the VMA and region record slabs
437 void __init mmap_init(void)
439 int ret;
441 ret = percpu_counter_init(&vm_committed_as, 0);
442 VM_BUG_ON(ret);
443 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
447 * validate the region tree
448 * - the caller must hold the region lock
450 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
451 static noinline void validate_nommu_regions(void)
453 struct vm_region *region, *last;
454 struct rb_node *p, *lastp;
456 lastp = rb_first(&nommu_region_tree);
457 if (!lastp)
458 return;
460 last = rb_entry(lastp, struct vm_region, vm_rb);
461 BUG_ON(unlikely(last->vm_end <= last->vm_start));
462 BUG_ON(unlikely(last->vm_top < last->vm_end));
464 while ((p = rb_next(lastp))) {
465 region = rb_entry(p, struct vm_region, vm_rb);
466 last = rb_entry(lastp, struct vm_region, vm_rb);
468 BUG_ON(unlikely(region->vm_end <= region->vm_start));
469 BUG_ON(unlikely(region->vm_top < region->vm_end));
470 BUG_ON(unlikely(region->vm_start < last->vm_top));
472 lastp = p;
475 #else
476 static void validate_nommu_regions(void)
479 #endif
482 * add a region into the global tree
484 static void add_nommu_region(struct vm_region *region)
486 struct vm_region *pregion;
487 struct rb_node **p, *parent;
489 validate_nommu_regions();
491 parent = NULL;
492 p = &nommu_region_tree.rb_node;
493 while (*p) {
494 parent = *p;
495 pregion = rb_entry(parent, struct vm_region, vm_rb);
496 if (region->vm_start < pregion->vm_start)
497 p = &(*p)->rb_left;
498 else if (region->vm_start > pregion->vm_start)
499 p = &(*p)->rb_right;
500 else if (pregion == region)
501 return;
502 else
503 BUG();
506 rb_link_node(&region->vm_rb, parent, p);
507 rb_insert_color(&region->vm_rb, &nommu_region_tree);
509 validate_nommu_regions();
513 * delete a region from the global tree
515 static void delete_nommu_region(struct vm_region *region)
517 BUG_ON(!nommu_region_tree.rb_node);
519 validate_nommu_regions();
520 rb_erase(&region->vm_rb, &nommu_region_tree);
521 validate_nommu_regions();
525 * free a contiguous series of pages
527 static void free_page_series(unsigned long from, unsigned long to)
529 for (; from < to; from += PAGE_SIZE) {
530 struct page *page = virt_to_page(from);
532 kdebug("- free %lx", from);
533 atomic_long_dec(&mmap_pages_allocated);
534 if (page_count(page) != 1)
535 kdebug("free page %p: refcount not one: %d",
536 page, page_count(page));
537 put_page(page);
542 * release a reference to a region
543 * - the caller must hold the region semaphore for writing, which this releases
544 * - the region may not have been added to the tree yet, in which case vm_top
545 * will equal vm_start
547 static void __put_nommu_region(struct vm_region *region)
548 __releases(nommu_region_sem)
550 kenter("%p{%d}", region, region->vm_usage);
552 BUG_ON(!nommu_region_tree.rb_node);
554 if (--region->vm_usage == 0) {
555 if (region->vm_top > region->vm_start)
556 delete_nommu_region(region);
557 up_write(&nommu_region_sem);
559 if (region->vm_file)
560 fput(region->vm_file);
562 /* IO memory and memory shared directly out of the pagecache
563 * from ramfs/tmpfs mustn't be released here */
564 if (region->vm_flags & VM_MAPPED_COPY) {
565 kdebug("free series");
566 free_page_series(region->vm_start, region->vm_top);
568 kmem_cache_free(vm_region_jar, region);
569 } else {
570 up_write(&nommu_region_sem);
575 * release a reference to a region
577 static void put_nommu_region(struct vm_region *region)
579 down_write(&nommu_region_sem);
580 __put_nommu_region(region);
584 * update protection on a vma
586 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
588 #ifdef CONFIG_MPU
589 struct mm_struct *mm = vma->vm_mm;
590 long start = vma->vm_start & PAGE_MASK;
591 while (start < vma->vm_end) {
592 protect_page(mm, start, flags);
593 start += PAGE_SIZE;
595 update_protections(mm);
596 #endif
600 * add a VMA into a process's mm_struct in the appropriate place in the list
601 * and tree and add to the address space's page tree also if not an anonymous
602 * page
603 * - should be called with mm->mmap_sem held writelocked
605 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
607 struct vm_area_struct *pvma, **pp, *next;
608 struct address_space *mapping;
609 struct rb_node **p, *parent;
611 kenter(",%p", vma);
613 BUG_ON(!vma->vm_region);
615 mm->map_count++;
616 vma->vm_mm = mm;
618 protect_vma(vma, vma->vm_flags);
620 /* add the VMA to the mapping */
621 if (vma->vm_file) {
622 mapping = vma->vm_file->f_mapping;
624 flush_dcache_mmap_lock(mapping);
625 vma_prio_tree_insert(vma, &mapping->i_mmap);
626 flush_dcache_mmap_unlock(mapping);
629 /* add the VMA to the tree */
630 parent = NULL;
631 p = &mm->mm_rb.rb_node;
632 while (*p) {
633 parent = *p;
634 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
636 /* sort by: start addr, end addr, VMA struct addr in that order
637 * (the latter is necessary as we may get identical VMAs) */
638 if (vma->vm_start < pvma->vm_start)
639 p = &(*p)->rb_left;
640 else if (vma->vm_start > pvma->vm_start)
641 p = &(*p)->rb_right;
642 else if (vma->vm_end < pvma->vm_end)
643 p = &(*p)->rb_left;
644 else if (vma->vm_end > pvma->vm_end)
645 p = &(*p)->rb_right;
646 else if (vma < pvma)
647 p = &(*p)->rb_left;
648 else if (vma > pvma)
649 p = &(*p)->rb_right;
650 else
651 BUG();
654 rb_link_node(&vma->vm_rb, parent, p);
655 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
657 /* add VMA to the VMA list also */
658 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
659 if (pvma->vm_start > vma->vm_start)
660 break;
661 if (pvma->vm_start < vma->vm_start)
662 continue;
663 if (pvma->vm_end < vma->vm_end)
664 break;
667 next = *pp;
668 *pp = vma;
669 vma->vm_next = next;
670 if (next)
671 next->vm_prev = vma;
675 * delete a VMA from its owning mm_struct and address space
677 static void delete_vma_from_mm(struct vm_area_struct *vma)
679 struct vm_area_struct **pp;
680 struct address_space *mapping;
681 struct mm_struct *mm = vma->vm_mm;
683 kenter("%p", vma);
685 protect_vma(vma, 0);
687 mm->map_count--;
688 if (mm->mmap_cache == vma)
689 mm->mmap_cache = NULL;
691 /* remove the VMA from the mapping */
692 if (vma->vm_file) {
693 mapping = vma->vm_file->f_mapping;
695 flush_dcache_mmap_lock(mapping);
696 vma_prio_tree_remove(vma, &mapping->i_mmap);
697 flush_dcache_mmap_unlock(mapping);
700 /* remove from the MM's tree and list */
701 rb_erase(&vma->vm_rb, &mm->mm_rb);
702 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
703 if (*pp == vma) {
704 *pp = vma->vm_next;
705 break;
709 vma->vm_mm = NULL;
713 * destroy a VMA record
715 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
717 kenter("%p", vma);
718 if (vma->vm_ops && vma->vm_ops->close)
719 vma->vm_ops->close(vma);
720 if (vma->vm_file) {
721 fput(vma->vm_file);
722 if (vma->vm_flags & VM_EXECUTABLE)
723 removed_exe_file_vma(mm);
725 put_nommu_region(vma->vm_region);
726 kmem_cache_free(vm_area_cachep, vma);
730 * look up the first VMA in which addr resides, NULL if none
731 * - should be called with mm->mmap_sem at least held readlocked
733 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
735 struct vm_area_struct *vma;
736 struct rb_node *n = mm->mm_rb.rb_node;
738 /* check the cache first */
739 vma = mm->mmap_cache;
740 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
741 return vma;
743 /* trawl the tree (there may be multiple mappings in which addr
744 * resides) */
745 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
746 vma = rb_entry(n, struct vm_area_struct, vm_rb);
747 if (vma->vm_start > addr)
748 return NULL;
749 if (vma->vm_end > addr) {
750 mm->mmap_cache = vma;
751 return vma;
755 return NULL;
757 EXPORT_SYMBOL(find_vma);
760 * find a VMA
761 * - we don't extend stack VMAs under NOMMU conditions
763 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
765 return find_vma(mm, addr);
769 * expand a stack to a given address
770 * - not supported under NOMMU conditions
772 int expand_stack(struct vm_area_struct *vma, unsigned long address)
774 return -ENOMEM;
778 * look up the first VMA exactly that exactly matches addr
779 * - should be called with mm->mmap_sem at least held readlocked
781 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
782 unsigned long addr,
783 unsigned long len)
785 struct vm_area_struct *vma;
786 struct rb_node *n = mm->mm_rb.rb_node;
787 unsigned long end = addr + len;
789 /* check the cache first */
790 vma = mm->mmap_cache;
791 if (vma && vma->vm_start == addr && vma->vm_end == end)
792 return vma;
794 /* trawl the tree (there may be multiple mappings in which addr
795 * resides) */
796 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
797 vma = rb_entry(n, struct vm_area_struct, vm_rb);
798 if (vma->vm_start < addr)
799 continue;
800 if (vma->vm_start > addr)
801 return NULL;
802 if (vma->vm_end == end) {
803 mm->mmap_cache = vma;
804 return vma;
808 return NULL;
812 * determine whether a mapping should be permitted and, if so, what sort of
813 * mapping we're capable of supporting
815 static int validate_mmap_request(struct file *file,
816 unsigned long addr,
817 unsigned long len,
818 unsigned long prot,
819 unsigned long flags,
820 unsigned long pgoff,
821 unsigned long *_capabilities)
823 unsigned long capabilities, rlen;
824 unsigned long reqprot = prot;
825 int ret;
827 /* do the simple checks first */
828 if (flags & MAP_FIXED) {
829 printk(KERN_DEBUG
830 "%d: Can't do fixed-address/overlay mmap of RAM\n",
831 current->pid);
832 return -EINVAL;
835 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
836 (flags & MAP_TYPE) != MAP_SHARED)
837 return -EINVAL;
839 if (!len)
840 return -EINVAL;
842 /* Careful about overflows.. */
843 rlen = PAGE_ALIGN(len);
844 if (!rlen || rlen > TASK_SIZE)
845 return -ENOMEM;
847 /* offset overflow? */
848 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
849 return -EOVERFLOW;
851 if (file) {
852 /* validate file mapping requests */
853 struct address_space *mapping;
855 /* files must support mmap */
856 if (!file->f_op || !file->f_op->mmap)
857 return -ENODEV;
859 /* work out if what we've got could possibly be shared
860 * - we support chardevs that provide their own "memory"
861 * - we support files/blockdevs that are memory backed
863 mapping = file->f_mapping;
864 if (!mapping)
865 mapping = file->f_path.dentry->d_inode->i_mapping;
867 capabilities = 0;
868 if (mapping && mapping->backing_dev_info)
869 capabilities = mapping->backing_dev_info->capabilities;
871 if (!capabilities) {
872 /* no explicit capabilities set, so assume some
873 * defaults */
874 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
875 case S_IFREG:
876 case S_IFBLK:
877 capabilities = BDI_CAP_MAP_COPY;
878 break;
880 case S_IFCHR:
881 capabilities =
882 BDI_CAP_MAP_DIRECT |
883 BDI_CAP_READ_MAP |
884 BDI_CAP_WRITE_MAP;
885 break;
887 default:
888 return -EINVAL;
892 /* eliminate any capabilities that we can't support on this
893 * device */
894 if (!file->f_op->get_unmapped_area)
895 capabilities &= ~BDI_CAP_MAP_DIRECT;
896 if (!file->f_op->read)
897 capabilities &= ~BDI_CAP_MAP_COPY;
899 /* The file shall have been opened with read permission. */
900 if (!(file->f_mode & FMODE_READ))
901 return -EACCES;
903 if (flags & MAP_SHARED) {
904 /* do checks for writing, appending and locking */
905 if ((prot & PROT_WRITE) &&
906 !(file->f_mode & FMODE_WRITE))
907 return -EACCES;
909 if (IS_APPEND(file->f_path.dentry->d_inode) &&
910 (file->f_mode & FMODE_WRITE))
911 return -EACCES;
913 if (locks_verify_locked(file->f_path.dentry->d_inode))
914 return -EAGAIN;
916 if (!(capabilities & BDI_CAP_MAP_DIRECT))
917 return -ENODEV;
919 /* we mustn't privatise shared mappings */
920 capabilities &= ~BDI_CAP_MAP_COPY;
922 else {
923 /* we're going to read the file into private memory we
924 * allocate */
925 if (!(capabilities & BDI_CAP_MAP_COPY))
926 return -ENODEV;
928 /* we don't permit a private writable mapping to be
929 * shared with the backing device */
930 if (prot & PROT_WRITE)
931 capabilities &= ~BDI_CAP_MAP_DIRECT;
934 if (capabilities & BDI_CAP_MAP_DIRECT) {
935 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
936 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
937 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
939 capabilities &= ~BDI_CAP_MAP_DIRECT;
940 if (flags & MAP_SHARED) {
941 printk(KERN_WARNING
942 "MAP_SHARED not completely supported on !MMU\n");
943 return -EINVAL;
948 /* handle executable mappings and implied executable
949 * mappings */
950 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
951 if (prot & PROT_EXEC)
952 return -EPERM;
954 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
955 /* handle implication of PROT_EXEC by PROT_READ */
956 if (current->personality & READ_IMPLIES_EXEC) {
957 if (capabilities & BDI_CAP_EXEC_MAP)
958 prot |= PROT_EXEC;
961 else if ((prot & PROT_READ) &&
962 (prot & PROT_EXEC) &&
963 !(capabilities & BDI_CAP_EXEC_MAP)
965 /* backing file is not executable, try to copy */
966 capabilities &= ~BDI_CAP_MAP_DIRECT;
969 else {
970 /* anonymous mappings are always memory backed and can be
971 * privately mapped
973 capabilities = BDI_CAP_MAP_COPY;
975 /* handle PROT_EXEC implication by PROT_READ */
976 if ((prot & PROT_READ) &&
977 (current->personality & READ_IMPLIES_EXEC))
978 prot |= PROT_EXEC;
981 /* allow the security API to have its say */
982 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
983 if (ret < 0)
984 return ret;
986 /* looks okay */
987 *_capabilities = capabilities;
988 return 0;
992 * we've determined that we can make the mapping, now translate what we
993 * now know into VMA flags
995 static unsigned long determine_vm_flags(struct file *file,
996 unsigned long prot,
997 unsigned long flags,
998 unsigned long capabilities)
1000 unsigned long vm_flags;
1002 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1003 /* vm_flags |= mm->def_flags; */
1005 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1006 /* attempt to share read-only copies of mapped file chunks */
1007 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1008 if (file && !(prot & PROT_WRITE))
1009 vm_flags |= VM_MAYSHARE;
1010 } else {
1011 /* overlay a shareable mapping on the backing device or inode
1012 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1013 * romfs/cramfs */
1014 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1015 if (flags & MAP_SHARED)
1016 vm_flags |= VM_SHARED;
1019 /* refuse to let anyone share private mappings with this process if
1020 * it's being traced - otherwise breakpoints set in it may interfere
1021 * with another untraced process
1023 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
1024 vm_flags &= ~VM_MAYSHARE;
1026 return vm_flags;
1030 * set up a shared mapping on a file (the driver or filesystem provides and
1031 * pins the storage)
1033 static int do_mmap_shared_file(struct vm_area_struct *vma)
1035 int ret;
1037 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1038 if (ret == 0) {
1039 vma->vm_region->vm_top = vma->vm_region->vm_end;
1040 return 0;
1042 if (ret != -ENOSYS)
1043 return ret;
1045 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1046 * opposed to tried but failed) so we can only give a suitable error as
1047 * it's not possible to make a private copy if MAP_SHARED was given */
1048 return -ENODEV;
1052 * set up a private mapping or an anonymous shared mapping
1054 static int do_mmap_private(struct vm_area_struct *vma,
1055 struct vm_region *region,
1056 unsigned long len,
1057 unsigned long capabilities)
1059 struct page *pages;
1060 unsigned long total, point, n, rlen;
1061 void *base;
1062 int ret, order;
1064 /* invoke the file's mapping function so that it can keep track of
1065 * shared mappings on devices or memory
1066 * - VM_MAYSHARE will be set if it may attempt to share
1068 if (capabilities & BDI_CAP_MAP_DIRECT) {
1069 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1070 if (ret == 0) {
1071 /* shouldn't return success if we're not sharing */
1072 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1073 vma->vm_region->vm_top = vma->vm_region->vm_end;
1074 return 0;
1076 if (ret != -ENOSYS)
1077 return ret;
1079 /* getting an ENOSYS error indicates that direct mmap isn't
1080 * possible (as opposed to tried but failed) so we'll try to
1081 * make a private copy of the data and map that instead */
1084 rlen = PAGE_ALIGN(len);
1086 /* allocate some memory to hold the mapping
1087 * - note that this may not return a page-aligned address if the object
1088 * we're allocating is smaller than a page
1090 order = get_order(rlen);
1091 kdebug("alloc order %d for %lx", order, len);
1093 pages = alloc_pages(GFP_KERNEL, order);
1094 if (!pages)
1095 goto enomem;
1097 total = 1 << order;
1098 atomic_long_add(total, &mmap_pages_allocated);
1100 point = rlen >> PAGE_SHIFT;
1102 /* we allocated a power-of-2 sized page set, so we may want to trim off
1103 * the excess */
1104 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1105 while (total > point) {
1106 order = ilog2(total - point);
1107 n = 1 << order;
1108 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1109 atomic_long_sub(n, &mmap_pages_allocated);
1110 total -= n;
1111 set_page_refcounted(pages + total);
1112 __free_pages(pages + total, order);
1116 for (point = 1; point < total; point++)
1117 set_page_refcounted(&pages[point]);
1119 base = page_address(pages);
1120 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1121 region->vm_start = (unsigned long) base;
1122 region->vm_end = region->vm_start + rlen;
1123 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1125 vma->vm_start = region->vm_start;
1126 vma->vm_end = region->vm_start + len;
1128 if (vma->vm_file) {
1129 /* read the contents of a file into the copy */
1130 mm_segment_t old_fs;
1131 loff_t fpos;
1133 fpos = vma->vm_pgoff;
1134 fpos <<= PAGE_SHIFT;
1136 old_fs = get_fs();
1137 set_fs(KERNEL_DS);
1138 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
1139 set_fs(old_fs);
1141 if (ret < 0)
1142 goto error_free;
1144 /* clear the last little bit */
1145 if (ret < rlen)
1146 memset(base + ret, 0, rlen - ret);
1150 return 0;
1152 error_free:
1153 free_page_series(region->vm_start, region->vm_end);
1154 region->vm_start = vma->vm_start = 0;
1155 region->vm_end = vma->vm_end = 0;
1156 region->vm_top = 0;
1157 return ret;
1159 enomem:
1160 printk("Allocation of length %lu from process %d (%s) failed\n",
1161 len, current->pid, current->comm);
1162 show_free_areas();
1163 return -ENOMEM;
1167 * handle mapping creation for uClinux
1169 unsigned long do_mmap_pgoff(struct file *file,
1170 unsigned long addr,
1171 unsigned long len,
1172 unsigned long prot,
1173 unsigned long flags,
1174 unsigned long pgoff)
1176 struct vm_area_struct *vma;
1177 struct vm_region *region;
1178 struct rb_node *rb;
1179 unsigned long capabilities, vm_flags, result;
1180 int ret;
1182 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1184 /* decide whether we should attempt the mapping, and if so what sort of
1185 * mapping */
1186 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1187 &capabilities);
1188 if (ret < 0) {
1189 kleave(" = %d [val]", ret);
1190 return ret;
1193 /* we ignore the address hint */
1194 addr = 0;
1196 /* we've determined that we can make the mapping, now translate what we
1197 * now know into VMA flags */
1198 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1200 /* we're going to need to record the mapping */
1201 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1202 if (!region)
1203 goto error_getting_region;
1205 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1206 if (!vma)
1207 goto error_getting_vma;
1209 region->vm_usage = 1;
1210 region->vm_flags = vm_flags;
1211 region->vm_pgoff = pgoff;
1213 INIT_LIST_HEAD(&vma->anon_vma_chain);
1214 vma->vm_flags = vm_flags;
1215 vma->vm_pgoff = pgoff;
1217 if (file) {
1218 region->vm_file = file;
1219 get_file(file);
1220 vma->vm_file = file;
1221 get_file(file);
1222 if (vm_flags & VM_EXECUTABLE) {
1223 added_exe_file_vma(current->mm);
1224 vma->vm_mm = current->mm;
1228 down_write(&nommu_region_sem);
1230 /* if we want to share, we need to check for regions created by other
1231 * mmap() calls that overlap with our proposed mapping
1232 * - we can only share with a superset match on most regular files
1233 * - shared mappings on character devices and memory backed files are
1234 * permitted to overlap inexactly as far as we are concerned for in
1235 * these cases, sharing is handled in the driver or filesystem rather
1236 * than here
1238 if (vm_flags & VM_MAYSHARE) {
1239 struct vm_region *pregion;
1240 unsigned long pglen, rpglen, pgend, rpgend, start;
1242 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1243 pgend = pgoff + pglen;
1245 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1246 pregion = rb_entry(rb, struct vm_region, vm_rb);
1248 if (!(pregion->vm_flags & VM_MAYSHARE))
1249 continue;
1251 /* search for overlapping mappings on the same file */
1252 if (pregion->vm_file->f_path.dentry->d_inode !=
1253 file->f_path.dentry->d_inode)
1254 continue;
1256 if (pregion->vm_pgoff >= pgend)
1257 continue;
1259 rpglen = pregion->vm_end - pregion->vm_start;
1260 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1261 rpgend = pregion->vm_pgoff + rpglen;
1262 if (pgoff >= rpgend)
1263 continue;
1265 /* handle inexactly overlapping matches between
1266 * mappings */
1267 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1268 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1269 /* new mapping is not a subset of the region */
1270 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1271 goto sharing_violation;
1272 continue;
1275 /* we've found a region we can share */
1276 pregion->vm_usage++;
1277 vma->vm_region = pregion;
1278 start = pregion->vm_start;
1279 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1280 vma->vm_start = start;
1281 vma->vm_end = start + len;
1283 if (pregion->vm_flags & VM_MAPPED_COPY) {
1284 kdebug("share copy");
1285 vma->vm_flags |= VM_MAPPED_COPY;
1286 } else {
1287 kdebug("share mmap");
1288 ret = do_mmap_shared_file(vma);
1289 if (ret < 0) {
1290 vma->vm_region = NULL;
1291 vma->vm_start = 0;
1292 vma->vm_end = 0;
1293 pregion->vm_usage--;
1294 pregion = NULL;
1295 goto error_just_free;
1298 fput(region->vm_file);
1299 kmem_cache_free(vm_region_jar, region);
1300 region = pregion;
1301 result = start;
1302 goto share;
1305 /* obtain the address at which to make a shared mapping
1306 * - this is the hook for quasi-memory character devices to
1307 * tell us the location of a shared mapping
1309 if (capabilities & BDI_CAP_MAP_DIRECT) {
1310 addr = file->f_op->get_unmapped_area(file, addr, len,
1311 pgoff, flags);
1312 if (IS_ERR((void *) addr)) {
1313 ret = addr;
1314 if (ret != (unsigned long) -ENOSYS)
1315 goto error_just_free;
1317 /* the driver refused to tell us where to site
1318 * the mapping so we'll have to attempt to copy
1319 * it */
1320 ret = (unsigned long) -ENODEV;
1321 if (!(capabilities & BDI_CAP_MAP_COPY))
1322 goto error_just_free;
1324 capabilities &= ~BDI_CAP_MAP_DIRECT;
1325 } else {
1326 vma->vm_start = region->vm_start = addr;
1327 vma->vm_end = region->vm_end = addr + len;
1332 vma->vm_region = region;
1334 /* set up the mapping
1335 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1337 if (file && vma->vm_flags & VM_SHARED)
1338 ret = do_mmap_shared_file(vma);
1339 else
1340 ret = do_mmap_private(vma, region, len, capabilities);
1341 if (ret < 0)
1342 goto error_just_free;
1343 add_nommu_region(region);
1345 /* clear anonymous mappings that don't ask for uninitialized data */
1346 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1347 memset((void *)region->vm_start, 0,
1348 region->vm_end - region->vm_start);
1350 /* okay... we have a mapping; now we have to register it */
1351 result = vma->vm_start;
1353 current->mm->total_vm += len >> PAGE_SHIFT;
1355 share:
1356 add_vma_to_mm(current->mm, vma);
1358 /* we flush the region from the icache only when the first executable
1359 * mapping of it is made */
1360 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1361 flush_icache_range(region->vm_start, region->vm_end);
1362 region->vm_icache_flushed = true;
1365 up_write(&nommu_region_sem);
1367 kleave(" = %lx", result);
1368 return result;
1370 error_just_free:
1371 up_write(&nommu_region_sem);
1372 error:
1373 if (region->vm_file)
1374 fput(region->vm_file);
1375 kmem_cache_free(vm_region_jar, region);
1376 if (vma->vm_file)
1377 fput(vma->vm_file);
1378 if (vma->vm_flags & VM_EXECUTABLE)
1379 removed_exe_file_vma(vma->vm_mm);
1380 kmem_cache_free(vm_area_cachep, vma);
1381 kleave(" = %d", ret);
1382 return ret;
1384 sharing_violation:
1385 up_write(&nommu_region_sem);
1386 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1387 ret = -EINVAL;
1388 goto error;
1390 error_getting_vma:
1391 kmem_cache_free(vm_region_jar, region);
1392 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1393 " from process %d failed\n",
1394 len, current->pid);
1395 show_free_areas();
1396 return -ENOMEM;
1398 error_getting_region:
1399 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1400 " from process %d failed\n",
1401 len, current->pid);
1402 show_free_areas();
1403 return -ENOMEM;
1405 EXPORT_SYMBOL(do_mmap_pgoff);
1407 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1408 unsigned long, prot, unsigned long, flags,
1409 unsigned long, fd, unsigned long, pgoff)
1411 struct file *file = NULL;
1412 unsigned long retval = -EBADF;
1414 if (!(flags & MAP_ANONYMOUS)) {
1415 file = fget(fd);
1416 if (!file)
1417 goto out;
1420 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1422 down_write(&current->mm->mmap_sem);
1423 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1424 up_write(&current->mm->mmap_sem);
1426 if (file)
1427 fput(file);
1428 out:
1429 return retval;
1432 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1433 struct mmap_arg_struct {
1434 unsigned long addr;
1435 unsigned long len;
1436 unsigned long prot;
1437 unsigned long flags;
1438 unsigned long fd;
1439 unsigned long offset;
1442 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1444 struct mmap_arg_struct a;
1446 if (copy_from_user(&a, arg, sizeof(a)))
1447 return -EFAULT;
1448 if (a.offset & ~PAGE_MASK)
1449 return -EINVAL;
1451 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1452 a.offset >> PAGE_SHIFT);
1454 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1457 * split a vma into two pieces at address 'addr', a new vma is allocated either
1458 * for the first part or the tail.
1460 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1461 unsigned long addr, int new_below)
1463 struct vm_area_struct *new;
1464 struct vm_region *region;
1465 unsigned long npages;
1467 kenter("");
1469 /* we're only permitted to split anonymous regions (these should have
1470 * only a single usage on the region) */
1471 if (vma->vm_file)
1472 return -ENOMEM;
1474 if (mm->map_count >= sysctl_max_map_count)
1475 return -ENOMEM;
1477 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1478 if (!region)
1479 return -ENOMEM;
1481 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1482 if (!new) {
1483 kmem_cache_free(vm_region_jar, region);
1484 return -ENOMEM;
1487 /* most fields are the same, copy all, and then fixup */
1488 *new = *vma;
1489 *region = *vma->vm_region;
1490 new->vm_region = region;
1492 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1494 if (new_below) {
1495 region->vm_top = region->vm_end = new->vm_end = addr;
1496 } else {
1497 region->vm_start = new->vm_start = addr;
1498 region->vm_pgoff = new->vm_pgoff += npages;
1501 if (new->vm_ops && new->vm_ops->open)
1502 new->vm_ops->open(new);
1504 delete_vma_from_mm(vma);
1505 down_write(&nommu_region_sem);
1506 delete_nommu_region(vma->vm_region);
1507 if (new_below) {
1508 vma->vm_region->vm_start = vma->vm_start = addr;
1509 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1510 } else {
1511 vma->vm_region->vm_end = vma->vm_end = addr;
1512 vma->vm_region->vm_top = addr;
1514 add_nommu_region(vma->vm_region);
1515 add_nommu_region(new->vm_region);
1516 up_write(&nommu_region_sem);
1517 add_vma_to_mm(mm, vma);
1518 add_vma_to_mm(mm, new);
1519 return 0;
1523 * shrink a VMA by removing the specified chunk from either the beginning or
1524 * the end
1526 static int shrink_vma(struct mm_struct *mm,
1527 struct vm_area_struct *vma,
1528 unsigned long from, unsigned long to)
1530 struct vm_region *region;
1532 kenter("");
1534 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1535 * and list */
1536 delete_vma_from_mm(vma);
1537 if (from > vma->vm_start)
1538 vma->vm_end = from;
1539 else
1540 vma->vm_start = to;
1541 add_vma_to_mm(mm, vma);
1543 /* cut the backing region down to size */
1544 region = vma->vm_region;
1545 BUG_ON(region->vm_usage != 1);
1547 down_write(&nommu_region_sem);
1548 delete_nommu_region(region);
1549 if (from > region->vm_start) {
1550 to = region->vm_top;
1551 region->vm_top = region->vm_end = from;
1552 } else {
1553 region->vm_start = to;
1555 add_nommu_region(region);
1556 up_write(&nommu_region_sem);
1558 free_page_series(from, to);
1559 return 0;
1563 * release a mapping
1564 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1565 * VMA, though it need not cover the whole VMA
1567 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1569 struct vm_area_struct *vma;
1570 struct rb_node *rb;
1571 unsigned long end = start + len;
1572 int ret;
1574 kenter(",%lx,%zx", start, len);
1576 if (len == 0)
1577 return -EINVAL;
1579 /* find the first potentially overlapping VMA */
1580 vma = find_vma(mm, start);
1581 if (!vma) {
1582 static int limit = 0;
1583 if (limit < 5) {
1584 printk(KERN_WARNING
1585 "munmap of memory not mmapped by process %d"
1586 " (%s): 0x%lx-0x%lx\n",
1587 current->pid, current->comm,
1588 start, start + len - 1);
1589 limit++;
1591 return -EINVAL;
1594 /* we're allowed to split an anonymous VMA but not a file-backed one */
1595 if (vma->vm_file) {
1596 do {
1597 if (start > vma->vm_start) {
1598 kleave(" = -EINVAL [miss]");
1599 return -EINVAL;
1601 if (end == vma->vm_end)
1602 goto erase_whole_vma;
1603 rb = rb_next(&vma->vm_rb);
1604 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1605 } while (rb);
1606 kleave(" = -EINVAL [split file]");
1607 return -EINVAL;
1608 } else {
1609 /* the chunk must be a subset of the VMA found */
1610 if (start == vma->vm_start && end == vma->vm_end)
1611 goto erase_whole_vma;
1612 if (start < vma->vm_start || end > vma->vm_end) {
1613 kleave(" = -EINVAL [superset]");
1614 return -EINVAL;
1616 if (start & ~PAGE_MASK) {
1617 kleave(" = -EINVAL [unaligned start]");
1618 return -EINVAL;
1620 if (end != vma->vm_end && end & ~PAGE_MASK) {
1621 kleave(" = -EINVAL [unaligned split]");
1622 return -EINVAL;
1624 if (start != vma->vm_start && end != vma->vm_end) {
1625 ret = split_vma(mm, vma, start, 1);
1626 if (ret < 0) {
1627 kleave(" = %d [split]", ret);
1628 return ret;
1631 return shrink_vma(mm, vma, start, end);
1634 erase_whole_vma:
1635 delete_vma_from_mm(vma);
1636 delete_vma(mm, vma);
1637 kleave(" = 0");
1638 return 0;
1640 EXPORT_SYMBOL(do_munmap);
1642 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1644 int ret;
1645 struct mm_struct *mm = current->mm;
1647 down_write(&mm->mmap_sem);
1648 ret = do_munmap(mm, addr, len);
1649 up_write(&mm->mmap_sem);
1650 return ret;
1654 * release all the mappings made in a process's VM space
1656 void exit_mmap(struct mm_struct *mm)
1658 struct vm_area_struct *vma;
1660 if (!mm)
1661 return;
1663 kenter("");
1665 mm->total_vm = 0;
1667 while ((vma = mm->mmap)) {
1668 mm->mmap = vma->vm_next;
1669 delete_vma_from_mm(vma);
1670 delete_vma(mm, vma);
1673 kleave("");
1676 unsigned long do_brk(unsigned long addr, unsigned long len)
1678 return -ENOMEM;
1682 * expand (or shrink) an existing mapping, potentially moving it at the same
1683 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1685 * under NOMMU conditions, we only permit changing a mapping's size, and only
1686 * as long as it stays within the region allocated by do_mmap_private() and the
1687 * block is not shareable
1689 * MREMAP_FIXED is not supported under NOMMU conditions
1691 unsigned long do_mremap(unsigned long addr,
1692 unsigned long old_len, unsigned long new_len,
1693 unsigned long flags, unsigned long new_addr)
1695 struct vm_area_struct *vma;
1697 /* insanity checks first */
1698 if (old_len == 0 || new_len == 0)
1699 return (unsigned long) -EINVAL;
1701 if (addr & ~PAGE_MASK)
1702 return -EINVAL;
1704 if (flags & MREMAP_FIXED && new_addr != addr)
1705 return (unsigned long) -EINVAL;
1707 vma = find_vma_exact(current->mm, addr, old_len);
1708 if (!vma)
1709 return (unsigned long) -EINVAL;
1711 if (vma->vm_end != vma->vm_start + old_len)
1712 return (unsigned long) -EFAULT;
1714 if (vma->vm_flags & VM_MAYSHARE)
1715 return (unsigned long) -EPERM;
1717 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1718 return (unsigned long) -ENOMEM;
1720 /* all checks complete - do it */
1721 vma->vm_end = vma->vm_start + new_len;
1722 return vma->vm_start;
1724 EXPORT_SYMBOL(do_mremap);
1726 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1727 unsigned long, new_len, unsigned long, flags,
1728 unsigned long, new_addr)
1730 unsigned long ret;
1732 down_write(&current->mm->mmap_sem);
1733 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1734 up_write(&current->mm->mmap_sem);
1735 return ret;
1738 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1739 unsigned int foll_flags)
1741 return NULL;
1744 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1745 unsigned long to, unsigned long size, pgprot_t prot)
1747 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1748 return 0;
1750 EXPORT_SYMBOL(remap_pfn_range);
1752 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1753 unsigned long pgoff)
1755 unsigned int size = vma->vm_end - vma->vm_start;
1757 if (!(vma->vm_flags & VM_USERMAP))
1758 return -EINVAL;
1760 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1761 vma->vm_end = vma->vm_start + size;
1763 return 0;
1765 EXPORT_SYMBOL(remap_vmalloc_range);
1767 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1771 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1772 unsigned long len, unsigned long pgoff, unsigned long flags)
1774 return -ENOMEM;
1777 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1781 void unmap_mapping_range(struct address_space *mapping,
1782 loff_t const holebegin, loff_t const holelen,
1783 int even_cows)
1786 EXPORT_SYMBOL(unmap_mapping_range);
1789 * Check that a process has enough memory to allocate a new virtual
1790 * mapping. 0 means there is enough memory for the allocation to
1791 * succeed and -ENOMEM implies there is not.
1793 * We currently support three overcommit policies, which are set via the
1794 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1796 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1797 * Additional code 2002 Jul 20 by Robert Love.
1799 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1801 * Note this is a helper function intended to be used by LSMs which
1802 * wish to use this logic.
1804 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1806 unsigned long free, allowed;
1808 vm_acct_memory(pages);
1811 * Sometimes we want to use more memory than we have
1813 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1814 return 0;
1816 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1817 unsigned long n;
1819 free = global_page_state(NR_FILE_PAGES);
1820 free += nr_swap_pages;
1823 * Any slabs which are created with the
1824 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1825 * which are reclaimable, under pressure. The dentry
1826 * cache and most inode caches should fall into this
1828 free += global_page_state(NR_SLAB_RECLAIMABLE);
1831 * Leave the last 3% for root
1833 if (!cap_sys_admin)
1834 free -= free / 32;
1836 if (free > pages)
1837 return 0;
1840 * nr_free_pages() is very expensive on large systems,
1841 * only call if we're about to fail.
1843 n = nr_free_pages();
1846 * Leave reserved pages. The pages are not for anonymous pages.
1848 if (n <= totalreserve_pages)
1849 goto error;
1850 else
1851 n -= totalreserve_pages;
1854 * Leave the last 3% for root
1856 if (!cap_sys_admin)
1857 n -= n / 32;
1858 free += n;
1860 if (free > pages)
1861 return 0;
1863 goto error;
1866 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1868 * Leave the last 3% for root
1870 if (!cap_sys_admin)
1871 allowed -= allowed / 32;
1872 allowed += total_swap_pages;
1874 /* Don't let a single process grow too big:
1875 leave 3% of the size of this process for other processes */
1876 if (mm)
1877 allowed -= mm->total_vm / 32;
1879 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1880 return 0;
1882 error:
1883 vm_unacct_memory(pages);
1885 return -ENOMEM;
1888 int in_gate_area_no_task(unsigned long addr)
1890 return 0;
1893 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1895 BUG();
1896 return 0;
1898 EXPORT_SYMBOL(filemap_fault);
1901 * Access another process' address space.
1902 * - source/target buffer must be kernel space
1904 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1906 struct vm_area_struct *vma;
1907 struct mm_struct *mm;
1909 if (addr + len < addr)
1910 return 0;
1912 mm = get_task_mm(tsk);
1913 if (!mm)
1914 return 0;
1916 down_read(&mm->mmap_sem);
1918 /* the access must start within one of the target process's mappings */
1919 vma = find_vma(mm, addr);
1920 if (vma) {
1921 /* don't overrun this mapping */
1922 if (addr + len >= vma->vm_end)
1923 len = vma->vm_end - addr;
1925 /* only read or write mappings where it is permitted */
1926 if (write && vma->vm_flags & VM_MAYWRITE)
1927 copy_to_user_page(vma, NULL, addr,
1928 (void *) addr, buf, len);
1929 else if (!write && vma->vm_flags & VM_MAYREAD)
1930 copy_from_user_page(vma, NULL, addr,
1931 buf, (void *) addr, len);
1932 else
1933 len = 0;
1934 } else {
1935 len = 0;
1938 up_read(&mm->mmap_sem);
1939 mmput(mm);
1940 return len;
1944 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1945 * @inode: The inode to check
1946 * @size: The current filesize of the inode
1947 * @newsize: The proposed filesize of the inode
1949 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1950 * make sure that that any outstanding VMAs aren't broken and then shrink the
1951 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1952 * automatically grant mappings that are too large.
1954 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1955 size_t newsize)
1957 struct vm_area_struct *vma;
1958 struct prio_tree_iter iter;
1959 struct vm_region *region;
1960 pgoff_t low, high;
1961 size_t r_size, r_top;
1963 low = newsize >> PAGE_SHIFT;
1964 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1966 down_write(&nommu_region_sem);
1968 /* search for VMAs that fall within the dead zone */
1969 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
1970 low, high) {
1971 /* found one - only interested if it's shared out of the page
1972 * cache */
1973 if (vma->vm_flags & VM_SHARED) {
1974 up_write(&nommu_region_sem);
1975 return -ETXTBSY; /* not quite true, but near enough */
1979 /* reduce any regions that overlap the dead zone - if in existence,
1980 * these will be pointed to by VMAs that don't overlap the dead zone
1982 * we don't check for any regions that start beyond the EOF as there
1983 * shouldn't be any
1985 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
1986 0, ULONG_MAX) {
1987 if (!(vma->vm_flags & VM_SHARED))
1988 continue;
1990 region = vma->vm_region;
1991 r_size = region->vm_top - region->vm_start;
1992 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1994 if (r_top > newsize) {
1995 region->vm_top -= r_top - newsize;
1996 if (region->vm_end > region->vm_top)
1997 region->vm_end = region->vm_top;
2001 up_write(&nommu_region_sem);
2002 return 0;