coda: kill redundant cast in coda_alloc_inode()
[zen-stable.git] / mm / nommu.c
blob3613517c75929b17e1b3d93a650e5d9957c2315a
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>
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);
333 * vzalloc_node - allocate memory on a specific node with zero fill
334 * @size: allocation size
335 * @node: numa node
337 * Allocate enough pages to cover @size from the page level
338 * allocator and map them into contiguous kernel virtual space.
339 * The memory allocated is set to zero.
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
344 void *vzalloc_node(unsigned long size, int node)
346 return vzalloc(size);
348 EXPORT_SYMBOL(vzalloc_node);
350 #ifndef PAGE_KERNEL_EXEC
351 # define PAGE_KERNEL_EXEC PAGE_KERNEL
352 #endif
355 * vmalloc_exec - allocate virtually contiguous, executable memory
356 * @size: allocation size
358 * Kernel-internal function to allocate enough pages to cover @size
359 * the page level allocator and map them into contiguous and
360 * executable kernel virtual space.
362 * For tight control over page level allocator and protection flags
363 * use __vmalloc() instead.
366 void *vmalloc_exec(unsigned long size)
368 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
372 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
373 * @size: allocation size
375 * Allocate enough 32bit PA addressable pages to cover @size from the
376 * page level allocator and map them into continguos kernel virtual space.
378 void *vmalloc_32(unsigned long size)
380 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
382 EXPORT_SYMBOL(vmalloc_32);
385 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
386 * @size: allocation size
388 * The resulting memory area is 32bit addressable and zeroed so it can be
389 * mapped to userspace without leaking data.
391 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
392 * remap_vmalloc_range() are permissible.
394 void *vmalloc_32_user(unsigned long size)
397 * We'll have to sort out the ZONE_DMA bits for 64-bit,
398 * but for now this can simply use vmalloc_user() directly.
400 return vmalloc_user(size);
402 EXPORT_SYMBOL(vmalloc_32_user);
404 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
406 BUG();
407 return NULL;
409 EXPORT_SYMBOL(vmap);
411 void vunmap(const void *addr)
413 BUG();
415 EXPORT_SYMBOL(vunmap);
417 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
419 BUG();
420 return NULL;
422 EXPORT_SYMBOL(vm_map_ram);
424 void vm_unmap_ram(const void *mem, unsigned int count)
426 BUG();
428 EXPORT_SYMBOL(vm_unmap_ram);
430 void vm_unmap_aliases(void)
433 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
436 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
437 * have one.
439 void __attribute__((weak)) vmalloc_sync_all(void)
443 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
444 struct page *page)
446 return -EINVAL;
448 EXPORT_SYMBOL(vm_insert_page);
451 * sys_brk() for the most part doesn't need the global kernel
452 * lock, except when an application is doing something nasty
453 * like trying to un-brk an area that has already been mapped
454 * to a regular file. in this case, the unmapping will need
455 * to invoke file system routines that need the global lock.
457 SYSCALL_DEFINE1(brk, unsigned long, brk)
459 struct mm_struct *mm = current->mm;
461 if (brk < mm->start_brk || brk > mm->context.end_brk)
462 return mm->brk;
464 if (mm->brk == brk)
465 return mm->brk;
468 * Always allow shrinking brk
470 if (brk <= mm->brk) {
471 mm->brk = brk;
472 return brk;
476 * Ok, looks good - let it rip.
478 flush_icache_range(mm->brk, brk);
479 return mm->brk = brk;
483 * initialise the VMA and region record slabs
485 void __init mmap_init(void)
487 int ret;
489 ret = percpu_counter_init(&vm_committed_as, 0);
490 VM_BUG_ON(ret);
491 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
495 * validate the region tree
496 * - the caller must hold the region lock
498 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
499 static noinline void validate_nommu_regions(void)
501 struct vm_region *region, *last;
502 struct rb_node *p, *lastp;
504 lastp = rb_first(&nommu_region_tree);
505 if (!lastp)
506 return;
508 last = rb_entry(lastp, struct vm_region, vm_rb);
509 BUG_ON(unlikely(last->vm_end <= last->vm_start));
510 BUG_ON(unlikely(last->vm_top < last->vm_end));
512 while ((p = rb_next(lastp))) {
513 region = rb_entry(p, struct vm_region, vm_rb);
514 last = rb_entry(lastp, struct vm_region, vm_rb);
516 BUG_ON(unlikely(region->vm_end <= region->vm_start));
517 BUG_ON(unlikely(region->vm_top < region->vm_end));
518 BUG_ON(unlikely(region->vm_start < last->vm_top));
520 lastp = p;
523 #else
524 static void validate_nommu_regions(void)
527 #endif
530 * add a region into the global tree
532 static void add_nommu_region(struct vm_region *region)
534 struct vm_region *pregion;
535 struct rb_node **p, *parent;
537 validate_nommu_regions();
539 parent = NULL;
540 p = &nommu_region_tree.rb_node;
541 while (*p) {
542 parent = *p;
543 pregion = rb_entry(parent, struct vm_region, vm_rb);
544 if (region->vm_start < pregion->vm_start)
545 p = &(*p)->rb_left;
546 else if (region->vm_start > pregion->vm_start)
547 p = &(*p)->rb_right;
548 else if (pregion == region)
549 return;
550 else
551 BUG();
554 rb_link_node(&region->vm_rb, parent, p);
555 rb_insert_color(&region->vm_rb, &nommu_region_tree);
557 validate_nommu_regions();
561 * delete a region from the global tree
563 static void delete_nommu_region(struct vm_region *region)
565 BUG_ON(!nommu_region_tree.rb_node);
567 validate_nommu_regions();
568 rb_erase(&region->vm_rb, &nommu_region_tree);
569 validate_nommu_regions();
573 * free a contiguous series of pages
575 static void free_page_series(unsigned long from, unsigned long to)
577 for (; from < to; from += PAGE_SIZE) {
578 struct page *page = virt_to_page(from);
580 kdebug("- free %lx", from);
581 atomic_long_dec(&mmap_pages_allocated);
582 if (page_count(page) != 1)
583 kdebug("free page %p: refcount not one: %d",
584 page, page_count(page));
585 put_page(page);
590 * release a reference to a region
591 * - the caller must hold the region semaphore for writing, which this releases
592 * - the region may not have been added to the tree yet, in which case vm_top
593 * will equal vm_start
595 static void __put_nommu_region(struct vm_region *region)
596 __releases(nommu_region_sem)
598 kenter("%p{%d}", region, region->vm_usage);
600 BUG_ON(!nommu_region_tree.rb_node);
602 if (--region->vm_usage == 0) {
603 if (region->vm_top > region->vm_start)
604 delete_nommu_region(region);
605 up_write(&nommu_region_sem);
607 if (region->vm_file)
608 fput(region->vm_file);
610 /* IO memory and memory shared directly out of the pagecache
611 * from ramfs/tmpfs mustn't be released here */
612 if (region->vm_flags & VM_MAPPED_COPY) {
613 kdebug("free series");
614 free_page_series(region->vm_start, region->vm_top);
616 kmem_cache_free(vm_region_jar, region);
617 } else {
618 up_write(&nommu_region_sem);
623 * release a reference to a region
625 static void put_nommu_region(struct vm_region *region)
627 down_write(&nommu_region_sem);
628 __put_nommu_region(region);
632 * update protection on a vma
634 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
636 #ifdef CONFIG_MPU
637 struct mm_struct *mm = vma->vm_mm;
638 long start = vma->vm_start & PAGE_MASK;
639 while (start < vma->vm_end) {
640 protect_page(mm, start, flags);
641 start += PAGE_SIZE;
643 update_protections(mm);
644 #endif
648 * add a VMA into a process's mm_struct in the appropriate place in the list
649 * and tree and add to the address space's page tree also if not an anonymous
650 * page
651 * - should be called with mm->mmap_sem held writelocked
653 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
655 struct vm_area_struct *pvma, **pp, *next;
656 struct address_space *mapping;
657 struct rb_node **p, *parent;
659 kenter(",%p", vma);
661 BUG_ON(!vma->vm_region);
663 mm->map_count++;
664 vma->vm_mm = mm;
666 protect_vma(vma, vma->vm_flags);
668 /* add the VMA to the mapping */
669 if (vma->vm_file) {
670 mapping = vma->vm_file->f_mapping;
672 flush_dcache_mmap_lock(mapping);
673 vma_prio_tree_insert(vma, &mapping->i_mmap);
674 flush_dcache_mmap_unlock(mapping);
677 /* add the VMA to the tree */
678 parent = NULL;
679 p = &mm->mm_rb.rb_node;
680 while (*p) {
681 parent = *p;
682 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
684 /* sort by: start addr, end addr, VMA struct addr in that order
685 * (the latter is necessary as we may get identical VMAs) */
686 if (vma->vm_start < pvma->vm_start)
687 p = &(*p)->rb_left;
688 else if (vma->vm_start > pvma->vm_start)
689 p = &(*p)->rb_right;
690 else if (vma->vm_end < pvma->vm_end)
691 p = &(*p)->rb_left;
692 else if (vma->vm_end > pvma->vm_end)
693 p = &(*p)->rb_right;
694 else if (vma < pvma)
695 p = &(*p)->rb_left;
696 else if (vma > pvma)
697 p = &(*p)->rb_right;
698 else
699 BUG();
702 rb_link_node(&vma->vm_rb, parent, p);
703 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
705 /* add VMA to the VMA list also */
706 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
707 if (pvma->vm_start > vma->vm_start)
708 break;
709 if (pvma->vm_start < vma->vm_start)
710 continue;
711 if (pvma->vm_end < vma->vm_end)
712 break;
715 next = *pp;
716 *pp = vma;
717 vma->vm_next = next;
718 if (next)
719 next->vm_prev = vma;
723 * delete a VMA from its owning mm_struct and address space
725 static void delete_vma_from_mm(struct vm_area_struct *vma)
727 struct vm_area_struct **pp;
728 struct address_space *mapping;
729 struct mm_struct *mm = vma->vm_mm;
731 kenter("%p", vma);
733 protect_vma(vma, 0);
735 mm->map_count--;
736 if (mm->mmap_cache == vma)
737 mm->mmap_cache = NULL;
739 /* remove the VMA from the mapping */
740 if (vma->vm_file) {
741 mapping = vma->vm_file->f_mapping;
743 flush_dcache_mmap_lock(mapping);
744 vma_prio_tree_remove(vma, &mapping->i_mmap);
745 flush_dcache_mmap_unlock(mapping);
748 /* remove from the MM's tree and list */
749 rb_erase(&vma->vm_rb, &mm->mm_rb);
750 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
751 if (*pp == vma) {
752 *pp = vma->vm_next;
753 break;
757 vma->vm_mm = NULL;
761 * destroy a VMA record
763 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
765 kenter("%p", vma);
766 if (vma->vm_ops && vma->vm_ops->close)
767 vma->vm_ops->close(vma);
768 if (vma->vm_file) {
769 fput(vma->vm_file);
770 if (vma->vm_flags & VM_EXECUTABLE)
771 removed_exe_file_vma(mm);
773 put_nommu_region(vma->vm_region);
774 kmem_cache_free(vm_area_cachep, vma);
778 * look up the first VMA in which addr resides, NULL if none
779 * - should be called with mm->mmap_sem at least held readlocked
781 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
783 struct vm_area_struct *vma;
784 struct rb_node *n = mm->mm_rb.rb_node;
786 /* check the cache first */
787 vma = mm->mmap_cache;
788 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
789 return vma;
791 /* trawl the tree (there may be multiple mappings in which addr
792 * resides) */
793 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
794 vma = rb_entry(n, struct vm_area_struct, vm_rb);
795 if (vma->vm_start > addr)
796 return NULL;
797 if (vma->vm_end > addr) {
798 mm->mmap_cache = vma;
799 return vma;
803 return NULL;
805 EXPORT_SYMBOL(find_vma);
808 * find a VMA
809 * - we don't extend stack VMAs under NOMMU conditions
811 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
813 return find_vma(mm, addr);
817 * expand a stack to a given address
818 * - not supported under NOMMU conditions
820 int expand_stack(struct vm_area_struct *vma, unsigned long address)
822 return -ENOMEM;
826 * look up the first VMA exactly that exactly matches addr
827 * - should be called with mm->mmap_sem at least held readlocked
829 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
830 unsigned long addr,
831 unsigned long len)
833 struct vm_area_struct *vma;
834 struct rb_node *n = mm->mm_rb.rb_node;
835 unsigned long end = addr + len;
837 /* check the cache first */
838 vma = mm->mmap_cache;
839 if (vma && vma->vm_start == addr && vma->vm_end == end)
840 return vma;
842 /* trawl the tree (there may be multiple mappings in which addr
843 * resides) */
844 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
845 vma = rb_entry(n, struct vm_area_struct, vm_rb);
846 if (vma->vm_start < addr)
847 continue;
848 if (vma->vm_start > addr)
849 return NULL;
850 if (vma->vm_end == end) {
851 mm->mmap_cache = vma;
852 return vma;
856 return NULL;
860 * determine whether a mapping should be permitted and, if so, what sort of
861 * mapping we're capable of supporting
863 static int validate_mmap_request(struct file *file,
864 unsigned long addr,
865 unsigned long len,
866 unsigned long prot,
867 unsigned long flags,
868 unsigned long pgoff,
869 unsigned long *_capabilities)
871 unsigned long capabilities, rlen;
872 unsigned long reqprot = prot;
873 int ret;
875 /* do the simple checks first */
876 if (flags & MAP_FIXED) {
877 printk(KERN_DEBUG
878 "%d: Can't do fixed-address/overlay mmap of RAM\n",
879 current->pid);
880 return -EINVAL;
883 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
884 (flags & MAP_TYPE) != MAP_SHARED)
885 return -EINVAL;
887 if (!len)
888 return -EINVAL;
890 /* Careful about overflows.. */
891 rlen = PAGE_ALIGN(len);
892 if (!rlen || rlen > TASK_SIZE)
893 return -ENOMEM;
895 /* offset overflow? */
896 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
897 return -EOVERFLOW;
899 if (file) {
900 /* validate file mapping requests */
901 struct address_space *mapping;
903 /* files must support mmap */
904 if (!file->f_op || !file->f_op->mmap)
905 return -ENODEV;
907 /* work out if what we've got could possibly be shared
908 * - we support chardevs that provide their own "memory"
909 * - we support files/blockdevs that are memory backed
911 mapping = file->f_mapping;
912 if (!mapping)
913 mapping = file->f_path.dentry->d_inode->i_mapping;
915 capabilities = 0;
916 if (mapping && mapping->backing_dev_info)
917 capabilities = mapping->backing_dev_info->capabilities;
919 if (!capabilities) {
920 /* no explicit capabilities set, so assume some
921 * defaults */
922 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
923 case S_IFREG:
924 case S_IFBLK:
925 capabilities = BDI_CAP_MAP_COPY;
926 break;
928 case S_IFCHR:
929 capabilities =
930 BDI_CAP_MAP_DIRECT |
931 BDI_CAP_READ_MAP |
932 BDI_CAP_WRITE_MAP;
933 break;
935 default:
936 return -EINVAL;
940 /* eliminate any capabilities that we can't support on this
941 * device */
942 if (!file->f_op->get_unmapped_area)
943 capabilities &= ~BDI_CAP_MAP_DIRECT;
944 if (!file->f_op->read)
945 capabilities &= ~BDI_CAP_MAP_COPY;
947 /* The file shall have been opened with read permission. */
948 if (!(file->f_mode & FMODE_READ))
949 return -EACCES;
951 if (flags & MAP_SHARED) {
952 /* do checks for writing, appending and locking */
953 if ((prot & PROT_WRITE) &&
954 !(file->f_mode & FMODE_WRITE))
955 return -EACCES;
957 if (IS_APPEND(file->f_path.dentry->d_inode) &&
958 (file->f_mode & FMODE_WRITE))
959 return -EACCES;
961 if (locks_verify_locked(file->f_path.dentry->d_inode))
962 return -EAGAIN;
964 if (!(capabilities & BDI_CAP_MAP_DIRECT))
965 return -ENODEV;
967 /* we mustn't privatise shared mappings */
968 capabilities &= ~BDI_CAP_MAP_COPY;
970 else {
971 /* we're going to read the file into private memory we
972 * allocate */
973 if (!(capabilities & BDI_CAP_MAP_COPY))
974 return -ENODEV;
976 /* we don't permit a private writable mapping to be
977 * shared with the backing device */
978 if (prot & PROT_WRITE)
979 capabilities &= ~BDI_CAP_MAP_DIRECT;
982 if (capabilities & BDI_CAP_MAP_DIRECT) {
983 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
984 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
985 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
987 capabilities &= ~BDI_CAP_MAP_DIRECT;
988 if (flags & MAP_SHARED) {
989 printk(KERN_WARNING
990 "MAP_SHARED not completely supported on !MMU\n");
991 return -EINVAL;
996 /* handle executable mappings and implied executable
997 * mappings */
998 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
999 if (prot & PROT_EXEC)
1000 return -EPERM;
1002 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1003 /* handle implication of PROT_EXEC by PROT_READ */
1004 if (current->personality & READ_IMPLIES_EXEC) {
1005 if (capabilities & BDI_CAP_EXEC_MAP)
1006 prot |= PROT_EXEC;
1009 else if ((prot & PROT_READ) &&
1010 (prot & PROT_EXEC) &&
1011 !(capabilities & BDI_CAP_EXEC_MAP)
1013 /* backing file is not executable, try to copy */
1014 capabilities &= ~BDI_CAP_MAP_DIRECT;
1017 else {
1018 /* anonymous mappings are always memory backed and can be
1019 * privately mapped
1021 capabilities = BDI_CAP_MAP_COPY;
1023 /* handle PROT_EXEC implication by PROT_READ */
1024 if ((prot & PROT_READ) &&
1025 (current->personality & READ_IMPLIES_EXEC))
1026 prot |= PROT_EXEC;
1029 /* allow the security API to have its say */
1030 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1031 if (ret < 0)
1032 return ret;
1034 /* looks okay */
1035 *_capabilities = capabilities;
1036 return 0;
1040 * we've determined that we can make the mapping, now translate what we
1041 * now know into VMA flags
1043 static unsigned long determine_vm_flags(struct file *file,
1044 unsigned long prot,
1045 unsigned long flags,
1046 unsigned long capabilities)
1048 unsigned long vm_flags;
1050 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1051 /* vm_flags |= mm->def_flags; */
1053 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1054 /* attempt to share read-only copies of mapped file chunks */
1055 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1056 if (file && !(prot & PROT_WRITE))
1057 vm_flags |= VM_MAYSHARE;
1058 } else {
1059 /* overlay a shareable mapping on the backing device or inode
1060 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1061 * romfs/cramfs */
1062 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1063 if (flags & MAP_SHARED)
1064 vm_flags |= VM_SHARED;
1067 /* refuse to let anyone share private mappings with this process if
1068 * it's being traced - otherwise breakpoints set in it may interfere
1069 * with another untraced process
1071 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
1072 vm_flags &= ~VM_MAYSHARE;
1074 return vm_flags;
1078 * set up a shared mapping on a file (the driver or filesystem provides and
1079 * pins the storage)
1081 static int do_mmap_shared_file(struct vm_area_struct *vma)
1083 int ret;
1085 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1086 if (ret == 0) {
1087 vma->vm_region->vm_top = vma->vm_region->vm_end;
1088 return 0;
1090 if (ret != -ENOSYS)
1091 return ret;
1093 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1094 * opposed to tried but failed) so we can only give a suitable error as
1095 * it's not possible to make a private copy if MAP_SHARED was given */
1096 return -ENODEV;
1100 * set up a private mapping or an anonymous shared mapping
1102 static int do_mmap_private(struct vm_area_struct *vma,
1103 struct vm_region *region,
1104 unsigned long len,
1105 unsigned long capabilities)
1107 struct page *pages;
1108 unsigned long total, point, n, rlen;
1109 void *base;
1110 int ret, order;
1112 /* invoke the file's mapping function so that it can keep track of
1113 * shared mappings on devices or memory
1114 * - VM_MAYSHARE will be set if it may attempt to share
1116 if (capabilities & BDI_CAP_MAP_DIRECT) {
1117 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1118 if (ret == 0) {
1119 /* shouldn't return success if we're not sharing */
1120 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1121 vma->vm_region->vm_top = vma->vm_region->vm_end;
1122 return 0;
1124 if (ret != -ENOSYS)
1125 return ret;
1127 /* getting an ENOSYS error indicates that direct mmap isn't
1128 * possible (as opposed to tried but failed) so we'll try to
1129 * make a private copy of the data and map that instead */
1132 rlen = PAGE_ALIGN(len);
1134 /* allocate some memory to hold the mapping
1135 * - note that this may not return a page-aligned address if the object
1136 * we're allocating is smaller than a page
1138 order = get_order(rlen);
1139 kdebug("alloc order %d for %lx", order, len);
1141 pages = alloc_pages(GFP_KERNEL, order);
1142 if (!pages)
1143 goto enomem;
1145 total = 1 << order;
1146 atomic_long_add(total, &mmap_pages_allocated);
1148 point = rlen >> PAGE_SHIFT;
1150 /* we allocated a power-of-2 sized page set, so we may want to trim off
1151 * the excess */
1152 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1153 while (total > point) {
1154 order = ilog2(total - point);
1155 n = 1 << order;
1156 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1157 atomic_long_sub(n, &mmap_pages_allocated);
1158 total -= n;
1159 set_page_refcounted(pages + total);
1160 __free_pages(pages + total, order);
1164 for (point = 1; point < total; point++)
1165 set_page_refcounted(&pages[point]);
1167 base = page_address(pages);
1168 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1169 region->vm_start = (unsigned long) base;
1170 region->vm_end = region->vm_start + rlen;
1171 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1173 vma->vm_start = region->vm_start;
1174 vma->vm_end = region->vm_start + len;
1176 if (vma->vm_file) {
1177 /* read the contents of a file into the copy */
1178 mm_segment_t old_fs;
1179 loff_t fpos;
1181 fpos = vma->vm_pgoff;
1182 fpos <<= PAGE_SHIFT;
1184 old_fs = get_fs();
1185 set_fs(KERNEL_DS);
1186 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
1187 set_fs(old_fs);
1189 if (ret < 0)
1190 goto error_free;
1192 /* clear the last little bit */
1193 if (ret < rlen)
1194 memset(base + ret, 0, rlen - ret);
1198 return 0;
1200 error_free:
1201 free_page_series(region->vm_start, region->vm_end);
1202 region->vm_start = vma->vm_start = 0;
1203 region->vm_end = vma->vm_end = 0;
1204 region->vm_top = 0;
1205 return ret;
1207 enomem:
1208 printk("Allocation of length %lu from process %d (%s) failed\n",
1209 len, current->pid, current->comm);
1210 show_free_areas();
1211 return -ENOMEM;
1215 * handle mapping creation for uClinux
1217 unsigned long do_mmap_pgoff(struct file *file,
1218 unsigned long addr,
1219 unsigned long len,
1220 unsigned long prot,
1221 unsigned long flags,
1222 unsigned long pgoff)
1224 struct vm_area_struct *vma;
1225 struct vm_region *region;
1226 struct rb_node *rb;
1227 unsigned long capabilities, vm_flags, result;
1228 int ret;
1230 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1232 /* decide whether we should attempt the mapping, and if so what sort of
1233 * mapping */
1234 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1235 &capabilities);
1236 if (ret < 0) {
1237 kleave(" = %d [val]", ret);
1238 return ret;
1241 /* we ignore the address hint */
1242 addr = 0;
1244 /* we've determined that we can make the mapping, now translate what we
1245 * now know into VMA flags */
1246 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1248 /* we're going to need to record the mapping */
1249 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1250 if (!region)
1251 goto error_getting_region;
1253 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1254 if (!vma)
1255 goto error_getting_vma;
1257 region->vm_usage = 1;
1258 region->vm_flags = vm_flags;
1259 region->vm_pgoff = pgoff;
1261 INIT_LIST_HEAD(&vma->anon_vma_chain);
1262 vma->vm_flags = vm_flags;
1263 vma->vm_pgoff = pgoff;
1265 if (file) {
1266 region->vm_file = file;
1267 get_file(file);
1268 vma->vm_file = file;
1269 get_file(file);
1270 if (vm_flags & VM_EXECUTABLE) {
1271 added_exe_file_vma(current->mm);
1272 vma->vm_mm = current->mm;
1276 down_write(&nommu_region_sem);
1278 /* if we want to share, we need to check for regions created by other
1279 * mmap() calls that overlap with our proposed mapping
1280 * - we can only share with a superset match on most regular files
1281 * - shared mappings on character devices and memory backed files are
1282 * permitted to overlap inexactly as far as we are concerned for in
1283 * these cases, sharing is handled in the driver or filesystem rather
1284 * than here
1286 if (vm_flags & VM_MAYSHARE) {
1287 struct vm_region *pregion;
1288 unsigned long pglen, rpglen, pgend, rpgend, start;
1290 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1291 pgend = pgoff + pglen;
1293 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1294 pregion = rb_entry(rb, struct vm_region, vm_rb);
1296 if (!(pregion->vm_flags & VM_MAYSHARE))
1297 continue;
1299 /* search for overlapping mappings on the same file */
1300 if (pregion->vm_file->f_path.dentry->d_inode !=
1301 file->f_path.dentry->d_inode)
1302 continue;
1304 if (pregion->vm_pgoff >= pgend)
1305 continue;
1307 rpglen = pregion->vm_end - pregion->vm_start;
1308 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1309 rpgend = pregion->vm_pgoff + rpglen;
1310 if (pgoff >= rpgend)
1311 continue;
1313 /* handle inexactly overlapping matches between
1314 * mappings */
1315 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1316 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1317 /* new mapping is not a subset of the region */
1318 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1319 goto sharing_violation;
1320 continue;
1323 /* we've found a region we can share */
1324 pregion->vm_usage++;
1325 vma->vm_region = pregion;
1326 start = pregion->vm_start;
1327 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1328 vma->vm_start = start;
1329 vma->vm_end = start + len;
1331 if (pregion->vm_flags & VM_MAPPED_COPY) {
1332 kdebug("share copy");
1333 vma->vm_flags |= VM_MAPPED_COPY;
1334 } else {
1335 kdebug("share mmap");
1336 ret = do_mmap_shared_file(vma);
1337 if (ret < 0) {
1338 vma->vm_region = NULL;
1339 vma->vm_start = 0;
1340 vma->vm_end = 0;
1341 pregion->vm_usage--;
1342 pregion = NULL;
1343 goto error_just_free;
1346 fput(region->vm_file);
1347 kmem_cache_free(vm_region_jar, region);
1348 region = pregion;
1349 result = start;
1350 goto share;
1353 /* obtain the address at which to make a shared mapping
1354 * - this is the hook for quasi-memory character devices to
1355 * tell us the location of a shared mapping
1357 if (capabilities & BDI_CAP_MAP_DIRECT) {
1358 addr = file->f_op->get_unmapped_area(file, addr, len,
1359 pgoff, flags);
1360 if (IS_ERR((void *) addr)) {
1361 ret = addr;
1362 if (ret != (unsigned long) -ENOSYS)
1363 goto error_just_free;
1365 /* the driver refused to tell us where to site
1366 * the mapping so we'll have to attempt to copy
1367 * it */
1368 ret = (unsigned long) -ENODEV;
1369 if (!(capabilities & BDI_CAP_MAP_COPY))
1370 goto error_just_free;
1372 capabilities &= ~BDI_CAP_MAP_DIRECT;
1373 } else {
1374 vma->vm_start = region->vm_start = addr;
1375 vma->vm_end = region->vm_end = addr + len;
1380 vma->vm_region = region;
1382 /* set up the mapping
1383 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1385 if (file && vma->vm_flags & VM_SHARED)
1386 ret = do_mmap_shared_file(vma);
1387 else
1388 ret = do_mmap_private(vma, region, len, capabilities);
1389 if (ret < 0)
1390 goto error_just_free;
1391 add_nommu_region(region);
1393 /* clear anonymous mappings that don't ask for uninitialized data */
1394 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1395 memset((void *)region->vm_start, 0,
1396 region->vm_end - region->vm_start);
1398 /* okay... we have a mapping; now we have to register it */
1399 result = vma->vm_start;
1401 current->mm->total_vm += len >> PAGE_SHIFT;
1403 share:
1404 add_vma_to_mm(current->mm, vma);
1406 /* we flush the region from the icache only when the first executable
1407 * mapping of it is made */
1408 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1409 flush_icache_range(region->vm_start, region->vm_end);
1410 region->vm_icache_flushed = true;
1413 up_write(&nommu_region_sem);
1415 kleave(" = %lx", result);
1416 return result;
1418 error_just_free:
1419 up_write(&nommu_region_sem);
1420 error:
1421 if (region->vm_file)
1422 fput(region->vm_file);
1423 kmem_cache_free(vm_region_jar, region);
1424 if (vma->vm_file)
1425 fput(vma->vm_file);
1426 if (vma->vm_flags & VM_EXECUTABLE)
1427 removed_exe_file_vma(vma->vm_mm);
1428 kmem_cache_free(vm_area_cachep, vma);
1429 kleave(" = %d", ret);
1430 return ret;
1432 sharing_violation:
1433 up_write(&nommu_region_sem);
1434 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1435 ret = -EINVAL;
1436 goto error;
1438 error_getting_vma:
1439 kmem_cache_free(vm_region_jar, region);
1440 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1441 " from process %d failed\n",
1442 len, current->pid);
1443 show_free_areas();
1444 return -ENOMEM;
1446 error_getting_region:
1447 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1448 " from process %d failed\n",
1449 len, current->pid);
1450 show_free_areas();
1451 return -ENOMEM;
1453 EXPORT_SYMBOL(do_mmap_pgoff);
1455 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1456 unsigned long, prot, unsigned long, flags,
1457 unsigned long, fd, unsigned long, pgoff)
1459 struct file *file = NULL;
1460 unsigned long retval = -EBADF;
1462 audit_mmap_fd(fd, flags);
1463 if (!(flags & MAP_ANONYMOUS)) {
1464 file = fget(fd);
1465 if (!file)
1466 goto out;
1469 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1471 down_write(&current->mm->mmap_sem);
1472 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1473 up_write(&current->mm->mmap_sem);
1475 if (file)
1476 fput(file);
1477 out:
1478 return retval;
1481 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1482 struct mmap_arg_struct {
1483 unsigned long addr;
1484 unsigned long len;
1485 unsigned long prot;
1486 unsigned long flags;
1487 unsigned long fd;
1488 unsigned long offset;
1491 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1493 struct mmap_arg_struct a;
1495 if (copy_from_user(&a, arg, sizeof(a)))
1496 return -EFAULT;
1497 if (a.offset & ~PAGE_MASK)
1498 return -EINVAL;
1500 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1501 a.offset >> PAGE_SHIFT);
1503 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1506 * split a vma into two pieces at address 'addr', a new vma is allocated either
1507 * for the first part or the tail.
1509 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1510 unsigned long addr, int new_below)
1512 struct vm_area_struct *new;
1513 struct vm_region *region;
1514 unsigned long npages;
1516 kenter("");
1518 /* we're only permitted to split anonymous regions (these should have
1519 * only a single usage on the region) */
1520 if (vma->vm_file)
1521 return -ENOMEM;
1523 if (mm->map_count >= sysctl_max_map_count)
1524 return -ENOMEM;
1526 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1527 if (!region)
1528 return -ENOMEM;
1530 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1531 if (!new) {
1532 kmem_cache_free(vm_region_jar, region);
1533 return -ENOMEM;
1536 /* most fields are the same, copy all, and then fixup */
1537 *new = *vma;
1538 *region = *vma->vm_region;
1539 new->vm_region = region;
1541 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1543 if (new_below) {
1544 region->vm_top = region->vm_end = new->vm_end = addr;
1545 } else {
1546 region->vm_start = new->vm_start = addr;
1547 region->vm_pgoff = new->vm_pgoff += npages;
1550 if (new->vm_ops && new->vm_ops->open)
1551 new->vm_ops->open(new);
1553 delete_vma_from_mm(vma);
1554 down_write(&nommu_region_sem);
1555 delete_nommu_region(vma->vm_region);
1556 if (new_below) {
1557 vma->vm_region->vm_start = vma->vm_start = addr;
1558 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1559 } else {
1560 vma->vm_region->vm_end = vma->vm_end = addr;
1561 vma->vm_region->vm_top = addr;
1563 add_nommu_region(vma->vm_region);
1564 add_nommu_region(new->vm_region);
1565 up_write(&nommu_region_sem);
1566 add_vma_to_mm(mm, vma);
1567 add_vma_to_mm(mm, new);
1568 return 0;
1572 * shrink a VMA by removing the specified chunk from either the beginning or
1573 * the end
1575 static int shrink_vma(struct mm_struct *mm,
1576 struct vm_area_struct *vma,
1577 unsigned long from, unsigned long to)
1579 struct vm_region *region;
1581 kenter("");
1583 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1584 * and list */
1585 delete_vma_from_mm(vma);
1586 if (from > vma->vm_start)
1587 vma->vm_end = from;
1588 else
1589 vma->vm_start = to;
1590 add_vma_to_mm(mm, vma);
1592 /* cut the backing region down to size */
1593 region = vma->vm_region;
1594 BUG_ON(region->vm_usage != 1);
1596 down_write(&nommu_region_sem);
1597 delete_nommu_region(region);
1598 if (from > region->vm_start) {
1599 to = region->vm_top;
1600 region->vm_top = region->vm_end = from;
1601 } else {
1602 region->vm_start = to;
1604 add_nommu_region(region);
1605 up_write(&nommu_region_sem);
1607 free_page_series(from, to);
1608 return 0;
1612 * release a mapping
1613 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1614 * VMA, though it need not cover the whole VMA
1616 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1618 struct vm_area_struct *vma;
1619 struct rb_node *rb;
1620 unsigned long end = start + len;
1621 int ret;
1623 kenter(",%lx,%zx", start, len);
1625 if (len == 0)
1626 return -EINVAL;
1628 /* find the first potentially overlapping VMA */
1629 vma = find_vma(mm, start);
1630 if (!vma) {
1631 static int limit = 0;
1632 if (limit < 5) {
1633 printk(KERN_WARNING
1634 "munmap of memory not mmapped by process %d"
1635 " (%s): 0x%lx-0x%lx\n",
1636 current->pid, current->comm,
1637 start, start + len - 1);
1638 limit++;
1640 return -EINVAL;
1643 /* we're allowed to split an anonymous VMA but not a file-backed one */
1644 if (vma->vm_file) {
1645 do {
1646 if (start > vma->vm_start) {
1647 kleave(" = -EINVAL [miss]");
1648 return -EINVAL;
1650 if (end == vma->vm_end)
1651 goto erase_whole_vma;
1652 rb = rb_next(&vma->vm_rb);
1653 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1654 } while (rb);
1655 kleave(" = -EINVAL [split file]");
1656 return -EINVAL;
1657 } else {
1658 /* the chunk must be a subset of the VMA found */
1659 if (start == vma->vm_start && end == vma->vm_end)
1660 goto erase_whole_vma;
1661 if (start < vma->vm_start || end > vma->vm_end) {
1662 kleave(" = -EINVAL [superset]");
1663 return -EINVAL;
1665 if (start & ~PAGE_MASK) {
1666 kleave(" = -EINVAL [unaligned start]");
1667 return -EINVAL;
1669 if (end != vma->vm_end && end & ~PAGE_MASK) {
1670 kleave(" = -EINVAL [unaligned split]");
1671 return -EINVAL;
1673 if (start != vma->vm_start && end != vma->vm_end) {
1674 ret = split_vma(mm, vma, start, 1);
1675 if (ret < 0) {
1676 kleave(" = %d [split]", ret);
1677 return ret;
1680 return shrink_vma(mm, vma, start, end);
1683 erase_whole_vma:
1684 delete_vma_from_mm(vma);
1685 delete_vma(mm, vma);
1686 kleave(" = 0");
1687 return 0;
1689 EXPORT_SYMBOL(do_munmap);
1691 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1693 int ret;
1694 struct mm_struct *mm = current->mm;
1696 down_write(&mm->mmap_sem);
1697 ret = do_munmap(mm, addr, len);
1698 up_write(&mm->mmap_sem);
1699 return ret;
1703 * release all the mappings made in a process's VM space
1705 void exit_mmap(struct mm_struct *mm)
1707 struct vm_area_struct *vma;
1709 if (!mm)
1710 return;
1712 kenter("");
1714 mm->total_vm = 0;
1716 while ((vma = mm->mmap)) {
1717 mm->mmap = vma->vm_next;
1718 delete_vma_from_mm(vma);
1719 delete_vma(mm, vma);
1722 kleave("");
1725 unsigned long do_brk(unsigned long addr, unsigned long len)
1727 return -ENOMEM;
1731 * expand (or shrink) an existing mapping, potentially moving it at the same
1732 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1734 * under NOMMU conditions, we only permit changing a mapping's size, and only
1735 * as long as it stays within the region allocated by do_mmap_private() and the
1736 * block is not shareable
1738 * MREMAP_FIXED is not supported under NOMMU conditions
1740 unsigned long do_mremap(unsigned long addr,
1741 unsigned long old_len, unsigned long new_len,
1742 unsigned long flags, unsigned long new_addr)
1744 struct vm_area_struct *vma;
1746 /* insanity checks first */
1747 if (old_len == 0 || new_len == 0)
1748 return (unsigned long) -EINVAL;
1750 if (addr & ~PAGE_MASK)
1751 return -EINVAL;
1753 if (flags & MREMAP_FIXED && new_addr != addr)
1754 return (unsigned long) -EINVAL;
1756 vma = find_vma_exact(current->mm, addr, old_len);
1757 if (!vma)
1758 return (unsigned long) -EINVAL;
1760 if (vma->vm_end != vma->vm_start + old_len)
1761 return (unsigned long) -EFAULT;
1763 if (vma->vm_flags & VM_MAYSHARE)
1764 return (unsigned long) -EPERM;
1766 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1767 return (unsigned long) -ENOMEM;
1769 /* all checks complete - do it */
1770 vma->vm_end = vma->vm_start + new_len;
1771 return vma->vm_start;
1773 EXPORT_SYMBOL(do_mremap);
1775 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1776 unsigned long, new_len, unsigned long, flags,
1777 unsigned long, new_addr)
1779 unsigned long ret;
1781 down_write(&current->mm->mmap_sem);
1782 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1783 up_write(&current->mm->mmap_sem);
1784 return ret;
1787 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1788 unsigned int foll_flags)
1790 return NULL;
1793 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1794 unsigned long to, unsigned long size, pgprot_t prot)
1796 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1797 return 0;
1799 EXPORT_SYMBOL(remap_pfn_range);
1801 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1802 unsigned long pgoff)
1804 unsigned int size = vma->vm_end - vma->vm_start;
1806 if (!(vma->vm_flags & VM_USERMAP))
1807 return -EINVAL;
1809 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1810 vma->vm_end = vma->vm_start + size;
1812 return 0;
1814 EXPORT_SYMBOL(remap_vmalloc_range);
1816 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1820 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1821 unsigned long len, unsigned long pgoff, unsigned long flags)
1823 return -ENOMEM;
1826 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1830 void unmap_mapping_range(struct address_space *mapping,
1831 loff_t const holebegin, loff_t const holelen,
1832 int even_cows)
1835 EXPORT_SYMBOL(unmap_mapping_range);
1838 * Check that a process has enough memory to allocate a new virtual
1839 * mapping. 0 means there is enough memory for the allocation to
1840 * succeed and -ENOMEM implies there is not.
1842 * We currently support three overcommit policies, which are set via the
1843 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1845 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1846 * Additional code 2002 Jul 20 by Robert Love.
1848 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1850 * Note this is a helper function intended to be used by LSMs which
1851 * wish to use this logic.
1853 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1855 unsigned long free, allowed;
1857 vm_acct_memory(pages);
1860 * Sometimes we want to use more memory than we have
1862 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1863 return 0;
1865 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1866 unsigned long n;
1868 free = global_page_state(NR_FILE_PAGES);
1869 free += nr_swap_pages;
1872 * Any slabs which are created with the
1873 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1874 * which are reclaimable, under pressure. The dentry
1875 * cache and most inode caches should fall into this
1877 free += global_page_state(NR_SLAB_RECLAIMABLE);
1880 * Leave the last 3% for root
1882 if (!cap_sys_admin)
1883 free -= free / 32;
1885 if (free > pages)
1886 return 0;
1889 * nr_free_pages() is very expensive on large systems,
1890 * only call if we're about to fail.
1892 n = nr_free_pages();
1895 * Leave reserved pages. The pages are not for anonymous pages.
1897 if (n <= totalreserve_pages)
1898 goto error;
1899 else
1900 n -= totalreserve_pages;
1903 * Leave the last 3% for root
1905 if (!cap_sys_admin)
1906 n -= n / 32;
1907 free += n;
1909 if (free > pages)
1910 return 0;
1912 goto error;
1915 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1917 * Leave the last 3% for root
1919 if (!cap_sys_admin)
1920 allowed -= allowed / 32;
1921 allowed += total_swap_pages;
1923 /* Don't let a single process grow too big:
1924 leave 3% of the size of this process for other processes */
1925 if (mm)
1926 allowed -= mm->total_vm / 32;
1928 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1929 return 0;
1931 error:
1932 vm_unacct_memory(pages);
1934 return -ENOMEM;
1937 int in_gate_area_no_task(unsigned long addr)
1939 return 0;
1942 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1944 BUG();
1945 return 0;
1947 EXPORT_SYMBOL(filemap_fault);
1950 * Access another process' address space.
1951 * - source/target buffer must be kernel space
1953 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1955 struct vm_area_struct *vma;
1956 struct mm_struct *mm;
1958 if (addr + len < addr)
1959 return 0;
1961 mm = get_task_mm(tsk);
1962 if (!mm)
1963 return 0;
1965 down_read(&mm->mmap_sem);
1967 /* the access must start within one of the target process's mappings */
1968 vma = find_vma(mm, addr);
1969 if (vma) {
1970 /* don't overrun this mapping */
1971 if (addr + len >= vma->vm_end)
1972 len = vma->vm_end - addr;
1974 /* only read or write mappings where it is permitted */
1975 if (write && vma->vm_flags & VM_MAYWRITE)
1976 copy_to_user_page(vma, NULL, addr,
1977 (void *) addr, buf, len);
1978 else if (!write && vma->vm_flags & VM_MAYREAD)
1979 copy_from_user_page(vma, NULL, addr,
1980 buf, (void *) addr, len);
1981 else
1982 len = 0;
1983 } else {
1984 len = 0;
1987 up_read(&mm->mmap_sem);
1988 mmput(mm);
1989 return len;
1993 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1994 * @inode: The inode to check
1995 * @size: The current filesize of the inode
1996 * @newsize: The proposed filesize of the inode
1998 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1999 * make sure that that any outstanding VMAs aren't broken and then shrink the
2000 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2001 * automatically grant mappings that are too large.
2003 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2004 size_t newsize)
2006 struct vm_area_struct *vma;
2007 struct prio_tree_iter iter;
2008 struct vm_region *region;
2009 pgoff_t low, high;
2010 size_t r_size, r_top;
2012 low = newsize >> PAGE_SHIFT;
2013 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2015 down_write(&nommu_region_sem);
2017 /* search for VMAs that fall within the dead zone */
2018 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2019 low, high) {
2020 /* found one - only interested if it's shared out of the page
2021 * cache */
2022 if (vma->vm_flags & VM_SHARED) {
2023 up_write(&nommu_region_sem);
2024 return -ETXTBSY; /* not quite true, but near enough */
2028 /* reduce any regions that overlap the dead zone - if in existence,
2029 * these will be pointed to by VMAs that don't overlap the dead zone
2031 * we don't check for any regions that start beyond the EOF as there
2032 * shouldn't be any
2034 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2035 0, ULONG_MAX) {
2036 if (!(vma->vm_flags & VM_SHARED))
2037 continue;
2039 region = vma->vm_region;
2040 r_size = region->vm_top - region->vm_start;
2041 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2043 if (r_top > newsize) {
2044 region->vm_top -= r_top - newsize;
2045 if (region->vm_end > region->vm_top)
2046 region->vm_end = region->vm_top;
2050 up_write(&nommu_region_sem);
2051 return 0;