1 // SPDX-License-Identifier: GPL-2.0-only
5 * Replacement code for mm functions to support CPU's that don't
6 * have any form of memory management unit (thus no virtual memory).
8 * See Documentation/admin-guide/mm/nommu-mmap.rst
10 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
11 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
12 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
13 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
14 * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/export.h>
21 #include <linux/sched/mm.h>
22 #include <linux/vmacache.h>
23 #include <linux/mman.h>
24 #include <linux/swap.h>
25 #include <linux/file.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/backing-dev.h>
31 #include <linux/compiler.h>
32 #include <linux/mount.h>
33 #include <linux/personality.h>
34 #include <linux/security.h>
35 #include <linux/syscalls.h>
36 #include <linux/audit.h>
37 #include <linux/printk.h>
39 #include <linux/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 int sysctl_nr_trim_pages
= CONFIG_NOMMU_INITIAL_TRIM_EXCESS
;
52 int heap_stack_gap
= 0;
54 atomic_long_t mmap_pages_allocated
;
56 EXPORT_SYMBOL(mem_map
);
58 /* list of mapped, potentially shareable regions */
59 static struct kmem_cache
*vm_region_jar
;
60 struct rb_root nommu_region_tree
= RB_ROOT
;
61 DECLARE_RWSEM(nommu_region_sem
);
63 const struct vm_operations_struct generic_file_vm_ops
= {
67 * Return the total memory allocated for this pointer, not
68 * just what the caller asked for.
70 * Doesn't have to be accurate, i.e. may have races.
72 unsigned int kobjsize(const void *objp
)
77 * If the object we have should not have ksize performed on it,
80 if (!objp
|| !virt_addr_valid(objp
))
83 page
= virt_to_head_page(objp
);
86 * If the allocator sets PageSlab, we know the pointer came from
93 * If it's not a compound page, see if we have a matching VMA
94 * region. This test is intentionally done in reverse order,
95 * so if there's no VMA, we still fall through and hand back
96 * PAGE_SIZE for 0-order pages.
98 if (!PageCompound(page
)) {
99 struct vm_area_struct
*vma
;
101 vma
= find_vma(current
->mm
, (unsigned long)objp
);
103 return vma
->vm_end
- vma
->vm_start
;
107 * The ksize() function is only guaranteed to work for pointers
108 * returned by kmalloc(). So handle arbitrary pointers here.
110 return page_size(page
);
114 * follow_pfn - look up PFN at a user virtual address
115 * @vma: memory mapping
116 * @address: user virtual address
117 * @pfn: location to store found PFN
119 * Only IO mappings and raw PFN mappings are allowed.
121 * Returns zero and the pfn at @pfn on success, -ve otherwise.
123 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
126 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
129 *pfn
= address
>> PAGE_SHIFT
;
132 EXPORT_SYMBOL(follow_pfn
);
134 LIST_HEAD(vmap_area_list
);
136 void vfree(const void *addr
)
140 EXPORT_SYMBOL(vfree
);
142 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
)
145 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
146 * returns only a logical address.
148 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
150 EXPORT_SYMBOL(__vmalloc
);
152 void *__vmalloc_node_range(unsigned long size
, unsigned long align
,
153 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
154 pgprot_t prot
, unsigned long vm_flags
, int node
,
157 return __vmalloc(size
, gfp_mask
);
160 void *__vmalloc_node(unsigned long size
, unsigned long align
, gfp_t gfp_mask
,
161 int node
, const void *caller
)
163 return __vmalloc(size
, gfp_mask
);
166 static void *__vmalloc_user_flags(unsigned long size
, gfp_t flags
)
170 ret
= __vmalloc(size
, flags
);
172 struct vm_area_struct
*vma
;
174 mmap_write_lock(current
->mm
);
175 vma
= find_vma(current
->mm
, (unsigned long)ret
);
177 vma
->vm_flags
|= VM_USERMAP
;
178 mmap_write_unlock(current
->mm
);
184 void *vmalloc_user(unsigned long size
)
186 return __vmalloc_user_flags(size
, GFP_KERNEL
| __GFP_ZERO
);
188 EXPORT_SYMBOL(vmalloc_user
);
190 struct page
*vmalloc_to_page(const void *addr
)
192 return virt_to_page(addr
);
194 EXPORT_SYMBOL(vmalloc_to_page
);
196 unsigned long vmalloc_to_pfn(const void *addr
)
198 return page_to_pfn(virt_to_page(addr
));
200 EXPORT_SYMBOL(vmalloc_to_pfn
);
202 long vread(char *buf
, char *addr
, unsigned long count
)
204 /* Don't allow overflow */
205 if ((unsigned long) buf
+ count
< count
)
206 count
= -(unsigned long) buf
;
208 memcpy(buf
, addr
, count
);
213 * vmalloc - allocate virtually contiguous memory
215 * @size: allocation size
217 * Allocate enough pages to cover @size from the page level
218 * allocator and map them into contiguous kernel virtual space.
220 * For tight control over page level allocator and protection flags
221 * use __vmalloc() instead.
223 void *vmalloc(unsigned long size
)
225 return __vmalloc(size
, GFP_KERNEL
);
227 EXPORT_SYMBOL(vmalloc
);
229 void *vmalloc_huge(unsigned long size
, gfp_t gfp_mask
) __weak
__alias(__vmalloc
);
232 * vzalloc - allocate virtually contiguous memory with zero fill
234 * @size: allocation size
236 * Allocate enough pages to cover @size from the page level
237 * allocator and map them into contiguous kernel virtual space.
238 * The memory allocated is set to zero.
240 * For tight control over page level allocator and protection flags
241 * use __vmalloc() instead.
243 void *vzalloc(unsigned long size
)
245 return __vmalloc(size
, GFP_KERNEL
| __GFP_ZERO
);
247 EXPORT_SYMBOL(vzalloc
);
250 * vmalloc_node - allocate memory on a specific node
251 * @size: allocation size
254 * Allocate enough pages to cover @size from the page level
255 * allocator and map them into contiguous kernel virtual space.
257 * For tight control over page level allocator and protection flags
258 * use __vmalloc() instead.
260 void *vmalloc_node(unsigned long size
, int node
)
262 return vmalloc(size
);
264 EXPORT_SYMBOL(vmalloc_node
);
267 * vzalloc_node - allocate memory on a specific node with zero fill
268 * @size: allocation size
271 * Allocate enough pages to cover @size from the page level
272 * allocator and map them into contiguous kernel virtual space.
273 * The memory allocated is set to zero.
275 * For tight control over page level allocator and protection flags
276 * use __vmalloc() instead.
278 void *vzalloc_node(unsigned long size
, int node
)
280 return vzalloc(size
);
282 EXPORT_SYMBOL(vzalloc_node
);
285 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
286 * @size: allocation size
288 * Allocate enough 32bit PA addressable pages to cover @size from the
289 * page level allocator and map them into contiguous kernel virtual space.
291 void *vmalloc_32(unsigned long size
)
293 return __vmalloc(size
, GFP_KERNEL
);
295 EXPORT_SYMBOL(vmalloc_32
);
298 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
299 * @size: allocation size
301 * The resulting memory area is 32bit addressable and zeroed so it can be
302 * mapped to userspace without leaking data.
304 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
305 * remap_vmalloc_range() are permissible.
307 void *vmalloc_32_user(unsigned long size
)
310 * We'll have to sort out the ZONE_DMA bits for 64-bit,
311 * but for now this can simply use vmalloc_user() directly.
313 return vmalloc_user(size
);
315 EXPORT_SYMBOL(vmalloc_32_user
);
317 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
324 void vunmap(const void *addr
)
328 EXPORT_SYMBOL(vunmap
);
330 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
)
335 EXPORT_SYMBOL(vm_map_ram
);
337 void vm_unmap_ram(const void *mem
, unsigned int count
)
341 EXPORT_SYMBOL(vm_unmap_ram
);
343 void vm_unmap_aliases(void)
346 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
348 void free_vm_area(struct vm_struct
*area
)
352 EXPORT_SYMBOL_GPL(free_vm_area
);
354 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
359 EXPORT_SYMBOL(vm_insert_page
);
361 int vm_map_pages(struct vm_area_struct
*vma
, struct page
**pages
,
366 EXPORT_SYMBOL(vm_map_pages
);
368 int vm_map_pages_zero(struct vm_area_struct
*vma
, struct page
**pages
,
373 EXPORT_SYMBOL(vm_map_pages_zero
);
376 * sys_brk() for the most part doesn't need the global kernel
377 * lock, except when an application is doing something nasty
378 * like trying to un-brk an area that has already been mapped
379 * to a regular file. in this case, the unmapping will need
380 * to invoke file system routines that need the global lock.
382 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
384 struct mm_struct
*mm
= current
->mm
;
386 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
393 * Always allow shrinking brk
395 if (brk
<= mm
->brk
) {
401 * Ok, looks good - let it rip.
403 flush_icache_user_range(mm
->brk
, brk
);
404 return mm
->brk
= brk
;
408 * initialise the percpu counter for VM and region record slabs
410 void __init
mmap_init(void)
414 ret
= percpu_counter_init(&vm_committed_as
, 0, GFP_KERNEL
);
416 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
|SLAB_ACCOUNT
);
420 * validate the region tree
421 * - the caller must hold the region lock
423 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
424 static noinline
void validate_nommu_regions(void)
426 struct vm_region
*region
, *last
;
427 struct rb_node
*p
, *lastp
;
429 lastp
= rb_first(&nommu_region_tree
);
433 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
434 BUG_ON(last
->vm_end
<= last
->vm_start
);
435 BUG_ON(last
->vm_top
< last
->vm_end
);
437 while ((p
= rb_next(lastp
))) {
438 region
= rb_entry(p
, struct vm_region
, vm_rb
);
439 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
441 BUG_ON(region
->vm_end
<= region
->vm_start
);
442 BUG_ON(region
->vm_top
< region
->vm_end
);
443 BUG_ON(region
->vm_start
< last
->vm_top
);
449 static void validate_nommu_regions(void)
455 * add a region into the global tree
457 static void add_nommu_region(struct vm_region
*region
)
459 struct vm_region
*pregion
;
460 struct rb_node
**p
, *parent
;
462 validate_nommu_regions();
465 p
= &nommu_region_tree
.rb_node
;
468 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
469 if (region
->vm_start
< pregion
->vm_start
)
471 else if (region
->vm_start
> pregion
->vm_start
)
473 else if (pregion
== region
)
479 rb_link_node(®ion
->vm_rb
, parent
, p
);
480 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
482 validate_nommu_regions();
486 * delete a region from the global tree
488 static void delete_nommu_region(struct vm_region
*region
)
490 BUG_ON(!nommu_region_tree
.rb_node
);
492 validate_nommu_regions();
493 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
494 validate_nommu_regions();
498 * free a contiguous series of pages
500 static void free_page_series(unsigned long from
, unsigned long to
)
502 for (; from
< to
; from
+= PAGE_SIZE
) {
503 struct page
*page
= virt_to_page((void *)from
);
505 atomic_long_dec(&mmap_pages_allocated
);
511 * release a reference to a region
512 * - the caller must hold the region semaphore for writing, which this releases
513 * - the region may not have been added to the tree yet, in which case vm_top
514 * will equal vm_start
516 static void __put_nommu_region(struct vm_region
*region
)
517 __releases(nommu_region_sem
)
519 BUG_ON(!nommu_region_tree
.rb_node
);
521 if (--region
->vm_usage
== 0) {
522 if (region
->vm_top
> region
->vm_start
)
523 delete_nommu_region(region
);
524 up_write(&nommu_region_sem
);
527 fput(region
->vm_file
);
529 /* IO memory and memory shared directly out of the pagecache
530 * from ramfs/tmpfs mustn't be released here */
531 if (region
->vm_flags
& VM_MAPPED_COPY
)
532 free_page_series(region
->vm_start
, region
->vm_top
);
533 kmem_cache_free(vm_region_jar
, region
);
535 up_write(&nommu_region_sem
);
540 * release a reference to a region
542 static void put_nommu_region(struct vm_region
*region
)
544 down_write(&nommu_region_sem
);
545 __put_nommu_region(region
);
549 * add a VMA into a process's mm_struct in the appropriate place in the list
550 * and tree and add to the address space's page tree also if not an anonymous
552 * - should be called with mm->mmap_lock held writelocked
554 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
556 struct vm_area_struct
*pvma
, *prev
;
557 struct address_space
*mapping
;
558 struct rb_node
**p
, *parent
, *rb_prev
;
560 BUG_ON(!vma
->vm_region
);
565 /* add the VMA to the mapping */
567 mapping
= vma
->vm_file
->f_mapping
;
569 i_mmap_lock_write(mapping
);
570 flush_dcache_mmap_lock(mapping
);
571 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
572 flush_dcache_mmap_unlock(mapping
);
573 i_mmap_unlock_write(mapping
);
576 /* add the VMA to the tree */
577 parent
= rb_prev
= NULL
;
578 p
= &mm
->mm_rb
.rb_node
;
581 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
583 /* sort by: start addr, end addr, VMA struct addr in that order
584 * (the latter is necessary as we may get identical VMAs) */
585 if (vma
->vm_start
< pvma
->vm_start
)
587 else if (vma
->vm_start
> pvma
->vm_start
) {
590 } else if (vma
->vm_end
< pvma
->vm_end
)
592 else if (vma
->vm_end
> pvma
->vm_end
) {
595 } else if (vma
< pvma
)
597 else if (vma
> pvma
) {
604 rb_link_node(&vma
->vm_rb
, parent
, p
);
605 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
607 /* add VMA to the VMA list also */
610 prev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
612 __vma_link_list(mm
, vma
, prev
);
616 * delete a VMA from its owning mm_struct and address space
618 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
621 struct address_space
*mapping
;
622 struct mm_struct
*mm
= vma
->vm_mm
;
623 struct task_struct
*curr
= current
;
626 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
627 /* if the vma is cached, invalidate the entire cache */
628 if (curr
->vmacache
.vmas
[i
] == vma
) {
629 vmacache_invalidate(mm
);
634 /* remove the VMA from the mapping */
636 mapping
= vma
->vm_file
->f_mapping
;
638 i_mmap_lock_write(mapping
);
639 flush_dcache_mmap_lock(mapping
);
640 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
641 flush_dcache_mmap_unlock(mapping
);
642 i_mmap_unlock_write(mapping
);
645 /* remove from the MM's tree and list */
646 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
648 __vma_unlink_list(mm
, vma
);
652 * destroy a VMA record
654 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
656 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
657 vma
->vm_ops
->close(vma
);
660 put_nommu_region(vma
->vm_region
);
665 * look up the first VMA in which addr resides, NULL if none
666 * - should be called with mm->mmap_lock at least held readlocked
668 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
670 struct vm_area_struct
*vma
;
672 /* check the cache first */
673 vma
= vmacache_find(mm
, addr
);
677 /* trawl the list (there may be multiple mappings in which addr
679 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
680 if (vma
->vm_start
> addr
)
682 if (vma
->vm_end
> addr
) {
683 vmacache_update(addr
, vma
);
690 EXPORT_SYMBOL(find_vma
);
694 * - we don't extend stack VMAs under NOMMU conditions
696 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
698 return find_vma(mm
, addr
);
702 * expand a stack to a given address
703 * - not supported under NOMMU conditions
705 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
711 * look up the first VMA exactly that exactly matches addr
712 * - should be called with mm->mmap_lock at least held readlocked
714 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
718 struct vm_area_struct
*vma
;
719 unsigned long end
= addr
+ len
;
721 /* check the cache first */
722 vma
= vmacache_find_exact(mm
, addr
, end
);
726 /* trawl the list (there may be multiple mappings in which addr
728 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
729 if (vma
->vm_start
< addr
)
731 if (vma
->vm_start
> addr
)
733 if (vma
->vm_end
== end
) {
734 vmacache_update(addr
, vma
);
743 * determine whether a mapping should be permitted and, if so, what sort of
744 * mapping we're capable of supporting
746 static int validate_mmap_request(struct file
*file
,
752 unsigned long *_capabilities
)
754 unsigned long capabilities
, rlen
;
757 /* do the simple checks first */
758 if (flags
& MAP_FIXED
)
761 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
762 (flags
& MAP_TYPE
) != MAP_SHARED
)
768 /* Careful about overflows.. */
769 rlen
= PAGE_ALIGN(len
);
770 if (!rlen
|| rlen
> TASK_SIZE
)
773 /* offset overflow? */
774 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
778 /* files must support mmap */
779 if (!file
->f_op
->mmap
)
782 /* work out if what we've got could possibly be shared
783 * - we support chardevs that provide their own "memory"
784 * - we support files/blockdevs that are memory backed
786 if (file
->f_op
->mmap_capabilities
) {
787 capabilities
= file
->f_op
->mmap_capabilities(file
);
789 /* no explicit capabilities set, so assume some
791 switch (file_inode(file
)->i_mode
& S_IFMT
) {
794 capabilities
= NOMMU_MAP_COPY
;
809 /* eliminate any capabilities that we can't support on this
811 if (!file
->f_op
->get_unmapped_area
)
812 capabilities
&= ~NOMMU_MAP_DIRECT
;
813 if (!(file
->f_mode
& FMODE_CAN_READ
))
814 capabilities
&= ~NOMMU_MAP_COPY
;
816 /* The file shall have been opened with read permission. */
817 if (!(file
->f_mode
& FMODE_READ
))
820 if (flags
& MAP_SHARED
) {
821 /* do checks for writing, appending and locking */
822 if ((prot
& PROT_WRITE
) &&
823 !(file
->f_mode
& FMODE_WRITE
))
826 if (IS_APPEND(file_inode(file
)) &&
827 (file
->f_mode
& FMODE_WRITE
))
830 if (!(capabilities
& NOMMU_MAP_DIRECT
))
833 /* we mustn't privatise shared mappings */
834 capabilities
&= ~NOMMU_MAP_COPY
;
836 /* we're going to read the file into private memory we
838 if (!(capabilities
& NOMMU_MAP_COPY
))
841 /* we don't permit a private writable mapping to be
842 * shared with the backing device */
843 if (prot
& PROT_WRITE
)
844 capabilities
&= ~NOMMU_MAP_DIRECT
;
847 if (capabilities
& NOMMU_MAP_DIRECT
) {
848 if (((prot
& PROT_READ
) && !(capabilities
& NOMMU_MAP_READ
)) ||
849 ((prot
& PROT_WRITE
) && !(capabilities
& NOMMU_MAP_WRITE
)) ||
850 ((prot
& PROT_EXEC
) && !(capabilities
& NOMMU_MAP_EXEC
))
852 capabilities
&= ~NOMMU_MAP_DIRECT
;
853 if (flags
& MAP_SHARED
) {
854 pr_warn("MAP_SHARED not completely supported on !MMU\n");
860 /* handle executable mappings and implied executable
862 if (path_noexec(&file
->f_path
)) {
863 if (prot
& PROT_EXEC
)
865 } else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
866 /* handle implication of PROT_EXEC by PROT_READ */
867 if (current
->personality
& READ_IMPLIES_EXEC
) {
868 if (capabilities
& NOMMU_MAP_EXEC
)
871 } else if ((prot
& PROT_READ
) &&
872 (prot
& PROT_EXEC
) &&
873 !(capabilities
& NOMMU_MAP_EXEC
)
875 /* backing file is not executable, try to copy */
876 capabilities
&= ~NOMMU_MAP_DIRECT
;
879 /* anonymous mappings are always memory backed and can be
882 capabilities
= NOMMU_MAP_COPY
;
884 /* handle PROT_EXEC implication by PROT_READ */
885 if ((prot
& PROT_READ
) &&
886 (current
->personality
& READ_IMPLIES_EXEC
))
890 /* allow the security API to have its say */
891 ret
= security_mmap_addr(addr
);
896 *_capabilities
= capabilities
;
901 * we've determined that we can make the mapping, now translate what we
902 * now know into VMA flags
904 static unsigned long determine_vm_flags(struct file
*file
,
907 unsigned long capabilities
)
909 unsigned long vm_flags
;
911 vm_flags
= calc_vm_prot_bits(prot
, 0) | calc_vm_flag_bits(flags
);
912 /* vm_flags |= mm->def_flags; */
914 if (!(capabilities
& NOMMU_MAP_DIRECT
)) {
915 /* attempt to share read-only copies of mapped file chunks */
916 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
917 if (file
&& !(prot
& PROT_WRITE
))
918 vm_flags
|= VM_MAYSHARE
;
920 /* overlay a shareable mapping on the backing device or inode
921 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
923 vm_flags
|= VM_MAYSHARE
| (capabilities
& NOMMU_VMFLAGS
);
924 if (flags
& MAP_SHARED
)
925 vm_flags
|= VM_SHARED
;
928 /* refuse to let anyone share private mappings with this process if
929 * it's being traced - otherwise breakpoints set in it may interfere
930 * with another untraced process
932 if ((flags
& MAP_PRIVATE
) && current
->ptrace
)
933 vm_flags
&= ~VM_MAYSHARE
;
939 * set up a shared mapping on a file (the driver or filesystem provides and
942 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
946 ret
= call_mmap(vma
->vm_file
, vma
);
948 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
954 /* getting -ENOSYS indicates that direct mmap isn't possible (as
955 * opposed to tried but failed) so we can only give a suitable error as
956 * it's not possible to make a private copy if MAP_SHARED was given */
961 * set up a private mapping or an anonymous shared mapping
963 static int do_mmap_private(struct vm_area_struct
*vma
,
964 struct vm_region
*region
,
966 unsigned long capabilities
)
968 unsigned long total
, point
;
972 /* invoke the file's mapping function so that it can keep track of
973 * shared mappings on devices or memory
974 * - VM_MAYSHARE will be set if it may attempt to share
976 if (capabilities
& NOMMU_MAP_DIRECT
) {
977 ret
= call_mmap(vma
->vm_file
, vma
);
979 /* shouldn't return success if we're not sharing */
980 BUG_ON(!(vma
->vm_flags
& VM_MAYSHARE
));
981 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
987 /* getting an ENOSYS error indicates that direct mmap isn't
988 * possible (as opposed to tried but failed) so we'll try to
989 * make a private copy of the data and map that instead */
993 /* allocate some memory to hold the mapping
994 * - note that this may not return a page-aligned address if the object
995 * we're allocating is smaller than a page
997 order
= get_order(len
);
999 point
= len
>> PAGE_SHIFT
;
1001 /* we don't want to allocate a power-of-2 sized page set */
1002 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
)
1005 base
= alloc_pages_exact(total
<< PAGE_SHIFT
, GFP_KERNEL
);
1009 atomic_long_add(total
, &mmap_pages_allocated
);
1011 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1012 region
->vm_start
= (unsigned long) base
;
1013 region
->vm_end
= region
->vm_start
+ len
;
1014 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1016 vma
->vm_start
= region
->vm_start
;
1017 vma
->vm_end
= region
->vm_start
+ len
;
1020 /* read the contents of a file into the copy */
1023 fpos
= vma
->vm_pgoff
;
1024 fpos
<<= PAGE_SHIFT
;
1026 ret
= kernel_read(vma
->vm_file
, base
, len
, &fpos
);
1030 /* clear the last little bit */
1032 memset(base
+ ret
, 0, len
- ret
);
1035 vma_set_anonymous(vma
);
1041 free_page_series(region
->vm_start
, region
->vm_top
);
1042 region
->vm_start
= vma
->vm_start
= 0;
1043 region
->vm_end
= vma
->vm_end
= 0;
1048 pr_err("Allocation of length %lu from process %d (%s) failed\n",
1049 len
, current
->pid
, current
->comm
);
1050 show_free_areas(0, NULL
);
1055 * handle mapping creation for uClinux
1057 unsigned long do_mmap(struct file
*file
,
1061 unsigned long flags
,
1062 unsigned long pgoff
,
1063 unsigned long *populate
,
1064 struct list_head
*uf
)
1066 struct vm_area_struct
*vma
;
1067 struct vm_region
*region
;
1069 vm_flags_t vm_flags
;
1070 unsigned long capabilities
, result
;
1075 /* decide whether we should attempt the mapping, and if so what sort of
1077 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1082 /* we ignore the address hint */
1084 len
= PAGE_ALIGN(len
);
1086 /* we've determined that we can make the mapping, now translate what we
1087 * now know into VMA flags */
1088 vm_flags
= determine_vm_flags(file
, prot
, flags
, capabilities
);
1090 /* we're going to need to record the mapping */
1091 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1093 goto error_getting_region
;
1095 vma
= vm_area_alloc(current
->mm
);
1097 goto error_getting_vma
;
1099 region
->vm_usage
= 1;
1100 region
->vm_flags
= vm_flags
;
1101 region
->vm_pgoff
= pgoff
;
1103 vma
->vm_flags
= vm_flags
;
1104 vma
->vm_pgoff
= pgoff
;
1107 region
->vm_file
= get_file(file
);
1108 vma
->vm_file
= get_file(file
);
1111 down_write(&nommu_region_sem
);
1113 /* if we want to share, we need to check for regions created by other
1114 * mmap() calls that overlap with our proposed mapping
1115 * - we can only share with a superset match on most regular files
1116 * - shared mappings on character devices and memory backed files are
1117 * permitted to overlap inexactly as far as we are concerned for in
1118 * these cases, sharing is handled in the driver or filesystem rather
1121 if (vm_flags
& VM_MAYSHARE
) {
1122 struct vm_region
*pregion
;
1123 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1125 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1126 pgend
= pgoff
+ pglen
;
1128 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1129 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1131 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1134 /* search for overlapping mappings on the same file */
1135 if (file_inode(pregion
->vm_file
) !=
1139 if (pregion
->vm_pgoff
>= pgend
)
1142 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1143 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1144 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1145 if (pgoff
>= rpgend
)
1148 /* handle inexactly overlapping matches between
1150 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1151 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1152 /* new mapping is not a subset of the region */
1153 if (!(capabilities
& NOMMU_MAP_DIRECT
))
1154 goto sharing_violation
;
1158 /* we've found a region we can share */
1159 pregion
->vm_usage
++;
1160 vma
->vm_region
= pregion
;
1161 start
= pregion
->vm_start
;
1162 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1163 vma
->vm_start
= start
;
1164 vma
->vm_end
= start
+ len
;
1166 if (pregion
->vm_flags
& VM_MAPPED_COPY
)
1167 vma
->vm_flags
|= VM_MAPPED_COPY
;
1169 ret
= do_mmap_shared_file(vma
);
1171 vma
->vm_region
= NULL
;
1174 pregion
->vm_usage
--;
1176 goto error_just_free
;
1179 fput(region
->vm_file
);
1180 kmem_cache_free(vm_region_jar
, region
);
1186 /* obtain the address at which to make a shared mapping
1187 * - this is the hook for quasi-memory character devices to
1188 * tell us the location of a shared mapping
1190 if (capabilities
& NOMMU_MAP_DIRECT
) {
1191 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1193 if (IS_ERR_VALUE(addr
)) {
1196 goto error_just_free
;
1198 /* the driver refused to tell us where to site
1199 * the mapping so we'll have to attempt to copy
1202 if (!(capabilities
& NOMMU_MAP_COPY
))
1203 goto error_just_free
;
1205 capabilities
&= ~NOMMU_MAP_DIRECT
;
1207 vma
->vm_start
= region
->vm_start
= addr
;
1208 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1213 vma
->vm_region
= region
;
1215 /* set up the mapping
1216 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1218 if (file
&& vma
->vm_flags
& VM_SHARED
)
1219 ret
= do_mmap_shared_file(vma
);
1221 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1223 goto error_just_free
;
1224 add_nommu_region(region
);
1226 /* clear anonymous mappings that don't ask for uninitialized data */
1227 if (!vma
->vm_file
&&
1228 (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED
) ||
1229 !(flags
& MAP_UNINITIALIZED
)))
1230 memset((void *)region
->vm_start
, 0,
1231 region
->vm_end
- region
->vm_start
);
1233 /* okay... we have a mapping; now we have to register it */
1234 result
= vma
->vm_start
;
1236 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1239 add_vma_to_mm(current
->mm
, vma
);
1241 /* we flush the region from the icache only when the first executable
1242 * mapping of it is made */
1243 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1244 flush_icache_user_range(region
->vm_start
, region
->vm_end
);
1245 region
->vm_icache_flushed
= true;
1248 up_write(&nommu_region_sem
);
1253 up_write(&nommu_region_sem
);
1255 if (region
->vm_file
)
1256 fput(region
->vm_file
);
1257 kmem_cache_free(vm_region_jar
, region
);
1264 up_write(&nommu_region_sem
);
1265 pr_warn("Attempt to share mismatched mappings\n");
1270 kmem_cache_free(vm_region_jar
, region
);
1271 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1273 show_free_areas(0, NULL
);
1276 error_getting_region
:
1277 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1279 show_free_areas(0, NULL
);
1283 unsigned long ksys_mmap_pgoff(unsigned long addr
, unsigned long len
,
1284 unsigned long prot
, unsigned long flags
,
1285 unsigned long fd
, unsigned long pgoff
)
1287 struct file
*file
= NULL
;
1288 unsigned long retval
= -EBADF
;
1290 audit_mmap_fd(fd
, flags
);
1291 if (!(flags
& MAP_ANONYMOUS
)) {
1297 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1305 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1306 unsigned long, prot
, unsigned long, flags
,
1307 unsigned long, fd
, unsigned long, pgoff
)
1309 return ksys_mmap_pgoff(addr
, len
, prot
, flags
, fd
, pgoff
);
1312 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1313 struct mmap_arg_struct
{
1317 unsigned long flags
;
1319 unsigned long offset
;
1322 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1324 struct mmap_arg_struct a
;
1326 if (copy_from_user(&a
, arg
, sizeof(a
)))
1328 if (offset_in_page(a
.offset
))
1331 return ksys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1332 a
.offset
>> PAGE_SHIFT
);
1334 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1337 * split a vma into two pieces at address 'addr', a new vma is allocated either
1338 * for the first part or the tail.
1340 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1341 unsigned long addr
, int new_below
)
1343 struct vm_area_struct
*new;
1344 struct vm_region
*region
;
1345 unsigned long npages
;
1347 /* we're only permitted to split anonymous regions (these should have
1348 * only a single usage on the region) */
1352 if (mm
->map_count
>= sysctl_max_map_count
)
1355 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1359 new = vm_area_dup(vma
);
1361 kmem_cache_free(vm_region_jar
, region
);
1365 /* most fields are the same, copy all, and then fixup */
1366 *region
= *vma
->vm_region
;
1367 new->vm_region
= region
;
1369 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1372 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1374 region
->vm_start
= new->vm_start
= addr
;
1375 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1378 if (new->vm_ops
&& new->vm_ops
->open
)
1379 new->vm_ops
->open(new);
1381 delete_vma_from_mm(vma
);
1382 down_write(&nommu_region_sem
);
1383 delete_nommu_region(vma
->vm_region
);
1385 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1386 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1388 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1389 vma
->vm_region
->vm_top
= addr
;
1391 add_nommu_region(vma
->vm_region
);
1392 add_nommu_region(new->vm_region
);
1393 up_write(&nommu_region_sem
);
1394 add_vma_to_mm(mm
, vma
);
1395 add_vma_to_mm(mm
, new);
1400 * shrink a VMA by removing the specified chunk from either the beginning or
1403 static int shrink_vma(struct mm_struct
*mm
,
1404 struct vm_area_struct
*vma
,
1405 unsigned long from
, unsigned long to
)
1407 struct vm_region
*region
;
1409 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1411 delete_vma_from_mm(vma
);
1412 if (from
> vma
->vm_start
)
1416 add_vma_to_mm(mm
, vma
);
1418 /* cut the backing region down to size */
1419 region
= vma
->vm_region
;
1420 BUG_ON(region
->vm_usage
!= 1);
1422 down_write(&nommu_region_sem
);
1423 delete_nommu_region(region
);
1424 if (from
> region
->vm_start
) {
1425 to
= region
->vm_top
;
1426 region
->vm_top
= region
->vm_end
= from
;
1428 region
->vm_start
= to
;
1430 add_nommu_region(region
);
1431 up_write(&nommu_region_sem
);
1433 free_page_series(from
, to
);
1439 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1440 * VMA, though it need not cover the whole VMA
1442 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
, struct list_head
*uf
)
1444 struct vm_area_struct
*vma
;
1448 len
= PAGE_ALIGN(len
);
1454 /* find the first potentially overlapping VMA */
1455 vma
= find_vma(mm
, start
);
1459 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1460 current
->pid
, current
->comm
,
1461 start
, start
+ len
- 1);
1467 /* we're allowed to split an anonymous VMA but not a file-backed one */
1470 if (start
> vma
->vm_start
)
1472 if (end
== vma
->vm_end
)
1473 goto erase_whole_vma
;
1478 /* the chunk must be a subset of the VMA found */
1479 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1480 goto erase_whole_vma
;
1481 if (start
< vma
->vm_start
|| end
> vma
->vm_end
)
1483 if (offset_in_page(start
))
1485 if (end
!= vma
->vm_end
&& offset_in_page(end
))
1487 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1488 ret
= split_vma(mm
, vma
, start
, 1);
1492 return shrink_vma(mm
, vma
, start
, end
);
1496 delete_vma_from_mm(vma
);
1497 delete_vma(mm
, vma
);
1501 int vm_munmap(unsigned long addr
, size_t len
)
1503 struct mm_struct
*mm
= current
->mm
;
1506 mmap_write_lock(mm
);
1507 ret
= do_munmap(mm
, addr
, len
, NULL
);
1508 mmap_write_unlock(mm
);
1511 EXPORT_SYMBOL(vm_munmap
);
1513 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1515 return vm_munmap(addr
, len
);
1519 * release all the mappings made in a process's VM space
1521 void exit_mmap(struct mm_struct
*mm
)
1523 struct vm_area_struct
*vma
;
1530 while ((vma
= mm
->mmap
)) {
1531 mm
->mmap
= vma
->vm_next
;
1532 delete_vma_from_mm(vma
);
1533 delete_vma(mm
, vma
);
1538 int vm_brk(unsigned long addr
, unsigned long len
)
1544 * expand (or shrink) an existing mapping, potentially moving it at the same
1545 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1547 * under NOMMU conditions, we only permit changing a mapping's size, and only
1548 * as long as it stays within the region allocated by do_mmap_private() and the
1549 * block is not shareable
1551 * MREMAP_FIXED is not supported under NOMMU conditions
1553 static unsigned long do_mremap(unsigned long addr
,
1554 unsigned long old_len
, unsigned long new_len
,
1555 unsigned long flags
, unsigned long new_addr
)
1557 struct vm_area_struct
*vma
;
1559 /* insanity checks first */
1560 old_len
= PAGE_ALIGN(old_len
);
1561 new_len
= PAGE_ALIGN(new_len
);
1562 if (old_len
== 0 || new_len
== 0)
1563 return (unsigned long) -EINVAL
;
1565 if (offset_in_page(addr
))
1568 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1569 return (unsigned long) -EINVAL
;
1571 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1573 return (unsigned long) -EINVAL
;
1575 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1576 return (unsigned long) -EFAULT
;
1578 if (vma
->vm_flags
& VM_MAYSHARE
)
1579 return (unsigned long) -EPERM
;
1581 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1582 return (unsigned long) -ENOMEM
;
1584 /* all checks complete - do it */
1585 vma
->vm_end
= vma
->vm_start
+ new_len
;
1586 return vma
->vm_start
;
1589 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1590 unsigned long, new_len
, unsigned long, flags
,
1591 unsigned long, new_addr
)
1595 mmap_write_lock(current
->mm
);
1596 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1597 mmap_write_unlock(current
->mm
);
1601 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1602 unsigned int foll_flags
)
1607 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long addr
,
1608 unsigned long pfn
, unsigned long size
, pgprot_t prot
)
1610 if (addr
!= (pfn
<< PAGE_SHIFT
))
1613 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
1616 EXPORT_SYMBOL(remap_pfn_range
);
1618 int vm_iomap_memory(struct vm_area_struct
*vma
, phys_addr_t start
, unsigned long len
)
1620 unsigned long pfn
= start
>> PAGE_SHIFT
;
1621 unsigned long vm_len
= vma
->vm_end
- vma
->vm_start
;
1623 pfn
+= vma
->vm_pgoff
;
1624 return io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, vm_len
, vma
->vm_page_prot
);
1626 EXPORT_SYMBOL(vm_iomap_memory
);
1628 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1629 unsigned long pgoff
)
1631 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1633 if (!(vma
->vm_flags
& VM_USERMAP
))
1636 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1637 vma
->vm_end
= vma
->vm_start
+ size
;
1641 EXPORT_SYMBOL(remap_vmalloc_range
);
1643 vm_fault_t
filemap_fault(struct vm_fault
*vmf
)
1648 EXPORT_SYMBOL(filemap_fault
);
1650 vm_fault_t
filemap_map_pages(struct vm_fault
*vmf
,
1651 pgoff_t start_pgoff
, pgoff_t end_pgoff
)
1656 EXPORT_SYMBOL(filemap_map_pages
);
1658 int __access_remote_vm(struct mm_struct
*mm
, unsigned long addr
, void *buf
,
1659 int len
, unsigned int gup_flags
)
1661 struct vm_area_struct
*vma
;
1662 int write
= gup_flags
& FOLL_WRITE
;
1664 if (mmap_read_lock_killable(mm
))
1667 /* the access must start within one of the target process's mappings */
1668 vma
= find_vma(mm
, addr
);
1670 /* don't overrun this mapping */
1671 if (addr
+ len
>= vma
->vm_end
)
1672 len
= vma
->vm_end
- addr
;
1674 /* only read or write mappings where it is permitted */
1675 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1676 copy_to_user_page(vma
, NULL
, addr
,
1677 (void *) addr
, buf
, len
);
1678 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1679 copy_from_user_page(vma
, NULL
, addr
,
1680 buf
, (void *) addr
, len
);
1687 mmap_read_unlock(mm
);
1693 * access_remote_vm - access another process' address space
1694 * @mm: the mm_struct of the target address space
1695 * @addr: start address to access
1696 * @buf: source or destination buffer
1697 * @len: number of bytes to transfer
1698 * @gup_flags: flags modifying lookup behaviour
1700 * The caller must hold a reference on @mm.
1702 int access_remote_vm(struct mm_struct
*mm
, unsigned long addr
,
1703 void *buf
, int len
, unsigned int gup_flags
)
1705 return __access_remote_vm(mm
, addr
, buf
, len
, gup_flags
);
1709 * Access another process' address space.
1710 * - source/target buffer must be kernel space
1712 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
,
1713 unsigned int gup_flags
)
1715 struct mm_struct
*mm
;
1717 if (addr
+ len
< addr
)
1720 mm
= get_task_mm(tsk
);
1724 len
= __access_remote_vm(mm
, addr
, buf
, len
, gup_flags
);
1729 EXPORT_SYMBOL_GPL(access_process_vm
);
1732 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1733 * @inode: The inode to check
1734 * @size: The current filesize of the inode
1735 * @newsize: The proposed filesize of the inode
1737 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1738 * make sure that any outstanding VMAs aren't broken and then shrink the
1739 * vm_regions that extend beyond so that do_mmap() doesn't
1740 * automatically grant mappings that are too large.
1742 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
1745 struct vm_area_struct
*vma
;
1746 struct vm_region
*region
;
1748 size_t r_size
, r_top
;
1750 low
= newsize
>> PAGE_SHIFT
;
1751 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1753 down_write(&nommu_region_sem
);
1754 i_mmap_lock_read(inode
->i_mapping
);
1756 /* search for VMAs that fall within the dead zone */
1757 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, low
, high
) {
1758 /* found one - only interested if it's shared out of the page
1760 if (vma
->vm_flags
& VM_SHARED
) {
1761 i_mmap_unlock_read(inode
->i_mapping
);
1762 up_write(&nommu_region_sem
);
1763 return -ETXTBSY
; /* not quite true, but near enough */
1767 /* reduce any regions that overlap the dead zone - if in existence,
1768 * these will be pointed to by VMAs that don't overlap the dead zone
1770 * we don't check for any regions that start beyond the EOF as there
1773 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, 0, ULONG_MAX
) {
1774 if (!(vma
->vm_flags
& VM_SHARED
))
1777 region
= vma
->vm_region
;
1778 r_size
= region
->vm_top
- region
->vm_start
;
1779 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
1781 if (r_top
> newsize
) {
1782 region
->vm_top
-= r_top
- newsize
;
1783 if (region
->vm_end
> region
->vm_top
)
1784 region
->vm_end
= region
->vm_top
;
1788 i_mmap_unlock_read(inode
->i_mapping
);
1789 up_write(&nommu_region_sem
);
1794 * Initialise sysctl_user_reserve_kbytes.
1796 * This is intended to prevent a user from starting a single memory hogging
1797 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1800 * The default value is min(3% of free memory, 128MB)
1801 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1803 static int __meminit
init_user_reserve(void)
1805 unsigned long free_kbytes
;
1807 free_kbytes
= global_zone_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
1809 sysctl_user_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 17);
1812 subsys_initcall(init_user_reserve
);
1815 * Initialise sysctl_admin_reserve_kbytes.
1817 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1818 * to log in and kill a memory hogging process.
1820 * Systems with more than 256MB will reserve 8MB, enough to recover
1821 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1822 * only reserve 3% of free pages by default.
1824 static int __meminit
init_admin_reserve(void)
1826 unsigned long free_kbytes
;
1828 free_kbytes
= global_zone_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
1830 sysctl_admin_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 13);
1833 subsys_initcall(init_admin_reserve
);