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-2010 Paul Mundt <lethal@linux-sh.org>
16 #include <linux/export.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/blkdev.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mount.h>
28 #include <linux/personality.h>
29 #include <linux/security.h>
30 #include <linux/syscalls.h>
31 #include <linux/audit.h>
32 #include <linux/sched/sysctl.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
;
71 * The global memory commitment made in the system can be a metric
72 * that can be used to drive ballooning decisions when Linux is hosted
73 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
74 * balancing memory across competing virtual machines that are hosted.
75 * Several metrics drive this policy engine including the guest reported
78 unsigned long vm_memory_committed(void)
80 return percpu_counter_read_positive(&vm_committed_as
);
83 EXPORT_SYMBOL_GPL(vm_memory_committed
);
85 EXPORT_SYMBOL(mem_map
);
86 EXPORT_SYMBOL(num_physpages
);
88 /* list of mapped, potentially shareable regions */
89 static struct kmem_cache
*vm_region_jar
;
90 struct rb_root nommu_region_tree
= RB_ROOT
;
91 DECLARE_RWSEM(nommu_region_sem
);
93 const struct vm_operations_struct generic_file_vm_ops
= {
97 * Return the total memory allocated for this pointer, not
98 * just what the caller asked for.
100 * Doesn't have to be accurate, i.e. may have races.
102 unsigned int kobjsize(const void *objp
)
107 * If the object we have should not have ksize performed on it,
110 if (!objp
|| !virt_addr_valid(objp
))
113 page
= virt_to_head_page(objp
);
116 * If the allocator sets PageSlab, we know the pointer came from
123 * If it's not a compound page, see if we have a matching VMA
124 * region. This test is intentionally done in reverse order,
125 * so if there's no VMA, we still fall through and hand back
126 * PAGE_SIZE for 0-order pages.
128 if (!PageCompound(page
)) {
129 struct vm_area_struct
*vma
;
131 vma
= find_vma(current
->mm
, (unsigned long)objp
);
133 return vma
->vm_end
- vma
->vm_start
;
137 * The ksize() function is only guaranteed to work for pointers
138 * returned by kmalloc(). So handle arbitrary pointers here.
140 return PAGE_SIZE
<< compound_order(page
);
143 int __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
144 unsigned long start
, int nr_pages
, unsigned int foll_flags
,
145 struct page
**pages
, struct vm_area_struct
**vmas
,
148 struct vm_area_struct
*vma
;
149 unsigned long vm_flags
;
152 /* calculate required read or write permissions.
153 * If FOLL_FORCE is set, we only require the "MAY" flags.
155 vm_flags
= (foll_flags
& FOLL_WRITE
) ?
156 (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
157 vm_flags
&= (foll_flags
& FOLL_FORCE
) ?
158 (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
160 for (i
= 0; i
< nr_pages
; i
++) {
161 vma
= find_vma(mm
, start
);
163 goto finish_or_fault
;
165 /* protect what we can, including chardevs */
166 if ((vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)) ||
167 !(vm_flags
& vma
->vm_flags
))
168 goto finish_or_fault
;
171 pages
[i
] = virt_to_page(start
);
173 page_cache_get(pages
[i
]);
177 start
= (start
+ PAGE_SIZE
) & PAGE_MASK
;
183 return i
? : -EFAULT
;
187 * get a list of pages in an address range belonging to the specified process
188 * and indicate the VMA that covers each page
189 * - this is potentially dodgy as we may end incrementing the page count of a
190 * slab page or a secondary page from a compound page
191 * - don't permit access to VMAs that don't support it, such as I/O mappings
193 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
194 unsigned long start
, int nr_pages
, int write
, int force
,
195 struct page
**pages
, struct vm_area_struct
**vmas
)
204 return __get_user_pages(tsk
, mm
, start
, nr_pages
, flags
, pages
, vmas
,
207 EXPORT_SYMBOL(get_user_pages
);
210 * follow_pfn - look up PFN at a user virtual address
211 * @vma: memory mapping
212 * @address: user virtual address
213 * @pfn: location to store found PFN
215 * Only IO mappings and raw PFN mappings are allowed.
217 * Returns zero and the pfn at @pfn on success, -ve otherwise.
219 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
222 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
225 *pfn
= address
>> PAGE_SHIFT
;
228 EXPORT_SYMBOL(follow_pfn
);
230 DEFINE_RWLOCK(vmlist_lock
);
231 struct vm_struct
*vmlist
;
233 void vfree(const void *addr
)
237 EXPORT_SYMBOL(vfree
);
239 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
242 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
243 * returns only a logical address.
245 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
247 EXPORT_SYMBOL(__vmalloc
);
249 void *vmalloc_user(unsigned long size
)
253 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
256 struct vm_area_struct
*vma
;
258 down_write(¤t
->mm
->mmap_sem
);
259 vma
= find_vma(current
->mm
, (unsigned long)ret
);
261 vma
->vm_flags
|= VM_USERMAP
;
262 up_write(¤t
->mm
->mmap_sem
);
267 EXPORT_SYMBOL(vmalloc_user
);
269 struct page
*vmalloc_to_page(const void *addr
)
271 return virt_to_page(addr
);
273 EXPORT_SYMBOL(vmalloc_to_page
);
275 unsigned long vmalloc_to_pfn(const void *addr
)
277 return page_to_pfn(virt_to_page(addr
));
279 EXPORT_SYMBOL(vmalloc_to_pfn
);
281 long vread(char *buf
, char *addr
, unsigned long count
)
283 memcpy(buf
, addr
, count
);
287 long vwrite(char *buf
, char *addr
, unsigned long count
)
289 /* Don't allow overflow */
290 if ((unsigned long) addr
+ count
< count
)
291 count
= -(unsigned long) addr
;
293 memcpy(addr
, buf
, count
);
298 * vmalloc - allocate virtually continguos memory
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.
305 * For tight control over page level allocator and protection flags
306 * use __vmalloc() instead.
308 void *vmalloc(unsigned long size
)
310 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
312 EXPORT_SYMBOL(vmalloc
);
315 * vzalloc - allocate virtually continguos memory with zero fill
317 * @size: allocation size
319 * Allocate enough pages to cover @size from the page level
320 * allocator and map them into continguos kernel virtual space.
321 * The memory allocated is set to zero.
323 * For tight control over page level allocator and protection flags
324 * use __vmalloc() instead.
326 void *vzalloc(unsigned long size
)
328 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
331 EXPORT_SYMBOL(vzalloc
);
334 * vmalloc_node - allocate memory on a specific node
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.
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
344 void *vmalloc_node(unsigned long size
, int node
)
346 return vmalloc(size
);
348 EXPORT_SYMBOL(vmalloc_node
);
351 * vzalloc_node - allocate memory on a specific node with zero fill
352 * @size: allocation size
355 * Allocate enough pages to cover @size from the page level
356 * allocator and map them into contiguous kernel virtual space.
357 * The memory allocated is set to zero.
359 * For tight control over page level allocator and protection flags
360 * use __vmalloc() instead.
362 void *vzalloc_node(unsigned long size
, int node
)
364 return vzalloc(size
);
366 EXPORT_SYMBOL(vzalloc_node
);
368 #ifndef PAGE_KERNEL_EXEC
369 # define PAGE_KERNEL_EXEC PAGE_KERNEL
373 * vmalloc_exec - allocate virtually contiguous, executable memory
374 * @size: allocation size
376 * Kernel-internal function to allocate enough pages to cover @size
377 * the page level allocator and map them into contiguous and
378 * executable kernel virtual space.
380 * For tight control over page level allocator and protection flags
381 * use __vmalloc() instead.
384 void *vmalloc_exec(unsigned long size
)
386 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
390 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
391 * @size: allocation size
393 * Allocate enough 32bit PA addressable pages to cover @size from the
394 * page level allocator and map them into continguos kernel virtual space.
396 void *vmalloc_32(unsigned long size
)
398 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
400 EXPORT_SYMBOL(vmalloc_32
);
403 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
404 * @size: allocation size
406 * The resulting memory area is 32bit addressable and zeroed so it can be
407 * mapped to userspace without leaking data.
409 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
410 * remap_vmalloc_range() are permissible.
412 void *vmalloc_32_user(unsigned long size
)
415 * We'll have to sort out the ZONE_DMA bits for 64-bit,
416 * but for now this can simply use vmalloc_user() directly.
418 return vmalloc_user(size
);
420 EXPORT_SYMBOL(vmalloc_32_user
);
422 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
429 void vunmap(const void *addr
)
433 EXPORT_SYMBOL(vunmap
);
435 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
, pgprot_t prot
)
440 EXPORT_SYMBOL(vm_map_ram
);
442 void vm_unmap_ram(const void *mem
, unsigned int count
)
446 EXPORT_SYMBOL(vm_unmap_ram
);
448 void vm_unmap_aliases(void)
451 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
454 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
457 void __attribute__((weak
)) vmalloc_sync_all(void)
462 * alloc_vm_area - allocate a range of kernel address space
463 * @size: size of the area
465 * Returns: NULL on failure, vm_struct on success
467 * This function reserves a range of kernel address space, and
468 * allocates pagetables to map that range. No actual mappings
469 * are created. If the kernel address space is not shared
470 * between processes, it syncs the pagetable across all
473 struct vm_struct
*alloc_vm_area(size_t size
, pte_t
**ptes
)
478 EXPORT_SYMBOL_GPL(alloc_vm_area
);
480 void free_vm_area(struct vm_struct
*area
)
484 EXPORT_SYMBOL_GPL(free_vm_area
);
486 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
491 EXPORT_SYMBOL(vm_insert_page
);
494 * sys_brk() for the most part doesn't need the global kernel
495 * lock, except when an application is doing something nasty
496 * like trying to un-brk an area that has already been mapped
497 * to a regular file. in this case, the unmapping will need
498 * to invoke file system routines that need the global lock.
500 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
502 struct mm_struct
*mm
= current
->mm
;
504 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
511 * Always allow shrinking brk
513 if (brk
<= mm
->brk
) {
519 * Ok, looks good - let it rip.
521 flush_icache_range(mm
->brk
, brk
);
522 return mm
->brk
= brk
;
526 * initialise the VMA and region record slabs
528 void __init
mmap_init(void)
532 ret
= percpu_counter_init(&vm_committed_as
, 0);
534 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
);
538 * validate the region tree
539 * - the caller must hold the region lock
541 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
542 static noinline
void validate_nommu_regions(void)
544 struct vm_region
*region
, *last
;
545 struct rb_node
*p
, *lastp
;
547 lastp
= rb_first(&nommu_region_tree
);
551 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
552 BUG_ON(unlikely(last
->vm_end
<= last
->vm_start
));
553 BUG_ON(unlikely(last
->vm_top
< last
->vm_end
));
555 while ((p
= rb_next(lastp
))) {
556 region
= rb_entry(p
, struct vm_region
, vm_rb
);
557 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
559 BUG_ON(unlikely(region
->vm_end
<= region
->vm_start
));
560 BUG_ON(unlikely(region
->vm_top
< region
->vm_end
));
561 BUG_ON(unlikely(region
->vm_start
< last
->vm_top
));
567 static void validate_nommu_regions(void)
573 * add a region into the global tree
575 static void add_nommu_region(struct vm_region
*region
)
577 struct vm_region
*pregion
;
578 struct rb_node
**p
, *parent
;
580 validate_nommu_regions();
583 p
= &nommu_region_tree
.rb_node
;
586 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
587 if (region
->vm_start
< pregion
->vm_start
)
589 else if (region
->vm_start
> pregion
->vm_start
)
591 else if (pregion
== region
)
597 rb_link_node(®ion
->vm_rb
, parent
, p
);
598 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
600 validate_nommu_regions();
604 * delete a region from the global tree
606 static void delete_nommu_region(struct vm_region
*region
)
608 BUG_ON(!nommu_region_tree
.rb_node
);
610 validate_nommu_regions();
611 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
612 validate_nommu_regions();
616 * free a contiguous series of pages
618 static void free_page_series(unsigned long from
, unsigned long to
)
620 for (; from
< to
; from
+= PAGE_SIZE
) {
621 struct page
*page
= virt_to_page(from
);
623 kdebug("- free %lx", from
);
624 atomic_long_dec(&mmap_pages_allocated
);
625 if (page_count(page
) != 1)
626 kdebug("free page %p: refcount not one: %d",
627 page
, page_count(page
));
633 * release a reference to a region
634 * - the caller must hold the region semaphore for writing, which this releases
635 * - the region may not have been added to the tree yet, in which case vm_top
636 * will equal vm_start
638 static void __put_nommu_region(struct vm_region
*region
)
639 __releases(nommu_region_sem
)
641 kenter("%p{%d}", region
, region
->vm_usage
);
643 BUG_ON(!nommu_region_tree
.rb_node
);
645 if (--region
->vm_usage
== 0) {
646 if (region
->vm_top
> region
->vm_start
)
647 delete_nommu_region(region
);
648 up_write(&nommu_region_sem
);
651 fput(region
->vm_file
);
653 /* IO memory and memory shared directly out of the pagecache
654 * from ramfs/tmpfs mustn't be released here */
655 if (region
->vm_flags
& VM_MAPPED_COPY
) {
656 kdebug("free series");
657 free_page_series(region
->vm_start
, region
->vm_top
);
659 kmem_cache_free(vm_region_jar
, region
);
661 up_write(&nommu_region_sem
);
666 * release a reference to a region
668 static void put_nommu_region(struct vm_region
*region
)
670 down_write(&nommu_region_sem
);
671 __put_nommu_region(region
);
675 * update protection on a vma
677 static void protect_vma(struct vm_area_struct
*vma
, unsigned long flags
)
680 struct mm_struct
*mm
= vma
->vm_mm
;
681 long start
= vma
->vm_start
& PAGE_MASK
;
682 while (start
< vma
->vm_end
) {
683 protect_page(mm
, start
, flags
);
686 update_protections(mm
);
691 * add a VMA into a process's mm_struct in the appropriate place in the list
692 * and tree and add to the address space's page tree also if not an anonymous
694 * - should be called with mm->mmap_sem held writelocked
696 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
698 struct vm_area_struct
*pvma
, *prev
;
699 struct address_space
*mapping
;
700 struct rb_node
**p
, *parent
, *rb_prev
;
704 BUG_ON(!vma
->vm_region
);
709 protect_vma(vma
, vma
->vm_flags
);
711 /* add the VMA to the mapping */
713 mapping
= vma
->vm_file
->f_mapping
;
715 mutex_lock(&mapping
->i_mmap_mutex
);
716 flush_dcache_mmap_lock(mapping
);
717 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
718 flush_dcache_mmap_unlock(mapping
);
719 mutex_unlock(&mapping
->i_mmap_mutex
);
722 /* add the VMA to the tree */
723 parent
= rb_prev
= NULL
;
724 p
= &mm
->mm_rb
.rb_node
;
727 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
729 /* sort by: start addr, end addr, VMA struct addr in that order
730 * (the latter is necessary as we may get identical VMAs) */
731 if (vma
->vm_start
< pvma
->vm_start
)
733 else if (vma
->vm_start
> pvma
->vm_start
) {
736 } else if (vma
->vm_end
< pvma
->vm_end
)
738 else if (vma
->vm_end
> pvma
->vm_end
) {
741 } else if (vma
< pvma
)
743 else if (vma
> pvma
) {
750 rb_link_node(&vma
->vm_rb
, parent
, p
);
751 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
753 /* add VMA to the VMA list also */
756 prev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
758 __vma_link_list(mm
, vma
, prev
, parent
);
762 * delete a VMA from its owning mm_struct and address space
764 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
766 struct address_space
*mapping
;
767 struct mm_struct
*mm
= vma
->vm_mm
;
774 if (mm
->mmap_cache
== vma
)
775 mm
->mmap_cache
= NULL
;
777 /* remove the VMA from the mapping */
779 mapping
= vma
->vm_file
->f_mapping
;
781 mutex_lock(&mapping
->i_mmap_mutex
);
782 flush_dcache_mmap_lock(mapping
);
783 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
784 flush_dcache_mmap_unlock(mapping
);
785 mutex_unlock(&mapping
->i_mmap_mutex
);
788 /* remove from the MM's tree and list */
789 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
792 vma
->vm_prev
->vm_next
= vma
->vm_next
;
794 mm
->mmap
= vma
->vm_next
;
797 vma
->vm_next
->vm_prev
= vma
->vm_prev
;
801 * destroy a VMA record
803 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
806 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
807 vma
->vm_ops
->close(vma
);
810 put_nommu_region(vma
->vm_region
);
811 kmem_cache_free(vm_area_cachep
, vma
);
815 * look up the first VMA in which addr resides, NULL if none
816 * - should be called with mm->mmap_sem at least held readlocked
818 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
820 struct vm_area_struct
*vma
;
822 /* check the cache first */
823 vma
= mm
->mmap_cache
;
824 if (vma
&& vma
->vm_start
<= addr
&& vma
->vm_end
> addr
)
827 /* trawl the list (there may be multiple mappings in which addr
829 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
830 if (vma
->vm_start
> addr
)
832 if (vma
->vm_end
> addr
) {
833 mm
->mmap_cache
= vma
;
840 EXPORT_SYMBOL(find_vma
);
844 * - we don't extend stack VMAs under NOMMU conditions
846 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
848 return find_vma(mm
, addr
);
852 * expand a stack to a given address
853 * - not supported under NOMMU conditions
855 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
861 * look up the first VMA exactly that exactly matches addr
862 * - should be called with mm->mmap_sem at least held readlocked
864 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
868 struct vm_area_struct
*vma
;
869 unsigned long end
= addr
+ len
;
871 /* check the cache first */
872 vma
= mm
->mmap_cache
;
873 if (vma
&& vma
->vm_start
== addr
&& vma
->vm_end
== end
)
876 /* trawl the list (there may be multiple mappings in which addr
878 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
879 if (vma
->vm_start
< addr
)
881 if (vma
->vm_start
> addr
)
883 if (vma
->vm_end
== end
) {
884 mm
->mmap_cache
= vma
;
893 * determine whether a mapping should be permitted and, if so, what sort of
894 * mapping we're capable of supporting
896 static int validate_mmap_request(struct file
*file
,
902 unsigned long *_capabilities
)
904 unsigned long capabilities
, rlen
;
907 /* do the simple checks first */
908 if (flags
& MAP_FIXED
) {
910 "%d: Can't do fixed-address/overlay mmap of RAM\n",
915 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
916 (flags
& MAP_TYPE
) != MAP_SHARED
)
922 /* Careful about overflows.. */
923 rlen
= PAGE_ALIGN(len
);
924 if (!rlen
|| rlen
> TASK_SIZE
)
927 /* offset overflow? */
928 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
932 /* validate file mapping requests */
933 struct address_space
*mapping
;
935 /* files must support mmap */
936 if (!file
->f_op
|| !file
->f_op
->mmap
)
939 /* work out if what we've got could possibly be shared
940 * - we support chardevs that provide their own "memory"
941 * - we support files/blockdevs that are memory backed
943 mapping
= file
->f_mapping
;
945 mapping
= file
->f_path
.dentry
->d_inode
->i_mapping
;
948 if (mapping
&& mapping
->backing_dev_info
)
949 capabilities
= mapping
->backing_dev_info
->capabilities
;
952 /* no explicit capabilities set, so assume some
954 switch (file
->f_path
.dentry
->d_inode
->i_mode
& S_IFMT
) {
957 capabilities
= BDI_CAP_MAP_COPY
;
972 /* eliminate any capabilities that we can't support on this
974 if (!file
->f_op
->get_unmapped_area
)
975 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
976 if (!file
->f_op
->read
)
977 capabilities
&= ~BDI_CAP_MAP_COPY
;
979 /* The file shall have been opened with read permission. */
980 if (!(file
->f_mode
& FMODE_READ
))
983 if (flags
& MAP_SHARED
) {
984 /* do checks for writing, appending and locking */
985 if ((prot
& PROT_WRITE
) &&
986 !(file
->f_mode
& FMODE_WRITE
))
989 if (IS_APPEND(file
->f_path
.dentry
->d_inode
) &&
990 (file
->f_mode
& FMODE_WRITE
))
993 if (locks_verify_locked(file
->f_path
.dentry
->d_inode
))
996 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
999 /* we mustn't privatise shared mappings */
1000 capabilities
&= ~BDI_CAP_MAP_COPY
;
1003 /* we're going to read the file into private memory we
1005 if (!(capabilities
& BDI_CAP_MAP_COPY
))
1008 /* we don't permit a private writable mapping to be
1009 * shared with the backing device */
1010 if (prot
& PROT_WRITE
)
1011 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1014 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1015 if (((prot
& PROT_READ
) && !(capabilities
& BDI_CAP_READ_MAP
)) ||
1016 ((prot
& PROT_WRITE
) && !(capabilities
& BDI_CAP_WRITE_MAP
)) ||
1017 ((prot
& PROT_EXEC
) && !(capabilities
& BDI_CAP_EXEC_MAP
))
1019 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1020 if (flags
& MAP_SHARED
) {
1022 "MAP_SHARED not completely supported on !MMU\n");
1028 /* handle executable mappings and implied executable
1030 if (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
) {
1031 if (prot
& PROT_EXEC
)
1034 else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
1035 /* handle implication of PROT_EXEC by PROT_READ */
1036 if (current
->personality
& READ_IMPLIES_EXEC
) {
1037 if (capabilities
& BDI_CAP_EXEC_MAP
)
1041 else if ((prot
& PROT_READ
) &&
1042 (prot
& PROT_EXEC
) &&
1043 !(capabilities
& BDI_CAP_EXEC_MAP
)
1045 /* backing file is not executable, try to copy */
1046 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1050 /* anonymous mappings are always memory backed and can be
1053 capabilities
= BDI_CAP_MAP_COPY
;
1055 /* handle PROT_EXEC implication by PROT_READ */
1056 if ((prot
& PROT_READ
) &&
1057 (current
->personality
& READ_IMPLIES_EXEC
))
1061 /* allow the security API to have its say */
1062 ret
= security_mmap_addr(addr
);
1067 *_capabilities
= capabilities
;
1072 * we've determined that we can make the mapping, now translate what we
1073 * now know into VMA flags
1075 static unsigned long determine_vm_flags(struct file
*file
,
1077 unsigned long flags
,
1078 unsigned long capabilities
)
1080 unsigned long vm_flags
;
1082 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
1083 /* vm_flags |= mm->def_flags; */
1085 if (!(capabilities
& BDI_CAP_MAP_DIRECT
)) {
1086 /* attempt to share read-only copies of mapped file chunks */
1087 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1088 if (file
&& !(prot
& PROT_WRITE
))
1089 vm_flags
|= VM_MAYSHARE
;
1091 /* overlay a shareable mapping on the backing device or inode
1092 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1094 vm_flags
|= VM_MAYSHARE
| (capabilities
& BDI_CAP_VMFLAGS
);
1095 if (flags
& MAP_SHARED
)
1096 vm_flags
|= VM_SHARED
;
1099 /* refuse to let anyone share private mappings with this process if
1100 * it's being traced - otherwise breakpoints set in it may interfere
1101 * with another untraced process
1103 if ((flags
& MAP_PRIVATE
) && current
->ptrace
)
1104 vm_flags
&= ~VM_MAYSHARE
;
1110 * set up a shared mapping on a file (the driver or filesystem provides and
1113 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1117 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1119 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1125 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1126 * opposed to tried but failed) so we can only give a suitable error as
1127 * it's not possible to make a private copy if MAP_SHARED was given */
1132 * set up a private mapping or an anonymous shared mapping
1134 static int do_mmap_private(struct vm_area_struct
*vma
,
1135 struct vm_region
*region
,
1137 unsigned long capabilities
)
1140 unsigned long total
, point
, n
;
1144 /* invoke the file's mapping function so that it can keep track of
1145 * shared mappings on devices or memory
1146 * - VM_MAYSHARE will be set if it may attempt to share
1148 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1149 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1151 /* shouldn't return success if we're not sharing */
1152 BUG_ON(!(vma
->vm_flags
& VM_MAYSHARE
));
1153 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1159 /* getting an ENOSYS error indicates that direct mmap isn't
1160 * possible (as opposed to tried but failed) so we'll try to
1161 * make a private copy of the data and map that instead */
1165 /* allocate some memory to hold the mapping
1166 * - note that this may not return a page-aligned address if the object
1167 * we're allocating is smaller than a page
1169 order
= get_order(len
);
1170 kdebug("alloc order %d for %lx", order
, len
);
1172 pages
= alloc_pages(GFP_KERNEL
, order
);
1177 atomic_long_add(total
, &mmap_pages_allocated
);
1179 point
= len
>> PAGE_SHIFT
;
1181 /* we allocated a power-of-2 sized page set, so we may want to trim off
1183 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
) {
1184 while (total
> point
) {
1185 order
= ilog2(total
- point
);
1187 kdebug("shave %lu/%lu @%lu", n
, total
- point
, total
);
1188 atomic_long_sub(n
, &mmap_pages_allocated
);
1190 set_page_refcounted(pages
+ total
);
1191 __free_pages(pages
+ total
, order
);
1195 for (point
= 1; point
< total
; point
++)
1196 set_page_refcounted(&pages
[point
]);
1198 base
= page_address(pages
);
1199 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1200 region
->vm_start
= (unsigned long) base
;
1201 region
->vm_end
= region
->vm_start
+ len
;
1202 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1204 vma
->vm_start
= region
->vm_start
;
1205 vma
->vm_end
= region
->vm_start
+ len
;
1208 /* read the contents of a file into the copy */
1209 mm_segment_t old_fs
;
1212 fpos
= vma
->vm_pgoff
;
1213 fpos
<<= PAGE_SHIFT
;
1217 ret
= vma
->vm_file
->f_op
->read(vma
->vm_file
, base
, len
, &fpos
);
1223 /* clear the last little bit */
1225 memset(base
+ ret
, 0, len
- ret
);
1232 free_page_series(region
->vm_start
, region
->vm_top
);
1233 region
->vm_start
= vma
->vm_start
= 0;
1234 region
->vm_end
= vma
->vm_end
= 0;
1239 printk("Allocation of length %lu from process %d (%s) failed\n",
1240 len
, current
->pid
, current
->comm
);
1246 * handle mapping creation for uClinux
1248 unsigned long do_mmap_pgoff(struct file
*file
,
1252 unsigned long flags
,
1253 unsigned long pgoff
,
1254 unsigned long *populate
)
1256 struct vm_area_struct
*vma
;
1257 struct vm_region
*region
;
1259 unsigned long capabilities
, vm_flags
, result
;
1262 kenter(",%lx,%lx,%lx,%lx,%lx", addr
, len
, prot
, flags
, pgoff
);
1266 /* decide whether we should attempt the mapping, and if so what sort of
1268 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1271 kleave(" = %d [val]", ret
);
1275 /* we ignore the address hint */
1277 len
= PAGE_ALIGN(len
);
1279 /* we've determined that we can make the mapping, now translate what we
1280 * now know into VMA flags */
1281 vm_flags
= determine_vm_flags(file
, prot
, flags
, capabilities
);
1283 /* we're going to need to record the mapping */
1284 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1286 goto error_getting_region
;
1288 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1290 goto error_getting_vma
;
1292 region
->vm_usage
= 1;
1293 region
->vm_flags
= vm_flags
;
1294 region
->vm_pgoff
= pgoff
;
1296 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1297 vma
->vm_flags
= vm_flags
;
1298 vma
->vm_pgoff
= pgoff
;
1301 region
->vm_file
= get_file(file
);
1302 vma
->vm_file
= get_file(file
);
1305 down_write(&nommu_region_sem
);
1307 /* if we want to share, we need to check for regions created by other
1308 * mmap() calls that overlap with our proposed mapping
1309 * - we can only share with a superset match on most regular files
1310 * - shared mappings on character devices and memory backed files are
1311 * permitted to overlap inexactly as far as we are concerned for in
1312 * these cases, sharing is handled in the driver or filesystem rather
1315 if (vm_flags
& VM_MAYSHARE
) {
1316 struct vm_region
*pregion
;
1317 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1319 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1320 pgend
= pgoff
+ pglen
;
1322 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1323 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1325 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1328 /* search for overlapping mappings on the same file */
1329 if (pregion
->vm_file
->f_path
.dentry
->d_inode
!=
1330 file
->f_path
.dentry
->d_inode
)
1333 if (pregion
->vm_pgoff
>= pgend
)
1336 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1337 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1338 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1339 if (pgoff
>= rpgend
)
1342 /* handle inexactly overlapping matches between
1344 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1345 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1346 /* new mapping is not a subset of the region */
1347 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
1348 goto sharing_violation
;
1352 /* we've found a region we can share */
1353 pregion
->vm_usage
++;
1354 vma
->vm_region
= pregion
;
1355 start
= pregion
->vm_start
;
1356 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1357 vma
->vm_start
= start
;
1358 vma
->vm_end
= start
+ len
;
1360 if (pregion
->vm_flags
& VM_MAPPED_COPY
) {
1361 kdebug("share copy");
1362 vma
->vm_flags
|= VM_MAPPED_COPY
;
1364 kdebug("share mmap");
1365 ret
= do_mmap_shared_file(vma
);
1367 vma
->vm_region
= NULL
;
1370 pregion
->vm_usage
--;
1372 goto error_just_free
;
1375 fput(region
->vm_file
);
1376 kmem_cache_free(vm_region_jar
, region
);
1382 /* obtain the address at which to make a shared mapping
1383 * - this is the hook for quasi-memory character devices to
1384 * tell us the location of a shared mapping
1386 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1387 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1389 if (IS_ERR_VALUE(addr
)) {
1392 goto error_just_free
;
1394 /* the driver refused to tell us where to site
1395 * the mapping so we'll have to attempt to copy
1398 if (!(capabilities
& BDI_CAP_MAP_COPY
))
1399 goto error_just_free
;
1401 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1403 vma
->vm_start
= region
->vm_start
= addr
;
1404 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1409 vma
->vm_region
= region
;
1411 /* set up the mapping
1412 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1414 if (file
&& vma
->vm_flags
& VM_SHARED
)
1415 ret
= do_mmap_shared_file(vma
);
1417 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1419 goto error_just_free
;
1420 add_nommu_region(region
);
1422 /* clear anonymous mappings that don't ask for uninitialized data */
1423 if (!vma
->vm_file
&& !(flags
& MAP_UNINITIALIZED
))
1424 memset((void *)region
->vm_start
, 0,
1425 region
->vm_end
- region
->vm_start
);
1427 /* okay... we have a mapping; now we have to register it */
1428 result
= vma
->vm_start
;
1430 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1433 add_vma_to_mm(current
->mm
, vma
);
1435 /* we flush the region from the icache only when the first executable
1436 * mapping of it is made */
1437 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1438 flush_icache_range(region
->vm_start
, region
->vm_end
);
1439 region
->vm_icache_flushed
= true;
1442 up_write(&nommu_region_sem
);
1444 kleave(" = %lx", result
);
1448 up_write(&nommu_region_sem
);
1450 if (region
->vm_file
)
1451 fput(region
->vm_file
);
1452 kmem_cache_free(vm_region_jar
, region
);
1455 kmem_cache_free(vm_area_cachep
, vma
);
1456 kleave(" = %d", ret
);
1460 up_write(&nommu_region_sem
);
1461 printk(KERN_WARNING
"Attempt to share mismatched mappings\n");
1466 kmem_cache_free(vm_region_jar
, region
);
1467 printk(KERN_WARNING
"Allocation of vma for %lu byte allocation"
1468 " from process %d failed\n",
1473 error_getting_region
:
1474 printk(KERN_WARNING
"Allocation of vm region for %lu byte allocation"
1475 " from process %d failed\n",
1481 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1482 unsigned long, prot
, unsigned long, flags
,
1483 unsigned long, fd
, unsigned long, pgoff
)
1485 struct file
*file
= NULL
;
1486 unsigned long retval
= -EBADF
;
1488 audit_mmap_fd(fd
, flags
);
1489 if (!(flags
& MAP_ANONYMOUS
)) {
1495 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1497 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1505 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1506 struct mmap_arg_struct
{
1510 unsigned long flags
;
1512 unsigned long offset
;
1515 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1517 struct mmap_arg_struct a
;
1519 if (copy_from_user(&a
, arg
, sizeof(a
)))
1521 if (a
.offset
& ~PAGE_MASK
)
1524 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1525 a
.offset
>> PAGE_SHIFT
);
1527 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1530 * split a vma into two pieces at address 'addr', a new vma is allocated either
1531 * for the first part or the tail.
1533 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1534 unsigned long addr
, int new_below
)
1536 struct vm_area_struct
*new;
1537 struct vm_region
*region
;
1538 unsigned long npages
;
1542 /* we're only permitted to split anonymous regions (these should have
1543 * only a single usage on the region) */
1547 if (mm
->map_count
>= sysctl_max_map_count
)
1550 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1554 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
1556 kmem_cache_free(vm_region_jar
, region
);
1560 /* most fields are the same, copy all, and then fixup */
1562 *region
= *vma
->vm_region
;
1563 new->vm_region
= region
;
1565 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1568 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1570 region
->vm_start
= new->vm_start
= addr
;
1571 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1574 if (new->vm_ops
&& new->vm_ops
->open
)
1575 new->vm_ops
->open(new);
1577 delete_vma_from_mm(vma
);
1578 down_write(&nommu_region_sem
);
1579 delete_nommu_region(vma
->vm_region
);
1581 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1582 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1584 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1585 vma
->vm_region
->vm_top
= addr
;
1587 add_nommu_region(vma
->vm_region
);
1588 add_nommu_region(new->vm_region
);
1589 up_write(&nommu_region_sem
);
1590 add_vma_to_mm(mm
, vma
);
1591 add_vma_to_mm(mm
, new);
1596 * shrink a VMA by removing the specified chunk from either the beginning or
1599 static int shrink_vma(struct mm_struct
*mm
,
1600 struct vm_area_struct
*vma
,
1601 unsigned long from
, unsigned long to
)
1603 struct vm_region
*region
;
1607 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1609 delete_vma_from_mm(vma
);
1610 if (from
> vma
->vm_start
)
1614 add_vma_to_mm(mm
, vma
);
1616 /* cut the backing region down to size */
1617 region
= vma
->vm_region
;
1618 BUG_ON(region
->vm_usage
!= 1);
1620 down_write(&nommu_region_sem
);
1621 delete_nommu_region(region
);
1622 if (from
> region
->vm_start
) {
1623 to
= region
->vm_top
;
1624 region
->vm_top
= region
->vm_end
= from
;
1626 region
->vm_start
= to
;
1628 add_nommu_region(region
);
1629 up_write(&nommu_region_sem
);
1631 free_page_series(from
, to
);
1637 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1638 * VMA, though it need not cover the whole VMA
1640 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1642 struct vm_area_struct
*vma
;
1646 kenter(",%lx,%zx", start
, len
);
1648 len
= PAGE_ALIGN(len
);
1654 /* find the first potentially overlapping VMA */
1655 vma
= find_vma(mm
, start
);
1657 static int limit
= 0;
1660 "munmap of memory not mmapped by process %d"
1661 " (%s): 0x%lx-0x%lx\n",
1662 current
->pid
, current
->comm
,
1663 start
, start
+ len
- 1);
1669 /* we're allowed to split an anonymous VMA but not a file-backed one */
1672 if (start
> vma
->vm_start
) {
1673 kleave(" = -EINVAL [miss]");
1676 if (end
== vma
->vm_end
)
1677 goto erase_whole_vma
;
1680 kleave(" = -EINVAL [split file]");
1683 /* the chunk must be a subset of the VMA found */
1684 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1685 goto erase_whole_vma
;
1686 if (start
< vma
->vm_start
|| end
> vma
->vm_end
) {
1687 kleave(" = -EINVAL [superset]");
1690 if (start
& ~PAGE_MASK
) {
1691 kleave(" = -EINVAL [unaligned start]");
1694 if (end
!= vma
->vm_end
&& end
& ~PAGE_MASK
) {
1695 kleave(" = -EINVAL [unaligned split]");
1698 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1699 ret
= split_vma(mm
, vma
, start
, 1);
1701 kleave(" = %d [split]", ret
);
1705 return shrink_vma(mm
, vma
, start
, end
);
1709 delete_vma_from_mm(vma
);
1710 delete_vma(mm
, vma
);
1714 EXPORT_SYMBOL(do_munmap
);
1716 int vm_munmap(unsigned long addr
, size_t len
)
1718 struct mm_struct
*mm
= current
->mm
;
1721 down_write(&mm
->mmap_sem
);
1722 ret
= do_munmap(mm
, addr
, len
);
1723 up_write(&mm
->mmap_sem
);
1726 EXPORT_SYMBOL(vm_munmap
);
1728 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1730 return vm_munmap(addr
, len
);
1734 * release all the mappings made in a process's VM space
1736 void exit_mmap(struct mm_struct
*mm
)
1738 struct vm_area_struct
*vma
;
1747 while ((vma
= mm
->mmap
)) {
1748 mm
->mmap
= vma
->vm_next
;
1749 delete_vma_from_mm(vma
);
1750 delete_vma(mm
, vma
);
1757 unsigned long vm_brk(unsigned long addr
, unsigned long len
)
1763 * expand (or shrink) an existing mapping, potentially moving it at the same
1764 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1766 * under NOMMU conditions, we only permit changing a mapping's size, and only
1767 * as long as it stays within the region allocated by do_mmap_private() and the
1768 * block is not shareable
1770 * MREMAP_FIXED is not supported under NOMMU conditions
1772 unsigned long do_mremap(unsigned long addr
,
1773 unsigned long old_len
, unsigned long new_len
,
1774 unsigned long flags
, unsigned long new_addr
)
1776 struct vm_area_struct
*vma
;
1778 /* insanity checks first */
1779 old_len
= PAGE_ALIGN(old_len
);
1780 new_len
= PAGE_ALIGN(new_len
);
1781 if (old_len
== 0 || new_len
== 0)
1782 return (unsigned long) -EINVAL
;
1784 if (addr
& ~PAGE_MASK
)
1787 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1788 return (unsigned long) -EINVAL
;
1790 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1792 return (unsigned long) -EINVAL
;
1794 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1795 return (unsigned long) -EFAULT
;
1797 if (vma
->vm_flags
& VM_MAYSHARE
)
1798 return (unsigned long) -EPERM
;
1800 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1801 return (unsigned long) -ENOMEM
;
1803 /* all checks complete - do it */
1804 vma
->vm_end
= vma
->vm_start
+ new_len
;
1805 return vma
->vm_start
;
1807 EXPORT_SYMBOL(do_mremap
);
1809 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1810 unsigned long, new_len
, unsigned long, flags
,
1811 unsigned long, new_addr
)
1815 down_write(¤t
->mm
->mmap_sem
);
1816 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1817 up_write(¤t
->mm
->mmap_sem
);
1821 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1822 unsigned int foll_flags
)
1827 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long addr
,
1828 unsigned long pfn
, unsigned long size
, pgprot_t prot
)
1830 if (addr
!= (pfn
<< PAGE_SHIFT
))
1833 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
1836 EXPORT_SYMBOL(remap_pfn_range
);
1838 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1839 unsigned long pgoff
)
1841 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1843 if (!(vma
->vm_flags
& VM_USERMAP
))
1846 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1847 vma
->vm_end
= vma
->vm_start
+ size
;
1851 EXPORT_SYMBOL(remap_vmalloc_range
);
1853 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1854 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1859 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1863 void unmap_mapping_range(struct address_space
*mapping
,
1864 loff_t
const holebegin
, loff_t
const holelen
,
1868 EXPORT_SYMBOL(unmap_mapping_range
);
1871 * Check that a process has enough memory to allocate a new virtual
1872 * mapping. 0 means there is enough memory for the allocation to
1873 * succeed and -ENOMEM implies there is not.
1875 * We currently support three overcommit policies, which are set via the
1876 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1878 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1879 * Additional code 2002 Jul 20 by Robert Love.
1881 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1883 * Note this is a helper function intended to be used by LSMs which
1884 * wish to use this logic.
1886 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1888 unsigned long free
, allowed
;
1890 vm_acct_memory(pages
);
1893 * Sometimes we want to use more memory than we have
1895 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1898 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1899 free
= global_page_state(NR_FREE_PAGES
);
1900 free
+= global_page_state(NR_FILE_PAGES
);
1903 * shmem pages shouldn't be counted as free in this
1904 * case, they can't be purged, only swapped out, and
1905 * that won't affect the overall amount of available
1906 * memory in the system.
1908 free
-= global_page_state(NR_SHMEM
);
1910 free
+= nr_swap_pages
;
1913 * Any slabs which are created with the
1914 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1915 * which are reclaimable, under pressure. The dentry
1916 * cache and most inode caches should fall into this
1918 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1921 * Leave reserved pages. The pages are not for anonymous pages.
1923 if (free
<= totalreserve_pages
)
1926 free
-= totalreserve_pages
;
1929 * Leave the last 3% for root
1940 allowed
= totalram_pages
* sysctl_overcommit_ratio
/ 100;
1942 * Leave the last 3% for root
1945 allowed
-= allowed
/ 32;
1946 allowed
+= total_swap_pages
;
1948 /* Don't let a single process grow too big:
1949 leave 3% of the size of this process for other processes */
1951 allowed
-= mm
->total_vm
/ 32;
1953 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
1957 vm_unacct_memory(pages
);
1962 int in_gate_area_no_mm(unsigned long addr
)
1967 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1972 EXPORT_SYMBOL(filemap_fault
);
1974 int generic_file_remap_pages(struct vm_area_struct
*vma
, unsigned long addr
,
1975 unsigned long size
, pgoff_t pgoff
)
1980 EXPORT_SYMBOL(generic_file_remap_pages
);
1982 static int __access_remote_vm(struct task_struct
*tsk
, struct mm_struct
*mm
,
1983 unsigned long addr
, void *buf
, int len
, int write
)
1985 struct vm_area_struct
*vma
;
1987 down_read(&mm
->mmap_sem
);
1989 /* the access must start within one of the target process's mappings */
1990 vma
= find_vma(mm
, addr
);
1992 /* don't overrun this mapping */
1993 if (addr
+ len
>= vma
->vm_end
)
1994 len
= vma
->vm_end
- addr
;
1996 /* only read or write mappings where it is permitted */
1997 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1998 copy_to_user_page(vma
, NULL
, addr
,
1999 (void *) addr
, buf
, len
);
2000 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
2001 copy_from_user_page(vma
, NULL
, addr
,
2002 buf
, (void *) addr
, len
);
2009 up_read(&mm
->mmap_sem
);
2015 * @access_remote_vm - access another process' address space
2016 * @mm: the mm_struct of the target address space
2017 * @addr: start address to access
2018 * @buf: source or destination buffer
2019 * @len: number of bytes to transfer
2020 * @write: whether the access is a write
2022 * The caller must hold a reference on @mm.
2024 int access_remote_vm(struct mm_struct
*mm
, unsigned long addr
,
2025 void *buf
, int len
, int write
)
2027 return __access_remote_vm(NULL
, mm
, addr
, buf
, len
, write
);
2031 * Access another process' address space.
2032 * - source/target buffer must be kernel space
2034 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
2036 struct mm_struct
*mm
;
2038 if (addr
+ len
< addr
)
2041 mm
= get_task_mm(tsk
);
2045 len
= __access_remote_vm(tsk
, mm
, addr
, buf
, len
, write
);
2052 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2053 * @inode: The inode to check
2054 * @size: The current filesize of the inode
2055 * @newsize: The proposed filesize of the inode
2057 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2058 * make sure that that any outstanding VMAs aren't broken and then shrink the
2059 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2060 * automatically grant mappings that are too large.
2062 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
2065 struct vm_area_struct
*vma
;
2066 struct vm_region
*region
;
2068 size_t r_size
, r_top
;
2070 low
= newsize
>> PAGE_SHIFT
;
2071 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2073 down_write(&nommu_region_sem
);
2074 mutex_lock(&inode
->i_mapping
->i_mmap_mutex
);
2076 /* search for VMAs that fall within the dead zone */
2077 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, low
, high
) {
2078 /* found one - only interested if it's shared out of the page
2080 if (vma
->vm_flags
& VM_SHARED
) {
2081 mutex_unlock(&inode
->i_mapping
->i_mmap_mutex
);
2082 up_write(&nommu_region_sem
);
2083 return -ETXTBSY
; /* not quite true, but near enough */
2087 /* reduce any regions that overlap the dead zone - if in existence,
2088 * these will be pointed to by VMAs that don't overlap the dead zone
2090 * we don't check for any regions that start beyond the EOF as there
2093 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
,
2095 if (!(vma
->vm_flags
& VM_SHARED
))
2098 region
= vma
->vm_region
;
2099 r_size
= region
->vm_top
- region
->vm_start
;
2100 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
2102 if (r_top
> newsize
) {
2103 region
->vm_top
-= r_top
- newsize
;
2104 if (region
->vm_end
> region
->vm_top
)
2105 region
->vm_end
= region
->vm_top
;
2109 mutex_unlock(&inode
->i_mapping
->i_mmap_mutex
);
2110 up_write(&nommu_region_sem
);