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_all() if the architecture chose not to
478 void __weak
vmalloc_sync_all(void)
483 * alloc_vm_area - allocate a range of kernel address space
484 * @size: size of the area
486 * Returns: NULL on failure, vm_struct on success
488 * This function reserves a range of kernel address space, and
489 * allocates pagetables to map that range. No actual mappings
490 * are created. If the kernel address space is not shared
491 * between processes, it syncs the pagetable across all
494 struct vm_struct
*alloc_vm_area(size_t size
, pte_t
**ptes
)
499 EXPORT_SYMBOL_GPL(alloc_vm_area
);
501 void free_vm_area(struct vm_struct
*area
)
505 EXPORT_SYMBOL_GPL(free_vm_area
);
507 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
512 EXPORT_SYMBOL(vm_insert_page
);
515 * sys_brk() for the most part doesn't need the global kernel
516 * lock, except when an application is doing something nasty
517 * like trying to un-brk an area that has already been mapped
518 * to a regular file. in this case, the unmapping will need
519 * to invoke file system routines that need the global lock.
521 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
523 struct mm_struct
*mm
= current
->mm
;
525 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
532 * Always allow shrinking brk
534 if (brk
<= mm
->brk
) {
540 * Ok, looks good - let it rip.
542 flush_icache_range(mm
->brk
, brk
);
543 return mm
->brk
= brk
;
547 * initialise the VMA and region record slabs
549 void __init
mmap_init(void)
553 ret
= percpu_counter_init(&vm_committed_as
, 0, GFP_KERNEL
);
555 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
);
559 * validate the region tree
560 * - the caller must hold the region lock
562 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
563 static noinline
void validate_nommu_regions(void)
565 struct vm_region
*region
, *last
;
566 struct rb_node
*p
, *lastp
;
568 lastp
= rb_first(&nommu_region_tree
);
572 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
573 BUG_ON(last
->vm_end
<= last
->vm_start
);
574 BUG_ON(last
->vm_top
< last
->vm_end
);
576 while ((p
= rb_next(lastp
))) {
577 region
= rb_entry(p
, struct vm_region
, vm_rb
);
578 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
580 BUG_ON(region
->vm_end
<= region
->vm_start
);
581 BUG_ON(region
->vm_top
< region
->vm_end
);
582 BUG_ON(region
->vm_start
< last
->vm_top
);
588 static void validate_nommu_regions(void)
594 * add a region into the global tree
596 static void add_nommu_region(struct vm_region
*region
)
598 struct vm_region
*pregion
;
599 struct rb_node
**p
, *parent
;
601 validate_nommu_regions();
604 p
= &nommu_region_tree
.rb_node
;
607 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
608 if (region
->vm_start
< pregion
->vm_start
)
610 else if (region
->vm_start
> pregion
->vm_start
)
612 else if (pregion
== region
)
618 rb_link_node(®ion
->vm_rb
, parent
, p
);
619 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
621 validate_nommu_regions();
625 * delete a region from the global tree
627 static void delete_nommu_region(struct vm_region
*region
)
629 BUG_ON(!nommu_region_tree
.rb_node
);
631 validate_nommu_regions();
632 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
633 validate_nommu_regions();
637 * free a contiguous series of pages
639 static void free_page_series(unsigned long from
, unsigned long to
)
641 for (; from
< to
; from
+= PAGE_SIZE
) {
642 struct page
*page
= virt_to_page(from
);
644 atomic_long_dec(&mmap_pages_allocated
);
650 * release a reference to a region
651 * - the caller must hold the region semaphore for writing, which this releases
652 * - the region may not have been added to the tree yet, in which case vm_top
653 * will equal vm_start
655 static void __put_nommu_region(struct vm_region
*region
)
656 __releases(nommu_region_sem
)
658 BUG_ON(!nommu_region_tree
.rb_node
);
660 if (--region
->vm_usage
== 0) {
661 if (region
->vm_top
> region
->vm_start
)
662 delete_nommu_region(region
);
663 up_write(&nommu_region_sem
);
666 fput(region
->vm_file
);
668 /* IO memory and memory shared directly out of the pagecache
669 * from ramfs/tmpfs mustn't be released here */
670 if (region
->vm_flags
& VM_MAPPED_COPY
)
671 free_page_series(region
->vm_start
, region
->vm_top
);
672 kmem_cache_free(vm_region_jar
, region
);
674 up_write(&nommu_region_sem
);
679 * release a reference to a region
681 static void put_nommu_region(struct vm_region
*region
)
683 down_write(&nommu_region_sem
);
684 __put_nommu_region(region
);
688 * update protection on a vma
690 static void protect_vma(struct vm_area_struct
*vma
, unsigned long flags
)
693 struct mm_struct
*mm
= vma
->vm_mm
;
694 long start
= vma
->vm_start
& PAGE_MASK
;
695 while (start
< vma
->vm_end
) {
696 protect_page(mm
, start
, flags
);
699 update_protections(mm
);
704 * add a VMA into a process's mm_struct in the appropriate place in the list
705 * and tree and add to the address space's page tree also if not an anonymous
707 * - should be called with mm->mmap_sem held writelocked
709 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
711 struct vm_area_struct
*pvma
, *prev
;
712 struct address_space
*mapping
;
713 struct rb_node
**p
, *parent
, *rb_prev
;
715 BUG_ON(!vma
->vm_region
);
720 protect_vma(vma
, vma
->vm_flags
);
722 /* add the VMA to the mapping */
724 mapping
= vma
->vm_file
->f_mapping
;
726 i_mmap_lock_write(mapping
);
727 flush_dcache_mmap_lock(mapping
);
728 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
729 flush_dcache_mmap_unlock(mapping
);
730 i_mmap_unlock_write(mapping
);
733 /* add the VMA to the tree */
734 parent
= rb_prev
= NULL
;
735 p
= &mm
->mm_rb
.rb_node
;
738 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
740 /* sort by: start addr, end addr, VMA struct addr in that order
741 * (the latter is necessary as we may get identical VMAs) */
742 if (vma
->vm_start
< pvma
->vm_start
)
744 else if (vma
->vm_start
> pvma
->vm_start
) {
747 } else if (vma
->vm_end
< pvma
->vm_end
)
749 else if (vma
->vm_end
> pvma
->vm_end
) {
752 } else if (vma
< pvma
)
754 else if (vma
> pvma
) {
761 rb_link_node(&vma
->vm_rb
, parent
, p
);
762 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
764 /* add VMA to the VMA list also */
767 prev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
769 __vma_link_list(mm
, vma
, prev
, parent
);
773 * delete a VMA from its owning mm_struct and address space
775 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
778 struct address_space
*mapping
;
779 struct mm_struct
*mm
= vma
->vm_mm
;
780 struct task_struct
*curr
= current
;
785 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
786 /* if the vma is cached, invalidate the entire cache */
787 if (curr
->vmacache
[i
] == vma
) {
788 vmacache_invalidate(mm
);
793 /* remove the VMA from the mapping */
795 mapping
= vma
->vm_file
->f_mapping
;
797 i_mmap_lock_write(mapping
);
798 flush_dcache_mmap_lock(mapping
);
799 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
800 flush_dcache_mmap_unlock(mapping
);
801 i_mmap_unlock_write(mapping
);
804 /* remove from the MM's tree and list */
805 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
808 vma
->vm_prev
->vm_next
= vma
->vm_next
;
810 mm
->mmap
= vma
->vm_next
;
813 vma
->vm_next
->vm_prev
= vma
->vm_prev
;
817 * destroy a VMA record
819 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
821 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
822 vma
->vm_ops
->close(vma
);
825 put_nommu_region(vma
->vm_region
);
826 kmem_cache_free(vm_area_cachep
, vma
);
830 * look up the first VMA in which addr resides, NULL if none
831 * - should be called with mm->mmap_sem at least held readlocked
833 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
835 struct vm_area_struct
*vma
;
837 /* check the cache first */
838 vma
= vmacache_find(mm
, addr
);
842 /* trawl the list (there may be multiple mappings in which addr
844 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
845 if (vma
->vm_start
> addr
)
847 if (vma
->vm_end
> addr
) {
848 vmacache_update(addr
, vma
);
855 EXPORT_SYMBOL(find_vma
);
859 * - we don't extend stack VMAs under NOMMU conditions
861 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
863 return find_vma(mm
, addr
);
867 * expand a stack to a given address
868 * - not supported under NOMMU conditions
870 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
876 * look up the first VMA exactly that exactly matches addr
877 * - should be called with mm->mmap_sem at least held readlocked
879 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
883 struct vm_area_struct
*vma
;
884 unsigned long end
= addr
+ len
;
886 /* check the cache first */
887 vma
= vmacache_find_exact(mm
, addr
, end
);
891 /* trawl the list (there may be multiple mappings in which addr
893 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
894 if (vma
->vm_start
< addr
)
896 if (vma
->vm_start
> addr
)
898 if (vma
->vm_end
== end
) {
899 vmacache_update(addr
, vma
);
908 * determine whether a mapping should be permitted and, if so, what sort of
909 * mapping we're capable of supporting
911 static int validate_mmap_request(struct file
*file
,
917 unsigned long *_capabilities
)
919 unsigned long capabilities
, rlen
;
922 /* do the simple checks first */
923 if (flags
& MAP_FIXED
)
926 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
927 (flags
& MAP_TYPE
) != MAP_SHARED
)
933 /* Careful about overflows.. */
934 rlen
= PAGE_ALIGN(len
);
935 if (!rlen
|| rlen
> TASK_SIZE
)
938 /* offset overflow? */
939 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
943 /* files must support mmap */
944 if (!file
->f_op
->mmap
)
947 /* work out if what we've got could possibly be shared
948 * - we support chardevs that provide their own "memory"
949 * - we support files/blockdevs that are memory backed
951 if (file
->f_op
->mmap_capabilities
) {
952 capabilities
= file
->f_op
->mmap_capabilities(file
);
954 /* no explicit capabilities set, so assume some
956 switch (file_inode(file
)->i_mode
& S_IFMT
) {
959 capabilities
= NOMMU_MAP_COPY
;
974 /* eliminate any capabilities that we can't support on this
976 if (!file
->f_op
->get_unmapped_area
)
977 capabilities
&= ~NOMMU_MAP_DIRECT
;
978 if (!(file
->f_mode
& FMODE_CAN_READ
))
979 capabilities
&= ~NOMMU_MAP_COPY
;
981 /* The file shall have been opened with read permission. */
982 if (!(file
->f_mode
& FMODE_READ
))
985 if (flags
& MAP_SHARED
) {
986 /* do checks for writing, appending and locking */
987 if ((prot
& PROT_WRITE
) &&
988 !(file
->f_mode
& FMODE_WRITE
))
991 if (IS_APPEND(file_inode(file
)) &&
992 (file
->f_mode
& FMODE_WRITE
))
995 if (locks_verify_locked(file
))
998 if (!(capabilities
& NOMMU_MAP_DIRECT
))
1001 /* we mustn't privatise shared mappings */
1002 capabilities
&= ~NOMMU_MAP_COPY
;
1004 /* we're going to read the file into private memory we
1006 if (!(capabilities
& NOMMU_MAP_COPY
))
1009 /* we don't permit a private writable mapping to be
1010 * shared with the backing device */
1011 if (prot
& PROT_WRITE
)
1012 capabilities
&= ~NOMMU_MAP_DIRECT
;
1015 if (capabilities
& NOMMU_MAP_DIRECT
) {
1016 if (((prot
& PROT_READ
) && !(capabilities
& NOMMU_MAP_READ
)) ||
1017 ((prot
& PROT_WRITE
) && !(capabilities
& NOMMU_MAP_WRITE
)) ||
1018 ((prot
& PROT_EXEC
) && !(capabilities
& NOMMU_MAP_EXEC
))
1020 capabilities
&= ~NOMMU_MAP_DIRECT
;
1021 if (flags
& MAP_SHARED
) {
1022 pr_warn("MAP_SHARED not completely supported on !MMU\n");
1028 /* handle executable mappings and implied executable
1030 if (path_noexec(&file
->f_path
)) {
1031 if (prot
& PROT_EXEC
)
1033 } else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
1034 /* handle implication of PROT_EXEC by PROT_READ */
1035 if (current
->personality
& READ_IMPLIES_EXEC
) {
1036 if (capabilities
& NOMMU_MAP_EXEC
)
1039 } else if ((prot
& PROT_READ
) &&
1040 (prot
& PROT_EXEC
) &&
1041 !(capabilities
& NOMMU_MAP_EXEC
)
1043 /* backing file is not executable, try to copy */
1044 capabilities
&= ~NOMMU_MAP_DIRECT
;
1047 /* anonymous mappings are always memory backed and can be
1050 capabilities
= NOMMU_MAP_COPY
;
1052 /* handle PROT_EXEC implication by PROT_READ */
1053 if ((prot
& PROT_READ
) &&
1054 (current
->personality
& READ_IMPLIES_EXEC
))
1058 /* allow the security API to have its say */
1059 ret
= security_mmap_addr(addr
);
1064 *_capabilities
= capabilities
;
1069 * we've determined that we can make the mapping, now translate what we
1070 * now know into VMA flags
1072 static unsigned long determine_vm_flags(struct file
*file
,
1074 unsigned long flags
,
1075 unsigned long capabilities
)
1077 unsigned long vm_flags
;
1079 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
1080 /* vm_flags |= mm->def_flags; */
1082 if (!(capabilities
& NOMMU_MAP_DIRECT
)) {
1083 /* attempt to share read-only copies of mapped file chunks */
1084 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1085 if (file
&& !(prot
& PROT_WRITE
))
1086 vm_flags
|= VM_MAYSHARE
;
1088 /* overlay a shareable mapping on the backing device or inode
1089 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1091 vm_flags
|= VM_MAYSHARE
| (capabilities
& NOMMU_VMFLAGS
);
1092 if (flags
& MAP_SHARED
)
1093 vm_flags
|= VM_SHARED
;
1096 /* refuse to let anyone share private mappings with this process if
1097 * it's being traced - otherwise breakpoints set in it may interfere
1098 * with another untraced process
1100 if ((flags
& MAP_PRIVATE
) && current
->ptrace
)
1101 vm_flags
&= ~VM_MAYSHARE
;
1107 * set up a shared mapping on a file (the driver or filesystem provides and
1110 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1114 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1116 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1122 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1123 * opposed to tried but failed) so we can only give a suitable error as
1124 * it's not possible to make a private copy if MAP_SHARED was given */
1129 * set up a private mapping or an anonymous shared mapping
1131 static int do_mmap_private(struct vm_area_struct
*vma
,
1132 struct vm_region
*region
,
1134 unsigned long capabilities
)
1136 unsigned long total
, point
;
1140 /* invoke the file's mapping function so that it can keep track of
1141 * shared mappings on devices or memory
1142 * - VM_MAYSHARE will be set if it may attempt to share
1144 if (capabilities
& NOMMU_MAP_DIRECT
) {
1145 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1147 /* shouldn't return success if we're not sharing */
1148 BUG_ON(!(vma
->vm_flags
& VM_MAYSHARE
));
1149 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1155 /* getting an ENOSYS error indicates that direct mmap isn't
1156 * possible (as opposed to tried but failed) so we'll try to
1157 * make a private copy of the data and map that instead */
1161 /* allocate some memory to hold the mapping
1162 * - note that this may not return a page-aligned address if the object
1163 * we're allocating is smaller than a page
1165 order
= get_order(len
);
1167 point
= len
>> PAGE_SHIFT
;
1169 /* we don't want to allocate a power-of-2 sized page set */
1170 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
)
1173 base
= alloc_pages_exact(total
<< PAGE_SHIFT
, GFP_KERNEL
);
1177 atomic_long_add(total
, &mmap_pages_allocated
);
1179 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1180 region
->vm_start
= (unsigned long) base
;
1181 region
->vm_end
= region
->vm_start
+ len
;
1182 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1184 vma
->vm_start
= region
->vm_start
;
1185 vma
->vm_end
= region
->vm_start
+ len
;
1188 /* read the contents of a file into the copy */
1189 mm_segment_t old_fs
;
1192 fpos
= vma
->vm_pgoff
;
1193 fpos
<<= PAGE_SHIFT
;
1197 ret
= __vfs_read(vma
->vm_file
, base
, len
, &fpos
);
1203 /* clear the last little bit */
1205 memset(base
+ ret
, 0, len
- ret
);
1212 free_page_series(region
->vm_start
, region
->vm_top
);
1213 region
->vm_start
= vma
->vm_start
= 0;
1214 region
->vm_end
= vma
->vm_end
= 0;
1219 pr_err("Allocation of length %lu from process %d (%s) failed\n",
1220 len
, current
->pid
, current
->comm
);
1226 * handle mapping creation for uClinux
1228 unsigned long do_mmap(struct file
*file
,
1232 unsigned long flags
,
1233 vm_flags_t vm_flags
,
1234 unsigned long pgoff
,
1235 unsigned long *populate
)
1237 struct vm_area_struct
*vma
;
1238 struct vm_region
*region
;
1240 unsigned long capabilities
, result
;
1245 /* decide whether we should attempt the mapping, and if so what sort of
1247 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1252 /* we ignore the address hint */
1254 len
= PAGE_ALIGN(len
);
1256 /* we've determined that we can make the mapping, now translate what we
1257 * now know into VMA flags */
1258 vm_flags
|= determine_vm_flags(file
, prot
, flags
, capabilities
);
1260 /* we're going to need to record the mapping */
1261 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1263 goto error_getting_region
;
1265 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1267 goto error_getting_vma
;
1269 region
->vm_usage
= 1;
1270 region
->vm_flags
= vm_flags
;
1271 region
->vm_pgoff
= pgoff
;
1273 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1274 vma
->vm_flags
= vm_flags
;
1275 vma
->vm_pgoff
= pgoff
;
1278 region
->vm_file
= get_file(file
);
1279 vma
->vm_file
= get_file(file
);
1282 down_write(&nommu_region_sem
);
1284 /* if we want to share, we need to check for regions created by other
1285 * mmap() calls that overlap with our proposed mapping
1286 * - we can only share with a superset match on most regular files
1287 * - shared mappings on character devices and memory backed files are
1288 * permitted to overlap inexactly as far as we are concerned for in
1289 * these cases, sharing is handled in the driver or filesystem rather
1292 if (vm_flags
& VM_MAYSHARE
) {
1293 struct vm_region
*pregion
;
1294 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1296 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1297 pgend
= pgoff
+ pglen
;
1299 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1300 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1302 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1305 /* search for overlapping mappings on the same file */
1306 if (file_inode(pregion
->vm_file
) !=
1310 if (pregion
->vm_pgoff
>= pgend
)
1313 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1314 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1315 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1316 if (pgoff
>= rpgend
)
1319 /* handle inexactly overlapping matches between
1321 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1322 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1323 /* new mapping is not a subset of the region */
1324 if (!(capabilities
& NOMMU_MAP_DIRECT
))
1325 goto sharing_violation
;
1329 /* we've found a region we can share */
1330 pregion
->vm_usage
++;
1331 vma
->vm_region
= pregion
;
1332 start
= pregion
->vm_start
;
1333 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1334 vma
->vm_start
= start
;
1335 vma
->vm_end
= start
+ len
;
1337 if (pregion
->vm_flags
& VM_MAPPED_COPY
)
1338 vma
->vm_flags
|= VM_MAPPED_COPY
;
1340 ret
= do_mmap_shared_file(vma
);
1342 vma
->vm_region
= NULL
;
1345 pregion
->vm_usage
--;
1347 goto error_just_free
;
1350 fput(region
->vm_file
);
1351 kmem_cache_free(vm_region_jar
, region
);
1357 /* obtain the address at which to make a shared mapping
1358 * - this is the hook for quasi-memory character devices to
1359 * tell us the location of a shared mapping
1361 if (capabilities
& NOMMU_MAP_DIRECT
) {
1362 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1364 if (IS_ERR_VALUE(addr
)) {
1367 goto error_just_free
;
1369 /* the driver refused to tell us where to site
1370 * the mapping so we'll have to attempt to copy
1373 if (!(capabilities
& NOMMU_MAP_COPY
))
1374 goto error_just_free
;
1376 capabilities
&= ~NOMMU_MAP_DIRECT
;
1378 vma
->vm_start
= region
->vm_start
= addr
;
1379 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1384 vma
->vm_region
= region
;
1386 /* set up the mapping
1387 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1389 if (file
&& vma
->vm_flags
& VM_SHARED
)
1390 ret
= do_mmap_shared_file(vma
);
1392 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1394 goto error_just_free
;
1395 add_nommu_region(region
);
1397 /* clear anonymous mappings that don't ask for uninitialized data */
1398 if (!vma
->vm_file
&& !(flags
& MAP_UNINITIALIZED
))
1399 memset((void *)region
->vm_start
, 0,
1400 region
->vm_end
- region
->vm_start
);
1402 /* okay... we have a mapping; now we have to register it */
1403 result
= vma
->vm_start
;
1405 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1408 add_vma_to_mm(current
->mm
, vma
);
1410 /* we flush the region from the icache only when the first executable
1411 * mapping of it is made */
1412 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1413 flush_icache_range(region
->vm_start
, region
->vm_end
);
1414 region
->vm_icache_flushed
= true;
1417 up_write(&nommu_region_sem
);
1422 up_write(&nommu_region_sem
);
1424 if (region
->vm_file
)
1425 fput(region
->vm_file
);
1426 kmem_cache_free(vm_region_jar
, region
);
1429 kmem_cache_free(vm_area_cachep
, vma
);
1433 up_write(&nommu_region_sem
);
1434 pr_warn("Attempt to share mismatched mappings\n");
1439 kmem_cache_free(vm_region_jar
, region
);
1440 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1445 error_getting_region
:
1446 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1452 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1453 unsigned long, prot
, unsigned long, flags
,
1454 unsigned long, fd
, unsigned long, pgoff
)
1456 struct file
*file
= NULL
;
1457 unsigned long retval
= -EBADF
;
1459 audit_mmap_fd(fd
, flags
);
1460 if (!(flags
& MAP_ANONYMOUS
)) {
1466 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1468 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1476 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1477 struct mmap_arg_struct
{
1481 unsigned long flags
;
1483 unsigned long offset
;
1486 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1488 struct mmap_arg_struct a
;
1490 if (copy_from_user(&a
, arg
, sizeof(a
)))
1492 if (offset_in_page(a
.offset
))
1495 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1496 a
.offset
>> PAGE_SHIFT
);
1498 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1501 * split a vma into two pieces at address 'addr', a new vma is allocated either
1502 * for the first part or the tail.
1504 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1505 unsigned long addr
, int new_below
)
1507 struct vm_area_struct
*new;
1508 struct vm_region
*region
;
1509 unsigned long npages
;
1511 /* we're only permitted to split anonymous regions (these should have
1512 * only a single usage on the region) */
1516 if (mm
->map_count
>= sysctl_max_map_count
)
1519 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1523 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
1525 kmem_cache_free(vm_region_jar
, region
);
1529 /* most fields are the same, copy all, and then fixup */
1531 *region
= *vma
->vm_region
;
1532 new->vm_region
= region
;
1534 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1537 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1539 region
->vm_start
= new->vm_start
= addr
;
1540 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1543 if (new->vm_ops
&& new->vm_ops
->open
)
1544 new->vm_ops
->open(new);
1546 delete_vma_from_mm(vma
);
1547 down_write(&nommu_region_sem
);
1548 delete_nommu_region(vma
->vm_region
);
1550 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1551 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1553 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1554 vma
->vm_region
->vm_top
= addr
;
1556 add_nommu_region(vma
->vm_region
);
1557 add_nommu_region(new->vm_region
);
1558 up_write(&nommu_region_sem
);
1559 add_vma_to_mm(mm
, vma
);
1560 add_vma_to_mm(mm
, new);
1565 * shrink a VMA by removing the specified chunk from either the beginning or
1568 static int shrink_vma(struct mm_struct
*mm
,
1569 struct vm_area_struct
*vma
,
1570 unsigned long from
, unsigned long to
)
1572 struct vm_region
*region
;
1574 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1576 delete_vma_from_mm(vma
);
1577 if (from
> vma
->vm_start
)
1581 add_vma_to_mm(mm
, vma
);
1583 /* cut the backing region down to size */
1584 region
= vma
->vm_region
;
1585 BUG_ON(region
->vm_usage
!= 1);
1587 down_write(&nommu_region_sem
);
1588 delete_nommu_region(region
);
1589 if (from
> region
->vm_start
) {
1590 to
= region
->vm_top
;
1591 region
->vm_top
= region
->vm_end
= from
;
1593 region
->vm_start
= to
;
1595 add_nommu_region(region
);
1596 up_write(&nommu_region_sem
);
1598 free_page_series(from
, to
);
1604 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1605 * VMA, though it need not cover the whole VMA
1607 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1609 struct vm_area_struct
*vma
;
1613 len
= PAGE_ALIGN(len
);
1619 /* find the first potentially overlapping VMA */
1620 vma
= find_vma(mm
, start
);
1624 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1625 current
->pid
, current
->comm
,
1626 start
, start
+ len
- 1);
1632 /* we're allowed to split an anonymous VMA but not a file-backed one */
1635 if (start
> vma
->vm_start
)
1637 if (end
== vma
->vm_end
)
1638 goto erase_whole_vma
;
1643 /* the chunk must be a subset of the VMA found */
1644 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1645 goto erase_whole_vma
;
1646 if (start
< vma
->vm_start
|| end
> vma
->vm_end
)
1648 if (offset_in_page(start
))
1650 if (end
!= vma
->vm_end
&& offset_in_page(end
))
1652 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1653 ret
= split_vma(mm
, vma
, start
, 1);
1657 return shrink_vma(mm
, vma
, start
, end
);
1661 delete_vma_from_mm(vma
);
1662 delete_vma(mm
, vma
);
1665 EXPORT_SYMBOL(do_munmap
);
1667 int vm_munmap(unsigned long addr
, size_t len
)
1669 struct mm_struct
*mm
= current
->mm
;
1672 down_write(&mm
->mmap_sem
);
1673 ret
= do_munmap(mm
, addr
, len
);
1674 up_write(&mm
->mmap_sem
);
1677 EXPORT_SYMBOL(vm_munmap
);
1679 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1681 return vm_munmap(addr
, len
);
1685 * release all the mappings made in a process's VM space
1687 void exit_mmap(struct mm_struct
*mm
)
1689 struct vm_area_struct
*vma
;
1696 while ((vma
= mm
->mmap
)) {
1697 mm
->mmap
= vma
->vm_next
;
1698 delete_vma_from_mm(vma
);
1699 delete_vma(mm
, vma
);
1704 unsigned long vm_brk(unsigned long addr
, unsigned long len
)
1710 * expand (or shrink) an existing mapping, potentially moving it at the same
1711 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1713 * under NOMMU conditions, we only permit changing a mapping's size, and only
1714 * as long as it stays within the region allocated by do_mmap_private() and the
1715 * block is not shareable
1717 * MREMAP_FIXED is not supported under NOMMU conditions
1719 static unsigned long do_mremap(unsigned long addr
,
1720 unsigned long old_len
, unsigned long new_len
,
1721 unsigned long flags
, unsigned long new_addr
)
1723 struct vm_area_struct
*vma
;
1725 /* insanity checks first */
1726 old_len
= PAGE_ALIGN(old_len
);
1727 new_len
= PAGE_ALIGN(new_len
);
1728 if (old_len
== 0 || new_len
== 0)
1729 return (unsigned long) -EINVAL
;
1731 if (offset_in_page(addr
))
1734 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1735 return (unsigned long) -EINVAL
;
1737 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1739 return (unsigned long) -EINVAL
;
1741 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1742 return (unsigned long) -EFAULT
;
1744 if (vma
->vm_flags
& VM_MAYSHARE
)
1745 return (unsigned long) -EPERM
;
1747 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1748 return (unsigned long) -ENOMEM
;
1750 /* all checks complete - do it */
1751 vma
->vm_end
= vma
->vm_start
+ new_len
;
1752 return vma
->vm_start
;
1755 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1756 unsigned long, new_len
, unsigned long, flags
,
1757 unsigned long, new_addr
)
1761 down_write(¤t
->mm
->mmap_sem
);
1762 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1763 up_write(¤t
->mm
->mmap_sem
);
1767 struct page
*follow_page_mask(struct vm_area_struct
*vma
,
1768 unsigned long address
, unsigned int flags
,
1769 unsigned int *page_mask
)
1775 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long addr
,
1776 unsigned long pfn
, unsigned long size
, pgprot_t prot
)
1778 if (addr
!= (pfn
<< PAGE_SHIFT
))
1781 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
1784 EXPORT_SYMBOL(remap_pfn_range
);
1786 int vm_iomap_memory(struct vm_area_struct
*vma
, phys_addr_t start
, unsigned long len
)
1788 unsigned long pfn
= start
>> PAGE_SHIFT
;
1789 unsigned long vm_len
= vma
->vm_end
- vma
->vm_start
;
1791 pfn
+= vma
->vm_pgoff
;
1792 return io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, vm_len
, vma
->vm_page_prot
);
1794 EXPORT_SYMBOL(vm_iomap_memory
);
1796 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1797 unsigned long pgoff
)
1799 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1801 if (!(vma
->vm_flags
& VM_USERMAP
))
1804 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1805 vma
->vm_end
= vma
->vm_start
+ size
;
1809 EXPORT_SYMBOL(remap_vmalloc_range
);
1811 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1812 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1817 void unmap_mapping_range(struct address_space
*mapping
,
1818 loff_t
const holebegin
, loff_t
const holelen
,
1822 EXPORT_SYMBOL(unmap_mapping_range
);
1825 * Check that a process has enough memory to allocate a new virtual
1826 * mapping. 0 means there is enough memory for the allocation to
1827 * succeed and -ENOMEM implies there is not.
1829 * We currently support three overcommit policies, which are set via the
1830 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1832 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1833 * Additional code 2002 Jul 20 by Robert Love.
1835 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1837 * Note this is a helper function intended to be used by LSMs which
1838 * wish to use this logic.
1840 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1842 long free
, allowed
, reserve
;
1844 vm_acct_memory(pages
);
1847 * Sometimes we want to use more memory than we have
1849 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1852 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1853 free
= global_page_state(NR_FREE_PAGES
);
1854 free
+= global_page_state(NR_FILE_PAGES
);
1857 * shmem pages shouldn't be counted as free in this
1858 * case, they can't be purged, only swapped out, and
1859 * that won't affect the overall amount of available
1860 * memory in the system.
1862 free
-= global_page_state(NR_SHMEM
);
1864 free
+= get_nr_swap_pages();
1867 * Any slabs which are created with the
1868 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1869 * which are reclaimable, under pressure. The dentry
1870 * cache and most inode caches should fall into this
1872 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1875 * Leave reserved pages. The pages are not for anonymous pages.
1877 if (free
<= totalreserve_pages
)
1880 free
-= totalreserve_pages
;
1883 * Reserve some for root
1886 free
-= sysctl_admin_reserve_kbytes
>> (PAGE_SHIFT
- 10);
1894 allowed
= vm_commit_limit();
1896 * Reserve some 3% for root
1899 allowed
-= sysctl_admin_reserve_kbytes
>> (PAGE_SHIFT
- 10);
1902 * Don't let a single process grow so big a user can't recover
1905 reserve
= sysctl_user_reserve_kbytes
>> (PAGE_SHIFT
- 10);
1906 allowed
-= min_t(long, mm
->total_vm
/ 32, reserve
);
1909 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
1913 vm_unacct_memory(pages
);
1918 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1923 EXPORT_SYMBOL(filemap_fault
);
1925 void filemap_map_pages(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1929 EXPORT_SYMBOL(filemap_map_pages
);
1931 static int __access_remote_vm(struct task_struct
*tsk
, struct mm_struct
*mm
,
1932 unsigned long addr
, void *buf
, int len
, unsigned int gup_flags
)
1934 struct vm_area_struct
*vma
;
1935 int write
= gup_flags
& FOLL_WRITE
;
1937 down_read(&mm
->mmap_sem
);
1939 /* the access must start within one of the target process's mappings */
1940 vma
= find_vma(mm
, addr
);
1942 /* don't overrun this mapping */
1943 if (addr
+ len
>= vma
->vm_end
)
1944 len
= vma
->vm_end
- addr
;
1946 /* only read or write mappings where it is permitted */
1947 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1948 copy_to_user_page(vma
, NULL
, addr
,
1949 (void *) addr
, buf
, len
);
1950 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1951 copy_from_user_page(vma
, NULL
, addr
,
1952 buf
, (void *) addr
, len
);
1959 up_read(&mm
->mmap_sem
);
1965 * @access_remote_vm - access another process' address space
1966 * @mm: the mm_struct of the target address space
1967 * @addr: start address to access
1968 * @buf: source or destination buffer
1969 * @len: number of bytes to transfer
1970 * @gup_flags: flags modifying lookup behaviour
1972 * The caller must hold a reference on @mm.
1974 int access_remote_vm(struct mm_struct
*mm
, unsigned long addr
,
1975 void *buf
, int len
, unsigned int gup_flags
)
1977 return __access_remote_vm(NULL
, mm
, addr
, buf
, len
, gup_flags
);
1981 * Access another process' address space.
1982 * - source/target buffer must be kernel space
1984 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
1986 struct mm_struct
*mm
;
1988 if (addr
+ len
< addr
)
1991 mm
= get_task_mm(tsk
);
1995 len
= __access_remote_vm(tsk
, mm
, addr
, buf
, len
,
1996 write
? FOLL_WRITE
: 0);
2003 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2004 * @inode: The inode to check
2005 * @size: The current filesize of the inode
2006 * @newsize: The proposed filesize of the inode
2008 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2009 * make sure that that any outstanding VMAs aren't broken and then shrink the
2010 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2011 * automatically grant mappings that are too large.
2013 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
2016 struct vm_area_struct
*vma
;
2017 struct vm_region
*region
;
2019 size_t r_size
, r_top
;
2021 low
= newsize
>> PAGE_SHIFT
;
2022 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2024 down_write(&nommu_region_sem
);
2025 i_mmap_lock_read(inode
->i_mapping
);
2027 /* search for VMAs that fall within the dead zone */
2028 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, low
, high
) {
2029 /* found one - only interested if it's shared out of the page
2031 if (vma
->vm_flags
& VM_SHARED
) {
2032 i_mmap_unlock_read(inode
->i_mapping
);
2033 up_write(&nommu_region_sem
);
2034 return -ETXTBSY
; /* not quite true, but near enough */
2038 /* reduce any regions that overlap the dead zone - if in existence,
2039 * these will be pointed to by VMAs that don't overlap the dead zone
2041 * we don't check for any regions that start beyond the EOF as there
2044 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, 0, ULONG_MAX
) {
2045 if (!(vma
->vm_flags
& VM_SHARED
))
2048 region
= vma
->vm_region
;
2049 r_size
= region
->vm_top
- region
->vm_start
;
2050 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
2052 if (r_top
> newsize
) {
2053 region
->vm_top
-= r_top
- newsize
;
2054 if (region
->vm_end
> region
->vm_top
)
2055 region
->vm_end
= region
->vm_top
;
2059 i_mmap_unlock_read(inode
->i_mapping
);
2060 up_write(&nommu_region_sem
);
2065 * Initialise sysctl_user_reserve_kbytes.
2067 * This is intended to prevent a user from starting a single memory hogging
2068 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2071 * The default value is min(3% of free memory, 128MB)
2072 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2074 static int __meminit
init_user_reserve(void)
2076 unsigned long free_kbytes
;
2078 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
2080 sysctl_user_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 17);
2083 subsys_initcall(init_user_reserve
);
2086 * Initialise sysctl_admin_reserve_kbytes.
2088 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2089 * to log in and kill a memory hogging process.
2091 * Systems with more than 256MB will reserve 8MB, enough to recover
2092 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2093 * only reserve 3% of free pages by default.
2095 static int __meminit
init_admin_reserve(void)
2097 unsigned long free_kbytes
;
2099 free_kbytes
= global_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
2101 sysctl_admin_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 13);
2104 subsys_initcall(init_admin_reserve
);