mm: make do_mmap_pgoff return populate as a size in bytes, not as a bool
[linux-2.6.git] / mm / nommu.c
blob18c1b932e2c4450de1aa28d8e250420ced267b47
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/export.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/blkdev.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mount.h>
28 #include <linux/personality.h>
29 #include <linux/security.h>
30 #include <linux/syscalls.h>
31 #include <linux/audit.h>
32 #include <linux/sched/sysctl.h>
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;
71 * The global memory commitment made in the system can be a metric
72 * that can be used to drive ballooning decisions when Linux is hosted
73 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
74 * balancing memory across competing virtual machines that are hosted.
75 * Several metrics drive this policy engine including the guest reported
76 * memory commitment.
78 unsigned long vm_memory_committed(void)
80 return percpu_counter_read_positive(&vm_committed_as);
83 EXPORT_SYMBOL_GPL(vm_memory_committed);
85 EXPORT_SYMBOL(mem_map);
86 EXPORT_SYMBOL(num_physpages);
88 /* list of mapped, potentially shareable regions */
89 static struct kmem_cache *vm_region_jar;
90 struct rb_root nommu_region_tree = RB_ROOT;
91 DECLARE_RWSEM(nommu_region_sem);
93 const struct vm_operations_struct generic_file_vm_ops = {
97 * Return the total memory allocated for this pointer, not
98 * just what the caller asked for.
100 * Doesn't have to be accurate, i.e. may have races.
102 unsigned int kobjsize(const void *objp)
104 struct page *page;
107 * If the object we have should not have ksize performed on it,
108 * return size of 0
110 if (!objp || !virt_addr_valid(objp))
111 return 0;
113 page = virt_to_head_page(objp);
116 * If the allocator sets PageSlab, we know the pointer came from
117 * kmalloc().
119 if (PageSlab(page))
120 return ksize(objp);
123 * If it's not a compound page, see if we have a matching VMA
124 * region. This test is intentionally done in reverse order,
125 * so if there's no VMA, we still fall through and hand back
126 * PAGE_SIZE for 0-order pages.
128 if (!PageCompound(page)) {
129 struct vm_area_struct *vma;
131 vma = find_vma(current->mm, (unsigned long)objp);
132 if (vma)
133 return vma->vm_end - vma->vm_start;
137 * The ksize() function is only guaranteed to work for pointers
138 * returned by kmalloc(). So handle arbitrary pointers here.
140 return PAGE_SIZE << compound_order(page);
143 int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
144 unsigned long start, int nr_pages, unsigned int foll_flags,
145 struct page **pages, struct vm_area_struct **vmas,
146 int *retry)
148 struct vm_area_struct *vma;
149 unsigned long vm_flags;
150 int i;
152 /* calculate required read or write permissions.
153 * If FOLL_FORCE is set, we only require the "MAY" flags.
155 vm_flags = (foll_flags & FOLL_WRITE) ?
156 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
157 vm_flags &= (foll_flags & FOLL_FORCE) ?
158 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
160 for (i = 0; i < nr_pages; i++) {
161 vma = find_vma(mm, start);
162 if (!vma)
163 goto finish_or_fault;
165 /* protect what we can, including chardevs */
166 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
167 !(vm_flags & vma->vm_flags))
168 goto finish_or_fault;
170 if (pages) {
171 pages[i] = virt_to_page(start);
172 if (pages[i])
173 page_cache_get(pages[i]);
175 if (vmas)
176 vmas[i] = vma;
177 start = (start + PAGE_SIZE) & PAGE_MASK;
180 return i;
182 finish_or_fault:
183 return i ? : -EFAULT;
187 * get a list of pages in an address range belonging to the specified process
188 * and indicate the VMA that covers each page
189 * - this is potentially dodgy as we may end incrementing the page count of a
190 * slab page or a secondary page from a compound page
191 * - don't permit access to VMAs that don't support it, such as I/O mappings
193 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
194 unsigned long start, int nr_pages, int write, int force,
195 struct page **pages, struct vm_area_struct **vmas)
197 int flags = 0;
199 if (write)
200 flags |= FOLL_WRITE;
201 if (force)
202 flags |= FOLL_FORCE;
204 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
205 NULL);
207 EXPORT_SYMBOL(get_user_pages);
210 * follow_pfn - look up PFN at a user virtual address
211 * @vma: memory mapping
212 * @address: user virtual address
213 * @pfn: location to store found PFN
215 * Only IO mappings and raw PFN mappings are allowed.
217 * Returns zero and the pfn at @pfn on success, -ve otherwise.
219 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
220 unsigned long *pfn)
222 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
223 return -EINVAL;
225 *pfn = address >> PAGE_SHIFT;
226 return 0;
228 EXPORT_SYMBOL(follow_pfn);
230 DEFINE_RWLOCK(vmlist_lock);
231 struct vm_struct *vmlist;
233 void vfree(const void *addr)
235 kfree(addr);
237 EXPORT_SYMBOL(vfree);
239 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
242 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
243 * returns only a logical address.
245 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
247 EXPORT_SYMBOL(__vmalloc);
249 void *vmalloc_user(unsigned long size)
251 void *ret;
253 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
254 PAGE_KERNEL);
255 if (ret) {
256 struct vm_area_struct *vma;
258 down_write(&current->mm->mmap_sem);
259 vma = find_vma(current->mm, (unsigned long)ret);
260 if (vma)
261 vma->vm_flags |= VM_USERMAP;
262 up_write(&current->mm->mmap_sem);
265 return ret;
267 EXPORT_SYMBOL(vmalloc_user);
269 struct page *vmalloc_to_page(const void *addr)
271 return virt_to_page(addr);
273 EXPORT_SYMBOL(vmalloc_to_page);
275 unsigned long vmalloc_to_pfn(const void *addr)
277 return page_to_pfn(virt_to_page(addr));
279 EXPORT_SYMBOL(vmalloc_to_pfn);
281 long vread(char *buf, char *addr, unsigned long count)
283 memcpy(buf, addr, count);
284 return count;
287 long vwrite(char *buf, char *addr, unsigned long count)
289 /* Don't allow overflow */
290 if ((unsigned long) addr + count < count)
291 count = -(unsigned long) addr;
293 memcpy(addr, buf, count);
294 return(count);
298 * vmalloc - allocate virtually continguos memory
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.
305 * For tight control over page level allocator and protection flags
306 * use __vmalloc() instead.
308 void *vmalloc(unsigned long size)
310 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
312 EXPORT_SYMBOL(vmalloc);
315 * vzalloc - allocate virtually continguos memory with zero fill
317 * @size: allocation size
319 * Allocate enough pages to cover @size from the page level
320 * allocator and map them into continguos kernel virtual space.
321 * The memory allocated is set to zero.
323 * For tight control over page level allocator and protection flags
324 * use __vmalloc() instead.
326 void *vzalloc(unsigned long size)
328 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
329 PAGE_KERNEL);
331 EXPORT_SYMBOL(vzalloc);
334 * vmalloc_node - allocate memory on a specific node
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.
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
344 void *vmalloc_node(unsigned long size, int node)
346 return vmalloc(size);
348 EXPORT_SYMBOL(vmalloc_node);
351 * vzalloc_node - allocate memory on a specific node with zero fill
352 * @size: allocation size
353 * @node: numa node
355 * Allocate enough pages to cover @size from the page level
356 * allocator and map them into contiguous kernel virtual space.
357 * The memory allocated is set to zero.
359 * For tight control over page level allocator and protection flags
360 * use __vmalloc() instead.
362 void *vzalloc_node(unsigned long size, int node)
364 return vzalloc(size);
366 EXPORT_SYMBOL(vzalloc_node);
368 #ifndef PAGE_KERNEL_EXEC
369 # define PAGE_KERNEL_EXEC PAGE_KERNEL
370 #endif
373 * vmalloc_exec - allocate virtually contiguous, executable memory
374 * @size: allocation size
376 * Kernel-internal function to allocate enough pages to cover @size
377 * the page level allocator and map them into contiguous and
378 * executable kernel virtual space.
380 * For tight control over page level allocator and protection flags
381 * use __vmalloc() instead.
384 void *vmalloc_exec(unsigned long size)
386 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
390 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
391 * @size: allocation size
393 * Allocate enough 32bit PA addressable pages to cover @size from the
394 * page level allocator and map them into continguos kernel virtual space.
396 void *vmalloc_32(unsigned long size)
398 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
400 EXPORT_SYMBOL(vmalloc_32);
403 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
404 * @size: allocation size
406 * The resulting memory area is 32bit addressable and zeroed so it can be
407 * mapped to userspace without leaking data.
409 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
410 * remap_vmalloc_range() are permissible.
412 void *vmalloc_32_user(unsigned long size)
415 * We'll have to sort out the ZONE_DMA bits for 64-bit,
416 * but for now this can simply use vmalloc_user() directly.
418 return vmalloc_user(size);
420 EXPORT_SYMBOL(vmalloc_32_user);
422 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
424 BUG();
425 return NULL;
427 EXPORT_SYMBOL(vmap);
429 void vunmap(const void *addr)
431 BUG();
433 EXPORT_SYMBOL(vunmap);
435 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
437 BUG();
438 return NULL;
440 EXPORT_SYMBOL(vm_map_ram);
442 void vm_unmap_ram(const void *mem, unsigned int count)
444 BUG();
446 EXPORT_SYMBOL(vm_unmap_ram);
448 void vm_unmap_aliases(void)
451 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
454 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
455 * have one.
457 void __attribute__((weak)) vmalloc_sync_all(void)
462 * alloc_vm_area - allocate a range of kernel address space
463 * @size: size of the area
465 * Returns: NULL on failure, vm_struct on success
467 * This function reserves a range of kernel address space, and
468 * allocates pagetables to map that range. No actual mappings
469 * are created. If the kernel address space is not shared
470 * between processes, it syncs the pagetable across all
471 * processes.
473 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
475 BUG();
476 return NULL;
478 EXPORT_SYMBOL_GPL(alloc_vm_area);
480 void free_vm_area(struct vm_struct *area)
482 BUG();
484 EXPORT_SYMBOL_GPL(free_vm_area);
486 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
487 struct page *page)
489 return -EINVAL;
491 EXPORT_SYMBOL(vm_insert_page);
494 * sys_brk() for the most part doesn't need the global kernel
495 * lock, except when an application is doing something nasty
496 * like trying to un-brk an area that has already been mapped
497 * to a regular file. in this case, the unmapping will need
498 * to invoke file system routines that need the global lock.
500 SYSCALL_DEFINE1(brk, unsigned long, brk)
502 struct mm_struct *mm = current->mm;
504 if (brk < mm->start_brk || brk > mm->context.end_brk)
505 return mm->brk;
507 if (mm->brk == brk)
508 return mm->brk;
511 * Always allow shrinking brk
513 if (brk <= mm->brk) {
514 mm->brk = brk;
515 return brk;
519 * Ok, looks good - let it rip.
521 flush_icache_range(mm->brk, brk);
522 return mm->brk = brk;
526 * initialise the VMA and region record slabs
528 void __init mmap_init(void)
530 int ret;
532 ret = percpu_counter_init(&vm_committed_as, 0);
533 VM_BUG_ON(ret);
534 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
538 * validate the region tree
539 * - the caller must hold the region lock
541 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
542 static noinline void validate_nommu_regions(void)
544 struct vm_region *region, *last;
545 struct rb_node *p, *lastp;
547 lastp = rb_first(&nommu_region_tree);
548 if (!lastp)
549 return;
551 last = rb_entry(lastp, struct vm_region, vm_rb);
552 BUG_ON(unlikely(last->vm_end <= last->vm_start));
553 BUG_ON(unlikely(last->vm_top < last->vm_end));
555 while ((p = rb_next(lastp))) {
556 region = rb_entry(p, struct vm_region, vm_rb);
557 last = rb_entry(lastp, struct vm_region, vm_rb);
559 BUG_ON(unlikely(region->vm_end <= region->vm_start));
560 BUG_ON(unlikely(region->vm_top < region->vm_end));
561 BUG_ON(unlikely(region->vm_start < last->vm_top));
563 lastp = p;
566 #else
567 static void validate_nommu_regions(void)
570 #endif
573 * add a region into the global tree
575 static void add_nommu_region(struct vm_region *region)
577 struct vm_region *pregion;
578 struct rb_node **p, *parent;
580 validate_nommu_regions();
582 parent = NULL;
583 p = &nommu_region_tree.rb_node;
584 while (*p) {
585 parent = *p;
586 pregion = rb_entry(parent, struct vm_region, vm_rb);
587 if (region->vm_start < pregion->vm_start)
588 p = &(*p)->rb_left;
589 else if (region->vm_start > pregion->vm_start)
590 p = &(*p)->rb_right;
591 else if (pregion == region)
592 return;
593 else
594 BUG();
597 rb_link_node(&region->vm_rb, parent, p);
598 rb_insert_color(&region->vm_rb, &nommu_region_tree);
600 validate_nommu_regions();
604 * delete a region from the global tree
606 static void delete_nommu_region(struct vm_region *region)
608 BUG_ON(!nommu_region_tree.rb_node);
610 validate_nommu_regions();
611 rb_erase(&region->vm_rb, &nommu_region_tree);
612 validate_nommu_regions();
616 * free a contiguous series of pages
618 static void free_page_series(unsigned long from, unsigned long to)
620 for (; from < to; from += PAGE_SIZE) {
621 struct page *page = virt_to_page(from);
623 kdebug("- free %lx", from);
624 atomic_long_dec(&mmap_pages_allocated);
625 if (page_count(page) != 1)
626 kdebug("free page %p: refcount not one: %d",
627 page, page_count(page));
628 put_page(page);
633 * release a reference to a region
634 * - the caller must hold the region semaphore for writing, which this releases
635 * - the region may not have been added to the tree yet, in which case vm_top
636 * will equal vm_start
638 static void __put_nommu_region(struct vm_region *region)
639 __releases(nommu_region_sem)
641 kenter("%p{%d}", region, region->vm_usage);
643 BUG_ON(!nommu_region_tree.rb_node);
645 if (--region->vm_usage == 0) {
646 if (region->vm_top > region->vm_start)
647 delete_nommu_region(region);
648 up_write(&nommu_region_sem);
650 if (region->vm_file)
651 fput(region->vm_file);
653 /* IO memory and memory shared directly out of the pagecache
654 * from ramfs/tmpfs mustn't be released here */
655 if (region->vm_flags & VM_MAPPED_COPY) {
656 kdebug("free series");
657 free_page_series(region->vm_start, region->vm_top);
659 kmem_cache_free(vm_region_jar, region);
660 } else {
661 up_write(&nommu_region_sem);
666 * release a reference to a region
668 static void put_nommu_region(struct vm_region *region)
670 down_write(&nommu_region_sem);
671 __put_nommu_region(region);
675 * update protection on a vma
677 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
679 #ifdef CONFIG_MPU
680 struct mm_struct *mm = vma->vm_mm;
681 long start = vma->vm_start & PAGE_MASK;
682 while (start < vma->vm_end) {
683 protect_page(mm, start, flags);
684 start += PAGE_SIZE;
686 update_protections(mm);
687 #endif
691 * add a VMA into a process's mm_struct in the appropriate place in the list
692 * and tree and add to the address space's page tree also if not an anonymous
693 * page
694 * - should be called with mm->mmap_sem held writelocked
696 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
698 struct vm_area_struct *pvma, *prev;
699 struct address_space *mapping;
700 struct rb_node **p, *parent, *rb_prev;
702 kenter(",%p", vma);
704 BUG_ON(!vma->vm_region);
706 mm->map_count++;
707 vma->vm_mm = mm;
709 protect_vma(vma, vma->vm_flags);
711 /* add the VMA to the mapping */
712 if (vma->vm_file) {
713 mapping = vma->vm_file->f_mapping;
715 mutex_lock(&mapping->i_mmap_mutex);
716 flush_dcache_mmap_lock(mapping);
717 vma_interval_tree_insert(vma, &mapping->i_mmap);
718 flush_dcache_mmap_unlock(mapping);
719 mutex_unlock(&mapping->i_mmap_mutex);
722 /* add the VMA to the tree */
723 parent = rb_prev = NULL;
724 p = &mm->mm_rb.rb_node;
725 while (*p) {
726 parent = *p;
727 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
729 /* sort by: start addr, end addr, VMA struct addr in that order
730 * (the latter is necessary as we may get identical VMAs) */
731 if (vma->vm_start < pvma->vm_start)
732 p = &(*p)->rb_left;
733 else if (vma->vm_start > pvma->vm_start) {
734 rb_prev = parent;
735 p = &(*p)->rb_right;
736 } else if (vma->vm_end < pvma->vm_end)
737 p = &(*p)->rb_left;
738 else if (vma->vm_end > pvma->vm_end) {
739 rb_prev = parent;
740 p = &(*p)->rb_right;
741 } else if (vma < pvma)
742 p = &(*p)->rb_left;
743 else if (vma > pvma) {
744 rb_prev = parent;
745 p = &(*p)->rb_right;
746 } else
747 BUG();
750 rb_link_node(&vma->vm_rb, parent, p);
751 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
753 /* add VMA to the VMA list also */
754 prev = NULL;
755 if (rb_prev)
756 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
758 __vma_link_list(mm, vma, prev, parent);
762 * delete a VMA from its owning mm_struct and address space
764 static void delete_vma_from_mm(struct vm_area_struct *vma)
766 struct address_space *mapping;
767 struct mm_struct *mm = vma->vm_mm;
769 kenter("%p", vma);
771 protect_vma(vma, 0);
773 mm->map_count--;
774 if (mm->mmap_cache == vma)
775 mm->mmap_cache = NULL;
777 /* remove the VMA from the mapping */
778 if (vma->vm_file) {
779 mapping = vma->vm_file->f_mapping;
781 mutex_lock(&mapping->i_mmap_mutex);
782 flush_dcache_mmap_lock(mapping);
783 vma_interval_tree_remove(vma, &mapping->i_mmap);
784 flush_dcache_mmap_unlock(mapping);
785 mutex_unlock(&mapping->i_mmap_mutex);
788 /* remove from the MM's tree and list */
789 rb_erase(&vma->vm_rb, &mm->mm_rb);
791 if (vma->vm_prev)
792 vma->vm_prev->vm_next = vma->vm_next;
793 else
794 mm->mmap = vma->vm_next;
796 if (vma->vm_next)
797 vma->vm_next->vm_prev = vma->vm_prev;
801 * destroy a VMA record
803 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
805 kenter("%p", vma);
806 if (vma->vm_ops && vma->vm_ops->close)
807 vma->vm_ops->close(vma);
808 if (vma->vm_file)
809 fput(vma->vm_file);
810 put_nommu_region(vma->vm_region);
811 kmem_cache_free(vm_area_cachep, vma);
815 * look up the first VMA in which addr resides, NULL if none
816 * - should be called with mm->mmap_sem at least held readlocked
818 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
820 struct vm_area_struct *vma;
822 /* check the cache first */
823 vma = mm->mmap_cache;
824 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
825 return vma;
827 /* trawl the list (there may be multiple mappings in which addr
828 * resides) */
829 for (vma = mm->mmap; vma; vma = vma->vm_next) {
830 if (vma->vm_start > addr)
831 return NULL;
832 if (vma->vm_end > addr) {
833 mm->mmap_cache = vma;
834 return vma;
838 return NULL;
840 EXPORT_SYMBOL(find_vma);
843 * find a VMA
844 * - we don't extend stack VMAs under NOMMU conditions
846 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
848 return find_vma(mm, addr);
852 * expand a stack to a given address
853 * - not supported under NOMMU conditions
855 int expand_stack(struct vm_area_struct *vma, unsigned long address)
857 return -ENOMEM;
861 * look up the first VMA exactly that exactly matches addr
862 * - should be called with mm->mmap_sem at least held readlocked
864 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
865 unsigned long addr,
866 unsigned long len)
868 struct vm_area_struct *vma;
869 unsigned long end = addr + len;
871 /* check the cache first */
872 vma = mm->mmap_cache;
873 if (vma && vma->vm_start == addr && vma->vm_end == end)
874 return vma;
876 /* trawl the list (there may be multiple mappings in which addr
877 * resides) */
878 for (vma = mm->mmap; vma; vma = vma->vm_next) {
879 if (vma->vm_start < addr)
880 continue;
881 if (vma->vm_start > addr)
882 return NULL;
883 if (vma->vm_end == end) {
884 mm->mmap_cache = vma;
885 return vma;
889 return NULL;
893 * determine whether a mapping should be permitted and, if so, what sort of
894 * mapping we're capable of supporting
896 static int validate_mmap_request(struct file *file,
897 unsigned long addr,
898 unsigned long len,
899 unsigned long prot,
900 unsigned long flags,
901 unsigned long pgoff,
902 unsigned long *_capabilities)
904 unsigned long capabilities, rlen;
905 int ret;
907 /* do the simple checks first */
908 if (flags & MAP_FIXED) {
909 printk(KERN_DEBUG
910 "%d: Can't do fixed-address/overlay mmap of RAM\n",
911 current->pid);
912 return -EINVAL;
915 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
916 (flags & MAP_TYPE) != MAP_SHARED)
917 return -EINVAL;
919 if (!len)
920 return -EINVAL;
922 /* Careful about overflows.. */
923 rlen = PAGE_ALIGN(len);
924 if (!rlen || rlen > TASK_SIZE)
925 return -ENOMEM;
927 /* offset overflow? */
928 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
929 return -EOVERFLOW;
931 if (file) {
932 /* validate file mapping requests */
933 struct address_space *mapping;
935 /* files must support mmap */
936 if (!file->f_op || !file->f_op->mmap)
937 return -ENODEV;
939 /* work out if what we've got could possibly be shared
940 * - we support chardevs that provide their own "memory"
941 * - we support files/blockdevs that are memory backed
943 mapping = file->f_mapping;
944 if (!mapping)
945 mapping = file->f_path.dentry->d_inode->i_mapping;
947 capabilities = 0;
948 if (mapping && mapping->backing_dev_info)
949 capabilities = mapping->backing_dev_info->capabilities;
951 if (!capabilities) {
952 /* no explicit capabilities set, so assume some
953 * defaults */
954 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
955 case S_IFREG:
956 case S_IFBLK:
957 capabilities = BDI_CAP_MAP_COPY;
958 break;
960 case S_IFCHR:
961 capabilities =
962 BDI_CAP_MAP_DIRECT |
963 BDI_CAP_READ_MAP |
964 BDI_CAP_WRITE_MAP;
965 break;
967 default:
968 return -EINVAL;
972 /* eliminate any capabilities that we can't support on this
973 * device */
974 if (!file->f_op->get_unmapped_area)
975 capabilities &= ~BDI_CAP_MAP_DIRECT;
976 if (!file->f_op->read)
977 capabilities &= ~BDI_CAP_MAP_COPY;
979 /* The file shall have been opened with read permission. */
980 if (!(file->f_mode & FMODE_READ))
981 return -EACCES;
983 if (flags & MAP_SHARED) {
984 /* do checks for writing, appending and locking */
985 if ((prot & PROT_WRITE) &&
986 !(file->f_mode & FMODE_WRITE))
987 return -EACCES;
989 if (IS_APPEND(file->f_path.dentry->d_inode) &&
990 (file->f_mode & FMODE_WRITE))
991 return -EACCES;
993 if (locks_verify_locked(file->f_path.dentry->d_inode))
994 return -EAGAIN;
996 if (!(capabilities & BDI_CAP_MAP_DIRECT))
997 return -ENODEV;
999 /* we mustn't privatise shared mappings */
1000 capabilities &= ~BDI_CAP_MAP_COPY;
1002 else {
1003 /* we're going to read the file into private memory we
1004 * allocate */
1005 if (!(capabilities & BDI_CAP_MAP_COPY))
1006 return -ENODEV;
1008 /* we don't permit a private writable mapping to be
1009 * shared with the backing device */
1010 if (prot & PROT_WRITE)
1011 capabilities &= ~BDI_CAP_MAP_DIRECT;
1014 if (capabilities & BDI_CAP_MAP_DIRECT) {
1015 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1016 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1017 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1019 capabilities &= ~BDI_CAP_MAP_DIRECT;
1020 if (flags & MAP_SHARED) {
1021 printk(KERN_WARNING
1022 "MAP_SHARED not completely supported on !MMU\n");
1023 return -EINVAL;
1028 /* handle executable mappings and implied executable
1029 * mappings */
1030 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1031 if (prot & PROT_EXEC)
1032 return -EPERM;
1034 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1035 /* handle implication of PROT_EXEC by PROT_READ */
1036 if (current->personality & READ_IMPLIES_EXEC) {
1037 if (capabilities & BDI_CAP_EXEC_MAP)
1038 prot |= PROT_EXEC;
1041 else if ((prot & PROT_READ) &&
1042 (prot & PROT_EXEC) &&
1043 !(capabilities & BDI_CAP_EXEC_MAP)
1045 /* backing file is not executable, try to copy */
1046 capabilities &= ~BDI_CAP_MAP_DIRECT;
1049 else {
1050 /* anonymous mappings are always memory backed and can be
1051 * privately mapped
1053 capabilities = BDI_CAP_MAP_COPY;
1055 /* handle PROT_EXEC implication by PROT_READ */
1056 if ((prot & PROT_READ) &&
1057 (current->personality & READ_IMPLIES_EXEC))
1058 prot |= PROT_EXEC;
1061 /* allow the security API to have its say */
1062 ret = security_mmap_addr(addr);
1063 if (ret < 0)
1064 return ret;
1066 /* looks okay */
1067 *_capabilities = capabilities;
1068 return 0;
1072 * we've determined that we can make the mapping, now translate what we
1073 * now know into VMA flags
1075 static unsigned long determine_vm_flags(struct file *file,
1076 unsigned long prot,
1077 unsigned long flags,
1078 unsigned long capabilities)
1080 unsigned long vm_flags;
1082 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1083 /* vm_flags |= mm->def_flags; */
1085 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1086 /* attempt to share read-only copies of mapped file chunks */
1087 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1088 if (file && !(prot & PROT_WRITE))
1089 vm_flags |= VM_MAYSHARE;
1090 } else {
1091 /* overlay a shareable mapping on the backing device or inode
1092 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1093 * romfs/cramfs */
1094 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1095 if (flags & MAP_SHARED)
1096 vm_flags |= VM_SHARED;
1099 /* refuse to let anyone share private mappings with this process if
1100 * it's being traced - otherwise breakpoints set in it may interfere
1101 * with another untraced process
1103 if ((flags & MAP_PRIVATE) && current->ptrace)
1104 vm_flags &= ~VM_MAYSHARE;
1106 return vm_flags;
1110 * set up a shared mapping on a file (the driver or filesystem provides and
1111 * pins the storage)
1113 static int do_mmap_shared_file(struct vm_area_struct *vma)
1115 int ret;
1117 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1118 if (ret == 0) {
1119 vma->vm_region->vm_top = vma->vm_region->vm_end;
1120 return 0;
1122 if (ret != -ENOSYS)
1123 return ret;
1125 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1126 * opposed to tried but failed) so we can only give a suitable error as
1127 * it's not possible to make a private copy if MAP_SHARED was given */
1128 return -ENODEV;
1132 * set up a private mapping or an anonymous shared mapping
1134 static int do_mmap_private(struct vm_area_struct *vma,
1135 struct vm_region *region,
1136 unsigned long len,
1137 unsigned long capabilities)
1139 struct page *pages;
1140 unsigned long total, point, n;
1141 void *base;
1142 int ret, order;
1144 /* invoke the file's mapping function so that it can keep track of
1145 * shared mappings on devices or memory
1146 * - VM_MAYSHARE will be set if it may attempt to share
1148 if (capabilities & BDI_CAP_MAP_DIRECT) {
1149 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1150 if (ret == 0) {
1151 /* shouldn't return success if we're not sharing */
1152 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1153 vma->vm_region->vm_top = vma->vm_region->vm_end;
1154 return 0;
1156 if (ret != -ENOSYS)
1157 return ret;
1159 /* getting an ENOSYS error indicates that direct mmap isn't
1160 * possible (as opposed to tried but failed) so we'll try to
1161 * make a private copy of the data and map that instead */
1165 /* allocate some memory to hold the mapping
1166 * - note that this may not return a page-aligned address if the object
1167 * we're allocating is smaller than a page
1169 order = get_order(len);
1170 kdebug("alloc order %d for %lx", order, len);
1172 pages = alloc_pages(GFP_KERNEL, order);
1173 if (!pages)
1174 goto enomem;
1176 total = 1 << order;
1177 atomic_long_add(total, &mmap_pages_allocated);
1179 point = len >> PAGE_SHIFT;
1181 /* we allocated a power-of-2 sized page set, so we may want to trim off
1182 * the excess */
1183 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1184 while (total > point) {
1185 order = ilog2(total - point);
1186 n = 1 << order;
1187 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1188 atomic_long_sub(n, &mmap_pages_allocated);
1189 total -= n;
1190 set_page_refcounted(pages + total);
1191 __free_pages(pages + total, order);
1195 for (point = 1; point < total; point++)
1196 set_page_refcounted(&pages[point]);
1198 base = page_address(pages);
1199 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1200 region->vm_start = (unsigned long) base;
1201 region->vm_end = region->vm_start + len;
1202 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1204 vma->vm_start = region->vm_start;
1205 vma->vm_end = region->vm_start + len;
1207 if (vma->vm_file) {
1208 /* read the contents of a file into the copy */
1209 mm_segment_t old_fs;
1210 loff_t fpos;
1212 fpos = vma->vm_pgoff;
1213 fpos <<= PAGE_SHIFT;
1215 old_fs = get_fs();
1216 set_fs(KERNEL_DS);
1217 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
1218 set_fs(old_fs);
1220 if (ret < 0)
1221 goto error_free;
1223 /* clear the last little bit */
1224 if (ret < len)
1225 memset(base + ret, 0, len - ret);
1229 return 0;
1231 error_free:
1232 free_page_series(region->vm_start, region->vm_top);
1233 region->vm_start = vma->vm_start = 0;
1234 region->vm_end = vma->vm_end = 0;
1235 region->vm_top = 0;
1236 return ret;
1238 enomem:
1239 printk("Allocation of length %lu from process %d (%s) failed\n",
1240 len, current->pid, current->comm);
1241 show_free_areas(0);
1242 return -ENOMEM;
1246 * handle mapping creation for uClinux
1248 unsigned long do_mmap_pgoff(struct file *file,
1249 unsigned long addr,
1250 unsigned long len,
1251 unsigned long prot,
1252 unsigned long flags,
1253 unsigned long pgoff,
1254 unsigned long *populate)
1256 struct vm_area_struct *vma;
1257 struct vm_region *region;
1258 struct rb_node *rb;
1259 unsigned long capabilities, vm_flags, result;
1260 int ret;
1262 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1264 *populate = 0;
1266 /* decide whether we should attempt the mapping, and if so what sort of
1267 * mapping */
1268 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1269 &capabilities);
1270 if (ret < 0) {
1271 kleave(" = %d [val]", ret);
1272 return ret;
1275 /* we ignore the address hint */
1276 addr = 0;
1277 len = PAGE_ALIGN(len);
1279 /* we've determined that we can make the mapping, now translate what we
1280 * now know into VMA flags */
1281 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1283 /* we're going to need to record the mapping */
1284 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1285 if (!region)
1286 goto error_getting_region;
1288 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1289 if (!vma)
1290 goto error_getting_vma;
1292 region->vm_usage = 1;
1293 region->vm_flags = vm_flags;
1294 region->vm_pgoff = pgoff;
1296 INIT_LIST_HEAD(&vma->anon_vma_chain);
1297 vma->vm_flags = vm_flags;
1298 vma->vm_pgoff = pgoff;
1300 if (file) {
1301 region->vm_file = get_file(file);
1302 vma->vm_file = get_file(file);
1305 down_write(&nommu_region_sem);
1307 /* if we want to share, we need to check for regions created by other
1308 * mmap() calls that overlap with our proposed mapping
1309 * - we can only share with a superset match on most regular files
1310 * - shared mappings on character devices and memory backed files are
1311 * permitted to overlap inexactly as far as we are concerned for in
1312 * these cases, sharing is handled in the driver or filesystem rather
1313 * than here
1315 if (vm_flags & VM_MAYSHARE) {
1316 struct vm_region *pregion;
1317 unsigned long pglen, rpglen, pgend, rpgend, start;
1319 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1320 pgend = pgoff + pglen;
1322 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1323 pregion = rb_entry(rb, struct vm_region, vm_rb);
1325 if (!(pregion->vm_flags & VM_MAYSHARE))
1326 continue;
1328 /* search for overlapping mappings on the same file */
1329 if (pregion->vm_file->f_path.dentry->d_inode !=
1330 file->f_path.dentry->d_inode)
1331 continue;
1333 if (pregion->vm_pgoff >= pgend)
1334 continue;
1336 rpglen = pregion->vm_end - pregion->vm_start;
1337 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1338 rpgend = pregion->vm_pgoff + rpglen;
1339 if (pgoff >= rpgend)
1340 continue;
1342 /* handle inexactly overlapping matches between
1343 * mappings */
1344 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1345 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1346 /* new mapping is not a subset of the region */
1347 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1348 goto sharing_violation;
1349 continue;
1352 /* we've found a region we can share */
1353 pregion->vm_usage++;
1354 vma->vm_region = pregion;
1355 start = pregion->vm_start;
1356 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1357 vma->vm_start = start;
1358 vma->vm_end = start + len;
1360 if (pregion->vm_flags & VM_MAPPED_COPY) {
1361 kdebug("share copy");
1362 vma->vm_flags |= VM_MAPPED_COPY;
1363 } else {
1364 kdebug("share mmap");
1365 ret = do_mmap_shared_file(vma);
1366 if (ret < 0) {
1367 vma->vm_region = NULL;
1368 vma->vm_start = 0;
1369 vma->vm_end = 0;
1370 pregion->vm_usage--;
1371 pregion = NULL;
1372 goto error_just_free;
1375 fput(region->vm_file);
1376 kmem_cache_free(vm_region_jar, region);
1377 region = pregion;
1378 result = start;
1379 goto share;
1382 /* obtain the address at which to make a shared mapping
1383 * - this is the hook for quasi-memory character devices to
1384 * tell us the location of a shared mapping
1386 if (capabilities & BDI_CAP_MAP_DIRECT) {
1387 addr = file->f_op->get_unmapped_area(file, addr, len,
1388 pgoff, flags);
1389 if (IS_ERR_VALUE(addr)) {
1390 ret = addr;
1391 if (ret != -ENOSYS)
1392 goto error_just_free;
1394 /* the driver refused to tell us where to site
1395 * the mapping so we'll have to attempt to copy
1396 * it */
1397 ret = -ENODEV;
1398 if (!(capabilities & BDI_CAP_MAP_COPY))
1399 goto error_just_free;
1401 capabilities &= ~BDI_CAP_MAP_DIRECT;
1402 } else {
1403 vma->vm_start = region->vm_start = addr;
1404 vma->vm_end = region->vm_end = addr + len;
1409 vma->vm_region = region;
1411 /* set up the mapping
1412 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1414 if (file && vma->vm_flags & VM_SHARED)
1415 ret = do_mmap_shared_file(vma);
1416 else
1417 ret = do_mmap_private(vma, region, len, capabilities);
1418 if (ret < 0)
1419 goto error_just_free;
1420 add_nommu_region(region);
1422 /* clear anonymous mappings that don't ask for uninitialized data */
1423 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1424 memset((void *)region->vm_start, 0,
1425 region->vm_end - region->vm_start);
1427 /* okay... we have a mapping; now we have to register it */
1428 result = vma->vm_start;
1430 current->mm->total_vm += len >> PAGE_SHIFT;
1432 share:
1433 add_vma_to_mm(current->mm, vma);
1435 /* we flush the region from the icache only when the first executable
1436 * mapping of it is made */
1437 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1438 flush_icache_range(region->vm_start, region->vm_end);
1439 region->vm_icache_flushed = true;
1442 up_write(&nommu_region_sem);
1444 kleave(" = %lx", result);
1445 return result;
1447 error_just_free:
1448 up_write(&nommu_region_sem);
1449 error:
1450 if (region->vm_file)
1451 fput(region->vm_file);
1452 kmem_cache_free(vm_region_jar, region);
1453 if (vma->vm_file)
1454 fput(vma->vm_file);
1455 kmem_cache_free(vm_area_cachep, vma);
1456 kleave(" = %d", ret);
1457 return ret;
1459 sharing_violation:
1460 up_write(&nommu_region_sem);
1461 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1462 ret = -EINVAL;
1463 goto error;
1465 error_getting_vma:
1466 kmem_cache_free(vm_region_jar, region);
1467 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1468 " from process %d failed\n",
1469 len, current->pid);
1470 show_free_areas(0);
1471 return -ENOMEM;
1473 error_getting_region:
1474 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1475 " from process %d failed\n",
1476 len, current->pid);
1477 show_free_areas(0);
1478 return -ENOMEM;
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 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1499 if (file)
1500 fput(file);
1501 out:
1502 return retval;
1505 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1506 struct mmap_arg_struct {
1507 unsigned long addr;
1508 unsigned long len;
1509 unsigned long prot;
1510 unsigned long flags;
1511 unsigned long fd;
1512 unsigned long offset;
1515 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1517 struct mmap_arg_struct a;
1519 if (copy_from_user(&a, arg, sizeof(a)))
1520 return -EFAULT;
1521 if (a.offset & ~PAGE_MASK)
1522 return -EINVAL;
1524 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1525 a.offset >> PAGE_SHIFT);
1527 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1530 * split a vma into two pieces at address 'addr', a new vma is allocated either
1531 * for the first part or the tail.
1533 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1534 unsigned long addr, int new_below)
1536 struct vm_area_struct *new;
1537 struct vm_region *region;
1538 unsigned long npages;
1540 kenter("");
1542 /* we're only permitted to split anonymous regions (these should have
1543 * only a single usage on the region) */
1544 if (vma->vm_file)
1545 return -ENOMEM;
1547 if (mm->map_count >= sysctl_max_map_count)
1548 return -ENOMEM;
1550 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1551 if (!region)
1552 return -ENOMEM;
1554 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1555 if (!new) {
1556 kmem_cache_free(vm_region_jar, region);
1557 return -ENOMEM;
1560 /* most fields are the same, copy all, and then fixup */
1561 *new = *vma;
1562 *region = *vma->vm_region;
1563 new->vm_region = region;
1565 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1567 if (new_below) {
1568 region->vm_top = region->vm_end = new->vm_end = addr;
1569 } else {
1570 region->vm_start = new->vm_start = addr;
1571 region->vm_pgoff = new->vm_pgoff += npages;
1574 if (new->vm_ops && new->vm_ops->open)
1575 new->vm_ops->open(new);
1577 delete_vma_from_mm(vma);
1578 down_write(&nommu_region_sem);
1579 delete_nommu_region(vma->vm_region);
1580 if (new_below) {
1581 vma->vm_region->vm_start = vma->vm_start = addr;
1582 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1583 } else {
1584 vma->vm_region->vm_end = vma->vm_end = addr;
1585 vma->vm_region->vm_top = addr;
1587 add_nommu_region(vma->vm_region);
1588 add_nommu_region(new->vm_region);
1589 up_write(&nommu_region_sem);
1590 add_vma_to_mm(mm, vma);
1591 add_vma_to_mm(mm, new);
1592 return 0;
1596 * shrink a VMA by removing the specified chunk from either the beginning or
1597 * the end
1599 static int shrink_vma(struct mm_struct *mm,
1600 struct vm_area_struct *vma,
1601 unsigned long from, unsigned long to)
1603 struct vm_region *region;
1605 kenter("");
1607 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1608 * and list */
1609 delete_vma_from_mm(vma);
1610 if (from > vma->vm_start)
1611 vma->vm_end = from;
1612 else
1613 vma->vm_start = to;
1614 add_vma_to_mm(mm, vma);
1616 /* cut the backing region down to size */
1617 region = vma->vm_region;
1618 BUG_ON(region->vm_usage != 1);
1620 down_write(&nommu_region_sem);
1621 delete_nommu_region(region);
1622 if (from > region->vm_start) {
1623 to = region->vm_top;
1624 region->vm_top = region->vm_end = from;
1625 } else {
1626 region->vm_start = to;
1628 add_nommu_region(region);
1629 up_write(&nommu_region_sem);
1631 free_page_series(from, to);
1632 return 0;
1636 * release a mapping
1637 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1638 * VMA, though it need not cover the whole VMA
1640 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1642 struct vm_area_struct *vma;
1643 unsigned long end;
1644 int ret;
1646 kenter(",%lx,%zx", start, len);
1648 len = PAGE_ALIGN(len);
1649 if (len == 0)
1650 return -EINVAL;
1652 end = start + len;
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 vma = vma->vm_next;
1679 } while (vma);
1680 kleave(" = -EINVAL [split file]");
1681 return -EINVAL;
1682 } else {
1683 /* the chunk must be a subset of the VMA found */
1684 if (start == vma->vm_start && end == vma->vm_end)
1685 goto erase_whole_vma;
1686 if (start < vma->vm_start || end > vma->vm_end) {
1687 kleave(" = -EINVAL [superset]");
1688 return -EINVAL;
1690 if (start & ~PAGE_MASK) {
1691 kleave(" = -EINVAL [unaligned start]");
1692 return -EINVAL;
1694 if (end != vma->vm_end && end & ~PAGE_MASK) {
1695 kleave(" = -EINVAL [unaligned split]");
1696 return -EINVAL;
1698 if (start != vma->vm_start && end != vma->vm_end) {
1699 ret = split_vma(mm, vma, start, 1);
1700 if (ret < 0) {
1701 kleave(" = %d [split]", ret);
1702 return ret;
1705 return shrink_vma(mm, vma, start, end);
1708 erase_whole_vma:
1709 delete_vma_from_mm(vma);
1710 delete_vma(mm, vma);
1711 kleave(" = 0");
1712 return 0;
1714 EXPORT_SYMBOL(do_munmap);
1716 int vm_munmap(unsigned long addr, size_t len)
1718 struct mm_struct *mm = current->mm;
1719 int ret;
1721 down_write(&mm->mmap_sem);
1722 ret = do_munmap(mm, addr, len);
1723 up_write(&mm->mmap_sem);
1724 return ret;
1726 EXPORT_SYMBOL(vm_munmap);
1728 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1730 return vm_munmap(addr, len);
1734 * release all the mappings made in a process's VM space
1736 void exit_mmap(struct mm_struct *mm)
1738 struct vm_area_struct *vma;
1740 if (!mm)
1741 return;
1743 kenter("");
1745 mm->total_vm = 0;
1747 while ((vma = mm->mmap)) {
1748 mm->mmap = vma->vm_next;
1749 delete_vma_from_mm(vma);
1750 delete_vma(mm, vma);
1751 cond_resched();
1754 kleave("");
1757 unsigned long vm_brk(unsigned long addr, unsigned long len)
1759 return -ENOMEM;
1763 * expand (or shrink) an existing mapping, potentially moving it at the same
1764 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1766 * under NOMMU conditions, we only permit changing a mapping's size, and only
1767 * as long as it stays within the region allocated by do_mmap_private() and the
1768 * block is not shareable
1770 * MREMAP_FIXED is not supported under NOMMU conditions
1772 unsigned long do_mremap(unsigned long addr,
1773 unsigned long old_len, unsigned long new_len,
1774 unsigned long flags, unsigned long new_addr)
1776 struct vm_area_struct *vma;
1778 /* insanity checks first */
1779 old_len = PAGE_ALIGN(old_len);
1780 new_len = PAGE_ALIGN(new_len);
1781 if (old_len == 0 || new_len == 0)
1782 return (unsigned long) -EINVAL;
1784 if (addr & ~PAGE_MASK)
1785 return -EINVAL;
1787 if (flags & MREMAP_FIXED && new_addr != addr)
1788 return (unsigned long) -EINVAL;
1790 vma = find_vma_exact(current->mm, addr, old_len);
1791 if (!vma)
1792 return (unsigned long) -EINVAL;
1794 if (vma->vm_end != vma->vm_start + old_len)
1795 return (unsigned long) -EFAULT;
1797 if (vma->vm_flags & VM_MAYSHARE)
1798 return (unsigned long) -EPERM;
1800 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1801 return (unsigned long) -ENOMEM;
1803 /* all checks complete - do it */
1804 vma->vm_end = vma->vm_start + new_len;
1805 return vma->vm_start;
1807 EXPORT_SYMBOL(do_mremap);
1809 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1810 unsigned long, new_len, unsigned long, flags,
1811 unsigned long, new_addr)
1813 unsigned long ret;
1815 down_write(&current->mm->mmap_sem);
1816 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1817 up_write(&current->mm->mmap_sem);
1818 return ret;
1821 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1822 unsigned int foll_flags)
1824 return NULL;
1827 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1828 unsigned long pfn, unsigned long size, pgprot_t prot)
1830 if (addr != (pfn << PAGE_SHIFT))
1831 return -EINVAL;
1833 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1834 return 0;
1836 EXPORT_SYMBOL(remap_pfn_range);
1838 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1839 unsigned long pgoff)
1841 unsigned int size = vma->vm_end - vma->vm_start;
1843 if (!(vma->vm_flags & VM_USERMAP))
1844 return -EINVAL;
1846 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1847 vma->vm_end = vma->vm_start + size;
1849 return 0;
1851 EXPORT_SYMBOL(remap_vmalloc_range);
1853 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1854 unsigned long len, unsigned long pgoff, unsigned long flags)
1856 return -ENOMEM;
1859 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1863 void unmap_mapping_range(struct address_space *mapping,
1864 loff_t const holebegin, loff_t const holelen,
1865 int even_cows)
1868 EXPORT_SYMBOL(unmap_mapping_range);
1871 * Check that a process has enough memory to allocate a new virtual
1872 * mapping. 0 means there is enough memory for the allocation to
1873 * succeed and -ENOMEM implies there is not.
1875 * We currently support three overcommit policies, which are set via the
1876 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1878 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1879 * Additional code 2002 Jul 20 by Robert Love.
1881 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1883 * Note this is a helper function intended to be used by LSMs which
1884 * wish to use this logic.
1886 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1888 unsigned long free, allowed;
1890 vm_acct_memory(pages);
1893 * Sometimes we want to use more memory than we have
1895 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1896 return 0;
1898 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1899 free = global_page_state(NR_FREE_PAGES);
1900 free += global_page_state(NR_FILE_PAGES);
1903 * shmem pages shouldn't be counted as free in this
1904 * case, they can't be purged, only swapped out, and
1905 * that won't affect the overall amount of available
1906 * memory in the system.
1908 free -= global_page_state(NR_SHMEM);
1910 free += nr_swap_pages;
1913 * Any slabs which are created with the
1914 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1915 * which are reclaimable, under pressure. The dentry
1916 * cache and most inode caches should fall into this
1918 free += global_page_state(NR_SLAB_RECLAIMABLE);
1921 * Leave reserved pages. The pages are not for anonymous pages.
1923 if (free <= totalreserve_pages)
1924 goto error;
1925 else
1926 free -= totalreserve_pages;
1929 * Leave the last 3% for root
1931 if (!cap_sys_admin)
1932 free -= free / 32;
1934 if (free > pages)
1935 return 0;
1937 goto error;
1940 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1942 * Leave the last 3% for root
1944 if (!cap_sys_admin)
1945 allowed -= allowed / 32;
1946 allowed += total_swap_pages;
1948 /* Don't let a single process grow too big:
1949 leave 3% of the size of this process for other processes */
1950 if (mm)
1951 allowed -= mm->total_vm / 32;
1953 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1954 return 0;
1956 error:
1957 vm_unacct_memory(pages);
1959 return -ENOMEM;
1962 int in_gate_area_no_mm(unsigned long addr)
1964 return 0;
1967 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1969 BUG();
1970 return 0;
1972 EXPORT_SYMBOL(filemap_fault);
1974 int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1975 unsigned long size, pgoff_t pgoff)
1977 BUG();
1978 return 0;
1980 EXPORT_SYMBOL(generic_file_remap_pages);
1982 static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1983 unsigned long addr, void *buf, int len, int write)
1985 struct vm_area_struct *vma;
1987 down_read(&mm->mmap_sem);
1989 /* the access must start within one of the target process's mappings */
1990 vma = find_vma(mm, addr);
1991 if (vma) {
1992 /* don't overrun this mapping */
1993 if (addr + len >= vma->vm_end)
1994 len = vma->vm_end - addr;
1996 /* only read or write mappings where it is permitted */
1997 if (write && vma->vm_flags & VM_MAYWRITE)
1998 copy_to_user_page(vma, NULL, addr,
1999 (void *) addr, buf, len);
2000 else if (!write && vma->vm_flags & VM_MAYREAD)
2001 copy_from_user_page(vma, NULL, addr,
2002 buf, (void *) addr, len);
2003 else
2004 len = 0;
2005 } else {
2006 len = 0;
2009 up_read(&mm->mmap_sem);
2011 return len;
2015 * @access_remote_vm - access another process' address space
2016 * @mm: the mm_struct of the target address space
2017 * @addr: start address to access
2018 * @buf: source or destination buffer
2019 * @len: number of bytes to transfer
2020 * @write: whether the access is a write
2022 * The caller must hold a reference on @mm.
2024 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2025 void *buf, int len, int write)
2027 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2031 * Access another process' address space.
2032 * - source/target buffer must be kernel space
2034 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2036 struct mm_struct *mm;
2038 if (addr + len < addr)
2039 return 0;
2041 mm = get_task_mm(tsk);
2042 if (!mm)
2043 return 0;
2045 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2047 mmput(mm);
2048 return len;
2052 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2053 * @inode: The inode to check
2054 * @size: The current filesize of the inode
2055 * @newsize: The proposed filesize of the inode
2057 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2058 * make sure that that any outstanding VMAs aren't broken and then shrink the
2059 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2060 * automatically grant mappings that are too large.
2062 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2063 size_t newsize)
2065 struct vm_area_struct *vma;
2066 struct vm_region *region;
2067 pgoff_t low, high;
2068 size_t r_size, r_top;
2070 low = newsize >> PAGE_SHIFT;
2071 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2073 down_write(&nommu_region_sem);
2074 mutex_lock(&inode->i_mapping->i_mmap_mutex);
2076 /* search for VMAs that fall within the dead zone */
2077 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
2078 /* found one - only interested if it's shared out of the page
2079 * cache */
2080 if (vma->vm_flags & VM_SHARED) {
2081 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2082 up_write(&nommu_region_sem);
2083 return -ETXTBSY; /* not quite true, but near enough */
2087 /* reduce any regions that overlap the dead zone - if in existence,
2088 * these will be pointed to by VMAs that don't overlap the dead zone
2090 * we don't check for any regions that start beyond the EOF as there
2091 * shouldn't be any
2093 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2094 0, ULONG_MAX) {
2095 if (!(vma->vm_flags & VM_SHARED))
2096 continue;
2098 region = vma->vm_region;
2099 r_size = region->vm_top - region->vm_start;
2100 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2102 if (r_top > newsize) {
2103 region->vm_top -= r_top - newsize;
2104 if (region->vm_end > region->vm_top)
2105 region->vm_end = region->vm_top;
2109 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2110 up_write(&nommu_region_sem);
2111 return 0;