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>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/blkdev.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mount.h>
28 #include <linux/personality.h>
29 #include <linux/security.h>
30 #include <linux/syscalls.h>
31 #include <linux/audit.h>
33 #include <asm/uaccess.h>
35 #include <asm/tlbflush.h>
36 #include <asm/mmu_context.h>
40 #define kenter(FMT, ...) \
41 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
42 #define kleave(FMT, ...) \
43 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
44 #define kdebug(FMT, ...) \
45 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47 #define kenter(FMT, ...) \
48 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
49 #define kleave(FMT, ...) \
50 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
51 #define kdebug(FMT, ...) \
52 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
57 unsigned long max_mapnr;
58 unsigned long num_physpages;
59 unsigned long highest_memmap_pfn;
60 struct percpu_counter vm_committed_as;
61 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
62 int sysctl_overcommit_ratio = 50; /* default is 50% */
63 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
64 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
65 int heap_stack_gap = 0;
67 atomic_long_t mmap_pages_allocated;
70 * The global memory commitment made in the system can be a metric
71 * that can be used to drive ballooning decisions when Linux is hosted
72 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
73 * balancing memory across competing virtual machines that are hosted.
74 * Several metrics drive this policy engine including the guest reported
77 unsigned long vm_memory_committed(void)
79 return percpu_counter_read_positive(&vm_committed_as);
82 EXPORT_SYMBOL_GPL(vm_memory_committed);
84 EXPORT_SYMBOL(mem_map);
85 EXPORT_SYMBOL(num_physpages);
87 /* list of mapped, potentially shareable regions */
88 static struct kmem_cache *vm_region_jar;
89 struct rb_root nommu_region_tree = RB_ROOT;
90 DECLARE_RWSEM(nommu_region_sem);
92 const struct vm_operations_struct generic_file_vm_ops = {
96 * Return the total memory allocated for this pointer, not
97 * just what the caller asked for.
99 * Doesn't have to be accurate, i.e. may have races.
101 unsigned int kobjsize(const void *objp)
106 * If the object we have should not have ksize performed on it,
109 if (!objp || !virt_addr_valid(objp))
112 page = virt_to_head_page(objp);
115 * If the allocator sets PageSlab, we know the pointer came from
122 * If it's not a compound page, see if we have a matching VMA
123 * region. This test is intentionally done in reverse order,
124 * so if there's no VMA, we still fall through and hand back
125 * PAGE_SIZE for 0-order pages.
127 if (!PageCompound(page)) {
128 struct vm_area_struct *vma;
130 vma = find_vma(current->mm, (unsigned long)objp);
132 return vma->vm_end - vma->vm_start;
136 * The ksize() function is only guaranteed to work for pointers
137 * returned by kmalloc(). So handle arbitrary pointers here.
139 return PAGE_SIZE << compound_order(page);
142 int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
143 unsigned long start, int nr_pages, unsigned int foll_flags,
144 struct page **pages, struct vm_area_struct **vmas,
147 struct vm_area_struct *vma;
148 unsigned long vm_flags;
151 /* calculate required read or write permissions.
152 * If FOLL_FORCE is set, we only require the "MAY" flags.
154 vm_flags = (foll_flags & FOLL_WRITE) ?
155 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
156 vm_flags &= (foll_flags & FOLL_FORCE) ?
157 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
159 for (i = 0; i < nr_pages; i++) {
160 vma = find_vma(mm, start);
162 goto finish_or_fault;
164 /* protect what we can, including chardevs */
165 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
166 !(vm_flags & vma->vm_flags))
167 goto finish_or_fault;
170 pages[i] = virt_to_page(start);
172 page_cache_get(pages[i]);
176 start = (start + PAGE_SIZE) & PAGE_MASK;
182 return i ? : -EFAULT;
186 * get a list of pages in an address range belonging to the specified process
187 * and indicate the VMA that covers each page
188 * - this is potentially dodgy as we may end incrementing the page count of a
189 * slab page or a secondary page from a compound page
190 * - don't permit access to VMAs that don't support it, such as I/O mappings
192 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
193 unsigned long start, int nr_pages, int write, int force,
194 struct page **pages, struct vm_area_struct **vmas)
203 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
206 EXPORT_SYMBOL(get_user_pages);
209 * follow_pfn - look up PFN at a user virtual address
210 * @vma: memory mapping
211 * @address: user virtual address
212 * @pfn: location to store found PFN
214 * Only IO mappings and raw PFN mappings are allowed.
216 * Returns zero and the pfn at @pfn on success, -ve otherwise.
218 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
221 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
224 *pfn = address >> PAGE_SHIFT;
227 EXPORT_SYMBOL(follow_pfn);
229 DEFINE_RWLOCK(vmlist_lock);
230 struct vm_struct *vmlist;
232 void vfree(const void *addr)
236 EXPORT_SYMBOL(vfree);
238 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
241 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
242 * returns only a logical address.
244 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
246 EXPORT_SYMBOL(__vmalloc);
248 void *vmalloc_user(unsigned long size)
252 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
255 struct vm_area_struct *vma;
257 down_write(¤t->mm->mmap_sem);
258 vma = find_vma(current->mm, (unsigned long)ret);
260 vma->vm_flags |= VM_USERMAP;
261 up_write(¤t->mm->mmap_sem);
266 EXPORT_SYMBOL(vmalloc_user);
268 struct page *vmalloc_to_page(const void *addr)
270 return virt_to_page(addr);
272 EXPORT_SYMBOL(vmalloc_to_page);
274 unsigned long vmalloc_to_pfn(const void *addr)
276 return page_to_pfn(virt_to_page(addr));
278 EXPORT_SYMBOL(vmalloc_to_pfn);
280 long vread(char *buf, char *addr, unsigned long count)
282 memcpy(buf, addr, count);
286 long vwrite(char *buf, char *addr, unsigned long count)
288 /* Don't allow overflow */
289 if ((unsigned long) addr + count < count)
290 count = -(unsigned long) addr;
292 memcpy(addr, buf, count);
297 * vmalloc - allocate virtually continguos memory
299 * @size: allocation size
301 * Allocate enough pages to cover @size from the page level
302 * allocator and map them into continguos kernel virtual space.
304 * For tight control over page level allocator and protection flags
305 * use __vmalloc() instead.
307 void *vmalloc(unsigned long size)
309 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
311 EXPORT_SYMBOL(vmalloc);
314 * vzalloc - allocate virtually continguos memory with zero fill
316 * @size: allocation size
318 * Allocate enough pages to cover @size from the page level
319 * allocator and map them into continguos kernel virtual space.
320 * The memory allocated is set to zero.
322 * For tight control over page level allocator and protection flags
323 * use __vmalloc() instead.
325 void *vzalloc(unsigned long size)
327 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
330 EXPORT_SYMBOL(vzalloc);
333 * vmalloc_node - allocate memory on a specific node
334 * @size: allocation size
337 * Allocate enough pages to cover @size from the page level
338 * allocator and map them into contiguous kernel virtual space.
340 * For tight control over page level allocator and protection flags
341 * use __vmalloc() instead.
343 void *vmalloc_node(unsigned long size, int node)
345 return vmalloc(size);
347 EXPORT_SYMBOL(vmalloc_node);
350 * vzalloc_node - allocate memory on a specific node with zero fill
351 * @size: allocation size
354 * Allocate enough pages to cover @size from the page level
355 * allocator and map them into contiguous kernel virtual space.
356 * The memory allocated is set to zero.
358 * For tight control over page level allocator and protection flags
359 * use __vmalloc() instead.
361 void *vzalloc_node(unsigned long size, int node)
363 return vzalloc(size);
365 EXPORT_SYMBOL(vzalloc_node);
367 #ifndef PAGE_KERNEL_EXEC
368 # define PAGE_KERNEL_EXEC PAGE_KERNEL
372 * vmalloc_exec - allocate virtually contiguous, executable memory
373 * @size: allocation size
375 * Kernel-internal function to allocate enough pages to cover @size
376 * the page level allocator and map them into contiguous and
377 * executable kernel virtual space.
379 * For tight control over page level allocator and protection flags
380 * use __vmalloc() instead.
383 void *vmalloc_exec(unsigned long size)
385 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
389 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
390 * @size: allocation size
392 * Allocate enough 32bit PA addressable pages to cover @size from the
393 * page level allocator and map them into continguos kernel virtual space.
395 void *vmalloc_32(unsigned long size)
397 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
399 EXPORT_SYMBOL(vmalloc_32);
402 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
403 * @size: allocation size
405 * The resulting memory area is 32bit addressable and zeroed so it can be
406 * mapped to userspace without leaking data.
408 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
409 * remap_vmalloc_range() are permissible.
411 void *vmalloc_32_user(unsigned long size)
414 * We'll have to sort out the ZONE_DMA bits for 64-bit,
415 * but for now this can simply use vmalloc_user() directly.
417 return vmalloc_user(size);
419 EXPORT_SYMBOL(vmalloc_32_user);
421 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
428 void vunmap(const void *addr)
432 EXPORT_SYMBOL(vunmap);
434 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
439 EXPORT_SYMBOL(vm_map_ram);
441 void vm_unmap_ram(const void *mem, unsigned int count)
445 EXPORT_SYMBOL(vm_unmap_ram);
447 void vm_unmap_aliases(void)
450 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
453 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
456 void __attribute__((weak)) vmalloc_sync_all(void)
461 * alloc_vm_area - allocate a range of kernel address space
462 * @size: size of the area
464 * Returns: NULL on failure, vm_struct on success
466 * This function reserves a range of kernel address space, and
467 * allocates pagetables to map that range. No actual mappings
468 * are created. If the kernel address space is not shared
469 * between processes, it syncs the pagetable across all
472 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
477 EXPORT_SYMBOL_GPL(alloc_vm_area);
479 void free_vm_area(struct vm_struct *area)
483 EXPORT_SYMBOL_GPL(free_vm_area);
485 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
490 EXPORT_SYMBOL(vm_insert_page);
493 * sys_brk() for the most part doesn't need the global kernel
494 * lock, except when an application is doing something nasty
495 * like trying to un-brk an area that has already been mapped
496 * to a regular file. in this case, the unmapping will need
497 * to invoke file system routines that need the global lock.
499 SYSCALL_DEFINE1(brk, unsigned long, brk)
501 struct mm_struct *mm = current->mm;
503 if (brk < mm->start_brk || brk > mm->context.end_brk)
510 * Always allow shrinking brk
512 if (brk <= mm->brk) {
518 * Ok, looks good - let it rip.
520 flush_icache_range(mm->brk, brk);
521 return mm->brk = brk;
525 * initialise the VMA and region record slabs
527 void __init mmap_init(void)
531 ret = percpu_counter_init(&vm_committed_as, 0);
533 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
537 * validate the region tree
538 * - the caller must hold the region lock
540 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
541 static noinline void validate_nommu_regions(void)
543 struct vm_region *region, *last;
544 struct rb_node *p, *lastp;
546 lastp = rb_first(&nommu_region_tree);
550 last = rb_entry(lastp, struct vm_region, vm_rb);
551 BUG_ON(unlikely(last->vm_end <= last->vm_start));
552 BUG_ON(unlikely(last->vm_top < last->vm_end));
554 while ((p = rb_next(lastp))) {
555 region = rb_entry(p, struct vm_region, vm_rb);
556 last = rb_entry(lastp, struct vm_region, vm_rb);
558 BUG_ON(unlikely(region->vm_end <= region->vm_start));
559 BUG_ON(unlikely(region->vm_top < region->vm_end));
560 BUG_ON(unlikely(region->vm_start < last->vm_top));
566 static void validate_nommu_regions(void)
572 * add a region into the global tree
574 static void add_nommu_region(struct vm_region *region)
576 struct vm_region *pregion;
577 struct rb_node **p, *parent;
579 validate_nommu_regions();
582 p = &nommu_region_tree.rb_node;
585 pregion = rb_entry(parent, struct vm_region, vm_rb);
586 if (region->vm_start < pregion->vm_start)
588 else if (region->vm_start > pregion->vm_start)
590 else if (pregion == region)
596 rb_link_node(®ion->vm_rb, parent, p);
597 rb_insert_color(®ion->vm_rb, &nommu_region_tree);
599 validate_nommu_regions();
603 * delete a region from the global tree
605 static void delete_nommu_region(struct vm_region *region)
607 BUG_ON(!nommu_region_tree.rb_node);
609 validate_nommu_regions();
610 rb_erase(®ion->vm_rb, &nommu_region_tree);
611 validate_nommu_regions();
615 * free a contiguous series of pages
617 static void free_page_series(unsigned long from, unsigned long to)
619 for (; from < to; from += PAGE_SIZE) {
620 struct page *page = virt_to_page(from);
622 kdebug("- free %lx", from);
623 atomic_long_dec(&mmap_pages_allocated);
624 if (page_count(page) != 1)
625 kdebug("free page %p: refcount not one: %d",
626 page, page_count(page));
632 * release a reference to a region
633 * - the caller must hold the region semaphore for writing, which this releases
634 * - the region may not have been added to the tree yet, in which case vm_top
635 * will equal vm_start
637 static void __put_nommu_region(struct vm_region *region)
638 __releases(nommu_region_sem)
640 kenter("%p{%d}", region, region->vm_usage);
642 BUG_ON(!nommu_region_tree.rb_node);
644 if (--region->vm_usage == 0) {
645 if (region->vm_top > region->vm_start)
646 delete_nommu_region(region);
647 up_write(&nommu_region_sem);
650 fput(region->vm_file);
652 /* IO memory and memory shared directly out of the pagecache
653 * from ramfs/tmpfs mustn't be released here */
654 if (region->vm_flags & VM_MAPPED_COPY) {
655 kdebug("free series");
656 free_page_series(region->vm_start, region->vm_top);
658 kmem_cache_free(vm_region_jar, region);
660 up_write(&nommu_region_sem);
665 * release a reference to a region
667 static void put_nommu_region(struct vm_region *region)
669 down_write(&nommu_region_sem);
670 __put_nommu_region(region);
674 * update protection on a vma
676 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
679 struct mm_struct *mm = vma->vm_mm;
680 long start = vma->vm_start & PAGE_MASK;
681 while (start < vma->vm_end) {
682 protect_page(mm, start, flags);
685 update_protections(mm);
690 * add a VMA into a process's mm_struct in the appropriate place in the list
691 * and tree and add to the address space's page tree also if not an anonymous
693 * - should be called with mm->mmap_sem held writelocked
695 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
697 struct vm_area_struct *pvma, *prev;
698 struct address_space *mapping;
699 struct rb_node **p, *parent, *rb_prev;
703 BUG_ON(!vma->vm_region);
708 protect_vma(vma, vma->vm_flags);
710 /* add the VMA to the mapping */
712 mapping = vma->vm_file->f_mapping;
714 mutex_lock(&mapping->i_mmap_mutex);
715 flush_dcache_mmap_lock(mapping);
716 vma_interval_tree_insert(vma, &mapping->i_mmap);
717 flush_dcache_mmap_unlock(mapping);
718 mutex_unlock(&mapping->i_mmap_mutex);
721 /* add the VMA to the tree */
722 parent = rb_prev = NULL;
723 p = &mm->mm_rb.rb_node;
726 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
728 /* sort by: start addr, end addr, VMA struct addr in that order
729 * (the latter is necessary as we may get identical VMAs) */
730 if (vma->vm_start < pvma->vm_start)
732 else if (vma->vm_start > pvma->vm_start) {
735 } else if (vma->vm_end < pvma->vm_end)
737 else if (vma->vm_end > pvma->vm_end) {
740 } else if (vma < pvma)
742 else if (vma > pvma) {
749 rb_link_node(&vma->vm_rb, parent, p);
750 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
752 /* add VMA to the VMA list also */
755 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
757 __vma_link_list(mm, vma, prev, parent);
761 * delete a VMA from its owning mm_struct and address space
763 static void delete_vma_from_mm(struct vm_area_struct *vma)
765 struct address_space *mapping;
766 struct mm_struct *mm = vma->vm_mm;
773 if (mm->mmap_cache == vma)
774 mm->mmap_cache = NULL;
776 /* remove the VMA from the mapping */
778 mapping = vma->vm_file->f_mapping;
780 mutex_lock(&mapping->i_mmap_mutex);
781 flush_dcache_mmap_lock(mapping);
782 vma_interval_tree_remove(vma, &mapping->i_mmap);
783 flush_dcache_mmap_unlock(mapping);
784 mutex_unlock(&mapping->i_mmap_mutex);
787 /* remove from the MM's tree and list */
788 rb_erase(&vma->vm_rb, &mm->mm_rb);
791 vma->vm_prev->vm_next = vma->vm_next;
793 mm->mmap = vma->vm_next;
796 vma->vm_next->vm_prev = vma->vm_prev;
800 * destroy a VMA record
802 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
805 if (vma->vm_ops && vma->vm_ops->close)
806 vma->vm_ops->close(vma);
809 put_nommu_region(vma->vm_region);
810 kmem_cache_free(vm_area_cachep, vma);
814 * look up the first VMA in which addr resides, NULL if none
815 * - should be called with mm->mmap_sem at least held readlocked
817 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
819 struct vm_area_struct *vma;
821 /* check the cache first */
822 vma = mm->mmap_cache;
823 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
826 /* trawl the list (there may be multiple mappings in which addr
828 for (vma = mm->mmap; vma; vma = vma->vm_next) {
829 if (vma->vm_start > addr)
831 if (vma->vm_end > addr) {
832 mm->mmap_cache = vma;
839 EXPORT_SYMBOL(find_vma);
843 * - we don't extend stack VMAs under NOMMU conditions
845 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
847 return find_vma(mm, addr);
851 * expand a stack to a given address
852 * - not supported under NOMMU conditions
854 int expand_stack(struct vm_area_struct *vma, unsigned long address)
860 * look up the first VMA exactly that exactly matches addr
861 * - should be called with mm->mmap_sem at least held readlocked
863 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
867 struct vm_area_struct *vma;
868 unsigned long end = addr + len;
870 /* check the cache first */
871 vma = mm->mmap_cache;
872 if (vma && vma->vm_start == addr && vma->vm_end == end)
875 /* trawl the list (there may be multiple mappings in which addr
877 for (vma = mm->mmap; vma; vma = vma->vm_next) {
878 if (vma->vm_start < addr)
880 if (vma->vm_start > addr)
882 if (vma->vm_end == end) {
883 mm->mmap_cache = vma;
892 * determine whether a mapping should be permitted and, if so, what sort of
893 * mapping we're capable of supporting
895 static int validate_mmap_request(struct file *file,
901 unsigned long *_capabilities)
903 unsigned long capabilities, rlen;
906 /* do the simple checks first */
907 if (flags & MAP_FIXED) {
909 "%d: Can't do fixed-address/overlay mmap of RAM\n",
914 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
915 (flags & MAP_TYPE) != MAP_SHARED)
921 /* Careful about overflows.. */
922 rlen = PAGE_ALIGN(len);
923 if (!rlen || rlen > TASK_SIZE)
926 /* offset overflow? */
927 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
931 /* validate file mapping requests */
932 struct address_space *mapping;
934 /* files must support mmap */
935 if (!file->f_op || !file->f_op->mmap)
938 /* work out if what we've got could possibly be shared
939 * - we support chardevs that provide their own "memory"
940 * - we support files/blockdevs that are memory backed
942 mapping = file->f_mapping;
944 mapping = file->f_path.dentry->d_inode->i_mapping;
947 if (mapping && mapping->backing_dev_info)
948 capabilities = mapping->backing_dev_info->capabilities;
951 /* no explicit capabilities set, so assume some
953 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
956 capabilities = BDI_CAP_MAP_COPY;
971 /* eliminate any capabilities that we can't support on this
973 if (!file->f_op->get_unmapped_area)
974 capabilities &= ~BDI_CAP_MAP_DIRECT;
975 if (!file->f_op->read)
976 capabilities &= ~BDI_CAP_MAP_COPY;
978 /* The file shall have been opened with read permission. */
979 if (!(file->f_mode & FMODE_READ))
982 if (flags & MAP_SHARED) {
983 /* do checks for writing, appending and locking */
984 if ((prot & PROT_WRITE) &&
985 !(file->f_mode & FMODE_WRITE))
988 if (IS_APPEND(file->f_path.dentry->d_inode) &&
989 (file->f_mode & FMODE_WRITE))
992 if (locks_verify_locked(file->f_path.dentry->d_inode))
995 if (!(capabilities & BDI_CAP_MAP_DIRECT))
998 /* we mustn't privatise shared mappings */
999 capabilities &= ~BDI_CAP_MAP_COPY;
1002 /* we're going to read the file into private memory we
1004 if (!(capabilities & BDI_CAP_MAP_COPY))
1007 /* we don't permit a private writable mapping to be
1008 * shared with the backing device */
1009 if (prot & PROT_WRITE)
1010 capabilities &= ~BDI_CAP_MAP_DIRECT;
1013 if (capabilities & BDI_CAP_MAP_DIRECT) {
1014 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1015 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1016 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1018 capabilities &= ~BDI_CAP_MAP_DIRECT;
1019 if (flags & MAP_SHARED) {
1021 "MAP_SHARED not completely supported on !MMU\n");
1027 /* handle executable mappings and implied executable
1029 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1030 if (prot & PROT_EXEC)
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 & BDI_CAP_EXEC_MAP)
1040 else if ((prot & PROT_READ) &&
1041 (prot & PROT_EXEC) &&
1042 !(capabilities & BDI_CAP_EXEC_MAP)
1044 /* backing file is not executable, try to copy */
1045 capabilities &= ~BDI_CAP_MAP_DIRECT;
1049 /* anonymous mappings are always memory backed and can be
1052 capabilities = BDI_CAP_MAP_COPY;
1054 /* handle PROT_EXEC implication by PROT_READ */
1055 if ((prot & PROT_READ) &&
1056 (current->personality & READ_IMPLIES_EXEC))
1060 /* allow the security API to have its say */
1061 ret = security_mmap_addr(addr);
1066 *_capabilities = capabilities;
1071 * we've determined that we can make the mapping, now translate what we
1072 * now know into VMA flags
1074 static unsigned long determine_vm_flags(struct file *file,
1076 unsigned long flags,
1077 unsigned long capabilities)
1079 unsigned long vm_flags;
1081 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1082 /* vm_flags |= mm->def_flags; */
1084 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1085 /* attempt to share read-only copies of mapped file chunks */
1086 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1087 if (file && !(prot & PROT_WRITE))
1088 vm_flags |= VM_MAYSHARE;
1090 /* overlay a shareable mapping on the backing device or inode
1091 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1093 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1094 if (flags & MAP_SHARED)
1095 vm_flags |= VM_SHARED;
1098 /* refuse to let anyone share private mappings with this process if
1099 * it's being traced - otherwise breakpoints set in it may interfere
1100 * with another untraced process
1102 if ((flags & MAP_PRIVATE) && current->ptrace)
1103 vm_flags &= ~VM_MAYSHARE;
1109 * set up a shared mapping on a file (the driver or filesystem provides and
1112 static int do_mmap_shared_file(struct vm_area_struct *vma)
1116 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1118 vma->vm_region->vm_top = vma->vm_region->vm_end;
1124 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1125 * opposed to tried but failed) so we can only give a suitable error as
1126 * it's not possible to make a private copy if MAP_SHARED was given */
1131 * set up a private mapping or an anonymous shared mapping
1133 static int do_mmap_private(struct vm_area_struct *vma,
1134 struct vm_region *region,
1136 unsigned long capabilities)
1139 unsigned long total, point, n;
1143 /* invoke the file's mapping function so that it can keep track of
1144 * shared mappings on devices or memory
1145 * - VM_MAYSHARE will be set if it may attempt to share
1147 if (capabilities & BDI_CAP_MAP_DIRECT) {
1148 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1150 /* shouldn't return success if we're not sharing */
1151 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1152 vma->vm_region->vm_top = vma->vm_region->vm_end;
1158 /* getting an ENOSYS error indicates that direct mmap isn't
1159 * possible (as opposed to tried but failed) so we'll try to
1160 * make a private copy of the data and map that instead */
1164 /* allocate some memory to hold the mapping
1165 * - note that this may not return a page-aligned address if the object
1166 * we're allocating is smaller than a page
1168 order = get_order(len);
1169 kdebug("alloc order %d for %lx", order, len);
1171 pages = alloc_pages(GFP_KERNEL, order);
1176 atomic_long_add(total, &mmap_pages_allocated);
1178 point = len >> PAGE_SHIFT;
1180 /* we allocated a power-of-2 sized page set, so we may want to trim off
1182 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1183 while (total > point) {
1184 order = ilog2(total - point);
1186 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1187 atomic_long_sub(n, &mmap_pages_allocated);
1189 set_page_refcounted(pages + total);
1190 __free_pages(pages + total, order);
1194 for (point = 1; point < total; point++)
1195 set_page_refcounted(&pages[point]);
1197 base = page_address(pages);
1198 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1199 region->vm_start = (unsigned long) base;
1200 region->vm_end = region->vm_start + len;
1201 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1203 vma->vm_start = region->vm_start;
1204 vma->vm_end = region->vm_start + len;
1207 /* read the contents of a file into the copy */
1208 mm_segment_t old_fs;
1211 fpos = vma->vm_pgoff;
1212 fpos <<= PAGE_SHIFT;
1216 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
1222 /* clear the last little bit */
1224 memset(base + ret, 0, len - ret);
1231 free_page_series(region->vm_start, region->vm_top);
1232 region->vm_start = vma->vm_start = 0;
1233 region->vm_end = vma->vm_end = 0;
1238 printk("Allocation of length %lu from process %d (%s) failed\n",
1239 len, current->pid, current->comm);
1245 * handle mapping creation for uClinux
1247 unsigned long do_mmap_pgoff(struct file *file,
1251 unsigned long flags,
1252 unsigned long pgoff)
1254 struct vm_area_struct *vma;
1255 struct vm_region *region;
1257 unsigned long capabilities, vm_flags, result;
1260 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1262 /* decide whether we should attempt the mapping, and if so what sort of
1264 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1267 kleave(" = %d [val]", ret);
1271 /* we ignore the address hint */
1273 len = PAGE_ALIGN(len);
1275 /* we've determined that we can make the mapping, now translate what we
1276 * now know into VMA flags */
1277 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1279 /* we're going to need to record the mapping */
1280 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1282 goto error_getting_region;
1284 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1286 goto error_getting_vma;
1288 region->vm_usage = 1;
1289 region->vm_flags = vm_flags;
1290 region->vm_pgoff = pgoff;
1292 INIT_LIST_HEAD(&vma->anon_vma_chain);
1293 vma->vm_flags = vm_flags;
1294 vma->vm_pgoff = pgoff;
1297 region->vm_file = get_file(file);
1298 vma->vm_file = get_file(file);
1301 down_write(&nommu_region_sem);
1303 /* if we want to share, we need to check for regions created by other
1304 * mmap() calls that overlap with our proposed mapping
1305 * - we can only share with a superset match on most regular files
1306 * - shared mappings on character devices and memory backed files are
1307 * permitted to overlap inexactly as far as we are concerned for in
1308 * these cases, sharing is handled in the driver or filesystem rather
1311 if (vm_flags & VM_MAYSHARE) {
1312 struct vm_region *pregion;
1313 unsigned long pglen, rpglen, pgend, rpgend, start;
1315 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1316 pgend = pgoff + pglen;
1318 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1319 pregion = rb_entry(rb, struct vm_region, vm_rb);
1321 if (!(pregion->vm_flags & VM_MAYSHARE))
1324 /* search for overlapping mappings on the same file */
1325 if (pregion->vm_file->f_path.dentry->d_inode !=
1326 file->f_path.dentry->d_inode)
1329 if (pregion->vm_pgoff >= pgend)
1332 rpglen = pregion->vm_end - pregion->vm_start;
1333 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1334 rpgend = pregion->vm_pgoff + rpglen;
1335 if (pgoff >= rpgend)
1338 /* handle inexactly overlapping matches between
1340 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1341 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1342 /* new mapping is not a subset of the region */
1343 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1344 goto sharing_violation;
1348 /* we've found a region we can share */
1349 pregion->vm_usage++;
1350 vma->vm_region = pregion;
1351 start = pregion->vm_start;
1352 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1353 vma->vm_start = start;
1354 vma->vm_end = start + len;
1356 if (pregion->vm_flags & VM_MAPPED_COPY) {
1357 kdebug("share copy");
1358 vma->vm_flags |= VM_MAPPED_COPY;
1360 kdebug("share mmap");
1361 ret = do_mmap_shared_file(vma);
1363 vma->vm_region = NULL;
1366 pregion->vm_usage--;
1368 goto error_just_free;
1371 fput(region->vm_file);
1372 kmem_cache_free(vm_region_jar, region);
1378 /* obtain the address at which to make a shared mapping
1379 * - this is the hook for quasi-memory character devices to
1380 * tell us the location of a shared mapping
1382 if (capabilities & BDI_CAP_MAP_DIRECT) {
1383 addr = file->f_op->get_unmapped_area(file, addr, len,
1385 if (IS_ERR_VALUE(addr)) {
1388 goto error_just_free;
1390 /* the driver refused to tell us where to site
1391 * the mapping so we'll have to attempt to copy
1394 if (!(capabilities & BDI_CAP_MAP_COPY))
1395 goto error_just_free;
1397 capabilities &= ~BDI_CAP_MAP_DIRECT;
1399 vma->vm_start = region->vm_start = addr;
1400 vma->vm_end = region->vm_end = addr + len;
1405 vma->vm_region = region;
1407 /* set up the mapping
1408 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1410 if (file && vma->vm_flags & VM_SHARED)
1411 ret = do_mmap_shared_file(vma);
1413 ret = do_mmap_private(vma, region, len, capabilities);
1415 goto error_just_free;
1416 add_nommu_region(region);
1418 /* clear anonymous mappings that don't ask for uninitialized data */
1419 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1420 memset((void *)region->vm_start, 0,
1421 region->vm_end - region->vm_start);
1423 /* okay... we have a mapping; now we have to register it */
1424 result = vma->vm_start;
1426 current->mm->total_vm += len >> PAGE_SHIFT;
1429 add_vma_to_mm(current->mm, vma);
1431 /* we flush the region from the icache only when the first executable
1432 * mapping of it is made */
1433 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1434 flush_icache_range(region->vm_start, region->vm_end);
1435 region->vm_icache_flushed = true;
1438 up_write(&nommu_region_sem);
1440 kleave(" = %lx", result);
1444 up_write(&nommu_region_sem);
1446 if (region->vm_file)
1447 fput(region->vm_file);
1448 kmem_cache_free(vm_region_jar, region);
1451 kmem_cache_free(vm_area_cachep, vma);
1452 kleave(" = %d", ret);
1456 up_write(&nommu_region_sem);
1457 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1462 kmem_cache_free(vm_region_jar, region);
1463 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1464 " from process %d failed\n",
1469 error_getting_region:
1470 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1471 " from process %d failed\n",
1477 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1478 unsigned long, prot, unsigned long, flags,
1479 unsigned long, fd, unsigned long, pgoff)
1481 struct file *file = NULL;
1482 unsigned long retval = -EBADF;
1484 audit_mmap_fd(fd, flags);
1485 if (!(flags & MAP_ANONYMOUS)) {
1491 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1493 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1501 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1502 struct mmap_arg_struct {
1506 unsigned long flags;
1508 unsigned long offset;
1511 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1513 struct mmap_arg_struct a;
1515 if (copy_from_user(&a, arg, sizeof(a)))
1517 if (a.offset & ~PAGE_MASK)
1520 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1521 a.offset >> PAGE_SHIFT);
1523 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1526 * split a vma into two pieces at address 'addr', a new vma is allocated either
1527 * for the first part or the tail.
1529 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1530 unsigned long addr, int new_below)
1532 struct vm_area_struct *new;
1533 struct vm_region *region;
1534 unsigned long npages;
1538 /* we're only permitted to split anonymous regions (these should have
1539 * only a single usage on the region) */
1543 if (mm->map_count >= sysctl_max_map_count)
1546 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1550 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1552 kmem_cache_free(vm_region_jar, region);
1556 /* most fields are the same, copy all, and then fixup */
1558 *region = *vma->vm_region;
1559 new->vm_region = region;
1561 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1564 region->vm_top = region->vm_end = new->vm_end = addr;
1566 region->vm_start = new->vm_start = addr;
1567 region->vm_pgoff = new->vm_pgoff += npages;
1570 if (new->vm_ops && new->vm_ops->open)
1571 new->vm_ops->open(new);
1573 delete_vma_from_mm(vma);
1574 down_write(&nommu_region_sem);
1575 delete_nommu_region(vma->vm_region);
1577 vma->vm_region->vm_start = vma->vm_start = addr;
1578 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1580 vma->vm_region->vm_end = vma->vm_end = addr;
1581 vma->vm_region->vm_top = addr;
1583 add_nommu_region(vma->vm_region);
1584 add_nommu_region(new->vm_region);
1585 up_write(&nommu_region_sem);
1586 add_vma_to_mm(mm, vma);
1587 add_vma_to_mm(mm, new);
1592 * shrink a VMA by removing the specified chunk from either the beginning or
1595 static int shrink_vma(struct mm_struct *mm,
1596 struct vm_area_struct *vma,
1597 unsigned long from, unsigned long to)
1599 struct vm_region *region;
1603 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1605 delete_vma_from_mm(vma);
1606 if (from > vma->vm_start)
1610 add_vma_to_mm(mm, vma);
1612 /* cut the backing region down to size */
1613 region = vma->vm_region;
1614 BUG_ON(region->vm_usage != 1);
1616 down_write(&nommu_region_sem);
1617 delete_nommu_region(region);
1618 if (from > region->vm_start) {
1619 to = region->vm_top;
1620 region->vm_top = region->vm_end = from;
1622 region->vm_start = to;
1624 add_nommu_region(region);
1625 up_write(&nommu_region_sem);
1627 free_page_series(from, to);
1633 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1634 * VMA, though it need not cover the whole VMA
1636 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1638 struct vm_area_struct *vma;
1642 kenter(",%lx,%zx", start, len);
1644 len = PAGE_ALIGN(len);
1650 /* find the first potentially overlapping VMA */
1651 vma = find_vma(mm, start);
1653 static int limit = 0;
1656 "munmap of memory not mmapped by process %d"
1657 " (%s): 0x%lx-0x%lx\n",
1658 current->pid, current->comm,
1659 start, start + len - 1);
1665 /* we're allowed to split an anonymous VMA but not a file-backed one */
1668 if (start > vma->vm_start) {
1669 kleave(" = -EINVAL [miss]");
1672 if (end == vma->vm_end)
1673 goto erase_whole_vma;
1676 kleave(" = -EINVAL [split file]");
1679 /* the chunk must be a subset of the VMA found */
1680 if (start == vma->vm_start && end == vma->vm_end)
1681 goto erase_whole_vma;
1682 if (start < vma->vm_start || end > vma->vm_end) {
1683 kleave(" = -EINVAL [superset]");
1686 if (start & ~PAGE_MASK) {
1687 kleave(" = -EINVAL [unaligned start]");
1690 if (end != vma->vm_end && end & ~PAGE_MASK) {
1691 kleave(" = -EINVAL [unaligned split]");
1694 if (start != vma->vm_start && end != vma->vm_end) {
1695 ret = split_vma(mm, vma, start, 1);
1697 kleave(" = %d [split]", ret);
1701 return shrink_vma(mm, vma, start, end);
1705 delete_vma_from_mm(vma);
1706 delete_vma(mm, vma);
1710 EXPORT_SYMBOL(do_munmap);
1712 int vm_munmap(unsigned long addr, size_t len)
1714 struct mm_struct *mm = current->mm;
1717 down_write(&mm->mmap_sem);
1718 ret = do_munmap(mm, addr, len);
1719 up_write(&mm->mmap_sem);
1722 EXPORT_SYMBOL(vm_munmap);
1724 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1726 return vm_munmap(addr, len);
1730 * release all the mappings made in a process's VM space
1732 void exit_mmap(struct mm_struct *mm)
1734 struct vm_area_struct *vma;
1743 while ((vma = mm->mmap)) {
1744 mm->mmap = vma->vm_next;
1745 delete_vma_from_mm(vma);
1746 delete_vma(mm, vma);
1753 unsigned long vm_brk(unsigned long addr, unsigned long len)
1759 * expand (or shrink) an existing mapping, potentially moving it at the same
1760 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1762 * under NOMMU conditions, we only permit changing a mapping's size, and only
1763 * as long as it stays within the region allocated by do_mmap_private() and the
1764 * block is not shareable
1766 * MREMAP_FIXED is not supported under NOMMU conditions
1768 unsigned long do_mremap(unsigned long addr,
1769 unsigned long old_len, unsigned long new_len,
1770 unsigned long flags, unsigned long new_addr)
1772 struct vm_area_struct *vma;
1774 /* insanity checks first */
1775 old_len = PAGE_ALIGN(old_len);
1776 new_len = PAGE_ALIGN(new_len);
1777 if (old_len == 0 || new_len == 0)
1778 return (unsigned long) -EINVAL;
1780 if (addr & ~PAGE_MASK)
1783 if (flags & MREMAP_FIXED && new_addr != addr)
1784 return (unsigned long) -EINVAL;
1786 vma = find_vma_exact(current->mm, addr, old_len);
1788 return (unsigned long) -EINVAL;
1790 if (vma->vm_end != vma->vm_start + old_len)
1791 return (unsigned long) -EFAULT;
1793 if (vma->vm_flags & VM_MAYSHARE)
1794 return (unsigned long) -EPERM;
1796 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1797 return (unsigned long) -ENOMEM;
1799 /* all checks complete - do it */
1800 vma->vm_end = vma->vm_start + new_len;
1801 return vma->vm_start;
1803 EXPORT_SYMBOL(do_mremap);
1805 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1806 unsigned long, new_len, unsigned long, flags,
1807 unsigned long, new_addr)
1811 down_write(¤t->mm->mmap_sem);
1812 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1813 up_write(¤t->mm->mmap_sem);
1817 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1818 unsigned int foll_flags)
1823 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1824 unsigned long pfn, unsigned long size, pgprot_t prot)
1826 if (addr != (pfn << PAGE_SHIFT))
1829 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1832 EXPORT_SYMBOL(remap_pfn_range);
1834 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1835 unsigned long pgoff)
1837 unsigned int size = vma->vm_end - vma->vm_start;
1839 if (!(vma->vm_flags & VM_USERMAP))
1842 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1843 vma->vm_end = vma->vm_start + size;
1847 EXPORT_SYMBOL(remap_vmalloc_range);
1849 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1850 unsigned long len, unsigned long pgoff, unsigned long flags)
1855 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1859 void unmap_mapping_range(struct address_space *mapping,
1860 loff_t const holebegin, loff_t const holelen,
1864 EXPORT_SYMBOL(unmap_mapping_range);
1867 * Check that a process has enough memory to allocate a new virtual
1868 * mapping. 0 means there is enough memory for the allocation to
1869 * succeed and -ENOMEM implies there is not.
1871 * We currently support three overcommit policies, which are set via the
1872 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1874 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1875 * Additional code 2002 Jul 20 by Robert Love.
1877 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1879 * Note this is a helper function intended to be used by LSMs which
1880 * wish to use this logic.
1882 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1884 unsigned long free, allowed;
1886 vm_acct_memory(pages);
1889 * Sometimes we want to use more memory than we have
1891 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1894 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1895 free = global_page_state(NR_FREE_PAGES);
1896 free += global_page_state(NR_FILE_PAGES);
1899 * shmem pages shouldn't be counted as free in this
1900 * case, they can't be purged, only swapped out, and
1901 * that won't affect the overall amount of available
1902 * memory in the system.
1904 free -= global_page_state(NR_SHMEM);
1906 free += nr_swap_pages;
1909 * Any slabs which are created with the
1910 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1911 * which are reclaimable, under pressure. The dentry
1912 * cache and most inode caches should fall into this
1914 free += global_page_state(NR_SLAB_RECLAIMABLE);
1917 * Leave reserved pages. The pages are not for anonymous pages.
1919 if (free <= totalreserve_pages)
1922 free -= totalreserve_pages;
1925 * Leave the last 3% for root
1936 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1938 * Leave the last 3% for root
1941 allowed -= allowed / 32;
1942 allowed += total_swap_pages;
1944 /* Don't let a single process grow too big:
1945 leave 3% of the size of this process for other processes */
1947 allowed -= mm->total_vm / 32;
1949 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1953 vm_unacct_memory(pages);
1958 int in_gate_area_no_mm(unsigned long addr)
1963 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1968 EXPORT_SYMBOL(filemap_fault);
1970 int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1971 unsigned long size, pgoff_t pgoff)
1976 EXPORT_SYMBOL(generic_file_remap_pages);
1978 static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1979 unsigned long addr, void *buf, int len, int write)
1981 struct vm_area_struct *vma;
1983 down_read(&mm->mmap_sem);
1985 /* the access must start within one of the target process's mappings */
1986 vma = find_vma(mm, addr);
1988 /* don't overrun this mapping */
1989 if (addr + len >= vma->vm_end)
1990 len = vma->vm_end - addr;
1992 /* only read or write mappings where it is permitted */
1993 if (write && vma->vm_flags & VM_MAYWRITE)
1994 copy_to_user_page(vma, NULL, addr,
1995 (void *) addr, buf, len);
1996 else if (!write && vma->vm_flags & VM_MAYREAD)
1997 copy_from_user_page(vma, NULL, addr,
1998 buf, (void *) addr, len);
2005 up_read(&mm->mmap_sem);
2011 * @access_remote_vm - access another process' address space
2012 * @mm: the mm_struct of the target address space
2013 * @addr: start address to access
2014 * @buf: source or destination buffer
2015 * @len: number of bytes to transfer
2016 * @write: whether the access is a write
2018 * The caller must hold a reference on @mm.
2020 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2021 void *buf, int len, int write)
2023 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2027 * Access another process' address space.
2028 * - source/target buffer must be kernel space
2030 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2032 struct mm_struct *mm;
2034 if (addr + len < addr)
2037 mm = get_task_mm(tsk);
2041 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2048 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2049 * @inode: The inode to check
2050 * @size: The current filesize of the inode
2051 * @newsize: The proposed filesize of the inode
2053 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2054 * make sure that that any outstanding VMAs aren't broken and then shrink the
2055 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2056 * automatically grant mappings that are too large.
2058 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2061 struct vm_area_struct *vma;
2062 struct vm_region *region;
2064 size_t r_size, r_top;
2066 low = newsize >> PAGE_SHIFT;
2067 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2069 down_write(&nommu_region_sem);
2070 mutex_lock(&inode->i_mapping->i_mmap_mutex);
2072 /* search for VMAs that fall within the dead zone */
2073 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
2074 /* found one - only interested if it's shared out of the page
2076 if (vma->vm_flags & VM_SHARED) {
2077 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2078 up_write(&nommu_region_sem);
2079 return -ETXTBSY; /* not quite true, but near enough */
2083 /* reduce any regions that overlap the dead zone - if in existence,
2084 * these will be pointed to by VMAs that don't overlap the dead zone
2086 * we don't check for any regions that start beyond the EOF as there
2089 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2091 if (!(vma->vm_flags & VM_SHARED))
2094 region = vma->vm_region;
2095 r_size = region->vm_top - region->vm_start;
2096 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2098 if (r_top > newsize) {
2099 region->vm_top -= r_top - newsize;
2100 if (region->vm_end > region->vm_top)
2101 region->vm_end = region->vm_top;
2105 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2106 up_write(&nommu_region_sem);