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-2005 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>
16 #include <linux/mman.h>
17 #include <linux/swap.h>
18 #include <linux/file.h>
19 #include <linux/highmem.h>
20 #include <linux/pagemap.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 #include <linux/ptrace.h>
24 #include <linux/blkdev.h>
25 #include <linux/backing-dev.h>
26 #include <linux/mount.h>
27 #include <linux/personality.h>
28 #include <linux/security.h>
29 #include <linux/syscalls.h>
31 #include <asm/uaccess.h>
33 #include <asm/tlbflush.h>
37 unsigned long max_mapnr;
38 unsigned long num_physpages;
39 unsigned long askedalloc, realalloc;
40 atomic_t vm_committed_space = ATOMIC_INIT(0);
41 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
42 int sysctl_overcommit_ratio = 50; /* default is 50% */
43 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
44 int heap_stack_gap = 0;
46 EXPORT_SYMBOL(mem_map);
47 EXPORT_SYMBOL(__vm_enough_memory);
49 /* list of shareable VMAs */
50 struct rb_root nommu_vma_tree = RB_ROOT;
51 DECLARE_RWSEM(nommu_vma_sem);
53 struct vm_operations_struct generic_file_vm_ops = {
56 EXPORT_SYMBOL(vmalloc);
58 EXPORT_SYMBOL(vmalloc_to_page);
59 EXPORT_SYMBOL(vmalloc_32);
61 EXPORT_SYMBOL(vunmap);
64 * Handle all mappings that got truncated by a "truncate()"
67 * NOTE! We have to be ready to update the memory sharing
68 * between the file and the memory map for a potential last
69 * incomplete page. Ugly, but necessary.
71 int vmtruncate(struct inode *inode, loff_t offset)
73 struct address_space *mapping = inode->i_mapping;
76 if (inode->i_size < offset)
78 i_size_write(inode, offset);
80 truncate_inode_pages(mapping, offset);
84 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
85 if (limit != RLIM_INFINITY && offset > limit)
87 if (offset > inode->i_sb->s_maxbytes)
89 i_size_write(inode, offset);
92 if (inode->i_op && inode->i_op->truncate)
93 inode->i_op->truncate(inode);
96 send_sig(SIGXFSZ, current, 0);
101 EXPORT_SYMBOL(vmtruncate);
104 * Return the total memory allocated for this pointer, not
105 * just what the caller asked for.
107 * Doesn't have to be accurate, i.e. may have races.
109 unsigned int kobjsize(const void *objp)
113 if (!objp || !((page = virt_to_page(objp))))
119 BUG_ON(page->index < 0);
120 BUG_ON(page->index >= MAX_ORDER);
122 return (PAGE_SIZE << page->index);
126 * The nommu dodgy version :-)
128 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
129 unsigned long start, int len, int write, int force,
130 struct page **pages, struct vm_area_struct **vmas)
133 static struct vm_area_struct dummy_vma;
135 for (i = 0; i < len; i++) {
137 pages[i] = virt_to_page(start);
139 page_cache_get(pages[i]);
142 vmas[i] = &dummy_vma;
148 EXPORT_SYMBOL(get_user_pages);
150 DEFINE_RWLOCK(vmlist_lock);
151 struct vm_struct *vmlist;
153 void vfree(void *addr)
158 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
161 * kmalloc doesn't like __GFP_HIGHMEM for some reason
163 return kmalloc(size, gfp_mask & ~__GFP_HIGHMEM);
166 struct page * vmalloc_to_page(void *addr)
168 return virt_to_page(addr);
171 unsigned long vmalloc_to_pfn(void *addr)
173 return page_to_pfn(virt_to_page(addr));
177 long vread(char *buf, char *addr, unsigned long count)
179 memcpy(buf, addr, count);
183 long vwrite(char *buf, char *addr, unsigned long count)
185 /* Don't allow overflow */
186 if ((unsigned long) addr + count < count)
187 count = -(unsigned long) addr;
189 memcpy(addr, buf, count);
194 * vmalloc - allocate virtually continguos memory
196 * @size: allocation size
198 * Allocate enough pages to cover @size from the page level
199 * allocator and map them into continguos kernel virtual space.
201 * For tight cotrol over page level allocator and protection flags
202 * use __vmalloc() instead.
204 void *vmalloc(unsigned long size)
206 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
210 * vmalloc_32 - allocate virtually continguos memory (32bit addressable)
212 * @size: allocation size
214 * Allocate enough 32bit PA addressable pages to cover @size from the
215 * page level allocator and map them into continguos kernel virtual space.
217 void *vmalloc_32(unsigned long size)
219 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
222 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
228 void vunmap(void *addr)
234 * sys_brk() for the most part doesn't need the global kernel
235 * lock, except when an application is doing something nasty
236 * like trying to un-brk an area that has already been mapped
237 * to a regular file. in this case, the unmapping will need
238 * to invoke file system routines that need the global lock.
240 asmlinkage unsigned long sys_brk(unsigned long brk)
242 struct mm_struct *mm = current->mm;
244 if (brk < mm->start_brk || brk > mm->context.end_brk)
251 * Always allow shrinking brk
253 if (brk <= mm->brk) {
259 * Ok, looks good - let it rip.
261 return mm->brk = brk;
265 static void show_process_blocks(void)
267 struct vm_list_struct *vml;
269 printk("Process blocks %d:", current->pid);
271 for (vml = ¤t->mm->context.vmlist; vml; vml = vml->next) {
272 printk(" %p: %p", vml, vml->vma);
274 printk(" (%d @%lx #%d)",
275 kobjsize((void *) vml->vma->vm_start),
277 atomic_read(&vml->vma->vm_usage));
278 printk(vml->next ? " ->" : ".\n");
283 static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
285 struct vm_area_struct *vma;
286 struct rb_node *n = nommu_vma_tree.rb_node;
289 vma = rb_entry(n, struct vm_area_struct, vm_rb);
291 if (start < vma->vm_start)
293 else if (start > vma->vm_start)
302 static void add_nommu_vma(struct vm_area_struct *vma)
304 struct vm_area_struct *pvma;
305 struct address_space *mapping;
306 struct rb_node **p = &nommu_vma_tree.rb_node;
307 struct rb_node *parent = NULL;
309 /* add the VMA to the mapping */
311 mapping = vma->vm_file->f_mapping;
313 flush_dcache_mmap_lock(mapping);
314 vma_prio_tree_insert(vma, &mapping->i_mmap);
315 flush_dcache_mmap_unlock(mapping);
318 /* add the VMA to the master list */
321 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
323 if (vma->vm_start < pvma->vm_start) {
326 else if (vma->vm_start > pvma->vm_start) {
330 /* mappings are at the same address - this can only
331 * happen for shared-mem chardevs and shared file
332 * mappings backed by ramfs/tmpfs */
333 BUG_ON(!(pvma->vm_flags & VM_SHARED));
344 rb_link_node(&vma->vm_rb, parent, p);
345 rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
348 static void delete_nommu_vma(struct vm_area_struct *vma)
350 struct address_space *mapping;
352 /* remove the VMA from the mapping */
354 mapping = vma->vm_file->f_mapping;
356 flush_dcache_mmap_lock(mapping);
357 vma_prio_tree_remove(vma, &mapping->i_mmap);
358 flush_dcache_mmap_unlock(mapping);
361 /* remove from the master list */
362 rb_erase(&vma->vm_rb, &nommu_vma_tree);
366 * determine whether a mapping should be permitted and, if so, what sort of
367 * mapping we're capable of supporting
369 static int validate_mmap_request(struct file *file,
375 unsigned long *_capabilities)
377 unsigned long capabilities;
378 unsigned long reqprot = prot;
381 /* do the simple checks first */
382 if (flags & MAP_FIXED || addr) {
384 "%d: Can't do fixed-address/overlay mmap of RAM\n",
389 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
390 (flags & MAP_TYPE) != MAP_SHARED)
393 if (PAGE_ALIGN(len) == 0)
399 /* offset overflow? */
400 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
404 /* validate file mapping requests */
405 struct address_space *mapping;
407 /* files must support mmap */
408 if (!file->f_op || !file->f_op->mmap)
411 /* work out if what we've got could possibly be shared
412 * - we support chardevs that provide their own "memory"
413 * - we support files/blockdevs that are memory backed
415 mapping = file->f_mapping;
417 mapping = file->f_dentry->d_inode->i_mapping;
420 if (mapping && mapping->backing_dev_info)
421 capabilities = mapping->backing_dev_info->capabilities;
424 /* no explicit capabilities set, so assume some
426 switch (file->f_dentry->d_inode->i_mode & S_IFMT) {
429 capabilities = BDI_CAP_MAP_COPY;
444 /* eliminate any capabilities that we can't support on this
446 if (!file->f_op->get_unmapped_area)
447 capabilities &= ~BDI_CAP_MAP_DIRECT;
448 if (!file->f_op->read)
449 capabilities &= ~BDI_CAP_MAP_COPY;
451 if (flags & MAP_SHARED) {
452 /* do checks for writing, appending and locking */
453 if ((prot & PROT_WRITE) &&
454 !(file->f_mode & FMODE_WRITE))
457 if (IS_APPEND(file->f_dentry->d_inode) &&
458 (file->f_mode & FMODE_WRITE))
461 if (locks_verify_locked(file->f_dentry->d_inode))
464 if (!(capabilities & BDI_CAP_MAP_DIRECT))
467 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
468 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
469 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
471 printk("MAP_SHARED not completely supported on !MMU\n");
475 /* we mustn't privatise shared mappings */
476 capabilities &= ~BDI_CAP_MAP_COPY;
479 /* we're going to read the file into private memory we
481 if (!(capabilities & BDI_CAP_MAP_COPY))
484 /* we don't permit a private writable mapping to be
485 * shared with the backing device */
486 if (prot & PROT_WRITE)
487 capabilities &= ~BDI_CAP_MAP_DIRECT;
490 /* handle executable mappings and implied executable
492 if (file->f_vfsmnt->mnt_flags & MNT_NOEXEC) {
493 if (prot & PROT_EXEC)
496 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
497 /* handle implication of PROT_EXEC by PROT_READ */
498 if (current->personality & READ_IMPLIES_EXEC) {
499 if (capabilities & BDI_CAP_EXEC_MAP)
503 else if ((prot & PROT_READ) &&
504 (prot & PROT_EXEC) &&
505 !(capabilities & BDI_CAP_EXEC_MAP)
507 /* backing file is not executable, try to copy */
508 capabilities &= ~BDI_CAP_MAP_DIRECT;
512 /* anonymous mappings are always memory backed and can be
515 capabilities = BDI_CAP_MAP_COPY;
517 /* handle PROT_EXEC implication by PROT_READ */
518 if ((prot & PROT_READ) &&
519 (current->personality & READ_IMPLIES_EXEC))
523 /* allow the security API to have its say */
524 ret = security_file_mmap(file, reqprot, prot, flags);
529 *_capabilities = capabilities;
534 * we've determined that we can make the mapping, now translate what we
535 * now know into VMA flags
537 static unsigned long determine_vm_flags(struct file *file,
540 unsigned long capabilities)
542 unsigned long vm_flags;
544 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
545 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
546 /* vm_flags |= mm->def_flags; */
548 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
549 /* attempt to share read-only copies of mapped file chunks */
550 if (file && !(prot & PROT_WRITE))
551 vm_flags |= VM_MAYSHARE;
554 /* overlay a shareable mapping on the backing device or inode
555 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
557 if (flags & MAP_SHARED)
558 vm_flags |= VM_MAYSHARE | VM_SHARED;
559 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
560 vm_flags |= VM_MAYSHARE;
563 /* refuse to let anyone share private mappings with this process if
564 * it's being traced - otherwise breakpoints set in it may interfere
565 * with another untraced process
567 if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
568 vm_flags &= ~VM_MAYSHARE;
574 * set up a shared mapping on a file
576 static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
580 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
584 /* getting an ENOSYS error indicates that direct mmap isn't
585 * possible (as opposed to tried but failed) so we'll fall
586 * through to making a private copy of the data and mapping
592 * set up a private mapping or an anonymous shared mapping
594 static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
599 /* invoke the file's mapping function so that it can keep track of
600 * shared mappings on devices or memory
601 * - VM_MAYSHARE will be set if it may attempt to share
604 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
605 if (ret != -ENOSYS) {
606 /* shouldn't return success if we're not sharing */
607 BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
608 return ret; /* success or a real error */
611 /* getting an ENOSYS error indicates that direct mmap isn't
612 * possible (as opposed to tried but failed) so we'll try to
613 * make a private copy of the data and map that instead */
616 /* allocate some memory to hold the mapping
617 * - note that this may not return a page-aligned address if the object
618 * we're allocating is smaller than a page
620 base = kmalloc(len, GFP_KERNEL);
624 vma->vm_start = (unsigned long) base;
625 vma->vm_end = vma->vm_start + len;
626 vma->vm_flags |= VM_MAPPED_COPY;
629 if (len + WARN_ON_SLACK <= kobjsize(result))
630 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
631 len, current->pid, kobjsize(result) - len);
635 /* read the contents of a file into the copy */
639 fpos = vma->vm_pgoff;
644 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
650 /* clear the last little bit */
652 memset(base + ret, 0, len - ret);
655 /* if it's an anonymous mapping, then just clear it */
656 memset(base, 0, len);
667 printk("Allocation of length %lu from process %d failed\n",
674 * handle mapping creation for uClinux
676 unsigned long do_mmap_pgoff(struct file *file,
683 struct vm_list_struct *vml = NULL;
684 struct vm_area_struct *vma = NULL;
686 unsigned long capabilities, vm_flags;
690 /* decide whether we should attempt the mapping, and if so what sort of
692 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
697 /* we've determined that we can make the mapping, now translate what we
698 * now know into VMA flags */
699 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
701 /* we're going to need to record the mapping if it works */
702 vml = kmalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
704 goto error_getting_vml;
705 memset(vml, 0, sizeof(*vml));
707 down_write(&nommu_vma_sem);
709 /* if we want to share, we need to check for VMAs created by other
710 * mmap() calls that overlap with our proposed mapping
711 * - we can only share with an exact match on most regular files
712 * - shared mappings on character devices and memory backed files are
713 * permitted to overlap inexactly as far as we are concerned for in
714 * these cases, sharing is handled in the driver or filesystem rather
717 if (vm_flags & VM_MAYSHARE) {
718 unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
719 unsigned long vmpglen;
721 for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
722 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
724 if (!(vma->vm_flags & VM_MAYSHARE))
727 /* search for overlapping mappings on the same file */
728 if (vma->vm_file->f_dentry->d_inode != file->f_dentry->d_inode)
731 if (vma->vm_pgoff >= pgoff + pglen)
734 vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
735 vmpglen >>= PAGE_SHIFT;
736 if (pgoff >= vma->vm_pgoff + vmpglen)
739 /* handle inexactly overlapping matches between mappings */
740 if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
741 if (!(capabilities & BDI_CAP_MAP_DIRECT))
742 goto sharing_violation;
746 /* we've found a VMA we can share */
747 atomic_inc(&vma->vm_usage);
750 result = (void *) vma->vm_start;
756 /* obtain the address at which to make a shared mapping
757 * - this is the hook for quasi-memory character devices to
758 * tell us the location of a shared mapping
760 if (file && file->f_op->get_unmapped_area) {
761 addr = file->f_op->get_unmapped_area(file, addr, len,
763 if (IS_ERR((void *) addr)) {
765 if (ret != (unsigned long) -ENOSYS)
768 /* the driver refused to tell us where to site
769 * the mapping so we'll have to attempt to copy
771 ret = (unsigned long) -ENODEV;
772 if (!(capabilities & BDI_CAP_MAP_COPY))
775 capabilities &= ~BDI_CAP_MAP_DIRECT;
780 /* we're going to need a VMA struct as well */
781 vma = kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
783 goto error_getting_vma;
785 memset(vma, 0, sizeof(*vma));
786 INIT_LIST_HEAD(&vma->anon_vma_node);
787 atomic_set(&vma->vm_usage, 1);
791 vma->vm_flags = vm_flags;
792 vma->vm_start = addr;
793 vma->vm_end = addr + len;
794 vma->vm_pgoff = pgoff;
798 /* set up the mapping */
799 if (file && vma->vm_flags & VM_SHARED)
800 ret = do_mmap_shared_file(vma, len);
802 ret = do_mmap_private(vma, len);
806 /* okay... we have a mapping; now we have to register it */
807 result = (void *) vma->vm_start;
809 if (vma->vm_flags & VM_MAPPED_COPY) {
810 realalloc += kobjsize(result);
814 realalloc += kobjsize(vma);
815 askedalloc += sizeof(*vma);
817 current->mm->total_vm += len >> PAGE_SHIFT;
822 realalloc += kobjsize(vml);
823 askedalloc += sizeof(*vml);
825 vml->next = current->mm->context.vmlist;
826 current->mm->context.vmlist = vml;
828 up_write(&nommu_vma_sem);
830 if (prot & PROT_EXEC)
831 flush_icache_range((unsigned long) result,
832 (unsigned long) result + len);
835 printk("do_mmap:\n");
836 show_process_blocks();
839 return (unsigned long) result;
842 up_write(&nommu_vma_sem);
851 up_write(&nommu_vma_sem);
852 printk("Attempt to share mismatched mappings\n");
857 up_write(&nommu_vma_sem);
859 printk("Allocation of vma for %lu byte allocation from process %d failed\n",
865 printk("Allocation of vml for %lu byte allocation from process %d failed\n",
872 * handle mapping disposal for uClinux
874 static void put_vma(struct vm_area_struct *vma)
877 down_write(&nommu_vma_sem);
879 if (atomic_dec_and_test(&vma->vm_usage)) {
880 delete_nommu_vma(vma);
882 if (vma->vm_ops && vma->vm_ops->close)
883 vma->vm_ops->close(vma);
885 /* IO memory and memory shared directly out of the pagecache from
886 * ramfs/tmpfs mustn't be released here */
887 if (vma->vm_flags & VM_MAPPED_COPY) {
888 realalloc -= kobjsize((void *) vma->vm_start);
889 askedalloc -= vma->vm_end - vma->vm_start;
890 kfree((void *) vma->vm_start);
893 realalloc -= kobjsize(vma);
894 askedalloc -= sizeof(*vma);
901 up_write(&nommu_vma_sem);
905 int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
907 struct vm_list_struct *vml, **parent;
908 unsigned long end = addr + len;
911 printk("do_munmap:\n");
914 for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next)
915 if ((*parent)->vma->vm_start == addr &&
916 ((len == 0) || ((*parent)->vma->vm_end == end)))
919 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
920 current->pid, current->comm, (void *) addr);
929 realalloc -= kobjsize(vml);
930 askedalloc -= sizeof(*vml);
933 update_hiwater_vm(mm);
934 mm->total_vm -= len >> PAGE_SHIFT;
937 show_process_blocks();
943 /* Release all mmaps. */
944 void exit_mmap(struct mm_struct * mm)
946 struct vm_list_struct *tmp;
950 printk("Exit_mmap:\n");
955 while ((tmp = mm->context.vmlist)) {
956 mm->context.vmlist = tmp->next;
959 realalloc -= kobjsize(tmp);
960 askedalloc -= sizeof(*tmp);
965 show_process_blocks();
970 asmlinkage long sys_munmap(unsigned long addr, size_t len)
973 struct mm_struct *mm = current->mm;
975 down_write(&mm->mmap_sem);
976 ret = do_munmap(mm, addr, len);
977 up_write(&mm->mmap_sem);
981 unsigned long do_brk(unsigned long addr, unsigned long len)
987 * Expand (or shrink) an existing mapping, potentially moving it at the
988 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
990 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
991 * This option implies MREMAP_MAYMOVE.
993 * on uClinux, we only permit changing a mapping's size, and only as long as it stays within the
994 * hole allocated by the kmalloc() call in do_mmap_pgoff() and the block is not shareable
996 unsigned long do_mremap(unsigned long addr,
997 unsigned long old_len, unsigned long new_len,
998 unsigned long flags, unsigned long new_addr)
1000 struct vm_list_struct *vml = NULL;
1002 /* insanity checks first */
1004 return (unsigned long) -EINVAL;
1006 if (flags & MREMAP_FIXED && new_addr != addr)
1007 return (unsigned long) -EINVAL;
1009 for (vml = current->mm->context.vmlist; vml; vml = vml->next)
1010 if (vml->vma->vm_start == addr)
1013 return (unsigned long) -EINVAL;
1016 if (vml->vma->vm_end != vml->vma->vm_start + old_len)
1017 return (unsigned long) -EFAULT;
1019 if (vml->vma->vm_flags & VM_MAYSHARE)
1020 return (unsigned long) -EPERM;
1022 if (new_len > kobjsize((void *) addr))
1023 return (unsigned long) -ENOMEM;
1025 /* all checks complete - do it */
1026 vml->vma->vm_end = vml->vma->vm_start + new_len;
1028 askedalloc -= old_len;
1029 askedalloc += new_len;
1031 return vml->vma->vm_start;
1035 * Look up the first VMA which satisfies addr < vm_end, NULL if none
1037 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
1039 struct vm_list_struct *vml;
1041 for (vml = mm->context.vmlist; vml; vml = vml->next)
1042 if (addr >= vml->vma->vm_start && addr < vml->vma->vm_end)
1048 EXPORT_SYMBOL(find_vma);
1050 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1051 unsigned int foll_flags)
1056 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
1061 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1062 unsigned long to, unsigned long size, pgprot_t prot)
1064 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1068 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1072 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1073 unsigned long len, unsigned long pgoff, unsigned long flags)
1078 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1082 void unmap_mapping_range(struct address_space *mapping,
1083 loff_t const holebegin, loff_t const holelen,
1089 * Check that a process has enough memory to allocate a new virtual
1090 * mapping. 0 means there is enough memory for the allocation to
1091 * succeed and -ENOMEM implies there is not.
1093 * We currently support three overcommit policies, which are set via the
1094 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1096 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1097 * Additional code 2002 Jul 20 by Robert Love.
1099 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1101 * Note this is a helper function intended to be used by LSMs which
1102 * wish to use this logic.
1104 int __vm_enough_memory(long pages, int cap_sys_admin)
1106 unsigned long free, allowed;
1108 vm_acct_memory(pages);
1111 * Sometimes we want to use more memory than we have
1113 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1116 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1119 free = get_page_cache_size();
1120 free += nr_swap_pages;
1123 * Any slabs which are created with the
1124 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1125 * which are reclaimable, under pressure. The dentry
1126 * cache and most inode caches should fall into this
1128 free += atomic_read(&slab_reclaim_pages);
1131 * Leave the last 3% for root
1140 * nr_free_pages() is very expensive on large systems,
1141 * only call if we're about to fail.
1143 n = nr_free_pages();
1150 vm_unacct_memory(pages);
1154 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1156 * Leave the last 3% for root
1159 allowed -= allowed / 32;
1160 allowed += total_swap_pages;
1162 /* Don't let a single process grow too big:
1163 leave 3% of the size of this process for other processes */
1164 allowed -= current->mm->total_vm / 32;
1167 * cast `allowed' as a signed long because vm_committed_space
1168 * sometimes has a negative value
1170 if (atomic_read(&vm_committed_space) < (long)allowed)
1173 vm_unacct_memory(pages);
1178 int in_gate_area_no_task(unsigned long addr)
1183 struct page *filemap_nopage(struct vm_area_struct *area,
1184 unsigned long address, int *type)