Btrfs: kill BUG_ON in run_delayed_tree_ref
[linux/fpc-iii.git] / mm / nommu.c
blob2360546db065edaae9cf70491c91b09ce8a5b123
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/export.h>
19 #include <linux/mm.h>
20 #include <linux/vmacache.h>
21 #include <linux/mman.h>
22 #include <linux/swap.h>
23 #include <linux/file.h>
24 #include <linux/highmem.h>
25 #include <linux/pagemap.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/blkdev.h>
29 #include <linux/backing-dev.h>
30 #include <linux/compiler.h>
31 #include <linux/mount.h>
32 #include <linux/personality.h>
33 #include <linux/security.h>
34 #include <linux/syscalls.h>
35 #include <linux/audit.h>
36 #include <linux/sched/sysctl.h>
37 #include <linux/printk.h>
39 #include <asm/uaccess.h>
40 #include <asm/tlb.h>
41 #include <asm/tlbflush.h>
42 #include <asm/mmu_context.h>
43 #include "internal.h"
45 void *high_memory;
46 EXPORT_SYMBOL(high_memory);
47 struct page *mem_map;
48 unsigned long max_mapnr;
49 EXPORT_SYMBOL(max_mapnr);
50 unsigned long highest_memmap_pfn;
51 struct percpu_counter vm_committed_as;
52 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
53 int sysctl_overcommit_ratio = 50; /* default is 50% */
54 unsigned long sysctl_overcommit_kbytes __read_mostly;
55 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
56 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
57 unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
58 unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
59 int heap_stack_gap = 0;
61 atomic_long_t mmap_pages_allocated;
64 * The global memory commitment made in the system can be a metric
65 * that can be used to drive ballooning decisions when Linux is hosted
66 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
67 * balancing memory across competing virtual machines that are hosted.
68 * Several metrics drive this policy engine including the guest reported
69 * memory commitment.
71 unsigned long vm_memory_committed(void)
73 return percpu_counter_read_positive(&vm_committed_as);
76 EXPORT_SYMBOL_GPL(vm_memory_committed);
78 EXPORT_SYMBOL(mem_map);
80 /* list of mapped, potentially shareable regions */
81 static struct kmem_cache *vm_region_jar;
82 struct rb_root nommu_region_tree = RB_ROOT;
83 DECLARE_RWSEM(nommu_region_sem);
85 const struct vm_operations_struct generic_file_vm_ops = {
89 * Return the total memory allocated for this pointer, not
90 * just what the caller asked for.
92 * Doesn't have to be accurate, i.e. may have races.
94 unsigned int kobjsize(const void *objp)
96 struct page *page;
99 * If the object we have should not have ksize performed on it,
100 * return size of 0
102 if (!objp || !virt_addr_valid(objp))
103 return 0;
105 page = virt_to_head_page(objp);
108 * If the allocator sets PageSlab, we know the pointer came from
109 * kmalloc().
111 if (PageSlab(page))
112 return ksize(objp);
115 * If it's not a compound page, see if we have a matching VMA
116 * region. This test is intentionally done in reverse order,
117 * so if there's no VMA, we still fall through and hand back
118 * PAGE_SIZE for 0-order pages.
120 if (!PageCompound(page)) {
121 struct vm_area_struct *vma;
123 vma = find_vma(current->mm, (unsigned long)objp);
124 if (vma)
125 return vma->vm_end - vma->vm_start;
129 * The ksize() function is only guaranteed to work for pointers
130 * returned by kmalloc(). So handle arbitrary pointers here.
132 return PAGE_SIZE << compound_order(page);
135 long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
136 unsigned long start, unsigned long nr_pages,
137 unsigned int foll_flags, struct page **pages,
138 struct vm_area_struct **vmas, int *nonblocking)
140 struct vm_area_struct *vma;
141 unsigned long vm_flags;
142 int i;
144 /* calculate required read or write permissions.
145 * If FOLL_FORCE is set, we only require the "MAY" flags.
147 vm_flags = (foll_flags & FOLL_WRITE) ?
148 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
149 vm_flags &= (foll_flags & FOLL_FORCE) ?
150 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
152 for (i = 0; i < nr_pages; i++) {
153 vma = find_vma(mm, start);
154 if (!vma)
155 goto finish_or_fault;
157 /* protect what we can, including chardevs */
158 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
159 !(vm_flags & vma->vm_flags))
160 goto finish_or_fault;
162 if (pages) {
163 pages[i] = virt_to_page(start);
164 if (pages[i])
165 page_cache_get(pages[i]);
167 if (vmas)
168 vmas[i] = vma;
169 start = (start + PAGE_SIZE) & PAGE_MASK;
172 return i;
174 finish_or_fault:
175 return i ? : -EFAULT;
179 * get a list of pages in an address range belonging to the specified process
180 * and indicate the VMA that covers each page
181 * - this is potentially dodgy as we may end incrementing the page count of a
182 * slab page or a secondary page from a compound page
183 * - don't permit access to VMAs that don't support it, such as I/O mappings
185 long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
186 unsigned long start, unsigned long nr_pages,
187 unsigned int gup_flags, struct page **pages,
188 struct vm_area_struct **vmas)
190 return __get_user_pages(tsk, mm, start, nr_pages,
191 gup_flags, pages, vmas, NULL);
193 EXPORT_SYMBOL(get_user_pages);
195 long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
196 unsigned long start, unsigned long nr_pages,
197 unsigned int gup_flags, struct page **pages,
198 int *locked)
200 return get_user_pages(tsk, mm, start, nr_pages, gup_flags,
201 pages, NULL);
203 EXPORT_SYMBOL(get_user_pages_locked);
205 long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
206 unsigned long start, unsigned long nr_pages,
207 struct page **pages, unsigned int gup_flags)
209 long ret;
210 down_read(&mm->mmap_sem);
211 ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages,
212 NULL, NULL);
213 up_read(&mm->mmap_sem);
214 return ret;
216 EXPORT_SYMBOL(__get_user_pages_unlocked);
218 long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
219 unsigned long start, unsigned long nr_pages,
220 struct page **pages, unsigned int gup_flags)
222 return __get_user_pages_unlocked(tsk, mm, start, nr_pages,
223 pages, gup_flags);
225 EXPORT_SYMBOL(get_user_pages_unlocked);
228 * follow_pfn - look up PFN at a user virtual address
229 * @vma: memory mapping
230 * @address: user virtual address
231 * @pfn: location to store found PFN
233 * Only IO mappings and raw PFN mappings are allowed.
235 * Returns zero and the pfn at @pfn on success, -ve otherwise.
237 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
238 unsigned long *pfn)
240 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
241 return -EINVAL;
243 *pfn = address >> PAGE_SHIFT;
244 return 0;
246 EXPORT_SYMBOL(follow_pfn);
248 LIST_HEAD(vmap_area_list);
250 void vfree(const void *addr)
252 kfree(addr);
254 EXPORT_SYMBOL(vfree);
256 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
259 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
260 * returns only a logical address.
262 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
264 EXPORT_SYMBOL(__vmalloc);
266 void *vmalloc_user(unsigned long size)
268 void *ret;
270 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
271 PAGE_KERNEL);
272 if (ret) {
273 struct vm_area_struct *vma;
275 down_write(&current->mm->mmap_sem);
276 vma = find_vma(current->mm, (unsigned long)ret);
277 if (vma)
278 vma->vm_flags |= VM_USERMAP;
279 up_write(&current->mm->mmap_sem);
282 return ret;
284 EXPORT_SYMBOL(vmalloc_user);
286 struct page *vmalloc_to_page(const void *addr)
288 return virt_to_page(addr);
290 EXPORT_SYMBOL(vmalloc_to_page);
292 unsigned long vmalloc_to_pfn(const void *addr)
294 return page_to_pfn(virt_to_page(addr));
296 EXPORT_SYMBOL(vmalloc_to_pfn);
298 long vread(char *buf, char *addr, unsigned long count)
300 /* Don't allow overflow */
301 if ((unsigned long) buf + count < count)
302 count = -(unsigned long) buf;
304 memcpy(buf, addr, count);
305 return count;
308 long vwrite(char *buf, char *addr, unsigned long count)
310 /* Don't allow overflow */
311 if ((unsigned long) addr + count < count)
312 count = -(unsigned long) addr;
314 memcpy(addr, buf, count);
315 return count;
319 * vmalloc - allocate virtually contiguous memory
321 * @size: allocation size
323 * Allocate enough pages to cover @size from the page level
324 * allocator and map them into contiguous kernel virtual space.
326 * For tight control over page level allocator and protection flags
327 * use __vmalloc() instead.
329 void *vmalloc(unsigned long size)
331 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
333 EXPORT_SYMBOL(vmalloc);
336 * vzalloc - allocate virtually contiguous memory with zero fill
338 * @size: allocation size
340 * Allocate enough pages to cover @size from the page level
341 * allocator and map them into contiguous kernel virtual space.
342 * The memory allocated is set to zero.
344 * For tight control over page level allocator and protection flags
345 * use __vmalloc() instead.
347 void *vzalloc(unsigned long size)
349 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
350 PAGE_KERNEL);
352 EXPORT_SYMBOL(vzalloc);
355 * vmalloc_node - allocate memory on a specific node
356 * @size: allocation size
357 * @node: numa node
359 * Allocate enough pages to cover @size from the page level
360 * allocator and map them into contiguous kernel virtual space.
362 * For tight control over page level allocator and protection flags
363 * use __vmalloc() instead.
365 void *vmalloc_node(unsigned long size, int node)
367 return vmalloc(size);
369 EXPORT_SYMBOL(vmalloc_node);
372 * vzalloc_node - allocate memory on a specific node with zero fill
373 * @size: allocation size
374 * @node: numa node
376 * Allocate enough pages to cover @size from the page level
377 * allocator and map them into contiguous kernel virtual space.
378 * The memory allocated is set to zero.
380 * For tight control over page level allocator and protection flags
381 * use __vmalloc() instead.
383 void *vzalloc_node(unsigned long size, int node)
385 return vzalloc(size);
387 EXPORT_SYMBOL(vzalloc_node);
389 #ifndef PAGE_KERNEL_EXEC
390 # define PAGE_KERNEL_EXEC PAGE_KERNEL
391 #endif
394 * vmalloc_exec - allocate virtually contiguous, executable memory
395 * @size: allocation size
397 * Kernel-internal function to allocate enough pages to cover @size
398 * the page level allocator and map them into contiguous and
399 * executable kernel virtual space.
401 * For tight control over page level allocator and protection flags
402 * use __vmalloc() instead.
405 void *vmalloc_exec(unsigned long size)
407 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
411 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
412 * @size: allocation size
414 * Allocate enough 32bit PA addressable pages to cover @size from the
415 * page level allocator and map them into contiguous kernel virtual space.
417 void *vmalloc_32(unsigned long size)
419 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
421 EXPORT_SYMBOL(vmalloc_32);
424 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
425 * @size: allocation size
427 * The resulting memory area is 32bit addressable and zeroed so it can be
428 * mapped to userspace without leaking data.
430 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
431 * remap_vmalloc_range() are permissible.
433 void *vmalloc_32_user(unsigned long size)
436 * We'll have to sort out the ZONE_DMA bits for 64-bit,
437 * but for now this can simply use vmalloc_user() directly.
439 return vmalloc_user(size);
441 EXPORT_SYMBOL(vmalloc_32_user);
443 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
445 BUG();
446 return NULL;
448 EXPORT_SYMBOL(vmap);
450 void vunmap(const void *addr)
452 BUG();
454 EXPORT_SYMBOL(vunmap);
456 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
458 BUG();
459 return NULL;
461 EXPORT_SYMBOL(vm_map_ram);
463 void vm_unmap_ram(const void *mem, unsigned int count)
465 BUG();
467 EXPORT_SYMBOL(vm_unmap_ram);
469 void vm_unmap_aliases(void)
472 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
475 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
476 * have one.
478 void __weak vmalloc_sync_all(void)
483 * alloc_vm_area - allocate a range of kernel address space
484 * @size: size of the area
486 * Returns: NULL on failure, vm_struct on success
488 * This function reserves a range of kernel address space, and
489 * allocates pagetables to map that range. No actual mappings
490 * are created. If the kernel address space is not shared
491 * between processes, it syncs the pagetable across all
492 * processes.
494 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
496 BUG();
497 return NULL;
499 EXPORT_SYMBOL_GPL(alloc_vm_area);
501 void free_vm_area(struct vm_struct *area)
503 BUG();
505 EXPORT_SYMBOL_GPL(free_vm_area);
507 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
508 struct page *page)
510 return -EINVAL;
512 EXPORT_SYMBOL(vm_insert_page);
515 * sys_brk() for the most part doesn't need the global kernel
516 * lock, except when an application is doing something nasty
517 * like trying to un-brk an area that has already been mapped
518 * to a regular file. in this case, the unmapping will need
519 * to invoke file system routines that need the global lock.
521 SYSCALL_DEFINE1(brk, unsigned long, brk)
523 struct mm_struct *mm = current->mm;
525 if (brk < mm->start_brk || brk > mm->context.end_brk)
526 return mm->brk;
528 if (mm->brk == brk)
529 return mm->brk;
532 * Always allow shrinking brk
534 if (brk <= mm->brk) {
535 mm->brk = brk;
536 return brk;
540 * Ok, looks good - let it rip.
542 flush_icache_range(mm->brk, brk);
543 return mm->brk = brk;
547 * initialise the VMA and region record slabs
549 void __init mmap_init(void)
551 int ret;
553 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
554 VM_BUG_ON(ret);
555 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
559 * validate the region tree
560 * - the caller must hold the region lock
562 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
563 static noinline void validate_nommu_regions(void)
565 struct vm_region *region, *last;
566 struct rb_node *p, *lastp;
568 lastp = rb_first(&nommu_region_tree);
569 if (!lastp)
570 return;
572 last = rb_entry(lastp, struct vm_region, vm_rb);
573 BUG_ON(last->vm_end <= last->vm_start);
574 BUG_ON(last->vm_top < last->vm_end);
576 while ((p = rb_next(lastp))) {
577 region = rb_entry(p, struct vm_region, vm_rb);
578 last = rb_entry(lastp, struct vm_region, vm_rb);
580 BUG_ON(region->vm_end <= region->vm_start);
581 BUG_ON(region->vm_top < region->vm_end);
582 BUG_ON(region->vm_start < last->vm_top);
584 lastp = p;
587 #else
588 static void validate_nommu_regions(void)
591 #endif
594 * add a region into the global tree
596 static void add_nommu_region(struct vm_region *region)
598 struct vm_region *pregion;
599 struct rb_node **p, *parent;
601 validate_nommu_regions();
603 parent = NULL;
604 p = &nommu_region_tree.rb_node;
605 while (*p) {
606 parent = *p;
607 pregion = rb_entry(parent, struct vm_region, vm_rb);
608 if (region->vm_start < pregion->vm_start)
609 p = &(*p)->rb_left;
610 else if (region->vm_start > pregion->vm_start)
611 p = &(*p)->rb_right;
612 else if (pregion == region)
613 return;
614 else
615 BUG();
618 rb_link_node(&region->vm_rb, parent, p);
619 rb_insert_color(&region->vm_rb, &nommu_region_tree);
621 validate_nommu_regions();
625 * delete a region from the global tree
627 static void delete_nommu_region(struct vm_region *region)
629 BUG_ON(!nommu_region_tree.rb_node);
631 validate_nommu_regions();
632 rb_erase(&region->vm_rb, &nommu_region_tree);
633 validate_nommu_regions();
637 * free a contiguous series of pages
639 static void free_page_series(unsigned long from, unsigned long to)
641 for (; from < to; from += PAGE_SIZE) {
642 struct page *page = virt_to_page(from);
644 atomic_long_dec(&mmap_pages_allocated);
645 put_page(page);
650 * release a reference to a region
651 * - the caller must hold the region semaphore for writing, which this releases
652 * - the region may not have been added to the tree yet, in which case vm_top
653 * will equal vm_start
655 static void __put_nommu_region(struct vm_region *region)
656 __releases(nommu_region_sem)
658 BUG_ON(!nommu_region_tree.rb_node);
660 if (--region->vm_usage == 0) {
661 if (region->vm_top > region->vm_start)
662 delete_nommu_region(region);
663 up_write(&nommu_region_sem);
665 if (region->vm_file)
666 fput(region->vm_file);
668 /* IO memory and memory shared directly out of the pagecache
669 * from ramfs/tmpfs mustn't be released here */
670 if (region->vm_flags & VM_MAPPED_COPY)
671 free_page_series(region->vm_start, region->vm_top);
672 kmem_cache_free(vm_region_jar, region);
673 } else {
674 up_write(&nommu_region_sem);
679 * release a reference to a region
681 static void put_nommu_region(struct vm_region *region)
683 down_write(&nommu_region_sem);
684 __put_nommu_region(region);
688 * update protection on a vma
690 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
692 #ifdef CONFIG_MPU
693 struct mm_struct *mm = vma->vm_mm;
694 long start = vma->vm_start & PAGE_MASK;
695 while (start < vma->vm_end) {
696 protect_page(mm, start, flags);
697 start += PAGE_SIZE;
699 update_protections(mm);
700 #endif
704 * add a VMA into a process's mm_struct in the appropriate place in the list
705 * and tree and add to the address space's page tree also if not an anonymous
706 * page
707 * - should be called with mm->mmap_sem held writelocked
709 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
711 struct vm_area_struct *pvma, *prev;
712 struct address_space *mapping;
713 struct rb_node **p, *parent, *rb_prev;
715 BUG_ON(!vma->vm_region);
717 mm->map_count++;
718 vma->vm_mm = mm;
720 protect_vma(vma, vma->vm_flags);
722 /* add the VMA to the mapping */
723 if (vma->vm_file) {
724 mapping = vma->vm_file->f_mapping;
726 i_mmap_lock_write(mapping);
727 flush_dcache_mmap_lock(mapping);
728 vma_interval_tree_insert(vma, &mapping->i_mmap);
729 flush_dcache_mmap_unlock(mapping);
730 i_mmap_unlock_write(mapping);
733 /* add the VMA to the tree */
734 parent = rb_prev = NULL;
735 p = &mm->mm_rb.rb_node;
736 while (*p) {
737 parent = *p;
738 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
740 /* sort by: start addr, end addr, VMA struct addr in that order
741 * (the latter is necessary as we may get identical VMAs) */
742 if (vma->vm_start < pvma->vm_start)
743 p = &(*p)->rb_left;
744 else if (vma->vm_start > pvma->vm_start) {
745 rb_prev = parent;
746 p = &(*p)->rb_right;
747 } else if (vma->vm_end < pvma->vm_end)
748 p = &(*p)->rb_left;
749 else if (vma->vm_end > pvma->vm_end) {
750 rb_prev = parent;
751 p = &(*p)->rb_right;
752 } else if (vma < pvma)
753 p = &(*p)->rb_left;
754 else if (vma > pvma) {
755 rb_prev = parent;
756 p = &(*p)->rb_right;
757 } else
758 BUG();
761 rb_link_node(&vma->vm_rb, parent, p);
762 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
764 /* add VMA to the VMA list also */
765 prev = NULL;
766 if (rb_prev)
767 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
769 __vma_link_list(mm, vma, prev, parent);
773 * delete a VMA from its owning mm_struct and address space
775 static void delete_vma_from_mm(struct vm_area_struct *vma)
777 int i;
778 struct address_space *mapping;
779 struct mm_struct *mm = vma->vm_mm;
780 struct task_struct *curr = current;
782 protect_vma(vma, 0);
784 mm->map_count--;
785 for (i = 0; i < VMACACHE_SIZE; i++) {
786 /* if the vma is cached, invalidate the entire cache */
787 if (curr->vmacache[i] == vma) {
788 vmacache_invalidate(mm);
789 break;
793 /* remove the VMA from the mapping */
794 if (vma->vm_file) {
795 mapping = vma->vm_file->f_mapping;
797 i_mmap_lock_write(mapping);
798 flush_dcache_mmap_lock(mapping);
799 vma_interval_tree_remove(vma, &mapping->i_mmap);
800 flush_dcache_mmap_unlock(mapping);
801 i_mmap_unlock_write(mapping);
804 /* remove from the MM's tree and list */
805 rb_erase(&vma->vm_rb, &mm->mm_rb);
807 if (vma->vm_prev)
808 vma->vm_prev->vm_next = vma->vm_next;
809 else
810 mm->mmap = vma->vm_next;
812 if (vma->vm_next)
813 vma->vm_next->vm_prev = vma->vm_prev;
817 * destroy a VMA record
819 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
821 if (vma->vm_ops && vma->vm_ops->close)
822 vma->vm_ops->close(vma);
823 if (vma->vm_file)
824 fput(vma->vm_file);
825 put_nommu_region(vma->vm_region);
826 kmem_cache_free(vm_area_cachep, vma);
830 * look up the first VMA in which addr resides, NULL if none
831 * - should be called with mm->mmap_sem at least held readlocked
833 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
835 struct vm_area_struct *vma;
837 /* check the cache first */
838 vma = vmacache_find(mm, addr);
839 if (likely(vma))
840 return vma;
842 /* trawl the list (there may be multiple mappings in which addr
843 * resides) */
844 for (vma = mm->mmap; vma; vma = vma->vm_next) {
845 if (vma->vm_start > addr)
846 return NULL;
847 if (vma->vm_end > addr) {
848 vmacache_update(addr, vma);
849 return vma;
853 return NULL;
855 EXPORT_SYMBOL(find_vma);
858 * find a VMA
859 * - we don't extend stack VMAs under NOMMU conditions
861 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
863 return find_vma(mm, addr);
867 * expand a stack to a given address
868 * - not supported under NOMMU conditions
870 int expand_stack(struct vm_area_struct *vma, unsigned long address)
872 return -ENOMEM;
876 * look up the first VMA exactly that exactly matches addr
877 * - should be called with mm->mmap_sem at least held readlocked
879 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
880 unsigned long addr,
881 unsigned long len)
883 struct vm_area_struct *vma;
884 unsigned long end = addr + len;
886 /* check the cache first */
887 vma = vmacache_find_exact(mm, addr, end);
888 if (vma)
889 return vma;
891 /* trawl the list (there may be multiple mappings in which addr
892 * resides) */
893 for (vma = mm->mmap; vma; vma = vma->vm_next) {
894 if (vma->vm_start < addr)
895 continue;
896 if (vma->vm_start > addr)
897 return NULL;
898 if (vma->vm_end == end) {
899 vmacache_update(addr, vma);
900 return vma;
904 return NULL;
908 * determine whether a mapping should be permitted and, if so, what sort of
909 * mapping we're capable of supporting
911 static int validate_mmap_request(struct file *file,
912 unsigned long addr,
913 unsigned long len,
914 unsigned long prot,
915 unsigned long flags,
916 unsigned long pgoff,
917 unsigned long *_capabilities)
919 unsigned long capabilities, rlen;
920 int ret;
922 /* do the simple checks first */
923 if (flags & MAP_FIXED)
924 return -EINVAL;
926 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
927 (flags & MAP_TYPE) != MAP_SHARED)
928 return -EINVAL;
930 if (!len)
931 return -EINVAL;
933 /* Careful about overflows.. */
934 rlen = PAGE_ALIGN(len);
935 if (!rlen || rlen > TASK_SIZE)
936 return -ENOMEM;
938 /* offset overflow? */
939 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
940 return -EOVERFLOW;
942 if (file) {
943 /* files must support mmap */
944 if (!file->f_op->mmap)
945 return -ENODEV;
947 /* work out if what we've got could possibly be shared
948 * - we support chardevs that provide their own "memory"
949 * - we support files/blockdevs that are memory backed
951 if (file->f_op->mmap_capabilities) {
952 capabilities = file->f_op->mmap_capabilities(file);
953 } else {
954 /* no explicit capabilities set, so assume some
955 * defaults */
956 switch (file_inode(file)->i_mode & S_IFMT) {
957 case S_IFREG:
958 case S_IFBLK:
959 capabilities = NOMMU_MAP_COPY;
960 break;
962 case S_IFCHR:
963 capabilities =
964 NOMMU_MAP_DIRECT |
965 NOMMU_MAP_READ |
966 NOMMU_MAP_WRITE;
967 break;
969 default:
970 return -EINVAL;
974 /* eliminate any capabilities that we can't support on this
975 * device */
976 if (!file->f_op->get_unmapped_area)
977 capabilities &= ~NOMMU_MAP_DIRECT;
978 if (!(file->f_mode & FMODE_CAN_READ))
979 capabilities &= ~NOMMU_MAP_COPY;
981 /* The file shall have been opened with read permission. */
982 if (!(file->f_mode & FMODE_READ))
983 return -EACCES;
985 if (flags & MAP_SHARED) {
986 /* do checks for writing, appending and locking */
987 if ((prot & PROT_WRITE) &&
988 !(file->f_mode & FMODE_WRITE))
989 return -EACCES;
991 if (IS_APPEND(file_inode(file)) &&
992 (file->f_mode & FMODE_WRITE))
993 return -EACCES;
995 if (locks_verify_locked(file))
996 return -EAGAIN;
998 if (!(capabilities & NOMMU_MAP_DIRECT))
999 return -ENODEV;
1001 /* we mustn't privatise shared mappings */
1002 capabilities &= ~NOMMU_MAP_COPY;
1003 } else {
1004 /* we're going to read the file into private memory we
1005 * allocate */
1006 if (!(capabilities & NOMMU_MAP_COPY))
1007 return -ENODEV;
1009 /* we don't permit a private writable mapping to be
1010 * shared with the backing device */
1011 if (prot & PROT_WRITE)
1012 capabilities &= ~NOMMU_MAP_DIRECT;
1015 if (capabilities & NOMMU_MAP_DIRECT) {
1016 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
1017 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
1018 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
1020 capabilities &= ~NOMMU_MAP_DIRECT;
1021 if (flags & MAP_SHARED) {
1022 pr_warn("MAP_SHARED not completely supported on !MMU\n");
1023 return -EINVAL;
1028 /* handle executable mappings and implied executable
1029 * mappings */
1030 if (path_noexec(&file->f_path)) {
1031 if (prot & PROT_EXEC)
1032 return -EPERM;
1033 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1034 /* handle implication of PROT_EXEC by PROT_READ */
1035 if (current->personality & READ_IMPLIES_EXEC) {
1036 if (capabilities & NOMMU_MAP_EXEC)
1037 prot |= PROT_EXEC;
1039 } else if ((prot & PROT_READ) &&
1040 (prot & PROT_EXEC) &&
1041 !(capabilities & NOMMU_MAP_EXEC)
1043 /* backing file is not executable, try to copy */
1044 capabilities &= ~NOMMU_MAP_DIRECT;
1046 } else {
1047 /* anonymous mappings are always memory backed and can be
1048 * privately mapped
1050 capabilities = NOMMU_MAP_COPY;
1052 /* handle PROT_EXEC implication by PROT_READ */
1053 if ((prot & PROT_READ) &&
1054 (current->personality & READ_IMPLIES_EXEC))
1055 prot |= PROT_EXEC;
1058 /* allow the security API to have its say */
1059 ret = security_mmap_addr(addr);
1060 if (ret < 0)
1061 return ret;
1063 /* looks okay */
1064 *_capabilities = capabilities;
1065 return 0;
1069 * we've determined that we can make the mapping, now translate what we
1070 * now know into VMA flags
1072 static unsigned long determine_vm_flags(struct file *file,
1073 unsigned long prot,
1074 unsigned long flags,
1075 unsigned long capabilities)
1077 unsigned long vm_flags;
1079 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1080 /* vm_flags |= mm->def_flags; */
1082 if (!(capabilities & NOMMU_MAP_DIRECT)) {
1083 /* attempt to share read-only copies of mapped file chunks */
1084 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1085 if (file && !(prot & PROT_WRITE))
1086 vm_flags |= VM_MAYSHARE;
1087 } else {
1088 /* overlay a shareable mapping on the backing device or inode
1089 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1090 * romfs/cramfs */
1091 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
1092 if (flags & MAP_SHARED)
1093 vm_flags |= VM_SHARED;
1096 /* refuse to let anyone share private mappings with this process if
1097 * it's being traced - otherwise breakpoints set in it may interfere
1098 * with another untraced process
1100 if ((flags & MAP_PRIVATE) && current->ptrace)
1101 vm_flags &= ~VM_MAYSHARE;
1103 return vm_flags;
1107 * set up a shared mapping on a file (the driver or filesystem provides and
1108 * pins the storage)
1110 static int do_mmap_shared_file(struct vm_area_struct *vma)
1112 int ret;
1114 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1115 if (ret == 0) {
1116 vma->vm_region->vm_top = vma->vm_region->vm_end;
1117 return 0;
1119 if (ret != -ENOSYS)
1120 return ret;
1122 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1123 * opposed to tried but failed) so we can only give a suitable error as
1124 * it's not possible to make a private copy if MAP_SHARED was given */
1125 return -ENODEV;
1129 * set up a private mapping or an anonymous shared mapping
1131 static int do_mmap_private(struct vm_area_struct *vma,
1132 struct vm_region *region,
1133 unsigned long len,
1134 unsigned long capabilities)
1136 unsigned long total, point;
1137 void *base;
1138 int ret, order;
1140 /* invoke the file's mapping function so that it can keep track of
1141 * shared mappings on devices or memory
1142 * - VM_MAYSHARE will be set if it may attempt to share
1144 if (capabilities & NOMMU_MAP_DIRECT) {
1145 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1146 if (ret == 0) {
1147 /* shouldn't return success if we're not sharing */
1148 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1149 vma->vm_region->vm_top = vma->vm_region->vm_end;
1150 return 0;
1152 if (ret != -ENOSYS)
1153 return ret;
1155 /* getting an ENOSYS error indicates that direct mmap isn't
1156 * possible (as opposed to tried but failed) so we'll try to
1157 * make a private copy of the data and map that instead */
1161 /* allocate some memory to hold the mapping
1162 * - note that this may not return a page-aligned address if the object
1163 * we're allocating is smaller than a page
1165 order = get_order(len);
1166 total = 1 << order;
1167 point = len >> PAGE_SHIFT;
1169 /* we don't want to allocate a power-of-2 sized page set */
1170 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1171 total = point;
1173 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1174 if (!base)
1175 goto enomem;
1177 atomic_long_add(total, &mmap_pages_allocated);
1179 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1180 region->vm_start = (unsigned long) base;
1181 region->vm_end = region->vm_start + len;
1182 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1184 vma->vm_start = region->vm_start;
1185 vma->vm_end = region->vm_start + len;
1187 if (vma->vm_file) {
1188 /* read the contents of a file into the copy */
1189 mm_segment_t old_fs;
1190 loff_t fpos;
1192 fpos = vma->vm_pgoff;
1193 fpos <<= PAGE_SHIFT;
1195 old_fs = get_fs();
1196 set_fs(KERNEL_DS);
1197 ret = __vfs_read(vma->vm_file, base, len, &fpos);
1198 set_fs(old_fs);
1200 if (ret < 0)
1201 goto error_free;
1203 /* clear the last little bit */
1204 if (ret < len)
1205 memset(base + ret, 0, len - ret);
1209 return 0;
1211 error_free:
1212 free_page_series(region->vm_start, region->vm_top);
1213 region->vm_start = vma->vm_start = 0;
1214 region->vm_end = vma->vm_end = 0;
1215 region->vm_top = 0;
1216 return ret;
1218 enomem:
1219 pr_err("Allocation of length %lu from process %d (%s) failed\n",
1220 len, current->pid, current->comm);
1221 show_free_areas(0);
1222 return -ENOMEM;
1226 * handle mapping creation for uClinux
1228 unsigned long do_mmap(struct file *file,
1229 unsigned long addr,
1230 unsigned long len,
1231 unsigned long prot,
1232 unsigned long flags,
1233 vm_flags_t vm_flags,
1234 unsigned long pgoff,
1235 unsigned long *populate)
1237 struct vm_area_struct *vma;
1238 struct vm_region *region;
1239 struct rb_node *rb;
1240 unsigned long capabilities, result;
1241 int ret;
1243 *populate = 0;
1245 /* decide whether we should attempt the mapping, and if so what sort of
1246 * mapping */
1247 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1248 &capabilities);
1249 if (ret < 0)
1250 return ret;
1252 /* we ignore the address hint */
1253 addr = 0;
1254 len = PAGE_ALIGN(len);
1256 /* we've determined that we can make the mapping, now translate what we
1257 * now know into VMA flags */
1258 vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
1260 /* we're going to need to record the mapping */
1261 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1262 if (!region)
1263 goto error_getting_region;
1265 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1266 if (!vma)
1267 goto error_getting_vma;
1269 region->vm_usage = 1;
1270 region->vm_flags = vm_flags;
1271 region->vm_pgoff = pgoff;
1273 INIT_LIST_HEAD(&vma->anon_vma_chain);
1274 vma->vm_flags = vm_flags;
1275 vma->vm_pgoff = pgoff;
1277 if (file) {
1278 region->vm_file = get_file(file);
1279 vma->vm_file = get_file(file);
1282 down_write(&nommu_region_sem);
1284 /* if we want to share, we need to check for regions created by other
1285 * mmap() calls that overlap with our proposed mapping
1286 * - we can only share with a superset match on most regular files
1287 * - shared mappings on character devices and memory backed files are
1288 * permitted to overlap inexactly as far as we are concerned for in
1289 * these cases, sharing is handled in the driver or filesystem rather
1290 * than here
1292 if (vm_flags & VM_MAYSHARE) {
1293 struct vm_region *pregion;
1294 unsigned long pglen, rpglen, pgend, rpgend, start;
1296 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1297 pgend = pgoff + pglen;
1299 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1300 pregion = rb_entry(rb, struct vm_region, vm_rb);
1302 if (!(pregion->vm_flags & VM_MAYSHARE))
1303 continue;
1305 /* search for overlapping mappings on the same file */
1306 if (file_inode(pregion->vm_file) !=
1307 file_inode(file))
1308 continue;
1310 if (pregion->vm_pgoff >= pgend)
1311 continue;
1313 rpglen = pregion->vm_end - pregion->vm_start;
1314 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1315 rpgend = pregion->vm_pgoff + rpglen;
1316 if (pgoff >= rpgend)
1317 continue;
1319 /* handle inexactly overlapping matches between
1320 * mappings */
1321 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1322 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1323 /* new mapping is not a subset of the region */
1324 if (!(capabilities & NOMMU_MAP_DIRECT))
1325 goto sharing_violation;
1326 continue;
1329 /* we've found a region we can share */
1330 pregion->vm_usage++;
1331 vma->vm_region = pregion;
1332 start = pregion->vm_start;
1333 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1334 vma->vm_start = start;
1335 vma->vm_end = start + len;
1337 if (pregion->vm_flags & VM_MAPPED_COPY)
1338 vma->vm_flags |= VM_MAPPED_COPY;
1339 else {
1340 ret = do_mmap_shared_file(vma);
1341 if (ret < 0) {
1342 vma->vm_region = NULL;
1343 vma->vm_start = 0;
1344 vma->vm_end = 0;
1345 pregion->vm_usage--;
1346 pregion = NULL;
1347 goto error_just_free;
1350 fput(region->vm_file);
1351 kmem_cache_free(vm_region_jar, region);
1352 region = pregion;
1353 result = start;
1354 goto share;
1357 /* obtain the address at which to make a shared mapping
1358 * - this is the hook for quasi-memory character devices to
1359 * tell us the location of a shared mapping
1361 if (capabilities & NOMMU_MAP_DIRECT) {
1362 addr = file->f_op->get_unmapped_area(file, addr, len,
1363 pgoff, flags);
1364 if (IS_ERR_VALUE(addr)) {
1365 ret = addr;
1366 if (ret != -ENOSYS)
1367 goto error_just_free;
1369 /* the driver refused to tell us where to site
1370 * the mapping so we'll have to attempt to copy
1371 * it */
1372 ret = -ENODEV;
1373 if (!(capabilities & NOMMU_MAP_COPY))
1374 goto error_just_free;
1376 capabilities &= ~NOMMU_MAP_DIRECT;
1377 } else {
1378 vma->vm_start = region->vm_start = addr;
1379 vma->vm_end = region->vm_end = addr + len;
1384 vma->vm_region = region;
1386 /* set up the mapping
1387 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1389 if (file && vma->vm_flags & VM_SHARED)
1390 ret = do_mmap_shared_file(vma);
1391 else
1392 ret = do_mmap_private(vma, region, len, capabilities);
1393 if (ret < 0)
1394 goto error_just_free;
1395 add_nommu_region(region);
1397 /* clear anonymous mappings that don't ask for uninitialized data */
1398 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1399 memset((void *)region->vm_start, 0,
1400 region->vm_end - region->vm_start);
1402 /* okay... we have a mapping; now we have to register it */
1403 result = vma->vm_start;
1405 current->mm->total_vm += len >> PAGE_SHIFT;
1407 share:
1408 add_vma_to_mm(current->mm, vma);
1410 /* we flush the region from the icache only when the first executable
1411 * mapping of it is made */
1412 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1413 flush_icache_range(region->vm_start, region->vm_end);
1414 region->vm_icache_flushed = true;
1417 up_write(&nommu_region_sem);
1419 return result;
1421 error_just_free:
1422 up_write(&nommu_region_sem);
1423 error:
1424 if (region->vm_file)
1425 fput(region->vm_file);
1426 kmem_cache_free(vm_region_jar, region);
1427 if (vma->vm_file)
1428 fput(vma->vm_file);
1429 kmem_cache_free(vm_area_cachep, vma);
1430 return ret;
1432 sharing_violation:
1433 up_write(&nommu_region_sem);
1434 pr_warn("Attempt to share mismatched mappings\n");
1435 ret = -EINVAL;
1436 goto error;
1438 error_getting_vma:
1439 kmem_cache_free(vm_region_jar, region);
1440 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1441 len, current->pid);
1442 show_free_areas(0);
1443 return -ENOMEM;
1445 error_getting_region:
1446 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1447 len, current->pid);
1448 show_free_areas(0);
1449 return -ENOMEM;
1452 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1453 unsigned long, prot, unsigned long, flags,
1454 unsigned long, fd, unsigned long, pgoff)
1456 struct file *file = NULL;
1457 unsigned long retval = -EBADF;
1459 audit_mmap_fd(fd, flags);
1460 if (!(flags & MAP_ANONYMOUS)) {
1461 file = fget(fd);
1462 if (!file)
1463 goto out;
1466 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1468 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1470 if (file)
1471 fput(file);
1472 out:
1473 return retval;
1476 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1477 struct mmap_arg_struct {
1478 unsigned long addr;
1479 unsigned long len;
1480 unsigned long prot;
1481 unsigned long flags;
1482 unsigned long fd;
1483 unsigned long offset;
1486 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1488 struct mmap_arg_struct a;
1490 if (copy_from_user(&a, arg, sizeof(a)))
1491 return -EFAULT;
1492 if (offset_in_page(a.offset))
1493 return -EINVAL;
1495 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1496 a.offset >> PAGE_SHIFT);
1498 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1501 * split a vma into two pieces at address 'addr', a new vma is allocated either
1502 * for the first part or the tail.
1504 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1505 unsigned long addr, int new_below)
1507 struct vm_area_struct *new;
1508 struct vm_region *region;
1509 unsigned long npages;
1511 /* we're only permitted to split anonymous regions (these should have
1512 * only a single usage on the region) */
1513 if (vma->vm_file)
1514 return -ENOMEM;
1516 if (mm->map_count >= sysctl_max_map_count)
1517 return -ENOMEM;
1519 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1520 if (!region)
1521 return -ENOMEM;
1523 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1524 if (!new) {
1525 kmem_cache_free(vm_region_jar, region);
1526 return -ENOMEM;
1529 /* most fields are the same, copy all, and then fixup */
1530 *new = *vma;
1531 *region = *vma->vm_region;
1532 new->vm_region = region;
1534 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1536 if (new_below) {
1537 region->vm_top = region->vm_end = new->vm_end = addr;
1538 } else {
1539 region->vm_start = new->vm_start = addr;
1540 region->vm_pgoff = new->vm_pgoff += npages;
1543 if (new->vm_ops && new->vm_ops->open)
1544 new->vm_ops->open(new);
1546 delete_vma_from_mm(vma);
1547 down_write(&nommu_region_sem);
1548 delete_nommu_region(vma->vm_region);
1549 if (new_below) {
1550 vma->vm_region->vm_start = vma->vm_start = addr;
1551 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1552 } else {
1553 vma->vm_region->vm_end = vma->vm_end = addr;
1554 vma->vm_region->vm_top = addr;
1556 add_nommu_region(vma->vm_region);
1557 add_nommu_region(new->vm_region);
1558 up_write(&nommu_region_sem);
1559 add_vma_to_mm(mm, vma);
1560 add_vma_to_mm(mm, new);
1561 return 0;
1565 * shrink a VMA by removing the specified chunk from either the beginning or
1566 * the end
1568 static int shrink_vma(struct mm_struct *mm,
1569 struct vm_area_struct *vma,
1570 unsigned long from, unsigned long to)
1572 struct vm_region *region;
1574 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1575 * and list */
1576 delete_vma_from_mm(vma);
1577 if (from > vma->vm_start)
1578 vma->vm_end = from;
1579 else
1580 vma->vm_start = to;
1581 add_vma_to_mm(mm, vma);
1583 /* cut the backing region down to size */
1584 region = vma->vm_region;
1585 BUG_ON(region->vm_usage != 1);
1587 down_write(&nommu_region_sem);
1588 delete_nommu_region(region);
1589 if (from > region->vm_start) {
1590 to = region->vm_top;
1591 region->vm_top = region->vm_end = from;
1592 } else {
1593 region->vm_start = to;
1595 add_nommu_region(region);
1596 up_write(&nommu_region_sem);
1598 free_page_series(from, to);
1599 return 0;
1603 * release a mapping
1604 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1605 * VMA, though it need not cover the whole VMA
1607 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1609 struct vm_area_struct *vma;
1610 unsigned long end;
1611 int ret;
1613 len = PAGE_ALIGN(len);
1614 if (len == 0)
1615 return -EINVAL;
1617 end = start + len;
1619 /* find the first potentially overlapping VMA */
1620 vma = find_vma(mm, start);
1621 if (!vma) {
1622 static int limit;
1623 if (limit < 5) {
1624 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1625 current->pid, current->comm,
1626 start, start + len - 1);
1627 limit++;
1629 return -EINVAL;
1632 /* we're allowed to split an anonymous VMA but not a file-backed one */
1633 if (vma->vm_file) {
1634 do {
1635 if (start > vma->vm_start)
1636 return -EINVAL;
1637 if (end == vma->vm_end)
1638 goto erase_whole_vma;
1639 vma = vma->vm_next;
1640 } while (vma);
1641 return -EINVAL;
1642 } else {
1643 /* the chunk must be a subset of the VMA found */
1644 if (start == vma->vm_start && end == vma->vm_end)
1645 goto erase_whole_vma;
1646 if (start < vma->vm_start || end > vma->vm_end)
1647 return -EINVAL;
1648 if (offset_in_page(start))
1649 return -EINVAL;
1650 if (end != vma->vm_end && offset_in_page(end))
1651 return -EINVAL;
1652 if (start != vma->vm_start && end != vma->vm_end) {
1653 ret = split_vma(mm, vma, start, 1);
1654 if (ret < 0)
1655 return ret;
1657 return shrink_vma(mm, vma, start, end);
1660 erase_whole_vma:
1661 delete_vma_from_mm(vma);
1662 delete_vma(mm, vma);
1663 return 0;
1665 EXPORT_SYMBOL(do_munmap);
1667 int vm_munmap(unsigned long addr, size_t len)
1669 struct mm_struct *mm = current->mm;
1670 int ret;
1672 down_write(&mm->mmap_sem);
1673 ret = do_munmap(mm, addr, len);
1674 up_write(&mm->mmap_sem);
1675 return ret;
1677 EXPORT_SYMBOL(vm_munmap);
1679 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1681 return vm_munmap(addr, len);
1685 * release all the mappings made in a process's VM space
1687 void exit_mmap(struct mm_struct *mm)
1689 struct vm_area_struct *vma;
1691 if (!mm)
1692 return;
1694 mm->total_vm = 0;
1696 while ((vma = mm->mmap)) {
1697 mm->mmap = vma->vm_next;
1698 delete_vma_from_mm(vma);
1699 delete_vma(mm, vma);
1700 cond_resched();
1704 unsigned long vm_brk(unsigned long addr, unsigned long len)
1706 return -ENOMEM;
1710 * expand (or shrink) an existing mapping, potentially moving it at the same
1711 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1713 * under NOMMU conditions, we only permit changing a mapping's size, and only
1714 * as long as it stays within the region allocated by do_mmap_private() and the
1715 * block is not shareable
1717 * MREMAP_FIXED is not supported under NOMMU conditions
1719 static unsigned long do_mremap(unsigned long addr,
1720 unsigned long old_len, unsigned long new_len,
1721 unsigned long flags, unsigned long new_addr)
1723 struct vm_area_struct *vma;
1725 /* insanity checks first */
1726 old_len = PAGE_ALIGN(old_len);
1727 new_len = PAGE_ALIGN(new_len);
1728 if (old_len == 0 || new_len == 0)
1729 return (unsigned long) -EINVAL;
1731 if (offset_in_page(addr))
1732 return -EINVAL;
1734 if (flags & MREMAP_FIXED && new_addr != addr)
1735 return (unsigned long) -EINVAL;
1737 vma = find_vma_exact(current->mm, addr, old_len);
1738 if (!vma)
1739 return (unsigned long) -EINVAL;
1741 if (vma->vm_end != vma->vm_start + old_len)
1742 return (unsigned long) -EFAULT;
1744 if (vma->vm_flags & VM_MAYSHARE)
1745 return (unsigned long) -EPERM;
1747 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1748 return (unsigned long) -ENOMEM;
1750 /* all checks complete - do it */
1751 vma->vm_end = vma->vm_start + new_len;
1752 return vma->vm_start;
1755 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1756 unsigned long, new_len, unsigned long, flags,
1757 unsigned long, new_addr)
1759 unsigned long ret;
1761 down_write(&current->mm->mmap_sem);
1762 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1763 up_write(&current->mm->mmap_sem);
1764 return ret;
1767 struct page *follow_page_mask(struct vm_area_struct *vma,
1768 unsigned long address, unsigned int flags,
1769 unsigned int *page_mask)
1771 *page_mask = 0;
1772 return NULL;
1775 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1776 unsigned long pfn, unsigned long size, pgprot_t prot)
1778 if (addr != (pfn << PAGE_SHIFT))
1779 return -EINVAL;
1781 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1782 return 0;
1784 EXPORT_SYMBOL(remap_pfn_range);
1786 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1788 unsigned long pfn = start >> PAGE_SHIFT;
1789 unsigned long vm_len = vma->vm_end - vma->vm_start;
1791 pfn += vma->vm_pgoff;
1792 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1794 EXPORT_SYMBOL(vm_iomap_memory);
1796 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1797 unsigned long pgoff)
1799 unsigned int size = vma->vm_end - vma->vm_start;
1801 if (!(vma->vm_flags & VM_USERMAP))
1802 return -EINVAL;
1804 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1805 vma->vm_end = vma->vm_start + size;
1807 return 0;
1809 EXPORT_SYMBOL(remap_vmalloc_range);
1811 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1812 unsigned long len, unsigned long pgoff, unsigned long flags)
1814 return -ENOMEM;
1817 void unmap_mapping_range(struct address_space *mapping,
1818 loff_t const holebegin, loff_t const holelen,
1819 int even_cows)
1822 EXPORT_SYMBOL(unmap_mapping_range);
1825 * Check that a process has enough memory to allocate a new virtual
1826 * mapping. 0 means there is enough memory for the allocation to
1827 * succeed and -ENOMEM implies there is not.
1829 * We currently support three overcommit policies, which are set via the
1830 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1832 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1833 * Additional code 2002 Jul 20 by Robert Love.
1835 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1837 * Note this is a helper function intended to be used by LSMs which
1838 * wish to use this logic.
1840 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1842 long free, allowed, reserve;
1844 vm_acct_memory(pages);
1847 * Sometimes we want to use more memory than we have
1849 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1850 return 0;
1852 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1853 free = global_page_state(NR_FREE_PAGES);
1854 free += global_page_state(NR_FILE_PAGES);
1857 * shmem pages shouldn't be counted as free in this
1858 * case, they can't be purged, only swapped out, and
1859 * that won't affect the overall amount of available
1860 * memory in the system.
1862 free -= global_page_state(NR_SHMEM);
1864 free += get_nr_swap_pages();
1867 * Any slabs which are created with the
1868 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1869 * which are reclaimable, under pressure. The dentry
1870 * cache and most inode caches should fall into this
1872 free += global_page_state(NR_SLAB_RECLAIMABLE);
1875 * Leave reserved pages. The pages are not for anonymous pages.
1877 if (free <= totalreserve_pages)
1878 goto error;
1879 else
1880 free -= totalreserve_pages;
1883 * Reserve some for root
1885 if (!cap_sys_admin)
1886 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1888 if (free > pages)
1889 return 0;
1891 goto error;
1894 allowed = vm_commit_limit();
1896 * Reserve some 3% for root
1898 if (!cap_sys_admin)
1899 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1902 * Don't let a single process grow so big a user can't recover
1904 if (mm) {
1905 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1906 allowed -= min_t(long, mm->total_vm / 32, reserve);
1909 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1910 return 0;
1912 error:
1913 vm_unacct_memory(pages);
1915 return -ENOMEM;
1918 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1920 BUG();
1921 return 0;
1923 EXPORT_SYMBOL(filemap_fault);
1925 void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
1927 BUG();
1929 EXPORT_SYMBOL(filemap_map_pages);
1931 static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1932 unsigned long addr, void *buf, int len, unsigned int gup_flags)
1934 struct vm_area_struct *vma;
1935 int write = gup_flags & FOLL_WRITE;
1937 down_read(&mm->mmap_sem);
1939 /* the access must start within one of the target process's mappings */
1940 vma = find_vma(mm, addr);
1941 if (vma) {
1942 /* don't overrun this mapping */
1943 if (addr + len >= vma->vm_end)
1944 len = vma->vm_end - addr;
1946 /* only read or write mappings where it is permitted */
1947 if (write && vma->vm_flags & VM_MAYWRITE)
1948 copy_to_user_page(vma, NULL, addr,
1949 (void *) addr, buf, len);
1950 else if (!write && vma->vm_flags & VM_MAYREAD)
1951 copy_from_user_page(vma, NULL, addr,
1952 buf, (void *) addr, len);
1953 else
1954 len = 0;
1955 } else {
1956 len = 0;
1959 up_read(&mm->mmap_sem);
1961 return len;
1965 * @access_remote_vm - access another process' address space
1966 * @mm: the mm_struct of the target address space
1967 * @addr: start address to access
1968 * @buf: source or destination buffer
1969 * @len: number of bytes to transfer
1970 * @gup_flags: flags modifying lookup behaviour
1972 * The caller must hold a reference on @mm.
1974 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
1975 void *buf, int len, unsigned int gup_flags)
1977 return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
1981 * Access another process' address space.
1982 * - source/target buffer must be kernel space
1984 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1986 struct mm_struct *mm;
1988 if (addr + len < addr)
1989 return 0;
1991 mm = get_task_mm(tsk);
1992 if (!mm)
1993 return 0;
1995 len = __access_remote_vm(tsk, mm, addr, buf, len,
1996 write ? FOLL_WRITE : 0);
1998 mmput(mm);
1999 return len;
2003 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2004 * @inode: The inode to check
2005 * @size: The current filesize of the inode
2006 * @newsize: The proposed filesize of the inode
2008 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2009 * make sure that that any outstanding VMAs aren't broken and then shrink the
2010 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2011 * automatically grant mappings that are too large.
2013 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2014 size_t newsize)
2016 struct vm_area_struct *vma;
2017 struct vm_region *region;
2018 pgoff_t low, high;
2019 size_t r_size, r_top;
2021 low = newsize >> PAGE_SHIFT;
2022 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2024 down_write(&nommu_region_sem);
2025 i_mmap_lock_read(inode->i_mapping);
2027 /* search for VMAs that fall within the dead zone */
2028 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
2029 /* found one - only interested if it's shared out of the page
2030 * cache */
2031 if (vma->vm_flags & VM_SHARED) {
2032 i_mmap_unlock_read(inode->i_mapping);
2033 up_write(&nommu_region_sem);
2034 return -ETXTBSY; /* not quite true, but near enough */
2038 /* reduce any regions that overlap the dead zone - if in existence,
2039 * these will be pointed to by VMAs that don't overlap the dead zone
2041 * we don't check for any regions that start beyond the EOF as there
2042 * shouldn't be any
2044 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
2045 if (!(vma->vm_flags & VM_SHARED))
2046 continue;
2048 region = vma->vm_region;
2049 r_size = region->vm_top - region->vm_start;
2050 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2052 if (r_top > newsize) {
2053 region->vm_top -= r_top - newsize;
2054 if (region->vm_end > region->vm_top)
2055 region->vm_end = region->vm_top;
2059 i_mmap_unlock_read(inode->i_mapping);
2060 up_write(&nommu_region_sem);
2061 return 0;
2065 * Initialise sysctl_user_reserve_kbytes.
2067 * This is intended to prevent a user from starting a single memory hogging
2068 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2069 * mode.
2071 * The default value is min(3% of free memory, 128MB)
2072 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2074 static int __meminit init_user_reserve(void)
2076 unsigned long free_kbytes;
2078 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2080 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2081 return 0;
2083 subsys_initcall(init_user_reserve);
2086 * Initialise sysctl_admin_reserve_kbytes.
2088 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2089 * to log in and kill a memory hogging process.
2091 * Systems with more than 256MB will reserve 8MB, enough to recover
2092 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2093 * only reserve 3% of free pages by default.
2095 static int __meminit init_admin_reserve(void)
2097 unsigned long free_kbytes;
2099 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2101 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2102 return 0;
2104 subsys_initcall(init_admin_reserve);