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>
13 * Copyright (c) 2007 Paul Mundt <lethal@linux-sh.org>
16 #include <linux/module.h>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/tracehook.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mount.h>
29 #include <linux/personality.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
33 #include <asm/uaccess.h>
35 #include <asm/tlbflush.h>
41 unsigned long max_mapnr
;
42 unsigned long num_physpages
;
43 unsigned long askedalloc
, realalloc
;
44 atomic_long_t vm_committed_space
= ATOMIC_LONG_INIT(0);
45 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
46 int sysctl_overcommit_ratio
= 50; /* default is 50% */
47 int sysctl_max_map_count
= DEFAULT_MAX_MAP_COUNT
;
48 int heap_stack_gap
= 0;
50 EXPORT_SYMBOL(mem_map
);
51 EXPORT_SYMBOL(num_physpages
);
53 /* list of shareable VMAs */
54 struct rb_root nommu_vma_tree
= RB_ROOT
;
55 DECLARE_RWSEM(nommu_vma_sem
);
57 struct vm_operations_struct generic_file_vm_ops
= {
61 * Handle all mappings that got truncated by a "truncate()"
64 * NOTE! We have to be ready to update the memory sharing
65 * between the file and the memory map for a potential last
66 * incomplete page. Ugly, but necessary.
68 int vmtruncate(struct inode
*inode
, loff_t offset
)
70 struct address_space
*mapping
= inode
->i_mapping
;
73 if (inode
->i_size
< offset
)
75 i_size_write(inode
, offset
);
77 truncate_inode_pages(mapping
, offset
);
81 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
82 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
84 if (offset
> inode
->i_sb
->s_maxbytes
)
86 i_size_write(inode
, offset
);
89 if (inode
->i_op
&& inode
->i_op
->truncate
)
90 inode
->i_op
->truncate(inode
);
93 send_sig(SIGXFSZ
, current
, 0);
98 EXPORT_SYMBOL(vmtruncate
);
101 * Return the total memory allocated for this pointer, not
102 * just what the caller asked for.
104 * Doesn't have to be accurate, i.e. may have races.
106 unsigned int kobjsize(const void *objp
)
111 * If the object we have should not have ksize performed on it,
114 if (!objp
|| !virt_addr_valid(objp
))
117 page
= virt_to_head_page(objp
);
120 * If the allocator sets PageSlab, we know the pointer came from
127 * The ksize() function is only guaranteed to work for pointers
128 * returned by kmalloc(). So handle arbitrary pointers here.
130 return PAGE_SIZE
<< compound_order(page
);
133 int __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
134 unsigned long start
, int len
, int flags
,
135 struct page
**pages
, struct vm_area_struct
**vmas
)
137 struct vm_area_struct
*vma
;
138 unsigned long vm_flags
;
140 int write
= !!(flags
& GUP_FLAGS_WRITE
);
141 int force
= !!(flags
& GUP_FLAGS_FORCE
);
142 int ignore
= !!(flags
& GUP_FLAGS_IGNORE_VMA_PERMISSIONS
);
144 /* calculate required read or write permissions.
145 * - if 'force' is set, we only require the "MAY" flags.
147 vm_flags
= write
? (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
148 vm_flags
&= force
? (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
150 for (i
= 0; i
< len
; i
++) {
151 vma
= find_vma(mm
, start
);
153 goto finish_or_fault
;
155 /* protect what we can, including chardevs */
156 if (vma
->vm_flags
& (VM_IO
| VM_PFNMAP
) ||
157 (!ignore
&& !(vm_flags
& vma
->vm_flags
)))
158 goto finish_or_fault
;
161 pages
[i
] = virt_to_page(start
);
163 page_cache_get(pages
[i
]);
173 return i
? : -EFAULT
;
178 * get a list of pages in an address range belonging to the specified process
179 * and indicate the VMA that covers each page
180 * - this is potentially dodgy as we may end incrementing the page count of a
181 * slab page or a secondary page from a compound page
182 * - don't permit access to VMAs that don't support it, such as I/O mappings
184 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
185 unsigned long start
, int len
, int write
, int force
,
186 struct page
**pages
, struct vm_area_struct
**vmas
)
191 flags
|= GUP_FLAGS_WRITE
;
193 flags
|= GUP_FLAGS_FORCE
;
195 return __get_user_pages(tsk
, mm
,
199 EXPORT_SYMBOL(get_user_pages
);
201 DEFINE_RWLOCK(vmlist_lock
);
202 struct vm_struct
*vmlist
;
204 void vfree(const void *addr
)
208 EXPORT_SYMBOL(vfree
);
210 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
213 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
214 * returns only a logical address.
216 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
218 EXPORT_SYMBOL(__vmalloc
);
220 void *vmalloc_user(unsigned long size
)
224 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
227 struct vm_area_struct
*vma
;
229 down_write(¤t
->mm
->mmap_sem
);
230 vma
= find_vma(current
->mm
, (unsigned long)ret
);
232 vma
->vm_flags
|= VM_USERMAP
;
233 up_write(¤t
->mm
->mmap_sem
);
238 EXPORT_SYMBOL(vmalloc_user
);
240 struct page
*vmalloc_to_page(const void *addr
)
242 return virt_to_page(addr
);
244 EXPORT_SYMBOL(vmalloc_to_page
);
246 unsigned long vmalloc_to_pfn(const void *addr
)
248 return page_to_pfn(virt_to_page(addr
));
250 EXPORT_SYMBOL(vmalloc_to_pfn
);
252 long vread(char *buf
, char *addr
, unsigned long count
)
254 memcpy(buf
, addr
, count
);
258 long vwrite(char *buf
, char *addr
, unsigned long count
)
260 /* Don't allow overflow */
261 if ((unsigned long) addr
+ count
< count
)
262 count
= -(unsigned long) addr
;
264 memcpy(addr
, buf
, count
);
269 * vmalloc - allocate virtually continguos memory
271 * @size: allocation size
273 * Allocate enough pages to cover @size from the page level
274 * allocator and map them into continguos kernel virtual space.
276 * For tight control over page level allocator and protection flags
277 * use __vmalloc() instead.
279 void *vmalloc(unsigned long size
)
281 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
283 EXPORT_SYMBOL(vmalloc
);
285 void *vmalloc_node(unsigned long size
, int node
)
287 return vmalloc(size
);
289 EXPORT_SYMBOL(vmalloc_node
);
291 #ifndef PAGE_KERNEL_EXEC
292 # define PAGE_KERNEL_EXEC PAGE_KERNEL
296 * vmalloc_exec - allocate virtually contiguous, executable memory
297 * @size: allocation size
299 * Kernel-internal function to allocate enough pages to cover @size
300 * the page level allocator and map them into contiguous and
301 * executable kernel virtual space.
303 * For tight control over page level allocator and protection flags
304 * use __vmalloc() instead.
307 void *vmalloc_exec(unsigned long size
)
309 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
313 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
314 * @size: allocation size
316 * Allocate enough 32bit PA addressable pages to cover @size from the
317 * page level allocator and map them into continguos kernel virtual space.
319 void *vmalloc_32(unsigned long size
)
321 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
323 EXPORT_SYMBOL(vmalloc_32
);
326 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
327 * @size: allocation size
329 * The resulting memory area is 32bit addressable and zeroed so it can be
330 * mapped to userspace without leaking data.
332 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
333 * remap_vmalloc_range() are permissible.
335 void *vmalloc_32_user(unsigned long size
)
338 * We'll have to sort out the ZONE_DMA bits for 64-bit,
339 * but for now this can simply use vmalloc_user() directly.
341 return vmalloc_user(size
);
343 EXPORT_SYMBOL(vmalloc_32_user
);
345 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
352 void vunmap(const void *addr
)
356 EXPORT_SYMBOL(vunmap
);
359 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
362 void __attribute__((weak
)) vmalloc_sync_all(void)
366 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
371 EXPORT_SYMBOL(vm_insert_page
);
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 asmlinkage
unsigned long sys_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 return mm
->brk
= brk
;
405 static void show_process_blocks(void)
407 struct vm_list_struct
*vml
;
409 printk("Process blocks %d:", current
->pid
);
411 for (vml
= ¤t
->mm
->context
.vmlist
; vml
; vml
= vml
->next
) {
412 printk(" %p: %p", vml
, vml
->vma
);
414 printk(" (%d @%lx #%d)",
415 kobjsize((void *) vml
->vma
->vm_start
),
417 atomic_read(&vml
->vma
->vm_usage
));
418 printk(vml
->next
? " ->" : ".\n");
424 * add a VMA into a process's mm_struct in the appropriate place in the list
425 * - should be called with mm->mmap_sem held writelocked
427 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_list_struct
*vml
)
429 struct vm_list_struct
**ppv
;
431 for (ppv
= ¤t
->mm
->context
.vmlist
; *ppv
; ppv
= &(*ppv
)->next
)
432 if ((*ppv
)->vma
->vm_start
> vml
->vma
->vm_start
)
440 * look up the first VMA in which addr resides, NULL if none
441 * - should be called with mm->mmap_sem at least held readlocked
443 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
445 struct vm_list_struct
*loop
, *vml
;
447 /* search the vm_start ordered list */
449 for (loop
= mm
->context
.vmlist
; loop
; loop
= loop
->next
) {
450 if (loop
->vma
->vm_start
> addr
)
455 if (vml
&& vml
->vma
->vm_end
> addr
)
460 EXPORT_SYMBOL(find_vma
);
464 * - we don't extend stack VMAs under NOMMU conditions
466 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
468 return find_vma(mm
, addr
);
471 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
477 * look up the first VMA exactly that exactly matches addr
478 * - should be called with mm->mmap_sem at least held readlocked
480 static inline struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
483 struct vm_list_struct
*vml
;
485 /* search the vm_start ordered list */
486 for (vml
= mm
->context
.vmlist
; vml
; vml
= vml
->next
) {
487 if (vml
->vma
->vm_start
== addr
)
489 if (vml
->vma
->vm_start
> addr
)
497 * find a VMA in the global tree
499 static inline struct vm_area_struct
*find_nommu_vma(unsigned long start
)
501 struct vm_area_struct
*vma
;
502 struct rb_node
*n
= nommu_vma_tree
.rb_node
;
505 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
507 if (start
< vma
->vm_start
)
509 else if (start
> vma
->vm_start
)
519 * add a VMA in the global tree
521 static void add_nommu_vma(struct vm_area_struct
*vma
)
523 struct vm_area_struct
*pvma
;
524 struct address_space
*mapping
;
525 struct rb_node
**p
= &nommu_vma_tree
.rb_node
;
526 struct rb_node
*parent
= NULL
;
528 /* add the VMA to the mapping */
530 mapping
= vma
->vm_file
->f_mapping
;
532 flush_dcache_mmap_lock(mapping
);
533 vma_prio_tree_insert(vma
, &mapping
->i_mmap
);
534 flush_dcache_mmap_unlock(mapping
);
537 /* add the VMA to the master list */
540 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
542 if (vma
->vm_start
< pvma
->vm_start
) {
545 else if (vma
->vm_start
> pvma
->vm_start
) {
549 /* mappings are at the same address - this can only
550 * happen for shared-mem chardevs and shared file
551 * mappings backed by ramfs/tmpfs */
552 BUG_ON(!(pvma
->vm_flags
& VM_SHARED
));
563 rb_link_node(&vma
->vm_rb
, parent
, p
);
564 rb_insert_color(&vma
->vm_rb
, &nommu_vma_tree
);
568 * delete a VMA from the global list
570 static void delete_nommu_vma(struct vm_area_struct
*vma
)
572 struct address_space
*mapping
;
574 /* remove the VMA from the mapping */
576 mapping
= vma
->vm_file
->f_mapping
;
578 flush_dcache_mmap_lock(mapping
);
579 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
580 flush_dcache_mmap_unlock(mapping
);
583 /* remove from the master list */
584 rb_erase(&vma
->vm_rb
, &nommu_vma_tree
);
588 * determine whether a mapping should be permitted and, if so, what sort of
589 * mapping we're capable of supporting
591 static int validate_mmap_request(struct file
*file
,
597 unsigned long *_capabilities
)
599 unsigned long capabilities
;
600 unsigned long reqprot
= prot
;
603 /* do the simple checks first */
604 if (flags
& MAP_FIXED
|| addr
) {
606 "%d: Can't do fixed-address/overlay mmap of RAM\n",
611 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
612 (flags
& MAP_TYPE
) != MAP_SHARED
)
618 /* Careful about overflows.. */
619 len
= PAGE_ALIGN(len
);
620 if (!len
|| len
> TASK_SIZE
)
623 /* offset overflow? */
624 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
628 /* validate file mapping requests */
629 struct address_space
*mapping
;
631 /* files must support mmap */
632 if (!file
->f_op
|| !file
->f_op
->mmap
)
635 /* work out if what we've got could possibly be shared
636 * - we support chardevs that provide their own "memory"
637 * - we support files/blockdevs that are memory backed
639 mapping
= file
->f_mapping
;
641 mapping
= file
->f_path
.dentry
->d_inode
->i_mapping
;
644 if (mapping
&& mapping
->backing_dev_info
)
645 capabilities
= mapping
->backing_dev_info
->capabilities
;
648 /* no explicit capabilities set, so assume some
650 switch (file
->f_path
.dentry
->d_inode
->i_mode
& S_IFMT
) {
653 capabilities
= BDI_CAP_MAP_COPY
;
668 /* eliminate any capabilities that we can't support on this
670 if (!file
->f_op
->get_unmapped_area
)
671 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
672 if (!file
->f_op
->read
)
673 capabilities
&= ~BDI_CAP_MAP_COPY
;
675 if (flags
& MAP_SHARED
) {
676 /* do checks for writing, appending and locking */
677 if ((prot
& PROT_WRITE
) &&
678 !(file
->f_mode
& FMODE_WRITE
))
681 if (IS_APPEND(file
->f_path
.dentry
->d_inode
) &&
682 (file
->f_mode
& FMODE_WRITE
))
685 if (locks_verify_locked(file
->f_path
.dentry
->d_inode
))
688 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
691 if (((prot
& PROT_READ
) && !(capabilities
& BDI_CAP_READ_MAP
)) ||
692 ((prot
& PROT_WRITE
) && !(capabilities
& BDI_CAP_WRITE_MAP
)) ||
693 ((prot
& PROT_EXEC
) && !(capabilities
& BDI_CAP_EXEC_MAP
))
695 printk("MAP_SHARED not completely supported on !MMU\n");
699 /* we mustn't privatise shared mappings */
700 capabilities
&= ~BDI_CAP_MAP_COPY
;
703 /* we're going to read the file into private memory we
705 if (!(capabilities
& BDI_CAP_MAP_COPY
))
708 /* we don't permit a private writable mapping to be
709 * shared with the backing device */
710 if (prot
& PROT_WRITE
)
711 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
714 /* handle executable mappings and implied executable
716 if (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
) {
717 if (prot
& PROT_EXEC
)
720 else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
721 /* handle implication of PROT_EXEC by PROT_READ */
722 if (current
->personality
& READ_IMPLIES_EXEC
) {
723 if (capabilities
& BDI_CAP_EXEC_MAP
)
727 else if ((prot
& PROT_READ
) &&
728 (prot
& PROT_EXEC
) &&
729 !(capabilities
& BDI_CAP_EXEC_MAP
)
731 /* backing file is not executable, try to copy */
732 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
736 /* anonymous mappings are always memory backed and can be
739 capabilities
= BDI_CAP_MAP_COPY
;
741 /* handle PROT_EXEC implication by PROT_READ */
742 if ((prot
& PROT_READ
) &&
743 (current
->personality
& READ_IMPLIES_EXEC
))
747 /* allow the security API to have its say */
748 ret
= security_file_mmap(file
, reqprot
, prot
, flags
, addr
, 0);
753 *_capabilities
= capabilities
;
758 * we've determined that we can make the mapping, now translate what we
759 * now know into VMA flags
761 static unsigned long determine_vm_flags(struct file
*file
,
764 unsigned long capabilities
)
766 unsigned long vm_flags
;
768 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
769 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
770 /* vm_flags |= mm->def_flags; */
772 if (!(capabilities
& BDI_CAP_MAP_DIRECT
)) {
773 /* attempt to share read-only copies of mapped file chunks */
774 if (file
&& !(prot
& PROT_WRITE
))
775 vm_flags
|= VM_MAYSHARE
;
778 /* overlay a shareable mapping on the backing device or inode
779 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
781 if (flags
& MAP_SHARED
)
782 vm_flags
|= VM_MAYSHARE
| VM_SHARED
;
783 else if ((((vm_flags
& capabilities
) ^ vm_flags
) & BDI_CAP_VMFLAGS
) == 0)
784 vm_flags
|= VM_MAYSHARE
;
787 /* refuse to let anyone share private mappings with this process if
788 * it's being traced - otherwise breakpoints set in it may interfere
789 * with another untraced process
791 if ((flags
& MAP_PRIVATE
) && tracehook_expect_breakpoints(current
))
792 vm_flags
&= ~VM_MAYSHARE
;
798 * set up a shared mapping on a file
800 static int do_mmap_shared_file(struct vm_area_struct
*vma
, unsigned long len
)
804 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
808 /* getting an ENOSYS error indicates that direct mmap isn't
809 * possible (as opposed to tried but failed) so we'll fall
810 * through to making a private copy of the data and mapping
816 * set up a private mapping or an anonymous shared mapping
818 static int do_mmap_private(struct vm_area_struct
*vma
, unsigned long len
)
823 /* invoke the file's mapping function so that it can keep track of
824 * shared mappings on devices or memory
825 * - VM_MAYSHARE will be set if it may attempt to share
828 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
829 if (ret
!= -ENOSYS
) {
830 /* shouldn't return success if we're not sharing */
831 BUG_ON(ret
== 0 && !(vma
->vm_flags
& VM_MAYSHARE
));
832 return ret
; /* success or a real error */
835 /* getting an ENOSYS error indicates that direct mmap isn't
836 * possible (as opposed to tried but failed) so we'll try to
837 * make a private copy of the data and map that instead */
840 /* allocate some memory to hold the mapping
841 * - note that this may not return a page-aligned address if the object
842 * we're allocating is smaller than a page
844 base
= kmalloc(len
, GFP_KERNEL
|__GFP_COMP
);
848 vma
->vm_start
= (unsigned long) base
;
849 vma
->vm_end
= vma
->vm_start
+ len
;
850 vma
->vm_flags
|= VM_MAPPED_COPY
;
853 if (len
+ WARN_ON_SLACK
<= kobjsize(result
))
854 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
855 len
, current
->pid
, kobjsize(result
) - len
);
859 /* read the contents of a file into the copy */
863 fpos
= vma
->vm_pgoff
;
868 ret
= vma
->vm_file
->f_op
->read(vma
->vm_file
, base
, len
, &fpos
);
874 /* clear the last little bit */
876 memset(base
+ ret
, 0, len
- ret
);
879 /* if it's an anonymous mapping, then just clear it */
880 memset(base
, 0, len
);
891 printk("Allocation of length %lu from process %d failed\n",
898 * handle mapping creation for uClinux
900 unsigned long do_mmap_pgoff(struct file
*file
,
907 struct vm_list_struct
*vml
= NULL
;
908 struct vm_area_struct
*vma
= NULL
;
910 unsigned long capabilities
, vm_flags
;
914 if (!(flags
& MAP_FIXED
))
915 addr
= round_hint_to_min(addr
);
917 /* decide whether we should attempt the mapping, and if so what sort of
919 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
924 /* we've determined that we can make the mapping, now translate what we
925 * now know into VMA flags */
926 vm_flags
= determine_vm_flags(file
, prot
, flags
, capabilities
);
928 /* we're going to need to record the mapping if it works */
929 vml
= kzalloc(sizeof(struct vm_list_struct
), GFP_KERNEL
);
931 goto error_getting_vml
;
933 down_write(&nommu_vma_sem
);
935 /* if we want to share, we need to check for VMAs created by other
936 * mmap() calls that overlap with our proposed mapping
937 * - we can only share with an exact match on most regular files
938 * - shared mappings on character devices and memory backed files are
939 * permitted to overlap inexactly as far as we are concerned for in
940 * these cases, sharing is handled in the driver or filesystem rather
943 if (vm_flags
& VM_MAYSHARE
) {
944 unsigned long pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
945 unsigned long vmpglen
;
947 /* suppress VMA sharing for shared regions */
948 if (vm_flags
& VM_SHARED
&&
949 capabilities
& BDI_CAP_MAP_DIRECT
)
950 goto dont_share_VMAs
;
952 for (rb
= rb_first(&nommu_vma_tree
); rb
; rb
= rb_next(rb
)) {
953 vma
= rb_entry(rb
, struct vm_area_struct
, vm_rb
);
955 if (!(vma
->vm_flags
& VM_MAYSHARE
))
958 /* search for overlapping mappings on the same file */
959 if (vma
->vm_file
->f_path
.dentry
->d_inode
!= file
->f_path
.dentry
->d_inode
)
962 if (vma
->vm_pgoff
>= pgoff
+ pglen
)
965 vmpglen
= vma
->vm_end
- vma
->vm_start
+ PAGE_SIZE
- 1;
966 vmpglen
>>= PAGE_SHIFT
;
967 if (pgoff
>= vma
->vm_pgoff
+ vmpglen
)
970 /* handle inexactly overlapping matches between mappings */
971 if (vma
->vm_pgoff
!= pgoff
|| vmpglen
!= pglen
) {
972 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
973 goto sharing_violation
;
977 /* we've found a VMA we can share */
978 atomic_inc(&vma
->vm_usage
);
981 result
= (void *) vma
->vm_start
;
988 /* obtain the address at which to make a shared mapping
989 * - this is the hook for quasi-memory character devices to
990 * tell us the location of a shared mapping
992 if (file
&& file
->f_op
->get_unmapped_area
) {
993 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
995 if (IS_ERR((void *) addr
)) {
997 if (ret
!= (unsigned long) -ENOSYS
)
1000 /* the driver refused to tell us where to site
1001 * the mapping so we'll have to attempt to copy
1003 ret
= (unsigned long) -ENODEV
;
1004 if (!(capabilities
& BDI_CAP_MAP_COPY
))
1007 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1012 /* we're going to need a VMA struct as well */
1013 vma
= kzalloc(sizeof(struct vm_area_struct
), GFP_KERNEL
);
1015 goto error_getting_vma
;
1017 INIT_LIST_HEAD(&vma
->anon_vma_node
);
1018 atomic_set(&vma
->vm_usage
, 1);
1021 if (vm_flags
& VM_EXECUTABLE
) {
1022 added_exe_file_vma(current
->mm
);
1023 vma
->vm_mm
= current
->mm
;
1026 vma
->vm_file
= file
;
1027 vma
->vm_flags
= vm_flags
;
1028 vma
->vm_start
= addr
;
1029 vma
->vm_end
= addr
+ len
;
1030 vma
->vm_pgoff
= pgoff
;
1034 /* set up the mapping */
1035 if (file
&& vma
->vm_flags
& VM_SHARED
)
1036 ret
= do_mmap_shared_file(vma
, len
);
1038 ret
= do_mmap_private(vma
, len
);
1042 /* okay... we have a mapping; now we have to register it */
1043 result
= (void *) vma
->vm_start
;
1045 if (vma
->vm_flags
& VM_MAPPED_COPY
) {
1046 realalloc
+= kobjsize(result
);
1050 realalloc
+= kobjsize(vma
);
1051 askedalloc
+= sizeof(*vma
);
1053 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1058 realalloc
+= kobjsize(vml
);
1059 askedalloc
+= sizeof(*vml
);
1061 add_vma_to_mm(current
->mm
, vml
);
1063 up_write(&nommu_vma_sem
);
1065 if (prot
& PROT_EXEC
)
1066 flush_icache_range((unsigned long) result
,
1067 (unsigned long) result
+ len
);
1070 printk("do_mmap:\n");
1071 show_process_blocks();
1074 return (unsigned long) result
;
1077 up_write(&nommu_vma_sem
);
1082 if (vma
->vm_flags
& VM_EXECUTABLE
)
1083 removed_exe_file_vma(vma
->vm_mm
);
1090 up_write(&nommu_vma_sem
);
1091 printk("Attempt to share mismatched mappings\n");
1096 up_write(&nommu_vma_sem
);
1098 printk("Allocation of vma for %lu byte allocation from process %d failed\n",
1104 printk("Allocation of vml for %lu byte allocation from process %d failed\n",
1109 EXPORT_SYMBOL(do_mmap_pgoff
);
1112 * handle mapping disposal for uClinux
1114 static void put_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
1117 down_write(&nommu_vma_sem
);
1119 if (atomic_dec_and_test(&vma
->vm_usage
)) {
1120 delete_nommu_vma(vma
);
1122 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
1123 vma
->vm_ops
->close(vma
);
1125 /* IO memory and memory shared directly out of the pagecache from
1126 * ramfs/tmpfs mustn't be released here */
1127 if (vma
->vm_flags
& VM_MAPPED_COPY
) {
1128 realalloc
-= kobjsize((void *) vma
->vm_start
);
1129 askedalloc
-= vma
->vm_end
- vma
->vm_start
;
1130 kfree((void *) vma
->vm_start
);
1133 realalloc
-= kobjsize(vma
);
1134 askedalloc
-= sizeof(*vma
);
1138 if (vma
->vm_flags
& VM_EXECUTABLE
)
1139 removed_exe_file_vma(mm
);
1144 up_write(&nommu_vma_sem
);
1150 * - under NOMMU conditions the parameters must match exactly to the mapping to
1153 int do_munmap(struct mm_struct
*mm
, unsigned long addr
, size_t len
)
1155 struct vm_list_struct
*vml
, **parent
;
1156 unsigned long end
= addr
+ len
;
1159 printk("do_munmap:\n");
1162 for (parent
= &mm
->context
.vmlist
; *parent
; parent
= &(*parent
)->next
) {
1163 if ((*parent
)->vma
->vm_start
> addr
)
1165 if ((*parent
)->vma
->vm_start
== addr
&&
1166 ((len
== 0) || ((*parent
)->vma
->vm_end
== end
)))
1170 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
1171 current
->pid
, current
->comm
, (void *) addr
);
1177 put_vma(mm
, vml
->vma
);
1179 *parent
= vml
->next
;
1180 realalloc
-= kobjsize(vml
);
1181 askedalloc
-= sizeof(*vml
);
1184 update_hiwater_vm(mm
);
1185 mm
->total_vm
-= len
>> PAGE_SHIFT
;
1188 show_process_blocks();
1193 EXPORT_SYMBOL(do_munmap
);
1195 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
1198 struct mm_struct
*mm
= current
->mm
;
1200 down_write(&mm
->mmap_sem
);
1201 ret
= do_munmap(mm
, addr
, len
);
1202 up_write(&mm
->mmap_sem
);
1207 * Release all mappings
1209 void exit_mmap(struct mm_struct
* mm
)
1211 struct vm_list_struct
*tmp
;
1215 printk("Exit_mmap:\n");
1220 while ((tmp
= mm
->context
.vmlist
)) {
1221 mm
->context
.vmlist
= tmp
->next
;
1222 put_vma(mm
, tmp
->vma
);
1224 realalloc
-= kobjsize(tmp
);
1225 askedalloc
-= sizeof(*tmp
);
1230 show_process_blocks();
1235 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1241 * expand (or shrink) an existing mapping, potentially moving it at the same
1242 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1244 * under NOMMU conditions, we only permit changing a mapping's size, and only
1245 * as long as it stays within the hole allocated by the kmalloc() call in
1246 * do_mmap_pgoff() and the block is not shareable
1248 * MREMAP_FIXED is not supported under NOMMU conditions
1250 unsigned long do_mremap(unsigned long addr
,
1251 unsigned long old_len
, unsigned long new_len
,
1252 unsigned long flags
, unsigned long new_addr
)
1254 struct vm_area_struct
*vma
;
1256 /* insanity checks first */
1258 return (unsigned long) -EINVAL
;
1260 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1261 return (unsigned long) -EINVAL
;
1263 vma
= find_vma_exact(current
->mm
, addr
);
1265 return (unsigned long) -EINVAL
;
1267 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1268 return (unsigned long) -EFAULT
;
1270 if (vma
->vm_flags
& VM_MAYSHARE
)
1271 return (unsigned long) -EPERM
;
1273 if (new_len
> kobjsize((void *) addr
))
1274 return (unsigned long) -ENOMEM
;
1276 /* all checks complete - do it */
1277 vma
->vm_end
= vma
->vm_start
+ new_len
;
1279 askedalloc
-= old_len
;
1280 askedalloc
+= new_len
;
1282 return vma
->vm_start
;
1284 EXPORT_SYMBOL(do_mremap
);
1286 asmlinkage
unsigned long sys_mremap(unsigned long addr
,
1287 unsigned long old_len
, unsigned long new_len
,
1288 unsigned long flags
, unsigned long new_addr
)
1292 down_write(¤t
->mm
->mmap_sem
);
1293 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1294 up_write(¤t
->mm
->mmap_sem
);
1298 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1299 unsigned int foll_flags
)
1304 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long from
,
1305 unsigned long to
, unsigned long size
, pgprot_t prot
)
1307 vma
->vm_start
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1310 EXPORT_SYMBOL(remap_pfn_range
);
1312 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1313 unsigned long pgoff
)
1315 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1317 if (!(vma
->vm_flags
& VM_USERMAP
))
1320 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1321 vma
->vm_end
= vma
->vm_start
+ size
;
1325 EXPORT_SYMBOL(remap_vmalloc_range
);
1327 void swap_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
1331 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1332 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1337 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1341 void unmap_mapping_range(struct address_space
*mapping
,
1342 loff_t
const holebegin
, loff_t
const holelen
,
1346 EXPORT_SYMBOL(unmap_mapping_range
);
1349 * ask for an unmapped area at which to create a mapping on a file
1351 unsigned long get_unmapped_area(struct file
*file
, unsigned long addr
,
1352 unsigned long len
, unsigned long pgoff
,
1353 unsigned long flags
)
1355 unsigned long (*get_area
)(struct file
*, unsigned long, unsigned long,
1356 unsigned long, unsigned long);
1358 get_area
= current
->mm
->get_unmapped_area
;
1359 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
1360 get_area
= file
->f_op
->get_unmapped_area
;
1365 return get_area(file
, addr
, len
, pgoff
, flags
);
1367 EXPORT_SYMBOL(get_unmapped_area
);
1370 * Check that a process has enough memory to allocate a new virtual
1371 * mapping. 0 means there is enough memory for the allocation to
1372 * succeed and -ENOMEM implies there is not.
1374 * We currently support three overcommit policies, which are set via the
1375 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1377 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1378 * Additional code 2002 Jul 20 by Robert Love.
1380 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1382 * Note this is a helper function intended to be used by LSMs which
1383 * wish to use this logic.
1385 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1387 unsigned long free
, allowed
;
1389 vm_acct_memory(pages
);
1392 * Sometimes we want to use more memory than we have
1394 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1397 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1400 free
= global_page_state(NR_FILE_PAGES
);
1401 free
+= nr_swap_pages
;
1404 * Any slabs which are created with the
1405 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1406 * which are reclaimable, under pressure. The dentry
1407 * cache and most inode caches should fall into this
1409 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1412 * Leave the last 3% for root
1421 * nr_free_pages() is very expensive on large systems,
1422 * only call if we're about to fail.
1424 n
= nr_free_pages();
1427 * Leave reserved pages. The pages are not for anonymous pages.
1429 if (n
<= totalreserve_pages
)
1432 n
-= totalreserve_pages
;
1435 * Leave the last 3% for root
1447 allowed
= totalram_pages
* sysctl_overcommit_ratio
/ 100;
1449 * Leave the last 3% for root
1452 allowed
-= allowed
/ 32;
1453 allowed
+= total_swap_pages
;
1455 /* Don't let a single process grow too big:
1456 leave 3% of the size of this process for other processes */
1457 allowed
-= current
->mm
->total_vm
/ 32;
1460 * cast `allowed' as a signed long because vm_committed_space
1461 * sometimes has a negative value
1463 if (atomic_long_read(&vm_committed_space
) < (long)allowed
)
1466 vm_unacct_memory(pages
);
1471 int in_gate_area_no_task(unsigned long addr
)
1476 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1481 EXPORT_SYMBOL(filemap_fault
);
1484 * Access another process' address space.
1485 * - source/target buffer must be kernel space
1487 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
1489 struct vm_area_struct
*vma
;
1490 struct mm_struct
*mm
;
1492 if (addr
+ len
< addr
)
1495 mm
= get_task_mm(tsk
);
1499 down_read(&mm
->mmap_sem
);
1501 /* the access must start within one of the target process's mappings */
1502 vma
= find_vma(mm
, addr
);
1504 /* don't overrun this mapping */
1505 if (addr
+ len
>= vma
->vm_end
)
1506 len
= vma
->vm_end
- addr
;
1508 /* only read or write mappings where it is permitted */
1509 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1510 len
-= copy_to_user((void *) addr
, buf
, len
);
1511 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1512 len
-= copy_from_user(buf
, (void *) addr
, len
);
1519 up_read(&mm
->mmap_sem
);