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/nommu-mmap.txt
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/blkdev.h>
31 #include <linux/backing-dev.h>
32 #include <linux/compiler.h>
33 #include <linux/mount.h>
34 #include <linux/personality.h>
35 #include <linux/security.h>
36 #include <linux/syscalls.h>
37 #include <linux/audit.h>
38 #include <linux/printk.h>
40 #include <linux/uaccess.h>
42 #include <asm/tlbflush.h>
43 #include <asm/mmu_context.h>
47 EXPORT_SYMBOL(high_memory
);
49 unsigned long max_mapnr
;
50 EXPORT_SYMBOL(max_mapnr
);
51 unsigned long highest_memmap_pfn
;
52 int sysctl_nr_trim_pages
= CONFIG_NOMMU_INITIAL_TRIM_EXCESS
;
53 int heap_stack_gap
= 0;
55 atomic_long_t mmap_pages_allocated
;
57 EXPORT_SYMBOL(mem_map
);
59 /* list of mapped, potentially shareable regions */
60 static struct kmem_cache
*vm_region_jar
;
61 struct rb_root nommu_region_tree
= RB_ROOT
;
62 DECLARE_RWSEM(nommu_region_sem
);
64 const struct vm_operations_struct generic_file_vm_ops
= {
68 * Return the total memory allocated for this pointer, not
69 * just what the caller asked for.
71 * Doesn't have to be accurate, i.e. may have races.
73 unsigned int kobjsize(const void *objp
)
78 * If the object we have should not have ksize performed on it,
81 if (!objp
|| !virt_addr_valid(objp
))
84 page
= virt_to_head_page(objp
);
87 * If the allocator sets PageSlab, we know the pointer came from
94 * If it's not a compound page, see if we have a matching VMA
95 * region. This test is intentionally done in reverse order,
96 * so if there's no VMA, we still fall through and hand back
97 * PAGE_SIZE for 0-order pages.
99 if (!PageCompound(page
)) {
100 struct vm_area_struct
*vma
;
102 vma
= find_vma(current
->mm
, (unsigned long)objp
);
104 return vma
->vm_end
- vma
->vm_start
;
108 * The ksize() function is only guaranteed to work for pointers
109 * returned by kmalloc(). So handle arbitrary pointers here.
111 return PAGE_SIZE
<< compound_order(page
);
114 static long __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
115 unsigned long start
, unsigned long nr_pages
,
116 unsigned int foll_flags
, struct page
**pages
,
117 struct vm_area_struct
**vmas
, int *nonblocking
)
119 struct vm_area_struct
*vma
;
120 unsigned long vm_flags
;
123 /* calculate required read or write permissions.
124 * If FOLL_FORCE is set, we only require the "MAY" flags.
126 vm_flags
= (foll_flags
& FOLL_WRITE
) ?
127 (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
128 vm_flags
&= (foll_flags
& FOLL_FORCE
) ?
129 (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
131 for (i
= 0; i
< nr_pages
; i
++) {
132 vma
= find_vma(mm
, start
);
134 goto finish_or_fault
;
136 /* protect what we can, including chardevs */
137 if ((vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)) ||
138 !(vm_flags
& vma
->vm_flags
))
139 goto finish_or_fault
;
142 pages
[i
] = virt_to_page(start
);
148 start
= (start
+ PAGE_SIZE
) & PAGE_MASK
;
154 return i
? : -EFAULT
;
158 * get a list of pages in an address range belonging to the specified process
159 * and indicate the VMA that covers each page
160 * - this is potentially dodgy as we may end incrementing the page count of a
161 * slab page or a secondary page from a compound page
162 * - don't permit access to VMAs that don't support it, such as I/O mappings
164 long get_user_pages(unsigned long start
, unsigned long nr_pages
,
165 unsigned int gup_flags
, struct page
**pages
,
166 struct vm_area_struct
**vmas
)
168 return __get_user_pages(current
, current
->mm
, start
, nr_pages
,
169 gup_flags
, pages
, vmas
, NULL
);
171 EXPORT_SYMBOL(get_user_pages
);
173 long get_user_pages_locked(unsigned long start
, unsigned long nr_pages
,
174 unsigned int gup_flags
, struct page
**pages
,
177 return get_user_pages(start
, nr_pages
, gup_flags
, pages
, NULL
);
179 EXPORT_SYMBOL(get_user_pages_locked
);
181 static long __get_user_pages_unlocked(struct task_struct
*tsk
,
182 struct mm_struct
*mm
, unsigned long start
,
183 unsigned long nr_pages
, struct page
**pages
,
184 unsigned int gup_flags
)
187 down_read(&mm
->mmap_sem
);
188 ret
= __get_user_pages(tsk
, mm
, start
, nr_pages
, gup_flags
, pages
,
190 up_read(&mm
->mmap_sem
);
194 long get_user_pages_unlocked(unsigned long start
, unsigned long nr_pages
,
195 struct page
**pages
, unsigned int gup_flags
)
197 return __get_user_pages_unlocked(current
, current
->mm
, start
, nr_pages
,
200 EXPORT_SYMBOL(get_user_pages_unlocked
);
203 * follow_pfn - look up PFN at a user virtual address
204 * @vma: memory mapping
205 * @address: user virtual address
206 * @pfn: location to store found PFN
208 * Only IO mappings and raw PFN mappings are allowed.
210 * Returns zero and the pfn at @pfn on success, -ve otherwise.
212 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
215 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
218 *pfn
= address
>> PAGE_SHIFT
;
221 EXPORT_SYMBOL(follow_pfn
);
223 LIST_HEAD(vmap_area_list
);
225 void vfree(const void *addr
)
229 EXPORT_SYMBOL(vfree
);
231 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
234 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
235 * returns only a logical address.
237 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
239 EXPORT_SYMBOL(__vmalloc
);
241 void *__vmalloc_node_flags(unsigned long size
, int node
, gfp_t flags
)
243 return __vmalloc(size
, flags
, PAGE_KERNEL
);
246 void *vmalloc_user(unsigned long size
)
250 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_ZERO
, PAGE_KERNEL
);
252 struct vm_area_struct
*vma
;
254 down_write(¤t
->mm
->mmap_sem
);
255 vma
= find_vma(current
->mm
, (unsigned long)ret
);
257 vma
->vm_flags
|= VM_USERMAP
;
258 up_write(¤t
->mm
->mmap_sem
);
263 EXPORT_SYMBOL(vmalloc_user
);
265 struct page
*vmalloc_to_page(const void *addr
)
267 return virt_to_page(addr
);
269 EXPORT_SYMBOL(vmalloc_to_page
);
271 unsigned long vmalloc_to_pfn(const void *addr
)
273 return page_to_pfn(virt_to_page(addr
));
275 EXPORT_SYMBOL(vmalloc_to_pfn
);
277 long vread(char *buf
, char *addr
, unsigned long count
)
279 /* Don't allow overflow */
280 if ((unsigned long) buf
+ count
< count
)
281 count
= -(unsigned long) buf
;
283 memcpy(buf
, addr
, count
);
287 long vwrite(char *buf
, char *addr
, unsigned long count
)
289 /* Don't allow overflow */
290 if ((unsigned long) addr
+ count
< count
)
291 count
= -(unsigned long) addr
;
293 memcpy(addr
, buf
, count
);
298 * vmalloc - allocate virtually contiguous memory
300 * @size: allocation size
302 * Allocate enough pages to cover @size from the page level
303 * allocator and map them into contiguous kernel virtual space.
305 * For tight control over page level allocator and protection flags
306 * use __vmalloc() instead.
308 void *vmalloc(unsigned long size
)
310 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
312 EXPORT_SYMBOL(vmalloc
);
315 * vzalloc - allocate virtually contiguous memory with zero fill
317 * @size: allocation size
319 * Allocate enough pages to cover @size from the page level
320 * allocator and map them into contiguous kernel virtual space.
321 * The memory allocated is set to zero.
323 * For tight control over page level allocator and protection flags
324 * use __vmalloc() instead.
326 void *vzalloc(unsigned long size
)
328 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
331 EXPORT_SYMBOL(vzalloc
);
334 * vmalloc_node - allocate memory on a specific node
335 * @size: allocation size
338 * Allocate enough pages to cover @size from the page level
339 * allocator and map them into contiguous kernel virtual space.
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
344 void *vmalloc_node(unsigned long size
, int node
)
346 return vmalloc(size
);
348 EXPORT_SYMBOL(vmalloc_node
);
351 * vzalloc_node - allocate memory on a specific node with zero fill
352 * @size: allocation size
355 * Allocate enough pages to cover @size from the page level
356 * allocator and map them into contiguous kernel virtual space.
357 * The memory allocated is set to zero.
359 * For tight control over page level allocator and protection flags
360 * use __vmalloc() instead.
362 void *vzalloc_node(unsigned long size
, int node
)
364 return vzalloc(size
);
366 EXPORT_SYMBOL(vzalloc_node
);
369 * vmalloc_exec - allocate virtually contiguous, executable memory
370 * @size: allocation size
372 * Kernel-internal function to allocate enough pages to cover @size
373 * the page level allocator and map them into contiguous and
374 * executable kernel virtual space.
376 * For tight control over page level allocator and protection flags
377 * use __vmalloc() instead.
380 void *vmalloc_exec(unsigned long size
)
382 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
386 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
387 * @size: allocation size
389 * Allocate enough 32bit PA addressable pages to cover @size from the
390 * page level allocator and map them into contiguous kernel virtual space.
392 void *vmalloc_32(unsigned long size
)
394 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
396 EXPORT_SYMBOL(vmalloc_32
);
399 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
400 * @size: allocation size
402 * The resulting memory area is 32bit addressable and zeroed so it can be
403 * mapped to userspace without leaking data.
405 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
406 * remap_vmalloc_range() are permissible.
408 void *vmalloc_32_user(unsigned long size
)
411 * We'll have to sort out the ZONE_DMA bits for 64-bit,
412 * but for now this can simply use vmalloc_user() directly.
414 return vmalloc_user(size
);
416 EXPORT_SYMBOL(vmalloc_32_user
);
418 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
425 void vunmap(const void *addr
)
429 EXPORT_SYMBOL(vunmap
);
431 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
, pgprot_t prot
)
436 EXPORT_SYMBOL(vm_map_ram
);
438 void vm_unmap_ram(const void *mem
, unsigned int count
)
442 EXPORT_SYMBOL(vm_unmap_ram
);
444 void vm_unmap_aliases(void)
447 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
450 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
453 void __weak
vmalloc_sync_all(void)
457 struct vm_struct
*alloc_vm_area(size_t size
, pte_t
**ptes
)
462 EXPORT_SYMBOL_GPL(alloc_vm_area
);
464 void free_vm_area(struct vm_struct
*area
)
468 EXPORT_SYMBOL_GPL(free_vm_area
);
470 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
475 EXPORT_SYMBOL(vm_insert_page
);
477 int vm_map_pages(struct vm_area_struct
*vma
, struct page
**pages
,
482 EXPORT_SYMBOL(vm_map_pages
);
484 int vm_map_pages_zero(struct vm_area_struct
*vma
, struct page
**pages
,
489 EXPORT_SYMBOL(vm_map_pages_zero
);
492 * sys_brk() for the most part doesn't need the global kernel
493 * lock, except when an application is doing something nasty
494 * like trying to un-brk an area that has already been mapped
495 * to a regular file. in this case, the unmapping will need
496 * to invoke file system routines that need the global lock.
498 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
500 struct mm_struct
*mm
= current
->mm
;
502 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
509 * Always allow shrinking brk
511 if (brk
<= mm
->brk
) {
517 * Ok, looks good - let it rip.
519 flush_icache_range(mm
->brk
, brk
);
520 return mm
->brk
= brk
;
524 * initialise the percpu counter for VM and region record slabs
526 void __init
mmap_init(void)
530 ret
= percpu_counter_init(&vm_committed_as
, 0, GFP_KERNEL
);
532 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
|SLAB_ACCOUNT
);
536 * validate the region tree
537 * - the caller must hold the region lock
539 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
540 static noinline
void validate_nommu_regions(void)
542 struct vm_region
*region
, *last
;
543 struct rb_node
*p
, *lastp
;
545 lastp
= rb_first(&nommu_region_tree
);
549 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
550 BUG_ON(last
->vm_end
<= last
->vm_start
);
551 BUG_ON(last
->vm_top
< last
->vm_end
);
553 while ((p
= rb_next(lastp
))) {
554 region
= rb_entry(p
, struct vm_region
, vm_rb
);
555 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
557 BUG_ON(region
->vm_end
<= region
->vm_start
);
558 BUG_ON(region
->vm_top
< region
->vm_end
);
559 BUG_ON(region
->vm_start
< last
->vm_top
);
565 static void validate_nommu_regions(void)
571 * add a region into the global tree
573 static void add_nommu_region(struct vm_region
*region
)
575 struct vm_region
*pregion
;
576 struct rb_node
**p
, *parent
;
578 validate_nommu_regions();
581 p
= &nommu_region_tree
.rb_node
;
584 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
585 if (region
->vm_start
< pregion
->vm_start
)
587 else if (region
->vm_start
> pregion
->vm_start
)
589 else if (pregion
== region
)
595 rb_link_node(®ion
->vm_rb
, parent
, p
);
596 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
598 validate_nommu_regions();
602 * delete a region from the global tree
604 static void delete_nommu_region(struct vm_region
*region
)
606 BUG_ON(!nommu_region_tree
.rb_node
);
608 validate_nommu_regions();
609 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
610 validate_nommu_regions();
614 * free a contiguous series of pages
616 static void free_page_series(unsigned long from
, unsigned long to
)
618 for (; from
< to
; from
+= PAGE_SIZE
) {
619 struct page
*page
= virt_to_page(from
);
621 atomic_long_dec(&mmap_pages_allocated
);
627 * release a reference to a region
628 * - the caller must hold the region semaphore for writing, which this releases
629 * - the region may not have been added to the tree yet, in which case vm_top
630 * will equal vm_start
632 static void __put_nommu_region(struct vm_region
*region
)
633 __releases(nommu_region_sem
)
635 BUG_ON(!nommu_region_tree
.rb_node
);
637 if (--region
->vm_usage
== 0) {
638 if (region
->vm_top
> region
->vm_start
)
639 delete_nommu_region(region
);
640 up_write(&nommu_region_sem
);
643 fput(region
->vm_file
);
645 /* IO memory and memory shared directly out of the pagecache
646 * from ramfs/tmpfs mustn't be released here */
647 if (region
->vm_flags
& VM_MAPPED_COPY
)
648 free_page_series(region
->vm_start
, region
->vm_top
);
649 kmem_cache_free(vm_region_jar
, region
);
651 up_write(&nommu_region_sem
);
656 * release a reference to a region
658 static void put_nommu_region(struct vm_region
*region
)
660 down_write(&nommu_region_sem
);
661 __put_nommu_region(region
);
665 * add a VMA into a process's mm_struct in the appropriate place in the list
666 * and tree and add to the address space's page tree also if not an anonymous
668 * - should be called with mm->mmap_sem held writelocked
670 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
672 struct vm_area_struct
*pvma
, *prev
;
673 struct address_space
*mapping
;
674 struct rb_node
**p
, *parent
, *rb_prev
;
676 BUG_ON(!vma
->vm_region
);
681 /* add the VMA to the mapping */
683 mapping
= vma
->vm_file
->f_mapping
;
685 i_mmap_lock_write(mapping
);
686 flush_dcache_mmap_lock(mapping
);
687 vma_interval_tree_insert(vma
, &mapping
->i_mmap
);
688 flush_dcache_mmap_unlock(mapping
);
689 i_mmap_unlock_write(mapping
);
692 /* add the VMA to the tree */
693 parent
= rb_prev
= NULL
;
694 p
= &mm
->mm_rb
.rb_node
;
697 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
699 /* sort by: start addr, end addr, VMA struct addr in that order
700 * (the latter is necessary as we may get identical VMAs) */
701 if (vma
->vm_start
< pvma
->vm_start
)
703 else if (vma
->vm_start
> pvma
->vm_start
) {
706 } else if (vma
->vm_end
< pvma
->vm_end
)
708 else if (vma
->vm_end
> pvma
->vm_end
) {
711 } else if (vma
< pvma
)
713 else if (vma
> pvma
) {
720 rb_link_node(&vma
->vm_rb
, parent
, p
);
721 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
723 /* add VMA to the VMA list also */
726 prev
= rb_entry(rb_prev
, struct vm_area_struct
, vm_rb
);
728 __vma_link_list(mm
, vma
, prev
, parent
);
732 * delete a VMA from its owning mm_struct and address space
734 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
737 struct address_space
*mapping
;
738 struct mm_struct
*mm
= vma
->vm_mm
;
739 struct task_struct
*curr
= current
;
742 for (i
= 0; i
< VMACACHE_SIZE
; i
++) {
743 /* if the vma is cached, invalidate the entire cache */
744 if (curr
->vmacache
.vmas
[i
] == vma
) {
745 vmacache_invalidate(mm
);
750 /* remove the VMA from the mapping */
752 mapping
= vma
->vm_file
->f_mapping
;
754 i_mmap_lock_write(mapping
);
755 flush_dcache_mmap_lock(mapping
);
756 vma_interval_tree_remove(vma
, &mapping
->i_mmap
);
757 flush_dcache_mmap_unlock(mapping
);
758 i_mmap_unlock_write(mapping
);
761 /* remove from the MM's tree and list */
762 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
765 vma
->vm_prev
->vm_next
= vma
->vm_next
;
767 mm
->mmap
= vma
->vm_next
;
770 vma
->vm_next
->vm_prev
= vma
->vm_prev
;
774 * destroy a VMA record
776 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
778 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
779 vma
->vm_ops
->close(vma
);
782 put_nommu_region(vma
->vm_region
);
787 * look up the first VMA in which addr resides, NULL if none
788 * - should be called with mm->mmap_sem at least held readlocked
790 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
792 struct vm_area_struct
*vma
;
794 /* check the cache first */
795 vma
= vmacache_find(mm
, addr
);
799 /* trawl the list (there may be multiple mappings in which addr
801 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
802 if (vma
->vm_start
> addr
)
804 if (vma
->vm_end
> addr
) {
805 vmacache_update(addr
, vma
);
812 EXPORT_SYMBOL(find_vma
);
816 * - we don't extend stack VMAs under NOMMU conditions
818 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
820 return find_vma(mm
, addr
);
824 * expand a stack to a given address
825 * - not supported under NOMMU conditions
827 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
833 * look up the first VMA exactly that exactly matches addr
834 * - should be called with mm->mmap_sem at least held readlocked
836 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
840 struct vm_area_struct
*vma
;
841 unsigned long end
= addr
+ len
;
843 /* check the cache first */
844 vma
= vmacache_find_exact(mm
, addr
, end
);
848 /* trawl the list (there may be multiple mappings in which addr
850 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
851 if (vma
->vm_start
< addr
)
853 if (vma
->vm_start
> addr
)
855 if (vma
->vm_end
== end
) {
856 vmacache_update(addr
, vma
);
865 * determine whether a mapping should be permitted and, if so, what sort of
866 * mapping we're capable of supporting
868 static int validate_mmap_request(struct file
*file
,
874 unsigned long *_capabilities
)
876 unsigned long capabilities
, rlen
;
879 /* do the simple checks first */
880 if (flags
& MAP_FIXED
)
883 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
884 (flags
& MAP_TYPE
) != MAP_SHARED
)
890 /* Careful about overflows.. */
891 rlen
= PAGE_ALIGN(len
);
892 if (!rlen
|| rlen
> TASK_SIZE
)
895 /* offset overflow? */
896 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
900 /* files must support mmap */
901 if (!file
->f_op
->mmap
)
904 /* work out if what we've got could possibly be shared
905 * - we support chardevs that provide their own "memory"
906 * - we support files/blockdevs that are memory backed
908 if (file
->f_op
->mmap_capabilities
) {
909 capabilities
= file
->f_op
->mmap_capabilities(file
);
911 /* no explicit capabilities set, so assume some
913 switch (file_inode(file
)->i_mode
& S_IFMT
) {
916 capabilities
= NOMMU_MAP_COPY
;
931 /* eliminate any capabilities that we can't support on this
933 if (!file
->f_op
->get_unmapped_area
)
934 capabilities
&= ~NOMMU_MAP_DIRECT
;
935 if (!(file
->f_mode
& FMODE_CAN_READ
))
936 capabilities
&= ~NOMMU_MAP_COPY
;
938 /* The file shall have been opened with read permission. */
939 if (!(file
->f_mode
& FMODE_READ
))
942 if (flags
& MAP_SHARED
) {
943 /* do checks for writing, appending and locking */
944 if ((prot
& PROT_WRITE
) &&
945 !(file
->f_mode
& FMODE_WRITE
))
948 if (IS_APPEND(file_inode(file
)) &&
949 (file
->f_mode
& FMODE_WRITE
))
952 if (locks_verify_locked(file
))
955 if (!(capabilities
& NOMMU_MAP_DIRECT
))
958 /* we mustn't privatise shared mappings */
959 capabilities
&= ~NOMMU_MAP_COPY
;
961 /* we're going to read the file into private memory we
963 if (!(capabilities
& NOMMU_MAP_COPY
))
966 /* we don't permit a private writable mapping to be
967 * shared with the backing device */
968 if (prot
& PROT_WRITE
)
969 capabilities
&= ~NOMMU_MAP_DIRECT
;
972 if (capabilities
& NOMMU_MAP_DIRECT
) {
973 if (((prot
& PROT_READ
) && !(capabilities
& NOMMU_MAP_READ
)) ||
974 ((prot
& PROT_WRITE
) && !(capabilities
& NOMMU_MAP_WRITE
)) ||
975 ((prot
& PROT_EXEC
) && !(capabilities
& NOMMU_MAP_EXEC
))
977 capabilities
&= ~NOMMU_MAP_DIRECT
;
978 if (flags
& MAP_SHARED
) {
979 pr_warn("MAP_SHARED not completely supported on !MMU\n");
985 /* handle executable mappings and implied executable
987 if (path_noexec(&file
->f_path
)) {
988 if (prot
& PROT_EXEC
)
990 } else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
991 /* handle implication of PROT_EXEC by PROT_READ */
992 if (current
->personality
& READ_IMPLIES_EXEC
) {
993 if (capabilities
& NOMMU_MAP_EXEC
)
996 } else if ((prot
& PROT_READ
) &&
997 (prot
& PROT_EXEC
) &&
998 !(capabilities
& NOMMU_MAP_EXEC
)
1000 /* backing file is not executable, try to copy */
1001 capabilities
&= ~NOMMU_MAP_DIRECT
;
1004 /* anonymous mappings are always memory backed and can be
1007 capabilities
= NOMMU_MAP_COPY
;
1009 /* handle PROT_EXEC implication by PROT_READ */
1010 if ((prot
& PROT_READ
) &&
1011 (current
->personality
& READ_IMPLIES_EXEC
))
1015 /* allow the security API to have its say */
1016 ret
= security_mmap_addr(addr
);
1021 *_capabilities
= capabilities
;
1026 * we've determined that we can make the mapping, now translate what we
1027 * now know into VMA flags
1029 static unsigned long determine_vm_flags(struct file
*file
,
1031 unsigned long flags
,
1032 unsigned long capabilities
)
1034 unsigned long vm_flags
;
1036 vm_flags
= calc_vm_prot_bits(prot
, 0) | calc_vm_flag_bits(flags
);
1037 /* vm_flags |= mm->def_flags; */
1039 if (!(capabilities
& NOMMU_MAP_DIRECT
)) {
1040 /* attempt to share read-only copies of mapped file chunks */
1041 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1042 if (file
&& !(prot
& PROT_WRITE
))
1043 vm_flags
|= VM_MAYSHARE
;
1045 /* overlay a shareable mapping on the backing device or inode
1046 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1048 vm_flags
|= VM_MAYSHARE
| (capabilities
& NOMMU_VMFLAGS
);
1049 if (flags
& MAP_SHARED
)
1050 vm_flags
|= VM_SHARED
;
1053 /* refuse to let anyone share private mappings with this process if
1054 * it's being traced - otherwise breakpoints set in it may interfere
1055 * with another untraced process
1057 if ((flags
& MAP_PRIVATE
) && current
->ptrace
)
1058 vm_flags
&= ~VM_MAYSHARE
;
1064 * set up a shared mapping on a file (the driver or filesystem provides and
1067 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1071 ret
= call_mmap(vma
->vm_file
, vma
);
1073 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1079 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1080 * opposed to tried but failed) so we can only give a suitable error as
1081 * it's not possible to make a private copy if MAP_SHARED was given */
1086 * set up a private mapping or an anonymous shared mapping
1088 static int do_mmap_private(struct vm_area_struct
*vma
,
1089 struct vm_region
*region
,
1091 unsigned long capabilities
)
1093 unsigned long total
, point
;
1097 /* invoke the file's mapping function so that it can keep track of
1098 * shared mappings on devices or memory
1099 * - VM_MAYSHARE will be set if it may attempt to share
1101 if (capabilities
& NOMMU_MAP_DIRECT
) {
1102 ret
= call_mmap(vma
->vm_file
, vma
);
1104 /* shouldn't return success if we're not sharing */
1105 BUG_ON(!(vma
->vm_flags
& VM_MAYSHARE
));
1106 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1112 /* getting an ENOSYS error indicates that direct mmap isn't
1113 * possible (as opposed to tried but failed) so we'll try to
1114 * make a private copy of the data and map that instead */
1118 /* allocate some memory to hold the mapping
1119 * - note that this may not return a page-aligned address if the object
1120 * we're allocating is smaller than a page
1122 order
= get_order(len
);
1124 point
= len
>> PAGE_SHIFT
;
1126 /* we don't want to allocate a power-of-2 sized page set */
1127 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
)
1130 base
= alloc_pages_exact(total
<< PAGE_SHIFT
, GFP_KERNEL
);
1134 atomic_long_add(total
, &mmap_pages_allocated
);
1136 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1137 region
->vm_start
= (unsigned long) base
;
1138 region
->vm_end
= region
->vm_start
+ len
;
1139 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1141 vma
->vm_start
= region
->vm_start
;
1142 vma
->vm_end
= region
->vm_start
+ len
;
1145 /* read the contents of a file into the copy */
1148 fpos
= vma
->vm_pgoff
;
1149 fpos
<<= PAGE_SHIFT
;
1151 ret
= kernel_read(vma
->vm_file
, base
, len
, &fpos
);
1155 /* clear the last little bit */
1157 memset(base
+ ret
, 0, len
- ret
);
1160 vma_set_anonymous(vma
);
1166 free_page_series(region
->vm_start
, region
->vm_top
);
1167 region
->vm_start
= vma
->vm_start
= 0;
1168 region
->vm_end
= vma
->vm_end
= 0;
1173 pr_err("Allocation of length %lu from process %d (%s) failed\n",
1174 len
, current
->pid
, current
->comm
);
1175 show_free_areas(0, NULL
);
1180 * handle mapping creation for uClinux
1182 unsigned long do_mmap(struct file
*file
,
1186 unsigned long flags
,
1187 vm_flags_t vm_flags
,
1188 unsigned long pgoff
,
1189 unsigned long *populate
,
1190 struct list_head
*uf
)
1192 struct vm_area_struct
*vma
;
1193 struct vm_region
*region
;
1195 unsigned long capabilities
, result
;
1200 /* decide whether we should attempt the mapping, and if so what sort of
1202 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1207 /* we ignore the address hint */
1209 len
= PAGE_ALIGN(len
);
1211 /* we've determined that we can make the mapping, now translate what we
1212 * now know into VMA flags */
1213 vm_flags
|= determine_vm_flags(file
, prot
, flags
, capabilities
);
1215 /* we're going to need to record the mapping */
1216 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1218 goto error_getting_region
;
1220 vma
= vm_area_alloc(current
->mm
);
1222 goto error_getting_vma
;
1224 region
->vm_usage
= 1;
1225 region
->vm_flags
= vm_flags
;
1226 region
->vm_pgoff
= pgoff
;
1228 vma
->vm_flags
= vm_flags
;
1229 vma
->vm_pgoff
= pgoff
;
1232 region
->vm_file
= get_file(file
);
1233 vma
->vm_file
= get_file(file
);
1236 down_write(&nommu_region_sem
);
1238 /* if we want to share, we need to check for regions created by other
1239 * mmap() calls that overlap with our proposed mapping
1240 * - we can only share with a superset match on most regular files
1241 * - shared mappings on character devices and memory backed files are
1242 * permitted to overlap inexactly as far as we are concerned for in
1243 * these cases, sharing is handled in the driver or filesystem rather
1246 if (vm_flags
& VM_MAYSHARE
) {
1247 struct vm_region
*pregion
;
1248 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1250 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1251 pgend
= pgoff
+ pglen
;
1253 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1254 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1256 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1259 /* search for overlapping mappings on the same file */
1260 if (file_inode(pregion
->vm_file
) !=
1264 if (pregion
->vm_pgoff
>= pgend
)
1267 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1268 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1269 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1270 if (pgoff
>= rpgend
)
1273 /* handle inexactly overlapping matches between
1275 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1276 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1277 /* new mapping is not a subset of the region */
1278 if (!(capabilities
& NOMMU_MAP_DIRECT
))
1279 goto sharing_violation
;
1283 /* we've found a region we can share */
1284 pregion
->vm_usage
++;
1285 vma
->vm_region
= pregion
;
1286 start
= pregion
->vm_start
;
1287 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1288 vma
->vm_start
= start
;
1289 vma
->vm_end
= start
+ len
;
1291 if (pregion
->vm_flags
& VM_MAPPED_COPY
)
1292 vma
->vm_flags
|= VM_MAPPED_COPY
;
1294 ret
= do_mmap_shared_file(vma
);
1296 vma
->vm_region
= NULL
;
1299 pregion
->vm_usage
--;
1301 goto error_just_free
;
1304 fput(region
->vm_file
);
1305 kmem_cache_free(vm_region_jar
, region
);
1311 /* obtain the address at which to make a shared mapping
1312 * - this is the hook for quasi-memory character devices to
1313 * tell us the location of a shared mapping
1315 if (capabilities
& NOMMU_MAP_DIRECT
) {
1316 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1318 if (IS_ERR_VALUE(addr
)) {
1321 goto error_just_free
;
1323 /* the driver refused to tell us where to site
1324 * the mapping so we'll have to attempt to copy
1327 if (!(capabilities
& NOMMU_MAP_COPY
))
1328 goto error_just_free
;
1330 capabilities
&= ~NOMMU_MAP_DIRECT
;
1332 vma
->vm_start
= region
->vm_start
= addr
;
1333 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1338 vma
->vm_region
= region
;
1340 /* set up the mapping
1341 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1343 if (file
&& vma
->vm_flags
& VM_SHARED
)
1344 ret
= do_mmap_shared_file(vma
);
1346 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1348 goto error_just_free
;
1349 add_nommu_region(region
);
1351 /* clear anonymous mappings that don't ask for uninitialized data */
1352 if (!vma
->vm_file
&& !(flags
& MAP_UNINITIALIZED
))
1353 memset((void *)region
->vm_start
, 0,
1354 region
->vm_end
- region
->vm_start
);
1356 /* okay... we have a mapping; now we have to register it */
1357 result
= vma
->vm_start
;
1359 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1362 add_vma_to_mm(current
->mm
, vma
);
1364 /* we flush the region from the icache only when the first executable
1365 * mapping of it is made */
1366 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1367 flush_icache_range(region
->vm_start
, region
->vm_end
);
1368 region
->vm_icache_flushed
= true;
1371 up_write(&nommu_region_sem
);
1376 up_write(&nommu_region_sem
);
1378 if (region
->vm_file
)
1379 fput(region
->vm_file
);
1380 kmem_cache_free(vm_region_jar
, region
);
1387 up_write(&nommu_region_sem
);
1388 pr_warn("Attempt to share mismatched mappings\n");
1393 kmem_cache_free(vm_region_jar
, region
);
1394 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1396 show_free_areas(0, NULL
);
1399 error_getting_region
:
1400 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1402 show_free_areas(0, NULL
);
1406 unsigned long ksys_mmap_pgoff(unsigned long addr
, unsigned long len
,
1407 unsigned long prot
, unsigned long flags
,
1408 unsigned long fd
, unsigned long pgoff
)
1410 struct file
*file
= NULL
;
1411 unsigned long retval
= -EBADF
;
1413 audit_mmap_fd(fd
, flags
);
1414 if (!(flags
& MAP_ANONYMOUS
)) {
1420 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1422 retval
= vm_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1430 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1431 unsigned long, prot
, unsigned long, flags
,
1432 unsigned long, fd
, unsigned long, pgoff
)
1434 return ksys_mmap_pgoff(addr
, len
, prot
, flags
, fd
, pgoff
);
1437 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1438 struct mmap_arg_struct
{
1442 unsigned long flags
;
1444 unsigned long offset
;
1447 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1449 struct mmap_arg_struct a
;
1451 if (copy_from_user(&a
, arg
, sizeof(a
)))
1453 if (offset_in_page(a
.offset
))
1456 return ksys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1457 a
.offset
>> PAGE_SHIFT
);
1459 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1462 * split a vma into two pieces at address 'addr', a new vma is allocated either
1463 * for the first part or the tail.
1465 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1466 unsigned long addr
, int new_below
)
1468 struct vm_area_struct
*new;
1469 struct vm_region
*region
;
1470 unsigned long npages
;
1472 /* we're only permitted to split anonymous regions (these should have
1473 * only a single usage on the region) */
1477 if (mm
->map_count
>= sysctl_max_map_count
)
1480 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1484 new = vm_area_dup(vma
);
1486 kmem_cache_free(vm_region_jar
, region
);
1490 /* most fields are the same, copy all, and then fixup */
1491 *region
= *vma
->vm_region
;
1492 new->vm_region
= region
;
1494 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1497 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1499 region
->vm_start
= new->vm_start
= addr
;
1500 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1503 if (new->vm_ops
&& new->vm_ops
->open
)
1504 new->vm_ops
->open(new);
1506 delete_vma_from_mm(vma
);
1507 down_write(&nommu_region_sem
);
1508 delete_nommu_region(vma
->vm_region
);
1510 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1511 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1513 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1514 vma
->vm_region
->vm_top
= addr
;
1516 add_nommu_region(vma
->vm_region
);
1517 add_nommu_region(new->vm_region
);
1518 up_write(&nommu_region_sem
);
1519 add_vma_to_mm(mm
, vma
);
1520 add_vma_to_mm(mm
, new);
1525 * shrink a VMA by removing the specified chunk from either the beginning or
1528 static int shrink_vma(struct mm_struct
*mm
,
1529 struct vm_area_struct
*vma
,
1530 unsigned long from
, unsigned long to
)
1532 struct vm_region
*region
;
1534 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1536 delete_vma_from_mm(vma
);
1537 if (from
> vma
->vm_start
)
1541 add_vma_to_mm(mm
, vma
);
1543 /* cut the backing region down to size */
1544 region
= vma
->vm_region
;
1545 BUG_ON(region
->vm_usage
!= 1);
1547 down_write(&nommu_region_sem
);
1548 delete_nommu_region(region
);
1549 if (from
> region
->vm_start
) {
1550 to
= region
->vm_top
;
1551 region
->vm_top
= region
->vm_end
= from
;
1553 region
->vm_start
= to
;
1555 add_nommu_region(region
);
1556 up_write(&nommu_region_sem
);
1558 free_page_series(from
, to
);
1564 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1565 * VMA, though it need not cover the whole VMA
1567 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
, struct list_head
*uf
)
1569 struct vm_area_struct
*vma
;
1573 len
= PAGE_ALIGN(len
);
1579 /* find the first potentially overlapping VMA */
1580 vma
= find_vma(mm
, start
);
1584 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1585 current
->pid
, current
->comm
,
1586 start
, start
+ len
- 1);
1592 /* we're allowed to split an anonymous VMA but not a file-backed one */
1595 if (start
> vma
->vm_start
)
1597 if (end
== vma
->vm_end
)
1598 goto erase_whole_vma
;
1603 /* the chunk must be a subset of the VMA found */
1604 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1605 goto erase_whole_vma
;
1606 if (start
< vma
->vm_start
|| end
> vma
->vm_end
)
1608 if (offset_in_page(start
))
1610 if (end
!= vma
->vm_end
&& offset_in_page(end
))
1612 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1613 ret
= split_vma(mm
, vma
, start
, 1);
1617 return shrink_vma(mm
, vma
, start
, end
);
1621 delete_vma_from_mm(vma
);
1622 delete_vma(mm
, vma
);
1625 EXPORT_SYMBOL(do_munmap
);
1627 int vm_munmap(unsigned long addr
, size_t len
)
1629 struct mm_struct
*mm
= current
->mm
;
1632 down_write(&mm
->mmap_sem
);
1633 ret
= do_munmap(mm
, addr
, len
, NULL
);
1634 up_write(&mm
->mmap_sem
);
1637 EXPORT_SYMBOL(vm_munmap
);
1639 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1641 return vm_munmap(addr
, len
);
1645 * release all the mappings made in a process's VM space
1647 void exit_mmap(struct mm_struct
*mm
)
1649 struct vm_area_struct
*vma
;
1656 while ((vma
= mm
->mmap
)) {
1657 mm
->mmap
= vma
->vm_next
;
1658 delete_vma_from_mm(vma
);
1659 delete_vma(mm
, vma
);
1664 int vm_brk(unsigned long addr
, unsigned long len
)
1670 * expand (or shrink) an existing mapping, potentially moving it at the same
1671 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1673 * under NOMMU conditions, we only permit changing a mapping's size, and only
1674 * as long as it stays within the region allocated by do_mmap_private() and the
1675 * block is not shareable
1677 * MREMAP_FIXED is not supported under NOMMU conditions
1679 static unsigned long do_mremap(unsigned long addr
,
1680 unsigned long old_len
, unsigned long new_len
,
1681 unsigned long flags
, unsigned long new_addr
)
1683 struct vm_area_struct
*vma
;
1685 /* insanity checks first */
1686 old_len
= PAGE_ALIGN(old_len
);
1687 new_len
= PAGE_ALIGN(new_len
);
1688 if (old_len
== 0 || new_len
== 0)
1689 return (unsigned long) -EINVAL
;
1691 if (offset_in_page(addr
))
1694 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1695 return (unsigned long) -EINVAL
;
1697 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1699 return (unsigned long) -EINVAL
;
1701 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1702 return (unsigned long) -EFAULT
;
1704 if (vma
->vm_flags
& VM_MAYSHARE
)
1705 return (unsigned long) -EPERM
;
1707 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1708 return (unsigned long) -ENOMEM
;
1710 /* all checks complete - do it */
1711 vma
->vm_end
= vma
->vm_start
+ new_len
;
1712 return vma
->vm_start
;
1715 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1716 unsigned long, new_len
, unsigned long, flags
,
1717 unsigned long, new_addr
)
1721 down_write(¤t
->mm
->mmap_sem
);
1722 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1723 up_write(¤t
->mm
->mmap_sem
);
1727 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1728 unsigned int foll_flags
)
1733 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long addr
,
1734 unsigned long pfn
, unsigned long size
, pgprot_t prot
)
1736 if (addr
!= (pfn
<< PAGE_SHIFT
))
1739 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
1742 EXPORT_SYMBOL(remap_pfn_range
);
1744 int vm_iomap_memory(struct vm_area_struct
*vma
, phys_addr_t start
, unsigned long len
)
1746 unsigned long pfn
= start
>> PAGE_SHIFT
;
1747 unsigned long vm_len
= vma
->vm_end
- vma
->vm_start
;
1749 pfn
+= vma
->vm_pgoff
;
1750 return io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, vm_len
, vma
->vm_page_prot
);
1752 EXPORT_SYMBOL(vm_iomap_memory
);
1754 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1755 unsigned long pgoff
)
1757 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1759 if (!(vma
->vm_flags
& VM_USERMAP
))
1762 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1763 vma
->vm_end
= vma
->vm_start
+ size
;
1767 EXPORT_SYMBOL(remap_vmalloc_range
);
1769 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1770 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1775 vm_fault_t
filemap_fault(struct vm_fault
*vmf
)
1780 EXPORT_SYMBOL(filemap_fault
);
1782 void filemap_map_pages(struct vm_fault
*vmf
,
1783 pgoff_t start_pgoff
, pgoff_t end_pgoff
)
1787 EXPORT_SYMBOL(filemap_map_pages
);
1789 int __access_remote_vm(struct task_struct
*tsk
, struct mm_struct
*mm
,
1790 unsigned long addr
, void *buf
, int len
, unsigned int gup_flags
)
1792 struct vm_area_struct
*vma
;
1793 int write
= gup_flags
& FOLL_WRITE
;
1795 down_read(&mm
->mmap_sem
);
1797 /* the access must start within one of the target process's mappings */
1798 vma
= find_vma(mm
, addr
);
1800 /* don't overrun this mapping */
1801 if (addr
+ len
>= vma
->vm_end
)
1802 len
= vma
->vm_end
- addr
;
1804 /* only read or write mappings where it is permitted */
1805 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1806 copy_to_user_page(vma
, NULL
, addr
,
1807 (void *) addr
, buf
, len
);
1808 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1809 copy_from_user_page(vma
, NULL
, addr
,
1810 buf
, (void *) addr
, len
);
1817 up_read(&mm
->mmap_sem
);
1823 * access_remote_vm - access another process' address space
1824 * @mm: the mm_struct of the target address space
1825 * @addr: start address to access
1826 * @buf: source or destination buffer
1827 * @len: number of bytes to transfer
1828 * @gup_flags: flags modifying lookup behaviour
1830 * The caller must hold a reference on @mm.
1832 int access_remote_vm(struct mm_struct
*mm
, unsigned long addr
,
1833 void *buf
, int len
, unsigned int gup_flags
)
1835 return __access_remote_vm(NULL
, mm
, addr
, buf
, len
, gup_flags
);
1839 * Access another process' address space.
1840 * - source/target buffer must be kernel space
1842 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
,
1843 unsigned int gup_flags
)
1845 struct mm_struct
*mm
;
1847 if (addr
+ len
< addr
)
1850 mm
= get_task_mm(tsk
);
1854 len
= __access_remote_vm(tsk
, mm
, addr
, buf
, len
, gup_flags
);
1859 EXPORT_SYMBOL_GPL(access_process_vm
);
1862 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1863 * @inode: The inode to check
1864 * @size: The current filesize of the inode
1865 * @newsize: The proposed filesize of the inode
1867 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1868 * make sure that that any outstanding VMAs aren't broken and then shrink the
1869 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1870 * automatically grant mappings that are too large.
1872 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
1875 struct vm_area_struct
*vma
;
1876 struct vm_region
*region
;
1878 size_t r_size
, r_top
;
1880 low
= newsize
>> PAGE_SHIFT
;
1881 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1883 down_write(&nommu_region_sem
);
1884 i_mmap_lock_read(inode
->i_mapping
);
1886 /* search for VMAs that fall within the dead zone */
1887 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, low
, high
) {
1888 /* found one - only interested if it's shared out of the page
1890 if (vma
->vm_flags
& VM_SHARED
) {
1891 i_mmap_unlock_read(inode
->i_mapping
);
1892 up_write(&nommu_region_sem
);
1893 return -ETXTBSY
; /* not quite true, but near enough */
1897 /* reduce any regions that overlap the dead zone - if in existence,
1898 * these will be pointed to by VMAs that don't overlap the dead zone
1900 * we don't check for any regions that start beyond the EOF as there
1903 vma_interval_tree_foreach(vma
, &inode
->i_mapping
->i_mmap
, 0, ULONG_MAX
) {
1904 if (!(vma
->vm_flags
& VM_SHARED
))
1907 region
= vma
->vm_region
;
1908 r_size
= region
->vm_top
- region
->vm_start
;
1909 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
1911 if (r_top
> newsize
) {
1912 region
->vm_top
-= r_top
- newsize
;
1913 if (region
->vm_end
> region
->vm_top
)
1914 region
->vm_end
= region
->vm_top
;
1918 i_mmap_unlock_read(inode
->i_mapping
);
1919 up_write(&nommu_region_sem
);
1924 * Initialise sysctl_user_reserve_kbytes.
1926 * This is intended to prevent a user from starting a single memory hogging
1927 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1930 * The default value is min(3% of free memory, 128MB)
1931 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1933 static int __meminit
init_user_reserve(void)
1935 unsigned long free_kbytes
;
1937 free_kbytes
= global_zone_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
1939 sysctl_user_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 17);
1942 subsys_initcall(init_user_reserve
);
1945 * Initialise sysctl_admin_reserve_kbytes.
1947 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1948 * to log in and kill a memory hogging process.
1950 * Systems with more than 256MB will reserve 8MB, enough to recover
1951 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1952 * only reserve 3% of free pages by default.
1954 static int __meminit
init_admin_reserve(void)
1956 unsigned long free_kbytes
;
1958 free_kbytes
= global_zone_page_state(NR_FREE_PAGES
) << (PAGE_SHIFT
- 10);
1960 sysctl_admin_reserve_kbytes
= min(free_kbytes
/ 32, 1UL << 13);
1963 subsys_initcall(init_admin_reserve
);