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-2009 Paul Mundt <lethal@linux-sh.org>
16 #include <linux/module.h>
18 #include <linux/mman.h>
19 #include <linux/swap.h>
20 #include <linux/file.h>
21 #include <linux/highmem.h>
22 #include <linux/pagemap.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/tracehook.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mount.h>
29 #include <linux/personality.h>
30 #include <linux/security.h>
31 #include <linux/syscalls.h>
33 #include <asm/uaccess.h>
35 #include <asm/tlbflush.h>
36 #include <asm/mmu_context.h>
40 #define kenter(FMT, ...) \
41 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
42 #define kleave(FMT, ...) \
43 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
44 #define kdebug(FMT, ...) \
45 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47 #define kenter(FMT, ...) \
48 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
49 #define kleave(FMT, ...) \
50 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
51 #define kdebug(FMT, ...) \
52 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
57 unsigned long max_mapnr
;
58 unsigned long num_physpages
;
59 unsigned long highest_memmap_pfn
;
60 struct percpu_counter vm_committed_as
;
61 int sysctl_overcommit_memory
= OVERCOMMIT_GUESS
; /* heuristic overcommit */
62 int sysctl_overcommit_ratio
= 50; /* default is 50% */
63 int sysctl_max_map_count
= DEFAULT_MAX_MAP_COUNT
;
64 int sysctl_nr_trim_pages
= CONFIG_NOMMU_INITIAL_TRIM_EXCESS
;
65 int heap_stack_gap
= 0;
67 atomic_long_t mmap_pages_allocated
;
69 EXPORT_SYMBOL(mem_map
);
70 EXPORT_SYMBOL(num_physpages
);
72 /* list of mapped, potentially shareable regions */
73 static struct kmem_cache
*vm_region_jar
;
74 struct rb_root nommu_region_tree
= RB_ROOT
;
75 DECLARE_RWSEM(nommu_region_sem
);
77 const struct vm_operations_struct generic_file_vm_ops
= {
81 * Return the total memory allocated for this pointer, not
82 * just what the caller asked for.
84 * Doesn't have to be accurate, i.e. may have races.
86 unsigned int kobjsize(const void *objp
)
91 * If the object we have should not have ksize performed on it,
94 if (!objp
|| !virt_addr_valid(objp
))
97 page
= virt_to_head_page(objp
);
100 * If the allocator sets PageSlab, we know the pointer came from
107 * If it's not a compound page, see if we have a matching VMA
108 * region. This test is intentionally done in reverse order,
109 * so if there's no VMA, we still fall through and hand back
110 * PAGE_SIZE for 0-order pages.
112 if (!PageCompound(page
)) {
113 struct vm_area_struct
*vma
;
115 vma
= find_vma(current
->mm
, (unsigned long)objp
);
117 return vma
->vm_end
- vma
->vm_start
;
121 * The ksize() function is only guaranteed to work for pointers
122 * returned by kmalloc(). So handle arbitrary pointers here.
124 return PAGE_SIZE
<< compound_order(page
);
127 int __get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
128 unsigned long start
, int nr_pages
, unsigned int foll_flags
,
129 struct page
**pages
, struct vm_area_struct
**vmas
)
131 struct vm_area_struct
*vma
;
132 unsigned long vm_flags
;
135 /* calculate required read or write permissions.
136 * If FOLL_FORCE is set, we only require the "MAY" flags.
138 vm_flags
= (foll_flags
& FOLL_WRITE
) ?
139 (VM_WRITE
| VM_MAYWRITE
) : (VM_READ
| VM_MAYREAD
);
140 vm_flags
&= (foll_flags
& FOLL_FORCE
) ?
141 (VM_MAYREAD
| VM_MAYWRITE
) : (VM_READ
| VM_WRITE
);
143 for (i
= 0; i
< nr_pages
; i
++) {
144 vma
= find_vma(mm
, start
);
146 goto finish_or_fault
;
148 /* protect what we can, including chardevs */
149 if ((vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)) ||
150 !(vm_flags
& vma
->vm_flags
))
151 goto finish_or_fault
;
154 pages
[i
] = virt_to_page(start
);
156 page_cache_get(pages
[i
]);
160 start
= (start
+ PAGE_SIZE
) & PAGE_MASK
;
166 return i
? : -EFAULT
;
170 * get a list of pages in an address range belonging to the specified process
171 * and indicate the VMA that covers each page
172 * - this is potentially dodgy as we may end incrementing the page count of a
173 * slab page or a secondary page from a compound page
174 * - don't permit access to VMAs that don't support it, such as I/O mappings
176 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
177 unsigned long start
, int nr_pages
, int write
, int force
,
178 struct page
**pages
, struct vm_area_struct
**vmas
)
187 return __get_user_pages(tsk
, mm
, start
, nr_pages
, flags
, pages
, vmas
);
189 EXPORT_SYMBOL(get_user_pages
);
192 * follow_pfn - look up PFN at a user virtual address
193 * @vma: memory mapping
194 * @address: user virtual address
195 * @pfn: location to store found PFN
197 * Only IO mappings and raw PFN mappings are allowed.
199 * Returns zero and the pfn at @pfn on success, -ve otherwise.
201 int follow_pfn(struct vm_area_struct
*vma
, unsigned long address
,
204 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
207 *pfn
= address
>> PAGE_SHIFT
;
210 EXPORT_SYMBOL(follow_pfn
);
212 DEFINE_RWLOCK(vmlist_lock
);
213 struct vm_struct
*vmlist
;
215 void vfree(const void *addr
)
219 EXPORT_SYMBOL(vfree
);
221 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
224 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
225 * returns only a logical address.
227 return kmalloc(size
, (gfp_mask
| __GFP_COMP
) & ~__GFP_HIGHMEM
);
229 EXPORT_SYMBOL(__vmalloc
);
231 void *vmalloc_user(unsigned long size
)
235 ret
= __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
238 struct vm_area_struct
*vma
;
240 down_write(¤t
->mm
->mmap_sem
);
241 vma
= find_vma(current
->mm
, (unsigned long)ret
);
243 vma
->vm_flags
|= VM_USERMAP
;
244 up_write(¤t
->mm
->mmap_sem
);
249 EXPORT_SYMBOL(vmalloc_user
);
251 struct page
*vmalloc_to_page(const void *addr
)
253 return virt_to_page(addr
);
255 EXPORT_SYMBOL(vmalloc_to_page
);
257 unsigned long vmalloc_to_pfn(const void *addr
)
259 return page_to_pfn(virt_to_page(addr
));
261 EXPORT_SYMBOL(vmalloc_to_pfn
);
263 long vread(char *buf
, char *addr
, unsigned long count
)
265 memcpy(buf
, addr
, count
);
269 long vwrite(char *buf
, char *addr
, unsigned long count
)
271 /* Don't allow overflow */
272 if ((unsigned long) addr
+ count
< count
)
273 count
= -(unsigned long) addr
;
275 memcpy(addr
, buf
, count
);
280 * vmalloc - allocate virtually continguos memory
282 * @size: allocation size
284 * Allocate enough pages to cover @size from the page level
285 * allocator and map them into continguos kernel virtual space.
287 * For tight control over page level allocator and protection flags
288 * use __vmalloc() instead.
290 void *vmalloc(unsigned long size
)
292 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
294 EXPORT_SYMBOL(vmalloc
);
296 void *vmalloc_node(unsigned long size
, int node
)
298 return vmalloc(size
);
300 EXPORT_SYMBOL(vmalloc_node
);
302 #ifndef PAGE_KERNEL_EXEC
303 # define PAGE_KERNEL_EXEC PAGE_KERNEL
307 * vmalloc_exec - allocate virtually contiguous, executable memory
308 * @size: allocation size
310 * Kernel-internal function to allocate enough pages to cover @size
311 * the page level allocator and map them into contiguous and
312 * executable kernel virtual space.
314 * For tight control over page level allocator and protection flags
315 * use __vmalloc() instead.
318 void *vmalloc_exec(unsigned long size
)
320 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
);
324 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
325 * @size: allocation size
327 * Allocate enough 32bit PA addressable pages to cover @size from the
328 * page level allocator and map them into continguos kernel virtual space.
330 void *vmalloc_32(unsigned long size
)
332 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
334 EXPORT_SYMBOL(vmalloc_32
);
337 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
338 * @size: allocation size
340 * The resulting memory area is 32bit addressable and zeroed so it can be
341 * mapped to userspace without leaking data.
343 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
344 * remap_vmalloc_range() are permissible.
346 void *vmalloc_32_user(unsigned long size
)
349 * We'll have to sort out the ZONE_DMA bits for 64-bit,
350 * but for now this can simply use vmalloc_user() directly.
352 return vmalloc_user(size
);
354 EXPORT_SYMBOL(vmalloc_32_user
);
356 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
363 void vunmap(const void *addr
)
367 EXPORT_SYMBOL(vunmap
);
369 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
, pgprot_t prot
)
374 EXPORT_SYMBOL(vm_map_ram
);
376 void vm_unmap_ram(const void *mem
, unsigned int count
)
380 EXPORT_SYMBOL(vm_unmap_ram
);
382 void vm_unmap_aliases(void)
385 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
388 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
391 void __attribute__((weak
)) vmalloc_sync_all(void)
395 int vm_insert_page(struct vm_area_struct
*vma
, unsigned long addr
,
400 EXPORT_SYMBOL(vm_insert_page
);
403 * sys_brk() for the most part doesn't need the global kernel
404 * lock, except when an application is doing something nasty
405 * like trying to un-brk an area that has already been mapped
406 * to a regular file. in this case, the unmapping will need
407 * to invoke file system routines that need the global lock.
409 SYSCALL_DEFINE1(brk
, unsigned long, brk
)
411 struct mm_struct
*mm
= current
->mm
;
413 if (brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
420 * Always allow shrinking brk
422 if (brk
<= mm
->brk
) {
428 * Ok, looks good - let it rip.
430 flush_icache_range(mm
->brk
, brk
);
431 return mm
->brk
= brk
;
435 * initialise the VMA and region record slabs
437 void __init
mmap_init(void)
441 ret
= percpu_counter_init(&vm_committed_as
, 0);
443 vm_region_jar
= KMEM_CACHE(vm_region
, SLAB_PANIC
);
447 * validate the region tree
448 * - the caller must hold the region lock
450 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
451 static noinline
void validate_nommu_regions(void)
453 struct vm_region
*region
, *last
;
454 struct rb_node
*p
, *lastp
;
456 lastp
= rb_first(&nommu_region_tree
);
460 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
461 BUG_ON(unlikely(last
->vm_end
<= last
->vm_start
));
462 BUG_ON(unlikely(last
->vm_top
< last
->vm_end
));
464 while ((p
= rb_next(lastp
))) {
465 region
= rb_entry(p
, struct vm_region
, vm_rb
);
466 last
= rb_entry(lastp
, struct vm_region
, vm_rb
);
468 BUG_ON(unlikely(region
->vm_end
<= region
->vm_start
));
469 BUG_ON(unlikely(region
->vm_top
< region
->vm_end
));
470 BUG_ON(unlikely(region
->vm_start
< last
->vm_top
));
476 static void validate_nommu_regions(void)
482 * add a region into the global tree
484 static void add_nommu_region(struct vm_region
*region
)
486 struct vm_region
*pregion
;
487 struct rb_node
**p
, *parent
;
489 validate_nommu_regions();
492 p
= &nommu_region_tree
.rb_node
;
495 pregion
= rb_entry(parent
, struct vm_region
, vm_rb
);
496 if (region
->vm_start
< pregion
->vm_start
)
498 else if (region
->vm_start
> pregion
->vm_start
)
500 else if (pregion
== region
)
506 rb_link_node(®ion
->vm_rb
, parent
, p
);
507 rb_insert_color(®ion
->vm_rb
, &nommu_region_tree
);
509 validate_nommu_regions();
513 * delete a region from the global tree
515 static void delete_nommu_region(struct vm_region
*region
)
517 BUG_ON(!nommu_region_tree
.rb_node
);
519 validate_nommu_regions();
520 rb_erase(®ion
->vm_rb
, &nommu_region_tree
);
521 validate_nommu_regions();
525 * free a contiguous series of pages
527 static void free_page_series(unsigned long from
, unsigned long to
)
529 for (; from
< to
; from
+= PAGE_SIZE
) {
530 struct page
*page
= virt_to_page(from
);
532 kdebug("- free %lx", from
);
533 atomic_long_dec(&mmap_pages_allocated
);
534 if (page_count(page
) != 1)
535 kdebug("free page %p: refcount not one: %d",
536 page
, page_count(page
));
542 * release a reference to a region
543 * - the caller must hold the region semaphore for writing, which this releases
544 * - the region may not have been added to the tree yet, in which case vm_top
545 * will equal vm_start
547 static void __put_nommu_region(struct vm_region
*region
)
548 __releases(nommu_region_sem
)
550 kenter("%p{%d}", region
, region
->vm_usage
);
552 BUG_ON(!nommu_region_tree
.rb_node
);
554 if (--region
->vm_usage
== 0) {
555 if (region
->vm_top
> region
->vm_start
)
556 delete_nommu_region(region
);
557 up_write(&nommu_region_sem
);
560 fput(region
->vm_file
);
562 /* IO memory and memory shared directly out of the pagecache
563 * from ramfs/tmpfs mustn't be released here */
564 if (region
->vm_flags
& VM_MAPPED_COPY
) {
565 kdebug("free series");
566 free_page_series(region
->vm_start
, region
->vm_top
);
568 kmem_cache_free(vm_region_jar
, region
);
570 up_write(&nommu_region_sem
);
575 * release a reference to a region
577 static void put_nommu_region(struct vm_region
*region
)
579 down_write(&nommu_region_sem
);
580 __put_nommu_region(region
);
584 * update protection on a vma
586 static void protect_vma(struct vm_area_struct
*vma
, unsigned long flags
)
589 struct mm_struct
*mm
= vma
->vm_mm
;
590 long start
= vma
->vm_start
& PAGE_MASK
;
591 while (start
< vma
->vm_end
) {
592 protect_page(mm
, start
, flags
);
595 update_protections(mm
);
600 * add a VMA into a process's mm_struct in the appropriate place in the list
601 * and tree and add to the address space's page tree also if not an anonymous
603 * - should be called with mm->mmap_sem held writelocked
605 static void add_vma_to_mm(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
607 struct vm_area_struct
*pvma
, **pp
;
608 struct address_space
*mapping
;
609 struct rb_node
**p
, *parent
;
613 BUG_ON(!vma
->vm_region
);
618 protect_vma(vma
, vma
->vm_flags
);
620 /* add the VMA to the mapping */
622 mapping
= vma
->vm_file
->f_mapping
;
624 flush_dcache_mmap_lock(mapping
);
625 vma_prio_tree_insert(vma
, &mapping
->i_mmap
);
626 flush_dcache_mmap_unlock(mapping
);
629 /* add the VMA to the tree */
631 p
= &mm
->mm_rb
.rb_node
;
634 pvma
= rb_entry(parent
, struct vm_area_struct
, vm_rb
);
636 /* sort by: start addr, end addr, VMA struct addr in that order
637 * (the latter is necessary as we may get identical VMAs) */
638 if (vma
->vm_start
< pvma
->vm_start
)
640 else if (vma
->vm_start
> pvma
->vm_start
)
642 else if (vma
->vm_end
< pvma
->vm_end
)
644 else if (vma
->vm_end
> pvma
->vm_end
)
654 rb_link_node(&vma
->vm_rb
, parent
, p
);
655 rb_insert_color(&vma
->vm_rb
, &mm
->mm_rb
);
657 /* add VMA to the VMA list also */
658 for (pp
= &mm
->mmap
; (pvma
= *pp
); pp
= &(*pp
)->vm_next
) {
659 if (pvma
->vm_start
> vma
->vm_start
)
661 if (pvma
->vm_start
< vma
->vm_start
)
663 if (pvma
->vm_end
< vma
->vm_end
)
672 * delete a VMA from its owning mm_struct and address space
674 static void delete_vma_from_mm(struct vm_area_struct
*vma
)
676 struct vm_area_struct
**pp
;
677 struct address_space
*mapping
;
678 struct mm_struct
*mm
= vma
->vm_mm
;
685 if (mm
->mmap_cache
== vma
)
686 mm
->mmap_cache
= NULL
;
688 /* remove the VMA from the mapping */
690 mapping
= vma
->vm_file
->f_mapping
;
692 flush_dcache_mmap_lock(mapping
);
693 vma_prio_tree_remove(vma
, &mapping
->i_mmap
);
694 flush_dcache_mmap_unlock(mapping
);
697 /* remove from the MM's tree and list */
698 rb_erase(&vma
->vm_rb
, &mm
->mm_rb
);
699 for (pp
= &mm
->mmap
; *pp
; pp
= &(*pp
)->vm_next
) {
710 * destroy a VMA record
712 static void delete_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
)
715 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
716 vma
->vm_ops
->close(vma
);
719 if (vma
->vm_flags
& VM_EXECUTABLE
)
720 removed_exe_file_vma(mm
);
722 put_nommu_region(vma
->vm_region
);
723 kmem_cache_free(vm_area_cachep
, vma
);
727 * look up the first VMA in which addr resides, NULL if none
728 * - should be called with mm->mmap_sem at least held readlocked
730 struct vm_area_struct
*find_vma(struct mm_struct
*mm
, unsigned long addr
)
732 struct vm_area_struct
*vma
;
733 struct rb_node
*n
= mm
->mm_rb
.rb_node
;
735 /* check the cache first */
736 vma
= mm
->mmap_cache
;
737 if (vma
&& vma
->vm_start
<= addr
&& vma
->vm_end
> addr
)
740 /* trawl the tree (there may be multiple mappings in which addr
742 for (n
= rb_first(&mm
->mm_rb
); n
; n
= rb_next(n
)) {
743 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
744 if (vma
->vm_start
> addr
)
746 if (vma
->vm_end
> addr
) {
747 mm
->mmap_cache
= vma
;
754 EXPORT_SYMBOL(find_vma
);
758 * - we don't extend stack VMAs under NOMMU conditions
760 struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
)
762 return find_vma(mm
, addr
);
766 * expand a stack to a given address
767 * - not supported under NOMMU conditions
769 int expand_stack(struct vm_area_struct
*vma
, unsigned long address
)
775 * look up the first VMA exactly that exactly matches addr
776 * - should be called with mm->mmap_sem at least held readlocked
778 static struct vm_area_struct
*find_vma_exact(struct mm_struct
*mm
,
782 struct vm_area_struct
*vma
;
783 struct rb_node
*n
= mm
->mm_rb
.rb_node
;
784 unsigned long end
= addr
+ len
;
786 /* check the cache first */
787 vma
= mm
->mmap_cache
;
788 if (vma
&& vma
->vm_start
== addr
&& vma
->vm_end
== end
)
791 /* trawl the tree (there may be multiple mappings in which addr
793 for (n
= rb_first(&mm
->mm_rb
); n
; n
= rb_next(n
)) {
794 vma
= rb_entry(n
, struct vm_area_struct
, vm_rb
);
795 if (vma
->vm_start
< addr
)
797 if (vma
->vm_start
> addr
)
799 if (vma
->vm_end
== end
) {
800 mm
->mmap_cache
= vma
;
809 * determine whether a mapping should be permitted and, if so, what sort of
810 * mapping we're capable of supporting
812 static int validate_mmap_request(struct file
*file
,
818 unsigned long *_capabilities
)
820 unsigned long capabilities
, rlen
;
821 unsigned long reqprot
= prot
;
824 /* do the simple checks first */
825 if (flags
& MAP_FIXED
) {
827 "%d: Can't do fixed-address/overlay mmap of RAM\n",
832 if ((flags
& MAP_TYPE
) != MAP_PRIVATE
&&
833 (flags
& MAP_TYPE
) != MAP_SHARED
)
839 /* Careful about overflows.. */
840 rlen
= PAGE_ALIGN(len
);
841 if (!rlen
|| rlen
> TASK_SIZE
)
844 /* offset overflow? */
845 if ((pgoff
+ (rlen
>> PAGE_SHIFT
)) < pgoff
)
849 /* validate file mapping requests */
850 struct address_space
*mapping
;
852 /* files must support mmap */
853 if (!file
->f_op
|| !file
->f_op
->mmap
)
856 /* work out if what we've got could possibly be shared
857 * - we support chardevs that provide their own "memory"
858 * - we support files/blockdevs that are memory backed
860 mapping
= file
->f_mapping
;
862 mapping
= file
->f_path
.dentry
->d_inode
->i_mapping
;
865 if (mapping
&& mapping
->backing_dev_info
)
866 capabilities
= mapping
->backing_dev_info
->capabilities
;
869 /* no explicit capabilities set, so assume some
871 switch (file
->f_path
.dentry
->d_inode
->i_mode
& S_IFMT
) {
874 capabilities
= BDI_CAP_MAP_COPY
;
889 /* eliminate any capabilities that we can't support on this
891 if (!file
->f_op
->get_unmapped_area
)
892 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
893 if (!file
->f_op
->read
)
894 capabilities
&= ~BDI_CAP_MAP_COPY
;
896 /* The file shall have been opened with read permission. */
897 if (!(file
->f_mode
& FMODE_READ
))
900 if (flags
& MAP_SHARED
) {
901 /* do checks for writing, appending and locking */
902 if ((prot
& PROT_WRITE
) &&
903 !(file
->f_mode
& FMODE_WRITE
))
906 if (IS_APPEND(file
->f_path
.dentry
->d_inode
) &&
907 (file
->f_mode
& FMODE_WRITE
))
910 if (locks_verify_locked(file
->f_path
.dentry
->d_inode
))
913 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
916 /* we mustn't privatise shared mappings */
917 capabilities
&= ~BDI_CAP_MAP_COPY
;
920 /* we're going to read the file into private memory we
922 if (!(capabilities
& BDI_CAP_MAP_COPY
))
925 /* we don't permit a private writable mapping to be
926 * shared with the backing device */
927 if (prot
& PROT_WRITE
)
928 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
931 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
932 if (((prot
& PROT_READ
) && !(capabilities
& BDI_CAP_READ_MAP
)) ||
933 ((prot
& PROT_WRITE
) && !(capabilities
& BDI_CAP_WRITE_MAP
)) ||
934 ((prot
& PROT_EXEC
) && !(capabilities
& BDI_CAP_EXEC_MAP
))
936 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
937 if (flags
& MAP_SHARED
) {
939 "MAP_SHARED not completely supported on !MMU\n");
945 /* handle executable mappings and implied executable
947 if (file
->f_path
.mnt
->mnt_flags
& MNT_NOEXEC
) {
948 if (prot
& PROT_EXEC
)
951 else if ((prot
& PROT_READ
) && !(prot
& PROT_EXEC
)) {
952 /* handle implication of PROT_EXEC by PROT_READ */
953 if (current
->personality
& READ_IMPLIES_EXEC
) {
954 if (capabilities
& BDI_CAP_EXEC_MAP
)
958 else if ((prot
& PROT_READ
) &&
959 (prot
& PROT_EXEC
) &&
960 !(capabilities
& BDI_CAP_EXEC_MAP
)
962 /* backing file is not executable, try to copy */
963 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
967 /* anonymous mappings are always memory backed and can be
970 capabilities
= BDI_CAP_MAP_COPY
;
972 /* handle PROT_EXEC implication by PROT_READ */
973 if ((prot
& PROT_READ
) &&
974 (current
->personality
& READ_IMPLIES_EXEC
))
978 /* allow the security API to have its say */
979 ret
= security_file_mmap(file
, reqprot
, prot
, flags
, addr
, 0);
984 *_capabilities
= capabilities
;
989 * we've determined that we can make the mapping, now translate what we
990 * now know into VMA flags
992 static unsigned long determine_vm_flags(struct file
*file
,
995 unsigned long capabilities
)
997 unsigned long vm_flags
;
999 vm_flags
= calc_vm_prot_bits(prot
) | calc_vm_flag_bits(flags
);
1000 /* vm_flags |= mm->def_flags; */
1002 if (!(capabilities
& BDI_CAP_MAP_DIRECT
)) {
1003 /* attempt to share read-only copies of mapped file chunks */
1004 vm_flags
|= VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
1005 if (file
&& !(prot
& PROT_WRITE
))
1006 vm_flags
|= VM_MAYSHARE
;
1008 /* overlay a shareable mapping on the backing device or inode
1009 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1011 vm_flags
|= VM_MAYSHARE
| (capabilities
& BDI_CAP_VMFLAGS
);
1012 if (flags
& MAP_SHARED
)
1013 vm_flags
|= VM_SHARED
;
1016 /* refuse to let anyone share private mappings with this process if
1017 * it's being traced - otherwise breakpoints set in it may interfere
1018 * with another untraced process
1020 if ((flags
& MAP_PRIVATE
) && tracehook_expect_breakpoints(current
))
1021 vm_flags
&= ~VM_MAYSHARE
;
1027 * set up a shared mapping on a file (the driver or filesystem provides and
1030 static int do_mmap_shared_file(struct vm_area_struct
*vma
)
1034 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1036 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1042 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1043 * opposed to tried but failed) so we can only give a suitable error as
1044 * it's not possible to make a private copy if MAP_SHARED was given */
1049 * set up a private mapping or an anonymous shared mapping
1051 static int do_mmap_private(struct vm_area_struct
*vma
,
1052 struct vm_region
*region
,
1054 unsigned long capabilities
)
1057 unsigned long total
, point
, n
, rlen
;
1061 /* invoke the file's mapping function so that it can keep track of
1062 * shared mappings on devices or memory
1063 * - VM_MAYSHARE will be set if it may attempt to share
1065 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1066 ret
= vma
->vm_file
->f_op
->mmap(vma
->vm_file
, vma
);
1068 /* shouldn't return success if we're not sharing */
1069 BUG_ON(!(vma
->vm_flags
& VM_MAYSHARE
));
1070 vma
->vm_region
->vm_top
= vma
->vm_region
->vm_end
;
1076 /* getting an ENOSYS error indicates that direct mmap isn't
1077 * possible (as opposed to tried but failed) so we'll try to
1078 * make a private copy of the data and map that instead */
1081 rlen
= PAGE_ALIGN(len
);
1083 /* allocate some memory to hold the mapping
1084 * - note that this may not return a page-aligned address if the object
1085 * we're allocating is smaller than a page
1087 order
= get_order(rlen
);
1088 kdebug("alloc order %d for %lx", order
, len
);
1090 pages
= alloc_pages(GFP_KERNEL
, order
);
1095 atomic_long_add(total
, &mmap_pages_allocated
);
1097 point
= rlen
>> PAGE_SHIFT
;
1099 /* we allocated a power-of-2 sized page set, so we may want to trim off
1101 if (sysctl_nr_trim_pages
&& total
- point
>= sysctl_nr_trim_pages
) {
1102 while (total
> point
) {
1103 order
= ilog2(total
- point
);
1105 kdebug("shave %lu/%lu @%lu", n
, total
- point
, total
);
1106 atomic_long_sub(n
, &mmap_pages_allocated
);
1108 set_page_refcounted(pages
+ total
);
1109 __free_pages(pages
+ total
, order
);
1113 for (point
= 1; point
< total
; point
++)
1114 set_page_refcounted(&pages
[point
]);
1116 base
= page_address(pages
);
1117 region
->vm_flags
= vma
->vm_flags
|= VM_MAPPED_COPY
;
1118 region
->vm_start
= (unsigned long) base
;
1119 region
->vm_end
= region
->vm_start
+ rlen
;
1120 region
->vm_top
= region
->vm_start
+ (total
<< PAGE_SHIFT
);
1122 vma
->vm_start
= region
->vm_start
;
1123 vma
->vm_end
= region
->vm_start
+ len
;
1126 /* read the contents of a file into the copy */
1127 mm_segment_t old_fs
;
1130 fpos
= vma
->vm_pgoff
;
1131 fpos
<<= PAGE_SHIFT
;
1135 ret
= vma
->vm_file
->f_op
->read(vma
->vm_file
, base
, rlen
, &fpos
);
1141 /* clear the last little bit */
1143 memset(base
+ ret
, 0, rlen
- ret
);
1150 free_page_series(region
->vm_start
, region
->vm_end
);
1151 region
->vm_start
= vma
->vm_start
= 0;
1152 region
->vm_end
= vma
->vm_end
= 0;
1157 printk("Allocation of length %lu from process %d (%s) failed\n",
1158 len
, current
->pid
, current
->comm
);
1164 * handle mapping creation for uClinux
1166 unsigned long do_mmap_pgoff(struct file
*file
,
1170 unsigned long flags
,
1171 unsigned long pgoff
)
1173 struct vm_area_struct
*vma
;
1174 struct vm_region
*region
;
1176 unsigned long capabilities
, vm_flags
, result
;
1179 kenter(",%lx,%lx,%lx,%lx,%lx", addr
, len
, prot
, flags
, pgoff
);
1181 /* decide whether we should attempt the mapping, and if so what sort of
1183 ret
= validate_mmap_request(file
, addr
, len
, prot
, flags
, pgoff
,
1186 kleave(" = %d [val]", ret
);
1190 /* we ignore the address hint */
1193 /* we've determined that we can make the mapping, now translate what we
1194 * now know into VMA flags */
1195 vm_flags
= determine_vm_flags(file
, prot
, flags
, capabilities
);
1197 /* we're going to need to record the mapping */
1198 region
= kmem_cache_zalloc(vm_region_jar
, GFP_KERNEL
);
1200 goto error_getting_region
;
1202 vma
= kmem_cache_zalloc(vm_area_cachep
, GFP_KERNEL
);
1204 goto error_getting_vma
;
1206 region
->vm_usage
= 1;
1207 region
->vm_flags
= vm_flags
;
1208 region
->vm_pgoff
= pgoff
;
1210 INIT_LIST_HEAD(&vma
->anon_vma_chain
);
1211 vma
->vm_flags
= vm_flags
;
1212 vma
->vm_pgoff
= pgoff
;
1215 region
->vm_file
= file
;
1217 vma
->vm_file
= file
;
1219 if (vm_flags
& VM_EXECUTABLE
) {
1220 added_exe_file_vma(current
->mm
);
1221 vma
->vm_mm
= current
->mm
;
1225 down_write(&nommu_region_sem
);
1227 /* if we want to share, we need to check for regions created by other
1228 * mmap() calls that overlap with our proposed mapping
1229 * - we can only share with a superset match on most regular files
1230 * - shared mappings on character devices and memory backed files are
1231 * permitted to overlap inexactly as far as we are concerned for in
1232 * these cases, sharing is handled in the driver or filesystem rather
1235 if (vm_flags
& VM_MAYSHARE
) {
1236 struct vm_region
*pregion
;
1237 unsigned long pglen
, rpglen
, pgend
, rpgend
, start
;
1239 pglen
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1240 pgend
= pgoff
+ pglen
;
1242 for (rb
= rb_first(&nommu_region_tree
); rb
; rb
= rb_next(rb
)) {
1243 pregion
= rb_entry(rb
, struct vm_region
, vm_rb
);
1245 if (!(pregion
->vm_flags
& VM_MAYSHARE
))
1248 /* search for overlapping mappings on the same file */
1249 if (pregion
->vm_file
->f_path
.dentry
->d_inode
!=
1250 file
->f_path
.dentry
->d_inode
)
1253 if (pregion
->vm_pgoff
>= pgend
)
1256 rpglen
= pregion
->vm_end
- pregion
->vm_start
;
1257 rpglen
= (rpglen
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1258 rpgend
= pregion
->vm_pgoff
+ rpglen
;
1259 if (pgoff
>= rpgend
)
1262 /* handle inexactly overlapping matches between
1264 if ((pregion
->vm_pgoff
!= pgoff
|| rpglen
!= pglen
) &&
1265 !(pgoff
>= pregion
->vm_pgoff
&& pgend
<= rpgend
)) {
1266 /* new mapping is not a subset of the region */
1267 if (!(capabilities
& BDI_CAP_MAP_DIRECT
))
1268 goto sharing_violation
;
1272 /* we've found a region we can share */
1273 pregion
->vm_usage
++;
1274 vma
->vm_region
= pregion
;
1275 start
= pregion
->vm_start
;
1276 start
+= (pgoff
- pregion
->vm_pgoff
) << PAGE_SHIFT
;
1277 vma
->vm_start
= start
;
1278 vma
->vm_end
= start
+ len
;
1280 if (pregion
->vm_flags
& VM_MAPPED_COPY
) {
1281 kdebug("share copy");
1282 vma
->vm_flags
|= VM_MAPPED_COPY
;
1284 kdebug("share mmap");
1285 ret
= do_mmap_shared_file(vma
);
1287 vma
->vm_region
= NULL
;
1290 pregion
->vm_usage
--;
1292 goto error_just_free
;
1295 fput(region
->vm_file
);
1296 kmem_cache_free(vm_region_jar
, region
);
1302 /* obtain the address at which to make a shared mapping
1303 * - this is the hook for quasi-memory character devices to
1304 * tell us the location of a shared mapping
1306 if (capabilities
& BDI_CAP_MAP_DIRECT
) {
1307 addr
= file
->f_op
->get_unmapped_area(file
, addr
, len
,
1309 if (IS_ERR((void *) addr
)) {
1311 if (ret
!= (unsigned long) -ENOSYS
)
1312 goto error_just_free
;
1314 /* the driver refused to tell us where to site
1315 * the mapping so we'll have to attempt to copy
1317 ret
= (unsigned long) -ENODEV
;
1318 if (!(capabilities
& BDI_CAP_MAP_COPY
))
1319 goto error_just_free
;
1321 capabilities
&= ~BDI_CAP_MAP_DIRECT
;
1323 vma
->vm_start
= region
->vm_start
= addr
;
1324 vma
->vm_end
= region
->vm_end
= addr
+ len
;
1329 vma
->vm_region
= region
;
1331 /* set up the mapping
1332 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1334 if (file
&& vma
->vm_flags
& VM_SHARED
)
1335 ret
= do_mmap_shared_file(vma
);
1337 ret
= do_mmap_private(vma
, region
, len
, capabilities
);
1339 goto error_just_free
;
1340 add_nommu_region(region
);
1342 /* clear anonymous mappings that don't ask for uninitialized data */
1343 if (!vma
->vm_file
&& !(flags
& MAP_UNINITIALIZED
))
1344 memset((void *)region
->vm_start
, 0,
1345 region
->vm_end
- region
->vm_start
);
1347 /* okay... we have a mapping; now we have to register it */
1348 result
= vma
->vm_start
;
1350 current
->mm
->total_vm
+= len
>> PAGE_SHIFT
;
1353 add_vma_to_mm(current
->mm
, vma
);
1355 /* we flush the region from the icache only when the first executable
1356 * mapping of it is made */
1357 if (vma
->vm_flags
& VM_EXEC
&& !region
->vm_icache_flushed
) {
1358 flush_icache_range(region
->vm_start
, region
->vm_end
);
1359 region
->vm_icache_flushed
= true;
1362 up_write(&nommu_region_sem
);
1364 kleave(" = %lx", result
);
1368 up_write(&nommu_region_sem
);
1370 if (region
->vm_file
)
1371 fput(region
->vm_file
);
1372 kmem_cache_free(vm_region_jar
, region
);
1375 if (vma
->vm_flags
& VM_EXECUTABLE
)
1376 removed_exe_file_vma(vma
->vm_mm
);
1377 kmem_cache_free(vm_area_cachep
, vma
);
1378 kleave(" = %d", ret
);
1382 up_write(&nommu_region_sem
);
1383 printk(KERN_WARNING
"Attempt to share mismatched mappings\n");
1388 kmem_cache_free(vm_region_jar
, region
);
1389 printk(KERN_WARNING
"Allocation of vma for %lu byte allocation"
1390 " from process %d failed\n",
1395 error_getting_region
:
1396 printk(KERN_WARNING
"Allocation of vm region for %lu byte allocation"
1397 " from process %d failed\n",
1402 EXPORT_SYMBOL(do_mmap_pgoff
);
1404 SYSCALL_DEFINE6(mmap_pgoff
, unsigned long, addr
, unsigned long, len
,
1405 unsigned long, prot
, unsigned long, flags
,
1406 unsigned long, fd
, unsigned long, pgoff
)
1408 struct file
*file
= NULL
;
1409 unsigned long retval
= -EBADF
;
1411 if (!(flags
& MAP_ANONYMOUS
)) {
1417 flags
&= ~(MAP_EXECUTABLE
| MAP_DENYWRITE
);
1419 down_write(¤t
->mm
->mmap_sem
);
1420 retval
= do_mmap_pgoff(file
, addr
, len
, prot
, flags
, pgoff
);
1421 up_write(¤t
->mm
->mmap_sem
);
1429 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1430 struct mmap_arg_struct
{
1434 unsigned long flags
;
1436 unsigned long offset
;
1439 SYSCALL_DEFINE1(old_mmap
, struct mmap_arg_struct __user
*, arg
)
1441 struct mmap_arg_struct a
;
1443 if (copy_from_user(&a
, arg
, sizeof(a
)))
1445 if (a
.offset
& ~PAGE_MASK
)
1448 return sys_mmap_pgoff(a
.addr
, a
.len
, a
.prot
, a
.flags
, a
.fd
,
1449 a
.offset
>> PAGE_SHIFT
);
1451 #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1454 * split a vma into two pieces at address 'addr', a new vma is allocated either
1455 * for the first part or the tail.
1457 int split_vma(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1458 unsigned long addr
, int new_below
)
1460 struct vm_area_struct
*new;
1461 struct vm_region
*region
;
1462 unsigned long npages
;
1466 /* we're only permitted to split anonymous regions (these should have
1467 * only a single usage on the region) */
1471 if (mm
->map_count
>= sysctl_max_map_count
)
1474 region
= kmem_cache_alloc(vm_region_jar
, GFP_KERNEL
);
1478 new = kmem_cache_alloc(vm_area_cachep
, GFP_KERNEL
);
1480 kmem_cache_free(vm_region_jar
, region
);
1484 /* most fields are the same, copy all, and then fixup */
1486 *region
= *vma
->vm_region
;
1487 new->vm_region
= region
;
1489 npages
= (addr
- vma
->vm_start
) >> PAGE_SHIFT
;
1492 region
->vm_top
= region
->vm_end
= new->vm_end
= addr
;
1494 region
->vm_start
= new->vm_start
= addr
;
1495 region
->vm_pgoff
= new->vm_pgoff
+= npages
;
1498 if (new->vm_ops
&& new->vm_ops
->open
)
1499 new->vm_ops
->open(new);
1501 delete_vma_from_mm(vma
);
1502 down_write(&nommu_region_sem
);
1503 delete_nommu_region(vma
->vm_region
);
1505 vma
->vm_region
->vm_start
= vma
->vm_start
= addr
;
1506 vma
->vm_region
->vm_pgoff
= vma
->vm_pgoff
+= npages
;
1508 vma
->vm_region
->vm_end
= vma
->vm_end
= addr
;
1509 vma
->vm_region
->vm_top
= addr
;
1511 add_nommu_region(vma
->vm_region
);
1512 add_nommu_region(new->vm_region
);
1513 up_write(&nommu_region_sem
);
1514 add_vma_to_mm(mm
, vma
);
1515 add_vma_to_mm(mm
, new);
1520 * shrink a VMA by removing the specified chunk from either the beginning or
1523 static int shrink_vma(struct mm_struct
*mm
,
1524 struct vm_area_struct
*vma
,
1525 unsigned long from
, unsigned long to
)
1527 struct vm_region
*region
;
1531 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1533 delete_vma_from_mm(vma
);
1534 if (from
> vma
->vm_start
)
1538 add_vma_to_mm(mm
, vma
);
1540 /* cut the backing region down to size */
1541 region
= vma
->vm_region
;
1542 BUG_ON(region
->vm_usage
!= 1);
1544 down_write(&nommu_region_sem
);
1545 delete_nommu_region(region
);
1546 if (from
> region
->vm_start
) {
1547 to
= region
->vm_top
;
1548 region
->vm_top
= region
->vm_end
= from
;
1550 region
->vm_start
= to
;
1552 add_nommu_region(region
);
1553 up_write(&nommu_region_sem
);
1555 free_page_series(from
, to
);
1561 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1562 * VMA, though it need not cover the whole VMA
1564 int do_munmap(struct mm_struct
*mm
, unsigned long start
, size_t len
)
1566 struct vm_area_struct
*vma
;
1568 unsigned long end
= start
+ len
;
1571 kenter(",%lx,%zx", start
, len
);
1576 /* find the first potentially overlapping VMA */
1577 vma
= find_vma(mm
, start
);
1579 static int limit
= 0;
1582 "munmap of memory not mmapped by process %d"
1583 " (%s): 0x%lx-0x%lx\n",
1584 current
->pid
, current
->comm
,
1585 start
, start
+ len
- 1);
1591 /* we're allowed to split an anonymous VMA but not a file-backed one */
1594 if (start
> vma
->vm_start
) {
1595 kleave(" = -EINVAL [miss]");
1598 if (end
== vma
->vm_end
)
1599 goto erase_whole_vma
;
1600 rb
= rb_next(&vma
->vm_rb
);
1601 vma
= rb_entry(rb
, struct vm_area_struct
, vm_rb
);
1603 kleave(" = -EINVAL [split file]");
1606 /* the chunk must be a subset of the VMA found */
1607 if (start
== vma
->vm_start
&& end
== vma
->vm_end
)
1608 goto erase_whole_vma
;
1609 if (start
< vma
->vm_start
|| end
> vma
->vm_end
) {
1610 kleave(" = -EINVAL [superset]");
1613 if (start
& ~PAGE_MASK
) {
1614 kleave(" = -EINVAL [unaligned start]");
1617 if (end
!= vma
->vm_end
&& end
& ~PAGE_MASK
) {
1618 kleave(" = -EINVAL [unaligned split]");
1621 if (start
!= vma
->vm_start
&& end
!= vma
->vm_end
) {
1622 ret
= split_vma(mm
, vma
, start
, 1);
1624 kleave(" = %d [split]", ret
);
1628 return shrink_vma(mm
, vma
, start
, end
);
1632 delete_vma_from_mm(vma
);
1633 delete_vma(mm
, vma
);
1637 EXPORT_SYMBOL(do_munmap
);
1639 SYSCALL_DEFINE2(munmap
, unsigned long, addr
, size_t, len
)
1642 struct mm_struct
*mm
= current
->mm
;
1644 down_write(&mm
->mmap_sem
);
1645 ret
= do_munmap(mm
, addr
, len
);
1646 up_write(&mm
->mmap_sem
);
1651 * release all the mappings made in a process's VM space
1653 void exit_mmap(struct mm_struct
*mm
)
1655 struct vm_area_struct
*vma
;
1664 while ((vma
= mm
->mmap
)) {
1665 mm
->mmap
= vma
->vm_next
;
1666 delete_vma_from_mm(vma
);
1667 delete_vma(mm
, vma
);
1673 unsigned long do_brk(unsigned long addr
, unsigned long len
)
1679 * expand (or shrink) an existing mapping, potentially moving it at the same
1680 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1682 * under NOMMU conditions, we only permit changing a mapping's size, and only
1683 * as long as it stays within the region allocated by do_mmap_private() and the
1684 * block is not shareable
1686 * MREMAP_FIXED is not supported under NOMMU conditions
1688 unsigned long do_mremap(unsigned long addr
,
1689 unsigned long old_len
, unsigned long new_len
,
1690 unsigned long flags
, unsigned long new_addr
)
1692 struct vm_area_struct
*vma
;
1694 /* insanity checks first */
1695 if (old_len
== 0 || new_len
== 0)
1696 return (unsigned long) -EINVAL
;
1698 if (addr
& ~PAGE_MASK
)
1701 if (flags
& MREMAP_FIXED
&& new_addr
!= addr
)
1702 return (unsigned long) -EINVAL
;
1704 vma
= find_vma_exact(current
->mm
, addr
, old_len
);
1706 return (unsigned long) -EINVAL
;
1708 if (vma
->vm_end
!= vma
->vm_start
+ old_len
)
1709 return (unsigned long) -EFAULT
;
1711 if (vma
->vm_flags
& VM_MAYSHARE
)
1712 return (unsigned long) -EPERM
;
1714 if (new_len
> vma
->vm_region
->vm_end
- vma
->vm_region
->vm_start
)
1715 return (unsigned long) -ENOMEM
;
1717 /* all checks complete - do it */
1718 vma
->vm_end
= vma
->vm_start
+ new_len
;
1719 return vma
->vm_start
;
1721 EXPORT_SYMBOL(do_mremap
);
1723 SYSCALL_DEFINE5(mremap
, unsigned long, addr
, unsigned long, old_len
,
1724 unsigned long, new_len
, unsigned long, flags
,
1725 unsigned long, new_addr
)
1729 down_write(¤t
->mm
->mmap_sem
);
1730 ret
= do_mremap(addr
, old_len
, new_len
, flags
, new_addr
);
1731 up_write(¤t
->mm
->mmap_sem
);
1735 struct page
*follow_page(struct vm_area_struct
*vma
, unsigned long address
,
1736 unsigned int foll_flags
)
1741 int remap_pfn_range(struct vm_area_struct
*vma
, unsigned long from
,
1742 unsigned long to
, unsigned long size
, pgprot_t prot
)
1744 vma
->vm_start
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1747 EXPORT_SYMBOL(remap_pfn_range
);
1749 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
1750 unsigned long pgoff
)
1752 unsigned int size
= vma
->vm_end
- vma
->vm_start
;
1754 if (!(vma
->vm_flags
& VM_USERMAP
))
1757 vma
->vm_start
= (unsigned long)(addr
+ (pgoff
<< PAGE_SHIFT
));
1758 vma
->vm_end
= vma
->vm_start
+ size
;
1762 EXPORT_SYMBOL(remap_vmalloc_range
);
1764 void swap_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
1768 unsigned long arch_get_unmapped_area(struct file
*file
, unsigned long addr
,
1769 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
1774 void arch_unmap_area(struct mm_struct
*mm
, unsigned long addr
)
1778 void unmap_mapping_range(struct address_space
*mapping
,
1779 loff_t
const holebegin
, loff_t
const holelen
,
1783 EXPORT_SYMBOL(unmap_mapping_range
);
1786 * Check that a process has enough memory to allocate a new virtual
1787 * mapping. 0 means there is enough memory for the allocation to
1788 * succeed and -ENOMEM implies there is not.
1790 * We currently support three overcommit policies, which are set via the
1791 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1793 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1794 * Additional code 2002 Jul 20 by Robert Love.
1796 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1798 * Note this is a helper function intended to be used by LSMs which
1799 * wish to use this logic.
1801 int __vm_enough_memory(struct mm_struct
*mm
, long pages
, int cap_sys_admin
)
1803 unsigned long free
, allowed
;
1805 vm_acct_memory(pages
);
1808 * Sometimes we want to use more memory than we have
1810 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1813 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1816 free
= global_page_state(NR_FILE_PAGES
);
1817 free
+= nr_swap_pages
;
1820 * Any slabs which are created with the
1821 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1822 * which are reclaimable, under pressure. The dentry
1823 * cache and most inode caches should fall into this
1825 free
+= global_page_state(NR_SLAB_RECLAIMABLE
);
1828 * Leave the last 3% for root
1837 * nr_free_pages() is very expensive on large systems,
1838 * only call if we're about to fail.
1840 n
= nr_free_pages();
1843 * Leave reserved pages. The pages are not for anonymous pages.
1845 if (n
<= totalreserve_pages
)
1848 n
-= totalreserve_pages
;
1851 * Leave the last 3% for root
1863 allowed
= totalram_pages
* sysctl_overcommit_ratio
/ 100;
1865 * Leave the last 3% for root
1868 allowed
-= allowed
/ 32;
1869 allowed
+= total_swap_pages
;
1871 /* Don't let a single process grow too big:
1872 leave 3% of the size of this process for other processes */
1874 allowed
-= mm
->total_vm
/ 32;
1876 if (percpu_counter_read_positive(&vm_committed_as
) < allowed
)
1880 vm_unacct_memory(pages
);
1885 int in_gate_area_no_task(unsigned long addr
)
1890 int filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1895 EXPORT_SYMBOL(filemap_fault
);
1898 * Access another process' address space.
1899 * - source/target buffer must be kernel space
1901 int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
)
1903 struct vm_area_struct
*vma
;
1904 struct mm_struct
*mm
;
1906 if (addr
+ len
< addr
)
1909 mm
= get_task_mm(tsk
);
1913 down_read(&mm
->mmap_sem
);
1915 /* the access must start within one of the target process's mappings */
1916 vma
= find_vma(mm
, addr
);
1918 /* don't overrun this mapping */
1919 if (addr
+ len
>= vma
->vm_end
)
1920 len
= vma
->vm_end
- addr
;
1922 /* only read or write mappings where it is permitted */
1923 if (write
&& vma
->vm_flags
& VM_MAYWRITE
)
1924 copy_to_user_page(vma
, NULL
, addr
,
1925 (void *) addr
, buf
, len
);
1926 else if (!write
&& vma
->vm_flags
& VM_MAYREAD
)
1927 copy_from_user_page(vma
, NULL
, addr
,
1928 buf
, (void *) addr
, len
);
1935 up_read(&mm
->mmap_sem
);
1941 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1942 * @inode: The inode to check
1943 * @size: The current filesize of the inode
1944 * @newsize: The proposed filesize of the inode
1946 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1947 * make sure that that any outstanding VMAs aren't broken and then shrink the
1948 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1949 * automatically grant mappings that are too large.
1951 int nommu_shrink_inode_mappings(struct inode
*inode
, size_t size
,
1954 struct vm_area_struct
*vma
;
1955 struct prio_tree_iter iter
;
1956 struct vm_region
*region
;
1958 size_t r_size
, r_top
;
1960 low
= newsize
>> PAGE_SHIFT
;
1961 high
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1963 down_write(&nommu_region_sem
);
1965 /* search for VMAs that fall within the dead zone */
1966 vma_prio_tree_foreach(vma
, &iter
, &inode
->i_mapping
->i_mmap
,
1968 /* found one - only interested if it's shared out of the page
1970 if (vma
->vm_flags
& VM_SHARED
) {
1971 up_write(&nommu_region_sem
);
1972 return -ETXTBSY
; /* not quite true, but near enough */
1976 /* reduce any regions that overlap the dead zone - if in existence,
1977 * these will be pointed to by VMAs that don't overlap the dead zone
1979 * we don't check for any regions that start beyond the EOF as there
1982 vma_prio_tree_foreach(vma
, &iter
, &inode
->i_mapping
->i_mmap
,
1984 if (!(vma
->vm_flags
& VM_SHARED
))
1987 region
= vma
->vm_region
;
1988 r_size
= region
->vm_top
- region
->vm_start
;
1989 r_top
= (region
->vm_pgoff
<< PAGE_SHIFT
) + r_size
;
1991 if (r_top
> newsize
) {
1992 region
->vm_top
-= r_top
- newsize
;
1993 if (region
->vm_end
> region
->vm_top
)
1994 region
->vm_end
= region
->vm_top
;
1998 up_write(&nommu_region_sem
);