1 // SPDX-License-Identifier: GPL-2.0-only
5 * Replacement code for mm functions to support CPU's that don't
6 * have any form of memory management unit (thus no virtual memory).
8 * See Documentation/admin-guide/mm/nommu-mmap.rst
10 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
11 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
12 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
13 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
14 * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/export.h>
21 #include <linux/sched/mm.h>
22 #include <linux/vmacache.h>
23 #include <linux/mman.h>
24 #include <linux/swap.h>
25 #include <linux/file.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/backing-dev.h>
31 #include <linux/compiler.h>
32 #include <linux/mount.h>
33 #include <linux/personality.h>
34 #include <linux/security.h>
35 #include <linux/syscalls.h>
36 #include <linux/audit.h>
37 #include <linux/printk.h>
39 #include <linux/uaccess.h>
41 #include <asm/tlbflush.h>
42 #include <asm/mmu_context.h>
46 EXPORT_SYMBOL(high_memory);
48 unsigned long max_mapnr;
49 EXPORT_SYMBOL(max_mapnr);
50 unsigned long highest_memmap_pfn;
51 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
52 int heap_stack_gap = 0;
54 atomic_long_t mmap_pages_allocated;
56 EXPORT_SYMBOL(mem_map);
58 /* list of mapped, potentially shareable regions */
59 static struct kmem_cache *vm_region_jar;
60 struct rb_root nommu_region_tree = RB_ROOT;
61 DECLARE_RWSEM(nommu_region_sem);
63 const struct vm_operations_struct generic_file_vm_ops = {
67 * Return the total memory allocated for this pointer, not
68 * just what the caller asked for.
70 * Doesn't have to be accurate, i.e. may have races.
72 unsigned int kobjsize(const void *objp)
77 * If the object we have should not have ksize performed on it,
80 if (!objp || !virt_addr_valid(objp))
83 page = virt_to_head_page(objp);
86 * If the allocator sets PageSlab, we know the pointer came from
93 * If it's not a compound page, see if we have a matching VMA
94 * region. This test is intentionally done in reverse order,
95 * so if there's no VMA, we still fall through and hand back
96 * PAGE_SIZE for 0-order pages.
98 if (!PageCompound(page)) {
99 struct vm_area_struct *vma;
101 vma = find_vma(current->mm, (unsigned long)objp);
103 return vma->vm_end - vma->vm_start;
107 * The ksize() function is only guaranteed to work for pointers
108 * returned by kmalloc(). So handle arbitrary pointers here.
110 return page_size(page);
114 * follow_pfn - look up PFN at a user virtual address
115 * @vma: memory mapping
116 * @address: user virtual address
117 * @pfn: location to store found PFN
119 * Only IO mappings and raw PFN mappings are allowed.
121 * Returns zero and the pfn at @pfn on success, -ve otherwise.
123 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
126 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
129 *pfn = address >> PAGE_SHIFT;
132 EXPORT_SYMBOL(follow_pfn);
134 LIST_HEAD(vmap_area_list);
136 void vfree(const void *addr)
140 EXPORT_SYMBOL(vfree);
142 void *__vmalloc(unsigned long size, gfp_t gfp_mask)
145 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
146 * returns only a logical address.
148 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
150 EXPORT_SYMBOL(__vmalloc);
152 void *__vmalloc_node_range(unsigned long size, unsigned long align,
153 unsigned long start, unsigned long end, gfp_t gfp_mask,
154 pgprot_t prot, unsigned long vm_flags, int node,
157 return __vmalloc(size, gfp_mask);
160 void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
161 int node, const void *caller)
163 return __vmalloc(size, gfp_mask);
166 static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
170 ret = __vmalloc(size, flags);
172 struct vm_area_struct *vma;
174 mmap_write_lock(current->mm);
175 vma = find_vma(current->mm, (unsigned long)ret);
177 vma->vm_flags |= VM_USERMAP;
178 mmap_write_unlock(current->mm);
184 void *vmalloc_user(unsigned long size)
186 return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
188 EXPORT_SYMBOL(vmalloc_user);
190 struct page *vmalloc_to_page(const void *addr)
192 return virt_to_page(addr);
194 EXPORT_SYMBOL(vmalloc_to_page);
196 unsigned long vmalloc_to_pfn(const void *addr)
198 return page_to_pfn(virt_to_page(addr));
200 EXPORT_SYMBOL(vmalloc_to_pfn);
202 long vread(char *buf, char *addr, unsigned long count)
204 /* Don't allow overflow */
205 if ((unsigned long) buf + count < count)
206 count = -(unsigned long) buf;
208 memcpy(buf, addr, count);
213 * vmalloc - allocate virtually contiguous memory
215 * @size: allocation size
217 * Allocate enough pages to cover @size from the page level
218 * allocator and map them into contiguous kernel virtual space.
220 * For tight control over page level allocator and protection flags
221 * use __vmalloc() instead.
223 void *vmalloc(unsigned long size)
225 return __vmalloc(size, GFP_KERNEL);
227 EXPORT_SYMBOL(vmalloc);
230 * vzalloc - allocate virtually contiguous memory with zero fill
232 * @size: allocation size
234 * Allocate enough pages to cover @size from the page level
235 * allocator and map them into contiguous kernel virtual space.
236 * The memory allocated is set to zero.
238 * For tight control over page level allocator and protection flags
239 * use __vmalloc() instead.
241 void *vzalloc(unsigned long size)
243 return __vmalloc(size, GFP_KERNEL | __GFP_ZERO);
245 EXPORT_SYMBOL(vzalloc);
248 * vmalloc_node - allocate memory on a specific node
249 * @size: allocation size
252 * Allocate enough pages to cover @size from the page level
253 * allocator and map them into contiguous kernel virtual space.
255 * For tight control over page level allocator and protection flags
256 * use __vmalloc() instead.
258 void *vmalloc_node(unsigned long size, int node)
260 return vmalloc(size);
262 EXPORT_SYMBOL(vmalloc_node);
265 * vzalloc_node - allocate memory on a specific node with zero fill
266 * @size: allocation size
269 * Allocate enough pages to cover @size from the page level
270 * allocator and map them into contiguous kernel virtual space.
271 * The memory allocated is set to zero.
273 * For tight control over page level allocator and protection flags
274 * use __vmalloc() instead.
276 void *vzalloc_node(unsigned long size, int node)
278 return vzalloc(size);
280 EXPORT_SYMBOL(vzalloc_node);
283 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
284 * @size: allocation size
286 * Allocate enough 32bit PA addressable pages to cover @size from the
287 * page level allocator and map them into contiguous kernel virtual space.
289 void *vmalloc_32(unsigned long size)
291 return __vmalloc(size, GFP_KERNEL);
293 EXPORT_SYMBOL(vmalloc_32);
296 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
297 * @size: allocation size
299 * The resulting memory area is 32bit addressable and zeroed so it can be
300 * mapped to userspace without leaking data.
302 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
303 * remap_vmalloc_range() are permissible.
305 void *vmalloc_32_user(unsigned long size)
308 * We'll have to sort out the ZONE_DMA bits for 64-bit,
309 * but for now this can simply use vmalloc_user() directly.
311 return vmalloc_user(size);
313 EXPORT_SYMBOL(vmalloc_32_user);
315 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
322 void vunmap(const void *addr)
326 EXPORT_SYMBOL(vunmap);
328 void *vm_map_ram(struct page **pages, unsigned int count, int node)
333 EXPORT_SYMBOL(vm_map_ram);
335 void vm_unmap_ram(const void *mem, unsigned int count)
339 EXPORT_SYMBOL(vm_unmap_ram);
341 void vm_unmap_aliases(void)
344 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
346 void free_vm_area(struct vm_struct *area)
350 EXPORT_SYMBOL_GPL(free_vm_area);
352 int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
357 EXPORT_SYMBOL(vm_insert_page);
359 int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
364 EXPORT_SYMBOL(vm_map_pages);
366 int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
371 EXPORT_SYMBOL(vm_map_pages_zero);
374 * sys_brk() for the most part doesn't need the global kernel
375 * lock, except when an application is doing something nasty
376 * like trying to un-brk an area that has already been mapped
377 * to a regular file. in this case, the unmapping will need
378 * to invoke file system routines that need the global lock.
380 SYSCALL_DEFINE1(brk, unsigned long, brk)
382 struct mm_struct *mm = current->mm;
384 if (brk < mm->start_brk || brk > mm->context.end_brk)
391 * Always allow shrinking brk
393 if (brk <= mm->brk) {
399 * Ok, looks good - let it rip.
401 flush_icache_user_range(mm->brk, brk);
402 return mm->brk = brk;
406 * initialise the percpu counter for VM and region record slabs
408 void __init mmap_init(void)
412 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
414 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
418 * validate the region tree
419 * - the caller must hold the region lock
421 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
422 static noinline void validate_nommu_regions(void)
424 struct vm_region *region, *last;
425 struct rb_node *p, *lastp;
427 lastp = rb_first(&nommu_region_tree);
431 last = rb_entry(lastp, struct vm_region, vm_rb);
432 BUG_ON(last->vm_end <= last->vm_start);
433 BUG_ON(last->vm_top < last->vm_end);
435 while ((p = rb_next(lastp))) {
436 region = rb_entry(p, struct vm_region, vm_rb);
437 last = rb_entry(lastp, struct vm_region, vm_rb);
439 BUG_ON(region->vm_end <= region->vm_start);
440 BUG_ON(region->vm_top < region->vm_end);
441 BUG_ON(region->vm_start < last->vm_top);
447 static void validate_nommu_regions(void)
453 * add a region into the global tree
455 static void add_nommu_region(struct vm_region *region)
457 struct vm_region *pregion;
458 struct rb_node **p, *parent;
460 validate_nommu_regions();
463 p = &nommu_region_tree.rb_node;
466 pregion = rb_entry(parent, struct vm_region, vm_rb);
467 if (region->vm_start < pregion->vm_start)
469 else if (region->vm_start > pregion->vm_start)
471 else if (pregion == region)
477 rb_link_node(®ion->vm_rb, parent, p);
478 rb_insert_color(®ion->vm_rb, &nommu_region_tree);
480 validate_nommu_regions();
484 * delete a region from the global tree
486 static void delete_nommu_region(struct vm_region *region)
488 BUG_ON(!nommu_region_tree.rb_node);
490 validate_nommu_regions();
491 rb_erase(®ion->vm_rb, &nommu_region_tree);
492 validate_nommu_regions();
496 * free a contiguous series of pages
498 static void free_page_series(unsigned long from, unsigned long to)
500 for (; from < to; from += PAGE_SIZE) {
501 struct page *page = virt_to_page(from);
503 atomic_long_dec(&mmap_pages_allocated);
509 * release a reference to a region
510 * - the caller must hold the region semaphore for writing, which this releases
511 * - the region may not have been added to the tree yet, in which case vm_top
512 * will equal vm_start
514 static void __put_nommu_region(struct vm_region *region)
515 __releases(nommu_region_sem)
517 BUG_ON(!nommu_region_tree.rb_node);
519 if (--region->vm_usage == 0) {
520 if (region->vm_top > region->vm_start)
521 delete_nommu_region(region);
522 up_write(&nommu_region_sem);
525 fput(region->vm_file);
527 /* IO memory and memory shared directly out of the pagecache
528 * from ramfs/tmpfs mustn't be released here */
529 if (region->vm_flags & VM_MAPPED_COPY)
530 free_page_series(region->vm_start, region->vm_top);
531 kmem_cache_free(vm_region_jar, region);
533 up_write(&nommu_region_sem);
538 * release a reference to a region
540 static void put_nommu_region(struct vm_region *region)
542 down_write(&nommu_region_sem);
543 __put_nommu_region(region);
547 * add a VMA into a process's mm_struct in the appropriate place in the list
548 * and tree and add to the address space's page tree also if not an anonymous
550 * - should be called with mm->mmap_lock held writelocked
552 static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
554 struct vm_area_struct *pvma, *prev;
555 struct address_space *mapping;
556 struct rb_node **p, *parent, *rb_prev;
558 BUG_ON(!vma->vm_region);
563 /* add the VMA to the mapping */
565 mapping = vma->vm_file->f_mapping;
567 i_mmap_lock_write(mapping);
568 flush_dcache_mmap_lock(mapping);
569 vma_interval_tree_insert(vma, &mapping->i_mmap);
570 flush_dcache_mmap_unlock(mapping);
571 i_mmap_unlock_write(mapping);
574 /* add the VMA to the tree */
575 parent = rb_prev = NULL;
576 p = &mm->mm_rb.rb_node;
579 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
581 /* sort by: start addr, end addr, VMA struct addr in that order
582 * (the latter is necessary as we may get identical VMAs) */
583 if (vma->vm_start < pvma->vm_start)
585 else if (vma->vm_start > pvma->vm_start) {
588 } else if (vma->vm_end < pvma->vm_end)
590 else if (vma->vm_end > pvma->vm_end) {
593 } else if (vma < pvma)
595 else if (vma > pvma) {
602 rb_link_node(&vma->vm_rb, parent, p);
603 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
605 /* add VMA to the VMA list also */
608 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
610 __vma_link_list(mm, vma, prev);
614 * delete a VMA from its owning mm_struct and address space
616 static void delete_vma_from_mm(struct vm_area_struct *vma)
619 struct address_space *mapping;
620 struct mm_struct *mm = vma->vm_mm;
621 struct task_struct *curr = current;
624 for (i = 0; i < VMACACHE_SIZE; i++) {
625 /* if the vma is cached, invalidate the entire cache */
626 if (curr->vmacache.vmas[i] == vma) {
627 vmacache_invalidate(mm);
632 /* remove the VMA from the mapping */
634 mapping = vma->vm_file->f_mapping;
636 i_mmap_lock_write(mapping);
637 flush_dcache_mmap_lock(mapping);
638 vma_interval_tree_remove(vma, &mapping->i_mmap);
639 flush_dcache_mmap_unlock(mapping);
640 i_mmap_unlock_write(mapping);
643 /* remove from the MM's tree and list */
644 rb_erase(&vma->vm_rb, &mm->mm_rb);
646 __vma_unlink_list(mm, vma);
650 * destroy a VMA record
652 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
654 if (vma->vm_ops && vma->vm_ops->close)
655 vma->vm_ops->close(vma);
658 put_nommu_region(vma->vm_region);
663 * look up the first VMA in which addr resides, NULL if none
664 * - should be called with mm->mmap_lock at least held readlocked
666 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
668 struct vm_area_struct *vma;
670 /* check the cache first */
671 vma = vmacache_find(mm, addr);
675 /* trawl the list (there may be multiple mappings in which addr
677 for (vma = mm->mmap; vma; vma = vma->vm_next) {
678 if (vma->vm_start > addr)
680 if (vma->vm_end > addr) {
681 vmacache_update(addr, vma);
688 EXPORT_SYMBOL(find_vma);
692 * - we don't extend stack VMAs under NOMMU conditions
694 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
696 return find_vma(mm, addr);
700 * expand a stack to a given address
701 * - not supported under NOMMU conditions
703 int expand_stack(struct vm_area_struct *vma, unsigned long address)
709 * look up the first VMA exactly that exactly matches addr
710 * - should be called with mm->mmap_lock at least held readlocked
712 static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
716 struct vm_area_struct *vma;
717 unsigned long end = addr + len;
719 /* check the cache first */
720 vma = vmacache_find_exact(mm, addr, end);
724 /* trawl the list (there may be multiple mappings in which addr
726 for (vma = mm->mmap; vma; vma = vma->vm_next) {
727 if (vma->vm_start < addr)
729 if (vma->vm_start > addr)
731 if (vma->vm_end == end) {
732 vmacache_update(addr, vma);
741 * determine whether a mapping should be permitted and, if so, what sort of
742 * mapping we're capable of supporting
744 static int validate_mmap_request(struct file *file,
750 unsigned long *_capabilities)
752 unsigned long capabilities, rlen;
755 /* do the simple checks first */
756 if (flags & MAP_FIXED)
759 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
760 (flags & MAP_TYPE) != MAP_SHARED)
766 /* Careful about overflows.. */
767 rlen = PAGE_ALIGN(len);
768 if (!rlen || rlen > TASK_SIZE)
771 /* offset overflow? */
772 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
776 /* files must support mmap */
777 if (!file->f_op->mmap)
780 /* work out if what we've got could possibly be shared
781 * - we support chardevs that provide their own "memory"
782 * - we support files/blockdevs that are memory backed
784 if (file->f_op->mmap_capabilities) {
785 capabilities = file->f_op->mmap_capabilities(file);
787 /* no explicit capabilities set, so assume some
789 switch (file_inode(file)->i_mode & S_IFMT) {
792 capabilities = NOMMU_MAP_COPY;
807 /* eliminate any capabilities that we can't support on this
809 if (!file->f_op->get_unmapped_area)
810 capabilities &= ~NOMMU_MAP_DIRECT;
811 if (!(file->f_mode & FMODE_CAN_READ))
812 capabilities &= ~NOMMU_MAP_COPY;
814 /* The file shall have been opened with read permission. */
815 if (!(file->f_mode & FMODE_READ))
818 if (flags & MAP_SHARED) {
819 /* do checks for writing, appending and locking */
820 if ((prot & PROT_WRITE) &&
821 !(file->f_mode & FMODE_WRITE))
824 if (IS_APPEND(file_inode(file)) &&
825 (file->f_mode & FMODE_WRITE))
828 if (!(capabilities & NOMMU_MAP_DIRECT))
831 /* we mustn't privatise shared mappings */
832 capabilities &= ~NOMMU_MAP_COPY;
834 /* we're going to read the file into private memory we
836 if (!(capabilities & NOMMU_MAP_COPY))
839 /* we don't permit a private writable mapping to be
840 * shared with the backing device */
841 if (prot & PROT_WRITE)
842 capabilities &= ~NOMMU_MAP_DIRECT;
845 if (capabilities & NOMMU_MAP_DIRECT) {
846 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
847 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
848 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
850 capabilities &= ~NOMMU_MAP_DIRECT;
851 if (flags & MAP_SHARED) {
852 pr_warn("MAP_SHARED not completely supported on !MMU\n");
858 /* handle executable mappings and implied executable
860 if (path_noexec(&file->f_path)) {
861 if (prot & PROT_EXEC)
863 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
864 /* handle implication of PROT_EXEC by PROT_READ */
865 if (current->personality & READ_IMPLIES_EXEC) {
866 if (capabilities & NOMMU_MAP_EXEC)
869 } else if ((prot & PROT_READ) &&
870 (prot & PROT_EXEC) &&
871 !(capabilities & NOMMU_MAP_EXEC)
873 /* backing file is not executable, try to copy */
874 capabilities &= ~NOMMU_MAP_DIRECT;
877 /* anonymous mappings are always memory backed and can be
880 capabilities = NOMMU_MAP_COPY;
882 /* handle PROT_EXEC implication by PROT_READ */
883 if ((prot & PROT_READ) &&
884 (current->personality & READ_IMPLIES_EXEC))
888 /* allow the security API to have its say */
889 ret = security_mmap_addr(addr);
894 *_capabilities = capabilities;
899 * we've determined that we can make the mapping, now translate what we
900 * now know into VMA flags
902 static unsigned long determine_vm_flags(struct file *file,
905 unsigned long capabilities)
907 unsigned long vm_flags;
909 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
910 /* vm_flags |= mm->def_flags; */
912 if (!(capabilities & NOMMU_MAP_DIRECT)) {
913 /* attempt to share read-only copies of mapped file chunks */
914 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
915 if (file && !(prot & PROT_WRITE))
916 vm_flags |= VM_MAYSHARE;
918 /* overlay a shareable mapping on the backing device or inode
919 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
921 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
922 if (flags & MAP_SHARED)
923 vm_flags |= VM_SHARED;
926 /* refuse to let anyone share private mappings with this process if
927 * it's being traced - otherwise breakpoints set in it may interfere
928 * with another untraced process
930 if ((flags & MAP_PRIVATE) && current->ptrace)
931 vm_flags &= ~VM_MAYSHARE;
937 * set up a shared mapping on a file (the driver or filesystem provides and
940 static int do_mmap_shared_file(struct vm_area_struct *vma)
944 ret = call_mmap(vma->vm_file, vma);
946 vma->vm_region->vm_top = vma->vm_region->vm_end;
952 /* getting -ENOSYS indicates that direct mmap isn't possible (as
953 * opposed to tried but failed) so we can only give a suitable error as
954 * it's not possible to make a private copy if MAP_SHARED was given */
959 * set up a private mapping or an anonymous shared mapping
961 static int do_mmap_private(struct vm_area_struct *vma,
962 struct vm_region *region,
964 unsigned long capabilities)
966 unsigned long total, point;
970 /* invoke the file's mapping function so that it can keep track of
971 * shared mappings on devices or memory
972 * - VM_MAYSHARE will be set if it may attempt to share
974 if (capabilities & NOMMU_MAP_DIRECT) {
975 ret = call_mmap(vma->vm_file, vma);
977 /* shouldn't return success if we're not sharing */
978 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
979 vma->vm_region->vm_top = vma->vm_region->vm_end;
985 /* getting an ENOSYS error indicates that direct mmap isn't
986 * possible (as opposed to tried but failed) so we'll try to
987 * make a private copy of the data and map that instead */
991 /* allocate some memory to hold the mapping
992 * - note that this may not return a page-aligned address if the object
993 * we're allocating is smaller than a page
995 order = get_order(len);
997 point = len >> PAGE_SHIFT;
999 /* we don't want to allocate a power-of-2 sized page set */
1000 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1003 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1007 atomic_long_add(total, &mmap_pages_allocated);
1009 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1010 region->vm_start = (unsigned long) base;
1011 region->vm_end = region->vm_start + len;
1012 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
1014 vma->vm_start = region->vm_start;
1015 vma->vm_end = region->vm_start + len;
1018 /* read the contents of a file into the copy */
1021 fpos = vma->vm_pgoff;
1022 fpos <<= PAGE_SHIFT;
1024 ret = kernel_read(vma->vm_file, base, len, &fpos);
1028 /* clear the last little bit */
1030 memset(base + ret, 0, len - ret);
1033 vma_set_anonymous(vma);
1039 free_page_series(region->vm_start, region->vm_top);
1040 region->vm_start = vma->vm_start = 0;
1041 region->vm_end = vma->vm_end = 0;
1046 pr_err("Allocation of length %lu from process %d (%s) failed\n",
1047 len, current->pid, current->comm);
1048 show_free_areas(0, NULL);
1053 * handle mapping creation for uClinux
1055 unsigned long do_mmap(struct file *file,
1059 unsigned long flags,
1060 unsigned long pgoff,
1061 unsigned long *populate,
1062 struct list_head *uf)
1064 struct vm_area_struct *vma;
1065 struct vm_region *region;
1067 vm_flags_t vm_flags;
1068 unsigned long capabilities, result;
1073 /* decide whether we should attempt the mapping, and if so what sort of
1075 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1080 /* we ignore the address hint */
1082 len = PAGE_ALIGN(len);
1084 /* we've determined that we can make the mapping, now translate what we
1085 * now know into VMA flags */
1086 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1088 /* we're going to need to record the mapping */
1089 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1091 goto error_getting_region;
1093 vma = vm_area_alloc(current->mm);
1095 goto error_getting_vma;
1097 region->vm_usage = 1;
1098 region->vm_flags = vm_flags;
1099 region->vm_pgoff = pgoff;
1101 vma->vm_flags = vm_flags;
1102 vma->vm_pgoff = pgoff;
1105 region->vm_file = get_file(file);
1106 vma->vm_file = get_file(file);
1109 down_write(&nommu_region_sem);
1111 /* if we want to share, we need to check for regions created by other
1112 * mmap() calls that overlap with our proposed mapping
1113 * - we can only share with a superset match on most regular files
1114 * - shared mappings on character devices and memory backed files are
1115 * permitted to overlap inexactly as far as we are concerned for in
1116 * these cases, sharing is handled in the driver or filesystem rather
1119 if (vm_flags & VM_MAYSHARE) {
1120 struct vm_region *pregion;
1121 unsigned long pglen, rpglen, pgend, rpgend, start;
1123 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1124 pgend = pgoff + pglen;
1126 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1127 pregion = rb_entry(rb, struct vm_region, vm_rb);
1129 if (!(pregion->vm_flags & VM_MAYSHARE))
1132 /* search for overlapping mappings on the same file */
1133 if (file_inode(pregion->vm_file) !=
1137 if (pregion->vm_pgoff >= pgend)
1140 rpglen = pregion->vm_end - pregion->vm_start;
1141 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1142 rpgend = pregion->vm_pgoff + rpglen;
1143 if (pgoff >= rpgend)
1146 /* handle inexactly overlapping matches between
1148 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1149 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1150 /* new mapping is not a subset of the region */
1151 if (!(capabilities & NOMMU_MAP_DIRECT))
1152 goto sharing_violation;
1156 /* we've found a region we can share */
1157 pregion->vm_usage++;
1158 vma->vm_region = pregion;
1159 start = pregion->vm_start;
1160 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1161 vma->vm_start = start;
1162 vma->vm_end = start + len;
1164 if (pregion->vm_flags & VM_MAPPED_COPY)
1165 vma->vm_flags |= VM_MAPPED_COPY;
1167 ret = do_mmap_shared_file(vma);
1169 vma->vm_region = NULL;
1172 pregion->vm_usage--;
1174 goto error_just_free;
1177 fput(region->vm_file);
1178 kmem_cache_free(vm_region_jar, region);
1184 /* obtain the address at which to make a shared mapping
1185 * - this is the hook for quasi-memory character devices to
1186 * tell us the location of a shared mapping
1188 if (capabilities & NOMMU_MAP_DIRECT) {
1189 addr = file->f_op->get_unmapped_area(file, addr, len,
1191 if (IS_ERR_VALUE(addr)) {
1194 goto error_just_free;
1196 /* the driver refused to tell us where to site
1197 * the mapping so we'll have to attempt to copy
1200 if (!(capabilities & NOMMU_MAP_COPY))
1201 goto error_just_free;
1203 capabilities &= ~NOMMU_MAP_DIRECT;
1205 vma->vm_start = region->vm_start = addr;
1206 vma->vm_end = region->vm_end = addr + len;
1211 vma->vm_region = region;
1213 /* set up the mapping
1214 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1216 if (file && vma->vm_flags & VM_SHARED)
1217 ret = do_mmap_shared_file(vma);
1219 ret = do_mmap_private(vma, region, len, capabilities);
1221 goto error_just_free;
1222 add_nommu_region(region);
1224 /* clear anonymous mappings that don't ask for uninitialized data */
1225 if (!vma->vm_file &&
1226 (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
1227 !(flags & MAP_UNINITIALIZED)))
1228 memset((void *)region->vm_start, 0,
1229 region->vm_end - region->vm_start);
1231 /* okay... we have a mapping; now we have to register it */
1232 result = vma->vm_start;
1234 current->mm->total_vm += len >> PAGE_SHIFT;
1237 add_vma_to_mm(current->mm, vma);
1239 /* we flush the region from the icache only when the first executable
1240 * mapping of it is made */
1241 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1242 flush_icache_user_range(region->vm_start, region->vm_end);
1243 region->vm_icache_flushed = true;
1246 up_write(&nommu_region_sem);
1251 up_write(&nommu_region_sem);
1253 if (region->vm_file)
1254 fput(region->vm_file);
1255 kmem_cache_free(vm_region_jar, region);
1262 up_write(&nommu_region_sem);
1263 pr_warn("Attempt to share mismatched mappings\n");
1268 kmem_cache_free(vm_region_jar, region);
1269 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1271 show_free_areas(0, NULL);
1274 error_getting_region:
1275 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1277 show_free_areas(0, NULL);
1281 unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1282 unsigned long prot, unsigned long flags,
1283 unsigned long fd, unsigned long pgoff)
1285 struct file *file = NULL;
1286 unsigned long retval = -EBADF;
1288 audit_mmap_fd(fd, flags);
1289 if (!(flags & MAP_ANONYMOUS)) {
1295 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1303 SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1304 unsigned long, prot, unsigned long, flags,
1305 unsigned long, fd, unsigned long, pgoff)
1307 return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1310 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1311 struct mmap_arg_struct {
1315 unsigned long flags;
1317 unsigned long offset;
1320 SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1322 struct mmap_arg_struct a;
1324 if (copy_from_user(&a, arg, sizeof(a)))
1326 if (offset_in_page(a.offset))
1329 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1330 a.offset >> PAGE_SHIFT);
1332 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1335 * split a vma into two pieces at address 'addr', a new vma is allocated either
1336 * for the first part or the tail.
1338 int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1339 unsigned long addr, int new_below)
1341 struct vm_area_struct *new;
1342 struct vm_region *region;
1343 unsigned long npages;
1345 /* we're only permitted to split anonymous regions (these should have
1346 * only a single usage on the region) */
1350 if (mm->map_count >= sysctl_max_map_count)
1353 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1357 new = vm_area_dup(vma);
1359 kmem_cache_free(vm_region_jar, region);
1363 /* most fields are the same, copy all, and then fixup */
1364 *region = *vma->vm_region;
1365 new->vm_region = region;
1367 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1370 region->vm_top = region->vm_end = new->vm_end = addr;
1372 region->vm_start = new->vm_start = addr;
1373 region->vm_pgoff = new->vm_pgoff += npages;
1376 if (new->vm_ops && new->vm_ops->open)
1377 new->vm_ops->open(new);
1379 delete_vma_from_mm(vma);
1380 down_write(&nommu_region_sem);
1381 delete_nommu_region(vma->vm_region);
1383 vma->vm_region->vm_start = vma->vm_start = addr;
1384 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1386 vma->vm_region->vm_end = vma->vm_end = addr;
1387 vma->vm_region->vm_top = addr;
1389 add_nommu_region(vma->vm_region);
1390 add_nommu_region(new->vm_region);
1391 up_write(&nommu_region_sem);
1392 add_vma_to_mm(mm, vma);
1393 add_vma_to_mm(mm, new);
1398 * shrink a VMA by removing the specified chunk from either the beginning or
1401 static int shrink_vma(struct mm_struct *mm,
1402 struct vm_area_struct *vma,
1403 unsigned long from, unsigned long to)
1405 struct vm_region *region;
1407 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1409 delete_vma_from_mm(vma);
1410 if (from > vma->vm_start)
1414 add_vma_to_mm(mm, vma);
1416 /* cut the backing region down to size */
1417 region = vma->vm_region;
1418 BUG_ON(region->vm_usage != 1);
1420 down_write(&nommu_region_sem);
1421 delete_nommu_region(region);
1422 if (from > region->vm_start) {
1423 to = region->vm_top;
1424 region->vm_top = region->vm_end = from;
1426 region->vm_start = to;
1428 add_nommu_region(region);
1429 up_write(&nommu_region_sem);
1431 free_page_series(from, to);
1437 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1438 * VMA, though it need not cover the whole VMA
1440 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
1442 struct vm_area_struct *vma;
1446 len = PAGE_ALIGN(len);
1452 /* find the first potentially overlapping VMA */
1453 vma = find_vma(mm, start);
1457 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1458 current->pid, current->comm,
1459 start, start + len - 1);
1465 /* we're allowed to split an anonymous VMA but not a file-backed one */
1468 if (start > vma->vm_start)
1470 if (end == vma->vm_end)
1471 goto erase_whole_vma;
1476 /* the chunk must be a subset of the VMA found */
1477 if (start == vma->vm_start && end == vma->vm_end)
1478 goto erase_whole_vma;
1479 if (start < vma->vm_start || end > vma->vm_end)
1481 if (offset_in_page(start))
1483 if (end != vma->vm_end && offset_in_page(end))
1485 if (start != vma->vm_start && end != vma->vm_end) {
1486 ret = split_vma(mm, vma, start, 1);
1490 return shrink_vma(mm, vma, start, end);
1494 delete_vma_from_mm(vma);
1495 delete_vma(mm, vma);
1499 int vm_munmap(unsigned long addr, size_t len)
1501 struct mm_struct *mm = current->mm;
1504 mmap_write_lock(mm);
1505 ret = do_munmap(mm, addr, len, NULL);
1506 mmap_write_unlock(mm);
1509 EXPORT_SYMBOL(vm_munmap);
1511 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1513 return vm_munmap(addr, len);
1517 * release all the mappings made in a process's VM space
1519 void exit_mmap(struct mm_struct *mm)
1521 struct vm_area_struct *vma;
1528 while ((vma = mm->mmap)) {
1529 mm->mmap = vma->vm_next;
1530 delete_vma_from_mm(vma);
1531 delete_vma(mm, vma);
1536 int vm_brk(unsigned long addr, unsigned long len)
1542 * expand (or shrink) an existing mapping, potentially moving it at the same
1543 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1545 * under NOMMU conditions, we only permit changing a mapping's size, and only
1546 * as long as it stays within the region allocated by do_mmap_private() and the
1547 * block is not shareable
1549 * MREMAP_FIXED is not supported under NOMMU conditions
1551 static unsigned long do_mremap(unsigned long addr,
1552 unsigned long old_len, unsigned long new_len,
1553 unsigned long flags, unsigned long new_addr)
1555 struct vm_area_struct *vma;
1557 /* insanity checks first */
1558 old_len = PAGE_ALIGN(old_len);
1559 new_len = PAGE_ALIGN(new_len);
1560 if (old_len == 0 || new_len == 0)
1561 return (unsigned long) -EINVAL;
1563 if (offset_in_page(addr))
1566 if (flags & MREMAP_FIXED && new_addr != addr)
1567 return (unsigned long) -EINVAL;
1569 vma = find_vma_exact(current->mm, addr, old_len);
1571 return (unsigned long) -EINVAL;
1573 if (vma->vm_end != vma->vm_start + old_len)
1574 return (unsigned long) -EFAULT;
1576 if (vma->vm_flags & VM_MAYSHARE)
1577 return (unsigned long) -EPERM;
1579 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1580 return (unsigned long) -ENOMEM;
1582 /* all checks complete - do it */
1583 vma->vm_end = vma->vm_start + new_len;
1584 return vma->vm_start;
1587 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1588 unsigned long, new_len, unsigned long, flags,
1589 unsigned long, new_addr)
1593 mmap_write_lock(current->mm);
1594 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1595 mmap_write_unlock(current->mm);
1599 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1600 unsigned int foll_flags)
1605 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1606 unsigned long pfn, unsigned long size, pgprot_t prot)
1608 if (addr != (pfn << PAGE_SHIFT))
1611 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1614 EXPORT_SYMBOL(remap_pfn_range);
1616 int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1618 unsigned long pfn = start >> PAGE_SHIFT;
1619 unsigned long vm_len = vma->vm_end - vma->vm_start;
1621 pfn += vma->vm_pgoff;
1622 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1624 EXPORT_SYMBOL(vm_iomap_memory);
1626 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1627 unsigned long pgoff)
1629 unsigned int size = vma->vm_end - vma->vm_start;
1631 if (!(vma->vm_flags & VM_USERMAP))
1634 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1635 vma->vm_end = vma->vm_start + size;
1639 EXPORT_SYMBOL(remap_vmalloc_range);
1641 vm_fault_t filemap_fault(struct vm_fault *vmf)
1646 EXPORT_SYMBOL(filemap_fault);
1648 vm_fault_t filemap_map_pages(struct vm_fault *vmf,
1649 pgoff_t start_pgoff, pgoff_t end_pgoff)
1654 EXPORT_SYMBOL(filemap_map_pages);
1656 int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
1657 int len, unsigned int gup_flags)
1659 struct vm_area_struct *vma;
1660 int write = gup_flags & FOLL_WRITE;
1662 if (mmap_read_lock_killable(mm))
1665 /* the access must start within one of the target process's mappings */
1666 vma = find_vma(mm, addr);
1668 /* don't overrun this mapping */
1669 if (addr + len >= vma->vm_end)
1670 len = vma->vm_end - addr;
1672 /* only read or write mappings where it is permitted */
1673 if (write && vma->vm_flags & VM_MAYWRITE)
1674 copy_to_user_page(vma, NULL, addr,
1675 (void *) addr, buf, len);
1676 else if (!write && vma->vm_flags & VM_MAYREAD)
1677 copy_from_user_page(vma, NULL, addr,
1678 buf, (void *) addr, len);
1685 mmap_read_unlock(mm);
1691 * access_remote_vm - access another process' address space
1692 * @mm: the mm_struct of the target address space
1693 * @addr: start address to access
1694 * @buf: source or destination buffer
1695 * @len: number of bytes to transfer
1696 * @gup_flags: flags modifying lookup behaviour
1698 * The caller must hold a reference on @mm.
1700 int access_remote_vm(struct mm_struct *mm, unsigned long addr,
1701 void *buf, int len, unsigned int gup_flags)
1703 return __access_remote_vm(mm, addr, buf, len, gup_flags);
1707 * Access another process' address space.
1708 * - source/target buffer must be kernel space
1710 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1711 unsigned int gup_flags)
1713 struct mm_struct *mm;
1715 if (addr + len < addr)
1718 mm = get_task_mm(tsk);
1722 len = __access_remote_vm(mm, addr, buf, len, gup_flags);
1727 EXPORT_SYMBOL_GPL(access_process_vm);
1730 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1731 * @inode: The inode to check
1732 * @size: The current filesize of the inode
1733 * @newsize: The proposed filesize of the inode
1735 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1736 * make sure that any outstanding VMAs aren't broken and then shrink the
1737 * vm_regions that extend beyond so that do_mmap() doesn't
1738 * automatically grant mappings that are too large.
1740 int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1743 struct vm_area_struct *vma;
1744 struct vm_region *region;
1746 size_t r_size, r_top;
1748 low = newsize >> PAGE_SHIFT;
1749 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1751 down_write(&nommu_region_sem);
1752 i_mmap_lock_read(inode->i_mapping);
1754 /* search for VMAs that fall within the dead zone */
1755 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
1756 /* found one - only interested if it's shared out of the page
1758 if (vma->vm_flags & VM_SHARED) {
1759 i_mmap_unlock_read(inode->i_mapping);
1760 up_write(&nommu_region_sem);
1761 return -ETXTBSY; /* not quite true, but near enough */
1765 /* reduce any regions that overlap the dead zone - if in existence,
1766 * these will be pointed to by VMAs that don't overlap the dead zone
1768 * we don't check for any regions that start beyond the EOF as there
1771 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
1772 if (!(vma->vm_flags & VM_SHARED))
1775 region = vma->vm_region;
1776 r_size = region->vm_top - region->vm_start;
1777 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1779 if (r_top > newsize) {
1780 region->vm_top -= r_top - newsize;
1781 if (region->vm_end > region->vm_top)
1782 region->vm_end = region->vm_top;
1786 i_mmap_unlock_read(inode->i_mapping);
1787 up_write(&nommu_region_sem);
1792 * Initialise sysctl_user_reserve_kbytes.
1794 * This is intended to prevent a user from starting a single memory hogging
1795 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1798 * The default value is min(3% of free memory, 128MB)
1799 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1801 static int __meminit init_user_reserve(void)
1803 unsigned long free_kbytes;
1805 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1807 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1810 subsys_initcall(init_user_reserve);
1813 * Initialise sysctl_admin_reserve_kbytes.
1815 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1816 * to log in and kill a memory hogging process.
1818 * Systems with more than 256MB will reserve 8MB, enough to recover
1819 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1820 * only reserve 3% of free pages by default.
1822 static int __meminit init_admin_reserve(void)
1824 unsigned long free_kbytes;
1826 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1828 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
1831 subsys_initcall(init_admin_reserve);