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-2008 Paul Mundt <lethal@linux-sh.org>
16 #include <linux/module.h>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/tracehook.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mount.h>
29 #include <linux/personality.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
33 #include <asm/uaccess.h>
35 #include <asm/tlbflush.h>
38 static inline __attribute__((format(printf, 1, 2)))
39 void no_printk(const char *fmt, ...)
44 #define kenter(FMT, ...) \
45 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
46 #define kleave(FMT, ...) \
47 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
48 #define kdebug(FMT, ...) \
49 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
51 #define kenter(FMT, ...) \
52 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
53 #define kleave(FMT, ...) \
54 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
55 #define kdebug(FMT, ...) \
56 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
63 unsigned long max_mapnr;
64 unsigned long num_physpages;
65 atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
66 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
67 int sysctl_overcommit_ratio = 50; /* default is 50% */
68 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
69 int sysctl_nr_trim_pages = 1; /* page trimming behaviour */
70 int heap_stack_gap = 0;
72 atomic_t mmap_pages_allocated;
74 EXPORT_SYMBOL(mem_map);
75 EXPORT_SYMBOL(num_physpages);
77 /* list of mapped, potentially shareable regions */
78 static struct kmem_cache *vm_region_jar;
79 struct rb_root nommu_region_tree = RB_ROOT;
80 DECLARE_RWSEM(nommu_region_sem);
82 struct vm_operations_struct generic_file_vm_ops = {
86 * Handle all mappings that got truncated by a "truncate()"
89 * NOTE! We have to be ready to update the memory sharing
90 * between the file and the memory map for a potential last
91 * incomplete page. Ugly, but necessary.
93 int vmtruncate(struct inode *inode, loff_t offset)
95 struct address_space *mapping = inode->i_mapping;
98 if (inode->i_size < offset)
100 i_size_write(inode, offset);
102 truncate_inode_pages(mapping, offset);
106 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
107 if (limit != RLIM_INFINITY && offset > limit)
109 if (offset > inode->i_sb->s_maxbytes)
111 i_size_write(inode, offset);
114 if (inode->i_op->truncate)
115 inode->i_op->truncate(inode);
118 send_sig(SIGXFSZ, current, 0);
123 EXPORT_SYMBOL(vmtruncate);
126 * Return the total memory allocated for this pointer, not
127 * just what the caller asked for.
129 * Doesn't have to be accurate, i.e. may have races.
131 unsigned int kobjsize(const void *objp)
136 * If the object we have should not have ksize performed on it,
139 if (!objp || !virt_addr_valid(objp))
142 page = virt_to_head_page(objp);
145 * If the allocator sets PageSlab, we know the pointer came from
152 * The ksize() function is only guaranteed to work for pointers
153 * returned by kmalloc(). So handle arbitrary pointers here.
155 return PAGE_SIZE << compound_order(page);
158 int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
159 unsigned long start, int len, int flags,
160 struct page **pages, struct vm_area_struct **vmas)
162 struct vm_area_struct *vma;
163 unsigned long vm_flags;
165 int write = !!(flags & GUP_FLAGS_WRITE);
166 int force = !!(flags & GUP_FLAGS_FORCE);
167 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
169 /* calculate required read or write permissions.
170 * - if 'force' is set, we only require the "MAY" flags.
172 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
173 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
175 for (i = 0; i < len; i++) {
176 vma = find_vma(mm, start);
178 goto finish_or_fault;
180 /* protect what we can, including chardevs */
181 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
182 (!ignore && !(vm_flags & vma->vm_flags)))
183 goto finish_or_fault;
186 pages[i] = virt_to_page(start);
188 page_cache_get(pages[i]);
198 return i ? : -EFAULT;
203 * get a list of pages in an address range belonging to the specified process
204 * and indicate the VMA that covers each page
205 * - this is potentially dodgy as we may end incrementing the page count of a
206 * slab page or a secondary page from a compound page
207 * - don't permit access to VMAs that don't support it, such as I/O mappings
209 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
210 unsigned long start, int len, int write, int force,
211 struct page **pages, struct vm_area_struct **vmas)
216 flags |= GUP_FLAGS_WRITE;
218 flags |= GUP_FLAGS_FORCE;
220 return __get_user_pages(tsk, mm,
224 EXPORT_SYMBOL(get_user_pages);
226 DEFINE_RWLOCK(vmlist_lock);
227 struct vm_struct *vmlist;
229 void vfree(const void *addr)
233 EXPORT_SYMBOL(vfree);
235 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
238 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
239 * returns only a logical address.
241 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
243 EXPORT_SYMBOL(__vmalloc);
245 void *vmalloc_user(unsigned long size)
249 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
252 struct vm_area_struct *vma;
254 down_write(¤t->mm->mmap_sem);
255 vma = find_vma(current->mm, (unsigned long)ret);
257 vma->vm_flags |= VM_USERMAP;
258 up_write(¤t->mm->mmap_sem);
263 EXPORT_SYMBOL(vmalloc_user);
265 struct page *vmalloc_to_page(const void *addr)
267 return virt_to_page(addr);
269 EXPORT_SYMBOL(vmalloc_to_page);
271 unsigned long vmalloc_to_pfn(const void *addr)
273 return page_to_pfn(virt_to_page(addr));
275 EXPORT_SYMBOL(vmalloc_to_pfn);
277 long vread(char *buf, char *addr, unsigned long count)
279 memcpy(buf, addr, count);
283 long vwrite(char *buf, char *addr, unsigned long count)
285 /* Don't allow overflow */
286 if ((unsigned long) addr + count < count)
287 count = -(unsigned long) addr;
289 memcpy(addr, buf, count);
294 * vmalloc - allocate virtually continguos memory
296 * @size: allocation size
298 * Allocate enough pages to cover @size from the page level
299 * allocator and map them into continguos kernel virtual space.
301 * For tight control over page level allocator and protection flags
302 * use __vmalloc() instead.
304 void *vmalloc(unsigned long size)
306 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
308 EXPORT_SYMBOL(vmalloc);
310 void *vmalloc_node(unsigned long size, int node)
312 return vmalloc(size);
314 EXPORT_SYMBOL(vmalloc_node);
316 #ifndef PAGE_KERNEL_EXEC
317 # define PAGE_KERNEL_EXEC PAGE_KERNEL
321 * vmalloc_exec - allocate virtually contiguous, executable memory
322 * @size: allocation size
324 * Kernel-internal function to allocate enough pages to cover @size
325 * the page level allocator and map them into contiguous and
326 * executable kernel virtual space.
328 * For tight control over page level allocator and protection flags
329 * use __vmalloc() instead.
332 void *vmalloc_exec(unsigned long size)
334 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
338 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
339 * @size: allocation size
341 * Allocate enough 32bit PA addressable pages to cover @size from the
342 * page level allocator and map them into continguos kernel virtual space.
344 void *vmalloc_32(unsigned long size)
346 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
348 EXPORT_SYMBOL(vmalloc_32);
351 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
352 * @size: allocation size
354 * The resulting memory area is 32bit addressable and zeroed so it can be
355 * mapped to userspace without leaking data.
357 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
358 * remap_vmalloc_range() are permissible.
360 void *vmalloc_32_user(unsigned long size)
363 * We'll have to sort out the ZONE_DMA bits for 64-bit,
364 * but for now this can simply use vmalloc_user() directly.
366 return vmalloc_user(size);
368 EXPORT_SYMBOL(vmalloc_32_user);
370 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
377 void vunmap(const void *addr)
381 EXPORT_SYMBOL(vunmap);
384 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
387 void __attribute__((weak)) vmalloc_sync_all(void)
391 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
396 EXPORT_SYMBOL(vm_insert_page);
399 * sys_brk() for the most part doesn't need the global kernel
400 * lock, except when an application is doing something nasty
401 * like trying to un-brk an area that has already been mapped
402 * to a regular file. in this case, the unmapping will need
403 * to invoke file system routines that need the global lock.
405 asmlinkage unsigned long sys_brk(unsigned long brk)
407 struct mm_struct *mm = current->mm;
409 if (brk < mm->start_brk || brk > mm->context.end_brk)
416 * Always allow shrinking brk
418 if (brk <= mm->brk) {
424 * Ok, looks good - let it rip.
426 return mm->brk = brk;
430 * initialise the VMA and region record slabs
432 void __init mmap_init(void)
434 vm_region_jar = kmem_cache_create("vm_region_jar",
435 sizeof(struct vm_region), 0,
437 vm_area_cachep = kmem_cache_create("vm_area_struct",
438 sizeof(struct vm_area_struct), 0,
443 * validate the region tree
444 * - the caller must hold the region lock
446 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
447 static noinline void validate_nommu_regions(void)
449 struct vm_region *region, *last;
450 struct rb_node *p, *lastp;
452 lastp = rb_first(&nommu_region_tree);
456 last = rb_entry(lastp, struct vm_region, vm_rb);
457 if (unlikely(last->vm_end <= last->vm_start))
459 if (unlikely(last->vm_top < last->vm_end))
462 while ((p = rb_next(lastp))) {
463 region = rb_entry(p, struct vm_region, vm_rb);
464 last = rb_entry(lastp, struct vm_region, vm_rb);
466 if (unlikely(region->vm_end <= region->vm_start))
468 if (unlikely(region->vm_top < region->vm_end))
470 if (unlikely(region->vm_start < last->vm_top))
477 #define validate_nommu_regions() do {} while(0)
481 * add a region into the global tree
483 static void add_nommu_region(struct vm_region *region)
485 struct vm_region *pregion;
486 struct rb_node **p, *parent;
488 validate_nommu_regions();
490 BUG_ON(region->vm_start & ~PAGE_MASK);
493 p = &nommu_region_tree.rb_node;
496 pregion = rb_entry(parent, struct vm_region, vm_rb);
497 if (region->vm_start < pregion->vm_start)
499 else if (region->vm_start > pregion->vm_start)
501 else if (pregion == region)
507 rb_link_node(®ion->vm_rb, parent, p);
508 rb_insert_color(®ion->vm_rb, &nommu_region_tree);
510 validate_nommu_regions();
514 * delete a region from the global tree
516 static void delete_nommu_region(struct vm_region *region)
518 BUG_ON(!nommu_region_tree.rb_node);
520 validate_nommu_regions();
521 rb_erase(®ion->vm_rb, &nommu_region_tree);
522 validate_nommu_regions();
526 * free a contiguous series of pages
528 static void free_page_series(unsigned long from, unsigned long to)
530 for (; from < to; from += PAGE_SIZE) {
531 struct page *page = virt_to_page(from);
533 kdebug("- free %lx", from);
534 atomic_dec(&mmap_pages_allocated);
535 if (page_count(page) != 1)
536 kdebug("free page %p [%d]", page, page_count(page));
542 * release a reference to a region
543 * - the caller must hold the region semaphore, which this releases
544 * - the region may not have been added to the tree yet, in which case vm_top
545 * will equal vm_start
547 static void __put_nommu_region(struct vm_region *region)
548 __releases(nommu_region_sem)
550 kenter("%p{%d}", region, atomic_read(®ion->vm_usage));
552 BUG_ON(!nommu_region_tree.rb_node);
554 if (atomic_dec_and_test(®ion->vm_usage)) {
555 if (region->vm_top > region->vm_start)
556 delete_nommu_region(region);
557 up_write(&nommu_region_sem);
560 fput(region->vm_file);
562 /* IO memory and memory shared directly out of the pagecache
563 * from ramfs/tmpfs mustn't be released here */
564 if (region->vm_flags & VM_MAPPED_COPY) {
565 kdebug("free series");
566 free_page_series(region->vm_start, region->vm_top);
568 kmem_cache_free(vm_region_jar, region);
570 up_write(&nommu_region_sem);
575 * release a reference to a region
577 static void put_nommu_region(struct vm_region *region)
579 down_write(&nommu_region_sem);
580 __put_nommu_region(region);
584 * add a VMA into a process's mm_struct in the appropriate place in the list
585 * and tree and add to the address space's page tree also if not an anonymous
587 * - should be called with mm->mmap_sem held writelocked
589 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
591 struct vm_area_struct *pvma, **pp;
592 struct address_space *mapping;
593 struct rb_node **p, *parent;
597 BUG_ON(!vma->vm_region);
602 /* add the VMA to the mapping */
604 mapping = vma->vm_file->f_mapping;
606 flush_dcache_mmap_lock(mapping);
607 vma_prio_tree_insert(vma, &mapping->i_mmap);
608 flush_dcache_mmap_unlock(mapping);
611 /* add the VMA to the tree */
613 p = &mm->mm_rb.rb_node;
616 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
618 /* sort by: start addr, end addr, VMA struct addr in that order
619 * (the latter is necessary as we may get identical VMAs) */
620 if (vma->vm_start < pvma->vm_start)
622 else if (vma->vm_start > pvma->vm_start)
624 else if (vma->vm_end < pvma->vm_end)
626 else if (vma->vm_end > pvma->vm_end)
636 rb_link_node(&vma->vm_rb, parent, p);
637 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
639 /* add VMA to the VMA list also */
640 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
641 if (pvma->vm_start > vma->vm_start)
643 if (pvma->vm_start < vma->vm_start)
645 if (pvma->vm_end < vma->vm_end)
654 * delete a VMA from its owning mm_struct and address space
656 static void delete_vma_from_mm(struct vm_area_struct *vma)
658 struct vm_area_struct **pp;
659 struct address_space *mapping;
660 struct mm_struct *mm = vma->vm_mm;
665 if (mm->mmap_cache == vma)
666 mm->mmap_cache = NULL;
668 /* remove the VMA from the mapping */
670 mapping = vma->vm_file->f_mapping;
672 flush_dcache_mmap_lock(mapping);
673 vma_prio_tree_remove(vma, &mapping->i_mmap);
674 flush_dcache_mmap_unlock(mapping);
677 /* remove from the MM's tree and list */
678 rb_erase(&vma->vm_rb, &mm->mm_rb);
679 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
690 * destroy a VMA record
692 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
695 if (vma->vm_ops && vma->vm_ops->close)
696 vma->vm_ops->close(vma);
699 if (vma->vm_flags & VM_EXECUTABLE)
700 removed_exe_file_vma(mm);
702 put_nommu_region(vma->vm_region);
703 kmem_cache_free(vm_area_cachep, vma);
707 * look up the first VMA in which addr resides, NULL if none
708 * - should be called with mm->mmap_sem at least held readlocked
710 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
712 struct vm_area_struct *vma;
713 struct rb_node *n = mm->mm_rb.rb_node;
715 /* check the cache first */
716 vma = mm->mmap_cache;
717 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
720 /* trawl the tree (there may be multiple mappings in which addr
722 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
723 vma = rb_entry(n, struct vm_area_struct, vm_rb);
724 if (vma->vm_start > addr)
726 if (vma->vm_end > addr) {
727 mm->mmap_cache = vma;
734 EXPORT_SYMBOL(find_vma);
738 * - we don't extend stack VMAs under NOMMU conditions
740 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
742 return find_vma(mm, addr);
746 * expand a stack to a given address
747 * - not supported under NOMMU conditions
749 int expand_stack(struct vm_area_struct *vma, unsigned long address)
755 * look up the first VMA exactly that exactly matches addr
756 * - should be called with mm->mmap_sem at least held readlocked
758 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
762 struct vm_area_struct *vma;
763 struct rb_node *n = mm->mm_rb.rb_node;
764 unsigned long end = addr + len;
766 /* check the cache first */
767 vma = mm->mmap_cache;
768 if (vma && vma->vm_start == addr && vma->vm_end == end)
771 /* trawl the tree (there may be multiple mappings in which addr
773 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
774 vma = rb_entry(n, struct vm_area_struct, vm_rb);
775 if (vma->vm_start < addr)
777 if (vma->vm_start > addr)
779 if (vma->vm_end == end) {
780 mm->mmap_cache = vma;
789 * determine whether a mapping should be permitted and, if so, what sort of
790 * mapping we're capable of supporting
792 static int validate_mmap_request(struct file *file,
798 unsigned long *_capabilities)
800 unsigned long capabilities, rlen;
801 unsigned long reqprot = prot;
804 /* do the simple checks first */
805 if (flags & MAP_FIXED || addr) {
807 "%d: Can't do fixed-address/overlay mmap of RAM\n",
812 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
813 (flags & MAP_TYPE) != MAP_SHARED)
819 /* Careful about overflows.. */
820 rlen = PAGE_ALIGN(len);
821 if (!rlen || rlen > TASK_SIZE)
824 /* offset overflow? */
825 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
829 /* validate file mapping requests */
830 struct address_space *mapping;
832 /* files must support mmap */
833 if (!file->f_op || !file->f_op->mmap)
836 /* work out if what we've got could possibly be shared
837 * - we support chardevs that provide their own "memory"
838 * - we support files/blockdevs that are memory backed
840 mapping = file->f_mapping;
842 mapping = file->f_path.dentry->d_inode->i_mapping;
845 if (mapping && mapping->backing_dev_info)
846 capabilities = mapping->backing_dev_info->capabilities;
849 /* no explicit capabilities set, so assume some
851 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
854 capabilities = BDI_CAP_MAP_COPY;
869 /* eliminate any capabilities that we can't support on this
871 if (!file->f_op->get_unmapped_area)
872 capabilities &= ~BDI_CAP_MAP_DIRECT;
873 if (!file->f_op->read)
874 capabilities &= ~BDI_CAP_MAP_COPY;
876 if (flags & MAP_SHARED) {
877 /* do checks for writing, appending and locking */
878 if ((prot & PROT_WRITE) &&
879 !(file->f_mode & FMODE_WRITE))
882 if (IS_APPEND(file->f_path.dentry->d_inode) &&
883 (file->f_mode & FMODE_WRITE))
886 if (locks_verify_locked(file->f_path.dentry->d_inode))
889 if (!(capabilities & BDI_CAP_MAP_DIRECT))
892 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
893 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
894 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
896 printk("MAP_SHARED not completely supported on !MMU\n");
900 /* we mustn't privatise shared mappings */
901 capabilities &= ~BDI_CAP_MAP_COPY;
904 /* we're going to read the file into private memory we
906 if (!(capabilities & BDI_CAP_MAP_COPY))
909 /* we don't permit a private writable mapping to be
910 * shared with the backing device */
911 if (prot & PROT_WRITE)
912 capabilities &= ~BDI_CAP_MAP_DIRECT;
915 /* handle executable mappings and implied executable
917 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
918 if (prot & PROT_EXEC)
921 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
922 /* handle implication of PROT_EXEC by PROT_READ */
923 if (current->personality & READ_IMPLIES_EXEC) {
924 if (capabilities & BDI_CAP_EXEC_MAP)
928 else if ((prot & PROT_READ) &&
929 (prot & PROT_EXEC) &&
930 !(capabilities & BDI_CAP_EXEC_MAP)
932 /* backing file is not executable, try to copy */
933 capabilities &= ~BDI_CAP_MAP_DIRECT;
937 /* anonymous mappings are always memory backed and can be
940 capabilities = BDI_CAP_MAP_COPY;
942 /* handle PROT_EXEC implication by PROT_READ */
943 if ((prot & PROT_READ) &&
944 (current->personality & READ_IMPLIES_EXEC))
948 /* allow the security API to have its say */
949 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
954 *_capabilities = capabilities;
959 * we've determined that we can make the mapping, now translate what we
960 * now know into VMA flags
962 static unsigned long determine_vm_flags(struct file *file,
965 unsigned long capabilities)
967 unsigned long vm_flags;
969 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
970 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
971 /* vm_flags |= mm->def_flags; */
973 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
974 /* attempt to share read-only copies of mapped file chunks */
975 if (file && !(prot & PROT_WRITE))
976 vm_flags |= VM_MAYSHARE;
979 /* overlay a shareable mapping on the backing device or inode
980 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
982 if (flags & MAP_SHARED)
983 vm_flags |= VM_MAYSHARE | VM_SHARED;
984 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
985 vm_flags |= VM_MAYSHARE;
988 /* refuse to let anyone share private mappings with this process if
989 * it's being traced - otherwise breakpoints set in it may interfere
990 * with another untraced process
992 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
993 vm_flags &= ~VM_MAYSHARE;
999 * set up a shared mapping on a file (the driver or filesystem provides and
1002 static int do_mmap_shared_file(struct vm_area_struct *vma)
1006 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1008 vma->vm_region->vm_top = vma->vm_region->vm_end;
1014 /* getting an ENOSYS error indicates that direct mmap isn't
1015 * possible (as opposed to tried but failed) so we'll fall
1016 * through to making a private copy of the data and mapping
1022 * set up a private mapping or an anonymous shared mapping
1024 static int do_mmap_private(struct vm_area_struct *vma,
1025 struct vm_region *region,
1029 unsigned long total, point, n, rlen;
1033 /* invoke the file's mapping function so that it can keep track of
1034 * shared mappings on devices or memory
1035 * - VM_MAYSHARE will be set if it may attempt to share
1038 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1040 /* shouldn't return success if we're not sharing */
1041 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1042 vma->vm_region->vm_top = vma->vm_region->vm_end;
1048 /* getting an ENOSYS error indicates that direct mmap isn't
1049 * possible (as opposed to tried but failed) so we'll try to
1050 * make a private copy of the data and map that instead */
1053 rlen = PAGE_ALIGN(len);
1055 /* allocate some memory to hold the mapping
1056 * - note that this may not return a page-aligned address if the object
1057 * we're allocating is smaller than a page
1059 order = get_order(rlen);
1060 kdebug("alloc order %d for %lx", order, len);
1062 pages = alloc_pages(GFP_KERNEL, order);
1067 atomic_add(total, &mmap_pages_allocated);
1069 point = rlen >> PAGE_SHIFT;
1071 /* we allocated a power-of-2 sized page set, so we may want to trim off
1073 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1074 while (total > point) {
1075 order = ilog2(total - point);
1077 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1078 atomic_sub(n, &mmap_pages_allocated);
1080 set_page_refcounted(pages + total);
1081 __free_pages(pages + total, order);
1085 for (point = 1; point < total; point++)
1086 set_page_refcounted(&pages[point]);
1088 base = page_address(pages);
1089 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1090 region->vm_start = (unsigned long) base;
1091 region->vm_end = region->vm_start + rlen;
1092 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1094 vma->vm_start = region->vm_start;
1095 vma->vm_end = region->vm_start + len;
1098 /* read the contents of a file into the copy */
1099 mm_segment_t old_fs;
1102 fpos = vma->vm_pgoff;
1103 fpos <<= PAGE_SHIFT;
1107 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
1113 /* clear the last little bit */
1115 memset(base + ret, 0, rlen - ret);
1118 /* if it's an anonymous mapping, then just clear it */
1119 memset(base, 0, rlen);
1125 free_page_series(region->vm_start, region->vm_end);
1126 region->vm_start = vma->vm_start = 0;
1127 region->vm_end = vma->vm_end = 0;
1132 printk("Allocation of length %lu from process %d failed\n",
1139 * handle mapping creation for uClinux
1141 unsigned long do_mmap_pgoff(struct file *file,
1145 unsigned long flags,
1146 unsigned long pgoff)
1148 struct vm_area_struct *vma;
1149 struct vm_region *region;
1151 unsigned long capabilities, vm_flags, result;
1154 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1156 if (!(flags & MAP_FIXED))
1157 addr = round_hint_to_min(addr);
1159 /* decide whether we should attempt the mapping, and if so what sort of
1161 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1164 kleave(" = %d [val]", ret);
1168 /* we've determined that we can make the mapping, now translate what we
1169 * now know into VMA flags */
1170 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1172 /* we're going to need to record the mapping */
1173 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1175 goto error_getting_region;
1177 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1179 goto error_getting_vma;
1181 atomic_set(®ion->vm_usage, 1);
1182 region->vm_flags = vm_flags;
1183 region->vm_pgoff = pgoff;
1185 INIT_LIST_HEAD(&vma->anon_vma_node);
1186 vma->vm_flags = vm_flags;
1187 vma->vm_pgoff = pgoff;
1190 region->vm_file = file;
1192 vma->vm_file = file;
1194 if (vm_flags & VM_EXECUTABLE) {
1195 added_exe_file_vma(current->mm);
1196 vma->vm_mm = current->mm;
1200 down_write(&nommu_region_sem);
1202 /* if we want to share, we need to check for regions created by other
1203 * mmap() calls that overlap with our proposed mapping
1204 * - we can only share with a superset match on most regular files
1205 * - shared mappings on character devices and memory backed files are
1206 * permitted to overlap inexactly as far as we are concerned for in
1207 * these cases, sharing is handled in the driver or filesystem rather
1210 if (vm_flags & VM_MAYSHARE) {
1211 struct vm_region *pregion;
1212 unsigned long pglen, rpglen, pgend, rpgend, start;
1214 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1215 pgend = pgoff + pglen;
1217 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1218 pregion = rb_entry(rb, struct vm_region, vm_rb);
1220 if (!(pregion->vm_flags & VM_MAYSHARE))
1223 /* search for overlapping mappings on the same file */
1224 if (pregion->vm_file->f_path.dentry->d_inode !=
1225 file->f_path.dentry->d_inode)
1228 if (pregion->vm_pgoff >= pgend)
1231 rpglen = pregion->vm_end - pregion->vm_start;
1232 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1233 rpgend = pregion->vm_pgoff + rpglen;
1234 if (pgoff >= rpgend)
1237 /* handle inexactly overlapping matches between
1239 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1240 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1241 /* new mapping is not a subset of the region */
1242 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1243 goto sharing_violation;
1247 /* we've found a region we can share */
1248 atomic_inc(&pregion->vm_usage);
1249 vma->vm_region = pregion;
1250 start = pregion->vm_start;
1251 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1252 vma->vm_start = start;
1253 vma->vm_end = start + len;
1255 if (pregion->vm_flags & VM_MAPPED_COPY) {
1256 kdebug("share copy");
1257 vma->vm_flags |= VM_MAPPED_COPY;
1259 kdebug("share mmap");
1260 ret = do_mmap_shared_file(vma);
1262 vma->vm_region = NULL;
1265 atomic_dec(&pregion->vm_usage);
1267 goto error_just_free;
1270 fput(region->vm_file);
1271 kmem_cache_free(vm_region_jar, region);
1277 /* obtain the address at which to make a shared mapping
1278 * - this is the hook for quasi-memory character devices to
1279 * tell us the location of a shared mapping
1281 if (file && file->f_op->get_unmapped_area) {
1282 addr = file->f_op->get_unmapped_area(file, addr, len,
1284 if (IS_ERR((void *) addr)) {
1286 if (ret != (unsigned long) -ENOSYS)
1287 goto error_just_free;
1289 /* the driver refused to tell us where to site
1290 * the mapping so we'll have to attempt to copy
1292 ret = (unsigned long) -ENODEV;
1293 if (!(capabilities & BDI_CAP_MAP_COPY))
1294 goto error_just_free;
1296 capabilities &= ~BDI_CAP_MAP_DIRECT;
1298 vma->vm_start = region->vm_start = addr;
1299 vma->vm_end = region->vm_end = addr + len;
1304 vma->vm_region = region;
1306 /* set up the mapping */
1307 if (file && vma->vm_flags & VM_SHARED)
1308 ret = do_mmap_shared_file(vma);
1310 ret = do_mmap_private(vma, region, len);
1312 goto error_put_region;
1314 add_nommu_region(region);
1316 /* okay... we have a mapping; now we have to register it */
1317 result = vma->vm_start;
1319 current->mm->total_vm += len >> PAGE_SHIFT;
1322 add_vma_to_mm(current->mm, vma);
1324 up_write(&nommu_region_sem);
1326 if (prot & PROT_EXEC)
1327 flush_icache_range(result, result + len);
1329 kleave(" = %lx", result);
1333 __put_nommu_region(region);
1337 if (vma->vm_flags & VM_EXECUTABLE)
1338 removed_exe_file_vma(vma->vm_mm);
1340 kmem_cache_free(vm_area_cachep, vma);
1342 kleave(" = %d [pr]", ret);
1346 up_write(&nommu_region_sem);
1348 fput(region->vm_file);
1349 kmem_cache_free(vm_region_jar, region);
1351 if (vma->vm_flags & VM_EXECUTABLE)
1352 removed_exe_file_vma(vma->vm_mm);
1353 kmem_cache_free(vm_area_cachep, vma);
1354 kleave(" = %d", ret);
1358 up_write(&nommu_region_sem);
1359 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1364 kmem_cache_free(vm_region_jar, region);
1365 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1366 " from process %d failed\n",
1371 error_getting_region:
1372 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1373 " from process %d failed\n",
1378 EXPORT_SYMBOL(do_mmap_pgoff);
1381 * split a vma into two pieces at address 'addr', a new vma is allocated either
1382 * for the first part or the tail.
1384 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1385 unsigned long addr, int new_below)
1387 struct vm_area_struct *new;
1388 struct vm_region *region;
1389 unsigned long npages;
1393 /* we're only permitted to split anonymous regions that have a single
1396 atomic_read(&vma->vm_region->vm_usage) != 1)
1399 if (mm->map_count >= sysctl_max_map_count)
1402 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1406 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1408 kmem_cache_free(vm_region_jar, region);
1412 /* most fields are the same, copy all, and then fixup */
1414 *region = *vma->vm_region;
1415 new->vm_region = region;
1417 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1420 region->vm_top = region->vm_end = new->vm_end = addr;
1422 region->vm_start = new->vm_start = addr;
1423 region->vm_pgoff = new->vm_pgoff += npages;
1426 if (new->vm_ops && new->vm_ops->open)
1427 new->vm_ops->open(new);
1429 delete_vma_from_mm(vma);
1430 down_write(&nommu_region_sem);
1431 delete_nommu_region(vma->vm_region);
1433 vma->vm_region->vm_start = vma->vm_start = addr;
1434 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1436 vma->vm_region->vm_end = vma->vm_end = addr;
1437 vma->vm_region->vm_top = addr;
1439 add_nommu_region(vma->vm_region);
1440 add_nommu_region(new->vm_region);
1441 up_write(&nommu_region_sem);
1442 add_vma_to_mm(mm, vma);
1443 add_vma_to_mm(mm, new);
1448 * shrink a VMA by removing the specified chunk from either the beginning or
1451 static int shrink_vma(struct mm_struct *mm,
1452 struct vm_area_struct *vma,
1453 unsigned long from, unsigned long to)
1455 struct vm_region *region;
1459 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1461 delete_vma_from_mm(vma);
1462 if (from > vma->vm_start)
1466 add_vma_to_mm(mm, vma);
1468 /* cut the backing region down to size */
1469 region = vma->vm_region;
1470 BUG_ON(atomic_read(®ion->vm_usage) != 1);
1472 down_write(&nommu_region_sem);
1473 delete_nommu_region(region);
1474 if (from > region->vm_start) {
1475 to = region->vm_top;
1476 region->vm_top = region->vm_end = from;
1478 region->vm_start = to;
1480 add_nommu_region(region);
1481 up_write(&nommu_region_sem);
1483 free_page_series(from, to);
1489 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1490 * VMA, though it need not cover the whole VMA
1492 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1494 struct vm_area_struct *vma;
1496 unsigned long end = start + len;
1499 kenter(",%lx,%zx", start, len);
1504 /* find the first potentially overlapping VMA */
1505 vma = find_vma(mm, start);
1508 "munmap of memory not mmapped by process %d (%s):"
1510 current->pid, current->comm, start, start + len - 1);
1514 /* we're allowed to split an anonymous VMA but not a file-backed one */
1517 if (start > vma->vm_start) {
1518 kleave(" = -EINVAL [miss]");
1521 if (end == vma->vm_end)
1522 goto erase_whole_vma;
1523 rb = rb_next(&vma->vm_rb);
1524 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1526 kleave(" = -EINVAL [split file]");
1529 /* the chunk must be a subset of the VMA found */
1530 if (start == vma->vm_start && end == vma->vm_end)
1531 goto erase_whole_vma;
1532 if (start < vma->vm_start || end > vma->vm_end) {
1533 kleave(" = -EINVAL [superset]");
1536 if (start & ~PAGE_MASK) {
1537 kleave(" = -EINVAL [unaligned start]");
1540 if (end != vma->vm_end && end & ~PAGE_MASK) {
1541 kleave(" = -EINVAL [unaligned split]");
1544 if (start != vma->vm_start && end != vma->vm_end) {
1545 ret = split_vma(mm, vma, start, 1);
1547 kleave(" = %d [split]", ret);
1551 return shrink_vma(mm, vma, start, end);
1555 delete_vma_from_mm(vma);
1556 delete_vma(mm, vma);
1560 EXPORT_SYMBOL(do_munmap);
1562 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1565 struct mm_struct *mm = current->mm;
1567 down_write(&mm->mmap_sem);
1568 ret = do_munmap(mm, addr, len);
1569 up_write(&mm->mmap_sem);
1574 * release all the mappings made in a process's VM space
1576 void exit_mmap(struct mm_struct *mm)
1578 struct vm_area_struct *vma;
1587 while ((vma = mm->mmap)) {
1588 mm->mmap = vma->vm_next;
1589 delete_vma_from_mm(vma);
1590 delete_vma(mm, vma);
1596 unsigned long do_brk(unsigned long addr, unsigned long len)
1602 * expand (or shrink) an existing mapping, potentially moving it at the same
1603 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1605 * under NOMMU conditions, we only permit changing a mapping's size, and only
1606 * as long as it stays within the region allocated by do_mmap_private() and the
1607 * block is not shareable
1609 * MREMAP_FIXED is not supported under NOMMU conditions
1611 unsigned long do_mremap(unsigned long addr,
1612 unsigned long old_len, unsigned long new_len,
1613 unsigned long flags, unsigned long new_addr)
1615 struct vm_area_struct *vma;
1617 /* insanity checks first */
1618 if (old_len == 0 || new_len == 0)
1619 return (unsigned long) -EINVAL;
1621 if (addr & ~PAGE_MASK)
1624 if (flags & MREMAP_FIXED && new_addr != addr)
1625 return (unsigned long) -EINVAL;
1627 vma = find_vma_exact(current->mm, addr, old_len);
1629 return (unsigned long) -EINVAL;
1631 if (vma->vm_end != vma->vm_start + old_len)
1632 return (unsigned long) -EFAULT;
1634 if (vma->vm_flags & VM_MAYSHARE)
1635 return (unsigned long) -EPERM;
1637 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1638 return (unsigned long) -ENOMEM;
1640 /* all checks complete - do it */
1641 vma->vm_end = vma->vm_start + new_len;
1642 return vma->vm_start;
1644 EXPORT_SYMBOL(do_mremap);
1647 unsigned long sys_mremap(unsigned long addr,
1648 unsigned long old_len, unsigned long new_len,
1649 unsigned long flags, unsigned long new_addr)
1653 down_write(¤t->mm->mmap_sem);
1654 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1655 up_write(¤t->mm->mmap_sem);
1659 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1660 unsigned int foll_flags)
1665 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1666 unsigned long to, unsigned long size, pgprot_t prot)
1668 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1671 EXPORT_SYMBOL(remap_pfn_range);
1673 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1674 unsigned long pgoff)
1676 unsigned int size = vma->vm_end - vma->vm_start;
1678 if (!(vma->vm_flags & VM_USERMAP))
1681 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1682 vma->vm_end = vma->vm_start + size;
1686 EXPORT_SYMBOL(remap_vmalloc_range);
1688 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1692 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1693 unsigned long len, unsigned long pgoff, unsigned long flags)
1698 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1702 void unmap_mapping_range(struct address_space *mapping,
1703 loff_t const holebegin, loff_t const holelen,
1707 EXPORT_SYMBOL(unmap_mapping_range);
1710 * ask for an unmapped area at which to create a mapping on a file
1712 unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1713 unsigned long len, unsigned long pgoff,
1714 unsigned long flags)
1716 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1717 unsigned long, unsigned long);
1719 get_area = current->mm->get_unmapped_area;
1720 if (file && file->f_op && file->f_op->get_unmapped_area)
1721 get_area = file->f_op->get_unmapped_area;
1726 return get_area(file, addr, len, pgoff, flags);
1728 EXPORT_SYMBOL(get_unmapped_area);
1731 * Check that a process has enough memory to allocate a new virtual
1732 * mapping. 0 means there is enough memory for the allocation to
1733 * succeed and -ENOMEM implies there is not.
1735 * We currently support three overcommit policies, which are set via the
1736 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1738 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1739 * Additional code 2002 Jul 20 by Robert Love.
1741 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1743 * Note this is a helper function intended to be used by LSMs which
1744 * wish to use this logic.
1746 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1748 unsigned long free, allowed;
1750 vm_acct_memory(pages);
1753 * Sometimes we want to use more memory than we have
1755 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1758 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1761 free = global_page_state(NR_FILE_PAGES);
1762 free += nr_swap_pages;
1765 * Any slabs which are created with the
1766 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1767 * which are reclaimable, under pressure. The dentry
1768 * cache and most inode caches should fall into this
1770 free += global_page_state(NR_SLAB_RECLAIMABLE);
1773 * Leave the last 3% for root
1782 * nr_free_pages() is very expensive on large systems,
1783 * only call if we're about to fail.
1785 n = nr_free_pages();
1788 * Leave reserved pages. The pages are not for anonymous pages.
1790 if (n <= totalreserve_pages)
1793 n -= totalreserve_pages;
1796 * Leave the last 3% for root
1808 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1810 * Leave the last 3% for root
1813 allowed -= allowed / 32;
1814 allowed += total_swap_pages;
1816 /* Don't let a single process grow too big:
1817 leave 3% of the size of this process for other processes */
1819 allowed -= mm->total_vm / 32;
1822 * cast `allowed' as a signed long because vm_committed_space
1823 * sometimes has a negative value
1825 if (atomic_long_read(&vm_committed_space) < (long)allowed)
1828 vm_unacct_memory(pages);
1833 int in_gate_area_no_task(unsigned long addr)
1838 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1843 EXPORT_SYMBOL(filemap_fault);
1846 * Access another process' address space.
1847 * - source/target buffer must be kernel space
1849 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1851 struct vm_area_struct *vma;
1852 struct mm_struct *mm;
1854 if (addr + len < addr)
1857 mm = get_task_mm(tsk);
1861 down_read(&mm->mmap_sem);
1863 /* the access must start within one of the target process's mappings */
1864 vma = find_vma(mm, addr);
1866 /* don't overrun this mapping */
1867 if (addr + len >= vma->vm_end)
1868 len = vma->vm_end - addr;
1870 /* only read or write mappings where it is permitted */
1871 if (write && vma->vm_flags & VM_MAYWRITE)
1872 len -= copy_to_user((void *) addr, buf, len);
1873 else if (!write && vma->vm_flags & VM_MAYREAD)
1874 len -= copy_from_user(buf, (void *) addr, len);
1881 up_read(&mm->mmap_sem);