4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
7 * See Documentation/nommu-mmap.txt
9 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
10 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
13 * Copyright (c) 2007 Paul Mundt <lethal@linux-sh.org>
16 #include <linux/module.h>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/tracehook.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mount.h>
29 #include <linux/personality.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
33 #include <asm/uaccess.h>
35 #include <asm/tlbflush.h>
38 static inline __attribute__((format(printf
, 1, 2)))
39 void no_printk(const char *fmt
, ...)
44 #define kenter(FMT, ...) \
45 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
46 #define kleave(FMT, ...) \
47 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
48 #define kdebug(FMT, ...) \
49 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
51 #define kenter(FMT, ...) \
52 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
53 #define kleave(FMT, ...) \
54 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
55 #define kdebug(FMT, ...) \
56 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
63 unsigned long max_mapnr
;
64 unsigned long num_physpages
;
65 atomic_long_t vm_committed_space
= ATOMIC_LONG_INIT(0);
66 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
67 int sysctl_overcommit_ratio
= 50; /* default is 50% */
68 int sysctl_max_map_count
= DEFAULT_MAX_MAP_COUNT
;
69 int heap_stack_gap
= 0;
71 atomic_t mmap_pages_allocated
;
73 EXPORT_SYMBOL(mem_map
);
74 EXPORT_SYMBOL(num_physpages
);
76 /* list of mapped, potentially shareable regions */
77 static struct kmem_cache
*vm_region_jar
;
78 struct rb_root nommu_region_tree
= RB_ROOT
;
79 DECLARE_RWSEM(nommu_region_sem
);
81 struct vm_operations_struct generic_file_vm_ops
= {
85 * Handle all mappings that got truncated by a "truncate()"
88 * NOTE! We have to be ready to update the memory sharing
89 * between the file and the memory map for a potential last
90 * incomplete page. Ugly, but necessary.
92 int vmtruncate(struct inode
*inode
, loff_t offset
)
94 struct address_space
*mapping
= inode
->i_mapping
;
97 if (inode
->i_size
< offset
)
99 i_size_write(inode
, offset
);
101 truncate_inode_pages(mapping
, offset
);
105 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
106 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
108 if (offset
> inode
->i_sb
->s_maxbytes
)
110 i_size_write(inode
, offset
);
113 if (inode
->i_op
->truncate
)
114 inode
->i_op
->truncate(inode
);
117 send_sig(SIGXFSZ
, current
, 0);
122 EXPORT_SYMBOL(vmtruncate
);
125 * Return the total memory allocated for this pointer, not
126 * just what the caller asked for.
128 * Doesn't have to be accurate, i.e. may have races.
130 unsigned int kobjsize(const void *objp
)
135 * If the object we have should not have ksize performed on it,
138 if (!objp
|| !virt_addr_valid(objp
))
141 page
= virt_to_head_page(objp
);
144 * If the allocator sets PageSlab, we know the pointer came from
151 * The ksize() function is only guaranteed to work for pointers
152 * returned by kmalloc(). So handle arbitrary pointers here.
154 return PAGE_SIZE
<< compound_order(page
);
157 int __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
158 unsigned long start
, int len
, int flags
,
159 struct page
**pages
, struct vm_area_struct
**vmas
)
161 struct vm_area_struct
*vma
;
162 unsigned long vm_flags
;
164 int write
= !!(flags
& GUP_FLAGS_WRITE
);
165 int force
= !!(flags
& GUP_FLAGS_FORCE
);
166 int ignore
= !!(flags
& GUP_FLAGS_IGNORE_VMA_PERMISSIONS
);
168 /* calculate required read or write permissions.
169 * - if 'force' is set, we only require the "MAY" flags.
171 vm_flags
= write
? (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
172 vm_flags
&= force
? (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
174 for (i
= 0; i
< len
; i
++) {
175 vma
= find_vma(mm
, start
);
177 goto finish_or_fault
;
179 /* protect what we can, including chardevs */
180 if (vma
->vm_flags
& (VM_IO
| VM_PFNMAP
) ||
181 (!ignore
&& !(vm_flags
& vma
->vm_flags
)))
182 goto finish_or_fault
;
185 pages
[i
] = virt_to_page(start
);
187 page_cache_get(pages
[i
]);
197 return i
? : -EFAULT
;
202 * get a list of pages in an address range belonging to the specified process
203 * and indicate the VMA that covers each page
204 * - this is potentially dodgy as we may end incrementing the page count of a
205 * slab page or a secondary page from a compound page
206 * - don't permit access to VMAs that don't support it, such as I/O mappings
208 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
209 unsigned long start
, int len
, int write
, int force
,
210 struct page
**pages
, struct vm_area_struct
**vmas
)
215 flags
|= GUP_FLAGS_WRITE
;
217 flags
|= GUP_FLAGS_FORCE
;
219 return __get_user_pages(tsk
, mm
,
223 EXPORT_SYMBOL(get_user_pages
);
225 DEFINE_RWLOCK(vmlist_lock
);
226 struct vm_struct
*vmlist
;
228 void vfree(const void *addr
)
232 EXPORT_SYMBOL(vfree
);
234 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
237 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
238 * returns only a logical address.
240 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
242 EXPORT_SYMBOL(__vmalloc
);
244 void *vmalloc_user(unsigned long size
)
248 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
251 struct vm_area_struct
*vma
;
253 down_write(¤t
->mm
->mmap_sem
);
254 vma
= find_vma(current
->mm
, (unsigned long)ret
);
256 vma
->vm_flags
|= VM_USERMAP
;
257 up_write(¤t
->mm
->mmap_sem
);
262 EXPORT_SYMBOL(vmalloc_user
);
264 struct page
*vmalloc_to_page(const void *addr
)
266 return virt_to_page(addr
);
268 EXPORT_SYMBOL(vmalloc_to_page
);
270 unsigned long vmalloc_to_pfn(const void *addr
)
272 return page_to_pfn(virt_to_page(addr
));
274 EXPORT_SYMBOL(vmalloc_to_pfn
);
276 long vread(char *buf
, char *addr
, unsigned long count
)
278 memcpy(buf
, addr
, count
);
282 long vwrite(char *buf
, char *addr
, unsigned long count
)
284 /* Don't allow overflow */
285 if ((unsigned long) addr
+ count
< count
)
286 count
= -(unsigned long) addr
;
288 memcpy(addr
, buf
, count
);
293 * vmalloc - allocate virtually continguos memory
295 * @size: allocation size
297 * Allocate enough pages to cover @size from the page level
298 * allocator and map them into continguos kernel virtual space.
300 * For tight control over page level allocator and protection flags
301 * use __vmalloc() instead.
303 void *vmalloc(unsigned long size
)
305 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
307 EXPORT_SYMBOL(vmalloc
);
309 void *vmalloc_node(unsigned long size
, int node
)
311 return vmalloc(size
);
313 EXPORT_SYMBOL(vmalloc_node
);
315 #ifndef PAGE_KERNEL_EXEC
316 # define PAGE_KERNEL_EXEC PAGE_KERNEL
320 * vmalloc_exec - allocate virtually contiguous, executable memory
321 * @size: allocation size
323 * Kernel-internal function to allocate enough pages to cover @size
324 * the page level allocator and map them into contiguous and
325 * executable kernel virtual space.
327 * For tight control over page level allocator and protection flags
328 * use __vmalloc() instead.
331 void *vmalloc_exec(unsigned long size
)
333 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
337 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
338 * @size: allocation size
340 * Allocate enough 32bit PA addressable pages to cover @size from the
341 * page level allocator and map them into continguos kernel virtual space.
343 void *vmalloc_32(unsigned long size
)
345 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
347 EXPORT_SYMBOL(vmalloc_32
);
350 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
351 * @size: allocation size
353 * The resulting memory area is 32bit addressable and zeroed so it can be
354 * mapped to userspace without leaking data.
356 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
357 * remap_vmalloc_range() are permissible.
359 void *vmalloc_32_user(unsigned long size
)
362 * We'll have to sort out the ZONE_DMA bits for 64-bit,
363 * but for now this can simply use vmalloc_user() directly.
365 return vmalloc_user(size
);
367 EXPORT_SYMBOL(vmalloc_32_user
);
369 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
376 void vunmap(const void *addr
)
380 EXPORT_SYMBOL(vunmap
);
383 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
386 void __attribute__((weak
)) vmalloc_sync_all(void)
390 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
395 EXPORT_SYMBOL(vm_insert_page
);
398 * sys_brk() for the most part doesn't need the global kernel
399 * lock, except when an application is doing something nasty
400 * like trying to un-brk an area that has already been mapped
401 * to a regular file. in this case, the unmapping will need
402 * to invoke file system routines that need the global lock.
404 asmlinkage
unsigned long sys_brk(unsigned long brk
)
406 struct mm_struct
*mm
= current
->mm
;
408 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
415 * Always allow shrinking brk
417 if (brk
<= mm
->brk
) {
423 * Ok, looks good - let it rip.
425 return mm
->brk
= brk
;
429 * initialise the VMA and region record slabs
431 void __init
mmap_init(void)
433 vm_region_jar
= kmem_cache_create("vm_region_jar",
434 sizeof(struct vm_region
), 0,
436 vm_area_cachep
= kmem_cache_create("vm_area_struct",
437 sizeof(struct vm_area_struct
), 0,
442 * validate the region tree
443 * - the caller must hold the region lock
445 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
446 static noinline
void validate_nommu_regions(void)
448 struct vm_region
*region
, *last
;
449 struct rb_node
*p
, *lastp
;
451 lastp
= rb_first(&nommu_region_tree
);
455 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
456 if (unlikely(last
->vm_end
<= last
->vm_start
))
459 while ((p
= rb_next(lastp
))) {
460 region
= rb_entry(p
, struct vm_region
, vm_rb
);
461 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
463 if (unlikely(region
->vm_end
<= region
->vm_start
))
465 if (unlikely(region
->vm_start
< last
->vm_end
))
472 #define validate_nommu_regions() do {} while(0)
476 * add a region into the global tree
478 static void add_nommu_region(struct vm_region
*region
)
480 struct vm_region
*pregion
;
481 struct rb_node
**p
, *parent
;
483 validate_nommu_regions();
485 BUG_ON(region
->vm_start
& ~PAGE_MASK
);
488 p
= &nommu_region_tree
.rb_node
;
491 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
492 if (region
->vm_start
< pregion
->vm_start
)
494 else if (region
->vm_start
> pregion
->vm_start
)
496 else if (pregion
== region
)
502 rb_link_node(®ion
->vm_rb
, parent
, p
);
503 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
505 validate_nommu_regions();
509 * delete a region from the global tree
511 static void delete_nommu_region(struct vm_region
*region
)
513 BUG_ON(!nommu_region_tree
.rb_node
);
515 validate_nommu_regions();
516 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
517 validate_nommu_regions();
521 * free a contiguous series of pages
523 static void free_page_series(unsigned long from
, unsigned long to
)
525 for (; from
< to
; from
+= PAGE_SIZE
) {
526 struct page
*page
= virt_to_page(from
);
528 kdebug("- free %lx", from
);
529 atomic_dec(&mmap_pages_allocated
);
530 if (page_count(page
) != 1)
531 kdebug("free page %p [%d]", page
, page_count(page
));
537 * release a reference to a region
538 * - the caller must hold the region semaphore, which this releases
539 * - the region may not have been added to the tree yet, in which case vm_end
540 * will equal vm_start
542 static void __put_nommu_region(struct vm_region
*region
)
543 __releases(nommu_region_sem
)
545 kenter("%p{%d}", region
, atomic_read(®ion
->vm_usage
));
547 BUG_ON(!nommu_region_tree
.rb_node
);
549 if (atomic_dec_and_test(®ion
->vm_usage
)) {
550 if (region
->vm_end
> region
->vm_start
)
551 delete_nommu_region(region
);
552 up_write(&nommu_region_sem
);
555 fput(region
->vm_file
);
557 /* IO memory and memory shared directly out of the pagecache
558 * from ramfs/tmpfs mustn't be released here */
559 if (region
->vm_flags
& VM_MAPPED_COPY
) {
560 kdebug("free series");
561 free_page_series(region
->vm_start
, region
->vm_end
);
563 kmem_cache_free(vm_region_jar
, region
);
565 up_write(&nommu_region_sem
);
570 * release a reference to a region
572 static void put_nommu_region(struct vm_region
*region
)
574 down_write(&nommu_region_sem
);
575 __put_nommu_region(region
);
579 * add a VMA into a process's mm_struct in the appropriate place in the list
580 * and tree and add to the address space's page tree also if not an anonymous
582 * - should be called with mm->mmap_sem held writelocked
584 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
586 struct vm_area_struct
*pvma
, **pp
;
587 struct address_space
*mapping
;
588 struct rb_node
**p
, *parent
;
592 BUG_ON(!vma
->vm_region
);
597 /* add the VMA to the mapping */
599 mapping
= vma
->vm_file
->f_mapping
;
601 flush_dcache_mmap_lock(mapping
);
602 vma_prio_tree_insert(vma
, &mapping
->i_mmap
);
603 flush_dcache_mmap_unlock(mapping
);
606 /* add the VMA to the tree */
608 p
= &mm
->mm_rb
.rb_node
;
611 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
613 /* sort by: start addr, end addr, VMA struct addr in that order
614 * (the latter is necessary as we may get identical VMAs) */
615 if (vma
->vm_start
< pvma
->vm_start
)
617 else if (vma
->vm_start
> pvma
->vm_start
)
619 else if (vma
->vm_end
< pvma
->vm_end
)
621 else if (vma
->vm_end
> pvma
->vm_end
)
631 rb_link_node(&vma
->vm_rb
, parent
, p
);
632 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
634 /* add VMA to the VMA list also */
635 for (pp
= &mm
->mmap
; (pvma
= *pp
); pp
= &(*pp
)->vm_next
) {
636 if (pvma
->vm_start
> vma
->vm_start
)
638 if (pvma
->vm_start
< vma
->vm_start
)
640 if (pvma
->vm_end
< vma
->vm_end
)
649 * delete a VMA from its owning mm_struct and address space
651 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
653 struct vm_area_struct
**pp
;
654 struct address_space
*mapping
;
655 struct mm_struct
*mm
= vma
->vm_mm
;
660 if (mm
->mmap_cache
== vma
)
661 mm
->mmap_cache
= NULL
;
663 /* remove the VMA from the mapping */
665 mapping
= vma
->vm_file
->f_mapping
;
667 flush_dcache_mmap_lock(mapping
);
668 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
669 flush_dcache_mmap_unlock(mapping
);
672 /* remove from the MM's tree and list */
673 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
674 for (pp
= &mm
->mmap
; *pp
; pp
= &(*pp
)->vm_next
) {
685 * destroy a VMA record
687 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
690 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
691 vma
->vm_ops
->close(vma
);
694 if (vma
->vm_flags
& VM_EXECUTABLE
)
695 removed_exe_file_vma(mm
);
697 put_nommu_region(vma
->vm_region
);
698 kmem_cache_free(vm_area_cachep
, vma
);
702 * look up the first VMA in which addr resides, NULL if none
703 * - should be called with mm->mmap_sem at least held readlocked
705 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
707 struct vm_area_struct
*vma
;
708 struct rb_node
*n
= mm
->mm_rb
.rb_node
;
710 /* check the cache first */
711 vma
= mm
->mmap_cache
;
712 if (vma
&& vma
->vm_start
<= addr
&& vma
->vm_end
> addr
)
715 /* trawl the tree (there may be multiple mappings in which addr
717 for (n
= rb_first(&mm
->mm_rb
); n
; n
= rb_next(n
)) {
718 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
719 if (vma
->vm_start
> addr
)
721 if (vma
->vm_end
> addr
) {
722 mm
->mmap_cache
= vma
;
729 EXPORT_SYMBOL(find_vma
);
733 * - we don't extend stack VMAs under NOMMU conditions
735 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
737 return find_vma(mm
, addr
);
741 * expand a stack to a given address
742 * - not supported under NOMMU conditions
744 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
750 * look up the first VMA exactly that exactly matches addr
751 * - should be called with mm->mmap_sem at least held readlocked
753 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
757 struct vm_area_struct
*vma
;
758 struct rb_node
*n
= mm
->mm_rb
.rb_node
;
759 unsigned long end
= addr
+ len
;
761 /* check the cache first */
762 vma
= mm
->mmap_cache
;
763 if (vma
&& vma
->vm_start
== addr
&& vma
->vm_end
== end
)
766 /* trawl the tree (there may be multiple mappings in which addr
768 for (n
= rb_first(&mm
->mm_rb
); n
; n
= rb_next(n
)) {
769 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
770 if (vma
->vm_start
< addr
)
772 if (vma
->vm_start
> addr
)
774 if (vma
->vm_end
== end
) {
775 mm
->mmap_cache
= vma
;
784 * determine whether a mapping should be permitted and, if so, what sort of
785 * mapping we're capable of supporting
787 static int validate_mmap_request(struct file
*file
,
793 unsigned long *_capabilities
)
795 unsigned long capabilities
, rlen
;
796 unsigned long reqprot
= prot
;
799 /* do the simple checks first */
800 if (flags
& MAP_FIXED
|| addr
) {
802 "%d: Can't do fixed-address/overlay mmap of RAM\n",
807 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
808 (flags
& MAP_TYPE
) != MAP_SHARED
)
814 /* Careful about overflows.. */
815 rlen
= PAGE_ALIGN(len
);
816 if (!rlen
|| rlen
> TASK_SIZE
)
819 /* offset overflow? */
820 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
824 /* validate file mapping requests */
825 struct address_space
*mapping
;
827 /* files must support mmap */
828 if (!file
->f_op
|| !file
->f_op
->mmap
)
831 /* work out if what we've got could possibly be shared
832 * - we support chardevs that provide their own "memory"
833 * - we support files/blockdevs that are memory backed
835 mapping
= file
->f_mapping
;
837 mapping
= file
->f_path
.dentry
->d_inode
->i_mapping
;
840 if (mapping
&& mapping
->backing_dev_info
)
841 capabilities
= mapping
->backing_dev_info
->capabilities
;
844 /* no explicit capabilities set, so assume some
846 switch (file
->f_path
.dentry
->d_inode
->i_mode
& S_IFMT
) {
849 capabilities
= BDI_CAP_MAP_COPY
;
864 /* eliminate any capabilities that we can't support on this
866 if (!file
->f_op
->get_unmapped_area
)
867 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
868 if (!file
->f_op
->read
)
869 capabilities
&= ~BDI_CAP_MAP_COPY
;
871 if (flags
& MAP_SHARED
) {
872 /* do checks for writing, appending and locking */
873 if ((prot
& PROT_WRITE
) &&
874 !(file
->f_mode
& FMODE_WRITE
))
877 if (IS_APPEND(file
->f_path
.dentry
->d_inode
) &&
878 (file
->f_mode
& FMODE_WRITE
))
881 if (locks_verify_locked(file
->f_path
.dentry
->d_inode
))
884 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
887 if (((prot
& PROT_READ
) && !(capabilities
& BDI_CAP_READ_MAP
)) ||
888 ((prot
& PROT_WRITE
) && !(capabilities
& BDI_CAP_WRITE_MAP
)) ||
889 ((prot
& PROT_EXEC
) && !(capabilities
& BDI_CAP_EXEC_MAP
))
891 printk("MAP_SHARED not completely supported on !MMU\n");
895 /* we mustn't privatise shared mappings */
896 capabilities
&= ~BDI_CAP_MAP_COPY
;
899 /* we're going to read the file into private memory we
901 if (!(capabilities
& BDI_CAP_MAP_COPY
))
904 /* we don't permit a private writable mapping to be
905 * shared with the backing device */
906 if (prot
& PROT_WRITE
)
907 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
910 /* handle executable mappings and implied executable
912 if (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
) {
913 if (prot
& PROT_EXEC
)
916 else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
917 /* handle implication of PROT_EXEC by PROT_READ */
918 if (current
->personality
& READ_IMPLIES_EXEC
) {
919 if (capabilities
& BDI_CAP_EXEC_MAP
)
923 else if ((prot
& PROT_READ
) &&
924 (prot
& PROT_EXEC
) &&
925 !(capabilities
& BDI_CAP_EXEC_MAP
)
927 /* backing file is not executable, try to copy */
928 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
932 /* anonymous mappings are always memory backed and can be
935 capabilities
= BDI_CAP_MAP_COPY
;
937 /* handle PROT_EXEC implication by PROT_READ */
938 if ((prot
& PROT_READ
) &&
939 (current
->personality
& READ_IMPLIES_EXEC
))
943 /* allow the security API to have its say */
944 ret
= security_file_mmap(file
, reqprot
, prot
, flags
, addr
, 0);
949 *_capabilities
= capabilities
;
954 * we've determined that we can make the mapping, now translate what we
955 * now know into VMA flags
957 static unsigned long determine_vm_flags(struct file
*file
,
960 unsigned long capabilities
)
962 unsigned long vm_flags
;
964 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
965 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
966 /* vm_flags |= mm->def_flags; */
968 if (!(capabilities
& BDI_CAP_MAP_DIRECT
)) {
969 /* attempt to share read-only copies of mapped file chunks */
970 if (file
&& !(prot
& PROT_WRITE
))
971 vm_flags
|= VM_MAYSHARE
;
974 /* overlay a shareable mapping on the backing device or inode
975 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
977 if (flags
& MAP_SHARED
)
978 vm_flags
|= VM_MAYSHARE
| VM_SHARED
;
979 else if ((((vm_flags
& capabilities
) ^ vm_flags
) & BDI_CAP_VMFLAGS
) == 0)
980 vm_flags
|= VM_MAYSHARE
;
983 /* refuse to let anyone share private mappings with this process if
984 * it's being traced - otherwise breakpoints set in it may interfere
985 * with another untraced process
987 if ((flags
& MAP_PRIVATE
) && tracehook_expect_breakpoints(current
))
988 vm_flags
&= ~VM_MAYSHARE
;
994 * set up a shared mapping on a file (the driver or filesystem provides and
997 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1001 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1005 /* getting an ENOSYS error indicates that direct mmap isn't
1006 * possible (as opposed to tried but failed) so we'll fall
1007 * through to making a private copy of the data and mapping
1013 * set up a private mapping or an anonymous shared mapping
1015 static int do_mmap_private(struct vm_area_struct
*vma
,
1016 struct vm_region
*region
,
1020 unsigned long total
, point
, n
, rlen
;
1024 /* invoke the file's mapping function so that it can keep track of
1025 * shared mappings on devices or memory
1026 * - VM_MAYSHARE will be set if it may attempt to share
1029 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1030 if (ret
!= -ENOSYS
) {
1031 /* shouldn't return success if we're not sharing */
1032 BUG_ON(ret
== 0 && !(vma
->vm_flags
& VM_MAYSHARE
));
1033 return ret
; /* success or a real error */
1036 /* getting an ENOSYS error indicates that direct mmap isn't
1037 * possible (as opposed to tried but failed) so we'll try to
1038 * make a private copy of the data and map that instead */
1041 rlen
= PAGE_ALIGN(len
);
1043 /* allocate some memory to hold the mapping
1044 * - note that this may not return a page-aligned address if the object
1045 * we're allocating is smaller than a page
1047 order
= get_order(rlen
);
1048 kdebug("alloc order %d for %lx", order
, len
);
1050 pages
= alloc_pages(GFP_KERNEL
, order
);
1054 /* we allocated a power-of-2 sized page set, so we need to trim off the
1057 atomic_add(total
, &mmap_pages_allocated
);
1059 point
= rlen
>> PAGE_SHIFT
;
1060 while (total
> point
) {
1061 order
= ilog2(total
- point
);
1063 kdebug("shave %lu/%lu @%lu", n
, total
- point
, total
);
1064 atomic_sub(n
, &mmap_pages_allocated
);
1066 set_page_refcounted(pages
+ total
);
1067 __free_pages(pages
+ total
, order
);
1070 total
= rlen
>> PAGE_SHIFT
;
1071 for (point
= 1; point
< total
; point
++)
1072 set_page_refcounted(&pages
[point
]);
1074 base
= page_address(pages
);
1075 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1076 region
->vm_start
= (unsigned long) base
;
1077 region
->vm_end
= region
->vm_start
+ rlen
;
1079 vma
->vm_start
= region
->vm_start
;
1080 vma
->vm_end
= region
->vm_start
+ len
;
1083 /* read the contents of a file into the copy */
1084 mm_segment_t old_fs
;
1087 fpos
= vma
->vm_pgoff
;
1088 fpos
<<= PAGE_SHIFT
;
1092 ret
= vma
->vm_file
->f_op
->read(vma
->vm_file
, base
, rlen
, &fpos
);
1098 /* clear the last little bit */
1100 memset(base
+ ret
, 0, rlen
- ret
);
1103 /* if it's an anonymous mapping, then just clear it */
1104 memset(base
, 0, rlen
);
1110 free_page_series(region
->vm_start
, region
->vm_end
);
1111 region
->vm_start
= vma
->vm_start
= 0;
1112 region
->vm_end
= vma
->vm_end
= 0;
1116 printk("Allocation of length %lu from process %d failed\n",
1123 * handle mapping creation for uClinux
1125 unsigned long do_mmap_pgoff(struct file
*file
,
1129 unsigned long flags
,
1130 unsigned long pgoff
)
1132 struct vm_area_struct
*vma
;
1133 struct vm_region
*region
;
1135 unsigned long capabilities
, vm_flags
, result
;
1138 kenter(",%lx,%lx,%lx,%lx,%lx", addr
, len
, prot
, flags
, pgoff
);
1140 if (!(flags
& MAP_FIXED
))
1141 addr
= round_hint_to_min(addr
);
1143 /* decide whether we should attempt the mapping, and if so what sort of
1145 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1148 kleave(" = %d [val]", ret
);
1152 /* we've determined that we can make the mapping, now translate what we
1153 * now know into VMA flags */
1154 vm_flags
= determine_vm_flags(file
, prot
, flags
, capabilities
);
1156 /* we're going to need to record the mapping */
1157 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1159 goto error_getting_region
;
1161 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1163 goto error_getting_vma
;
1165 atomic_set(®ion
->vm_usage
, 1);
1166 region
->vm_flags
= vm_flags
;
1167 region
->vm_pgoff
= pgoff
;
1169 INIT_LIST_HEAD(&vma
->anon_vma_node
);
1170 vma
->vm_flags
= vm_flags
;
1171 vma
->vm_pgoff
= pgoff
;
1174 region
->vm_file
= file
;
1176 vma
->vm_file
= file
;
1178 if (vm_flags
& VM_EXECUTABLE
) {
1179 added_exe_file_vma(current
->mm
);
1180 vma
->vm_mm
= current
->mm
;
1184 down_write(&nommu_region_sem
);
1186 /* if we want to share, we need to check for regions created by other
1187 * mmap() calls that overlap with our proposed mapping
1188 * - we can only share with a superset match on most regular files
1189 * - shared mappings on character devices and memory backed files are
1190 * permitted to overlap inexactly as far as we are concerned for in
1191 * these cases, sharing is handled in the driver or filesystem rather
1194 if (vm_flags
& VM_MAYSHARE
) {
1195 struct vm_region
*pregion
;
1196 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1198 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1199 pgend
= pgoff
+ pglen
;
1201 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1202 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1204 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1207 /* search for overlapping mappings on the same file */
1208 if (pregion
->vm_file
->f_path
.dentry
->d_inode
!=
1209 file
->f_path
.dentry
->d_inode
)
1212 if (pregion
->vm_pgoff
>= pgend
)
1215 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1216 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1217 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1218 if (pgoff
>= rpgend
)
1221 /* handle inexactly overlapping matches between
1223 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1224 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1225 /* new mapping is not a subset of the region */
1226 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
1227 goto sharing_violation
;
1231 /* we've found a region we can share */
1232 atomic_inc(&pregion
->vm_usage
);
1233 vma
->vm_region
= pregion
;
1234 start
= pregion
->vm_start
;
1235 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1236 vma
->vm_start
= start
;
1237 vma
->vm_end
= start
+ len
;
1239 if (pregion
->vm_flags
& VM_MAPPED_COPY
) {
1240 kdebug("share copy");
1241 vma
->vm_flags
|= VM_MAPPED_COPY
;
1243 kdebug("share mmap");
1244 ret
= do_mmap_shared_file(vma
);
1246 vma
->vm_region
= NULL
;
1249 atomic_dec(&pregion
->vm_usage
);
1251 goto error_just_free
;
1254 fput(region
->vm_file
);
1255 kmem_cache_free(vm_region_jar
, region
);
1261 /* obtain the address at which to make a shared mapping
1262 * - this is the hook for quasi-memory character devices to
1263 * tell us the location of a shared mapping
1265 if (file
&& file
->f_op
->get_unmapped_area
) {
1266 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1268 if (IS_ERR((void *) addr
)) {
1270 if (ret
!= (unsigned long) -ENOSYS
)
1271 goto error_just_free
;
1273 /* the driver refused to tell us where to site
1274 * the mapping so we'll have to attempt to copy
1276 ret
= (unsigned long) -ENODEV
;
1277 if (!(capabilities
& BDI_CAP_MAP_COPY
))
1278 goto error_just_free
;
1280 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1282 vma
->vm_start
= region
->vm_start
= addr
;
1283 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1288 vma
->vm_region
= region
;
1290 /* set up the mapping */
1291 if (file
&& vma
->vm_flags
& VM_SHARED
)
1292 ret
= do_mmap_shared_file(vma
);
1294 ret
= do_mmap_private(vma
, region
, len
);
1296 goto error_put_region
;
1298 add_nommu_region(region
);
1300 /* okay... we have a mapping; now we have to register it */
1301 result
= vma
->vm_start
;
1303 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1306 add_vma_to_mm(current
->mm
, vma
);
1308 up_write(&nommu_region_sem
);
1310 if (prot
& PROT_EXEC
)
1311 flush_icache_range(result
, result
+ len
);
1313 kleave(" = %lx", result
);
1317 __put_nommu_region(region
);
1321 if (vma
->vm_flags
& VM_EXECUTABLE
)
1322 removed_exe_file_vma(vma
->vm_mm
);
1324 kmem_cache_free(vm_area_cachep
, vma
);
1326 kleave(" = %d [pr]", ret
);
1330 up_write(&nommu_region_sem
);
1332 fput(region
->vm_file
);
1333 kmem_cache_free(vm_region_jar
, region
);
1335 if (vma
->vm_flags
& VM_EXECUTABLE
)
1336 removed_exe_file_vma(vma
->vm_mm
);
1337 kmem_cache_free(vm_area_cachep
, vma
);
1338 kleave(" = %d", ret
);
1342 up_write(&nommu_region_sem
);
1343 printk(KERN_WARNING
"Attempt to share mismatched mappings\n");
1348 kmem_cache_free(vm_region_jar
, region
);
1349 printk(KERN_WARNING
"Allocation of vma for %lu byte allocation"
1350 " from process %d failed\n",
1355 error_getting_region
:
1356 printk(KERN_WARNING
"Allocation of vm region for %lu byte allocation"
1357 " from process %d failed\n",
1362 EXPORT_SYMBOL(do_mmap_pgoff
);
1365 * split a vma into two pieces at address 'addr', a new vma is allocated either
1366 * for the first part or the tail.
1368 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1369 unsigned long addr
, int new_below
)
1371 struct vm_area_struct
*new;
1372 struct vm_region
*region
;
1373 unsigned long npages
;
1377 /* we're only permitted to split anonymous regions that have a single
1380 atomic_read(&vma
->vm_region
->vm_usage
) != 1)
1383 if (mm
->map_count
>= sysctl_max_map_count
)
1386 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1390 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
1392 kmem_cache_free(vm_region_jar
, region
);
1396 /* most fields are the same, copy all, and then fixup */
1398 *region
= *vma
->vm_region
;
1399 new->vm_region
= region
;
1401 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1404 region
->vm_end
= new->vm_end
= addr
;
1406 region
->vm_start
= new->vm_start
= addr
;
1407 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1410 if (new->vm_ops
&& new->vm_ops
->open
)
1411 new->vm_ops
->open(new);
1413 delete_vma_from_mm(vma
);
1414 down_write(&nommu_region_sem
);
1415 delete_nommu_region(vma
->vm_region
);
1417 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1418 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1420 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1422 add_nommu_region(vma
->vm_region
);
1423 add_nommu_region(new->vm_region
);
1424 up_write(&nommu_region_sem
);
1425 add_vma_to_mm(mm
, vma
);
1426 add_vma_to_mm(mm
, new);
1431 * shrink a VMA by removing the specified chunk from either the beginning or
1434 static int shrink_vma(struct mm_struct
*mm
,
1435 struct vm_area_struct
*vma
,
1436 unsigned long from
, unsigned long to
)
1438 struct vm_region
*region
;
1442 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1444 delete_vma_from_mm(vma
);
1445 if (from
> vma
->vm_start
)
1449 add_vma_to_mm(mm
, vma
);
1451 /* cut the backing region down to size */
1452 region
= vma
->vm_region
;
1453 BUG_ON(atomic_read(®ion
->vm_usage
) != 1);
1455 down_write(&nommu_region_sem
);
1456 delete_nommu_region(region
);
1457 if (from
> region
->vm_start
)
1458 region
->vm_end
= from
;
1460 region
->vm_start
= to
;
1461 add_nommu_region(region
);
1462 up_write(&nommu_region_sem
);
1464 free_page_series(from
, to
);
1470 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1471 * VMA, though it need not cover the whole VMA
1473 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1475 struct vm_area_struct
*vma
;
1477 unsigned long end
= start
+ len
;
1480 kenter(",%lx,%zx", start
, len
);
1485 /* find the first potentially overlapping VMA */
1486 vma
= find_vma(mm
, start
);
1489 "munmap of memory not mmapped by process %d (%s):"
1491 current
->pid
, current
->comm
, start
, start
+ len
- 1);
1495 /* we're allowed to split an anonymous VMA but not a file-backed one */
1498 if (start
> vma
->vm_start
) {
1499 kleave(" = -EINVAL [miss]");
1502 if (end
== vma
->vm_end
)
1503 goto erase_whole_vma
;
1504 rb
= rb_next(&vma
->vm_rb
);
1505 vma
= rb_entry(rb
, struct vm_area_struct
, vm_rb
);
1507 kleave(" = -EINVAL [split file]");
1510 /* the chunk must be a subset of the VMA found */
1511 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1512 goto erase_whole_vma
;
1513 if (start
< vma
->vm_start
|| end
> vma
->vm_end
) {
1514 kleave(" = -EINVAL [superset]");
1517 if (start
& ~PAGE_MASK
) {
1518 kleave(" = -EINVAL [unaligned start]");
1521 if (end
!= vma
->vm_end
&& end
& ~PAGE_MASK
) {
1522 kleave(" = -EINVAL [unaligned split]");
1525 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1526 ret
= split_vma(mm
, vma
, start
, 1);
1528 kleave(" = %d [split]", ret
);
1532 return shrink_vma(mm
, vma
, start
, end
);
1536 delete_vma_from_mm(vma
);
1537 delete_vma(mm
, vma
);
1541 EXPORT_SYMBOL(do_munmap
);
1543 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
1546 struct mm_struct
*mm
= current
->mm
;
1548 down_write(&mm
->mmap_sem
);
1549 ret
= do_munmap(mm
, addr
, len
);
1550 up_write(&mm
->mmap_sem
);
1555 * release all the mappings made in a process's VM space
1557 void exit_mmap(struct mm_struct
*mm
)
1559 struct vm_area_struct
*vma
;
1568 while ((vma
= mm
->mmap
)) {
1569 mm
->mmap
= vma
->vm_next
;
1570 delete_vma_from_mm(vma
);
1571 delete_vma(mm
, vma
);
1577 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1583 * expand (or shrink) an existing mapping, potentially moving it at the same
1584 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1586 * under NOMMU conditions, we only permit changing a mapping's size, and only
1587 * as long as it stays within the region allocated by do_mmap_private() and the
1588 * block is not shareable
1590 * MREMAP_FIXED is not supported under NOMMU conditions
1592 unsigned long do_mremap(unsigned long addr
,
1593 unsigned long old_len
, unsigned long new_len
,
1594 unsigned long flags
, unsigned long new_addr
)
1596 struct vm_area_struct
*vma
;
1598 /* insanity checks first */
1599 if (old_len
== 0 || new_len
== 0)
1600 return (unsigned long) -EINVAL
;
1602 if (addr
& ~PAGE_MASK
)
1605 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1606 return (unsigned long) -EINVAL
;
1608 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1610 return (unsigned long) -EINVAL
;
1612 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1613 return (unsigned long) -EFAULT
;
1615 if (vma
->vm_flags
& VM_MAYSHARE
)
1616 return (unsigned long) -EPERM
;
1618 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1619 return (unsigned long) -ENOMEM
;
1621 /* all checks complete - do it */
1622 vma
->vm_end
= vma
->vm_start
+ new_len
;
1623 return vma
->vm_start
;
1625 EXPORT_SYMBOL(do_mremap
);
1628 unsigned long sys_mremap(unsigned long addr
,
1629 unsigned long old_len
, unsigned long new_len
,
1630 unsigned long flags
, unsigned long new_addr
)
1634 down_write(¤t
->mm
->mmap_sem
);
1635 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1636 up_write(¤t
->mm
->mmap_sem
);
1640 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1641 unsigned int foll_flags
)
1646 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long from
,
1647 unsigned long to
, unsigned long size
, pgprot_t prot
)
1649 vma
->vm_start
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1652 EXPORT_SYMBOL(remap_pfn_range
);
1654 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1655 unsigned long pgoff
)
1657 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1659 if (!(vma
->vm_flags
& VM_USERMAP
))
1662 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1663 vma
->vm_end
= vma
->vm_start
+ size
;
1667 EXPORT_SYMBOL(remap_vmalloc_range
);
1669 void swap_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
1673 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1674 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1679 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1683 void unmap_mapping_range(struct address_space
*mapping
,
1684 loff_t
const holebegin
, loff_t
const holelen
,
1688 EXPORT_SYMBOL(unmap_mapping_range
);
1691 * ask for an unmapped area at which to create a mapping on a file
1693 unsigned long get_unmapped_area(struct file
*file
, unsigned long addr
,
1694 unsigned long len
, unsigned long pgoff
,
1695 unsigned long flags
)
1697 unsigned long (*get_area
)(struct file
*, unsigned long, unsigned long,
1698 unsigned long, unsigned long);
1700 get_area
= current
->mm
->get_unmapped_area
;
1701 if (file
&& file
->f_op
&& file
->f_op
->get_unmapped_area
)
1702 get_area
= file
->f_op
->get_unmapped_area
;
1707 return get_area(file
, addr
, len
, pgoff
, flags
);
1709 EXPORT_SYMBOL(get_unmapped_area
);
1712 * Check that a process has enough memory to allocate a new virtual
1713 * mapping. 0 means there is enough memory for the allocation to
1714 * succeed and -ENOMEM implies there is not.
1716 * We currently support three overcommit policies, which are set via the
1717 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1719 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1720 * Additional code 2002 Jul 20 by Robert Love.
1722 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1724 * Note this is a helper function intended to be used by LSMs which
1725 * wish to use this logic.
1727 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1729 unsigned long free
, allowed
;
1731 vm_acct_memory(pages
);
1734 * Sometimes we want to use more memory than we have
1736 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1739 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1742 free
= global_page_state(NR_FILE_PAGES
);
1743 free
+= nr_swap_pages
;
1746 * Any slabs which are created with the
1747 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1748 * which are reclaimable, under pressure. The dentry
1749 * cache and most inode caches should fall into this
1751 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1754 * Leave the last 3% for root
1763 * nr_free_pages() is very expensive on large systems,
1764 * only call if we're about to fail.
1766 n
= nr_free_pages();
1769 * Leave reserved pages. The pages are not for anonymous pages.
1771 if (n
<= totalreserve_pages
)
1774 n
-= totalreserve_pages
;
1777 * Leave the last 3% for root
1789 allowed
= totalram_pages
* sysctl_overcommit_ratio
/ 100;
1791 * Leave the last 3% for root
1794 allowed
-= allowed
/ 32;
1795 allowed
+= total_swap_pages
;
1797 /* Don't let a single process grow too big:
1798 leave 3% of the size of this process for other processes */
1800 allowed
-= mm
->total_vm
/ 32;
1803 * cast `allowed' as a signed long because vm_committed_space
1804 * sometimes has a negative value
1806 if (atomic_long_read(&vm_committed_space
) < (long)allowed
)
1809 vm_unacct_memory(pages
);
1814 int in_gate_area_no_task(unsigned long addr
)
1819 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1824 EXPORT_SYMBOL(filemap_fault
);
1827 * Access another process' address space.
1828 * - source/target buffer must be kernel space
1830 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
1832 struct vm_area_struct
*vma
;
1833 struct mm_struct
*mm
;
1835 if (addr
+ len
< addr
)
1838 mm
= get_task_mm(tsk
);
1842 down_read(&mm
->mmap_sem
);
1844 /* the access must start within one of the target process's mappings */
1845 vma
= find_vma(mm
, addr
);
1847 /* don't overrun this mapping */
1848 if (addr
+ len
>= vma
->vm_end
)
1849 len
= vma
->vm_end
- addr
;
1851 /* only read or write mappings where it is permitted */
1852 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1853 len
-= copy_to_user((void *) addr
, buf
, len
);
1854 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1855 len
-= copy_from_user(buf
, (void *) addr
, len
);
1862 up_read(&mm
->mmap_sem
);