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-2009 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>
32 #include <linux/audit.h>
34 #include <asm/uaccess.h>
36 #include <asm/tlbflush.h>
37 #include <asm/mmu_context.h>
41 #define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43 #define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45 #define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
48 #define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50 #define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52 #define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
58 unsigned long max_mapnr
;
59 unsigned long num_physpages
;
60 unsigned long highest_memmap_pfn
;
61 struct percpu_counter vm_committed_as
;
62 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
63 int sysctl_overcommit_ratio
= 50; /* default is 50% */
64 int sysctl_max_map_count
= DEFAULT_MAX_MAP_COUNT
;
65 int sysctl_nr_trim_pages
= CONFIG_NOMMU_INITIAL_TRIM_EXCESS
;
66 int heap_stack_gap
= 0;
68 atomic_long_t mmap_pages_allocated
;
70 EXPORT_SYMBOL(mem_map
);
71 EXPORT_SYMBOL(num_physpages
);
73 /* list of mapped, potentially shareable regions */
74 static struct kmem_cache
*vm_region_jar
;
75 struct rb_root nommu_region_tree
= RB_ROOT
;
76 DECLARE_RWSEM(nommu_region_sem
);
78 const struct vm_operations_struct generic_file_vm_ops
= {
82 * Return the total memory allocated for this pointer, not
83 * just what the caller asked for.
85 * Doesn't have to be accurate, i.e. may have races.
87 unsigned int kobjsize(const void *objp
)
92 * If the object we have should not have ksize performed on it,
95 if (!objp
|| !virt_addr_valid(objp
))
98 page
= virt_to_head_page(objp
);
101 * If the allocator sets PageSlab, we know the pointer came from
108 * If it's not a compound page, see if we have a matching VMA
109 * region. This test is intentionally done in reverse order,
110 * so if there's no VMA, we still fall through and hand back
111 * PAGE_SIZE for 0-order pages.
113 if (!PageCompound(page
)) {
114 struct vm_area_struct
*vma
;
116 vma
= find_vma(current
->mm
, (unsigned long)objp
);
118 return vma
->vm_end
- vma
->vm_start
;
122 * The ksize() function is only guaranteed to work for pointers
123 * returned by kmalloc(). So handle arbitrary pointers here.
125 return PAGE_SIZE
<< compound_order(page
);
128 int __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
129 unsigned long start
, int nr_pages
, unsigned int foll_flags
,
130 struct page
**pages
, struct vm_area_struct
**vmas
)
132 struct vm_area_struct
*vma
;
133 unsigned long vm_flags
;
136 /* calculate required read or write permissions.
137 * If FOLL_FORCE is set, we only require the "MAY" flags.
139 vm_flags
= (foll_flags
& FOLL_WRITE
) ?
140 (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
141 vm_flags
&= (foll_flags
& FOLL_FORCE
) ?
142 (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
144 for (i
= 0; i
< nr_pages
; i
++) {
145 vma
= find_vma(mm
, start
);
147 goto finish_or_fault
;
149 /* protect what we can, including chardevs */
150 if ((vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)) ||
151 !(vm_flags
& vma
->vm_flags
))
152 goto finish_or_fault
;
155 pages
[i
] = virt_to_page(start
);
157 page_cache_get(pages
[i
]);
161 start
= (start
+ PAGE_SIZE
) & PAGE_MASK
;
167 return i
? : -EFAULT
;
171 * get a list of pages in an address range belonging to the specified process
172 * and indicate the VMA that covers each page
173 * - this is potentially dodgy as we may end incrementing the page count of a
174 * slab page or a secondary page from a compound page
175 * - don't permit access to VMAs that don't support it, such as I/O mappings
177 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
178 unsigned long start
, int nr_pages
, int write
, int force
,
179 struct page
**pages
, struct vm_area_struct
**vmas
)
188 return __get_user_pages(tsk
, mm
, start
, nr_pages
, flags
, pages
, vmas
);
190 EXPORT_SYMBOL(get_user_pages
);
193 * follow_pfn - look up PFN at a user virtual address
194 * @vma: memory mapping
195 * @address: user virtual address
196 * @pfn: location to store found PFN
198 * Only IO mappings and raw PFN mappings are allowed.
200 * Returns zero and the pfn at @pfn on success, -ve otherwise.
202 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
205 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
208 *pfn
= address
>> PAGE_SHIFT
;
211 EXPORT_SYMBOL(follow_pfn
);
213 DEFINE_RWLOCK(vmlist_lock
);
214 struct vm_struct
*vmlist
;
216 void vfree(const void *addr
)
220 EXPORT_SYMBOL(vfree
);
222 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
225 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
226 * returns only a logical address.
228 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
230 EXPORT_SYMBOL(__vmalloc
);
232 void *vmalloc_user(unsigned long size
)
236 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
239 struct vm_area_struct
*vma
;
241 down_write(¤t
->mm
->mmap_sem
);
242 vma
= find_vma(current
->mm
, (unsigned long)ret
);
244 vma
->vm_flags
|= VM_USERMAP
;
245 up_write(¤t
->mm
->mmap_sem
);
250 EXPORT_SYMBOL(vmalloc_user
);
252 struct page
*vmalloc_to_page(const void *addr
)
254 return virt_to_page(addr
);
256 EXPORT_SYMBOL(vmalloc_to_page
);
258 unsigned long vmalloc_to_pfn(const void *addr
)
260 return page_to_pfn(virt_to_page(addr
));
262 EXPORT_SYMBOL(vmalloc_to_pfn
);
264 long vread(char *buf
, char *addr
, unsigned long count
)
266 memcpy(buf
, addr
, count
);
270 long vwrite(char *buf
, char *addr
, unsigned long count
)
272 /* Don't allow overflow */
273 if ((unsigned long) addr
+ count
< count
)
274 count
= -(unsigned long) addr
;
276 memcpy(addr
, buf
, count
);
281 * vmalloc - allocate virtually continguos memory
283 * @size: allocation size
285 * Allocate enough pages to cover @size from the page level
286 * allocator and map them into continguos kernel virtual space.
288 * For tight control over page level allocator and protection flags
289 * use __vmalloc() instead.
291 void *vmalloc(unsigned long size
)
293 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
295 EXPORT_SYMBOL(vmalloc
);
298 * vzalloc - allocate virtually continguos memory with zero fill
300 * @size: allocation size
302 * Allocate enough pages to cover @size from the page level
303 * allocator and map them into continguos kernel virtual space.
304 * The memory allocated is set to zero.
306 * For tight control over page level allocator and protection flags
307 * use __vmalloc() instead.
309 void *vzalloc(unsigned long size
)
311 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
314 EXPORT_SYMBOL(vzalloc
);
317 * vmalloc_node - allocate memory on a specific node
318 * @size: allocation size
321 * Allocate enough pages to cover @size from the page level
322 * allocator and map them into contiguous kernel virtual space.
324 * For tight control over page level allocator and protection flags
325 * use __vmalloc() instead.
327 void *vmalloc_node(unsigned long size
, int node
)
329 return vmalloc(size
);
331 EXPORT_SYMBOL(vmalloc_node
);
334 * vzalloc_node - allocate memory on a specific node with zero fill
335 * @size: allocation size
338 * Allocate enough pages to cover @size from the page level
339 * allocator and map them into contiguous kernel virtual space.
340 * The memory allocated is set to zero.
342 * For tight control over page level allocator and protection flags
343 * use __vmalloc() instead.
345 void *vzalloc_node(unsigned long size
, int node
)
347 return vzalloc(size
);
349 EXPORT_SYMBOL(vzalloc_node
);
351 #ifndef PAGE_KERNEL_EXEC
352 # define PAGE_KERNEL_EXEC PAGE_KERNEL
356 * vmalloc_exec - allocate virtually contiguous, executable memory
357 * @size: allocation size
359 * Kernel-internal function to allocate enough pages to cover @size
360 * the page level allocator and map them into contiguous and
361 * executable kernel virtual space.
363 * For tight control over page level allocator and protection flags
364 * use __vmalloc() instead.
367 void *vmalloc_exec(unsigned long size
)
369 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
373 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
374 * @size: allocation size
376 * Allocate enough 32bit PA addressable pages to cover @size from the
377 * page level allocator and map them into continguos kernel virtual space.
379 void *vmalloc_32(unsigned long size
)
381 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
383 EXPORT_SYMBOL(vmalloc_32
);
386 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
387 * @size: allocation size
389 * The resulting memory area is 32bit addressable and zeroed so it can be
390 * mapped to userspace without leaking data.
392 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
393 * remap_vmalloc_range() are permissible.
395 void *vmalloc_32_user(unsigned long size
)
398 * We'll have to sort out the ZONE_DMA bits for 64-bit,
399 * but for now this can simply use vmalloc_user() directly.
401 return vmalloc_user(size
);
403 EXPORT_SYMBOL(vmalloc_32_user
);
405 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
412 void vunmap(const void *addr
)
416 EXPORT_SYMBOL(vunmap
);
418 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
, pgprot_t prot
)
423 EXPORT_SYMBOL(vm_map_ram
);
425 void vm_unmap_ram(const void *mem
, unsigned int count
)
429 EXPORT_SYMBOL(vm_unmap_ram
);
431 void vm_unmap_aliases(void)
434 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
437 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
440 void __attribute__((weak
)) vmalloc_sync_all(void)
444 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
449 EXPORT_SYMBOL(vm_insert_page
);
452 * sys_brk() for the most part doesn't need the global kernel
453 * lock, except when an application is doing something nasty
454 * like trying to un-brk an area that has already been mapped
455 * to a regular file. in this case, the unmapping will need
456 * to invoke file system routines that need the global lock.
458 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
460 struct mm_struct
*mm
= current
->mm
;
462 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
469 * Always allow shrinking brk
471 if (brk
<= mm
->brk
) {
477 * Ok, looks good - let it rip.
479 flush_icache_range(mm
->brk
, brk
);
480 return mm
->brk
= brk
;
484 * initialise the VMA and region record slabs
486 void __init
mmap_init(void)
490 ret
= percpu_counter_init(&vm_committed_as
, 0);
492 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
);
496 * validate the region tree
497 * - the caller must hold the region lock
499 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
500 static noinline
void validate_nommu_regions(void)
502 struct vm_region
*region
, *last
;
503 struct rb_node
*p
, *lastp
;
505 lastp
= rb_first(&nommu_region_tree
);
509 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
510 BUG_ON(unlikely(last
->vm_end
<= last
->vm_start
));
511 BUG_ON(unlikely(last
->vm_top
< last
->vm_end
));
513 while ((p
= rb_next(lastp
))) {
514 region
= rb_entry(p
, struct vm_region
, vm_rb
);
515 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
517 BUG_ON(unlikely(region
->vm_end
<= region
->vm_start
));
518 BUG_ON(unlikely(region
->vm_top
< region
->vm_end
));
519 BUG_ON(unlikely(region
->vm_start
< last
->vm_top
));
525 static void validate_nommu_regions(void)
531 * add a region into the global tree
533 static void add_nommu_region(struct vm_region
*region
)
535 struct vm_region
*pregion
;
536 struct rb_node
**p
, *parent
;
538 validate_nommu_regions();
541 p
= &nommu_region_tree
.rb_node
;
544 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
545 if (region
->vm_start
< pregion
->vm_start
)
547 else if (region
->vm_start
> pregion
->vm_start
)
549 else if (pregion
== region
)
555 rb_link_node(®ion
->vm_rb
, parent
, p
);
556 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
558 validate_nommu_regions();
562 * delete a region from the global tree
564 static void delete_nommu_region(struct vm_region
*region
)
566 BUG_ON(!nommu_region_tree
.rb_node
);
568 validate_nommu_regions();
569 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
570 validate_nommu_regions();
574 * free a contiguous series of pages
576 static void free_page_series(unsigned long from
, unsigned long to
)
578 for (; from
< to
; from
+= PAGE_SIZE
) {
579 struct page
*page
= virt_to_page(from
);
581 kdebug("- free %lx", from
);
582 atomic_long_dec(&mmap_pages_allocated
);
583 if (page_count(page
) != 1)
584 kdebug("free page %p: refcount not one: %d",
585 page
, page_count(page
));
591 * release a reference to a region
592 * - the caller must hold the region semaphore for writing, which this releases
593 * - the region may not have been added to the tree yet, in which case vm_top
594 * will equal vm_start
596 static void __put_nommu_region(struct vm_region
*region
)
597 __releases(nommu_region_sem
)
599 kenter("%p{%d}", region
, region
->vm_usage
);
601 BUG_ON(!nommu_region_tree
.rb_node
);
603 if (--region
->vm_usage
== 0) {
604 if (region
->vm_top
> region
->vm_start
)
605 delete_nommu_region(region
);
606 up_write(&nommu_region_sem
);
609 fput(region
->vm_file
);
611 /* IO memory and memory shared directly out of the pagecache
612 * from ramfs/tmpfs mustn't be released here */
613 if (region
->vm_flags
& VM_MAPPED_COPY
) {
614 kdebug("free series");
615 free_page_series(region
->vm_start
, region
->vm_top
);
617 kmem_cache_free(vm_region_jar
, region
);
619 up_write(&nommu_region_sem
);
624 * release a reference to a region
626 static void put_nommu_region(struct vm_region
*region
)
628 down_write(&nommu_region_sem
);
629 __put_nommu_region(region
);
633 * update protection on a vma
635 static void protect_vma(struct vm_area_struct
*vma
, unsigned long flags
)
638 struct mm_struct
*mm
= vma
->vm_mm
;
639 long start
= vma
->vm_start
& PAGE_MASK
;
640 while (start
< vma
->vm_end
) {
641 protect_page(mm
, start
, flags
);
644 update_protections(mm
);
649 * add a VMA into a process's mm_struct in the appropriate place in the list
650 * and tree and add to the address space's page tree also if not an anonymous
652 * - should be called with mm->mmap_sem held writelocked
654 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
656 struct vm_area_struct
*pvma
, **pp
, *next
;
657 struct address_space
*mapping
;
658 struct rb_node
**p
, *parent
;
662 BUG_ON(!vma
->vm_region
);
667 protect_vma(vma
, vma
->vm_flags
);
669 /* add the VMA to the mapping */
671 mapping
= vma
->vm_file
->f_mapping
;
673 flush_dcache_mmap_lock(mapping
);
674 vma_prio_tree_insert(vma
, &mapping
->i_mmap
);
675 flush_dcache_mmap_unlock(mapping
);
678 /* add the VMA to the tree */
680 p
= &mm
->mm_rb
.rb_node
;
683 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
685 /* sort by: start addr, end addr, VMA struct addr in that order
686 * (the latter is necessary as we may get identical VMAs) */
687 if (vma
->vm_start
< pvma
->vm_start
)
689 else if (vma
->vm_start
> pvma
->vm_start
)
691 else if (vma
->vm_end
< pvma
->vm_end
)
693 else if (vma
->vm_end
> pvma
->vm_end
)
703 rb_link_node(&vma
->vm_rb
, parent
, p
);
704 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
706 /* add VMA to the VMA list also */
707 for (pp
= &mm
->mmap
; (pvma
= *pp
); pp
= &(*pp
)->vm_next
) {
708 if (pvma
->vm_start
> vma
->vm_start
)
710 if (pvma
->vm_start
< vma
->vm_start
)
712 if (pvma
->vm_end
< vma
->vm_end
)
724 * delete a VMA from its owning mm_struct and address space
726 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
728 struct vm_area_struct
**pp
;
729 struct address_space
*mapping
;
730 struct mm_struct
*mm
= vma
->vm_mm
;
737 if (mm
->mmap_cache
== vma
)
738 mm
->mmap_cache
= NULL
;
740 /* remove the VMA from the mapping */
742 mapping
= vma
->vm_file
->f_mapping
;
744 flush_dcache_mmap_lock(mapping
);
745 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
746 flush_dcache_mmap_unlock(mapping
);
749 /* remove from the MM's tree and list */
750 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
751 for (pp
= &mm
->mmap
; *pp
; pp
= &(*pp
)->vm_next
) {
762 * destroy a VMA record
764 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
767 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
768 vma
->vm_ops
->close(vma
);
771 if (vma
->vm_flags
& VM_EXECUTABLE
)
772 removed_exe_file_vma(mm
);
774 put_nommu_region(vma
->vm_region
);
775 kmem_cache_free(vm_area_cachep
, vma
);
779 * look up the first VMA in which addr resides, NULL if none
780 * - should be called with mm->mmap_sem at least held readlocked
782 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
784 struct vm_area_struct
*vma
;
785 struct rb_node
*n
= mm
->mm_rb
.rb_node
;
787 /* check the cache first */
788 vma
= mm
->mmap_cache
;
789 if (vma
&& vma
->vm_start
<= addr
&& vma
->vm_end
> addr
)
792 /* trawl the tree (there may be multiple mappings in which addr
794 for (n
= rb_first(&mm
->mm_rb
); n
; n
= rb_next(n
)) {
795 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
796 if (vma
->vm_start
> addr
)
798 if (vma
->vm_end
> addr
) {
799 mm
->mmap_cache
= vma
;
806 EXPORT_SYMBOL(find_vma
);
810 * - we don't extend stack VMAs under NOMMU conditions
812 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
814 return find_vma(mm
, addr
);
818 * expand a stack to a given address
819 * - not supported under NOMMU conditions
821 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
827 * look up the first VMA exactly that exactly matches addr
828 * - should be called with mm->mmap_sem at least held readlocked
830 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
834 struct vm_area_struct
*vma
;
835 struct rb_node
*n
= mm
->mm_rb
.rb_node
;
836 unsigned long end
= addr
+ len
;
838 /* check the cache first */
839 vma
= mm
->mmap_cache
;
840 if (vma
&& vma
->vm_start
== addr
&& vma
->vm_end
== end
)
843 /* trawl the tree (there may be multiple mappings in which addr
845 for (n
= rb_first(&mm
->mm_rb
); n
; n
= rb_next(n
)) {
846 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
847 if (vma
->vm_start
< addr
)
849 if (vma
->vm_start
> addr
)
851 if (vma
->vm_end
== end
) {
852 mm
->mmap_cache
= vma
;
861 * determine whether a mapping should be permitted and, if so, what sort of
862 * mapping we're capable of supporting
864 static int validate_mmap_request(struct file
*file
,
870 unsigned long *_capabilities
)
872 unsigned long capabilities
, rlen
;
873 unsigned long reqprot
= prot
;
876 /* do the simple checks first */
877 if (flags
& MAP_FIXED
) {
879 "%d: Can't do fixed-address/overlay mmap of RAM\n",
884 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
885 (flags
& MAP_TYPE
) != MAP_SHARED
)
891 /* Careful about overflows.. */
892 rlen
= PAGE_ALIGN(len
);
893 if (!rlen
|| rlen
> TASK_SIZE
)
896 /* offset overflow? */
897 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
901 /* validate file mapping requests */
902 struct address_space
*mapping
;
904 /* files must support mmap */
905 if (!file
->f_op
|| !file
->f_op
->mmap
)
908 /* work out if what we've got could possibly be shared
909 * - we support chardevs that provide their own "memory"
910 * - we support files/blockdevs that are memory backed
912 mapping
= file
->f_mapping
;
914 mapping
= file
->f_path
.dentry
->d_inode
->i_mapping
;
917 if (mapping
&& mapping
->backing_dev_info
)
918 capabilities
= mapping
->backing_dev_info
->capabilities
;
921 /* no explicit capabilities set, so assume some
923 switch (file
->f_path
.dentry
->d_inode
->i_mode
& S_IFMT
) {
926 capabilities
= BDI_CAP_MAP_COPY
;
941 /* eliminate any capabilities that we can't support on this
943 if (!file
->f_op
->get_unmapped_area
)
944 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
945 if (!file
->f_op
->read
)
946 capabilities
&= ~BDI_CAP_MAP_COPY
;
948 /* The file shall have been opened with read permission. */
949 if (!(file
->f_mode
& FMODE_READ
))
952 if (flags
& MAP_SHARED
) {
953 /* do checks for writing, appending and locking */
954 if ((prot
& PROT_WRITE
) &&
955 !(file
->f_mode
& FMODE_WRITE
))
958 if (IS_APPEND(file
->f_path
.dentry
->d_inode
) &&
959 (file
->f_mode
& FMODE_WRITE
))
962 if (locks_verify_locked(file
->f_path
.dentry
->d_inode
))
965 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
968 /* we mustn't privatise shared mappings */
969 capabilities
&= ~BDI_CAP_MAP_COPY
;
972 /* we're going to read the file into private memory we
974 if (!(capabilities
& BDI_CAP_MAP_COPY
))
977 /* we don't permit a private writable mapping to be
978 * shared with the backing device */
979 if (prot
& PROT_WRITE
)
980 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
983 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
984 if (((prot
& PROT_READ
) && !(capabilities
& BDI_CAP_READ_MAP
)) ||
985 ((prot
& PROT_WRITE
) && !(capabilities
& BDI_CAP_WRITE_MAP
)) ||
986 ((prot
& PROT_EXEC
) && !(capabilities
& BDI_CAP_EXEC_MAP
))
988 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
989 if (flags
& MAP_SHARED
) {
991 "MAP_SHARED not completely supported on !MMU\n");
997 /* handle executable mappings and implied executable
999 if (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
) {
1000 if (prot
& PROT_EXEC
)
1003 else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
1004 /* handle implication of PROT_EXEC by PROT_READ */
1005 if (current
->personality
& READ_IMPLIES_EXEC
) {
1006 if (capabilities
& BDI_CAP_EXEC_MAP
)
1010 else if ((prot
& PROT_READ
) &&
1011 (prot
& PROT_EXEC
) &&
1012 !(capabilities
& BDI_CAP_EXEC_MAP
)
1014 /* backing file is not executable, try to copy */
1015 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1019 /* anonymous mappings are always memory backed and can be
1022 capabilities
= BDI_CAP_MAP_COPY
;
1024 /* handle PROT_EXEC implication by PROT_READ */
1025 if ((prot
& PROT_READ
) &&
1026 (current
->personality
& READ_IMPLIES_EXEC
))
1030 /* allow the security API to have its say */
1031 ret
= security_file_mmap(file
, reqprot
, prot
, flags
, addr
, 0);
1036 *_capabilities
= capabilities
;
1041 * we've determined that we can make the mapping, now translate what we
1042 * now know into VMA flags
1044 static unsigned long determine_vm_flags(struct file
*file
,
1046 unsigned long flags
,
1047 unsigned long capabilities
)
1049 unsigned long vm_flags
;
1051 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
1052 /* vm_flags |= mm->def_flags; */
1054 if (!(capabilities
& BDI_CAP_MAP_DIRECT
)) {
1055 /* attempt to share read-only copies of mapped file chunks */
1056 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1057 if (file
&& !(prot
& PROT_WRITE
))
1058 vm_flags
|= VM_MAYSHARE
;
1060 /* overlay a shareable mapping on the backing device or inode
1061 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1063 vm_flags
|= VM_MAYSHARE
| (capabilities
& BDI_CAP_VMFLAGS
);
1064 if (flags
& MAP_SHARED
)
1065 vm_flags
|= VM_SHARED
;
1068 /* refuse to let anyone share private mappings with this process if
1069 * it's being traced - otherwise breakpoints set in it may interfere
1070 * with another untraced process
1072 if ((flags
& MAP_PRIVATE
) && tracehook_expect_breakpoints(current
))
1073 vm_flags
&= ~VM_MAYSHARE
;
1079 * set up a shared mapping on a file (the driver or filesystem provides and
1082 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1086 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1088 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1094 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1095 * opposed to tried but failed) so we can only give a suitable error as
1096 * it's not possible to make a private copy if MAP_SHARED was given */
1101 * set up a private mapping or an anonymous shared mapping
1103 static int do_mmap_private(struct vm_area_struct
*vma
,
1104 struct vm_region
*region
,
1106 unsigned long capabilities
)
1109 unsigned long total
, point
, n
, rlen
;
1113 /* invoke the file's mapping function so that it can keep track of
1114 * shared mappings on devices or memory
1115 * - VM_MAYSHARE will be set if it may attempt to share
1117 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1118 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1120 /* shouldn't return success if we're not sharing */
1121 BUG_ON(!(vma
->vm_flags
& VM_MAYSHARE
));
1122 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1128 /* getting an ENOSYS error indicates that direct mmap isn't
1129 * possible (as opposed to tried but failed) so we'll try to
1130 * make a private copy of the data and map that instead */
1133 rlen
= PAGE_ALIGN(len
);
1135 /* allocate some memory to hold the mapping
1136 * - note that this may not return a page-aligned address if the object
1137 * we're allocating is smaller than a page
1139 order
= get_order(rlen
);
1140 kdebug("alloc order %d for %lx", order
, len
);
1142 pages
= alloc_pages(GFP_KERNEL
, order
);
1147 atomic_long_add(total
, &mmap_pages_allocated
);
1149 point
= rlen
>> PAGE_SHIFT
;
1151 /* we allocated a power-of-2 sized page set, so we may want to trim off
1153 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
) {
1154 while (total
> point
) {
1155 order
= ilog2(total
- point
);
1157 kdebug("shave %lu/%lu @%lu", n
, total
- point
, total
);
1158 atomic_long_sub(n
, &mmap_pages_allocated
);
1160 set_page_refcounted(pages
+ total
);
1161 __free_pages(pages
+ total
, order
);
1165 for (point
= 1; point
< total
; point
++)
1166 set_page_refcounted(&pages
[point
]);
1168 base
= page_address(pages
);
1169 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1170 region
->vm_start
= (unsigned long) base
;
1171 region
->vm_end
= region
->vm_start
+ rlen
;
1172 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1174 vma
->vm_start
= region
->vm_start
;
1175 vma
->vm_end
= region
->vm_start
+ len
;
1178 /* read the contents of a file into the copy */
1179 mm_segment_t old_fs
;
1182 fpos
= vma
->vm_pgoff
;
1183 fpos
<<= PAGE_SHIFT
;
1187 ret
= vma
->vm_file
->f_op
->read(vma
->vm_file
, base
, rlen
, &fpos
);
1193 /* clear the last little bit */
1195 memset(base
+ ret
, 0, rlen
- ret
);
1202 free_page_series(region
->vm_start
, region
->vm_end
);
1203 region
->vm_start
= vma
->vm_start
= 0;
1204 region
->vm_end
= vma
->vm_end
= 0;
1209 printk("Allocation of length %lu from process %d (%s) failed\n",
1210 len
, current
->pid
, current
->comm
);
1216 * handle mapping creation for uClinux
1218 unsigned long do_mmap_pgoff(struct file
*file
,
1222 unsigned long flags
,
1223 unsigned long pgoff
)
1225 struct vm_area_struct
*vma
;
1226 struct vm_region
*region
;
1228 unsigned long capabilities
, vm_flags
, result
;
1231 kenter(",%lx,%lx,%lx,%lx,%lx", addr
, len
, prot
, flags
, pgoff
);
1233 /* decide whether we should attempt the mapping, and if so what sort of
1235 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1238 kleave(" = %d [val]", ret
);
1242 /* we ignore the address hint */
1245 /* we've determined that we can make the mapping, now translate what we
1246 * now know into VMA flags */
1247 vm_flags
= determine_vm_flags(file
, prot
, flags
, capabilities
);
1249 /* we're going to need to record the mapping */
1250 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1252 goto error_getting_region
;
1254 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1256 goto error_getting_vma
;
1258 region
->vm_usage
= 1;
1259 region
->vm_flags
= vm_flags
;
1260 region
->vm_pgoff
= pgoff
;
1262 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1263 vma
->vm_flags
= vm_flags
;
1264 vma
->vm_pgoff
= pgoff
;
1267 region
->vm_file
= file
;
1269 vma
->vm_file
= file
;
1271 if (vm_flags
& VM_EXECUTABLE
) {
1272 added_exe_file_vma(current
->mm
);
1273 vma
->vm_mm
= current
->mm
;
1277 down_write(&nommu_region_sem
);
1279 /* if we want to share, we need to check for regions created by other
1280 * mmap() calls that overlap with our proposed mapping
1281 * - we can only share with a superset match on most regular files
1282 * - shared mappings on character devices and memory backed files are
1283 * permitted to overlap inexactly as far as we are concerned for in
1284 * these cases, sharing is handled in the driver or filesystem rather
1287 if (vm_flags
& VM_MAYSHARE
) {
1288 struct vm_region
*pregion
;
1289 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1291 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1292 pgend
= pgoff
+ pglen
;
1294 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1295 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1297 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1300 /* search for overlapping mappings on the same file */
1301 if (pregion
->vm_file
->f_path
.dentry
->d_inode
!=
1302 file
->f_path
.dentry
->d_inode
)
1305 if (pregion
->vm_pgoff
>= pgend
)
1308 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1309 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1310 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1311 if (pgoff
>= rpgend
)
1314 /* handle inexactly overlapping matches between
1316 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1317 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1318 /* new mapping is not a subset of the region */
1319 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
1320 goto sharing_violation
;
1324 /* we've found a region we can share */
1325 pregion
->vm_usage
++;
1326 vma
->vm_region
= pregion
;
1327 start
= pregion
->vm_start
;
1328 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1329 vma
->vm_start
= start
;
1330 vma
->vm_end
= start
+ len
;
1332 if (pregion
->vm_flags
& VM_MAPPED_COPY
) {
1333 kdebug("share copy");
1334 vma
->vm_flags
|= VM_MAPPED_COPY
;
1336 kdebug("share mmap");
1337 ret
= do_mmap_shared_file(vma
);
1339 vma
->vm_region
= NULL
;
1342 pregion
->vm_usage
--;
1344 goto error_just_free
;
1347 fput(region
->vm_file
);
1348 kmem_cache_free(vm_region_jar
, region
);
1354 /* obtain the address at which to make a shared mapping
1355 * - this is the hook for quasi-memory character devices to
1356 * tell us the location of a shared mapping
1358 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1359 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1361 if (IS_ERR((void *) addr
)) {
1363 if (ret
!= (unsigned long) -ENOSYS
)
1364 goto error_just_free
;
1366 /* the driver refused to tell us where to site
1367 * the mapping so we'll have to attempt to copy
1369 ret
= (unsigned long) -ENODEV
;
1370 if (!(capabilities
& BDI_CAP_MAP_COPY
))
1371 goto error_just_free
;
1373 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1375 vma
->vm_start
= region
->vm_start
= addr
;
1376 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1381 vma
->vm_region
= region
;
1383 /* set up the mapping
1384 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1386 if (file
&& vma
->vm_flags
& VM_SHARED
)
1387 ret
= do_mmap_shared_file(vma
);
1389 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1391 goto error_just_free
;
1392 add_nommu_region(region
);
1394 /* clear anonymous mappings that don't ask for uninitialized data */
1395 if (!vma
->vm_file
&& !(flags
& MAP_UNINITIALIZED
))
1396 memset((void *)region
->vm_start
, 0,
1397 region
->vm_end
- region
->vm_start
);
1399 /* okay... we have a mapping; now we have to register it */
1400 result
= vma
->vm_start
;
1402 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1405 add_vma_to_mm(current
->mm
, vma
);
1407 /* we flush the region from the icache only when the first executable
1408 * mapping of it is made */
1409 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1410 flush_icache_range(region
->vm_start
, region
->vm_end
);
1411 region
->vm_icache_flushed
= true;
1414 up_write(&nommu_region_sem
);
1416 kleave(" = %lx", result
);
1420 up_write(&nommu_region_sem
);
1422 if (region
->vm_file
)
1423 fput(region
->vm_file
);
1424 kmem_cache_free(vm_region_jar
, region
);
1427 if (vma
->vm_flags
& VM_EXECUTABLE
)
1428 removed_exe_file_vma(vma
->vm_mm
);
1429 kmem_cache_free(vm_area_cachep
, vma
);
1430 kleave(" = %d", ret
);
1434 up_write(&nommu_region_sem
);
1435 printk(KERN_WARNING
"Attempt to share mismatched mappings\n");
1440 kmem_cache_free(vm_region_jar
, region
);
1441 printk(KERN_WARNING
"Allocation of vma for %lu byte allocation"
1442 " from process %d failed\n",
1447 error_getting_region
:
1448 printk(KERN_WARNING
"Allocation of vm region for %lu byte allocation"
1449 " from process %d failed\n",
1454 EXPORT_SYMBOL(do_mmap_pgoff
);
1456 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1457 unsigned long, prot
, unsigned long, flags
,
1458 unsigned long, fd
, unsigned long, pgoff
)
1460 struct file
*file
= NULL
;
1461 unsigned long retval
= -EBADF
;
1463 audit_mmap_fd(fd
, flags
);
1464 if (!(flags
& MAP_ANONYMOUS
)) {
1470 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1472 down_write(¤t
->mm
->mmap_sem
);
1473 retval
= do_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1474 up_write(¤t
->mm
->mmap_sem
);
1482 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1483 struct mmap_arg_struct
{
1487 unsigned long flags
;
1489 unsigned long offset
;
1492 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1494 struct mmap_arg_struct a
;
1496 if (copy_from_user(&a
, arg
, sizeof(a
)))
1498 if (a
.offset
& ~PAGE_MASK
)
1501 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1502 a
.offset
>> PAGE_SHIFT
);
1504 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1507 * split a vma into two pieces at address 'addr', a new vma is allocated either
1508 * for the first part or the tail.
1510 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1511 unsigned long addr
, int new_below
)
1513 struct vm_area_struct
*new;
1514 struct vm_region
*region
;
1515 unsigned long npages
;
1519 /* we're only permitted to split anonymous regions (these should have
1520 * only a single usage on the region) */
1524 if (mm
->map_count
>= sysctl_max_map_count
)
1527 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1531 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
1533 kmem_cache_free(vm_region_jar
, region
);
1537 /* most fields are the same, copy all, and then fixup */
1539 *region
= *vma
->vm_region
;
1540 new->vm_region
= region
;
1542 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1545 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1547 region
->vm_start
= new->vm_start
= addr
;
1548 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1551 if (new->vm_ops
&& new->vm_ops
->open
)
1552 new->vm_ops
->open(new);
1554 delete_vma_from_mm(vma
);
1555 down_write(&nommu_region_sem
);
1556 delete_nommu_region(vma
->vm_region
);
1558 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1559 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1561 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1562 vma
->vm_region
->vm_top
= addr
;
1564 add_nommu_region(vma
->vm_region
);
1565 add_nommu_region(new->vm_region
);
1566 up_write(&nommu_region_sem
);
1567 add_vma_to_mm(mm
, vma
);
1568 add_vma_to_mm(mm
, new);
1573 * shrink a VMA by removing the specified chunk from either the beginning or
1576 static int shrink_vma(struct mm_struct
*mm
,
1577 struct vm_area_struct
*vma
,
1578 unsigned long from
, unsigned long to
)
1580 struct vm_region
*region
;
1584 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1586 delete_vma_from_mm(vma
);
1587 if (from
> vma
->vm_start
)
1591 add_vma_to_mm(mm
, vma
);
1593 /* cut the backing region down to size */
1594 region
= vma
->vm_region
;
1595 BUG_ON(region
->vm_usage
!= 1);
1597 down_write(&nommu_region_sem
);
1598 delete_nommu_region(region
);
1599 if (from
> region
->vm_start
) {
1600 to
= region
->vm_top
;
1601 region
->vm_top
= region
->vm_end
= from
;
1603 region
->vm_start
= to
;
1605 add_nommu_region(region
);
1606 up_write(&nommu_region_sem
);
1608 free_page_series(from
, to
);
1614 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1615 * VMA, though it need not cover the whole VMA
1617 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1619 struct vm_area_struct
*vma
;
1621 unsigned long end
= start
+ len
;
1624 kenter(",%lx,%zx", start
, len
);
1629 /* find the first potentially overlapping VMA */
1630 vma
= find_vma(mm
, start
);
1632 static int limit
= 0;
1635 "munmap of memory not mmapped by process %d"
1636 " (%s): 0x%lx-0x%lx\n",
1637 current
->pid
, current
->comm
,
1638 start
, start
+ len
- 1);
1644 /* we're allowed to split an anonymous VMA but not a file-backed one */
1647 if (start
> vma
->vm_start
) {
1648 kleave(" = -EINVAL [miss]");
1651 if (end
== vma
->vm_end
)
1652 goto erase_whole_vma
;
1653 rb
= rb_next(&vma
->vm_rb
);
1654 vma
= rb_entry(rb
, struct vm_area_struct
, vm_rb
);
1656 kleave(" = -EINVAL [split file]");
1659 /* the chunk must be a subset of the VMA found */
1660 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1661 goto erase_whole_vma
;
1662 if (start
< vma
->vm_start
|| end
> vma
->vm_end
) {
1663 kleave(" = -EINVAL [superset]");
1666 if (start
& ~PAGE_MASK
) {
1667 kleave(" = -EINVAL [unaligned start]");
1670 if (end
!= vma
->vm_end
&& end
& ~PAGE_MASK
) {
1671 kleave(" = -EINVAL [unaligned split]");
1674 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1675 ret
= split_vma(mm
, vma
, start
, 1);
1677 kleave(" = %d [split]", ret
);
1681 return shrink_vma(mm
, vma
, start
, end
);
1685 delete_vma_from_mm(vma
);
1686 delete_vma(mm
, vma
);
1690 EXPORT_SYMBOL(do_munmap
);
1692 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1695 struct mm_struct
*mm
= current
->mm
;
1697 down_write(&mm
->mmap_sem
);
1698 ret
= do_munmap(mm
, addr
, len
);
1699 up_write(&mm
->mmap_sem
);
1704 * release all the mappings made in a process's VM space
1706 void exit_mmap(struct mm_struct
*mm
)
1708 struct vm_area_struct
*vma
;
1717 while ((vma
= mm
->mmap
)) {
1718 mm
->mmap
= vma
->vm_next
;
1719 delete_vma_from_mm(vma
);
1720 delete_vma(mm
, vma
);
1727 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1733 * expand (or shrink) an existing mapping, potentially moving it at the same
1734 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1736 * under NOMMU conditions, we only permit changing a mapping's size, and only
1737 * as long as it stays within the region allocated by do_mmap_private() and the
1738 * block is not shareable
1740 * MREMAP_FIXED is not supported under NOMMU conditions
1742 unsigned long do_mremap(unsigned long addr
,
1743 unsigned long old_len
, unsigned long new_len
,
1744 unsigned long flags
, unsigned long new_addr
)
1746 struct vm_area_struct
*vma
;
1748 /* insanity checks first */
1749 if (old_len
== 0 || new_len
== 0)
1750 return (unsigned long) -EINVAL
;
1752 if (addr
& ~PAGE_MASK
)
1755 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1756 return (unsigned long) -EINVAL
;
1758 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1760 return (unsigned long) -EINVAL
;
1762 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1763 return (unsigned long) -EFAULT
;
1765 if (vma
->vm_flags
& VM_MAYSHARE
)
1766 return (unsigned long) -EPERM
;
1768 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1769 return (unsigned long) -ENOMEM
;
1771 /* all checks complete - do it */
1772 vma
->vm_end
= vma
->vm_start
+ new_len
;
1773 return vma
->vm_start
;
1775 EXPORT_SYMBOL(do_mremap
);
1777 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1778 unsigned long, new_len
, unsigned long, flags
,
1779 unsigned long, new_addr
)
1783 down_write(¤t
->mm
->mmap_sem
);
1784 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1785 up_write(¤t
->mm
->mmap_sem
);
1789 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1790 unsigned int foll_flags
)
1795 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long from
,
1796 unsigned long to
, unsigned long size
, pgprot_t prot
)
1798 vma
->vm_start
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1801 EXPORT_SYMBOL(remap_pfn_range
);
1803 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1804 unsigned long pgoff
)
1806 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1808 if (!(vma
->vm_flags
& VM_USERMAP
))
1811 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1812 vma
->vm_end
= vma
->vm_start
+ size
;
1816 EXPORT_SYMBOL(remap_vmalloc_range
);
1818 void swap_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
1822 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1823 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1828 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1832 void unmap_mapping_range(struct address_space
*mapping
,
1833 loff_t
const holebegin
, loff_t
const holelen
,
1837 EXPORT_SYMBOL(unmap_mapping_range
);
1840 * Check that a process has enough memory to allocate a new virtual
1841 * mapping. 0 means there is enough memory for the allocation to
1842 * succeed and -ENOMEM implies there is not.
1844 * We currently support three overcommit policies, which are set via the
1845 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1847 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1848 * Additional code 2002 Jul 20 by Robert Love.
1850 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1852 * Note this is a helper function intended to be used by LSMs which
1853 * wish to use this logic.
1855 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1857 unsigned long free
, allowed
;
1859 vm_acct_memory(pages
);
1862 * Sometimes we want to use more memory than we have
1864 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1867 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1870 free
= global_page_state(NR_FILE_PAGES
);
1871 free
+= nr_swap_pages
;
1874 * Any slabs which are created with the
1875 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1876 * which are reclaimable, under pressure. The dentry
1877 * cache and most inode caches should fall into this
1879 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1882 * Leave the last 3% for root
1891 * nr_free_pages() is very expensive on large systems,
1892 * only call if we're about to fail.
1894 n
= nr_free_pages();
1897 * Leave reserved pages. The pages are not for anonymous pages.
1899 if (n
<= totalreserve_pages
)
1902 n
-= totalreserve_pages
;
1905 * Leave the last 3% for root
1917 allowed
= totalram_pages
* sysctl_overcommit_ratio
/ 100;
1919 * Leave the last 3% for root
1922 allowed
-= allowed
/ 32;
1923 allowed
+= total_swap_pages
;
1925 /* Don't let a single process grow too big:
1926 leave 3% of the size of this process for other processes */
1928 allowed
-= mm
->total_vm
/ 32;
1930 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
1934 vm_unacct_memory(pages
);
1939 int in_gate_area_no_task(unsigned long addr
)
1944 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1949 EXPORT_SYMBOL(filemap_fault
);
1952 * Access another process' address space.
1953 * - source/target buffer must be kernel space
1955 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
1957 struct vm_area_struct
*vma
;
1958 struct mm_struct
*mm
;
1960 if (addr
+ len
< addr
)
1963 mm
= get_task_mm(tsk
);
1967 down_read(&mm
->mmap_sem
);
1969 /* the access must start within one of the target process's mappings */
1970 vma
= find_vma(mm
, addr
);
1972 /* don't overrun this mapping */
1973 if (addr
+ len
>= vma
->vm_end
)
1974 len
= vma
->vm_end
- addr
;
1976 /* only read or write mappings where it is permitted */
1977 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1978 copy_to_user_page(vma
, NULL
, addr
,
1979 (void *) addr
, buf
, len
);
1980 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1981 copy_from_user_page(vma
, NULL
, addr
,
1982 buf
, (void *) addr
, len
);
1989 up_read(&mm
->mmap_sem
);
1995 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1996 * @inode: The inode to check
1997 * @size: The current filesize of the inode
1998 * @newsize: The proposed filesize of the inode
2000 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2001 * make sure that that any outstanding VMAs aren't broken and then shrink the
2002 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2003 * automatically grant mappings that are too large.
2005 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
2008 struct vm_area_struct
*vma
;
2009 struct prio_tree_iter iter
;
2010 struct vm_region
*region
;
2012 size_t r_size
, r_top
;
2014 low
= newsize
>> PAGE_SHIFT
;
2015 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2017 down_write(&nommu_region_sem
);
2019 /* search for VMAs that fall within the dead zone */
2020 vma_prio_tree_foreach(vma
, &iter
, &inode
->i_mapping
->i_mmap
,
2022 /* found one - only interested if it's shared out of the page
2024 if (vma
->vm_flags
& VM_SHARED
) {
2025 up_write(&nommu_region_sem
);
2026 return -ETXTBSY
; /* not quite true, but near enough */
2030 /* reduce any regions that overlap the dead zone - if in existence,
2031 * these will be pointed to by VMAs that don't overlap the dead zone
2033 * we don't check for any regions that start beyond the EOF as there
2036 vma_prio_tree_foreach(vma
, &iter
, &inode
->i_mapping
->i_mmap
,
2038 if (!(vma
->vm_flags
& VM_SHARED
))
2041 region
= vma
->vm_region
;
2042 r_size
= region
->vm_top
- region
->vm_start
;
2043 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
2045 if (r_top
> newsize
) {
2046 region
->vm_top
-= r_top
- newsize
;
2047 if (region
->vm_end
> region
->vm_top
)
2048 region
->vm_end
= region
->vm_top
;
2052 up_write(&nommu_region_sem
);