Linux 3.12.39
[linux/fpc-iii.git] / mm / nommu.c
blob97d19be3823310be481b6740404d9705bb214be7
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/vmacache.h>
19 #include <linux/mman.h>
20 #include <linux/swap.h>
21 #include <linux/file.h>
22 #include <linux/highmem.h>
23 #include <linux/pagemap.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.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>
33 #include <linux/sched/sysctl.h>
35 #include <asm/uaccess.h>
36 #include <asm/tlb.h>
37 #include <asm/tlbflush.h>
38 #include <asm/mmu_context.h>
39 #include "internal.h"
41 #if 0
42 #define kenter(FMT, ...) \
43 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
44 #define kleave(FMT, ...) \
45 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
46 #define kdebug(FMT, ...) \
47 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
48 #else
49 #define kenter(FMT, ...) \
50 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
51 #define kleave(FMT, ...) \
52 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
53 #define kdebug(FMT, ...) \
54 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
55 #endif
57 void *high_memory;
58 struct page *mem_map;
59 unsigned long max_mapnr;
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 unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
67 unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
68 int heap_stack_gap = 0;
70 atomic_long_t mmap_pages_allocated;
73 * The global memory commitment made in the system can be a metric
74 * that can be used to drive ballooning decisions when Linux is hosted
75 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
76 * balancing memory across competing virtual machines that are hosted.
77 * Several metrics drive this policy engine including the guest reported
78 * memory commitment.
80 unsigned long vm_memory_committed(void)
82 return percpu_counter_read_positive(&vm_committed_as);
85 EXPORT_SYMBOL_GPL(vm_memory_committed);
87 EXPORT_SYMBOL(mem_map);
89 /* list of mapped, potentially shareable regions */
90 static struct kmem_cache *vm_region_jar;
91 struct rb_root nommu_region_tree = RB_ROOT;
92 DECLARE_RWSEM(nommu_region_sem);
94 const struct vm_operations_struct generic_file_vm_ops = {
98 * Return the total memory allocated for this pointer, not
99 * just what the caller asked for.
101 * Doesn't have to be accurate, i.e. may have races.
103 unsigned int kobjsize(const void *objp)
105 struct page *page;
108 * If the object we have should not have ksize performed on it,
109 * return size of 0
111 if (!objp || !virt_addr_valid(objp))
112 return 0;
114 page = virt_to_head_page(objp);
117 * If the allocator sets PageSlab, we know the pointer came from
118 * kmalloc().
120 if (PageSlab(page))
121 return ksize(objp);
124 * If it's not a compound page, see if we have a matching VMA
125 * region. This test is intentionally done in reverse order,
126 * so if there's no VMA, we still fall through and hand back
127 * PAGE_SIZE for 0-order pages.
129 if (!PageCompound(page)) {
130 struct vm_area_struct *vma;
132 vma = find_vma(current->mm, (unsigned long)objp);
133 if (vma)
134 return vma->vm_end - vma->vm_start;
138 * The ksize() function is only guaranteed to work for pointers
139 * returned by kmalloc(). So handle arbitrary pointers here.
141 return PAGE_SIZE << compound_order(page);
144 long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
145 unsigned long start, unsigned long nr_pages,
146 unsigned int foll_flags, struct page **pages,
147 struct vm_area_struct **vmas, int *nonblocking)
149 struct vm_area_struct *vma;
150 unsigned long vm_flags;
151 int i;
153 /* calculate required read or write permissions.
154 * If FOLL_FORCE is set, we only require the "MAY" flags.
156 vm_flags = (foll_flags & FOLL_WRITE) ?
157 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
158 vm_flags &= (foll_flags & FOLL_FORCE) ?
159 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
161 for (i = 0; i < nr_pages; i++) {
162 vma = find_vma(mm, start);
163 if (!vma)
164 goto finish_or_fault;
166 /* protect what we can, including chardevs */
167 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
168 !(vm_flags & vma->vm_flags))
169 goto finish_or_fault;
171 if (pages) {
172 pages[i] = virt_to_page(start);
173 if (pages[i])
174 page_cache_get(pages[i]);
176 if (vmas)
177 vmas[i] = vma;
178 start = (start + PAGE_SIZE) & PAGE_MASK;
181 return i;
183 finish_or_fault:
184 return i ? : -EFAULT;
188 * get a list of pages in an address range belonging to the specified process
189 * and indicate the VMA that covers each page
190 * - this is potentially dodgy as we may end incrementing the page count of a
191 * slab page or a secondary page from a compound page
192 * - don't permit access to VMAs that don't support it, such as I/O mappings
194 long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
195 unsigned long start, unsigned long nr_pages,
196 int write, int force, struct page **pages,
197 struct vm_area_struct **vmas)
199 int flags = 0;
201 if (write)
202 flags |= FOLL_WRITE;
203 if (force)
204 flags |= FOLL_FORCE;
206 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
207 NULL);
209 EXPORT_SYMBOL(get_user_pages);
212 * follow_pfn - look up PFN at a user virtual address
213 * @vma: memory mapping
214 * @address: user virtual address
215 * @pfn: location to store found PFN
217 * Only IO mappings and raw PFN mappings are allowed.
219 * Returns zero and the pfn at @pfn on success, -ve otherwise.
221 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
222 unsigned long *pfn)
224 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
225 return -EINVAL;
227 *pfn = address >> PAGE_SHIFT;
228 return 0;
230 EXPORT_SYMBOL(follow_pfn);
232 LIST_HEAD(vmap_area_list);
234 void vfree(const void *addr)
236 kfree(addr);
238 EXPORT_SYMBOL(vfree);
240 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
243 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
244 * returns only a logical address.
246 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
248 EXPORT_SYMBOL(__vmalloc);
250 void *vmalloc_user(unsigned long size)
252 void *ret;
254 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
255 PAGE_KERNEL);
256 if (ret) {
257 struct vm_area_struct *vma;
259 down_write(&current->mm->mmap_sem);
260 vma = find_vma(current->mm, (unsigned long)ret);
261 if (vma)
262 vma->vm_flags |= VM_USERMAP;
263 up_write(&current->mm->mmap_sem);
266 return ret;
268 EXPORT_SYMBOL(vmalloc_user);
270 struct page *vmalloc_to_page(const void *addr)
272 return virt_to_page(addr);
274 EXPORT_SYMBOL(vmalloc_to_page);
276 unsigned long vmalloc_to_pfn(const void *addr)
278 return page_to_pfn(virt_to_page(addr));
280 EXPORT_SYMBOL(vmalloc_to_pfn);
282 long vread(char *buf, char *addr, unsigned long count)
284 /* Don't allow overflow */
285 if ((unsigned long) buf + count < count)
286 count = -(unsigned long) buf;
288 memcpy(buf, addr, count);
289 return count;
292 long vwrite(char *buf, char *addr, unsigned long count)
294 /* Don't allow overflow */
295 if ((unsigned long) addr + count < count)
296 count = -(unsigned long) addr;
298 memcpy(addr, buf, count);
299 return(count);
303 * vmalloc - allocate virtually continguos memory
305 * @size: allocation size
307 * Allocate enough pages to cover @size from the page level
308 * allocator and map them into continguos kernel virtual space.
310 * For tight control over page level allocator and protection flags
311 * use __vmalloc() instead.
313 void *vmalloc(unsigned long size)
315 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
317 EXPORT_SYMBOL(vmalloc);
320 * vzalloc - allocate virtually continguos memory with zero fill
322 * @size: allocation size
324 * Allocate enough pages to cover @size from the page level
325 * allocator and map them into continguos kernel virtual space.
326 * The memory allocated is set to zero.
328 * For tight control over page level allocator and protection flags
329 * use __vmalloc() instead.
331 void *vzalloc(unsigned long size)
333 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
334 PAGE_KERNEL);
336 EXPORT_SYMBOL(vzalloc);
339 * vmalloc_node - allocate memory on a specific node
340 * @size: allocation size
341 * @node: numa node
343 * Allocate enough pages to cover @size from the page level
344 * allocator and map them into contiguous kernel virtual space.
346 * For tight control over page level allocator and protection flags
347 * use __vmalloc() instead.
349 void *vmalloc_node(unsigned long size, int node)
351 return vmalloc(size);
353 EXPORT_SYMBOL(vmalloc_node);
356 * vzalloc_node - allocate memory on a specific node with zero fill
357 * @size: allocation size
358 * @node: numa node
360 * Allocate enough pages to cover @size from the page level
361 * allocator and map them into contiguous kernel virtual space.
362 * The memory allocated is set to zero.
364 * For tight control over page level allocator and protection flags
365 * use __vmalloc() instead.
367 void *vzalloc_node(unsigned long size, int node)
369 return vzalloc(size);
371 EXPORT_SYMBOL(vzalloc_node);
373 #ifndef PAGE_KERNEL_EXEC
374 # define PAGE_KERNEL_EXEC PAGE_KERNEL
375 #endif
378 * vmalloc_exec - allocate virtually contiguous, executable memory
379 * @size: allocation size
381 * Kernel-internal function to allocate enough pages to cover @size
382 * the page level allocator and map them into contiguous and
383 * executable kernel virtual space.
385 * For tight control over page level allocator and protection flags
386 * use __vmalloc() instead.
389 void *vmalloc_exec(unsigned long size)
391 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
395 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
396 * @size: allocation size
398 * Allocate enough 32bit PA addressable pages to cover @size from the
399 * page level allocator and map them into continguos kernel virtual space.
401 void *vmalloc_32(unsigned long size)
403 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
405 EXPORT_SYMBOL(vmalloc_32);
408 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
409 * @size: allocation size
411 * The resulting memory area is 32bit addressable and zeroed so it can be
412 * mapped to userspace without leaking data.
414 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
415 * remap_vmalloc_range() are permissible.
417 void *vmalloc_32_user(unsigned long size)
420 * We'll have to sort out the ZONE_DMA bits for 64-bit,
421 * but for now this can simply use vmalloc_user() directly.
423 return vmalloc_user(size);
425 EXPORT_SYMBOL(vmalloc_32_user);
427 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
429 BUG();
430 return NULL;
432 EXPORT_SYMBOL(vmap);
434 void vunmap(const void *addr)
436 BUG();
438 EXPORT_SYMBOL(vunmap);
440 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
442 BUG();
443 return NULL;
445 EXPORT_SYMBOL(vm_map_ram);
447 void vm_unmap_ram(const void *mem, unsigned int count)
449 BUG();
451 EXPORT_SYMBOL(vm_unmap_ram);
453 void vm_unmap_aliases(void)
456 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
459 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
460 * have one.
462 void __attribute__((weak)) vmalloc_sync_all(void)
467 * alloc_vm_area - allocate a range of kernel address space
468 * @size: size of the area
470 * Returns: NULL on failure, vm_struct on success
472 * This function reserves a range of kernel address space, and
473 * allocates pagetables to map that range. No actual mappings
474 * are created. If the kernel address space is not shared
475 * between processes, it syncs the pagetable across all
476 * processes.
478 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
480 BUG();
481 return NULL;
483 EXPORT_SYMBOL_GPL(alloc_vm_area);
485 void free_vm_area(struct vm_struct *area)
487 BUG();
489 EXPORT_SYMBOL_GPL(free_vm_area);
491 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
492 struct page *page)
494 return -EINVAL;
496 EXPORT_SYMBOL(vm_insert_page);
499 * sys_brk() for the most part doesn't need the global kernel
500 * lock, except when an application is doing something nasty
501 * like trying to un-brk an area that has already been mapped
502 * to a regular file. in this case, the unmapping will need
503 * to invoke file system routines that need the global lock.
505 SYSCALL_DEFINE1(brk, unsigned long, brk)
507 struct mm_struct *mm = current->mm;
509 if (brk < mm->start_brk || brk > mm->context.end_brk)
510 return mm->brk;
512 if (mm->brk == brk)
513 return mm->brk;
516 * Always allow shrinking brk
518 if (brk <= mm->brk) {
519 mm->brk = brk;
520 return brk;
524 * Ok, looks good - let it rip.
526 flush_icache_range(mm->brk, brk);
527 return mm->brk = brk;
531 * initialise the VMA and region record slabs
533 void __init mmap_init(void)
535 int ret;
537 ret = percpu_counter_init(&vm_committed_as, 0);
538 VM_BUG_ON(ret);
539 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
543 * validate the region tree
544 * - the caller must hold the region lock
546 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
547 static noinline void validate_nommu_regions(void)
549 struct vm_region *region, *last;
550 struct rb_node *p, *lastp;
552 lastp = rb_first(&nommu_region_tree);
553 if (!lastp)
554 return;
556 last = rb_entry(lastp, struct vm_region, vm_rb);
557 BUG_ON(unlikely(last->vm_end <= last->vm_start));
558 BUG_ON(unlikely(last->vm_top < last->vm_end));
560 while ((p = rb_next(lastp))) {
561 region = rb_entry(p, struct vm_region, vm_rb);
562 last = rb_entry(lastp, struct vm_region, vm_rb);
564 BUG_ON(unlikely(region->vm_end <= region->vm_start));
565 BUG_ON(unlikely(region->vm_top < region->vm_end));
566 BUG_ON(unlikely(region->vm_start < last->vm_top));
568 lastp = p;
571 #else
572 static void validate_nommu_regions(void)
575 #endif
578 * add a region into the global tree
580 static void add_nommu_region(struct vm_region *region)
582 struct vm_region *pregion;
583 struct rb_node **p, *parent;
585 validate_nommu_regions();
587 parent = NULL;
588 p = &nommu_region_tree.rb_node;
589 while (*p) {
590 parent = *p;
591 pregion = rb_entry(parent, struct vm_region, vm_rb);
592 if (region->vm_start < pregion->vm_start)
593 p = &(*p)->rb_left;
594 else if (region->vm_start > pregion->vm_start)
595 p = &(*p)->rb_right;
596 else if (pregion == region)
597 return;
598 else
599 BUG();
602 rb_link_node(&region->vm_rb, parent, p);
603 rb_insert_color(&region->vm_rb, &nommu_region_tree);
605 validate_nommu_regions();
609 * delete a region from the global tree
611 static void delete_nommu_region(struct vm_region *region)
613 BUG_ON(!nommu_region_tree.rb_node);
615 validate_nommu_regions();
616 rb_erase(&region->vm_rb, &nommu_region_tree);
617 validate_nommu_regions();
621 * free a contiguous series of pages
623 static void free_page_series(unsigned long from, unsigned long to)
625 for (; from < to; from += PAGE_SIZE) {
626 struct page *page = virt_to_page(from);
628 kdebug("- free %lx", from);
629 atomic_long_dec(&mmap_pages_allocated);
630 if (page_count(page) != 1)
631 kdebug("free page %p: refcount not one: %d",
632 page, page_count(page));
633 put_page(page);
638 * release a reference to a region
639 * - the caller must hold the region semaphore for writing, which this releases
640 * - the region may not have been added to the tree yet, in which case vm_top
641 * will equal vm_start
643 static void __put_nommu_region(struct vm_region *region)
644 __releases(nommu_region_sem)
646 kenter("%p{%d}", region, region->vm_usage);
648 BUG_ON(!nommu_region_tree.rb_node);
650 if (--region->vm_usage == 0) {
651 if (region->vm_top > region->vm_start)
652 delete_nommu_region(region);
653 up_write(&nommu_region_sem);
655 if (region->vm_file)
656 fput(region->vm_file);
658 /* IO memory and memory shared directly out of the pagecache
659 * from ramfs/tmpfs mustn't be released here */
660 if (region->vm_flags & VM_MAPPED_COPY) {
661 kdebug("free series");
662 free_page_series(region->vm_start, region->vm_top);
664 kmem_cache_free(vm_region_jar, region);
665 } else {
666 up_write(&nommu_region_sem);
671 * release a reference to a region
673 static void put_nommu_region(struct vm_region *region)
675 down_write(&nommu_region_sem);
676 __put_nommu_region(region);
680 * update protection on a vma
682 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
684 #ifdef CONFIG_MPU
685 struct mm_struct *mm = vma->vm_mm;
686 long start = vma->vm_start & PAGE_MASK;
687 while (start < vma->vm_end) {
688 protect_page(mm, start, flags);
689 start += PAGE_SIZE;
691 update_protections(mm);
692 #endif
696 * add a VMA into a process's mm_struct in the appropriate place in the list
697 * and tree and add to the address space's page tree also if not an anonymous
698 * page
699 * - should be called with mm->mmap_sem held writelocked
701 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
703 struct vm_area_struct *pvma, *prev;
704 struct address_space *mapping;
705 struct rb_node **p, *parent, *rb_prev;
707 kenter(",%p", vma);
709 BUG_ON(!vma->vm_region);
711 mm->map_count++;
712 vma->vm_mm = mm;
714 protect_vma(vma, vma->vm_flags);
716 /* add the VMA to the mapping */
717 if (vma->vm_file) {
718 mapping = vma->vm_file->f_mapping;
720 mutex_lock(&mapping->i_mmap_mutex);
721 flush_dcache_mmap_lock(mapping);
722 vma_interval_tree_insert(vma, &mapping->i_mmap);
723 flush_dcache_mmap_unlock(mapping);
724 mutex_unlock(&mapping->i_mmap_mutex);
727 /* add the VMA to the tree */
728 parent = rb_prev = NULL;
729 p = &mm->mm_rb.rb_node;
730 while (*p) {
731 parent = *p;
732 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
734 /* sort by: start addr, end addr, VMA struct addr in that order
735 * (the latter is necessary as we may get identical VMAs) */
736 if (vma->vm_start < pvma->vm_start)
737 p = &(*p)->rb_left;
738 else if (vma->vm_start > pvma->vm_start) {
739 rb_prev = parent;
740 p = &(*p)->rb_right;
741 } else if (vma->vm_end < pvma->vm_end)
742 p = &(*p)->rb_left;
743 else if (vma->vm_end > pvma->vm_end) {
744 rb_prev = parent;
745 p = &(*p)->rb_right;
746 } else if (vma < pvma)
747 p = &(*p)->rb_left;
748 else if (vma > pvma) {
749 rb_prev = parent;
750 p = &(*p)->rb_right;
751 } else
752 BUG();
755 rb_link_node(&vma->vm_rb, parent, p);
756 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
758 /* add VMA to the VMA list also */
759 prev = NULL;
760 if (rb_prev)
761 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
763 __vma_link_list(mm, vma, prev, parent);
767 * delete a VMA from its owning mm_struct and address space
769 static void delete_vma_from_mm(struct vm_area_struct *vma)
771 int i;
772 struct address_space *mapping;
773 struct mm_struct *mm = vma->vm_mm;
774 struct task_struct *curr = current;
776 kenter("%p", vma);
778 protect_vma(vma, 0);
780 mm->map_count--;
781 for (i = 0; i < VMACACHE_SIZE; i++) {
782 /* if the vma is cached, invalidate the entire cache */
783 if (curr->vmacache[i] == vma) {
784 vmacache_invalidate(curr->mm);
785 break;
789 /* remove the VMA from the mapping */
790 if (vma->vm_file) {
791 mapping = vma->vm_file->f_mapping;
793 mutex_lock(&mapping->i_mmap_mutex);
794 flush_dcache_mmap_lock(mapping);
795 vma_interval_tree_remove(vma, &mapping->i_mmap);
796 flush_dcache_mmap_unlock(mapping);
797 mutex_unlock(&mapping->i_mmap_mutex);
800 /* remove from the MM's tree and list */
801 rb_erase(&vma->vm_rb, &mm->mm_rb);
803 if (vma->vm_prev)
804 vma->vm_prev->vm_next = vma->vm_next;
805 else
806 mm->mmap = vma->vm_next;
808 if (vma->vm_next)
809 vma->vm_next->vm_prev = vma->vm_prev;
813 * destroy a VMA record
815 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
817 kenter("%p", vma);
818 if (vma->vm_ops && vma->vm_ops->close)
819 vma->vm_ops->close(vma);
820 if (vma->vm_file)
821 fput(vma->vm_file);
822 put_nommu_region(vma->vm_region);
823 kmem_cache_free(vm_area_cachep, vma);
827 * look up the first VMA in which addr resides, NULL if none
828 * - should be called with mm->mmap_sem at least held readlocked
830 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
832 struct vm_area_struct *vma;
834 /* check the cache first */
835 vma = vmacache_find(mm, addr);
836 if (likely(vma))
837 return vma;
839 /* trawl the list (there may be multiple mappings in which addr
840 * resides) */
841 for (vma = mm->mmap; vma; vma = vma->vm_next) {
842 if (vma->vm_start > addr)
843 return NULL;
844 if (vma->vm_end > addr) {
845 vmacache_update(addr, vma);
846 return vma;
850 return NULL;
852 EXPORT_SYMBOL(find_vma);
855 * find a VMA
856 * - we don't extend stack VMAs under NOMMU conditions
858 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
860 return find_vma(mm, addr);
864 * expand a stack to a given address
865 * - not supported under NOMMU conditions
867 int expand_stack(struct vm_area_struct *vma, unsigned long address)
869 return -ENOMEM;
873 * look up the first VMA exactly that exactly matches addr
874 * - should be called with mm->mmap_sem at least held readlocked
876 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
877 unsigned long addr,
878 unsigned long len)
880 struct vm_area_struct *vma;
881 unsigned long end = addr + len;
883 /* check the cache first */
884 vma = vmacache_find_exact(mm, addr, end);
885 if (vma)
886 return vma;
888 /* trawl the list (there may be multiple mappings in which addr
889 * resides) */
890 for (vma = mm->mmap; vma; vma = vma->vm_next) {
891 if (vma->vm_start < addr)
892 continue;
893 if (vma->vm_start > addr)
894 return NULL;
895 if (vma->vm_end == end) {
896 vmacache_update(addr, vma);
897 return vma;
901 return NULL;
905 * determine whether a mapping should be permitted and, if so, what sort of
906 * mapping we're capable of supporting
908 static int validate_mmap_request(struct file *file,
909 unsigned long addr,
910 unsigned long len,
911 unsigned long prot,
912 unsigned long flags,
913 unsigned long pgoff,
914 unsigned long *_capabilities)
916 unsigned long capabilities, rlen;
917 int ret;
919 /* do the simple checks first */
920 if (flags & MAP_FIXED) {
921 printk(KERN_DEBUG
922 "%d: Can't do fixed-address/overlay mmap of RAM\n",
923 current->pid);
924 return -EINVAL;
927 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
928 (flags & MAP_TYPE) != MAP_SHARED)
929 return -EINVAL;
931 if (!len)
932 return -EINVAL;
934 /* Careful about overflows.. */
935 rlen = PAGE_ALIGN(len);
936 if (!rlen || rlen > TASK_SIZE)
937 return -ENOMEM;
939 /* offset overflow? */
940 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
941 return -EOVERFLOW;
943 if (file) {
944 /* validate file mapping requests */
945 struct address_space *mapping;
947 /* files must support mmap */
948 if (!file->f_op || !file->f_op->mmap)
949 return -ENODEV;
951 /* work out if what we've got could possibly be shared
952 * - we support chardevs that provide their own "memory"
953 * - we support files/blockdevs that are memory backed
955 mapping = file->f_mapping;
956 if (!mapping)
957 mapping = file_inode(file)->i_mapping;
959 capabilities = 0;
960 if (mapping && mapping->backing_dev_info)
961 capabilities = mapping->backing_dev_info->capabilities;
963 if (!capabilities) {
964 /* no explicit capabilities set, so assume some
965 * defaults */
966 switch (file_inode(file)->i_mode & S_IFMT) {
967 case S_IFREG:
968 case S_IFBLK:
969 capabilities = BDI_CAP_MAP_COPY;
970 break;
972 case S_IFCHR:
973 capabilities =
974 BDI_CAP_MAP_DIRECT |
975 BDI_CAP_READ_MAP |
976 BDI_CAP_WRITE_MAP;
977 break;
979 default:
980 return -EINVAL;
984 /* eliminate any capabilities that we can't support on this
985 * device */
986 if (!file->f_op->get_unmapped_area)
987 capabilities &= ~BDI_CAP_MAP_DIRECT;
988 if (!file->f_op->read)
989 capabilities &= ~BDI_CAP_MAP_COPY;
991 /* The file shall have been opened with read permission. */
992 if (!(file->f_mode & FMODE_READ))
993 return -EACCES;
995 if (flags & MAP_SHARED) {
996 /* do checks for writing, appending and locking */
997 if ((prot & PROT_WRITE) &&
998 !(file->f_mode & FMODE_WRITE))
999 return -EACCES;
1001 if (IS_APPEND(file_inode(file)) &&
1002 (file->f_mode & FMODE_WRITE))
1003 return -EACCES;
1005 if (locks_verify_locked(file_inode(file)))
1006 return -EAGAIN;
1008 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1009 return -ENODEV;
1011 /* we mustn't privatise shared mappings */
1012 capabilities &= ~BDI_CAP_MAP_COPY;
1014 else {
1015 /* we're going to read the file into private memory we
1016 * allocate */
1017 if (!(capabilities & BDI_CAP_MAP_COPY))
1018 return -ENODEV;
1020 /* we don't permit a private writable mapping to be
1021 * shared with the backing device */
1022 if (prot & PROT_WRITE)
1023 capabilities &= ~BDI_CAP_MAP_DIRECT;
1026 if (capabilities & BDI_CAP_MAP_DIRECT) {
1027 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1028 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1029 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1031 capabilities &= ~BDI_CAP_MAP_DIRECT;
1032 if (flags & MAP_SHARED) {
1033 printk(KERN_WARNING
1034 "MAP_SHARED not completely supported on !MMU\n");
1035 return -EINVAL;
1040 /* handle executable mappings and implied executable
1041 * mappings */
1042 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1043 if (prot & PROT_EXEC)
1044 return -EPERM;
1046 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1047 /* handle implication of PROT_EXEC by PROT_READ */
1048 if (current->personality & READ_IMPLIES_EXEC) {
1049 if (capabilities & BDI_CAP_EXEC_MAP)
1050 prot |= PROT_EXEC;
1053 else if ((prot & PROT_READ) &&
1054 (prot & PROT_EXEC) &&
1055 !(capabilities & BDI_CAP_EXEC_MAP)
1057 /* backing file is not executable, try to copy */
1058 capabilities &= ~BDI_CAP_MAP_DIRECT;
1061 else {
1062 /* anonymous mappings are always memory backed and can be
1063 * privately mapped
1065 capabilities = BDI_CAP_MAP_COPY;
1067 /* handle PROT_EXEC implication by PROT_READ */
1068 if ((prot & PROT_READ) &&
1069 (current->personality & READ_IMPLIES_EXEC))
1070 prot |= PROT_EXEC;
1073 /* allow the security API to have its say */
1074 ret = security_mmap_addr(addr);
1075 if (ret < 0)
1076 return ret;
1078 /* looks okay */
1079 *_capabilities = capabilities;
1080 return 0;
1084 * we've determined that we can make the mapping, now translate what we
1085 * now know into VMA flags
1087 static unsigned long determine_vm_flags(struct file *file,
1088 unsigned long prot,
1089 unsigned long flags,
1090 unsigned long capabilities)
1092 unsigned long vm_flags;
1094 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1095 /* vm_flags |= mm->def_flags; */
1097 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1098 /* attempt to share read-only copies of mapped file chunks */
1099 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1100 if (file && !(prot & PROT_WRITE))
1101 vm_flags |= VM_MAYSHARE;
1102 } else {
1103 /* overlay a shareable mapping on the backing device or inode
1104 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1105 * romfs/cramfs */
1106 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1107 if (flags & MAP_SHARED)
1108 vm_flags |= VM_SHARED;
1111 /* refuse to let anyone share private mappings with this process if
1112 * it's being traced - otherwise breakpoints set in it may interfere
1113 * with another untraced process
1115 if ((flags & MAP_PRIVATE) && current->ptrace)
1116 vm_flags &= ~VM_MAYSHARE;
1118 return vm_flags;
1122 * set up a shared mapping on a file (the driver or filesystem provides and
1123 * pins the storage)
1125 static int do_mmap_shared_file(struct vm_area_struct *vma)
1127 int ret;
1129 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1130 if (ret == 0) {
1131 vma->vm_region->vm_top = vma->vm_region->vm_end;
1132 return 0;
1134 if (ret != -ENOSYS)
1135 return ret;
1137 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1138 * opposed to tried but failed) so we can only give a suitable error as
1139 * it's not possible to make a private copy if MAP_SHARED was given */
1140 return -ENODEV;
1144 * set up a private mapping or an anonymous shared mapping
1146 static int do_mmap_private(struct vm_area_struct *vma,
1147 struct vm_region *region,
1148 unsigned long len,
1149 unsigned long capabilities)
1151 struct page *pages;
1152 unsigned long total, point, n;
1153 void *base;
1154 int ret, order;
1156 /* invoke the file's mapping function so that it can keep track of
1157 * shared mappings on devices or memory
1158 * - VM_MAYSHARE will be set if it may attempt to share
1160 if (capabilities & BDI_CAP_MAP_DIRECT) {
1161 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1162 if (ret == 0) {
1163 /* shouldn't return success if we're not sharing */
1164 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1165 vma->vm_region->vm_top = vma->vm_region->vm_end;
1166 return 0;
1168 if (ret != -ENOSYS)
1169 return ret;
1171 /* getting an ENOSYS error indicates that direct mmap isn't
1172 * possible (as opposed to tried but failed) so we'll try to
1173 * make a private copy of the data and map that instead */
1177 /* allocate some memory to hold the mapping
1178 * - note that this may not return a page-aligned address if the object
1179 * we're allocating is smaller than a page
1181 order = get_order(len);
1182 kdebug("alloc order %d for %lx", order, len);
1184 pages = alloc_pages(GFP_KERNEL, order);
1185 if (!pages)
1186 goto enomem;
1188 total = 1 << order;
1189 atomic_long_add(total, &mmap_pages_allocated);
1191 point = len >> PAGE_SHIFT;
1193 /* we allocated a power-of-2 sized page set, so we may want to trim off
1194 * the excess */
1195 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1196 while (total > point) {
1197 order = ilog2(total - point);
1198 n = 1 << order;
1199 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1200 atomic_long_sub(n, &mmap_pages_allocated);
1201 total -= n;
1202 set_page_refcounted(pages + total);
1203 __free_pages(pages + total, order);
1207 for (point = 1; point < total; point++)
1208 set_page_refcounted(&pages[point]);
1210 base = page_address(pages);
1211 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1212 region->vm_start = (unsigned long) base;
1213 region->vm_end = region->vm_start + len;
1214 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1216 vma->vm_start = region->vm_start;
1217 vma->vm_end = region->vm_start + len;
1219 if (vma->vm_file) {
1220 /* read the contents of a file into the copy */
1221 mm_segment_t old_fs;
1222 loff_t fpos;
1224 fpos = vma->vm_pgoff;
1225 fpos <<= PAGE_SHIFT;
1227 old_fs = get_fs();
1228 set_fs(KERNEL_DS);
1229 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
1230 set_fs(old_fs);
1232 if (ret < 0)
1233 goto error_free;
1235 /* clear the last little bit */
1236 if (ret < len)
1237 memset(base + ret, 0, len - ret);
1241 return 0;
1243 error_free:
1244 free_page_series(region->vm_start, region->vm_top);
1245 region->vm_start = vma->vm_start = 0;
1246 region->vm_end = vma->vm_end = 0;
1247 region->vm_top = 0;
1248 return ret;
1250 enomem:
1251 printk("Allocation of length %lu from process %d (%s) failed\n",
1252 len, current->pid, current->comm);
1253 show_free_areas(0);
1254 return -ENOMEM;
1258 * handle mapping creation for uClinux
1260 unsigned long do_mmap_pgoff(struct file *file,
1261 unsigned long addr,
1262 unsigned long len,
1263 unsigned long prot,
1264 unsigned long flags,
1265 unsigned long pgoff,
1266 unsigned long *populate)
1268 struct vm_area_struct *vma;
1269 struct vm_region *region;
1270 struct rb_node *rb;
1271 unsigned long capabilities, vm_flags, result;
1272 int ret;
1274 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1276 *populate = 0;
1278 /* decide whether we should attempt the mapping, and if so what sort of
1279 * mapping */
1280 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1281 &capabilities);
1282 if (ret < 0) {
1283 kleave(" = %d [val]", ret);
1284 return ret;
1287 /* we ignore the address hint */
1288 addr = 0;
1289 len = PAGE_ALIGN(len);
1291 /* we've determined that we can make the mapping, now translate what we
1292 * now know into VMA flags */
1293 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1295 /* we're going to need to record the mapping */
1296 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1297 if (!region)
1298 goto error_getting_region;
1300 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1301 if (!vma)
1302 goto error_getting_vma;
1304 region->vm_usage = 1;
1305 region->vm_flags = vm_flags;
1306 region->vm_pgoff = pgoff;
1308 INIT_LIST_HEAD(&vma->anon_vma_chain);
1309 vma->vm_flags = vm_flags;
1310 vma->vm_pgoff = pgoff;
1312 if (file) {
1313 region->vm_file = get_file(file);
1314 vma->vm_file = get_file(file);
1317 down_write(&nommu_region_sem);
1319 /* if we want to share, we need to check for regions created by other
1320 * mmap() calls that overlap with our proposed mapping
1321 * - we can only share with a superset match on most regular files
1322 * - shared mappings on character devices and memory backed files are
1323 * permitted to overlap inexactly as far as we are concerned for in
1324 * these cases, sharing is handled in the driver or filesystem rather
1325 * than here
1327 if (vm_flags & VM_MAYSHARE) {
1328 struct vm_region *pregion;
1329 unsigned long pglen, rpglen, pgend, rpgend, start;
1331 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1332 pgend = pgoff + pglen;
1334 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1335 pregion = rb_entry(rb, struct vm_region, vm_rb);
1337 if (!(pregion->vm_flags & VM_MAYSHARE))
1338 continue;
1340 /* search for overlapping mappings on the same file */
1341 if (file_inode(pregion->vm_file) !=
1342 file_inode(file))
1343 continue;
1345 if (pregion->vm_pgoff >= pgend)
1346 continue;
1348 rpglen = pregion->vm_end - pregion->vm_start;
1349 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1350 rpgend = pregion->vm_pgoff + rpglen;
1351 if (pgoff >= rpgend)
1352 continue;
1354 /* handle inexactly overlapping matches between
1355 * mappings */
1356 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1357 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1358 /* new mapping is not a subset of the region */
1359 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1360 goto sharing_violation;
1361 continue;
1364 /* we've found a region we can share */
1365 pregion->vm_usage++;
1366 vma->vm_region = pregion;
1367 start = pregion->vm_start;
1368 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1369 vma->vm_start = start;
1370 vma->vm_end = start + len;
1372 if (pregion->vm_flags & VM_MAPPED_COPY) {
1373 kdebug("share copy");
1374 vma->vm_flags |= VM_MAPPED_COPY;
1375 } else {
1376 kdebug("share mmap");
1377 ret = do_mmap_shared_file(vma);
1378 if (ret < 0) {
1379 vma->vm_region = NULL;
1380 vma->vm_start = 0;
1381 vma->vm_end = 0;
1382 pregion->vm_usage--;
1383 pregion = NULL;
1384 goto error_just_free;
1387 fput(region->vm_file);
1388 kmem_cache_free(vm_region_jar, region);
1389 region = pregion;
1390 result = start;
1391 goto share;
1394 /* obtain the address at which to make a shared mapping
1395 * - this is the hook for quasi-memory character devices to
1396 * tell us the location of a shared mapping
1398 if (capabilities & BDI_CAP_MAP_DIRECT) {
1399 addr = file->f_op->get_unmapped_area(file, addr, len,
1400 pgoff, flags);
1401 if (IS_ERR_VALUE(addr)) {
1402 ret = addr;
1403 if (ret != -ENOSYS)
1404 goto error_just_free;
1406 /* the driver refused to tell us where to site
1407 * the mapping so we'll have to attempt to copy
1408 * it */
1409 ret = -ENODEV;
1410 if (!(capabilities & BDI_CAP_MAP_COPY))
1411 goto error_just_free;
1413 capabilities &= ~BDI_CAP_MAP_DIRECT;
1414 } else {
1415 vma->vm_start = region->vm_start = addr;
1416 vma->vm_end = region->vm_end = addr + len;
1421 vma->vm_region = region;
1423 /* set up the mapping
1424 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1426 if (file && vma->vm_flags & VM_SHARED)
1427 ret = do_mmap_shared_file(vma);
1428 else
1429 ret = do_mmap_private(vma, region, len, capabilities);
1430 if (ret < 0)
1431 goto error_just_free;
1432 add_nommu_region(region);
1434 /* clear anonymous mappings that don't ask for uninitialized data */
1435 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1436 memset((void *)region->vm_start, 0,
1437 region->vm_end - region->vm_start);
1439 /* okay... we have a mapping; now we have to register it */
1440 result = vma->vm_start;
1442 current->mm->total_vm += len >> PAGE_SHIFT;
1444 share:
1445 add_vma_to_mm(current->mm, vma);
1447 /* we flush the region from the icache only when the first executable
1448 * mapping of it is made */
1449 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1450 flush_icache_range(region->vm_start, region->vm_end);
1451 region->vm_icache_flushed = true;
1454 up_write(&nommu_region_sem);
1456 kleave(" = %lx", result);
1457 return result;
1459 error_just_free:
1460 up_write(&nommu_region_sem);
1461 error:
1462 if (region->vm_file)
1463 fput(region->vm_file);
1464 kmem_cache_free(vm_region_jar, region);
1465 if (vma->vm_file)
1466 fput(vma->vm_file);
1467 kmem_cache_free(vm_area_cachep, vma);
1468 kleave(" = %d", ret);
1469 return ret;
1471 sharing_violation:
1472 up_write(&nommu_region_sem);
1473 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1474 ret = -EINVAL;
1475 goto error;
1477 error_getting_vma:
1478 kmem_cache_free(vm_region_jar, region);
1479 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1480 " from process %d failed\n",
1481 len, current->pid);
1482 show_free_areas(0);
1483 return -ENOMEM;
1485 error_getting_region:
1486 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1487 " from process %d failed\n",
1488 len, current->pid);
1489 show_free_areas(0);
1490 return -ENOMEM;
1493 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1494 unsigned long, prot, unsigned long, flags,
1495 unsigned long, fd, unsigned long, pgoff)
1497 struct file *file = NULL;
1498 unsigned long retval = -EBADF;
1500 audit_mmap_fd(fd, flags);
1501 if (!(flags & MAP_ANONYMOUS)) {
1502 file = fget(fd);
1503 if (!file)
1504 goto out;
1507 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1509 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1511 if (file)
1512 fput(file);
1513 out:
1514 return retval;
1517 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1518 struct mmap_arg_struct {
1519 unsigned long addr;
1520 unsigned long len;
1521 unsigned long prot;
1522 unsigned long flags;
1523 unsigned long fd;
1524 unsigned long offset;
1527 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1529 struct mmap_arg_struct a;
1531 if (copy_from_user(&a, arg, sizeof(a)))
1532 return -EFAULT;
1533 if (a.offset & ~PAGE_MASK)
1534 return -EINVAL;
1536 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1537 a.offset >> PAGE_SHIFT);
1539 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1542 * split a vma into two pieces at address 'addr', a new vma is allocated either
1543 * for the first part or the tail.
1545 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1546 unsigned long addr, int new_below)
1548 struct vm_area_struct *new;
1549 struct vm_region *region;
1550 unsigned long npages;
1552 kenter("");
1554 /* we're only permitted to split anonymous regions (these should have
1555 * only a single usage on the region) */
1556 if (vma->vm_file)
1557 return -ENOMEM;
1559 if (mm->map_count >= sysctl_max_map_count)
1560 return -ENOMEM;
1562 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1563 if (!region)
1564 return -ENOMEM;
1566 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1567 if (!new) {
1568 kmem_cache_free(vm_region_jar, region);
1569 return -ENOMEM;
1572 /* most fields are the same, copy all, and then fixup */
1573 *new = *vma;
1574 *region = *vma->vm_region;
1575 new->vm_region = region;
1577 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1579 if (new_below) {
1580 region->vm_top = region->vm_end = new->vm_end = addr;
1581 } else {
1582 region->vm_start = new->vm_start = addr;
1583 region->vm_pgoff = new->vm_pgoff += npages;
1586 if (new->vm_ops && new->vm_ops->open)
1587 new->vm_ops->open(new);
1589 delete_vma_from_mm(vma);
1590 down_write(&nommu_region_sem);
1591 delete_nommu_region(vma->vm_region);
1592 if (new_below) {
1593 vma->vm_region->vm_start = vma->vm_start = addr;
1594 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1595 } else {
1596 vma->vm_region->vm_end = vma->vm_end = addr;
1597 vma->vm_region->vm_top = addr;
1599 add_nommu_region(vma->vm_region);
1600 add_nommu_region(new->vm_region);
1601 up_write(&nommu_region_sem);
1602 add_vma_to_mm(mm, vma);
1603 add_vma_to_mm(mm, new);
1604 return 0;
1608 * shrink a VMA by removing the specified chunk from either the beginning or
1609 * the end
1611 static int shrink_vma(struct mm_struct *mm,
1612 struct vm_area_struct *vma,
1613 unsigned long from, unsigned long to)
1615 struct vm_region *region;
1617 kenter("");
1619 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1620 * and list */
1621 delete_vma_from_mm(vma);
1622 if (from > vma->vm_start)
1623 vma->vm_end = from;
1624 else
1625 vma->vm_start = to;
1626 add_vma_to_mm(mm, vma);
1628 /* cut the backing region down to size */
1629 region = vma->vm_region;
1630 BUG_ON(region->vm_usage != 1);
1632 down_write(&nommu_region_sem);
1633 delete_nommu_region(region);
1634 if (from > region->vm_start) {
1635 to = region->vm_top;
1636 region->vm_top = region->vm_end = from;
1637 } else {
1638 region->vm_start = to;
1640 add_nommu_region(region);
1641 up_write(&nommu_region_sem);
1643 free_page_series(from, to);
1644 return 0;
1648 * release a mapping
1649 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1650 * VMA, though it need not cover the whole VMA
1652 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1654 struct vm_area_struct *vma;
1655 unsigned long end;
1656 int ret;
1658 kenter(",%lx,%zx", start, len);
1660 len = PAGE_ALIGN(len);
1661 if (len == 0)
1662 return -EINVAL;
1664 end = start + len;
1666 /* find the first potentially overlapping VMA */
1667 vma = find_vma(mm, start);
1668 if (!vma) {
1669 static int limit = 0;
1670 if (limit < 5) {
1671 printk(KERN_WARNING
1672 "munmap of memory not mmapped by process %d"
1673 " (%s): 0x%lx-0x%lx\n",
1674 current->pid, current->comm,
1675 start, start + len - 1);
1676 limit++;
1678 return -EINVAL;
1681 /* we're allowed to split an anonymous VMA but not a file-backed one */
1682 if (vma->vm_file) {
1683 do {
1684 if (start > vma->vm_start) {
1685 kleave(" = -EINVAL [miss]");
1686 return -EINVAL;
1688 if (end == vma->vm_end)
1689 goto erase_whole_vma;
1690 vma = vma->vm_next;
1691 } while (vma);
1692 kleave(" = -EINVAL [split file]");
1693 return -EINVAL;
1694 } else {
1695 /* the chunk must be a subset of the VMA found */
1696 if (start == vma->vm_start && end == vma->vm_end)
1697 goto erase_whole_vma;
1698 if (start < vma->vm_start || end > vma->vm_end) {
1699 kleave(" = -EINVAL [superset]");
1700 return -EINVAL;
1702 if (start & ~PAGE_MASK) {
1703 kleave(" = -EINVAL [unaligned start]");
1704 return -EINVAL;
1706 if (end != vma->vm_end && end & ~PAGE_MASK) {
1707 kleave(" = -EINVAL [unaligned split]");
1708 return -EINVAL;
1710 if (start != vma->vm_start && end != vma->vm_end) {
1711 ret = split_vma(mm, vma, start, 1);
1712 if (ret < 0) {
1713 kleave(" = %d [split]", ret);
1714 return ret;
1717 return shrink_vma(mm, vma, start, end);
1720 erase_whole_vma:
1721 delete_vma_from_mm(vma);
1722 delete_vma(mm, vma);
1723 kleave(" = 0");
1724 return 0;
1726 EXPORT_SYMBOL(do_munmap);
1728 int vm_munmap(unsigned long addr, size_t len)
1730 struct mm_struct *mm = current->mm;
1731 int ret;
1733 down_write(&mm->mmap_sem);
1734 ret = do_munmap(mm, addr, len);
1735 up_write(&mm->mmap_sem);
1736 return ret;
1738 EXPORT_SYMBOL(vm_munmap);
1740 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1742 return vm_munmap(addr, len);
1746 * release all the mappings made in a process's VM space
1748 void exit_mmap(struct mm_struct *mm)
1750 struct vm_area_struct *vma;
1752 if (!mm)
1753 return;
1755 kenter("");
1757 mm->total_vm = 0;
1759 while ((vma = mm->mmap)) {
1760 mm->mmap = vma->vm_next;
1761 delete_vma_from_mm(vma);
1762 delete_vma(mm, vma);
1763 cond_resched();
1766 kleave("");
1769 unsigned long vm_brk(unsigned long addr, unsigned long len)
1771 return -ENOMEM;
1775 * expand (or shrink) an existing mapping, potentially moving it at the same
1776 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1778 * under NOMMU conditions, we only permit changing a mapping's size, and only
1779 * as long as it stays within the region allocated by do_mmap_private() and the
1780 * block is not shareable
1782 * MREMAP_FIXED is not supported under NOMMU conditions
1784 static unsigned long do_mremap(unsigned long addr,
1785 unsigned long old_len, unsigned long new_len,
1786 unsigned long flags, unsigned long new_addr)
1788 struct vm_area_struct *vma;
1790 /* insanity checks first */
1791 old_len = PAGE_ALIGN(old_len);
1792 new_len = PAGE_ALIGN(new_len);
1793 if (old_len == 0 || new_len == 0)
1794 return (unsigned long) -EINVAL;
1796 if (addr & ~PAGE_MASK)
1797 return -EINVAL;
1799 if (flags & MREMAP_FIXED && new_addr != addr)
1800 return (unsigned long) -EINVAL;
1802 vma = find_vma_exact(current->mm, addr, old_len);
1803 if (!vma)
1804 return (unsigned long) -EINVAL;
1806 if (vma->vm_end != vma->vm_start + old_len)
1807 return (unsigned long) -EFAULT;
1809 if (vma->vm_flags & VM_MAYSHARE)
1810 return (unsigned long) -EPERM;
1812 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1813 return (unsigned long) -ENOMEM;
1815 /* all checks complete - do it */
1816 vma->vm_end = vma->vm_start + new_len;
1817 return vma->vm_start;
1820 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1821 unsigned long, new_len, unsigned long, flags,
1822 unsigned long, new_addr)
1824 unsigned long ret;
1826 down_write(&current->mm->mmap_sem);
1827 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1828 up_write(&current->mm->mmap_sem);
1829 return ret;
1832 struct page *follow_page_mask(struct vm_area_struct *vma,
1833 unsigned long address, unsigned int flags,
1834 unsigned int *page_mask)
1836 *page_mask = 0;
1837 return NULL;
1840 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1841 unsigned long pfn, unsigned long size, pgprot_t prot)
1843 if (addr != (pfn << PAGE_SHIFT))
1844 return -EINVAL;
1846 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1847 return 0;
1849 EXPORT_SYMBOL(remap_pfn_range);
1851 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1853 unsigned long pfn = start >> PAGE_SHIFT;
1854 unsigned long vm_len = vma->vm_end - vma->vm_start;
1856 pfn += vma->vm_pgoff;
1857 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1859 EXPORT_SYMBOL(vm_iomap_memory);
1861 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1862 unsigned long pgoff)
1864 unsigned int size = vma->vm_end - vma->vm_start;
1866 if (!(vma->vm_flags & VM_USERMAP))
1867 return -EINVAL;
1869 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1870 vma->vm_end = vma->vm_start + size;
1872 return 0;
1874 EXPORT_SYMBOL(remap_vmalloc_range);
1876 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1877 unsigned long len, unsigned long pgoff, unsigned long flags)
1879 return -ENOMEM;
1882 void unmap_mapping_range(struct address_space *mapping,
1883 loff_t const holebegin, loff_t const holelen,
1884 int even_cows)
1887 EXPORT_SYMBOL(unmap_mapping_range);
1890 * Check that a process has enough memory to allocate a new virtual
1891 * mapping. 0 means there is enough memory for the allocation to
1892 * succeed and -ENOMEM implies there is not.
1894 * We currently support three overcommit policies, which are set via the
1895 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1897 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1898 * Additional code 2002 Jul 20 by Robert Love.
1900 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1902 * Note this is a helper function intended to be used by LSMs which
1903 * wish to use this logic.
1905 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1907 long free, allowed, reserve;
1909 vm_acct_memory(pages);
1912 * Sometimes we want to use more memory than we have
1914 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1915 return 0;
1917 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1918 free = global_page_state(NR_FREE_PAGES);
1919 free += global_page_state(NR_FILE_PAGES);
1922 * shmem pages shouldn't be counted as free in this
1923 * case, they can't be purged, only swapped out, and
1924 * that won't affect the overall amount of available
1925 * memory in the system.
1927 free -= global_page_state(NR_SHMEM);
1929 free += get_nr_swap_pages();
1932 * Any slabs which are created with the
1933 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1934 * which are reclaimable, under pressure. The dentry
1935 * cache and most inode caches should fall into this
1937 free += global_page_state(NR_SLAB_RECLAIMABLE);
1940 * Leave reserved pages. The pages are not for anonymous pages.
1942 if (free <= totalreserve_pages)
1943 goto error;
1944 else
1945 free -= totalreserve_pages;
1948 * Reserve some for root
1950 if (!cap_sys_admin)
1951 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1953 if (free > pages)
1954 return 0;
1956 goto error;
1959 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1961 * Reserve some 3% for root
1963 if (!cap_sys_admin)
1964 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1965 allowed += total_swap_pages;
1968 * Don't let a single process grow so big a user can't recover
1970 if (mm) {
1971 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1972 allowed -= min_t(long, mm->total_vm / 32, reserve);
1975 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1976 return 0;
1978 error:
1979 vm_unacct_memory(pages);
1981 return -ENOMEM;
1984 int in_gate_area_no_mm(unsigned long addr)
1986 return 0;
1989 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1991 BUG();
1992 return 0;
1994 EXPORT_SYMBOL(filemap_fault);
1996 int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1997 unsigned long size, pgoff_t pgoff)
1999 BUG();
2000 return 0;
2002 EXPORT_SYMBOL(generic_file_remap_pages);
2004 static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2005 unsigned long addr, void *buf, int len, int write)
2007 struct vm_area_struct *vma;
2009 down_read(&mm->mmap_sem);
2011 /* the access must start within one of the target process's mappings */
2012 vma = find_vma(mm, addr);
2013 if (vma) {
2014 /* don't overrun this mapping */
2015 if (addr + len >= vma->vm_end)
2016 len = vma->vm_end - addr;
2018 /* only read or write mappings where it is permitted */
2019 if (write && vma->vm_flags & VM_MAYWRITE)
2020 copy_to_user_page(vma, NULL, addr,
2021 (void *) addr, buf, len);
2022 else if (!write && vma->vm_flags & VM_MAYREAD)
2023 copy_from_user_page(vma, NULL, addr,
2024 buf, (void *) addr, len);
2025 else
2026 len = 0;
2027 } else {
2028 len = 0;
2031 up_read(&mm->mmap_sem);
2033 return len;
2037 * @access_remote_vm - access another process' address space
2038 * @mm: the mm_struct of the target address space
2039 * @addr: start address to access
2040 * @buf: source or destination buffer
2041 * @len: number of bytes to transfer
2042 * @write: whether the access is a write
2044 * The caller must hold a reference on @mm.
2046 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2047 void *buf, int len, int write)
2049 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2053 * Access another process' address space.
2054 * - source/target buffer must be kernel space
2056 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2058 struct mm_struct *mm;
2060 if (addr + len < addr)
2061 return 0;
2063 mm = get_task_mm(tsk);
2064 if (!mm)
2065 return 0;
2067 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2069 mmput(mm);
2070 return len;
2074 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2075 * @inode: The inode to check
2076 * @size: The current filesize of the inode
2077 * @newsize: The proposed filesize of the inode
2079 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2080 * make sure that that any outstanding VMAs aren't broken and then shrink the
2081 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2082 * automatically grant mappings that are too large.
2084 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2085 size_t newsize)
2087 struct vm_area_struct *vma;
2088 struct vm_region *region;
2089 pgoff_t low, high;
2090 size_t r_size, r_top;
2092 low = newsize >> PAGE_SHIFT;
2093 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2095 down_write(&nommu_region_sem);
2096 mutex_lock(&inode->i_mapping->i_mmap_mutex);
2098 /* search for VMAs that fall within the dead zone */
2099 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
2100 /* found one - only interested if it's shared out of the page
2101 * cache */
2102 if (vma->vm_flags & VM_SHARED) {
2103 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2104 up_write(&nommu_region_sem);
2105 return -ETXTBSY; /* not quite true, but near enough */
2109 /* reduce any regions that overlap the dead zone - if in existence,
2110 * these will be pointed to by VMAs that don't overlap the dead zone
2112 * we don't check for any regions that start beyond the EOF as there
2113 * shouldn't be any
2115 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2116 0, ULONG_MAX) {
2117 if (!(vma->vm_flags & VM_SHARED))
2118 continue;
2120 region = vma->vm_region;
2121 r_size = region->vm_top - region->vm_start;
2122 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2124 if (r_top > newsize) {
2125 region->vm_top -= r_top - newsize;
2126 if (region->vm_end > region->vm_top)
2127 region->vm_end = region->vm_top;
2131 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2132 up_write(&nommu_region_sem);
2133 return 0;
2137 * Initialise sysctl_user_reserve_kbytes.
2139 * This is intended to prevent a user from starting a single memory hogging
2140 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2141 * mode.
2143 * The default value is min(3% of free memory, 128MB)
2144 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2146 static int __meminit init_user_reserve(void)
2148 unsigned long free_kbytes;
2150 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2152 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2153 return 0;
2155 module_init(init_user_reserve)
2158 * Initialise sysctl_admin_reserve_kbytes.
2160 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2161 * to log in and kill a memory hogging process.
2163 * Systems with more than 256MB will reserve 8MB, enough to recover
2164 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2165 * only reserve 3% of free pages by default.
2167 static int __meminit init_admin_reserve(void)
2169 unsigned long free_kbytes;
2171 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2173 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2174 return 0;
2176 module_init(init_admin_reserve)