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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/export.h>
20 #include <linux/vmacache.h>
21 #include <linux/mman.h>
22 #include <linux/swap.h>
23 #include <linux/file.h>
24 #include <linux/highmem.h>
25 #include <linux/pagemap.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/blkdev.h>
29 #include <linux/backing-dev.h>
30 #include <linux/compiler.h>
31 #include <linux/mount.h>
32 #include <linux/personality.h>
33 #include <linux/security.h>
34 #include <linux/syscalls.h>
35 #include <linux/audit.h>
36 #include <linux/sched/sysctl.h>
37 #include <linux/printk.h>
39 #include <asm/uaccess.h>
41 #include <asm/tlbflush.h>
42 #include <asm/mmu_context.h>
46 EXPORT_SYMBOL(high_memory
);
48 unsigned long max_mapnr
;
49 EXPORT_SYMBOL(max_mapnr
);
50 unsigned long highest_memmap_pfn
;
51 struct percpu_counter vm_committed_as
;
52 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
53 int sysctl_overcommit_ratio
= 50; /* default is 50% */
54 unsigned long sysctl_overcommit_kbytes __read_mostly
;
55 int sysctl_max_map_count
= DEFAULT_MAX_MAP_COUNT
;
56 int sysctl_nr_trim_pages
= CONFIG_NOMMU_INITIAL_TRIM_EXCESS
;
57 unsigned long sysctl_user_reserve_kbytes __read_mostly
= 1UL << 17; /* 128MB */
58 unsigned long sysctl_admin_reserve_kbytes __read_mostly
= 1UL << 13; /* 8MB */
59 int heap_stack_gap
= 0;
61 atomic_long_t mmap_pages_allocated
;
64 * The global memory commitment made in the system can be a metric
65 * that can be used to drive ballooning decisions when Linux is hosted
66 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
67 * balancing memory across competing virtual machines that are hosted.
68 * Several metrics drive this policy engine including the guest reported
71 unsigned long vm_memory_committed(void)
73 return percpu_counter_read_positive(&vm_committed_as
);
76 EXPORT_SYMBOL_GPL(vm_memory_committed
);
78 EXPORT_SYMBOL(mem_map
);
80 /* list of mapped, potentially shareable regions */
81 static struct kmem_cache
*vm_region_jar
;
82 struct rb_root nommu_region_tree
= RB_ROOT
;
83 DECLARE_RWSEM(nommu_region_sem
);
85 const struct vm_operations_struct generic_file_vm_ops
= {
89 * Return the total memory allocated for this pointer, not
90 * just what the caller asked for.
92 * Doesn't have to be accurate, i.e. may have races.
94 unsigned int kobjsize(const void *objp
)
99 * If the object we have should not have ksize performed on it,
102 if (!objp
|| !virt_addr_valid(objp
))
105 page
= virt_to_head_page(objp
);
108 * If the allocator sets PageSlab, we know the pointer came from
115 * If it's not a compound page, see if we have a matching VMA
116 * region. This test is intentionally done in reverse order,
117 * so if there's no VMA, we still fall through and hand back
118 * PAGE_SIZE for 0-order pages.
120 if (!PageCompound(page
)) {
121 struct vm_area_struct
*vma
;
123 vma
= find_vma(current
->mm
, (unsigned long)objp
);
125 return vma
->vm_end
- vma
->vm_start
;
129 * The ksize() function is only guaranteed to work for pointers
130 * returned by kmalloc(). So handle arbitrary pointers here.
132 return PAGE_SIZE
<< compound_order(page
);
135 long __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
136 unsigned long start
, unsigned long nr_pages
,
137 unsigned int foll_flags
, struct page
**pages
,
138 struct vm_area_struct
**vmas
, int *nonblocking
)
140 struct vm_area_struct
*vma
;
141 unsigned long vm_flags
;
144 /* calculate required read or write permissions.
145 * If FOLL_FORCE is set, we only require the "MAY" flags.
147 vm_flags
= (foll_flags
& FOLL_WRITE
) ?
148 (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
149 vm_flags
&= (foll_flags
& FOLL_FORCE
) ?
150 (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
152 for (i
= 0; i
< nr_pages
; i
++) {
153 vma
= find_vma(mm
, start
);
155 goto finish_or_fault
;
157 /* protect what we can, including chardevs */
158 if ((vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)) ||
159 !(vm_flags
& vma
->vm_flags
))
160 goto finish_or_fault
;
163 pages
[i
] = virt_to_page(start
);
165 page_cache_get(pages
[i
]);
169 start
= (start
+ PAGE_SIZE
) & PAGE_MASK
;
175 return i
? : -EFAULT
;
179 * get a list of pages in an address range belonging to the specified process
180 * and indicate the VMA that covers each page
181 * - this is potentially dodgy as we may end incrementing the page count of a
182 * slab page or a secondary page from a compound page
183 * - don't permit access to VMAs that don't support it, such as I/O mappings
185 long get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
186 unsigned long start
, unsigned long nr_pages
,
187 unsigned int gup_flags
, struct page
**pages
,
188 struct vm_area_struct
**vmas
)
190 return __get_user_pages(tsk
, mm
, start
, nr_pages
,
191 gup_flags
, pages
, vmas
, NULL
);
193 EXPORT_SYMBOL(get_user_pages
);
195 long get_user_pages_locked(struct task_struct
*tsk
, struct mm_struct
*mm
,
196 unsigned long start
, unsigned long nr_pages
,
197 unsigned int gup_flags
, struct page
**pages
,
200 return get_user_pages(tsk
, mm
, start
, nr_pages
, gup_flags
,
203 EXPORT_SYMBOL(get_user_pages_locked
);
205 long __get_user_pages_unlocked(struct task_struct
*tsk
, struct mm_struct
*mm
,
206 unsigned long start
, unsigned long nr_pages
,
207 struct page
**pages
, unsigned int gup_flags
)
210 down_read(&mm
->mmap_sem
);
211 ret
= __get_user_pages(tsk
, mm
, start
, nr_pages
, gup_flags
, pages
,
213 up_read(&mm
->mmap_sem
);
216 EXPORT_SYMBOL(__get_user_pages_unlocked
);
218 long get_user_pages_unlocked(struct task_struct
*tsk
, struct mm_struct
*mm
,
219 unsigned long start
, unsigned long nr_pages
,
220 struct page
**pages
, unsigned int gup_flags
)
222 return __get_user_pages_unlocked(tsk
, mm
, start
, nr_pages
,
225 EXPORT_SYMBOL(get_user_pages_unlocked
);
228 * follow_pfn - look up PFN at a user virtual address
229 * @vma: memory mapping
230 * @address: user virtual address
231 * @pfn: location to store found PFN
233 * Only IO mappings and raw PFN mappings are allowed.
235 * Returns zero and the pfn at @pfn on success, -ve otherwise.
237 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
240 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
243 *pfn
= address
>> PAGE_SHIFT
;
246 EXPORT_SYMBOL(follow_pfn
);
248 LIST_HEAD(vmap_area_list
);
250 void vfree(const void *addr
)
254 EXPORT_SYMBOL(vfree
);
256 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
259 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
260 * returns only a logical address.
262 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
264 EXPORT_SYMBOL(__vmalloc
);
266 void *vmalloc_user(unsigned long size
)
270 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
273 struct vm_area_struct
*vma
;
275 down_write(¤t
->mm
->mmap_sem
);
276 vma
= find_vma(current
->mm
, (unsigned long)ret
);
278 vma
->vm_flags
|= VM_USERMAP
;
279 up_write(¤t
->mm
->mmap_sem
);
284 EXPORT_SYMBOL(vmalloc_user
);
286 struct page
*vmalloc_to_page(const void *addr
)
288 return virt_to_page(addr
);
290 EXPORT_SYMBOL(vmalloc_to_page
);
292 unsigned long vmalloc_to_pfn(const void *addr
)
294 return page_to_pfn(virt_to_page(addr
));
296 EXPORT_SYMBOL(vmalloc_to_pfn
);
298 long vread(char *buf
, char *addr
, unsigned long count
)
300 /* Don't allow overflow */
301 if ((unsigned long) buf
+ count
< count
)
302 count
= -(unsigned long) buf
;
304 memcpy(buf
, addr
, count
);
308 long vwrite(char *buf
, char *addr
, unsigned long count
)
310 /* Don't allow overflow */
311 if ((unsigned long) addr
+ count
< count
)
312 count
= -(unsigned long) addr
;
314 memcpy(addr
, buf
, count
);
319 * vmalloc - allocate virtually contiguous memory
321 * @size: allocation size
323 * Allocate enough pages to cover @size from the page level
324 * allocator and map them into contiguous kernel virtual space.
326 * For tight control over page level allocator and protection flags
327 * use __vmalloc() instead.
329 void *vmalloc(unsigned long size
)
331 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
333 EXPORT_SYMBOL(vmalloc
);
336 * vzalloc - allocate virtually contiguous memory with zero fill
338 * @size: allocation size
340 * Allocate enough pages to cover @size from the page level
341 * allocator and map them into contiguous kernel virtual space.
342 * The memory allocated is set to zero.
344 * For tight control over page level allocator and protection flags
345 * use __vmalloc() instead.
347 void *vzalloc(unsigned long size
)
349 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
352 EXPORT_SYMBOL(vzalloc
);
355 * vmalloc_node - allocate memory on a specific node
356 * @size: allocation size
359 * Allocate enough pages to cover @size from the page level
360 * allocator and map them into contiguous kernel virtual space.
362 * For tight control over page level allocator and protection flags
363 * use __vmalloc() instead.
365 void *vmalloc_node(unsigned long size
, int node
)
367 return vmalloc(size
);
369 EXPORT_SYMBOL(vmalloc_node
);
372 * vzalloc_node - allocate memory on a specific node with zero fill
373 * @size: allocation size
376 * Allocate enough pages to cover @size from the page level
377 * allocator and map them into contiguous kernel virtual space.
378 * The memory allocated is set to zero.
380 * For tight control over page level allocator and protection flags
381 * use __vmalloc() instead.
383 void *vzalloc_node(unsigned long size
, int node
)
385 return vzalloc(size
);
387 EXPORT_SYMBOL(vzalloc_node
);
389 #ifndef PAGE_KERNEL_EXEC
390 # define PAGE_KERNEL_EXEC PAGE_KERNEL
394 * vmalloc_exec - allocate virtually contiguous, executable memory
395 * @size: allocation size
397 * Kernel-internal function to allocate enough pages to cover @size
398 * the page level allocator and map them into contiguous and
399 * executable kernel virtual space.
401 * For tight control over page level allocator and protection flags
402 * use __vmalloc() instead.
405 void *vmalloc_exec(unsigned long size
)
407 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
411 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
412 * @size: allocation size
414 * Allocate enough 32bit PA addressable pages to cover @size from the
415 * page level allocator and map them into contiguous kernel virtual space.
417 void *vmalloc_32(unsigned long size
)
419 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
421 EXPORT_SYMBOL(vmalloc_32
);
424 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
425 * @size: allocation size
427 * The resulting memory area is 32bit addressable and zeroed so it can be
428 * mapped to userspace without leaking data.
430 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
431 * remap_vmalloc_range() are permissible.
433 void *vmalloc_32_user(unsigned long size
)
436 * We'll have to sort out the ZONE_DMA bits for 64-bit,
437 * but for now this can simply use vmalloc_user() directly.
439 return vmalloc_user(size
);
441 EXPORT_SYMBOL(vmalloc_32_user
);
443 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
450 void vunmap(const void *addr
)
454 EXPORT_SYMBOL(vunmap
);
456 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
, pgprot_t prot
)
461 EXPORT_SYMBOL(vm_map_ram
);
463 void vm_unmap_ram(const void *mem
, unsigned int count
)
467 EXPORT_SYMBOL(vm_unmap_ram
);
469 void vm_unmap_aliases(void)
472 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
475 * Implement a stub for vmalloc_sync_[un]mapping() if the architecture
476 * chose not to have one.
478 void __weak
vmalloc_sync_mappings(void)
482 void __weak
vmalloc_sync_unmappings(void)
487 * alloc_vm_area - allocate a range of kernel address space
488 * @size: size of the area
490 * Returns: NULL on failure, vm_struct on success
492 * This function reserves a range of kernel address space, and
493 * allocates pagetables to map that range. No actual mappings
494 * are created. If the kernel address space is not shared
495 * between processes, it syncs the pagetable across all
498 struct vm_struct
*alloc_vm_area(size_t size
, pte_t
**ptes
)
503 EXPORT_SYMBOL_GPL(alloc_vm_area
);
505 void free_vm_area(struct vm_struct
*area
)
509 EXPORT_SYMBOL_GPL(free_vm_area
);
511 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
516 EXPORT_SYMBOL(vm_insert_page
);
519 * sys_brk() for the most part doesn't need the global kernel
520 * lock, except when an application is doing something nasty
521 * like trying to un-brk an area that has already been mapped
522 * to a regular file. in this case, the unmapping will need
523 * to invoke file system routines that need the global lock.
525 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
527 struct mm_struct
*mm
= current
->mm
;
529 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
536 * Always allow shrinking brk
538 if (brk
<= mm
->brk
) {
544 * Ok, looks good - let it rip.
546 flush_icache_range(mm
->brk
, brk
);
547 return mm
->brk
= brk
;
551 * initialise the VMA and region record slabs
553 void __init
mmap_init(void)
557 ret
= percpu_counter_init(&vm_committed_as
, 0, GFP_KERNEL
);
559 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
);
563 * validate the region tree
564 * - the caller must hold the region lock
566 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
567 static noinline
void validate_nommu_regions(void)
569 struct vm_region
*region
, *last
;
570 struct rb_node
*p
, *lastp
;
572 lastp
= rb_first(&nommu_region_tree
);
576 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
577 BUG_ON(last
->vm_end
<= last
->vm_start
);
578 BUG_ON(last
->vm_top
< last
->vm_end
);
580 while ((p
= rb_next(lastp
))) {
581 region
= rb_entry(p
, struct vm_region
, vm_rb
);
582 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
584 BUG_ON(region
->vm_end
<= region
->vm_start
);
585 BUG_ON(region
->vm_top
< region
->vm_end
);
586 BUG_ON(region
->vm_start
< last
->vm_top
);
592 static void validate_nommu_regions(void)
598 * add a region into the global tree
600 static void add_nommu_region(struct vm_region
*region
)
602 struct vm_region
*pregion
;
603 struct rb_node
**p
, *parent
;
605 validate_nommu_regions();
608 p
= &nommu_region_tree
.rb_node
;
611 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
612 if (region
->vm_start
< pregion
->vm_start
)
614 else if (region
->vm_start
> pregion
->vm_start
)
616 else if (pregion
== region
)
622 rb_link_node(®ion
->vm_rb
, parent
, p
);
623 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
625 validate_nommu_regions();
629 * delete a region from the global tree
631 static void delete_nommu_region(struct vm_region
*region
)
633 BUG_ON(!nommu_region_tree
.rb_node
);
635 validate_nommu_regions();
636 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
637 validate_nommu_regions();
641 * free a contiguous series of pages
643 static void free_page_series(unsigned long from
, unsigned long to
)
645 for (; from
< to
; from
+= PAGE_SIZE
) {
646 struct page
*page
= virt_to_page(from
);
648 atomic_long_dec(&mmap_pages_allocated
);
654 * release a reference to a region
655 * - the caller must hold the region semaphore for writing, which this releases
656 * - the region may not have been added to the tree yet, in which case vm_top
657 * will equal vm_start
659 static void __put_nommu_region(struct vm_region
*region
)
660 __releases(nommu_region_sem
)
662 BUG_ON(!nommu_region_tree
.rb_node
);
664 if (--region
->vm_usage
== 0) {
665 if (region
->vm_top
> region
->vm_start
)
666 delete_nommu_region(region
);
667 up_write(&nommu_region_sem
);
670 fput(region
->vm_file
);
672 /* IO memory and memory shared directly out of the pagecache
673 * from ramfs/tmpfs mustn't be released here */
674 if (region
->vm_flags
& VM_MAPPED_COPY
)
675 free_page_series(region
->vm_start
, region
->vm_top
);
676 kmem_cache_free(vm_region_jar
, region
);
678 up_write(&nommu_region_sem
);
683 * release a reference to a region
685 static void put_nommu_region(struct vm_region
*region
)
687 down_write(&nommu_region_sem
);
688 __put_nommu_region(region
);
692 * update protection on a vma
694 static void protect_vma(struct vm_area_struct
*vma
, unsigned long flags
)
697 struct mm_struct
*mm
= vma
->vm_mm
;
698 long start
= vma
->vm_start
& PAGE_MASK
;
699 while (start
< vma
->vm_end
) {
700 protect_page(mm
, start
, flags
);
703 update_protections(mm
);
708 * add a VMA into a process's mm_struct in the appropriate place in the list
709 * and tree and add to the address space's page tree also if not an anonymous
711 * - should be called with mm->mmap_sem held writelocked
713 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
715 struct vm_area_struct
*pvma
, *prev
;
716 struct address_space
*mapping
;
717 struct rb_node
**p
, *parent
, *rb_prev
;
719 BUG_ON(!vma
->vm_region
);
724 protect_vma(vma
, vma
->vm_flags
);
726 /* add the VMA to the mapping */
728 mapping
= vma
->vm_file
->f_mapping
;
730 i_mmap_lock_write(mapping
);
731 flush_dcache_mmap_lock(mapping
);
732 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
733 flush_dcache_mmap_unlock(mapping
);
734 i_mmap_unlock_write(mapping
);
737 /* add the VMA to the tree */
738 parent
= rb_prev
= NULL
;
739 p
= &mm
->mm_rb
.rb_node
;
742 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
744 /* sort by: start addr, end addr, VMA struct addr in that order
745 * (the latter is necessary as we may get identical VMAs) */
746 if (vma
->vm_start
< pvma
->vm_start
)
748 else if (vma
->vm_start
> pvma
->vm_start
) {
751 } else if (vma
->vm_end
< pvma
->vm_end
)
753 else if (vma
->vm_end
> pvma
->vm_end
) {
756 } else if (vma
< pvma
)
758 else if (vma
> pvma
) {
765 rb_link_node(&vma
->vm_rb
, parent
, p
);
766 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
768 /* add VMA to the VMA list also */
771 prev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
773 __vma_link_list(mm
, vma
, prev
, parent
);
777 * delete a VMA from its owning mm_struct and address space
779 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
782 struct address_space
*mapping
;
783 struct mm_struct
*mm
= vma
->vm_mm
;
784 struct task_struct
*curr
= current
;
789 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
790 /* if the vma is cached, invalidate the entire cache */
791 if (curr
->vmacache
[i
] == vma
) {
792 vmacache_invalidate(mm
);
797 /* remove the VMA from the mapping */
799 mapping
= vma
->vm_file
->f_mapping
;
801 i_mmap_lock_write(mapping
);
802 flush_dcache_mmap_lock(mapping
);
803 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
804 flush_dcache_mmap_unlock(mapping
);
805 i_mmap_unlock_write(mapping
);
808 /* remove from the MM's tree and list */
809 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
812 vma
->vm_prev
->vm_next
= vma
->vm_next
;
814 mm
->mmap
= vma
->vm_next
;
817 vma
->vm_next
->vm_prev
= vma
->vm_prev
;
821 * destroy a VMA record
823 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
825 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
826 vma
->vm_ops
->close(vma
);
829 put_nommu_region(vma
->vm_region
);
830 kmem_cache_free(vm_area_cachep
, vma
);
834 * look up the first VMA in which addr resides, NULL if none
835 * - should be called with mm->mmap_sem at least held readlocked
837 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
839 struct vm_area_struct
*vma
;
841 /* check the cache first */
842 vma
= vmacache_find(mm
, addr
);
846 /* trawl the list (there may be multiple mappings in which addr
848 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
849 if (vma
->vm_start
> addr
)
851 if (vma
->vm_end
> addr
) {
852 vmacache_update(addr
, vma
);
859 EXPORT_SYMBOL(find_vma
);
863 * - we don't extend stack VMAs under NOMMU conditions
865 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
867 return find_vma(mm
, addr
);
871 * expand a stack to a given address
872 * - not supported under NOMMU conditions
874 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
880 * look up the first VMA exactly that exactly matches addr
881 * - should be called with mm->mmap_sem at least held readlocked
883 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
887 struct vm_area_struct
*vma
;
888 unsigned long end
= addr
+ len
;
890 /* check the cache first */
891 vma
= vmacache_find_exact(mm
, addr
, end
);
895 /* trawl the list (there may be multiple mappings in which addr
897 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
898 if (vma
->vm_start
< addr
)
900 if (vma
->vm_start
> addr
)
902 if (vma
->vm_end
== end
) {
903 vmacache_update(addr
, vma
);
912 * determine whether a mapping should be permitted and, if so, what sort of
913 * mapping we're capable of supporting
915 static int validate_mmap_request(struct file
*file
,
921 unsigned long *_capabilities
)
923 unsigned long capabilities
, rlen
;
926 /* do the simple checks first */
927 if (flags
& MAP_FIXED
)
930 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
931 (flags
& MAP_TYPE
) != MAP_SHARED
)
937 /* Careful about overflows.. */
938 rlen
= PAGE_ALIGN(len
);
939 if (!rlen
|| rlen
> TASK_SIZE
)
942 /* offset overflow? */
943 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
947 /* files must support mmap */
948 if (!file
->f_op
->mmap
)
951 /* work out if what we've got could possibly be shared
952 * - we support chardevs that provide their own "memory"
953 * - we support files/blockdevs that are memory backed
955 if (file
->f_op
->mmap_capabilities
) {
956 capabilities
= file
->f_op
->mmap_capabilities(file
);
958 /* no explicit capabilities set, so assume some
960 switch (file_inode(file
)->i_mode
& S_IFMT
) {
963 capabilities
= NOMMU_MAP_COPY
;
978 /* eliminate any capabilities that we can't support on this
980 if (!file
->f_op
->get_unmapped_area
)
981 capabilities
&= ~NOMMU_MAP_DIRECT
;
982 if (!(file
->f_mode
& FMODE_CAN_READ
))
983 capabilities
&= ~NOMMU_MAP_COPY
;
985 /* The file shall have been opened with read permission. */
986 if (!(file
->f_mode
& FMODE_READ
))
989 if (flags
& MAP_SHARED
) {
990 /* do checks for writing, appending and locking */
991 if ((prot
& PROT_WRITE
) &&
992 !(file
->f_mode
& FMODE_WRITE
))
995 if (IS_APPEND(file_inode(file
)) &&
996 (file
->f_mode
& FMODE_WRITE
))
999 if (locks_verify_locked(file
))
1002 if (!(capabilities
& NOMMU_MAP_DIRECT
))
1005 /* we mustn't privatise shared mappings */
1006 capabilities
&= ~NOMMU_MAP_COPY
;
1008 /* we're going to read the file into private memory we
1010 if (!(capabilities
& NOMMU_MAP_COPY
))
1013 /* we don't permit a private writable mapping to be
1014 * shared with the backing device */
1015 if (prot
& PROT_WRITE
)
1016 capabilities
&= ~NOMMU_MAP_DIRECT
;
1019 if (capabilities
& NOMMU_MAP_DIRECT
) {
1020 if (((prot
& PROT_READ
) && !(capabilities
& NOMMU_MAP_READ
)) ||
1021 ((prot
& PROT_WRITE
) && !(capabilities
& NOMMU_MAP_WRITE
)) ||
1022 ((prot
& PROT_EXEC
) && !(capabilities
& NOMMU_MAP_EXEC
))
1024 capabilities
&= ~NOMMU_MAP_DIRECT
;
1025 if (flags
& MAP_SHARED
) {
1026 pr_warn("MAP_SHARED not completely supported on !MMU\n");
1032 /* handle executable mappings and implied executable
1034 if (path_noexec(&file
->f_path
)) {
1035 if (prot
& PROT_EXEC
)
1037 } else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
1038 /* handle implication of PROT_EXEC by PROT_READ */
1039 if (current
->personality
& READ_IMPLIES_EXEC
) {
1040 if (capabilities
& NOMMU_MAP_EXEC
)
1043 } else if ((prot
& PROT_READ
) &&
1044 (prot
& PROT_EXEC
) &&
1045 !(capabilities
& NOMMU_MAP_EXEC
)
1047 /* backing file is not executable, try to copy */
1048 capabilities
&= ~NOMMU_MAP_DIRECT
;
1051 /* anonymous mappings are always memory backed and can be
1054 capabilities
= NOMMU_MAP_COPY
;
1056 /* handle PROT_EXEC implication by PROT_READ */
1057 if ((prot
& PROT_READ
) &&
1058 (current
->personality
& READ_IMPLIES_EXEC
))
1062 /* allow the security API to have its say */
1063 ret
= security_mmap_addr(addr
);
1068 *_capabilities
= capabilities
;
1073 * we've determined that we can make the mapping, now translate what we
1074 * now know into VMA flags
1076 static unsigned long determine_vm_flags(struct file
*file
,
1078 unsigned long flags
,
1079 unsigned long capabilities
)
1081 unsigned long vm_flags
;
1083 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
1084 /* vm_flags |= mm->def_flags; */
1086 if (!(capabilities
& NOMMU_MAP_DIRECT
)) {
1087 /* attempt to share read-only copies of mapped file chunks */
1088 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1089 if (file
&& !(prot
& PROT_WRITE
))
1090 vm_flags
|= VM_MAYSHARE
;
1092 /* overlay a shareable mapping on the backing device or inode
1093 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1095 vm_flags
|= VM_MAYSHARE
| (capabilities
& NOMMU_VMFLAGS
);
1096 if (flags
& MAP_SHARED
)
1097 vm_flags
|= VM_SHARED
;
1100 /* refuse to let anyone share private mappings with this process if
1101 * it's being traced - otherwise breakpoints set in it may interfere
1102 * with another untraced process
1104 if ((flags
& MAP_PRIVATE
) && current
->ptrace
)
1105 vm_flags
&= ~VM_MAYSHARE
;
1111 * set up a shared mapping on a file (the driver or filesystem provides and
1114 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1118 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1120 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1126 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1127 * opposed to tried but failed) so we can only give a suitable error as
1128 * it's not possible to make a private copy if MAP_SHARED was given */
1133 * set up a private mapping or an anonymous shared mapping
1135 static int do_mmap_private(struct vm_area_struct
*vma
,
1136 struct vm_region
*region
,
1138 unsigned long capabilities
)
1140 unsigned long total
, point
;
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
& NOMMU_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
);
1171 point
= len
>> PAGE_SHIFT
;
1173 /* we don't want to allocate a power-of-2 sized page set */
1174 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
)
1177 base
= alloc_pages_exact(total
<< PAGE_SHIFT
, GFP_KERNEL
);
1181 atomic_long_add(total
, &mmap_pages_allocated
);
1183 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1184 region
->vm_start
= (unsigned long) base
;
1185 region
->vm_end
= region
->vm_start
+ len
;
1186 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1188 vma
->vm_start
= region
->vm_start
;
1189 vma
->vm_end
= region
->vm_start
+ len
;
1192 /* read the contents of a file into the copy */
1193 mm_segment_t old_fs
;
1196 fpos
= vma
->vm_pgoff
;
1197 fpos
<<= PAGE_SHIFT
;
1201 ret
= __vfs_read(vma
->vm_file
, base
, len
, &fpos
);
1207 /* clear the last little bit */
1209 memset(base
+ ret
, 0, len
- ret
);
1216 free_page_series(region
->vm_start
, region
->vm_top
);
1217 region
->vm_start
= vma
->vm_start
= 0;
1218 region
->vm_end
= vma
->vm_end
= 0;
1223 pr_err("Allocation of length %lu from process %d (%s) failed\n",
1224 len
, current
->pid
, current
->comm
);
1230 * handle mapping creation for uClinux
1232 unsigned long do_mmap(struct file
*file
,
1236 unsigned long flags
,
1237 vm_flags_t vm_flags
,
1238 unsigned long pgoff
,
1239 unsigned long *populate
)
1241 struct vm_area_struct
*vma
;
1242 struct vm_region
*region
;
1244 unsigned long capabilities
, result
;
1249 /* decide whether we should attempt the mapping, and if so what sort of
1251 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1256 /* we ignore the address hint */
1258 len
= PAGE_ALIGN(len
);
1260 /* we've determined that we can make the mapping, now translate what we
1261 * now know into VMA flags */
1262 vm_flags
|= determine_vm_flags(file
, prot
, flags
, capabilities
);
1264 /* we're going to need to record the mapping */
1265 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1267 goto error_getting_region
;
1269 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1271 goto error_getting_vma
;
1273 region
->vm_usage
= 1;
1274 region
->vm_flags
= vm_flags
;
1275 region
->vm_pgoff
= pgoff
;
1277 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1278 vma
->vm_flags
= vm_flags
;
1279 vma
->vm_pgoff
= pgoff
;
1282 region
->vm_file
= get_file(file
);
1283 vma
->vm_file
= get_file(file
);
1286 down_write(&nommu_region_sem
);
1288 /* if we want to share, we need to check for regions created by other
1289 * mmap() calls that overlap with our proposed mapping
1290 * - we can only share with a superset match on most regular files
1291 * - shared mappings on character devices and memory backed files are
1292 * permitted to overlap inexactly as far as we are concerned for in
1293 * these cases, sharing is handled in the driver or filesystem rather
1296 if (vm_flags
& VM_MAYSHARE
) {
1297 struct vm_region
*pregion
;
1298 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1300 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1301 pgend
= pgoff
+ pglen
;
1303 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1304 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1306 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1309 /* search for overlapping mappings on the same file */
1310 if (file_inode(pregion
->vm_file
) !=
1314 if (pregion
->vm_pgoff
>= pgend
)
1317 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1318 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1319 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1320 if (pgoff
>= rpgend
)
1323 /* handle inexactly overlapping matches between
1325 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1326 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1327 /* new mapping is not a subset of the region */
1328 if (!(capabilities
& NOMMU_MAP_DIRECT
))
1329 goto sharing_violation
;
1333 /* we've found a region we can share */
1334 pregion
->vm_usage
++;
1335 vma
->vm_region
= pregion
;
1336 start
= pregion
->vm_start
;
1337 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1338 vma
->vm_start
= start
;
1339 vma
->vm_end
= start
+ len
;
1341 if (pregion
->vm_flags
& VM_MAPPED_COPY
)
1342 vma
->vm_flags
|= VM_MAPPED_COPY
;
1344 ret
= do_mmap_shared_file(vma
);
1346 vma
->vm_region
= NULL
;
1349 pregion
->vm_usage
--;
1351 goto error_just_free
;
1354 fput(region
->vm_file
);
1355 kmem_cache_free(vm_region_jar
, region
);
1361 /* obtain the address at which to make a shared mapping
1362 * - this is the hook for quasi-memory character devices to
1363 * tell us the location of a shared mapping
1365 if (capabilities
& NOMMU_MAP_DIRECT
) {
1366 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1368 if (IS_ERR_VALUE(addr
)) {
1371 goto error_just_free
;
1373 /* the driver refused to tell us where to site
1374 * the mapping so we'll have to attempt to copy
1377 if (!(capabilities
& NOMMU_MAP_COPY
))
1378 goto error_just_free
;
1380 capabilities
&= ~NOMMU_MAP_DIRECT
;
1382 vma
->vm_start
= region
->vm_start
= addr
;
1383 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1388 vma
->vm_region
= region
;
1390 /* set up the mapping
1391 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1393 if (file
&& vma
->vm_flags
& VM_SHARED
)
1394 ret
= do_mmap_shared_file(vma
);
1396 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1398 goto error_just_free
;
1399 add_nommu_region(region
);
1401 /* clear anonymous mappings that don't ask for uninitialized data */
1402 if (!vma
->vm_file
&& !(flags
& MAP_UNINITIALIZED
))
1403 memset((void *)region
->vm_start
, 0,
1404 region
->vm_end
- region
->vm_start
);
1406 /* okay... we have a mapping; now we have to register it */
1407 result
= vma
->vm_start
;
1409 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1412 add_vma_to_mm(current
->mm
, vma
);
1414 /* we flush the region from the icache only when the first executable
1415 * mapping of it is made */
1416 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1417 flush_icache_range(region
->vm_start
, region
->vm_end
);
1418 region
->vm_icache_flushed
= true;
1421 up_write(&nommu_region_sem
);
1426 up_write(&nommu_region_sem
);
1428 if (region
->vm_file
)
1429 fput(region
->vm_file
);
1430 kmem_cache_free(vm_region_jar
, region
);
1433 kmem_cache_free(vm_area_cachep
, vma
);
1437 up_write(&nommu_region_sem
);
1438 pr_warn("Attempt to share mismatched mappings\n");
1443 kmem_cache_free(vm_region_jar
, region
);
1444 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1449 error_getting_region
:
1450 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
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 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1480 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1481 struct mmap_arg_struct
{
1485 unsigned long flags
;
1487 unsigned long offset
;
1490 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1492 struct mmap_arg_struct a
;
1494 if (copy_from_user(&a
, arg
, sizeof(a
)))
1496 if (offset_in_page(a
.offset
))
1499 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1500 a
.offset
>> PAGE_SHIFT
);
1502 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1505 * split a vma into two pieces at address 'addr', a new vma is allocated either
1506 * for the first part or the tail.
1508 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1509 unsigned long addr
, int new_below
)
1511 struct vm_area_struct
*new;
1512 struct vm_region
*region
;
1513 unsigned long npages
;
1515 /* we're only permitted to split anonymous regions (these should have
1516 * only a single usage on the region) */
1520 if (mm
->map_count
>= sysctl_max_map_count
)
1523 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1527 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
1529 kmem_cache_free(vm_region_jar
, region
);
1533 /* most fields are the same, copy all, and then fixup */
1535 *region
= *vma
->vm_region
;
1536 new->vm_region
= region
;
1538 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1541 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1543 region
->vm_start
= new->vm_start
= addr
;
1544 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1547 if (new->vm_ops
&& new->vm_ops
->open
)
1548 new->vm_ops
->open(new);
1550 delete_vma_from_mm(vma
);
1551 down_write(&nommu_region_sem
);
1552 delete_nommu_region(vma
->vm_region
);
1554 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1555 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1557 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1558 vma
->vm_region
->vm_top
= addr
;
1560 add_nommu_region(vma
->vm_region
);
1561 add_nommu_region(new->vm_region
);
1562 up_write(&nommu_region_sem
);
1563 add_vma_to_mm(mm
, vma
);
1564 add_vma_to_mm(mm
, new);
1569 * shrink a VMA by removing the specified chunk from either the beginning or
1572 static int shrink_vma(struct mm_struct
*mm
,
1573 struct vm_area_struct
*vma
,
1574 unsigned long from
, unsigned long to
)
1576 struct vm_region
*region
;
1578 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1580 delete_vma_from_mm(vma
);
1581 if (from
> vma
->vm_start
)
1585 add_vma_to_mm(mm
, vma
);
1587 /* cut the backing region down to size */
1588 region
= vma
->vm_region
;
1589 BUG_ON(region
->vm_usage
!= 1);
1591 down_write(&nommu_region_sem
);
1592 delete_nommu_region(region
);
1593 if (from
> region
->vm_start
) {
1594 to
= region
->vm_top
;
1595 region
->vm_top
= region
->vm_end
= from
;
1597 region
->vm_start
= to
;
1599 add_nommu_region(region
);
1600 up_write(&nommu_region_sem
);
1602 free_page_series(from
, to
);
1608 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1609 * VMA, though it need not cover the whole VMA
1611 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1613 struct vm_area_struct
*vma
;
1617 len
= PAGE_ALIGN(len
);
1623 /* find the first potentially overlapping VMA */
1624 vma
= find_vma(mm
, start
);
1628 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1629 current
->pid
, current
->comm
,
1630 start
, start
+ len
- 1);
1636 /* we're allowed to split an anonymous VMA but not a file-backed one */
1639 if (start
> vma
->vm_start
)
1641 if (end
== vma
->vm_end
)
1642 goto erase_whole_vma
;
1647 /* the chunk must be a subset of the VMA found */
1648 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1649 goto erase_whole_vma
;
1650 if (start
< vma
->vm_start
|| end
> vma
->vm_end
)
1652 if (offset_in_page(start
))
1654 if (end
!= vma
->vm_end
&& offset_in_page(end
))
1656 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1657 ret
= split_vma(mm
, vma
, start
, 1);
1661 return shrink_vma(mm
, vma
, start
, end
);
1665 delete_vma_from_mm(vma
);
1666 delete_vma(mm
, vma
);
1669 EXPORT_SYMBOL(do_munmap
);
1671 int vm_munmap(unsigned long addr
, size_t len
)
1673 struct mm_struct
*mm
= current
->mm
;
1676 down_write(&mm
->mmap_sem
);
1677 ret
= do_munmap(mm
, addr
, len
);
1678 up_write(&mm
->mmap_sem
);
1681 EXPORT_SYMBOL(vm_munmap
);
1683 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1685 return vm_munmap(addr
, len
);
1689 * release all the mappings made in a process's VM space
1691 void exit_mmap(struct mm_struct
*mm
)
1693 struct vm_area_struct
*vma
;
1700 while ((vma
= mm
->mmap
)) {
1701 mm
->mmap
= vma
->vm_next
;
1702 delete_vma_from_mm(vma
);
1703 delete_vma(mm
, vma
);
1708 unsigned long vm_brk(unsigned long addr
, unsigned long len
)
1714 * expand (or shrink) an existing mapping, potentially moving it at the same
1715 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1717 * under NOMMU conditions, we only permit changing a mapping's size, and only
1718 * as long as it stays within the region allocated by do_mmap_private() and the
1719 * block is not shareable
1721 * MREMAP_FIXED is not supported under NOMMU conditions
1723 static unsigned long do_mremap(unsigned long addr
,
1724 unsigned long old_len
, unsigned long new_len
,
1725 unsigned long flags
, unsigned long new_addr
)
1727 struct vm_area_struct
*vma
;
1729 /* insanity checks first */
1730 old_len
= PAGE_ALIGN(old_len
);
1731 new_len
= PAGE_ALIGN(new_len
);
1732 if (old_len
== 0 || new_len
== 0)
1733 return (unsigned long) -EINVAL
;
1735 if (offset_in_page(addr
))
1738 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1739 return (unsigned long) -EINVAL
;
1741 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1743 return (unsigned long) -EINVAL
;
1745 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1746 return (unsigned long) -EFAULT
;
1748 if (vma
->vm_flags
& VM_MAYSHARE
)
1749 return (unsigned long) -EPERM
;
1751 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1752 return (unsigned long) -ENOMEM
;
1754 /* all checks complete - do it */
1755 vma
->vm_end
= vma
->vm_start
+ new_len
;
1756 return vma
->vm_start
;
1759 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1760 unsigned long, new_len
, unsigned long, flags
,
1761 unsigned long, new_addr
)
1765 down_write(¤t
->mm
->mmap_sem
);
1766 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1767 up_write(¤t
->mm
->mmap_sem
);
1771 struct page
*follow_page_mask(struct vm_area_struct
*vma
,
1772 unsigned long address
, unsigned int flags
,
1773 unsigned int *page_mask
)
1779 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long addr
,
1780 unsigned long pfn
, unsigned long size
, pgprot_t prot
)
1782 if (addr
!= (pfn
<< PAGE_SHIFT
))
1785 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
1788 EXPORT_SYMBOL(remap_pfn_range
);
1790 int vm_iomap_memory(struct vm_area_struct
*vma
, phys_addr_t start
, unsigned long len
)
1792 unsigned long pfn
= start
>> PAGE_SHIFT
;
1793 unsigned long vm_len
= vma
->vm_end
- vma
->vm_start
;
1795 pfn
+= vma
->vm_pgoff
;
1796 return io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, vm_len
, vma
->vm_page_prot
);
1798 EXPORT_SYMBOL(vm_iomap_memory
);
1800 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1801 unsigned long pgoff
)
1803 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1805 if (!(vma
->vm_flags
& VM_USERMAP
))
1808 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1809 vma
->vm_end
= vma
->vm_start
+ size
;
1813 EXPORT_SYMBOL(remap_vmalloc_range
);
1815 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1816 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1821 void unmap_mapping_range(struct address_space
*mapping
,
1822 loff_t
const holebegin
, loff_t
const holelen
,
1826 EXPORT_SYMBOL(unmap_mapping_range
);
1829 * Check that a process has enough memory to allocate a new virtual
1830 * mapping. 0 means there is enough memory for the allocation to
1831 * succeed and -ENOMEM implies there is not.
1833 * We currently support three overcommit policies, which are set via the
1834 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1836 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1837 * Additional code 2002 Jul 20 by Robert Love.
1839 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1841 * Note this is a helper function intended to be used by LSMs which
1842 * wish to use this logic.
1844 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1846 long free
, allowed
, reserve
;
1848 vm_acct_memory(pages
);
1851 * Sometimes we want to use more memory than we have
1853 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1856 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1857 free
= global_page_state(NR_FREE_PAGES
);
1858 free
+= global_page_state(NR_FILE_PAGES
);
1861 * shmem pages shouldn't be counted as free in this
1862 * case, they can't be purged, only swapped out, and
1863 * that won't affect the overall amount of available
1864 * memory in the system.
1866 free
-= global_page_state(NR_SHMEM
);
1868 free
+= get_nr_swap_pages();
1871 * Any slabs which are created with the
1872 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1873 * which are reclaimable, under pressure. The dentry
1874 * cache and most inode caches should fall into this
1876 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1879 * Leave reserved pages. The pages are not for anonymous pages.
1881 if (free
<= totalreserve_pages
)
1884 free
-= totalreserve_pages
;
1887 * Reserve some for root
1890 free
-= sysctl_admin_reserve_kbytes
>> (PAGE_SHIFT
- 10);
1898 allowed
= vm_commit_limit();
1900 * Reserve some 3% for root
1903 allowed
-= sysctl_admin_reserve_kbytes
>> (PAGE_SHIFT
- 10);
1906 * Don't let a single process grow so big a user can't recover
1909 reserve
= sysctl_user_reserve_kbytes
>> (PAGE_SHIFT
- 10);
1910 allowed
-= min_t(long, mm
->total_vm
/ 32, reserve
);
1913 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
1917 vm_unacct_memory(pages
);
1922 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1927 EXPORT_SYMBOL(filemap_fault
);
1929 void filemap_map_pages(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1933 EXPORT_SYMBOL(filemap_map_pages
);
1935 static int __access_remote_vm(struct task_struct
*tsk
, struct mm_struct
*mm
,
1936 unsigned long addr
, void *buf
, int len
, unsigned int gup_flags
)
1938 struct vm_area_struct
*vma
;
1939 int write
= gup_flags
& FOLL_WRITE
;
1941 down_read(&mm
->mmap_sem
);
1943 /* the access must start within one of the target process's mappings */
1944 vma
= find_vma(mm
, addr
);
1946 /* don't overrun this mapping */
1947 if (addr
+ len
>= vma
->vm_end
)
1948 len
= vma
->vm_end
- addr
;
1950 /* only read or write mappings where it is permitted */
1951 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1952 copy_to_user_page(vma
, NULL
, addr
,
1953 (void *) addr
, buf
, len
);
1954 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1955 copy_from_user_page(vma
, NULL
, addr
,
1956 buf
, (void *) addr
, len
);
1963 up_read(&mm
->mmap_sem
);
1969 * @access_remote_vm - access another process' address space
1970 * @mm: the mm_struct of the target address space
1971 * @addr: start address to access
1972 * @buf: source or destination buffer
1973 * @len: number of bytes to transfer
1974 * @gup_flags: flags modifying lookup behaviour
1976 * The caller must hold a reference on @mm.
1978 int access_remote_vm(struct mm_struct
*mm
, unsigned long addr
,
1979 void *buf
, int len
, unsigned int gup_flags
)
1981 return __access_remote_vm(NULL
, mm
, addr
, buf
, len
, gup_flags
);
1985 * Access another process' address space.
1986 * - source/target buffer must be kernel space
1988 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
1990 struct mm_struct
*mm
;
1992 if (addr
+ len
< addr
)
1995 mm
= get_task_mm(tsk
);
1999 len
= __access_remote_vm(tsk
, mm
, addr
, buf
, len
,
2000 write
? FOLL_WRITE
: 0);
2007 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2008 * @inode: The inode to check
2009 * @size: The current filesize of the inode
2010 * @newsize: The proposed filesize of the inode
2012 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2013 * make sure that that any outstanding VMAs aren't broken and then shrink the
2014 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2015 * automatically grant mappings that are too large.
2017 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
2020 struct vm_area_struct
*vma
;
2021 struct vm_region
*region
;
2023 size_t r_size
, r_top
;
2025 low
= newsize
>> PAGE_SHIFT
;
2026 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2028 down_write(&nommu_region_sem
);
2029 i_mmap_lock_read(inode
->i_mapping
);
2031 /* search for VMAs that fall within the dead zone */
2032 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, low
, high
) {
2033 /* found one - only interested if it's shared out of the page
2035 if (vma
->vm_flags
& VM_SHARED
) {
2036 i_mmap_unlock_read(inode
->i_mapping
);
2037 up_write(&nommu_region_sem
);
2038 return -ETXTBSY
; /* not quite true, but near enough */
2042 /* reduce any regions that overlap the dead zone - if in existence,
2043 * these will be pointed to by VMAs that don't overlap the dead zone
2045 * we don't check for any regions that start beyond the EOF as there
2048 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, 0, ULONG_MAX
) {
2049 if (!(vma
->vm_flags
& VM_SHARED
))
2052 region
= vma
->vm_region
;
2053 r_size
= region
->vm_top
- region
->vm_start
;
2054 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
2056 if (r_top
> newsize
) {
2057 region
->vm_top
-= r_top
- newsize
;
2058 if (region
->vm_end
> region
->vm_top
)
2059 region
->vm_end
= region
->vm_top
;
2063 i_mmap_unlock_read(inode
->i_mapping
);
2064 up_write(&nommu_region_sem
);
2069 * Initialise sysctl_user_reserve_kbytes.
2071 * This is intended to prevent a user from starting a single memory hogging
2072 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2075 * The default value is min(3% of free memory, 128MB)
2076 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2078 static int __meminit
init_user_reserve(void)
2080 unsigned long free_kbytes
;
2082 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
2084 sysctl_user_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 17);
2087 subsys_initcall(init_user_reserve
);
2090 * Initialise sysctl_admin_reserve_kbytes.
2092 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2093 * to log in and kill a memory hogging process.
2095 * Systems with more than 256MB will reserve 8MB, enough to recover
2096 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2097 * only reserve 3% of free pages by default.
2099 static int __meminit
init_admin_reserve(void)
2101 unsigned long free_kbytes
;
2103 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
2105 sysctl_admin_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 13);
2108 subsys_initcall(init_admin_reserve
);