Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6/cjktty.git] / mm / nommu.c
blobef4045d010d5f37631c3ad2f9de0e824e1de65a7
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-2010 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>
32 #include <linux/audit.h>
34 #include <asm/uaccess.h>
35 #include <asm/tlb.h>
36 #include <asm/tlbflush.h>
37 #include <asm/mmu_context.h>
38 #include "internal.h"
40 #if 0
41 #define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43 #define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45 #define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47 #else
48 #define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50 #define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52 #define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
54 #endif
56 void *high_memory;
57 struct page *mem_map;
58 unsigned long max_mapnr;
59 unsigned long num_physpages;
60 unsigned long highest_memmap_pfn;
61 struct percpu_counter vm_committed_as;
62 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
63 int sysctl_overcommit_ratio = 50; /* default is 50% */
64 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
65 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
66 int heap_stack_gap = 0;
68 atomic_long_t mmap_pages_allocated;
70 EXPORT_SYMBOL(mem_map);
71 EXPORT_SYMBOL(num_physpages);
73 /* list of mapped, potentially shareable regions */
74 static struct kmem_cache *vm_region_jar;
75 struct rb_root nommu_region_tree = RB_ROOT;
76 DECLARE_RWSEM(nommu_region_sem);
78 const struct vm_operations_struct generic_file_vm_ops = {
82 * Return the total memory allocated for this pointer, not
83 * just what the caller asked for.
85 * Doesn't have to be accurate, i.e. may have races.
87 unsigned int kobjsize(const void *objp)
89 struct page *page;
92 * If the object we have should not have ksize performed on it,
93 * return size of 0
95 if (!objp || !virt_addr_valid(objp))
96 return 0;
98 page = virt_to_head_page(objp);
101 * If the allocator sets PageSlab, we know the pointer came from
102 * kmalloc().
104 if (PageSlab(page))
105 return ksize(objp);
108 * If it's not a compound page, see if we have a matching VMA
109 * region. This test is intentionally done in reverse order,
110 * so if there's no VMA, we still fall through and hand back
111 * PAGE_SIZE for 0-order pages.
113 if (!PageCompound(page)) {
114 struct vm_area_struct *vma;
116 vma = find_vma(current->mm, (unsigned long)objp);
117 if (vma)
118 return vma->vm_end - vma->vm_start;
122 * The ksize() function is only guaranteed to work for pointers
123 * returned by kmalloc(). So handle arbitrary pointers here.
125 return PAGE_SIZE << compound_order(page);
128 int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
129 unsigned long start, int nr_pages, unsigned int foll_flags,
130 struct page **pages, struct vm_area_struct **vmas)
132 struct vm_area_struct *vma;
133 unsigned long vm_flags;
134 int i;
136 /* calculate required read or write permissions.
137 * If FOLL_FORCE is set, we only require the "MAY" flags.
139 vm_flags = (foll_flags & FOLL_WRITE) ?
140 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
141 vm_flags &= (foll_flags & FOLL_FORCE) ?
142 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
144 for (i = 0; i < nr_pages; i++) {
145 vma = find_vma(mm, start);
146 if (!vma)
147 goto finish_or_fault;
149 /* protect what we can, including chardevs */
150 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
151 !(vm_flags & vma->vm_flags))
152 goto finish_or_fault;
154 if (pages) {
155 pages[i] = virt_to_page(start);
156 if (pages[i])
157 page_cache_get(pages[i]);
159 if (vmas)
160 vmas[i] = vma;
161 start = (start + PAGE_SIZE) & PAGE_MASK;
164 return i;
166 finish_or_fault:
167 return i ? : -EFAULT;
171 * get a list of pages in an address range belonging to the specified process
172 * and indicate the VMA that covers each page
173 * - this is potentially dodgy as we may end incrementing the page count of a
174 * slab page or a secondary page from a compound page
175 * - don't permit access to VMAs that don't support it, such as I/O mappings
177 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
178 unsigned long start, int nr_pages, int write, int force,
179 struct page **pages, struct vm_area_struct **vmas)
181 int flags = 0;
183 if (write)
184 flags |= FOLL_WRITE;
185 if (force)
186 flags |= FOLL_FORCE;
188 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
190 EXPORT_SYMBOL(get_user_pages);
193 * follow_pfn - look up PFN at a user virtual address
194 * @vma: memory mapping
195 * @address: user virtual address
196 * @pfn: location to store found PFN
198 * Only IO mappings and raw PFN mappings are allowed.
200 * Returns zero and the pfn at @pfn on success, -ve otherwise.
202 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
203 unsigned long *pfn)
205 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
206 return -EINVAL;
208 *pfn = address >> PAGE_SHIFT;
209 return 0;
211 EXPORT_SYMBOL(follow_pfn);
213 DEFINE_RWLOCK(vmlist_lock);
214 struct vm_struct *vmlist;
216 void vfree(const void *addr)
218 kfree(addr);
220 EXPORT_SYMBOL(vfree);
222 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
225 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
226 * returns only a logical address.
228 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
230 EXPORT_SYMBOL(__vmalloc);
232 void *vmalloc_user(unsigned long size)
234 void *ret;
236 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
237 PAGE_KERNEL);
238 if (ret) {
239 struct vm_area_struct *vma;
241 down_write(&current->mm->mmap_sem);
242 vma = find_vma(current->mm, (unsigned long)ret);
243 if (vma)
244 vma->vm_flags |= VM_USERMAP;
245 up_write(&current->mm->mmap_sem);
248 return ret;
250 EXPORT_SYMBOL(vmalloc_user);
252 struct page *vmalloc_to_page(const void *addr)
254 return virt_to_page(addr);
256 EXPORT_SYMBOL(vmalloc_to_page);
258 unsigned long vmalloc_to_pfn(const void *addr)
260 return page_to_pfn(virt_to_page(addr));
262 EXPORT_SYMBOL(vmalloc_to_pfn);
264 long vread(char *buf, char *addr, unsigned long count)
266 memcpy(buf, addr, count);
267 return count;
270 long vwrite(char *buf, char *addr, unsigned long count)
272 /* Don't allow overflow */
273 if ((unsigned long) addr + count < count)
274 count = -(unsigned long) addr;
276 memcpy(addr, buf, count);
277 return(count);
281 * vmalloc - allocate virtually continguos memory
283 * @size: allocation size
285 * Allocate enough pages to cover @size from the page level
286 * allocator and map them into continguos kernel virtual space.
288 * For tight control over page level allocator and protection flags
289 * use __vmalloc() instead.
291 void *vmalloc(unsigned long size)
293 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
295 EXPORT_SYMBOL(vmalloc);
298 * vzalloc - allocate virtually continguos memory with zero fill
300 * @size: allocation size
302 * Allocate enough pages to cover @size from the page level
303 * allocator and map them into continguos kernel virtual space.
304 * The memory allocated is set to zero.
306 * For tight control over page level allocator and protection flags
307 * use __vmalloc() instead.
309 void *vzalloc(unsigned long size)
311 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
312 PAGE_KERNEL);
314 EXPORT_SYMBOL(vzalloc);
317 * vmalloc_node - allocate memory on a specific node
318 * @size: allocation size
319 * @node: numa node
321 * Allocate enough pages to cover @size from the page level
322 * allocator and map them into contiguous kernel virtual space.
324 * For tight control over page level allocator and protection flags
325 * use __vmalloc() instead.
327 void *vmalloc_node(unsigned long size, int node)
329 return vmalloc(size);
331 EXPORT_SYMBOL(vmalloc_node);
334 * vzalloc_node - allocate memory on a specific node with zero fill
335 * @size: allocation size
336 * @node: numa node
338 * Allocate enough pages to cover @size from the page level
339 * allocator and map them into contiguous kernel virtual space.
340 * The memory allocated is set to zero.
342 * For tight control over page level allocator and protection flags
343 * use __vmalloc() instead.
345 void *vzalloc_node(unsigned long size, int node)
347 return vzalloc(size);
349 EXPORT_SYMBOL(vzalloc_node);
351 #ifndef PAGE_KERNEL_EXEC
352 # define PAGE_KERNEL_EXEC PAGE_KERNEL
353 #endif
356 * vmalloc_exec - allocate virtually contiguous, executable memory
357 * @size: allocation size
359 * Kernel-internal function to allocate enough pages to cover @size
360 * the page level allocator and map them into contiguous and
361 * executable kernel virtual space.
363 * For tight control over page level allocator and protection flags
364 * use __vmalloc() instead.
367 void *vmalloc_exec(unsigned long size)
369 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
373 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
374 * @size: allocation size
376 * Allocate enough 32bit PA addressable pages to cover @size from the
377 * page level allocator and map them into continguos kernel virtual space.
379 void *vmalloc_32(unsigned long size)
381 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
383 EXPORT_SYMBOL(vmalloc_32);
386 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
387 * @size: allocation size
389 * The resulting memory area is 32bit addressable and zeroed so it can be
390 * mapped to userspace without leaking data.
392 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
393 * remap_vmalloc_range() are permissible.
395 void *vmalloc_32_user(unsigned long size)
398 * We'll have to sort out the ZONE_DMA bits for 64-bit,
399 * but for now this can simply use vmalloc_user() directly.
401 return vmalloc_user(size);
403 EXPORT_SYMBOL(vmalloc_32_user);
405 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
407 BUG();
408 return NULL;
410 EXPORT_SYMBOL(vmap);
412 void vunmap(const void *addr)
414 BUG();
416 EXPORT_SYMBOL(vunmap);
418 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
420 BUG();
421 return NULL;
423 EXPORT_SYMBOL(vm_map_ram);
425 void vm_unmap_ram(const void *mem, unsigned int count)
427 BUG();
429 EXPORT_SYMBOL(vm_unmap_ram);
431 void vm_unmap_aliases(void)
434 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
437 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
438 * have one.
440 void __attribute__((weak)) vmalloc_sync_all(void)
445 * alloc_vm_area - allocate a range of kernel address space
446 * @size: size of the area
448 * Returns: NULL on failure, vm_struct on success
450 * This function reserves a range of kernel address space, and
451 * allocates pagetables to map that range. No actual mappings
452 * are created. If the kernel address space is not shared
453 * between processes, it syncs the pagetable across all
454 * processes.
456 struct vm_struct *alloc_vm_area(size_t size)
458 BUG();
459 return NULL;
461 EXPORT_SYMBOL_GPL(alloc_vm_area);
463 void free_vm_area(struct vm_struct *area)
465 BUG();
467 EXPORT_SYMBOL_GPL(free_vm_area);
469 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
470 struct page *page)
472 return -EINVAL;
474 EXPORT_SYMBOL(vm_insert_page);
477 * sys_brk() for the most part doesn't need the global kernel
478 * lock, except when an application is doing something nasty
479 * like trying to un-brk an area that has already been mapped
480 * to a regular file. in this case, the unmapping will need
481 * to invoke file system routines that need the global lock.
483 SYSCALL_DEFINE1(brk, unsigned long, brk)
485 struct mm_struct *mm = current->mm;
487 if (brk < mm->start_brk || brk > mm->context.end_brk)
488 return mm->brk;
490 if (mm->brk == brk)
491 return mm->brk;
494 * Always allow shrinking brk
496 if (brk <= mm->brk) {
497 mm->brk = brk;
498 return brk;
502 * Ok, looks good - let it rip.
504 flush_icache_range(mm->brk, brk);
505 return mm->brk = brk;
509 * initialise the VMA and region record slabs
511 void __init mmap_init(void)
513 int ret;
515 ret = percpu_counter_init(&vm_committed_as, 0);
516 VM_BUG_ON(ret);
517 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
521 * validate the region tree
522 * - the caller must hold the region lock
524 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
525 static noinline void validate_nommu_regions(void)
527 struct vm_region *region, *last;
528 struct rb_node *p, *lastp;
530 lastp = rb_first(&nommu_region_tree);
531 if (!lastp)
532 return;
534 last = rb_entry(lastp, struct vm_region, vm_rb);
535 BUG_ON(unlikely(last->vm_end <= last->vm_start));
536 BUG_ON(unlikely(last->vm_top < last->vm_end));
538 while ((p = rb_next(lastp))) {
539 region = rb_entry(p, struct vm_region, vm_rb);
540 last = rb_entry(lastp, struct vm_region, vm_rb);
542 BUG_ON(unlikely(region->vm_end <= region->vm_start));
543 BUG_ON(unlikely(region->vm_top < region->vm_end));
544 BUG_ON(unlikely(region->vm_start < last->vm_top));
546 lastp = p;
549 #else
550 static void validate_nommu_regions(void)
553 #endif
556 * add a region into the global tree
558 static void add_nommu_region(struct vm_region *region)
560 struct vm_region *pregion;
561 struct rb_node **p, *parent;
563 validate_nommu_regions();
565 parent = NULL;
566 p = &nommu_region_tree.rb_node;
567 while (*p) {
568 parent = *p;
569 pregion = rb_entry(parent, struct vm_region, vm_rb);
570 if (region->vm_start < pregion->vm_start)
571 p = &(*p)->rb_left;
572 else if (region->vm_start > pregion->vm_start)
573 p = &(*p)->rb_right;
574 else if (pregion == region)
575 return;
576 else
577 BUG();
580 rb_link_node(&region->vm_rb, parent, p);
581 rb_insert_color(&region->vm_rb, &nommu_region_tree);
583 validate_nommu_regions();
587 * delete a region from the global tree
589 static void delete_nommu_region(struct vm_region *region)
591 BUG_ON(!nommu_region_tree.rb_node);
593 validate_nommu_regions();
594 rb_erase(&region->vm_rb, &nommu_region_tree);
595 validate_nommu_regions();
599 * free a contiguous series of pages
601 static void free_page_series(unsigned long from, unsigned long to)
603 for (; from < to; from += PAGE_SIZE) {
604 struct page *page = virt_to_page(from);
606 kdebug("- free %lx", from);
607 atomic_long_dec(&mmap_pages_allocated);
608 if (page_count(page) != 1)
609 kdebug("free page %p: refcount not one: %d",
610 page, page_count(page));
611 put_page(page);
616 * release a reference to a region
617 * - the caller must hold the region semaphore for writing, which this releases
618 * - the region may not have been added to the tree yet, in which case vm_top
619 * will equal vm_start
621 static void __put_nommu_region(struct vm_region *region)
622 __releases(nommu_region_sem)
624 kenter("%p{%d}", region, region->vm_usage);
626 BUG_ON(!nommu_region_tree.rb_node);
628 if (--region->vm_usage == 0) {
629 if (region->vm_top > region->vm_start)
630 delete_nommu_region(region);
631 up_write(&nommu_region_sem);
633 if (region->vm_file)
634 fput(region->vm_file);
636 /* IO memory and memory shared directly out of the pagecache
637 * from ramfs/tmpfs mustn't be released here */
638 if (region->vm_flags & VM_MAPPED_COPY) {
639 kdebug("free series");
640 free_page_series(region->vm_start, region->vm_top);
642 kmem_cache_free(vm_region_jar, region);
643 } else {
644 up_write(&nommu_region_sem);
649 * release a reference to a region
651 static void put_nommu_region(struct vm_region *region)
653 down_write(&nommu_region_sem);
654 __put_nommu_region(region);
658 * update protection on a vma
660 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
662 #ifdef CONFIG_MPU
663 struct mm_struct *mm = vma->vm_mm;
664 long start = vma->vm_start & PAGE_MASK;
665 while (start < vma->vm_end) {
666 protect_page(mm, start, flags);
667 start += PAGE_SIZE;
669 update_protections(mm);
670 #endif
674 * add a VMA into a process's mm_struct in the appropriate place in the list
675 * and tree and add to the address space's page tree also if not an anonymous
676 * page
677 * - should be called with mm->mmap_sem held writelocked
679 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
681 struct vm_area_struct *pvma, **pp, *next;
682 struct address_space *mapping;
683 struct rb_node **p, *parent;
685 kenter(",%p", vma);
687 BUG_ON(!vma->vm_region);
689 mm->map_count++;
690 vma->vm_mm = mm;
692 protect_vma(vma, vma->vm_flags);
694 /* add the VMA to the mapping */
695 if (vma->vm_file) {
696 mapping = vma->vm_file->f_mapping;
698 flush_dcache_mmap_lock(mapping);
699 vma_prio_tree_insert(vma, &mapping->i_mmap);
700 flush_dcache_mmap_unlock(mapping);
703 /* add the VMA to the tree */
704 parent = NULL;
705 p = &mm->mm_rb.rb_node;
706 while (*p) {
707 parent = *p;
708 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
710 /* sort by: start addr, end addr, VMA struct addr in that order
711 * (the latter is necessary as we may get identical VMAs) */
712 if (vma->vm_start < pvma->vm_start)
713 p = &(*p)->rb_left;
714 else if (vma->vm_start > pvma->vm_start)
715 p = &(*p)->rb_right;
716 else if (vma->vm_end < pvma->vm_end)
717 p = &(*p)->rb_left;
718 else if (vma->vm_end > pvma->vm_end)
719 p = &(*p)->rb_right;
720 else if (vma < pvma)
721 p = &(*p)->rb_left;
722 else if (vma > pvma)
723 p = &(*p)->rb_right;
724 else
725 BUG();
728 rb_link_node(&vma->vm_rb, parent, p);
729 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
731 /* add VMA to the VMA list also */
732 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
733 if (pvma->vm_start > vma->vm_start)
734 break;
735 if (pvma->vm_start < vma->vm_start)
736 continue;
737 if (pvma->vm_end < vma->vm_end)
738 break;
741 next = *pp;
742 *pp = vma;
743 vma->vm_next = next;
744 if (next)
745 next->vm_prev = vma;
749 * delete a VMA from its owning mm_struct and address space
751 static void delete_vma_from_mm(struct vm_area_struct *vma)
753 struct vm_area_struct **pp;
754 struct address_space *mapping;
755 struct mm_struct *mm = vma->vm_mm;
757 kenter("%p", vma);
759 protect_vma(vma, 0);
761 mm->map_count--;
762 if (mm->mmap_cache == vma)
763 mm->mmap_cache = NULL;
765 /* remove the VMA from the mapping */
766 if (vma->vm_file) {
767 mapping = vma->vm_file->f_mapping;
769 flush_dcache_mmap_lock(mapping);
770 vma_prio_tree_remove(vma, &mapping->i_mmap);
771 flush_dcache_mmap_unlock(mapping);
774 /* remove from the MM's tree and list */
775 rb_erase(&vma->vm_rb, &mm->mm_rb);
776 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
777 if (*pp == vma) {
778 *pp = vma->vm_next;
779 break;
783 vma->vm_mm = NULL;
787 * destroy a VMA record
789 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
791 kenter("%p", vma);
792 if (vma->vm_ops && vma->vm_ops->close)
793 vma->vm_ops->close(vma);
794 if (vma->vm_file) {
795 fput(vma->vm_file);
796 if (vma->vm_flags & VM_EXECUTABLE)
797 removed_exe_file_vma(mm);
799 put_nommu_region(vma->vm_region);
800 kmem_cache_free(vm_area_cachep, vma);
804 * look up the first VMA in which addr resides, NULL if none
805 * - should be called with mm->mmap_sem at least held readlocked
807 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
809 struct vm_area_struct *vma;
810 struct rb_node *n = mm->mm_rb.rb_node;
812 /* check the cache first */
813 vma = mm->mmap_cache;
814 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
815 return vma;
817 /* trawl the tree (there may be multiple mappings in which addr
818 * resides) */
819 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
820 vma = rb_entry(n, struct vm_area_struct, vm_rb);
821 if (vma->vm_start > addr)
822 return NULL;
823 if (vma->vm_end > addr) {
824 mm->mmap_cache = vma;
825 return vma;
829 return NULL;
831 EXPORT_SYMBOL(find_vma);
834 * find a VMA
835 * - we don't extend stack VMAs under NOMMU conditions
837 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
839 return find_vma(mm, addr);
843 * expand a stack to a given address
844 * - not supported under NOMMU conditions
846 int expand_stack(struct vm_area_struct *vma, unsigned long address)
848 return -ENOMEM;
852 * look up the first VMA exactly that exactly matches addr
853 * - should be called with mm->mmap_sem at least held readlocked
855 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
856 unsigned long addr,
857 unsigned long len)
859 struct vm_area_struct *vma;
860 struct rb_node *n = mm->mm_rb.rb_node;
861 unsigned long end = addr + len;
863 /* check the cache first */
864 vma = mm->mmap_cache;
865 if (vma && vma->vm_start == addr && vma->vm_end == end)
866 return vma;
868 /* trawl the tree (there may be multiple mappings in which addr
869 * resides) */
870 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
871 vma = rb_entry(n, struct vm_area_struct, vm_rb);
872 if (vma->vm_start < addr)
873 continue;
874 if (vma->vm_start > addr)
875 return NULL;
876 if (vma->vm_end == end) {
877 mm->mmap_cache = vma;
878 return vma;
882 return NULL;
886 * determine whether a mapping should be permitted and, if so, what sort of
887 * mapping we're capable of supporting
889 static int validate_mmap_request(struct file *file,
890 unsigned long addr,
891 unsigned long len,
892 unsigned long prot,
893 unsigned long flags,
894 unsigned long pgoff,
895 unsigned long *_capabilities)
897 unsigned long capabilities, rlen;
898 unsigned long reqprot = prot;
899 int ret;
901 /* do the simple checks first */
902 if (flags & MAP_FIXED) {
903 printk(KERN_DEBUG
904 "%d: Can't do fixed-address/overlay mmap of RAM\n",
905 current->pid);
906 return -EINVAL;
909 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
910 (flags & MAP_TYPE) != MAP_SHARED)
911 return -EINVAL;
913 if (!len)
914 return -EINVAL;
916 /* Careful about overflows.. */
917 rlen = PAGE_ALIGN(len);
918 if (!rlen || rlen > TASK_SIZE)
919 return -ENOMEM;
921 /* offset overflow? */
922 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
923 return -EOVERFLOW;
925 if (file) {
926 /* validate file mapping requests */
927 struct address_space *mapping;
929 /* files must support mmap */
930 if (!file->f_op || !file->f_op->mmap)
931 return -ENODEV;
933 /* work out if what we've got could possibly be shared
934 * - we support chardevs that provide their own "memory"
935 * - we support files/blockdevs that are memory backed
937 mapping = file->f_mapping;
938 if (!mapping)
939 mapping = file->f_path.dentry->d_inode->i_mapping;
941 capabilities = 0;
942 if (mapping && mapping->backing_dev_info)
943 capabilities = mapping->backing_dev_info->capabilities;
945 if (!capabilities) {
946 /* no explicit capabilities set, so assume some
947 * defaults */
948 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
949 case S_IFREG:
950 case S_IFBLK:
951 capabilities = BDI_CAP_MAP_COPY;
952 break;
954 case S_IFCHR:
955 capabilities =
956 BDI_CAP_MAP_DIRECT |
957 BDI_CAP_READ_MAP |
958 BDI_CAP_WRITE_MAP;
959 break;
961 default:
962 return -EINVAL;
966 /* eliminate any capabilities that we can't support on this
967 * device */
968 if (!file->f_op->get_unmapped_area)
969 capabilities &= ~BDI_CAP_MAP_DIRECT;
970 if (!file->f_op->read)
971 capabilities &= ~BDI_CAP_MAP_COPY;
973 /* The file shall have been opened with read permission. */
974 if (!(file->f_mode & FMODE_READ))
975 return -EACCES;
977 if (flags & MAP_SHARED) {
978 /* do checks for writing, appending and locking */
979 if ((prot & PROT_WRITE) &&
980 !(file->f_mode & FMODE_WRITE))
981 return -EACCES;
983 if (IS_APPEND(file->f_path.dentry->d_inode) &&
984 (file->f_mode & FMODE_WRITE))
985 return -EACCES;
987 if (locks_verify_locked(file->f_path.dentry->d_inode))
988 return -EAGAIN;
990 if (!(capabilities & BDI_CAP_MAP_DIRECT))
991 return -ENODEV;
993 /* we mustn't privatise shared mappings */
994 capabilities &= ~BDI_CAP_MAP_COPY;
996 else {
997 /* we're going to read the file into private memory we
998 * allocate */
999 if (!(capabilities & BDI_CAP_MAP_COPY))
1000 return -ENODEV;
1002 /* we don't permit a private writable mapping to be
1003 * shared with the backing device */
1004 if (prot & PROT_WRITE)
1005 capabilities &= ~BDI_CAP_MAP_DIRECT;
1008 if (capabilities & BDI_CAP_MAP_DIRECT) {
1009 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1010 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1011 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1013 capabilities &= ~BDI_CAP_MAP_DIRECT;
1014 if (flags & MAP_SHARED) {
1015 printk(KERN_WARNING
1016 "MAP_SHARED not completely supported on !MMU\n");
1017 return -EINVAL;
1022 /* handle executable mappings and implied executable
1023 * mappings */
1024 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1025 if (prot & PROT_EXEC)
1026 return -EPERM;
1028 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1029 /* handle implication of PROT_EXEC by PROT_READ */
1030 if (current->personality & READ_IMPLIES_EXEC) {
1031 if (capabilities & BDI_CAP_EXEC_MAP)
1032 prot |= PROT_EXEC;
1035 else if ((prot & PROT_READ) &&
1036 (prot & PROT_EXEC) &&
1037 !(capabilities & BDI_CAP_EXEC_MAP)
1039 /* backing file is not executable, try to copy */
1040 capabilities &= ~BDI_CAP_MAP_DIRECT;
1043 else {
1044 /* anonymous mappings are always memory backed and can be
1045 * privately mapped
1047 capabilities = BDI_CAP_MAP_COPY;
1049 /* handle PROT_EXEC implication by PROT_READ */
1050 if ((prot & PROT_READ) &&
1051 (current->personality & READ_IMPLIES_EXEC))
1052 prot |= PROT_EXEC;
1055 /* allow the security API to have its say */
1056 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1057 if (ret < 0)
1058 return ret;
1060 /* looks okay */
1061 *_capabilities = capabilities;
1062 return 0;
1066 * we've determined that we can make the mapping, now translate what we
1067 * now know into VMA flags
1069 static unsigned long determine_vm_flags(struct file *file,
1070 unsigned long prot,
1071 unsigned long flags,
1072 unsigned long capabilities)
1074 unsigned long vm_flags;
1076 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1077 /* vm_flags |= mm->def_flags; */
1079 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1080 /* attempt to share read-only copies of mapped file chunks */
1081 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1082 if (file && !(prot & PROT_WRITE))
1083 vm_flags |= VM_MAYSHARE;
1084 } else {
1085 /* overlay a shareable mapping on the backing device or inode
1086 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1087 * romfs/cramfs */
1088 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1089 if (flags & MAP_SHARED)
1090 vm_flags |= VM_SHARED;
1093 /* refuse to let anyone share private mappings with this process if
1094 * it's being traced - otherwise breakpoints set in it may interfere
1095 * with another untraced process
1097 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
1098 vm_flags &= ~VM_MAYSHARE;
1100 return vm_flags;
1104 * set up a shared mapping on a file (the driver or filesystem provides and
1105 * pins the storage)
1107 static int do_mmap_shared_file(struct vm_area_struct *vma)
1109 int ret;
1111 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1112 if (ret == 0) {
1113 vma->vm_region->vm_top = vma->vm_region->vm_end;
1114 return 0;
1116 if (ret != -ENOSYS)
1117 return ret;
1119 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1120 * opposed to tried but failed) so we can only give a suitable error as
1121 * it's not possible to make a private copy if MAP_SHARED was given */
1122 return -ENODEV;
1126 * set up a private mapping or an anonymous shared mapping
1128 static int do_mmap_private(struct vm_area_struct *vma,
1129 struct vm_region *region,
1130 unsigned long len,
1131 unsigned long capabilities)
1133 struct page *pages;
1134 unsigned long total, point, n, rlen;
1135 void *base;
1136 int ret, order;
1138 /* invoke the file's mapping function so that it can keep track of
1139 * shared mappings on devices or memory
1140 * - VM_MAYSHARE will be set if it may attempt to share
1142 if (capabilities & BDI_CAP_MAP_DIRECT) {
1143 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1144 if (ret == 0) {
1145 /* shouldn't return success if we're not sharing */
1146 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1147 vma->vm_region->vm_top = vma->vm_region->vm_end;
1148 return 0;
1150 if (ret != -ENOSYS)
1151 return ret;
1153 /* getting an ENOSYS error indicates that direct mmap isn't
1154 * possible (as opposed to tried but failed) so we'll try to
1155 * make a private copy of the data and map that instead */
1158 rlen = PAGE_ALIGN(len);
1160 /* allocate some memory to hold the mapping
1161 * - note that this may not return a page-aligned address if the object
1162 * we're allocating is smaller than a page
1164 order = get_order(rlen);
1165 kdebug("alloc order %d for %lx", order, len);
1167 pages = alloc_pages(GFP_KERNEL, order);
1168 if (!pages)
1169 goto enomem;
1171 total = 1 << order;
1172 atomic_long_add(total, &mmap_pages_allocated);
1174 point = rlen >> PAGE_SHIFT;
1176 /* we allocated a power-of-2 sized page set, so we may want to trim off
1177 * the excess */
1178 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1179 while (total > point) {
1180 order = ilog2(total - point);
1181 n = 1 << order;
1182 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1183 atomic_long_sub(n, &mmap_pages_allocated);
1184 total -= n;
1185 set_page_refcounted(pages + total);
1186 __free_pages(pages + total, order);
1190 for (point = 1; point < total; point++)
1191 set_page_refcounted(&pages[point]);
1193 base = page_address(pages);
1194 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1195 region->vm_start = (unsigned long) base;
1196 region->vm_end = region->vm_start + rlen;
1197 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1199 vma->vm_start = region->vm_start;
1200 vma->vm_end = region->vm_start + len;
1202 if (vma->vm_file) {
1203 /* read the contents of a file into the copy */
1204 mm_segment_t old_fs;
1205 loff_t fpos;
1207 fpos = vma->vm_pgoff;
1208 fpos <<= PAGE_SHIFT;
1210 old_fs = get_fs();
1211 set_fs(KERNEL_DS);
1212 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
1213 set_fs(old_fs);
1215 if (ret < 0)
1216 goto error_free;
1218 /* clear the last little bit */
1219 if (ret < rlen)
1220 memset(base + ret, 0, rlen - ret);
1224 return 0;
1226 error_free:
1227 free_page_series(region->vm_start, region->vm_end);
1228 region->vm_start = vma->vm_start = 0;
1229 region->vm_end = vma->vm_end = 0;
1230 region->vm_top = 0;
1231 return ret;
1233 enomem:
1234 printk("Allocation of length %lu from process %d (%s) failed\n",
1235 len, current->pid, current->comm);
1236 show_free_areas();
1237 return -ENOMEM;
1241 * handle mapping creation for uClinux
1243 unsigned long do_mmap_pgoff(struct file *file,
1244 unsigned long addr,
1245 unsigned long len,
1246 unsigned long prot,
1247 unsigned long flags,
1248 unsigned long pgoff)
1250 struct vm_area_struct *vma;
1251 struct vm_region *region;
1252 struct rb_node *rb;
1253 unsigned long capabilities, vm_flags, result;
1254 int ret;
1256 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1258 /* decide whether we should attempt the mapping, and if so what sort of
1259 * mapping */
1260 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1261 &capabilities);
1262 if (ret < 0) {
1263 kleave(" = %d [val]", ret);
1264 return ret;
1267 /* we ignore the address hint */
1268 addr = 0;
1270 /* we've determined that we can make the mapping, now translate what we
1271 * now know into VMA flags */
1272 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1274 /* we're going to need to record the mapping */
1275 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1276 if (!region)
1277 goto error_getting_region;
1279 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1280 if (!vma)
1281 goto error_getting_vma;
1283 region->vm_usage = 1;
1284 region->vm_flags = vm_flags;
1285 region->vm_pgoff = pgoff;
1287 INIT_LIST_HEAD(&vma->anon_vma_chain);
1288 vma->vm_flags = vm_flags;
1289 vma->vm_pgoff = pgoff;
1291 if (file) {
1292 region->vm_file = file;
1293 get_file(file);
1294 vma->vm_file = file;
1295 get_file(file);
1296 if (vm_flags & VM_EXECUTABLE) {
1297 added_exe_file_vma(current->mm);
1298 vma->vm_mm = current->mm;
1302 down_write(&nommu_region_sem);
1304 /* if we want to share, we need to check for regions created by other
1305 * mmap() calls that overlap with our proposed mapping
1306 * - we can only share with a superset match on most regular files
1307 * - shared mappings on character devices and memory backed files are
1308 * permitted to overlap inexactly as far as we are concerned for in
1309 * these cases, sharing is handled in the driver or filesystem rather
1310 * than here
1312 if (vm_flags & VM_MAYSHARE) {
1313 struct vm_region *pregion;
1314 unsigned long pglen, rpglen, pgend, rpgend, start;
1316 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1317 pgend = pgoff + pglen;
1319 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1320 pregion = rb_entry(rb, struct vm_region, vm_rb);
1322 if (!(pregion->vm_flags & VM_MAYSHARE))
1323 continue;
1325 /* search for overlapping mappings on the same file */
1326 if (pregion->vm_file->f_path.dentry->d_inode !=
1327 file->f_path.dentry->d_inode)
1328 continue;
1330 if (pregion->vm_pgoff >= pgend)
1331 continue;
1333 rpglen = pregion->vm_end - pregion->vm_start;
1334 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1335 rpgend = pregion->vm_pgoff + rpglen;
1336 if (pgoff >= rpgend)
1337 continue;
1339 /* handle inexactly overlapping matches between
1340 * mappings */
1341 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1342 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1343 /* new mapping is not a subset of the region */
1344 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1345 goto sharing_violation;
1346 continue;
1349 /* we've found a region we can share */
1350 pregion->vm_usage++;
1351 vma->vm_region = pregion;
1352 start = pregion->vm_start;
1353 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1354 vma->vm_start = start;
1355 vma->vm_end = start + len;
1357 if (pregion->vm_flags & VM_MAPPED_COPY) {
1358 kdebug("share copy");
1359 vma->vm_flags |= VM_MAPPED_COPY;
1360 } else {
1361 kdebug("share mmap");
1362 ret = do_mmap_shared_file(vma);
1363 if (ret < 0) {
1364 vma->vm_region = NULL;
1365 vma->vm_start = 0;
1366 vma->vm_end = 0;
1367 pregion->vm_usage--;
1368 pregion = NULL;
1369 goto error_just_free;
1372 fput(region->vm_file);
1373 kmem_cache_free(vm_region_jar, region);
1374 region = pregion;
1375 result = start;
1376 goto share;
1379 /* obtain the address at which to make a shared mapping
1380 * - this is the hook for quasi-memory character devices to
1381 * tell us the location of a shared mapping
1383 if (capabilities & BDI_CAP_MAP_DIRECT) {
1384 addr = file->f_op->get_unmapped_area(file, addr, len,
1385 pgoff, flags);
1386 if (IS_ERR((void *) addr)) {
1387 ret = addr;
1388 if (ret != (unsigned long) -ENOSYS)
1389 goto error_just_free;
1391 /* the driver refused to tell us where to site
1392 * the mapping so we'll have to attempt to copy
1393 * it */
1394 ret = (unsigned long) -ENODEV;
1395 if (!(capabilities & BDI_CAP_MAP_COPY))
1396 goto error_just_free;
1398 capabilities &= ~BDI_CAP_MAP_DIRECT;
1399 } else {
1400 vma->vm_start = region->vm_start = addr;
1401 vma->vm_end = region->vm_end = addr + len;
1406 vma->vm_region = region;
1408 /* set up the mapping
1409 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1411 if (file && vma->vm_flags & VM_SHARED)
1412 ret = do_mmap_shared_file(vma);
1413 else
1414 ret = do_mmap_private(vma, region, len, capabilities);
1415 if (ret < 0)
1416 goto error_just_free;
1417 add_nommu_region(region);
1419 /* clear anonymous mappings that don't ask for uninitialized data */
1420 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1421 memset((void *)region->vm_start, 0,
1422 region->vm_end - region->vm_start);
1424 /* okay... we have a mapping; now we have to register it */
1425 result = vma->vm_start;
1427 current->mm->total_vm += len >> PAGE_SHIFT;
1429 share:
1430 add_vma_to_mm(current->mm, vma);
1432 /* we flush the region from the icache only when the first executable
1433 * mapping of it is made */
1434 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1435 flush_icache_range(region->vm_start, region->vm_end);
1436 region->vm_icache_flushed = true;
1439 up_write(&nommu_region_sem);
1441 kleave(" = %lx", result);
1442 return result;
1444 error_just_free:
1445 up_write(&nommu_region_sem);
1446 error:
1447 if (region->vm_file)
1448 fput(region->vm_file);
1449 kmem_cache_free(vm_region_jar, region);
1450 if (vma->vm_file)
1451 fput(vma->vm_file);
1452 if (vma->vm_flags & VM_EXECUTABLE)
1453 removed_exe_file_vma(vma->vm_mm);
1454 kmem_cache_free(vm_area_cachep, vma);
1455 kleave(" = %d", ret);
1456 return ret;
1458 sharing_violation:
1459 up_write(&nommu_region_sem);
1460 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1461 ret = -EINVAL;
1462 goto error;
1464 error_getting_vma:
1465 kmem_cache_free(vm_region_jar, region);
1466 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1467 " from process %d failed\n",
1468 len, current->pid);
1469 show_free_areas();
1470 return -ENOMEM;
1472 error_getting_region:
1473 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1474 " from process %d failed\n",
1475 len, current->pid);
1476 show_free_areas();
1477 return -ENOMEM;
1479 EXPORT_SYMBOL(do_mmap_pgoff);
1481 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1482 unsigned long, prot, unsigned long, flags,
1483 unsigned long, fd, unsigned long, pgoff)
1485 struct file *file = NULL;
1486 unsigned long retval = -EBADF;
1488 audit_mmap_fd(fd, flags);
1489 if (!(flags & MAP_ANONYMOUS)) {
1490 file = fget(fd);
1491 if (!file)
1492 goto out;
1495 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1497 down_write(&current->mm->mmap_sem);
1498 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1499 up_write(&current->mm->mmap_sem);
1501 if (file)
1502 fput(file);
1503 out:
1504 return retval;
1507 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1508 struct mmap_arg_struct {
1509 unsigned long addr;
1510 unsigned long len;
1511 unsigned long prot;
1512 unsigned long flags;
1513 unsigned long fd;
1514 unsigned long offset;
1517 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1519 struct mmap_arg_struct a;
1521 if (copy_from_user(&a, arg, sizeof(a)))
1522 return -EFAULT;
1523 if (a.offset & ~PAGE_MASK)
1524 return -EINVAL;
1526 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1527 a.offset >> PAGE_SHIFT);
1529 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1532 * split a vma into two pieces at address 'addr', a new vma is allocated either
1533 * for the first part or the tail.
1535 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1536 unsigned long addr, int new_below)
1538 struct vm_area_struct *new;
1539 struct vm_region *region;
1540 unsigned long npages;
1542 kenter("");
1544 /* we're only permitted to split anonymous regions (these should have
1545 * only a single usage on the region) */
1546 if (vma->vm_file)
1547 return -ENOMEM;
1549 if (mm->map_count >= sysctl_max_map_count)
1550 return -ENOMEM;
1552 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1553 if (!region)
1554 return -ENOMEM;
1556 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1557 if (!new) {
1558 kmem_cache_free(vm_region_jar, region);
1559 return -ENOMEM;
1562 /* most fields are the same, copy all, and then fixup */
1563 *new = *vma;
1564 *region = *vma->vm_region;
1565 new->vm_region = region;
1567 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1569 if (new_below) {
1570 region->vm_top = region->vm_end = new->vm_end = addr;
1571 } else {
1572 region->vm_start = new->vm_start = addr;
1573 region->vm_pgoff = new->vm_pgoff += npages;
1576 if (new->vm_ops && new->vm_ops->open)
1577 new->vm_ops->open(new);
1579 delete_vma_from_mm(vma);
1580 down_write(&nommu_region_sem);
1581 delete_nommu_region(vma->vm_region);
1582 if (new_below) {
1583 vma->vm_region->vm_start = vma->vm_start = addr;
1584 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1585 } else {
1586 vma->vm_region->vm_end = vma->vm_end = addr;
1587 vma->vm_region->vm_top = addr;
1589 add_nommu_region(vma->vm_region);
1590 add_nommu_region(new->vm_region);
1591 up_write(&nommu_region_sem);
1592 add_vma_to_mm(mm, vma);
1593 add_vma_to_mm(mm, new);
1594 return 0;
1598 * shrink a VMA by removing the specified chunk from either the beginning or
1599 * the end
1601 static int shrink_vma(struct mm_struct *mm,
1602 struct vm_area_struct *vma,
1603 unsigned long from, unsigned long to)
1605 struct vm_region *region;
1607 kenter("");
1609 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1610 * and list */
1611 delete_vma_from_mm(vma);
1612 if (from > vma->vm_start)
1613 vma->vm_end = from;
1614 else
1615 vma->vm_start = to;
1616 add_vma_to_mm(mm, vma);
1618 /* cut the backing region down to size */
1619 region = vma->vm_region;
1620 BUG_ON(region->vm_usage != 1);
1622 down_write(&nommu_region_sem);
1623 delete_nommu_region(region);
1624 if (from > region->vm_start) {
1625 to = region->vm_top;
1626 region->vm_top = region->vm_end = from;
1627 } else {
1628 region->vm_start = to;
1630 add_nommu_region(region);
1631 up_write(&nommu_region_sem);
1633 free_page_series(from, to);
1634 return 0;
1638 * release a mapping
1639 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1640 * VMA, though it need not cover the whole VMA
1642 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1644 struct vm_area_struct *vma;
1645 struct rb_node *rb;
1646 unsigned long end = start + len;
1647 int ret;
1649 kenter(",%lx,%zx", start, len);
1651 if (len == 0)
1652 return -EINVAL;
1654 /* find the first potentially overlapping VMA */
1655 vma = find_vma(mm, start);
1656 if (!vma) {
1657 static int limit = 0;
1658 if (limit < 5) {
1659 printk(KERN_WARNING
1660 "munmap of memory not mmapped by process %d"
1661 " (%s): 0x%lx-0x%lx\n",
1662 current->pid, current->comm,
1663 start, start + len - 1);
1664 limit++;
1666 return -EINVAL;
1669 /* we're allowed to split an anonymous VMA but not a file-backed one */
1670 if (vma->vm_file) {
1671 do {
1672 if (start > vma->vm_start) {
1673 kleave(" = -EINVAL [miss]");
1674 return -EINVAL;
1676 if (end == vma->vm_end)
1677 goto erase_whole_vma;
1678 rb = rb_next(&vma->vm_rb);
1679 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1680 } while (rb);
1681 kleave(" = -EINVAL [split file]");
1682 return -EINVAL;
1683 } else {
1684 /* the chunk must be a subset of the VMA found */
1685 if (start == vma->vm_start && end == vma->vm_end)
1686 goto erase_whole_vma;
1687 if (start < vma->vm_start || end > vma->vm_end) {
1688 kleave(" = -EINVAL [superset]");
1689 return -EINVAL;
1691 if (start & ~PAGE_MASK) {
1692 kleave(" = -EINVAL [unaligned start]");
1693 return -EINVAL;
1695 if (end != vma->vm_end && end & ~PAGE_MASK) {
1696 kleave(" = -EINVAL [unaligned split]");
1697 return -EINVAL;
1699 if (start != vma->vm_start && end != vma->vm_end) {
1700 ret = split_vma(mm, vma, start, 1);
1701 if (ret < 0) {
1702 kleave(" = %d [split]", ret);
1703 return ret;
1706 return shrink_vma(mm, vma, start, end);
1709 erase_whole_vma:
1710 delete_vma_from_mm(vma);
1711 delete_vma(mm, vma);
1712 kleave(" = 0");
1713 return 0;
1715 EXPORT_SYMBOL(do_munmap);
1717 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1719 int ret;
1720 struct mm_struct *mm = current->mm;
1722 down_write(&mm->mmap_sem);
1723 ret = do_munmap(mm, addr, len);
1724 up_write(&mm->mmap_sem);
1725 return ret;
1729 * release all the mappings made in a process's VM space
1731 void exit_mmap(struct mm_struct *mm)
1733 struct vm_area_struct *vma;
1735 if (!mm)
1736 return;
1738 kenter("");
1740 mm->total_vm = 0;
1742 while ((vma = mm->mmap)) {
1743 mm->mmap = vma->vm_next;
1744 delete_vma_from_mm(vma);
1745 delete_vma(mm, vma);
1746 cond_resched();
1749 kleave("");
1752 unsigned long do_brk(unsigned long addr, unsigned long len)
1754 return -ENOMEM;
1758 * expand (or shrink) an existing mapping, potentially moving it at the same
1759 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1761 * under NOMMU conditions, we only permit changing a mapping's size, and only
1762 * as long as it stays within the region allocated by do_mmap_private() and the
1763 * block is not shareable
1765 * MREMAP_FIXED is not supported under NOMMU conditions
1767 unsigned long do_mremap(unsigned long addr,
1768 unsigned long old_len, unsigned long new_len,
1769 unsigned long flags, unsigned long new_addr)
1771 struct vm_area_struct *vma;
1773 /* insanity checks first */
1774 if (old_len == 0 || new_len == 0)
1775 return (unsigned long) -EINVAL;
1777 if (addr & ~PAGE_MASK)
1778 return -EINVAL;
1780 if (flags & MREMAP_FIXED && new_addr != addr)
1781 return (unsigned long) -EINVAL;
1783 vma = find_vma_exact(current->mm, addr, old_len);
1784 if (!vma)
1785 return (unsigned long) -EINVAL;
1787 if (vma->vm_end != vma->vm_start + old_len)
1788 return (unsigned long) -EFAULT;
1790 if (vma->vm_flags & VM_MAYSHARE)
1791 return (unsigned long) -EPERM;
1793 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1794 return (unsigned long) -ENOMEM;
1796 /* all checks complete - do it */
1797 vma->vm_end = vma->vm_start + new_len;
1798 return vma->vm_start;
1800 EXPORT_SYMBOL(do_mremap);
1802 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1803 unsigned long, new_len, unsigned long, flags,
1804 unsigned long, new_addr)
1806 unsigned long ret;
1808 down_write(&current->mm->mmap_sem);
1809 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1810 up_write(&current->mm->mmap_sem);
1811 return ret;
1814 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1815 unsigned int foll_flags)
1817 return NULL;
1820 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1821 unsigned long to, unsigned long size, pgprot_t prot)
1823 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1824 return 0;
1826 EXPORT_SYMBOL(remap_pfn_range);
1828 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1829 unsigned long pgoff)
1831 unsigned int size = vma->vm_end - vma->vm_start;
1833 if (!(vma->vm_flags & VM_USERMAP))
1834 return -EINVAL;
1836 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1837 vma->vm_end = vma->vm_start + size;
1839 return 0;
1841 EXPORT_SYMBOL(remap_vmalloc_range);
1843 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1847 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1848 unsigned long len, unsigned long pgoff, unsigned long flags)
1850 return -ENOMEM;
1853 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1857 void unmap_mapping_range(struct address_space *mapping,
1858 loff_t const holebegin, loff_t const holelen,
1859 int even_cows)
1862 EXPORT_SYMBOL(unmap_mapping_range);
1865 * Check that a process has enough memory to allocate a new virtual
1866 * mapping. 0 means there is enough memory for the allocation to
1867 * succeed and -ENOMEM implies there is not.
1869 * We currently support three overcommit policies, which are set via the
1870 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1872 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1873 * Additional code 2002 Jul 20 by Robert Love.
1875 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1877 * Note this is a helper function intended to be used by LSMs which
1878 * wish to use this logic.
1880 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1882 unsigned long free, allowed;
1884 vm_acct_memory(pages);
1887 * Sometimes we want to use more memory than we have
1889 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1890 return 0;
1892 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1893 unsigned long n;
1895 free = global_page_state(NR_FILE_PAGES);
1896 free += nr_swap_pages;
1899 * Any slabs which are created with the
1900 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1901 * which are reclaimable, under pressure. The dentry
1902 * cache and most inode caches should fall into this
1904 free += global_page_state(NR_SLAB_RECLAIMABLE);
1907 * Leave the last 3% for root
1909 if (!cap_sys_admin)
1910 free -= free / 32;
1912 if (free > pages)
1913 return 0;
1916 * nr_free_pages() is very expensive on large systems,
1917 * only call if we're about to fail.
1919 n = nr_free_pages();
1922 * Leave reserved pages. The pages are not for anonymous pages.
1924 if (n <= totalreserve_pages)
1925 goto error;
1926 else
1927 n -= totalreserve_pages;
1930 * Leave the last 3% for root
1932 if (!cap_sys_admin)
1933 n -= n / 32;
1934 free += n;
1936 if (free > pages)
1937 return 0;
1939 goto error;
1942 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1944 * Leave the last 3% for root
1946 if (!cap_sys_admin)
1947 allowed -= allowed / 32;
1948 allowed += total_swap_pages;
1950 /* Don't let a single process grow too big:
1951 leave 3% of the size of this process for other processes */
1952 if (mm)
1953 allowed -= mm->total_vm / 32;
1955 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1956 return 0;
1958 error:
1959 vm_unacct_memory(pages);
1961 return -ENOMEM;
1964 int in_gate_area_no_task(unsigned long addr)
1966 return 0;
1969 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1971 BUG();
1972 return 0;
1974 EXPORT_SYMBOL(filemap_fault);
1977 * Access another process' address space.
1978 * - source/target buffer must be kernel space
1980 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1982 struct vm_area_struct *vma;
1983 struct mm_struct *mm;
1985 if (addr + len < addr)
1986 return 0;
1988 mm = get_task_mm(tsk);
1989 if (!mm)
1990 return 0;
1992 down_read(&mm->mmap_sem);
1994 /* the access must start within one of the target process's mappings */
1995 vma = find_vma(mm, addr);
1996 if (vma) {
1997 /* don't overrun this mapping */
1998 if (addr + len >= vma->vm_end)
1999 len = vma->vm_end - addr;
2001 /* only read or write mappings where it is permitted */
2002 if (write && vma->vm_flags & VM_MAYWRITE)
2003 copy_to_user_page(vma, NULL, addr,
2004 (void *) addr, buf, len);
2005 else if (!write && vma->vm_flags & VM_MAYREAD)
2006 copy_from_user_page(vma, NULL, addr,
2007 buf, (void *) addr, len);
2008 else
2009 len = 0;
2010 } else {
2011 len = 0;
2014 up_read(&mm->mmap_sem);
2015 mmput(mm);
2016 return len;
2020 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2021 * @inode: The inode to check
2022 * @size: The current filesize of the inode
2023 * @newsize: The proposed filesize of the inode
2025 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2026 * make sure that that any outstanding VMAs aren't broken and then shrink the
2027 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2028 * automatically grant mappings that are too large.
2030 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2031 size_t newsize)
2033 struct vm_area_struct *vma;
2034 struct prio_tree_iter iter;
2035 struct vm_region *region;
2036 pgoff_t low, high;
2037 size_t r_size, r_top;
2039 low = newsize >> PAGE_SHIFT;
2040 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2042 down_write(&nommu_region_sem);
2044 /* search for VMAs that fall within the dead zone */
2045 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2046 low, high) {
2047 /* found one - only interested if it's shared out of the page
2048 * cache */
2049 if (vma->vm_flags & VM_SHARED) {
2050 up_write(&nommu_region_sem);
2051 return -ETXTBSY; /* not quite true, but near enough */
2055 /* reduce any regions that overlap the dead zone - if in existence,
2056 * these will be pointed to by VMAs that don't overlap the dead zone
2058 * we don't check for any regions that start beyond the EOF as there
2059 * shouldn't be any
2061 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2062 0, ULONG_MAX) {
2063 if (!(vma->vm_flags & VM_SHARED))
2064 continue;
2066 region = vma->vm_region;
2067 r_size = region->vm_top - region->vm_start;
2068 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2070 if (r_top > newsize) {
2071 region->vm_top -= r_top - newsize;
2072 if (region->vm_end > region->vm_top)
2073 region->vm_end = region->vm_top;
2077 up_write(&nommu_region_sem);
2078 return 0;