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>
32 #include <linux/sched/sysctl.h>
34 #include <asm/uaccess.h>
36 #include <asm/tlbflush.h>
37 #include <asm/mmu_context.h>
41 #define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43 #define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45 #define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
48 #define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50 #define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52 #define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
58 unsigned long max_mapnr;
59 unsigned long num_physpages;
60 unsigned long highest_memmap_pfn;
61 struct percpu_counter vm_committed_as;
62 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
63 int sysctl_overcommit_ratio = 50; /* default is 50% */
64 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
65 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
66 unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
67 unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
68 int heap_stack_gap = 0;
70 atomic_long_t mmap_pages_allocated;
73 * The global memory commitment made in the system can be a metric
74 * that can be used to drive ballooning decisions when Linux is hosted
75 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
76 * balancing memory across competing virtual machines that are hosted.
77 * Several metrics drive this policy engine including the guest reported
80 unsigned long vm_memory_committed(void)
82 return percpu_counter_read_positive(&vm_committed_as);
85 EXPORT_SYMBOL_GPL(vm_memory_committed);
87 EXPORT_SYMBOL(mem_map);
88 EXPORT_SYMBOL(num_physpages);
90 /* list of mapped, potentially shareable regions */
91 static struct kmem_cache *vm_region_jar;
92 struct rb_root nommu_region_tree = RB_ROOT;
93 DECLARE_RWSEM(nommu_region_sem);
95 const struct vm_operations_struct generic_file_vm_ops = {
99 * Return the total memory allocated for this pointer, not
100 * just what the caller asked for.
102 * Doesn't have to be accurate, i.e. may have races.
104 unsigned int kobjsize(const void *objp)
109 * If the object we have should not have ksize performed on it,
112 if (!objp || !virt_addr_valid(objp))
115 page = virt_to_head_page(objp);
118 * If the allocator sets PageSlab, we know the pointer came from
125 * If it's not a compound page, see if we have a matching VMA
126 * region. This test is intentionally done in reverse order,
127 * so if there's no VMA, we still fall through and hand back
128 * PAGE_SIZE for 0-order pages.
130 if (!PageCompound(page)) {
131 struct vm_area_struct *vma;
133 vma = find_vma(current->mm, (unsigned long)objp);
135 return vma->vm_end - vma->vm_start;
139 * The ksize() function is only guaranteed to work for pointers
140 * returned by kmalloc(). So handle arbitrary pointers here.
142 return PAGE_SIZE << compound_order(page);
145 long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
146 unsigned long start, unsigned long nr_pages,
147 unsigned int foll_flags, struct page **pages,
148 struct vm_area_struct **vmas, int *nonblocking)
150 struct vm_area_struct *vma;
151 unsigned long vm_flags;
154 /* calculate required read or write permissions.
155 * If FOLL_FORCE is set, we only require the "MAY" flags.
157 vm_flags = (foll_flags & FOLL_WRITE) ?
158 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
159 vm_flags &= (foll_flags & FOLL_FORCE) ?
160 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
162 for (i = 0; i < nr_pages; i++) {
163 vma = find_vma(mm, start);
165 goto finish_or_fault;
167 /* protect what we can, including chardevs */
168 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
169 !(vm_flags & vma->vm_flags))
170 goto finish_or_fault;
173 pages[i] = virt_to_page(start);
175 page_cache_get(pages[i]);
179 start = (start + PAGE_SIZE) & PAGE_MASK;
185 return i ? : -EFAULT;
189 * get a list of pages in an address range belonging to the specified process
190 * and indicate the VMA that covers each page
191 * - this is potentially dodgy as we may end incrementing the page count of a
192 * slab page or a secondary page from a compound page
193 * - don't permit access to VMAs that don't support it, such as I/O mappings
195 long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
196 unsigned long start, unsigned long nr_pages,
197 int write, int force, struct page **pages,
198 struct vm_area_struct **vmas)
207 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
210 EXPORT_SYMBOL(get_user_pages);
213 * follow_pfn - look up PFN at a user virtual address
214 * @vma: memory mapping
215 * @address: user virtual address
216 * @pfn: location to store found PFN
218 * Only IO mappings and raw PFN mappings are allowed.
220 * Returns zero and the pfn at @pfn on success, -ve otherwise.
222 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
225 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
228 *pfn = address >> PAGE_SHIFT;
231 EXPORT_SYMBOL(follow_pfn);
233 LIST_HEAD(vmap_area_list);
235 void vfree(const void *addr)
239 EXPORT_SYMBOL(vfree);
241 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
244 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
245 * returns only a logical address.
247 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
249 EXPORT_SYMBOL(__vmalloc);
251 void *vmalloc_user(unsigned long size)
255 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
258 struct vm_area_struct *vma;
260 down_write(¤t->mm->mmap_sem);
261 vma = find_vma(current->mm, (unsigned long)ret);
263 vma->vm_flags |= VM_USERMAP;
264 up_write(¤t->mm->mmap_sem);
269 EXPORT_SYMBOL(vmalloc_user);
271 struct page *vmalloc_to_page(const void *addr)
273 return virt_to_page(addr);
275 EXPORT_SYMBOL(vmalloc_to_page);
277 unsigned long vmalloc_to_pfn(const void *addr)
279 return page_to_pfn(virt_to_page(addr));
281 EXPORT_SYMBOL(vmalloc_to_pfn);
283 long vread(char *buf, char *addr, unsigned long count)
285 /* Don't allow overflow */
286 if ((unsigned long) buf + count < count)
287 count = -(unsigned long) buf;
289 memcpy(buf, addr, count);
293 long vwrite(char *buf, char *addr, unsigned long count)
295 /* Don't allow overflow */
296 if ((unsigned long) addr + count < count)
297 count = -(unsigned long) addr;
299 memcpy(addr, buf, count);
304 * vmalloc - allocate virtually continguos memory
306 * @size: allocation size
308 * Allocate enough pages to cover @size from the page level
309 * allocator and map them into continguos kernel virtual space.
311 * For tight control over page level allocator and protection flags
312 * use __vmalloc() instead.
314 void *vmalloc(unsigned long size)
316 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
318 EXPORT_SYMBOL(vmalloc);
321 * vzalloc - allocate virtually continguos memory with zero fill
323 * @size: allocation size
325 * Allocate enough pages to cover @size from the page level
326 * allocator and map them into continguos kernel virtual space.
327 * The memory allocated is set to zero.
329 * For tight control over page level allocator and protection flags
330 * use __vmalloc() instead.
332 void *vzalloc(unsigned long size)
334 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
337 EXPORT_SYMBOL(vzalloc);
340 * vmalloc_node - allocate memory on a specific node
341 * @size: allocation size
344 * Allocate enough pages to cover @size from the page level
345 * allocator and map them into contiguous kernel virtual space.
347 * For tight control over page level allocator and protection flags
348 * use __vmalloc() instead.
350 void *vmalloc_node(unsigned long size, int node)
352 return vmalloc(size);
354 EXPORT_SYMBOL(vmalloc_node);
357 * vzalloc_node - allocate memory on a specific node with zero fill
358 * @size: allocation size
361 * Allocate enough pages to cover @size from the page level
362 * allocator and map them into contiguous kernel virtual space.
363 * The memory allocated is set to zero.
365 * For tight control over page level allocator and protection flags
366 * use __vmalloc() instead.
368 void *vzalloc_node(unsigned long size, int node)
370 return vzalloc(size);
372 EXPORT_SYMBOL(vzalloc_node);
374 #ifndef PAGE_KERNEL_EXEC
375 # define PAGE_KERNEL_EXEC PAGE_KERNEL
379 * vmalloc_exec - allocate virtually contiguous, executable memory
380 * @size: allocation size
382 * Kernel-internal function to allocate enough pages to cover @size
383 * the page level allocator and map them into contiguous and
384 * executable kernel virtual space.
386 * For tight control over page level allocator and protection flags
387 * use __vmalloc() instead.
390 void *vmalloc_exec(unsigned long size)
392 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
396 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
397 * @size: allocation size
399 * Allocate enough 32bit PA addressable pages to cover @size from the
400 * page level allocator and map them into continguos kernel virtual space.
402 void *vmalloc_32(unsigned long size)
404 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
406 EXPORT_SYMBOL(vmalloc_32);
409 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
410 * @size: allocation size
412 * The resulting memory area is 32bit addressable and zeroed so it can be
413 * mapped to userspace without leaking data.
415 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
416 * remap_vmalloc_range() are permissible.
418 void *vmalloc_32_user(unsigned long size)
421 * We'll have to sort out the ZONE_DMA bits for 64-bit,
422 * but for now this can simply use vmalloc_user() directly.
424 return vmalloc_user(size);
426 EXPORT_SYMBOL(vmalloc_32_user);
428 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
435 void vunmap(const void *addr)
439 EXPORT_SYMBOL(vunmap);
441 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
446 EXPORT_SYMBOL(vm_map_ram);
448 void vm_unmap_ram(const void *mem, unsigned int count)
452 EXPORT_SYMBOL(vm_unmap_ram);
454 void vm_unmap_aliases(void)
457 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
460 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
463 void __attribute__((weak)) vmalloc_sync_all(void)
468 * alloc_vm_area - allocate a range of kernel address space
469 * @size: size of the area
471 * Returns: NULL on failure, vm_struct on success
473 * This function reserves a range of kernel address space, and
474 * allocates pagetables to map that range. No actual mappings
475 * are created. If the kernel address space is not shared
476 * between processes, it syncs the pagetable across all
479 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
484 EXPORT_SYMBOL_GPL(alloc_vm_area);
486 void free_vm_area(struct vm_struct *area)
490 EXPORT_SYMBOL_GPL(free_vm_area);
492 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
497 EXPORT_SYMBOL(vm_insert_page);
500 * sys_brk() for the most part doesn't need the global kernel
501 * lock, except when an application is doing something nasty
502 * like trying to un-brk an area that has already been mapped
503 * to a regular file. in this case, the unmapping will need
504 * to invoke file system routines that need the global lock.
506 SYSCALL_DEFINE1(brk, unsigned long, brk)
508 struct mm_struct *mm = current->mm;
510 if (brk < mm->start_brk || brk > mm->context.end_brk)
517 * Always allow shrinking brk
519 if (brk <= mm->brk) {
525 * Ok, looks good - let it rip.
527 flush_icache_range(mm->brk, brk);
528 return mm->brk = brk;
532 * initialise the VMA and region record slabs
534 void __init mmap_init(void)
538 ret = percpu_counter_init(&vm_committed_as, 0);
540 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
544 * validate the region tree
545 * - the caller must hold the region lock
547 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
548 static noinline void validate_nommu_regions(void)
550 struct vm_region *region, *last;
551 struct rb_node *p, *lastp;
553 lastp = rb_first(&nommu_region_tree);
557 last = rb_entry(lastp, struct vm_region, vm_rb);
558 BUG_ON(unlikely(last->vm_end <= last->vm_start));
559 BUG_ON(unlikely(last->vm_top < last->vm_end));
561 while ((p = rb_next(lastp))) {
562 region = rb_entry(p, struct vm_region, vm_rb);
563 last = rb_entry(lastp, struct vm_region, vm_rb);
565 BUG_ON(unlikely(region->vm_end <= region->vm_start));
566 BUG_ON(unlikely(region->vm_top < region->vm_end));
567 BUG_ON(unlikely(region->vm_start < last->vm_top));
573 static void validate_nommu_regions(void)
579 * add a region into the global tree
581 static void add_nommu_region(struct vm_region *region)
583 struct vm_region *pregion;
584 struct rb_node **p, *parent;
586 validate_nommu_regions();
589 p = &nommu_region_tree.rb_node;
592 pregion = rb_entry(parent, struct vm_region, vm_rb);
593 if (region->vm_start < pregion->vm_start)
595 else if (region->vm_start > pregion->vm_start)
597 else if (pregion == region)
603 rb_link_node(®ion->vm_rb, parent, p);
604 rb_insert_color(®ion->vm_rb, &nommu_region_tree);
606 validate_nommu_regions();
610 * delete a region from the global tree
612 static void delete_nommu_region(struct vm_region *region)
614 BUG_ON(!nommu_region_tree.rb_node);
616 validate_nommu_regions();
617 rb_erase(®ion->vm_rb, &nommu_region_tree);
618 validate_nommu_regions();
622 * free a contiguous series of pages
624 static void free_page_series(unsigned long from, unsigned long to)
626 for (; from < to; from += PAGE_SIZE) {
627 struct page *page = virt_to_page(from);
629 kdebug("- free %lx", from);
630 atomic_long_dec(&mmap_pages_allocated);
631 if (page_count(page) != 1)
632 kdebug("free page %p: refcount not one: %d",
633 page, page_count(page));
639 * release a reference to a region
640 * - the caller must hold the region semaphore for writing, which this releases
641 * - the region may not have been added to the tree yet, in which case vm_top
642 * will equal vm_start
644 static void __put_nommu_region(struct vm_region *region)
645 __releases(nommu_region_sem)
647 kenter("%p{%d}", region, region->vm_usage);
649 BUG_ON(!nommu_region_tree.rb_node);
651 if (--region->vm_usage == 0) {
652 if (region->vm_top > region->vm_start)
653 delete_nommu_region(region);
654 up_write(&nommu_region_sem);
657 fput(region->vm_file);
659 /* IO memory and memory shared directly out of the pagecache
660 * from ramfs/tmpfs mustn't be released here */
661 if (region->vm_flags & VM_MAPPED_COPY) {
662 kdebug("free series");
663 free_page_series(region->vm_start, region->vm_top);
665 kmem_cache_free(vm_region_jar, region);
667 up_write(&nommu_region_sem);
672 * release a reference to a region
674 static void put_nommu_region(struct vm_region *region)
676 down_write(&nommu_region_sem);
677 __put_nommu_region(region);
681 * update protection on a vma
683 static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
686 struct mm_struct *mm = vma->vm_mm;
687 long start = vma->vm_start & PAGE_MASK;
688 while (start < vma->vm_end) {
689 protect_page(mm, start, flags);
692 update_protections(mm);
697 * add a VMA into a process's mm_struct in the appropriate place in the list
698 * and tree and add to the address space's page tree also if not an anonymous
700 * - should be called with mm->mmap_sem held writelocked
702 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
704 struct vm_area_struct *pvma, *prev;
705 struct address_space *mapping;
706 struct rb_node **p, *parent, *rb_prev;
710 BUG_ON(!vma->vm_region);
715 protect_vma(vma, vma->vm_flags);
717 /* add the VMA to the mapping */
719 mapping = vma->vm_file->f_mapping;
721 mutex_lock(&mapping->i_mmap_mutex);
722 flush_dcache_mmap_lock(mapping);
723 vma_interval_tree_insert(vma, &mapping->i_mmap);
724 flush_dcache_mmap_unlock(mapping);
725 mutex_unlock(&mapping->i_mmap_mutex);
728 /* add the VMA to the tree */
729 parent = rb_prev = NULL;
730 p = &mm->mm_rb.rb_node;
733 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
735 /* sort by: start addr, end addr, VMA struct addr in that order
736 * (the latter is necessary as we may get identical VMAs) */
737 if (vma->vm_start < pvma->vm_start)
739 else if (vma->vm_start > pvma->vm_start) {
742 } else if (vma->vm_end < pvma->vm_end)
744 else if (vma->vm_end > pvma->vm_end) {
747 } else if (vma < pvma)
749 else if (vma > pvma) {
756 rb_link_node(&vma->vm_rb, parent, p);
757 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
759 /* add VMA to the VMA list also */
762 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
764 __vma_link_list(mm, vma, prev, parent);
768 * delete a VMA from its owning mm_struct and address space
770 static void delete_vma_from_mm(struct vm_area_struct *vma)
772 struct address_space *mapping;
773 struct mm_struct *mm = vma->vm_mm;
780 if (mm->mmap_cache == vma)
781 mm->mmap_cache = NULL;
783 /* remove the VMA from the mapping */
785 mapping = vma->vm_file->f_mapping;
787 mutex_lock(&mapping->i_mmap_mutex);
788 flush_dcache_mmap_lock(mapping);
789 vma_interval_tree_remove(vma, &mapping->i_mmap);
790 flush_dcache_mmap_unlock(mapping);
791 mutex_unlock(&mapping->i_mmap_mutex);
794 /* remove from the MM's tree and list */
795 rb_erase(&vma->vm_rb, &mm->mm_rb);
798 vma->vm_prev->vm_next = vma->vm_next;
800 mm->mmap = vma->vm_next;
803 vma->vm_next->vm_prev = vma->vm_prev;
807 * destroy a VMA record
809 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
812 if (vma->vm_ops && vma->vm_ops->close)
813 vma->vm_ops->close(vma);
816 put_nommu_region(vma->vm_region);
817 kmem_cache_free(vm_area_cachep, vma);
821 * look up the first VMA in which addr resides, NULL if none
822 * - should be called with mm->mmap_sem at least held readlocked
824 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
826 struct vm_area_struct *vma;
828 /* check the cache first */
829 vma = ACCESS_ONCE(mm->mmap_cache);
830 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
833 /* trawl the list (there may be multiple mappings in which addr
835 for (vma = mm->mmap; vma; vma = vma->vm_next) {
836 if (vma->vm_start > addr)
838 if (vma->vm_end > addr) {
839 mm->mmap_cache = vma;
846 EXPORT_SYMBOL(find_vma);
850 * - we don't extend stack VMAs under NOMMU conditions
852 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
854 return find_vma(mm, addr);
858 * expand a stack to a given address
859 * - not supported under NOMMU conditions
861 int expand_stack(struct vm_area_struct *vma, unsigned long address)
867 * look up the first VMA exactly that exactly matches addr
868 * - should be called with mm->mmap_sem at least held readlocked
870 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
874 struct vm_area_struct *vma;
875 unsigned long end = addr + len;
877 /* check the cache first */
878 vma = mm->mmap_cache;
879 if (vma && vma->vm_start == addr && vma->vm_end == end)
882 /* trawl the list (there may be multiple mappings in which addr
884 for (vma = mm->mmap; vma; vma = vma->vm_next) {
885 if (vma->vm_start < addr)
887 if (vma->vm_start > addr)
889 if (vma->vm_end == end) {
890 mm->mmap_cache = vma;
899 * determine whether a mapping should be permitted and, if so, what sort of
900 * mapping we're capable of supporting
902 static int validate_mmap_request(struct file *file,
908 unsigned long *_capabilities)
910 unsigned long capabilities, rlen;
913 /* do the simple checks first */
914 if (flags & MAP_FIXED) {
916 "%d: Can't do fixed-address/overlay mmap of RAM\n",
921 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
922 (flags & MAP_TYPE) != MAP_SHARED)
928 /* Careful about overflows.. */
929 rlen = PAGE_ALIGN(len);
930 if (!rlen || rlen > TASK_SIZE)
933 /* offset overflow? */
934 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
938 /* validate file mapping requests */
939 struct address_space *mapping;
941 /* files must support mmap */
942 if (!file->f_op || !file->f_op->mmap)
945 /* work out if what we've got could possibly be shared
946 * - we support chardevs that provide their own "memory"
947 * - we support files/blockdevs that are memory backed
949 mapping = file->f_mapping;
951 mapping = file_inode(file)->i_mapping;
954 if (mapping && mapping->backing_dev_info)
955 capabilities = mapping->backing_dev_info->capabilities;
958 /* no explicit capabilities set, so assume some
960 switch (file_inode(file)->i_mode & S_IFMT) {
963 capabilities = BDI_CAP_MAP_COPY;
978 /* eliminate any capabilities that we can't support on this
980 if (!file->f_op->get_unmapped_area)
981 capabilities &= ~BDI_CAP_MAP_DIRECT;
982 if (!file->f_op->read)
983 capabilities &= ~BDI_CAP_MAP_COPY;
985 /* The file shall have been opened with read permission. */
986 if (!(file->f_mode & FMODE_READ))
989 if (flags & MAP_SHARED) {
990 /* do checks for writing, appending and locking */
991 if ((prot & PROT_WRITE) &&
992 !(file->f_mode & FMODE_WRITE))
995 if (IS_APPEND(file_inode(file)) &&
996 (file->f_mode & FMODE_WRITE))
999 if (locks_verify_locked(file_inode(file)))
1002 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1005 /* we mustn't privatise shared mappings */
1006 capabilities &= ~BDI_CAP_MAP_COPY;
1009 /* we're going to read the file into private memory we
1011 if (!(capabilities & BDI_CAP_MAP_COPY))
1014 /* we don't permit a private writable mapping to be
1015 * shared with the backing device */
1016 if (prot & PROT_WRITE)
1017 capabilities &= ~BDI_CAP_MAP_DIRECT;
1020 if (capabilities & BDI_CAP_MAP_DIRECT) {
1021 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1022 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1023 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1025 capabilities &= ~BDI_CAP_MAP_DIRECT;
1026 if (flags & MAP_SHARED) {
1028 "MAP_SHARED not completely supported on !MMU\n");
1034 /* handle executable mappings and implied executable
1036 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1037 if (prot & PROT_EXEC)
1040 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1041 /* handle implication of PROT_EXEC by PROT_READ */
1042 if (current->personality & READ_IMPLIES_EXEC) {
1043 if (capabilities & BDI_CAP_EXEC_MAP)
1047 else if ((prot & PROT_READ) &&
1048 (prot & PROT_EXEC) &&
1049 !(capabilities & BDI_CAP_EXEC_MAP)
1051 /* backing file is not executable, try to copy */
1052 capabilities &= ~BDI_CAP_MAP_DIRECT;
1056 /* anonymous mappings are always memory backed and can be
1059 capabilities = BDI_CAP_MAP_COPY;
1061 /* handle PROT_EXEC implication by PROT_READ */
1062 if ((prot & PROT_READ) &&
1063 (current->personality & READ_IMPLIES_EXEC))
1067 /* allow the security API to have its say */
1068 ret = security_mmap_addr(addr);
1073 *_capabilities = capabilities;
1078 * we've determined that we can make the mapping, now translate what we
1079 * now know into VMA flags
1081 static unsigned long determine_vm_flags(struct file *file,
1083 unsigned long flags,
1084 unsigned long capabilities)
1086 unsigned long vm_flags;
1088 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1089 /* vm_flags |= mm->def_flags; */
1091 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1092 /* attempt to share read-only copies of mapped file chunks */
1093 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1094 if (file && !(prot & PROT_WRITE))
1095 vm_flags |= VM_MAYSHARE;
1097 /* overlay a shareable mapping on the backing device or inode
1098 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1100 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
1101 if (flags & MAP_SHARED)
1102 vm_flags |= VM_SHARED;
1105 /* refuse to let anyone share private mappings with this process if
1106 * it's being traced - otherwise breakpoints set in it may interfere
1107 * with another untraced process
1109 if ((flags & MAP_PRIVATE) && current->ptrace)
1110 vm_flags &= ~VM_MAYSHARE;
1116 * set up a shared mapping on a file (the driver or filesystem provides and
1119 static int do_mmap_shared_file(struct vm_area_struct *vma)
1123 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1125 vma->vm_region->vm_top = vma->vm_region->vm_end;
1131 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1132 * opposed to tried but failed) so we can only give a suitable error as
1133 * it's not possible to make a private copy if MAP_SHARED was given */
1138 * set up a private mapping or an anonymous shared mapping
1140 static int do_mmap_private(struct vm_area_struct *vma,
1141 struct vm_region *region,
1143 unsigned long capabilities)
1146 unsigned long total, point, n;
1150 /* invoke the file's mapping function so that it can keep track of
1151 * shared mappings on devices or memory
1152 * - VM_MAYSHARE will be set if it may attempt to share
1154 if (capabilities & BDI_CAP_MAP_DIRECT) {
1155 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1157 /* shouldn't return success if we're not sharing */
1158 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1159 vma->vm_region->vm_top = vma->vm_region->vm_end;
1165 /* getting an ENOSYS error indicates that direct mmap isn't
1166 * possible (as opposed to tried but failed) so we'll try to
1167 * make a private copy of the data and map that instead */
1171 /* allocate some memory to hold the mapping
1172 * - note that this may not return a page-aligned address if the object
1173 * we're allocating is smaller than a page
1175 order = get_order(len);
1176 kdebug("alloc order %d for %lx", order, len);
1178 pages = alloc_pages(GFP_KERNEL, order);
1183 atomic_long_add(total, &mmap_pages_allocated);
1185 point = len >> PAGE_SHIFT;
1187 /* we allocated a power-of-2 sized page set, so we may want to trim off
1189 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1190 while (total > point) {
1191 order = ilog2(total - point);
1193 kdebug("shave %lu/%lu @%lu", n, total - point, total);
1194 atomic_long_sub(n, &mmap_pages_allocated);
1196 set_page_refcounted(pages + total);
1197 __free_pages(pages + total, order);
1201 for (point = 1; point < total; point++)
1202 set_page_refcounted(&pages[point]);
1204 base = page_address(pages);
1205 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1206 region->vm_start = (unsigned long) base;
1207 region->vm_end = region->vm_start + len;
1208 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1210 vma->vm_start = region->vm_start;
1211 vma->vm_end = region->vm_start + len;
1214 /* read the contents of a file into the copy */
1215 mm_segment_t old_fs;
1218 fpos = vma->vm_pgoff;
1219 fpos <<= PAGE_SHIFT;
1223 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
1229 /* clear the last little bit */
1231 memset(base + ret, 0, len - ret);
1238 free_page_series(region->vm_start, region->vm_top);
1239 region->vm_start = vma->vm_start = 0;
1240 region->vm_end = vma->vm_end = 0;
1245 printk("Allocation of length %lu from process %d (%s) failed\n",
1246 len, current->pid, current->comm);
1252 * handle mapping creation for uClinux
1254 unsigned long do_mmap_pgoff(struct file *file,
1258 unsigned long flags,
1259 unsigned long pgoff,
1260 unsigned long *populate)
1262 struct vm_area_struct *vma;
1263 struct vm_region *region;
1265 unsigned long capabilities, vm_flags, result;
1268 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1272 /* decide whether we should attempt the mapping, and if so what sort of
1274 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1277 kleave(" = %d [val]", ret);
1281 /* we ignore the address hint */
1283 len = PAGE_ALIGN(len);
1285 /* we've determined that we can make the mapping, now translate what we
1286 * now know into VMA flags */
1287 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1289 /* we're going to need to record the mapping */
1290 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1292 goto error_getting_region;
1294 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1296 goto error_getting_vma;
1298 region->vm_usage = 1;
1299 region->vm_flags = vm_flags;
1300 region->vm_pgoff = pgoff;
1302 INIT_LIST_HEAD(&vma->anon_vma_chain);
1303 vma->vm_flags = vm_flags;
1304 vma->vm_pgoff = pgoff;
1307 region->vm_file = get_file(file);
1308 vma->vm_file = get_file(file);
1311 down_write(&nommu_region_sem);
1313 /* if we want to share, we need to check for regions created by other
1314 * mmap() calls that overlap with our proposed mapping
1315 * - we can only share with a superset match on most regular files
1316 * - shared mappings on character devices and memory backed files are
1317 * permitted to overlap inexactly as far as we are concerned for in
1318 * these cases, sharing is handled in the driver or filesystem rather
1321 if (vm_flags & VM_MAYSHARE) {
1322 struct vm_region *pregion;
1323 unsigned long pglen, rpglen, pgend, rpgend, start;
1325 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1326 pgend = pgoff + pglen;
1328 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1329 pregion = rb_entry(rb, struct vm_region, vm_rb);
1331 if (!(pregion->vm_flags & VM_MAYSHARE))
1334 /* search for overlapping mappings on the same file */
1335 if (file_inode(pregion->vm_file) !=
1339 if (pregion->vm_pgoff >= pgend)
1342 rpglen = pregion->vm_end - pregion->vm_start;
1343 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1344 rpgend = pregion->vm_pgoff + rpglen;
1345 if (pgoff >= rpgend)
1348 /* handle inexactly overlapping matches between
1350 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1351 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1352 /* new mapping is not a subset of the region */
1353 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1354 goto sharing_violation;
1358 /* we've found a region we can share */
1359 pregion->vm_usage++;
1360 vma->vm_region = pregion;
1361 start = pregion->vm_start;
1362 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1363 vma->vm_start = start;
1364 vma->vm_end = start + len;
1366 if (pregion->vm_flags & VM_MAPPED_COPY) {
1367 kdebug("share copy");
1368 vma->vm_flags |= VM_MAPPED_COPY;
1370 kdebug("share mmap");
1371 ret = do_mmap_shared_file(vma);
1373 vma->vm_region = NULL;
1376 pregion->vm_usage--;
1378 goto error_just_free;
1381 fput(region->vm_file);
1382 kmem_cache_free(vm_region_jar, region);
1388 /* obtain the address at which to make a shared mapping
1389 * - this is the hook for quasi-memory character devices to
1390 * tell us the location of a shared mapping
1392 if (capabilities & BDI_CAP_MAP_DIRECT) {
1393 addr = file->f_op->get_unmapped_area(file, addr, len,
1395 if (IS_ERR_VALUE(addr)) {
1398 goto error_just_free;
1400 /* the driver refused to tell us where to site
1401 * the mapping so we'll have to attempt to copy
1404 if (!(capabilities & BDI_CAP_MAP_COPY))
1405 goto error_just_free;
1407 capabilities &= ~BDI_CAP_MAP_DIRECT;
1409 vma->vm_start = region->vm_start = addr;
1410 vma->vm_end = region->vm_end = addr + len;
1415 vma->vm_region = region;
1417 /* set up the mapping
1418 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1420 if (file && vma->vm_flags & VM_SHARED)
1421 ret = do_mmap_shared_file(vma);
1423 ret = do_mmap_private(vma, region, len, capabilities);
1425 goto error_just_free;
1426 add_nommu_region(region);
1428 /* clear anonymous mappings that don't ask for uninitialized data */
1429 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1430 memset((void *)region->vm_start, 0,
1431 region->vm_end - region->vm_start);
1433 /* okay... we have a mapping; now we have to register it */
1434 result = vma->vm_start;
1436 current->mm->total_vm += len >> PAGE_SHIFT;
1439 add_vma_to_mm(current->mm, vma);
1441 /* we flush the region from the icache only when the first executable
1442 * mapping of it is made */
1443 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1444 flush_icache_range(region->vm_start, region->vm_end);
1445 region->vm_icache_flushed = true;
1448 up_write(&nommu_region_sem);
1450 kleave(" = %lx", result);
1454 up_write(&nommu_region_sem);
1456 if (region->vm_file)
1457 fput(region->vm_file);
1458 kmem_cache_free(vm_region_jar, region);
1461 kmem_cache_free(vm_area_cachep, vma);
1462 kleave(" = %d", ret);
1466 up_write(&nommu_region_sem);
1467 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1472 kmem_cache_free(vm_region_jar, region);
1473 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1474 " from process %d failed\n",
1479 error_getting_region:
1480 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1481 " from process %d failed\n",
1487 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1488 unsigned long, prot, unsigned long, flags,
1489 unsigned long, fd, unsigned long, pgoff)
1491 struct file *file = NULL;
1492 unsigned long retval = -EBADF;
1494 audit_mmap_fd(fd, flags);
1495 if (!(flags & MAP_ANONYMOUS)) {
1501 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1503 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1511 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1512 struct mmap_arg_struct {
1516 unsigned long flags;
1518 unsigned long offset;
1521 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1523 struct mmap_arg_struct a;
1525 if (copy_from_user(&a, arg, sizeof(a)))
1527 if (a.offset & ~PAGE_MASK)
1530 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1531 a.offset >> PAGE_SHIFT);
1533 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1536 * split a vma into two pieces at address 'addr', a new vma is allocated either
1537 * for the first part or the tail.
1539 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1540 unsigned long addr, int new_below)
1542 struct vm_area_struct *new;
1543 struct vm_region *region;
1544 unsigned long npages;
1548 /* we're only permitted to split anonymous regions (these should have
1549 * only a single usage on the region) */
1553 if (mm->map_count >= sysctl_max_map_count)
1556 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1560 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1562 kmem_cache_free(vm_region_jar, region);
1566 /* most fields are the same, copy all, and then fixup */
1568 *region = *vma->vm_region;
1569 new->vm_region = region;
1571 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1574 region->vm_top = region->vm_end = new->vm_end = addr;
1576 region->vm_start = new->vm_start = addr;
1577 region->vm_pgoff = new->vm_pgoff += npages;
1580 if (new->vm_ops && new->vm_ops->open)
1581 new->vm_ops->open(new);
1583 delete_vma_from_mm(vma);
1584 down_write(&nommu_region_sem);
1585 delete_nommu_region(vma->vm_region);
1587 vma->vm_region->vm_start = vma->vm_start = addr;
1588 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1590 vma->vm_region->vm_end = vma->vm_end = addr;
1591 vma->vm_region->vm_top = addr;
1593 add_nommu_region(vma->vm_region);
1594 add_nommu_region(new->vm_region);
1595 up_write(&nommu_region_sem);
1596 add_vma_to_mm(mm, vma);
1597 add_vma_to_mm(mm, new);
1602 * shrink a VMA by removing the specified chunk from either the beginning or
1605 static int shrink_vma(struct mm_struct *mm,
1606 struct vm_area_struct *vma,
1607 unsigned long from, unsigned long to)
1609 struct vm_region *region;
1613 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1615 delete_vma_from_mm(vma);
1616 if (from > vma->vm_start)
1620 add_vma_to_mm(mm, vma);
1622 /* cut the backing region down to size */
1623 region = vma->vm_region;
1624 BUG_ON(region->vm_usage != 1);
1626 down_write(&nommu_region_sem);
1627 delete_nommu_region(region);
1628 if (from > region->vm_start) {
1629 to = region->vm_top;
1630 region->vm_top = region->vm_end = from;
1632 region->vm_start = to;
1634 add_nommu_region(region);
1635 up_write(&nommu_region_sem);
1637 free_page_series(from, to);
1643 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1644 * VMA, though it need not cover the whole VMA
1646 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1648 struct vm_area_struct *vma;
1652 kenter(",%lx,%zx", start, len);
1654 len = PAGE_ALIGN(len);
1660 /* find the first potentially overlapping VMA */
1661 vma = find_vma(mm, start);
1663 static int limit = 0;
1666 "munmap of memory not mmapped by process %d"
1667 " (%s): 0x%lx-0x%lx\n",
1668 current->pid, current->comm,
1669 start, start + len - 1);
1675 /* we're allowed to split an anonymous VMA but not a file-backed one */
1678 if (start > vma->vm_start) {
1679 kleave(" = -EINVAL [miss]");
1682 if (end == vma->vm_end)
1683 goto erase_whole_vma;
1686 kleave(" = -EINVAL [split file]");
1689 /* the chunk must be a subset of the VMA found */
1690 if (start == vma->vm_start && end == vma->vm_end)
1691 goto erase_whole_vma;
1692 if (start < vma->vm_start || end > vma->vm_end) {
1693 kleave(" = -EINVAL [superset]");
1696 if (start & ~PAGE_MASK) {
1697 kleave(" = -EINVAL [unaligned start]");
1700 if (end != vma->vm_end && end & ~PAGE_MASK) {
1701 kleave(" = -EINVAL [unaligned split]");
1704 if (start != vma->vm_start && end != vma->vm_end) {
1705 ret = split_vma(mm, vma, start, 1);
1707 kleave(" = %d [split]", ret);
1711 return shrink_vma(mm, vma, start, end);
1715 delete_vma_from_mm(vma);
1716 delete_vma(mm, vma);
1720 EXPORT_SYMBOL(do_munmap);
1722 int vm_munmap(unsigned long addr, size_t len)
1724 struct mm_struct *mm = current->mm;
1727 down_write(&mm->mmap_sem);
1728 ret = do_munmap(mm, addr, len);
1729 up_write(&mm->mmap_sem);
1732 EXPORT_SYMBOL(vm_munmap);
1734 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1736 return vm_munmap(addr, len);
1740 * release all the mappings made in a process's VM space
1742 void exit_mmap(struct mm_struct *mm)
1744 struct vm_area_struct *vma;
1753 while ((vma = mm->mmap)) {
1754 mm->mmap = vma->vm_next;
1755 delete_vma_from_mm(vma);
1756 delete_vma(mm, vma);
1763 unsigned long vm_brk(unsigned long addr, unsigned long len)
1769 * expand (or shrink) an existing mapping, potentially moving it at the same
1770 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1772 * under NOMMU conditions, we only permit changing a mapping's size, and only
1773 * as long as it stays within the region allocated by do_mmap_private() and the
1774 * block is not shareable
1776 * MREMAP_FIXED is not supported under NOMMU conditions
1778 static unsigned long do_mremap(unsigned long addr,
1779 unsigned long old_len, unsigned long new_len,
1780 unsigned long flags, unsigned long new_addr)
1782 struct vm_area_struct *vma;
1784 /* insanity checks first */
1785 old_len = PAGE_ALIGN(old_len);
1786 new_len = PAGE_ALIGN(new_len);
1787 if (old_len == 0 || new_len == 0)
1788 return (unsigned long) -EINVAL;
1790 if (addr & ~PAGE_MASK)
1793 if (flags & MREMAP_FIXED && new_addr != addr)
1794 return (unsigned long) -EINVAL;
1796 vma = find_vma_exact(current->mm, addr, old_len);
1798 return (unsigned long) -EINVAL;
1800 if (vma->vm_end != vma->vm_start + old_len)
1801 return (unsigned long) -EFAULT;
1803 if (vma->vm_flags & VM_MAYSHARE)
1804 return (unsigned long) -EPERM;
1806 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1807 return (unsigned long) -ENOMEM;
1809 /* all checks complete - do it */
1810 vma->vm_end = vma->vm_start + new_len;
1811 return vma->vm_start;
1814 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1815 unsigned long, new_len, unsigned long, flags,
1816 unsigned long, new_addr)
1820 down_write(¤t->mm->mmap_sem);
1821 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1822 up_write(¤t->mm->mmap_sem);
1826 struct page *follow_page_mask(struct vm_area_struct *vma,
1827 unsigned long address, unsigned int flags,
1828 unsigned int *page_mask)
1834 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1835 unsigned long pfn, unsigned long size, pgprot_t prot)
1837 if (addr != (pfn << PAGE_SHIFT))
1840 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1843 EXPORT_SYMBOL(remap_pfn_range);
1845 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1847 unsigned long pfn = start >> PAGE_SHIFT;
1848 unsigned long vm_len = vma->vm_end - vma->vm_start;
1850 pfn += vma->vm_pgoff;
1851 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1853 EXPORT_SYMBOL(vm_iomap_memory);
1855 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1856 unsigned long pgoff)
1858 unsigned int size = vma->vm_end - vma->vm_start;
1860 if (!(vma->vm_flags & VM_USERMAP))
1863 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1864 vma->vm_end = vma->vm_start + size;
1868 EXPORT_SYMBOL(remap_vmalloc_range);
1870 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1871 unsigned long len, unsigned long pgoff, unsigned long flags)
1876 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1880 void unmap_mapping_range(struct address_space *mapping,
1881 loff_t const holebegin, loff_t const holelen,
1885 EXPORT_SYMBOL(unmap_mapping_range);
1888 * Check that a process has enough memory to allocate a new virtual
1889 * mapping. 0 means there is enough memory for the allocation to
1890 * succeed and -ENOMEM implies there is not.
1892 * We currently support three overcommit policies, which are set via the
1893 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1895 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1896 * Additional code 2002 Jul 20 by Robert Love.
1898 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1900 * Note this is a helper function intended to be used by LSMs which
1901 * wish to use this logic.
1903 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1905 unsigned long free, allowed, reserve;
1907 vm_acct_memory(pages);
1910 * Sometimes we want to use more memory than we have
1912 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1915 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1916 free = global_page_state(NR_FREE_PAGES);
1917 free += global_page_state(NR_FILE_PAGES);
1920 * shmem pages shouldn't be counted as free in this
1921 * case, they can't be purged, only swapped out, and
1922 * that won't affect the overall amount of available
1923 * memory in the system.
1925 free -= global_page_state(NR_SHMEM);
1927 free += get_nr_swap_pages();
1930 * Any slabs which are created with the
1931 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1932 * which are reclaimable, under pressure. The dentry
1933 * cache and most inode caches should fall into this
1935 free += global_page_state(NR_SLAB_RECLAIMABLE);
1938 * Leave reserved pages. The pages are not for anonymous pages.
1940 if (free <= totalreserve_pages)
1943 free -= totalreserve_pages;
1946 * Reserve some for root
1949 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1957 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1959 * Reserve some 3% for root
1962 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
1963 allowed += total_swap_pages;
1966 * Don't let a single process grow so big a user can't recover
1969 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1970 allowed -= min(mm->total_vm / 32, reserve);
1973 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1977 vm_unacct_memory(pages);
1982 int in_gate_area_no_mm(unsigned long addr)
1987 int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1992 EXPORT_SYMBOL(filemap_fault);
1994 int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1995 unsigned long size, pgoff_t pgoff)
2000 EXPORT_SYMBOL(generic_file_remap_pages);
2002 static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2003 unsigned long addr, void *buf, int len, int write)
2005 struct vm_area_struct *vma;
2007 down_read(&mm->mmap_sem);
2009 /* the access must start within one of the target process's mappings */
2010 vma = find_vma(mm, addr);
2012 /* don't overrun this mapping */
2013 if (addr + len >= vma->vm_end)
2014 len = vma->vm_end - addr;
2016 /* only read or write mappings where it is permitted */
2017 if (write && vma->vm_flags & VM_MAYWRITE)
2018 copy_to_user_page(vma, NULL, addr,
2019 (void *) addr, buf, len);
2020 else if (!write && vma->vm_flags & VM_MAYREAD)
2021 copy_from_user_page(vma, NULL, addr,
2022 buf, (void *) addr, len);
2029 up_read(&mm->mmap_sem);
2035 * @access_remote_vm - access another process' address space
2036 * @mm: the mm_struct of the target address space
2037 * @addr: start address to access
2038 * @buf: source or destination buffer
2039 * @len: number of bytes to transfer
2040 * @write: whether the access is a write
2042 * The caller must hold a reference on @mm.
2044 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2045 void *buf, int len, int write)
2047 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2051 * Access another process' address space.
2052 * - source/target buffer must be kernel space
2054 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2056 struct mm_struct *mm;
2058 if (addr + len < addr)
2061 mm = get_task_mm(tsk);
2065 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2072 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2073 * @inode: The inode to check
2074 * @size: The current filesize of the inode
2075 * @newsize: The proposed filesize of the inode
2077 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2078 * make sure that that any outstanding VMAs aren't broken and then shrink the
2079 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2080 * automatically grant mappings that are too large.
2082 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2085 struct vm_area_struct *vma;
2086 struct vm_region *region;
2088 size_t r_size, r_top;
2090 low = newsize >> PAGE_SHIFT;
2091 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2093 down_write(&nommu_region_sem);
2094 mutex_lock(&inode->i_mapping->i_mmap_mutex);
2096 /* search for VMAs that fall within the dead zone */
2097 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
2098 /* found one - only interested if it's shared out of the page
2100 if (vma->vm_flags & VM_SHARED) {
2101 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2102 up_write(&nommu_region_sem);
2103 return -ETXTBSY; /* not quite true, but near enough */
2107 /* reduce any regions that overlap the dead zone - if in existence,
2108 * these will be pointed to by VMAs that don't overlap the dead zone
2110 * we don't check for any regions that start beyond the EOF as there
2113 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2115 if (!(vma->vm_flags & VM_SHARED))
2118 region = vma->vm_region;
2119 r_size = region->vm_top - region->vm_start;
2120 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2122 if (r_top > newsize) {
2123 region->vm_top -= r_top - newsize;
2124 if (region->vm_end > region->vm_top)
2125 region->vm_end = region->vm_top;
2129 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
2130 up_write(&nommu_region_sem);
2135 * Initialise sysctl_user_reserve_kbytes.
2137 * This is intended to prevent a user from starting a single memory hogging
2138 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2141 * The default value is min(3% of free memory, 128MB)
2142 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2144 static int __meminit init_user_reserve(void)
2146 unsigned long free_kbytes;
2148 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2150 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2153 module_init(init_user_reserve)
2156 * Initialise sysctl_admin_reserve_kbytes.
2158 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2159 * to log in and kill a memory hogging process.
2161 * Systems with more than 256MB will reserve 8MB, enough to recover
2162 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2163 * only reserve 3% of free pages by default.
2165 static int __meminit init_admin_reserve(void)
2167 unsigned long free_kbytes;
2169 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2171 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2174 module_init(init_admin_reserve)