xref: /linux/mm/nommu.c (revision e1ca7788dec6773b1a2bce51b7141948f2b8bccf)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/nommu.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Replacement code for mm functions to support CPU's that don't
51da177e4SLinus Torvalds  *  have any form of memory management unit (thus no virtual memory).
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  See Documentation/nommu-mmap.txt
81da177e4SLinus Torvalds  *
98feae131SDavid Howells  *  Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
101da177e4SLinus Torvalds  *  Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
111da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
121da177e4SLinus Torvalds  *  Copyright (c) 2002      Greg Ungerer <gerg@snapgear.com>
13eb6434d9SPaul Mundt  *  Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
16f2b8544fSDavid Howells #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/mm.h>
181da177e4SLinus Torvalds #include <linux/mman.h>
191da177e4SLinus Torvalds #include <linux/swap.h>
201da177e4SLinus Torvalds #include <linux/file.h>
211da177e4SLinus Torvalds #include <linux/highmem.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
231da177e4SLinus Torvalds #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/vmalloc.h>
25fa8e26ccSRoland McGrath #include <linux/tracehook.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/personality.h>
301da177e4SLinus Torvalds #include <linux/security.h>
311da177e4SLinus Torvalds #include <linux/syscalls.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <asm/uaccess.h>
341da177e4SLinus Torvalds #include <asm/tlb.h>
351da177e4SLinus Torvalds #include <asm/tlbflush.h>
36eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
378feae131SDavid Howells #include "internal.h"
388feae131SDavid Howells 
398feae131SDavid Howells #if 0
408feae131SDavid Howells #define kenter(FMT, ...) \
418feae131SDavid Howells 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
428feae131SDavid Howells #define kleave(FMT, ...) \
438feae131SDavid Howells 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
448feae131SDavid Howells #define kdebug(FMT, ...) \
458feae131SDavid Howells 	printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
468feae131SDavid Howells #else
478feae131SDavid Howells #define kenter(FMT, ...) \
488feae131SDavid Howells 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
498feae131SDavid Howells #define kleave(FMT, ...) \
508feae131SDavid Howells 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
518feae131SDavid Howells #define kdebug(FMT, ...) \
528feae131SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
538feae131SDavid Howells #endif
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds void *high_memory;
561da177e4SLinus Torvalds struct page *mem_map;
571da177e4SLinus Torvalds unsigned long max_mapnr;
581da177e4SLinus Torvalds unsigned long num_physpages;
594266c97aSHugh Dickins unsigned long highest_memmap_pfn;
6000a62ce9SKOSAKI Motohiro struct percpu_counter vm_committed_as;
611da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
621da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */
631da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
64fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
651da177e4SLinus Torvalds int heap_stack_gap = 0;
661da177e4SLinus Torvalds 
6733e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
688feae131SDavid Howells 
691da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
706a04de6dSWu, Bryan EXPORT_SYMBOL(num_physpages);
711da177e4SLinus Torvalds 
728feae131SDavid Howells /* list of mapped, potentially shareable regions */
738feae131SDavid Howells static struct kmem_cache *vm_region_jar;
748feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
758feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
761da177e4SLinus Torvalds 
77f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
781da177e4SLinus Torvalds };
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
821da177e4SLinus Torvalds  * just what the caller asked for.
831da177e4SLinus Torvalds  *
841da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct page *page;
891da177e4SLinus Torvalds 
904016a139SMichael Hennerich 	/*
914016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
924016a139SMichael Hennerich 	 * return size of 0
934016a139SMichael Hennerich 	 */
945a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
956cfd53fcSPaul Mundt 		return 0;
966cfd53fcSPaul Mundt 
976cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
986cfd53fcSPaul Mundt 
996cfd53fcSPaul Mundt 	/*
1006cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
1016cfd53fcSPaul Mundt 	 * kmalloc().
1026cfd53fcSPaul Mundt 	 */
1031da177e4SLinus Torvalds 	if (PageSlab(page))
1041da177e4SLinus Torvalds 		return ksize(objp);
1051da177e4SLinus Torvalds 
1066cfd53fcSPaul Mundt 	/*
107ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
108ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
109ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
110ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
111ab2e83eaSPaul Mundt 	 */
112ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
113ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
114ab2e83eaSPaul Mundt 
115ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
116ab2e83eaSPaul Mundt 		if (vma)
117ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
118ab2e83eaSPaul Mundt 	}
119ab2e83eaSPaul Mundt 
120ab2e83eaSPaul Mundt 	/*
1216cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1225a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1236cfd53fcSPaul Mundt 	 */
1245a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(page);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
127b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1284266c97aSHugh Dickins 		     unsigned long start, int nr_pages, unsigned int foll_flags,
1291da177e4SLinus Torvalds 		     struct page **pages, struct vm_area_struct **vmas)
1301da177e4SLinus Torvalds {
131910e46daSSonic Zhang 	struct vm_area_struct *vma;
1327b4d5b8bSDavid Howells 	unsigned long vm_flags;
1337b4d5b8bSDavid Howells 	int i;
1347b4d5b8bSDavid Howells 
1357b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
13658fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1377b4d5b8bSDavid Howells 	 */
13858fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
13958fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
14058fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
14158fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1421da177e4SLinus Torvalds 
1439d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
1447561e8caSDavid Howells 		vma = find_vma(mm, start);
145910e46daSSonic Zhang 		if (!vma)
1467b4d5b8bSDavid Howells 			goto finish_or_fault;
1477b4d5b8bSDavid Howells 
1487b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1491c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1501c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1517b4d5b8bSDavid Howells 			goto finish_or_fault;
152910e46daSSonic Zhang 
1531da177e4SLinus Torvalds 		if (pages) {
1541da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1551da177e4SLinus Torvalds 			if (pages[i])
1561da177e4SLinus Torvalds 				page_cache_get(pages[i]);
1571da177e4SLinus Torvalds 		}
1581da177e4SLinus Torvalds 		if (vmas)
159910e46daSSonic Zhang 			vmas[i] = vma;
160e1ee65d8SDavid Howells 		start = (start + PAGE_SIZE) & PAGE_MASK;
1611da177e4SLinus Torvalds 	}
1627b4d5b8bSDavid Howells 
1637b4d5b8bSDavid Howells 	return i;
1647b4d5b8bSDavid Howells 
1657b4d5b8bSDavid Howells finish_or_fault:
1667b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1671da177e4SLinus Torvalds }
168b291f000SNick Piggin 
169b291f000SNick Piggin /*
170b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
171b291f000SNick Piggin  * and indicate the VMA that covers each page
172b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
173b291f000SNick Piggin  *   slab page or a secondary page from a compound page
174b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
175b291f000SNick Piggin  */
176b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1779d73777eSPeter Zijlstra 	unsigned long start, int nr_pages, int write, int force,
178b291f000SNick Piggin 	struct page **pages, struct vm_area_struct **vmas)
179b291f000SNick Piggin {
180b291f000SNick Piggin 	int flags = 0;
181b291f000SNick Piggin 
182b291f000SNick Piggin 	if (write)
18358fa879eSHugh Dickins 		flags |= FOLL_WRITE;
184b291f000SNick Piggin 	if (force)
18558fa879eSHugh Dickins 		flags |= FOLL_FORCE;
186b291f000SNick Piggin 
1879d73777eSPeter Zijlstra 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
188b291f000SNick Piggin }
18966aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages);
19066aa2b4bSGreg Ungerer 
191dfc2f91aSPaul Mundt /**
192dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
193dfc2f91aSPaul Mundt  * @vma: memory mapping
194dfc2f91aSPaul Mundt  * @address: user virtual address
195dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
196dfc2f91aSPaul Mundt  *
197dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
198dfc2f91aSPaul Mundt  *
199dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
200dfc2f91aSPaul Mundt  */
201dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
202dfc2f91aSPaul Mundt 	unsigned long *pfn)
203dfc2f91aSPaul Mundt {
204dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
205dfc2f91aSPaul Mundt 		return -EINVAL;
206dfc2f91aSPaul Mundt 
207dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
208dfc2f91aSPaul Mundt 	return 0;
209dfc2f91aSPaul Mundt }
210dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
211dfc2f91aSPaul Mundt 
2121da177e4SLinus Torvalds DEFINE_RWLOCK(vmlist_lock);
2131da177e4SLinus Torvalds struct vm_struct *vmlist;
2141da177e4SLinus Torvalds 
215b3bdda02SChristoph Lameter void vfree(const void *addr)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	kfree(addr);
2181da177e4SLinus Torvalds }
219b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2201da177e4SLinus Torvalds 
221dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	/*
2248518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2258518609dSRobert P. J. Day 	 * returns only a logical address.
2261da177e4SLinus Torvalds 	 */
22784097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2281da177e4SLinus Torvalds }
229b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2301da177e4SLinus Torvalds 
231f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
232f905bc44SPaul Mundt {
233f905bc44SPaul Mundt 	void *ret;
234f905bc44SPaul Mundt 
235f905bc44SPaul Mundt 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
236f905bc44SPaul Mundt 			PAGE_KERNEL);
237f905bc44SPaul Mundt 	if (ret) {
238f905bc44SPaul Mundt 		struct vm_area_struct *vma;
239f905bc44SPaul Mundt 
240f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
241f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
242f905bc44SPaul Mundt 		if (vma)
243f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
244f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
245f905bc44SPaul Mundt 	}
246f905bc44SPaul Mundt 
247f905bc44SPaul Mundt 	return ret;
248f905bc44SPaul Mundt }
249f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
250f905bc44SPaul Mundt 
251b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	return virt_to_page(addr);
2541da177e4SLinus Torvalds }
255b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
2561da177e4SLinus Torvalds 
257b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2601da177e4SLinus Torvalds }
261b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2661da177e4SLinus Torvalds 	return count;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	/* Don't allow overflow */
2721da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2731da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	memcpy(addr, buf, count);
2761da177e4SLinus Torvalds 	return(count);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /*
2801da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually continguos memory
2811da177e4SLinus Torvalds  *
2821da177e4SLinus Torvalds  *	@size:		allocation size
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
2851da177e4SLinus Torvalds  *	allocator and map them into continguos kernel virtual space.
2861da177e4SLinus Torvalds  *
287c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2881da177e4SLinus Torvalds  *	use __vmalloc() instead.
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
2931da177e4SLinus Torvalds }
294f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
295f6138882SAndrew Morton 
296*e1ca7788SDave Young /*
297*e1ca7788SDave Young  *	vzalloc - allocate virtually continguos memory with zero fill
298*e1ca7788SDave Young  *
299*e1ca7788SDave Young  *	@size:		allocation size
300*e1ca7788SDave Young  *
301*e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
302*e1ca7788SDave Young  *	allocator and map them into continguos kernel virtual space.
303*e1ca7788SDave Young  *	The memory allocated is set to zero.
304*e1ca7788SDave Young  *
305*e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
306*e1ca7788SDave Young  *	use __vmalloc() instead.
307*e1ca7788SDave Young  */
308*e1ca7788SDave Young void *vzalloc(unsigned long size)
309*e1ca7788SDave Young {
310*e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
311*e1ca7788SDave Young 			PAGE_KERNEL);
312*e1ca7788SDave Young }
313*e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
314*e1ca7788SDave Young 
315*e1ca7788SDave Young /**
316*e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
317*e1ca7788SDave Young  * @size:	allocation size
318*e1ca7788SDave Young  * @node:	numa node
319*e1ca7788SDave Young  *
320*e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
321*e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
322*e1ca7788SDave Young  *
323*e1ca7788SDave Young  * For tight control over page level allocator and protection flags
324*e1ca7788SDave Young  * use __vmalloc() instead.
325*e1ca7788SDave Young  */
326f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
327f6138882SAndrew Morton {
328f6138882SAndrew Morton 	return vmalloc(size);
329f6138882SAndrew Morton }
330*e1ca7788SDave Young 
331*e1ca7788SDave Young /**
332*e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
333*e1ca7788SDave Young  * @size:	allocation size
334*e1ca7788SDave Young  * @node:	numa node
335*e1ca7788SDave Young  *
336*e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
337*e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
338*e1ca7788SDave Young  * The memory allocated is set to zero.
339*e1ca7788SDave Young  *
340*e1ca7788SDave Young  * For tight control over page level allocator and protection flags
341*e1ca7788SDave Young  * use __vmalloc() instead.
342*e1ca7788SDave Young  */
343*e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
344*e1ca7788SDave Young {
345*e1ca7788SDave Young 	return vzalloc(size);
346*e1ca7788SDave Young }
347*e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3481da177e4SLinus Torvalds 
3491af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
3501af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
3511af446edSPaul Mundt #endif
3521af446edSPaul Mundt 
3531af446edSPaul Mundt /**
3541af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
3551af446edSPaul Mundt  *	@size:		allocation size
3561af446edSPaul Mundt  *
3571af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
3581af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
3591af446edSPaul Mundt  *	executable kernel virtual space.
3601af446edSPaul Mundt  *
3611af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3621af446edSPaul Mundt  *	use __vmalloc() instead.
3631af446edSPaul Mundt  */
3641af446edSPaul Mundt 
3651af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3661af446edSPaul Mundt {
3671af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3681af446edSPaul Mundt }
3691af446edSPaul Mundt 
370b5073173SPaul Mundt /**
371b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3721da177e4SLinus Torvalds  *	@size:		allocation size
3731da177e4SLinus Torvalds  *
3741da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
3751da177e4SLinus Torvalds  *	page level allocator and map them into continguos kernel virtual space.
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
3801da177e4SLinus Torvalds }
381b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
382b5073173SPaul Mundt 
383b5073173SPaul Mundt /**
384b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
385b5073173SPaul Mundt  *	@size:		allocation size
386b5073173SPaul Mundt  *
387b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
388b5073173SPaul Mundt  * mapped to userspace without leaking data.
389f905bc44SPaul Mundt  *
390f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
391f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
392b5073173SPaul Mundt  */
393b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
394b5073173SPaul Mundt {
395f905bc44SPaul Mundt 	/*
396f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
397f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
398f905bc44SPaul Mundt 	 */
399f905bc44SPaul Mundt 	return vmalloc_user(size);
400b5073173SPaul Mundt }
401b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	BUG();
4061da177e4SLinus Torvalds 	return NULL;
4071da177e4SLinus Torvalds }
408b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
4091da177e4SLinus Torvalds 
410b3bdda02SChristoph Lameter void vunmap(const void *addr)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	BUG();
4131da177e4SLinus Torvalds }
414b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
4151da177e4SLinus Torvalds 
416eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
417eb6434d9SPaul Mundt {
418eb6434d9SPaul Mundt 	BUG();
419eb6434d9SPaul Mundt 	return NULL;
420eb6434d9SPaul Mundt }
421eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
422eb6434d9SPaul Mundt 
423eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
424eb6434d9SPaul Mundt {
425eb6434d9SPaul Mundt 	BUG();
426eb6434d9SPaul Mundt }
427eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
428eb6434d9SPaul Mundt 
429eb6434d9SPaul Mundt void vm_unmap_aliases(void)
430eb6434d9SPaul Mundt {
431eb6434d9SPaul Mundt }
432eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
433eb6434d9SPaul Mundt 
4341da177e4SLinus Torvalds /*
4351eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
4361eeb66a1SChristoph Hellwig  * have one.
4371eeb66a1SChristoph Hellwig  */
4381eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
4391eeb66a1SChristoph Hellwig {
4401eeb66a1SChristoph Hellwig }
4411eeb66a1SChristoph Hellwig 
442b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
443b5073173SPaul Mundt 		   struct page *page)
444b5073173SPaul Mundt {
445b5073173SPaul Mundt 	return -EINVAL;
446b5073173SPaul Mundt }
447b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
448b5073173SPaul Mundt 
4491eeb66a1SChristoph Hellwig /*
4501da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4511da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4521da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4531da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4541da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4551da177e4SLinus Torvalds  */
4566a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
4611da177e4SLinus Torvalds 		return mm->brk;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if (mm->brk == brk)
4641da177e4SLinus Torvalds 		return mm->brk;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	/*
4671da177e4SLinus Torvalds 	 * Always allow shrinking brk
4681da177e4SLinus Torvalds 	 */
4691da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4701da177e4SLinus Torvalds 		mm->brk = brk;
4711da177e4SLinus Torvalds 		return brk;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	/*
4751da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4761da177e4SLinus Torvalds 	 */
477cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
4781da177e4SLinus Torvalds 	return mm->brk = brk;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4818feae131SDavid Howells /*
4828feae131SDavid Howells  * initialise the VMA and region record slabs
4838feae131SDavid Howells  */
4848feae131SDavid Howells void __init mmap_init(void)
4851da177e4SLinus Torvalds {
48600a62ce9SKOSAKI Motohiro 	int ret;
48700a62ce9SKOSAKI Motohiro 
48800a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
48900a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
49033e5d769SDavid Howells 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
4918feae131SDavid Howells }
4921da177e4SLinus Torvalds 
4938feae131SDavid Howells /*
4948feae131SDavid Howells  * validate the region tree
4958feae131SDavid Howells  * - the caller must hold the region lock
4968feae131SDavid Howells  */
4978feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4988feae131SDavid Howells static noinline void validate_nommu_regions(void)
4998feae131SDavid Howells {
5008feae131SDavid Howells 	struct vm_region *region, *last;
5018feae131SDavid Howells 	struct rb_node *p, *lastp;
5021da177e4SLinus Torvalds 
5038feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
5048feae131SDavid Howells 	if (!lastp)
5058feae131SDavid Howells 		return;
5068feae131SDavid Howells 
5078feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
50833e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_end <= last->vm_start));
50933e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_top < last->vm_end));
5108feae131SDavid Howells 
5118feae131SDavid Howells 	while ((p = rb_next(lastp))) {
5128feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
5138feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
5148feae131SDavid Howells 
51533e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_end <= region->vm_start));
51633e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_top < region->vm_end));
51733e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_start < last->vm_top));
5188feae131SDavid Howells 
5198feae131SDavid Howells 		lastp = p;
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds }
5228feae131SDavid Howells #else
52333e5d769SDavid Howells static void validate_nommu_regions(void)
52433e5d769SDavid Howells {
52533e5d769SDavid Howells }
5268feae131SDavid Howells #endif
5278feae131SDavid Howells 
5288feae131SDavid Howells /*
5298feae131SDavid Howells  * add a region into the global tree
5308feae131SDavid Howells  */
5318feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
5328feae131SDavid Howells {
5338feae131SDavid Howells 	struct vm_region *pregion;
5348feae131SDavid Howells 	struct rb_node **p, *parent;
5358feae131SDavid Howells 
5368feae131SDavid Howells 	validate_nommu_regions();
5378feae131SDavid Howells 
5388feae131SDavid Howells 	parent = NULL;
5398feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
5408feae131SDavid Howells 	while (*p) {
5418feae131SDavid Howells 		parent = *p;
5428feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5438feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5448feae131SDavid Howells 			p = &(*p)->rb_left;
5458feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5468feae131SDavid Howells 			p = &(*p)->rb_right;
5478feae131SDavid Howells 		else if (pregion == region)
5488feae131SDavid Howells 			return;
5498feae131SDavid Howells 		else
5508feae131SDavid Howells 			BUG();
5518feae131SDavid Howells 	}
5528feae131SDavid Howells 
5538feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5548feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5558feae131SDavid Howells 
5568feae131SDavid Howells 	validate_nommu_regions();
5578feae131SDavid Howells }
5588feae131SDavid Howells 
5598feae131SDavid Howells /*
5608feae131SDavid Howells  * delete a region from the global tree
5618feae131SDavid Howells  */
5628feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
5638feae131SDavid Howells {
5648feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5658feae131SDavid Howells 
5668feae131SDavid Howells 	validate_nommu_regions();
5678feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5688feae131SDavid Howells 	validate_nommu_regions();
5698feae131SDavid Howells }
5708feae131SDavid Howells 
5718feae131SDavid Howells /*
5728feae131SDavid Howells  * free a contiguous series of pages
5738feae131SDavid Howells  */
5748feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5758feae131SDavid Howells {
5768feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5778feae131SDavid Howells 		struct page *page = virt_to_page(from);
5788feae131SDavid Howells 
5798feae131SDavid Howells 		kdebug("- free %lx", from);
58033e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5818feae131SDavid Howells 		if (page_count(page) != 1)
58233e5d769SDavid Howells 			kdebug("free page %p: refcount not one: %d",
58333e5d769SDavid Howells 			       page, page_count(page));
5848feae131SDavid Howells 		put_page(page);
5858feae131SDavid Howells 	}
5868feae131SDavid Howells }
5878feae131SDavid Howells 
5888feae131SDavid Howells /*
5898feae131SDavid Howells  * release a reference to a region
59033e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
591dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5928feae131SDavid Howells  *   will equal vm_start
5938feae131SDavid Howells  */
5948feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5958feae131SDavid Howells 	__releases(nommu_region_sem)
5968feae131SDavid Howells {
5971e2ae599SDavid Howells 	kenter("%p{%d}", region, region->vm_usage);
5988feae131SDavid Howells 
5998feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6008feae131SDavid Howells 
6011e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
602dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
6038feae131SDavid Howells 			delete_nommu_region(region);
6048feae131SDavid Howells 		up_write(&nommu_region_sem);
6058feae131SDavid Howells 
6068feae131SDavid Howells 		if (region->vm_file)
6078feae131SDavid Howells 			fput(region->vm_file);
6088feae131SDavid Howells 
6098feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
6108feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
6118feae131SDavid Howells 		if (region->vm_flags & VM_MAPPED_COPY) {
6128feae131SDavid Howells 			kdebug("free series");
613dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
6148feae131SDavid Howells 		}
6158feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
6168feae131SDavid Howells 	} else {
6178feae131SDavid Howells 		up_write(&nommu_region_sem);
6188feae131SDavid Howells 	}
6198feae131SDavid Howells }
6208feae131SDavid Howells 
6218feae131SDavid Howells /*
6228feae131SDavid Howells  * release a reference to a region
6238feae131SDavid Howells  */
6248feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
6258feae131SDavid Howells {
6268feae131SDavid Howells 	down_write(&nommu_region_sem);
6278feae131SDavid Howells 	__put_nommu_region(region);
6288feae131SDavid Howells }
6291da177e4SLinus Torvalds 
6303034097aSDavid Howells /*
631eb8cdec4SBernd Schmidt  * update protection on a vma
632eb8cdec4SBernd Schmidt  */
633eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
634eb8cdec4SBernd Schmidt {
635eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
636eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
637eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
638eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
639eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
640eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
641eb8cdec4SBernd Schmidt 	}
642eb8cdec4SBernd Schmidt 	update_protections(mm);
643eb8cdec4SBernd Schmidt #endif
644eb8cdec4SBernd Schmidt }
645eb8cdec4SBernd Schmidt 
646eb8cdec4SBernd Schmidt /*
6473034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
6488feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
6498feae131SDavid Howells  * page
6503034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
6513034097aSDavid Howells  */
6528feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
6533034097aSDavid Howells {
654297c5eeeSLinus Torvalds 	struct vm_area_struct *pvma, **pp, *next;
6558feae131SDavid Howells 	struct address_space *mapping;
6568feae131SDavid Howells 	struct rb_node **p, *parent;
6573034097aSDavid Howells 
6588feae131SDavid Howells 	kenter(",%p", vma);
6598feae131SDavid Howells 
6608feae131SDavid Howells 	BUG_ON(!vma->vm_region);
6618feae131SDavid Howells 
6628feae131SDavid Howells 	mm->map_count++;
6638feae131SDavid Howells 	vma->vm_mm = mm;
6648feae131SDavid Howells 
665eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
666eb8cdec4SBernd Schmidt 
6678feae131SDavid Howells 	/* add the VMA to the mapping */
6688feae131SDavid Howells 	if (vma->vm_file) {
6698feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6708feae131SDavid Howells 
6718feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6728feae131SDavid Howells 		vma_prio_tree_insert(vma, &mapping->i_mmap);
6738feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
6748feae131SDavid Howells 	}
6758feae131SDavid Howells 
6768feae131SDavid Howells 	/* add the VMA to the tree */
6778feae131SDavid Howells 	parent = NULL;
6788feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
6798feae131SDavid Howells 	while (*p) {
6808feae131SDavid Howells 		parent = *p;
6818feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
6828feae131SDavid Howells 
6838feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
6848feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
6858feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
6868feae131SDavid Howells 			p = &(*p)->rb_left;
6878feae131SDavid Howells 		else if (vma->vm_start > pvma->vm_start)
6888feae131SDavid Howells 			p = &(*p)->rb_right;
6898feae131SDavid Howells 		else if (vma->vm_end < pvma->vm_end)
6908feae131SDavid Howells 			p = &(*p)->rb_left;
6918feae131SDavid Howells 		else if (vma->vm_end > pvma->vm_end)
6928feae131SDavid Howells 			p = &(*p)->rb_right;
6938feae131SDavid Howells 		else if (vma < pvma)
6948feae131SDavid Howells 			p = &(*p)->rb_left;
6958feae131SDavid Howells 		else if (vma > pvma)
6968feae131SDavid Howells 			p = &(*p)->rb_right;
6978feae131SDavid Howells 		else
6988feae131SDavid Howells 			BUG();
6998feae131SDavid Howells 	}
7008feae131SDavid Howells 
7018feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
7028feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
7038feae131SDavid Howells 
7048feae131SDavid Howells 	/* add VMA to the VMA list also */
7058feae131SDavid Howells 	for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
7068feae131SDavid Howells 		if (pvma->vm_start > vma->vm_start)
7073034097aSDavid Howells 			break;
7088feae131SDavid Howells 		if (pvma->vm_start < vma->vm_start)
7098feae131SDavid Howells 			continue;
7108feae131SDavid Howells 		if (pvma->vm_end < vma->vm_end)
7118feae131SDavid Howells 			break;
7128feae131SDavid Howells 	}
7133034097aSDavid Howells 
714297c5eeeSLinus Torvalds 	next = *pp;
7158feae131SDavid Howells 	*pp = vma;
716297c5eeeSLinus Torvalds 	vma->vm_next = next;
717297c5eeeSLinus Torvalds 	if (next)
718297c5eeeSLinus Torvalds 		next->vm_prev = vma;
7198feae131SDavid Howells }
7208feae131SDavid Howells 
7218feae131SDavid Howells /*
7228feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
7238feae131SDavid Howells  */
7248feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
7258feae131SDavid Howells {
7268feae131SDavid Howells 	struct vm_area_struct **pp;
7278feae131SDavid Howells 	struct address_space *mapping;
7288feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
7298feae131SDavid Howells 
7308feae131SDavid Howells 	kenter("%p", vma);
7318feae131SDavid Howells 
732eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
733eb8cdec4SBernd Schmidt 
7348feae131SDavid Howells 	mm->map_count--;
7358feae131SDavid Howells 	if (mm->mmap_cache == vma)
7368feae131SDavid Howells 		mm->mmap_cache = NULL;
7378feae131SDavid Howells 
7388feae131SDavid Howells 	/* remove the VMA from the mapping */
7398feae131SDavid Howells 	if (vma->vm_file) {
7408feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7418feae131SDavid Howells 
7428feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7438feae131SDavid Howells 		vma_prio_tree_remove(vma, &mapping->i_mmap);
7448feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
7458feae131SDavid Howells 	}
7468feae131SDavid Howells 
7478feae131SDavid Howells 	/* remove from the MM's tree and list */
7488feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
7498feae131SDavid Howells 	for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
7508feae131SDavid Howells 		if (*pp == vma) {
7518feae131SDavid Howells 			*pp = vma->vm_next;
7528feae131SDavid Howells 			break;
7538feae131SDavid Howells 		}
7548feae131SDavid Howells 	}
7558feae131SDavid Howells 
7568feae131SDavid Howells 	vma->vm_mm = NULL;
7578feae131SDavid Howells }
7588feae131SDavid Howells 
7598feae131SDavid Howells /*
7608feae131SDavid Howells  * destroy a VMA record
7618feae131SDavid Howells  */
7628feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
7638feae131SDavid Howells {
7648feae131SDavid Howells 	kenter("%p", vma);
7658feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
7668feae131SDavid Howells 		vma->vm_ops->close(vma);
7678feae131SDavid Howells 	if (vma->vm_file) {
7688feae131SDavid Howells 		fput(vma->vm_file);
7698feae131SDavid Howells 		if (vma->vm_flags & VM_EXECUTABLE)
7708feae131SDavid Howells 			removed_exe_file_vma(mm);
7718feae131SDavid Howells 	}
7728feae131SDavid Howells 	put_nommu_region(vma->vm_region);
7738feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
7743034097aSDavid Howells }
7753034097aSDavid Howells 
7763034097aSDavid Howells /*
7773034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
7783034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7793034097aSDavid Howells  */
7803034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
7813034097aSDavid Howells {
7828feae131SDavid Howells 	struct vm_area_struct *vma;
7838feae131SDavid Howells 	struct rb_node *n = mm->mm_rb.rb_node;
7843034097aSDavid Howells 
7858feae131SDavid Howells 	/* check the cache first */
7868feae131SDavid Howells 	vma = mm->mmap_cache;
7878feae131SDavid Howells 	if (vma && vma->vm_start <= addr && vma->vm_end > addr)
7888feae131SDavid Howells 		return vma;
7898feae131SDavid Howells 
7908feae131SDavid Howells 	/* trawl the tree (there may be multiple mappings in which addr
7918feae131SDavid Howells 	 * resides) */
7928feae131SDavid Howells 	for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
7938feae131SDavid Howells 		vma = rb_entry(n, struct vm_area_struct, vm_rb);
7948feae131SDavid Howells 		if (vma->vm_start > addr)
7958feae131SDavid Howells 			return NULL;
7968feae131SDavid Howells 		if (vma->vm_end > addr) {
7978feae131SDavid Howells 			mm->mmap_cache = vma;
7988feae131SDavid Howells 			return vma;
7993034097aSDavid Howells 		}
8008feae131SDavid Howells 	}
8013034097aSDavid Howells 
8023034097aSDavid Howells 	return NULL;
8033034097aSDavid Howells }
8043034097aSDavid Howells EXPORT_SYMBOL(find_vma);
8053034097aSDavid Howells 
8063034097aSDavid Howells /*
807930e652aSDavid Howells  * find a VMA
808930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
809930e652aSDavid Howells  */
810930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
811930e652aSDavid Howells {
8127561e8caSDavid Howells 	return find_vma(mm, addr);
813930e652aSDavid Howells }
814930e652aSDavid Howells 
8158feae131SDavid Howells /*
8168feae131SDavid Howells  * expand a stack to a given address
8178feae131SDavid Howells  * - not supported under NOMMU conditions
8188feae131SDavid Howells  */
81957c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
82057c8f63eSGreg Ungerer {
82157c8f63eSGreg Ungerer 	return -ENOMEM;
82257c8f63eSGreg Ungerer }
82357c8f63eSGreg Ungerer 
824930e652aSDavid Howells /*
8256fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
8266fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8276fa5f80bSDavid Howells  */
8288feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
8298feae131SDavid Howells 					     unsigned long addr,
8308feae131SDavid Howells 					     unsigned long len)
8311da177e4SLinus Torvalds {
8321da177e4SLinus Torvalds 	struct vm_area_struct *vma;
8338feae131SDavid Howells 	struct rb_node *n = mm->mm_rb.rb_node;
8348feae131SDavid Howells 	unsigned long end = addr + len;
8351da177e4SLinus Torvalds 
8368feae131SDavid Howells 	/* check the cache first */
8378feae131SDavid Howells 	vma = mm->mmap_cache;
8388feae131SDavid Howells 	if (vma && vma->vm_start == addr && vma->vm_end == end)
8391da177e4SLinus Torvalds 		return vma;
8408feae131SDavid Howells 
8418feae131SDavid Howells 	/* trawl the tree (there may be multiple mappings in which addr
8428feae131SDavid Howells 	 * resides) */
8438feae131SDavid Howells 	for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
8448feae131SDavid Howells 		vma = rb_entry(n, struct vm_area_struct, vm_rb);
8458feae131SDavid Howells 		if (vma->vm_start < addr)
8468feae131SDavid Howells 			continue;
8478feae131SDavid Howells 		if (vma->vm_start > addr)
8488feae131SDavid Howells 			return NULL;
8498feae131SDavid Howells 		if (vma->vm_end == end) {
8508feae131SDavid Howells 			mm->mmap_cache = vma;
8518feae131SDavid Howells 			return vma;
8528feae131SDavid Howells 		}
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	return NULL;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8583034097aSDavid Howells /*
8591da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
8601da177e4SLinus Torvalds  * mapping we're capable of supporting
8611da177e4SLinus Torvalds  */
8621da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
8631da177e4SLinus Torvalds 				 unsigned long addr,
8641da177e4SLinus Torvalds 				 unsigned long len,
8651da177e4SLinus Torvalds 				 unsigned long prot,
8661da177e4SLinus Torvalds 				 unsigned long flags,
8671da177e4SLinus Torvalds 				 unsigned long pgoff,
8681da177e4SLinus Torvalds 				 unsigned long *_capabilities)
8691da177e4SLinus Torvalds {
8708feae131SDavid Howells 	unsigned long capabilities, rlen;
8711da177e4SLinus Torvalds 	unsigned long reqprot = prot;
8721da177e4SLinus Torvalds 	int ret;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	/* do the simple checks first */
87506aab5a3SDavid Howells 	if (flags & MAP_FIXED) {
8761da177e4SLinus Torvalds 		printk(KERN_DEBUG
8771da177e4SLinus Torvalds 		       "%d: Can't do fixed-address/overlay mmap of RAM\n",
8781da177e4SLinus Torvalds 		       current->pid);
8791da177e4SLinus Torvalds 		return -EINVAL;
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
8831da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
8841da177e4SLinus Torvalds 		return -EINVAL;
8851da177e4SLinus Torvalds 
886f81cff0dSMike Frysinger 	if (!len)
8871da177e4SLinus Torvalds 		return -EINVAL;
8881da177e4SLinus Torvalds 
889f81cff0dSMike Frysinger 	/* Careful about overflows.. */
8908feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
8918feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
892f81cff0dSMike Frysinger 		return -ENOMEM;
893f81cff0dSMike Frysinger 
8941da177e4SLinus Torvalds 	/* offset overflow? */
8958feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
896f81cff0dSMike Frysinger 		return -EOVERFLOW;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	if (file) {
8991da177e4SLinus Torvalds 		/* validate file mapping requests */
9001da177e4SLinus Torvalds 		struct address_space *mapping;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 		/* files must support mmap */
9031da177e4SLinus Torvalds 		if (!file->f_op || !file->f_op->mmap)
9041da177e4SLinus Torvalds 			return -ENODEV;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
9071da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
9081da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
9091da177e4SLinus Torvalds 		 */
9101da177e4SLinus Torvalds 		mapping = file->f_mapping;
9111da177e4SLinus Torvalds 		if (!mapping)
912e9536ae7SJosef Sipek 			mapping = file->f_path.dentry->d_inode->i_mapping;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		capabilities = 0;
9151da177e4SLinus Torvalds 		if (mapping && mapping->backing_dev_info)
9161da177e4SLinus Torvalds 			capabilities = mapping->backing_dev_info->capabilities;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 		if (!capabilities) {
9191da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
9201da177e4SLinus Torvalds 			 * defaults */
921e9536ae7SJosef Sipek 			switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
9221da177e4SLinus Torvalds 			case S_IFREG:
9231da177e4SLinus Torvalds 			case S_IFBLK:
9241da177e4SLinus Torvalds 				capabilities = BDI_CAP_MAP_COPY;
9251da177e4SLinus Torvalds 				break;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 			case S_IFCHR:
9281da177e4SLinus Torvalds 				capabilities =
9291da177e4SLinus Torvalds 					BDI_CAP_MAP_DIRECT |
9301da177e4SLinus Torvalds 					BDI_CAP_READ_MAP |
9311da177e4SLinus Torvalds 					BDI_CAP_WRITE_MAP;
9321da177e4SLinus Torvalds 				break;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 			default:
9351da177e4SLinus Torvalds 				return -EINVAL;
9361da177e4SLinus Torvalds 			}
9371da177e4SLinus Torvalds 		}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
9401da177e4SLinus Torvalds 		 * device */
9411da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
9421da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
9431da177e4SLinus Torvalds 		if (!file->f_op->read)
9441da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
9451da177e4SLinus Torvalds 
94628d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
94728d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
94828d7a6aeSGraff Yang 			return -EACCES;
94928d7a6aeSGraff Yang 
9501da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
9511da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
9521da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
9531da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
9541da177e4SLinus Torvalds 				return -EACCES;
9551da177e4SLinus Torvalds 
956e9536ae7SJosef Sipek 			if (IS_APPEND(file->f_path.dentry->d_inode) &&
9571da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
9581da177e4SLinus Torvalds 				return -EACCES;
9591da177e4SLinus Torvalds 
960e9536ae7SJosef Sipek 			if (locks_verify_locked(file->f_path.dentry->d_inode))
9611da177e4SLinus Torvalds 				return -EAGAIN;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_DIRECT))
9641da177e4SLinus Torvalds 				return -ENODEV;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
9671da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
9681da177e4SLinus Torvalds 		}
9691da177e4SLinus Torvalds 		else {
9701da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
9711da177e4SLinus Torvalds 			 * allocate */
9721da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_COPY))
9731da177e4SLinus Torvalds 				return -ENODEV;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
9761da177e4SLinus Torvalds 			 * shared with the backing device */
9771da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
9781da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
9791da177e4SLinus Torvalds 		}
9801da177e4SLinus Torvalds 
9813c7b2045SBernd Schmidt 		if (capabilities & BDI_CAP_MAP_DIRECT) {
9823c7b2045SBernd Schmidt 			if (((prot & PROT_READ)  && !(capabilities & BDI_CAP_READ_MAP))  ||
9833c7b2045SBernd Schmidt 			    ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
9843c7b2045SBernd Schmidt 			    ((prot & PROT_EXEC)  && !(capabilities & BDI_CAP_EXEC_MAP))
9853c7b2045SBernd Schmidt 			    ) {
9863c7b2045SBernd Schmidt 				capabilities &= ~BDI_CAP_MAP_DIRECT;
9873c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
9883c7b2045SBernd Schmidt 					printk(KERN_WARNING
9893c7b2045SBernd Schmidt 					       "MAP_SHARED not completely supported on !MMU\n");
9903c7b2045SBernd Schmidt 					return -EINVAL;
9913c7b2045SBernd Schmidt 				}
9923c7b2045SBernd Schmidt 			}
9933c7b2045SBernd Schmidt 		}
9943c7b2045SBernd Schmidt 
9951da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
9961da177e4SLinus Torvalds 		 * mappings */
997e9536ae7SJosef Sipek 		if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
9981da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
9991da177e4SLinus Torvalds 				return -EPERM;
10001da177e4SLinus Torvalds 		}
10011da177e4SLinus Torvalds 		else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
10021da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
10031da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
10041da177e4SLinus Torvalds 				if (capabilities & BDI_CAP_EXEC_MAP)
10051da177e4SLinus Torvalds 					prot |= PROT_EXEC;
10061da177e4SLinus Torvalds 			}
10071da177e4SLinus Torvalds 		}
10081da177e4SLinus Torvalds 		else if ((prot & PROT_READ) &&
10091da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
10101da177e4SLinus Torvalds 			 !(capabilities & BDI_CAP_EXEC_MAP)
10111da177e4SLinus Torvalds 			 ) {
10121da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
10131da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
10141da177e4SLinus Torvalds 		}
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	else {
10171da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
10181da177e4SLinus Torvalds 		 * privately mapped
10191da177e4SLinus Torvalds 		 */
10201da177e4SLinus Torvalds 		capabilities = BDI_CAP_MAP_COPY;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
10231da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
10241da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
10251da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	/* allow the security API to have its say */
1029ed032189SEric Paris 	ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
10301da177e4SLinus Torvalds 	if (ret < 0)
10311da177e4SLinus Torvalds 		return ret;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	/* looks okay */
10341da177e4SLinus Torvalds 	*_capabilities = capabilities;
10351da177e4SLinus Torvalds 	return 0;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds /*
10391da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
10401da177e4SLinus Torvalds  * now know into VMA flags
10411da177e4SLinus Torvalds  */
10421da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
10431da177e4SLinus Torvalds 					unsigned long prot,
10441da177e4SLinus Torvalds 					unsigned long flags,
10451da177e4SLinus Torvalds 					unsigned long capabilities)
10461da177e4SLinus Torvalds {
10471da177e4SLinus Torvalds 	unsigned long vm_flags;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
10501da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
10531da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
10543c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10551da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
10561da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10573c7b2045SBernd Schmidt 	} else {
10581da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
10591da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
10601da177e4SLinus Torvalds 		 * romfs/cramfs */
10613c7b2045SBernd Schmidt 		vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
10621da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
10633c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
10671da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
10681da177e4SLinus Torvalds 	 * with another untraced process
10691da177e4SLinus Torvalds 	 */
1070fa8e26ccSRoland McGrath 	if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
10711da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	return vm_flags;
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds /*
10778feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
10788feae131SDavid Howells  * pins the storage)
10791da177e4SLinus Torvalds  */
10808feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	int ret;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1085dd8632a1SPaul Mundt 	if (ret == 0) {
1086dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1087645d83c5SDavid Howells 		return 0;
1088dd8632a1SPaul Mundt 	}
10891da177e4SLinus Torvalds 	if (ret != -ENOSYS)
10901da177e4SLinus Torvalds 		return ret;
10911da177e4SLinus Torvalds 
10923fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
10933fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
10943fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
10951da177e4SLinus Torvalds 	return -ENODEV;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds /*
10991da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
11001da177e4SLinus Torvalds  */
11018feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
11028feae131SDavid Howells 			   struct vm_region *region,
1103645d83c5SDavid Howells 			   unsigned long len,
1104645d83c5SDavid Howells 			   unsigned long capabilities)
11051da177e4SLinus Torvalds {
11068feae131SDavid Howells 	struct page *pages;
11078feae131SDavid Howells 	unsigned long total, point, n, rlen;
11081da177e4SLinus Torvalds 	void *base;
11098feae131SDavid Howells 	int ret, order;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
11121da177e4SLinus Torvalds 	 * shared mappings on devices or memory
11131da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
11141da177e4SLinus Torvalds 	 */
1115645d83c5SDavid Howells 	if (capabilities & BDI_CAP_MAP_DIRECT) {
11161da177e4SLinus Torvalds 		ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1117dd8632a1SPaul Mundt 		if (ret == 0) {
11181da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1119dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1120dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1121645d83c5SDavid Howells 			return 0;
11221da177e4SLinus Torvalds 		}
1123dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1124dd8632a1SPaul Mundt 			return ret;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
11271da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
11281da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
11291da177e4SLinus Torvalds 	}
11301da177e4SLinus Torvalds 
11318feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
11328feae131SDavid Howells 
11331da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
11341da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
11351da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
11361da177e4SLinus Torvalds 	 */
11378feae131SDavid Howells 	order = get_order(rlen);
11388feae131SDavid Howells 	kdebug("alloc order %d for %lx", order, len);
11398feae131SDavid Howells 
11408feae131SDavid Howells 	pages = alloc_pages(GFP_KERNEL, order);
11418feae131SDavid Howells 	if (!pages)
11421da177e4SLinus Torvalds 		goto enomem;
11431da177e4SLinus Torvalds 
11448feae131SDavid Howells 	total = 1 << order;
114533e5d769SDavid Howells 	atomic_long_add(total, &mmap_pages_allocated);
11461da177e4SLinus Torvalds 
11478feae131SDavid Howells 	point = rlen >> PAGE_SHIFT;
1148dd8632a1SPaul Mundt 
1149dd8632a1SPaul Mundt 	/* we allocated a power-of-2 sized page set, so we may want to trim off
1150dd8632a1SPaul Mundt 	 * the excess */
1151dd8632a1SPaul Mundt 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
11528feae131SDavid Howells 		while (total > point) {
11538feae131SDavid Howells 			order = ilog2(total - point);
11548feae131SDavid Howells 			n = 1 << order;
11558feae131SDavid Howells 			kdebug("shave %lu/%lu @%lu", n, total - point, total);
115633e5d769SDavid Howells 			atomic_long_sub(n, &mmap_pages_allocated);
11578feae131SDavid Howells 			total -= n;
11588feae131SDavid Howells 			set_page_refcounted(pages + total);
11598feae131SDavid Howells 			__free_pages(pages + total, order);
11608feae131SDavid Howells 		}
1161dd8632a1SPaul Mundt 	}
11628feae131SDavid Howells 
11638feae131SDavid Howells 	for (point = 1; point < total; point++)
11648feae131SDavid Howells 		set_page_refcounted(&pages[point]);
11658feae131SDavid Howells 
11668feae131SDavid Howells 	base = page_address(pages);
11678feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
11688feae131SDavid Howells 	region->vm_start = (unsigned long) base;
11698feae131SDavid Howells 	region->vm_end   = region->vm_start + rlen;
1170dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
11718feae131SDavid Howells 
11728feae131SDavid Howells 	vma->vm_start = region->vm_start;
11738feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	if (vma->vm_file) {
11761da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
11771da177e4SLinus Torvalds 		mm_segment_t old_fs;
11781da177e4SLinus Torvalds 		loff_t fpos;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
11811da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		old_fs = get_fs();
11841da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
11858feae131SDavid Howells 		ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
11861da177e4SLinus Torvalds 		set_fs(old_fs);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		if (ret < 0)
11891da177e4SLinus Torvalds 			goto error_free;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 		/* clear the last little bit */
11928feae131SDavid Howells 		if (ret < rlen)
11938feae131SDavid Howells 			memset(base + ret, 0, rlen - ret);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	return 0;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds error_free:
12008feae131SDavid Howells 	free_page_series(region->vm_start, region->vm_end);
12018feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
12028feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1203dd8632a1SPaul Mundt 	region->vm_top   = 0;
12041da177e4SLinus Torvalds 	return ret;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds enomem:
120705ae6fa3SGreg Ungerer 	printk("Allocation of length %lu from process %d (%s) failed\n",
120805ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
12091da177e4SLinus Torvalds 	show_free_areas();
12101da177e4SLinus Torvalds 	return -ENOMEM;
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds /*
12141da177e4SLinus Torvalds  * handle mapping creation for uClinux
12151da177e4SLinus Torvalds  */
12161da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file *file,
12171da177e4SLinus Torvalds 			    unsigned long addr,
12181da177e4SLinus Torvalds 			    unsigned long len,
12191da177e4SLinus Torvalds 			    unsigned long prot,
12201da177e4SLinus Torvalds 			    unsigned long flags,
12211da177e4SLinus Torvalds 			    unsigned long pgoff)
12221da177e4SLinus Torvalds {
12238feae131SDavid Howells 	struct vm_area_struct *vma;
12248feae131SDavid Howells 	struct vm_region *region;
12251da177e4SLinus Torvalds 	struct rb_node *rb;
12268feae131SDavid Howells 	unsigned long capabilities, vm_flags, result;
12271da177e4SLinus Torvalds 	int ret;
12281da177e4SLinus Torvalds 
12298feae131SDavid Howells 	kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
12308feae131SDavid Howells 
12311da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
12321da177e4SLinus Torvalds 	 * mapping */
12331da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
12341da177e4SLinus Torvalds 				    &capabilities);
12358feae131SDavid Howells 	if (ret < 0) {
12368feae131SDavid Howells 		kleave(" = %d [val]", ret);
12371da177e4SLinus Torvalds 		return ret;
12388feae131SDavid Howells 	}
12391da177e4SLinus Torvalds 
124006aab5a3SDavid Howells 	/* we ignore the address hint */
124106aab5a3SDavid Howells 	addr = 0;
124206aab5a3SDavid Howells 
12431da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
12441da177e4SLinus Torvalds 	 * now know into VMA flags */
12451da177e4SLinus Torvalds 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
12461da177e4SLinus Torvalds 
12478feae131SDavid Howells 	/* we're going to need to record the mapping */
12488feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
12498feae131SDavid Howells 	if (!region)
12508feae131SDavid Howells 		goto error_getting_region;
12511da177e4SLinus Torvalds 
12528feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
12538feae131SDavid Howells 	if (!vma)
12548feae131SDavid Howells 		goto error_getting_vma;
12551da177e4SLinus Torvalds 
12561e2ae599SDavid Howells 	region->vm_usage = 1;
12578feae131SDavid Howells 	region->vm_flags = vm_flags;
12588feae131SDavid Howells 	region->vm_pgoff = pgoff;
12598feae131SDavid Howells 
12605beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
12618feae131SDavid Howells 	vma->vm_flags = vm_flags;
12628feae131SDavid Howells 	vma->vm_pgoff = pgoff;
12638feae131SDavid Howells 
12648feae131SDavid Howells 	if (file) {
12658feae131SDavid Howells 		region->vm_file = file;
12668feae131SDavid Howells 		get_file(file);
12678feae131SDavid Howells 		vma->vm_file = file;
12688feae131SDavid Howells 		get_file(file);
12698feae131SDavid Howells 		if (vm_flags & VM_EXECUTABLE) {
12708feae131SDavid Howells 			added_exe_file_vma(current->mm);
12718feae131SDavid Howells 			vma->vm_mm = current->mm;
12728feae131SDavid Howells 		}
12738feae131SDavid Howells 	}
12748feae131SDavid Howells 
12758feae131SDavid Howells 	down_write(&nommu_region_sem);
12768feae131SDavid Howells 
12778feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
12781da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
12798feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
12801da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
12811da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
12821da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
12831da177e4SLinus Torvalds 	 *   than here
12841da177e4SLinus Torvalds 	 */
12851da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
12868feae131SDavid Howells 		struct vm_region *pregion;
12878feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
12881da177e4SLinus Torvalds 
12898feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
12908feae131SDavid Howells 		pgend = pgoff + pglen;
1291165b2392SDavid Howells 
12928feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
12938feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
12941da177e4SLinus Torvalds 
12958feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
12961da177e4SLinus Torvalds 				continue;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
12998feae131SDavid Howells 			if (pregion->vm_file->f_path.dentry->d_inode !=
13008feae131SDavid Howells 			    file->f_path.dentry->d_inode)
13011da177e4SLinus Torvalds 				continue;
13021da177e4SLinus Torvalds 
13038feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
13041da177e4SLinus Torvalds 				continue;
13051da177e4SLinus Torvalds 
13068feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
13078feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
13088feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
13098feae131SDavid Howells 			if (pgoff >= rpgend)
13101da177e4SLinus Torvalds 				continue;
13111da177e4SLinus Torvalds 
13128feae131SDavid Howells 			/* handle inexactly overlapping matches between
13138feae131SDavid Howells 			 * mappings */
13148feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
13158feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
13168feae131SDavid Howells 				/* new mapping is not a subset of the region */
13171da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_DIRECT))
13181da177e4SLinus Torvalds 					goto sharing_violation;
13191da177e4SLinus Torvalds 				continue;
13201da177e4SLinus Torvalds 			}
13211da177e4SLinus Torvalds 
13228feae131SDavid Howells 			/* we've found a region we can share */
13231e2ae599SDavid Howells 			pregion->vm_usage++;
13248feae131SDavid Howells 			vma->vm_region = pregion;
13258feae131SDavid Howells 			start = pregion->vm_start;
13268feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
13278feae131SDavid Howells 			vma->vm_start = start;
13288feae131SDavid Howells 			vma->vm_end = start + len;
13291da177e4SLinus Torvalds 
13308feae131SDavid Howells 			if (pregion->vm_flags & VM_MAPPED_COPY) {
13318feae131SDavid Howells 				kdebug("share copy");
13328feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
13338feae131SDavid Howells 			} else {
13348feae131SDavid Howells 				kdebug("share mmap");
13358feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
13368feae131SDavid Howells 				if (ret < 0) {
13378feae131SDavid Howells 					vma->vm_region = NULL;
13388feae131SDavid Howells 					vma->vm_start = 0;
13398feae131SDavid Howells 					vma->vm_end = 0;
13401e2ae599SDavid Howells 					pregion->vm_usage--;
13418feae131SDavid Howells 					pregion = NULL;
13428feae131SDavid Howells 					goto error_just_free;
13431da177e4SLinus Torvalds 				}
13448feae131SDavid Howells 			}
13458feae131SDavid Howells 			fput(region->vm_file);
13468feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
13478feae131SDavid Howells 			region = pregion;
13488feae131SDavid Howells 			result = start;
13498feae131SDavid Howells 			goto share;
13508feae131SDavid Howells 		}
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
13531da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
13541da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
13551da177e4SLinus Torvalds 		 */
1356645d83c5SDavid Howells 		if (capabilities & BDI_CAP_MAP_DIRECT) {
13571da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
13581da177e4SLinus Torvalds 							     pgoff, flags);
13591da177e4SLinus Torvalds 			if (IS_ERR((void *) addr)) {
13601da177e4SLinus Torvalds 				ret = addr;
13611da177e4SLinus Torvalds 				if (ret != (unsigned long) -ENOSYS)
13628feae131SDavid Howells 					goto error_just_free;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
13651da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
13661da177e4SLinus Torvalds 				 * it */
13671da177e4SLinus Torvalds 				ret = (unsigned long) -ENODEV;
13681da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_COPY))
13698feae131SDavid Howells 					goto error_just_free;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
13728feae131SDavid Howells 			} else {
13738feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
13748feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
13751da177e4SLinus Torvalds 			}
13761da177e4SLinus Torvalds 		}
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13798feae131SDavid Howells 	vma->vm_region = region;
13801da177e4SLinus Torvalds 
1381645d83c5SDavid Howells 	/* set up the mapping
1382645d83c5SDavid Howells 	 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1383645d83c5SDavid Howells 	 */
13841da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
13858feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
13861da177e4SLinus Torvalds 	else
1387645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
13881da177e4SLinus Torvalds 	if (ret < 0)
1389645d83c5SDavid Howells 		goto error_just_free;
1390645d83c5SDavid Howells 	add_nommu_region(region);
13918feae131SDavid Howells 
1392ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1393ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1394ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1395ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1396ea637639SJie Zhang 
13971da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
13988feae131SDavid Howells 	result = vma->vm_start;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
14011da177e4SLinus Torvalds 
14028feae131SDavid Howells share:
14038feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
14041da177e4SLinus Torvalds 
1405cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1406cfe79c00SMike Frysinger 	 * mapping of it is made  */
1407cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1408cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1409cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1410cfe79c00SMike Frysinger 	}
14111da177e4SLinus Torvalds 
1412cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
14131da177e4SLinus Torvalds 
14148feae131SDavid Howells 	kleave(" = %lx", result);
14158feae131SDavid Howells 	return result;
14161da177e4SLinus Torvalds 
14178feae131SDavid Howells error_just_free:
14188feae131SDavid Howells 	up_write(&nommu_region_sem);
14198feae131SDavid Howells error:
142089a86402SDavid Howells 	if (region->vm_file)
14218feae131SDavid Howells 		fput(region->vm_file);
14228feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
142389a86402SDavid Howells 	if (vma->vm_file)
14248feae131SDavid Howells 		fput(vma->vm_file);
14258feae131SDavid Howells 	if (vma->vm_flags & VM_EXECUTABLE)
14268feae131SDavid Howells 		removed_exe_file_vma(vma->vm_mm);
14278feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
14288feae131SDavid Howells 	kleave(" = %d", ret);
14291da177e4SLinus Torvalds 	return ret;
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds sharing_violation:
14328feae131SDavid Howells 	up_write(&nommu_region_sem);
14338feae131SDavid Howells 	printk(KERN_WARNING "Attempt to share mismatched mappings\n");
14348feae131SDavid Howells 	ret = -EINVAL;
14358feae131SDavid Howells 	goto error;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds error_getting_vma:
14388feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
14398feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
14408feae131SDavid Howells 	       " from process %d failed\n",
14411da177e4SLinus Torvalds 	       len, current->pid);
14421da177e4SLinus Torvalds 	show_free_areas();
14431da177e4SLinus Torvalds 	return -ENOMEM;
14441da177e4SLinus Torvalds 
14458feae131SDavid Howells error_getting_region:
14468feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
14478feae131SDavid Howells 	       " from process %d failed\n",
14481da177e4SLinus Torvalds 	       len, current->pid);
14491da177e4SLinus Torvalds 	show_free_areas();
14501da177e4SLinus Torvalds 	return -ENOMEM;
14511da177e4SLinus Torvalds }
1452b5073173SPaul Mundt EXPORT_SYMBOL(do_mmap_pgoff);
14531da177e4SLinus Torvalds 
145466f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
145566f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
145666f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
145766f0dc48SHugh Dickins {
145866f0dc48SHugh Dickins 	struct file *file = NULL;
145966f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
146066f0dc48SHugh Dickins 
146166f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
146266f0dc48SHugh Dickins 		file = fget(fd);
146366f0dc48SHugh Dickins 		if (!file)
146466f0dc48SHugh Dickins 			goto out;
146566f0dc48SHugh Dickins 	}
146666f0dc48SHugh Dickins 
146766f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
146866f0dc48SHugh Dickins 
146966f0dc48SHugh Dickins 	down_write(&current->mm->mmap_sem);
147066f0dc48SHugh Dickins 	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
147166f0dc48SHugh Dickins 	up_write(&current->mm->mmap_sem);
147266f0dc48SHugh Dickins 
147366f0dc48SHugh Dickins 	if (file)
147466f0dc48SHugh Dickins 		fput(file);
147566f0dc48SHugh Dickins out:
147666f0dc48SHugh Dickins 	return retval;
147766f0dc48SHugh Dickins }
147866f0dc48SHugh Dickins 
1479a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1480a4679373SChristoph Hellwig struct mmap_arg_struct {
1481a4679373SChristoph Hellwig 	unsigned long addr;
1482a4679373SChristoph Hellwig 	unsigned long len;
1483a4679373SChristoph Hellwig 	unsigned long prot;
1484a4679373SChristoph Hellwig 	unsigned long flags;
1485a4679373SChristoph Hellwig 	unsigned long fd;
1486a4679373SChristoph Hellwig 	unsigned long offset;
1487a4679373SChristoph Hellwig };
1488a4679373SChristoph Hellwig 
1489a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1490a4679373SChristoph Hellwig {
1491a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1492a4679373SChristoph Hellwig 
1493a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1494a4679373SChristoph Hellwig 		return -EFAULT;
1495a4679373SChristoph Hellwig 	if (a.offset & ~PAGE_MASK)
1496a4679373SChristoph Hellwig 		return -EINVAL;
1497a4679373SChristoph Hellwig 
1498a4679373SChristoph Hellwig 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1499a4679373SChristoph Hellwig 			      a.offset >> PAGE_SHIFT);
1500a4679373SChristoph Hellwig }
1501a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1502a4679373SChristoph Hellwig 
15031da177e4SLinus Torvalds /*
15048feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
15058feae131SDavid Howells  * for the first part or the tail.
15061da177e4SLinus Torvalds  */
15078feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
15088feae131SDavid Howells 	      unsigned long addr, int new_below)
15091da177e4SLinus Torvalds {
15108feae131SDavid Howells 	struct vm_area_struct *new;
15118feae131SDavid Howells 	struct vm_region *region;
15128feae131SDavid Howells 	unsigned long npages;
15131da177e4SLinus Torvalds 
15148feae131SDavid Howells 	kenter("");
15151da177e4SLinus Torvalds 
1516779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1517779c1023SDavid Howells 	 * only a single usage on the region) */
1518779c1023SDavid Howells 	if (vma->vm_file)
15198feae131SDavid Howells 		return -ENOMEM;
15201da177e4SLinus Torvalds 
15218feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
15228feae131SDavid Howells 		return -ENOMEM;
15231da177e4SLinus Torvalds 
15248feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
15258feae131SDavid Howells 	if (!region)
15268feae131SDavid Howells 		return -ENOMEM;
15278feae131SDavid Howells 
15288feae131SDavid Howells 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
15298feae131SDavid Howells 	if (!new) {
15308feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
15318feae131SDavid Howells 		return -ENOMEM;
15321da177e4SLinus Torvalds 	}
15331da177e4SLinus Torvalds 
15348feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
15358feae131SDavid Howells 	*new = *vma;
15368feae131SDavid Howells 	*region = *vma->vm_region;
15378feae131SDavid Howells 	new->vm_region = region;
15388feae131SDavid Howells 
15398feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
15408feae131SDavid Howells 
15418feae131SDavid Howells 	if (new_below) {
1542dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
15438feae131SDavid Howells 	} else {
15448feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
15458feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
15461da177e4SLinus Torvalds 	}
15478feae131SDavid Howells 
15488feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
15498feae131SDavid Howells 		new->vm_ops->open(new);
15508feae131SDavid Howells 
15518feae131SDavid Howells 	delete_vma_from_mm(vma);
15528feae131SDavid Howells 	down_write(&nommu_region_sem);
15538feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
15548feae131SDavid Howells 	if (new_below) {
15558feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
15568feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
15578feae131SDavid Howells 	} else {
15588feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1559dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
15608feae131SDavid Howells 	}
15618feae131SDavid Howells 	add_nommu_region(vma->vm_region);
15628feae131SDavid Howells 	add_nommu_region(new->vm_region);
15638feae131SDavid Howells 	up_write(&nommu_region_sem);
15648feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15658feae131SDavid Howells 	add_vma_to_mm(mm, new);
15668feae131SDavid Howells 	return 0;
15678feae131SDavid Howells }
15688feae131SDavid Howells 
15698feae131SDavid Howells /*
15708feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
15718feae131SDavid Howells  * the end
15728feae131SDavid Howells  */
15738feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
15748feae131SDavid Howells 		      struct vm_area_struct *vma,
15758feae131SDavid Howells 		      unsigned long from, unsigned long to)
15768feae131SDavid Howells {
15778feae131SDavid Howells 	struct vm_region *region;
15788feae131SDavid Howells 
15798feae131SDavid Howells 	kenter("");
15808feae131SDavid Howells 
15818feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
15828feae131SDavid Howells 	 * and list */
15838feae131SDavid Howells 	delete_vma_from_mm(vma);
15848feae131SDavid Howells 	if (from > vma->vm_start)
15858feae131SDavid Howells 		vma->vm_end = from;
15868feae131SDavid Howells 	else
15878feae131SDavid Howells 		vma->vm_start = to;
15888feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15898feae131SDavid Howells 
15908feae131SDavid Howells 	/* cut the backing region down to size */
15918feae131SDavid Howells 	region = vma->vm_region;
15921e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
15938feae131SDavid Howells 
15948feae131SDavid Howells 	down_write(&nommu_region_sem);
15958feae131SDavid Howells 	delete_nommu_region(region);
1596dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1597dd8632a1SPaul Mundt 		to = region->vm_top;
1598dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1599dd8632a1SPaul Mundt 	} else {
16008feae131SDavid Howells 		region->vm_start = to;
1601dd8632a1SPaul Mundt 	}
16028feae131SDavid Howells 	add_nommu_region(region);
16038feae131SDavid Howells 	up_write(&nommu_region_sem);
16048feae131SDavid Howells 
16058feae131SDavid Howells 	free_page_series(from, to);
16068feae131SDavid Howells 	return 0;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
16093034097aSDavid Howells /*
16103034097aSDavid Howells  * release a mapping
16118feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
16128feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
16133034097aSDavid Howells  */
16148feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
16151da177e4SLinus Torvalds {
16168feae131SDavid Howells 	struct vm_area_struct *vma;
16178feae131SDavid Howells 	struct rb_node *rb;
16188feae131SDavid Howells 	unsigned long end = start + len;
16198feae131SDavid Howells 	int ret;
16201da177e4SLinus Torvalds 
16218feae131SDavid Howells 	kenter(",%lx,%zx", start, len);
16221da177e4SLinus Torvalds 
16238feae131SDavid Howells 	if (len == 0)
16241da177e4SLinus Torvalds 		return -EINVAL;
16251da177e4SLinus Torvalds 
16268feae131SDavid Howells 	/* find the first potentially overlapping VMA */
16278feae131SDavid Howells 	vma = find_vma(mm, start);
16288feae131SDavid Howells 	if (!vma) {
162933e5d769SDavid Howells 		static int limit = 0;
163033e5d769SDavid Howells 		if (limit < 5) {
16318feae131SDavid Howells 			printk(KERN_WARNING
163233e5d769SDavid Howells 			       "munmap of memory not mmapped by process %d"
163333e5d769SDavid Howells 			       " (%s): 0x%lx-0x%lx\n",
163433e5d769SDavid Howells 			       current->pid, current->comm,
163533e5d769SDavid Howells 			       start, start + len - 1);
163633e5d769SDavid Howells 			limit++;
163733e5d769SDavid Howells 		}
16388feae131SDavid Howells 		return -EINVAL;
16398feae131SDavid Howells 	}
16401da177e4SLinus Torvalds 
16418feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
16428feae131SDavid Howells 	if (vma->vm_file) {
16438feae131SDavid Howells 		do {
16448feae131SDavid Howells 			if (start > vma->vm_start) {
16458feae131SDavid Howells 				kleave(" = -EINVAL [miss]");
16468feae131SDavid Howells 				return -EINVAL;
16478feae131SDavid Howells 			}
16488feae131SDavid Howells 			if (end == vma->vm_end)
16498feae131SDavid Howells 				goto erase_whole_vma;
16508feae131SDavid Howells 			rb = rb_next(&vma->vm_rb);
16518feae131SDavid Howells 			vma = rb_entry(rb, struct vm_area_struct, vm_rb);
16528feae131SDavid Howells 		} while (rb);
16538feae131SDavid Howells 		kleave(" = -EINVAL [split file]");
16548feae131SDavid Howells 		return -EINVAL;
16558feae131SDavid Howells 	} else {
16568feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
16578feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
16588feae131SDavid Howells 			goto erase_whole_vma;
16598feae131SDavid Howells 		if (start < vma->vm_start || end > vma->vm_end) {
16608feae131SDavid Howells 			kleave(" = -EINVAL [superset]");
16618feae131SDavid Howells 			return -EINVAL;
16628feae131SDavid Howells 		}
16638feae131SDavid Howells 		if (start & ~PAGE_MASK) {
16648feae131SDavid Howells 			kleave(" = -EINVAL [unaligned start]");
16658feae131SDavid Howells 			return -EINVAL;
16668feae131SDavid Howells 		}
16678feae131SDavid Howells 		if (end != vma->vm_end && end & ~PAGE_MASK) {
16688feae131SDavid Howells 			kleave(" = -EINVAL [unaligned split]");
16698feae131SDavid Howells 			return -EINVAL;
16708feae131SDavid Howells 		}
16718feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
16728feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
16738feae131SDavid Howells 			if (ret < 0) {
16748feae131SDavid Howells 				kleave(" = %d [split]", ret);
16758feae131SDavid Howells 				return ret;
16768feae131SDavid Howells 			}
16778feae131SDavid Howells 		}
16788feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
16798feae131SDavid Howells 	}
16801da177e4SLinus Torvalds 
16818feae131SDavid Howells erase_whole_vma:
16828feae131SDavid Howells 	delete_vma_from_mm(vma);
16838feae131SDavid Howells 	delete_vma(mm, vma);
16848feae131SDavid Howells 	kleave(" = 0");
16851da177e4SLinus Torvalds 	return 0;
16861da177e4SLinus Torvalds }
1687b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
16881da177e4SLinus Torvalds 
16896a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
16903034097aSDavid Howells {
16913034097aSDavid Howells 	int ret;
16923034097aSDavid Howells 	struct mm_struct *mm = current->mm;
16933034097aSDavid Howells 
16943034097aSDavid Howells 	down_write(&mm->mmap_sem);
16953034097aSDavid Howells 	ret = do_munmap(mm, addr, len);
16963034097aSDavid Howells 	up_write(&mm->mmap_sem);
16973034097aSDavid Howells 	return ret;
16983034097aSDavid Howells }
16993034097aSDavid Howells 
17003034097aSDavid Howells /*
17018feae131SDavid Howells  * release all the mappings made in a process's VM space
17023034097aSDavid Howells  */
17031da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
17041da177e4SLinus Torvalds {
17058feae131SDavid Howells 	struct vm_area_struct *vma;
17061da177e4SLinus Torvalds 
17078feae131SDavid Howells 	if (!mm)
17088feae131SDavid Howells 		return;
17098feae131SDavid Howells 
17108feae131SDavid Howells 	kenter("");
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	mm->total_vm = 0;
17131da177e4SLinus Torvalds 
17148feae131SDavid Howells 	while ((vma = mm->mmap)) {
17158feae131SDavid Howells 		mm->mmap = vma->vm_next;
17168feae131SDavid Howells 		delete_vma_from_mm(vma);
17178feae131SDavid Howells 		delete_vma(mm, vma);
17181da177e4SLinus Torvalds 	}
17191da177e4SLinus Torvalds 
17208feae131SDavid Howells 	kleave("");
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len)
17241da177e4SLinus Torvalds {
17251da177e4SLinus Torvalds 	return -ENOMEM;
17261da177e4SLinus Torvalds }
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds /*
17296fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
17306fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
17311da177e4SLinus Torvalds  *
17326fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
17338feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
17348feae131SDavid Howells  * block is not shareable
17351da177e4SLinus Torvalds  *
17366fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
17371da177e4SLinus Torvalds  */
17381da177e4SLinus Torvalds unsigned long do_mremap(unsigned long addr,
17391da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
17401da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
17411da177e4SLinus Torvalds {
17426fa5f80bSDavid Howells 	struct vm_area_struct *vma;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	/* insanity checks first */
17458feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
17461da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17471da177e4SLinus Torvalds 
17488feae131SDavid Howells 	if (addr & ~PAGE_MASK)
17498feae131SDavid Howells 		return -EINVAL;
17508feae131SDavid Howells 
17511da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
17521da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17531da177e4SLinus Torvalds 
17548feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
17556fa5f80bSDavid Howells 	if (!vma)
17561da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17571da177e4SLinus Torvalds 
17586fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
17591da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
17601da177e4SLinus Torvalds 
17616fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
17621da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
17631da177e4SLinus Torvalds 
17648feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
17651da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	/* all checks complete - do it */
17686fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
17696fa5f80bSDavid Howells 	return vma->vm_start;
17706fa5f80bSDavid Howells }
1771b5073173SPaul Mundt EXPORT_SYMBOL(do_mremap);
17726fa5f80bSDavid Howells 
17736a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
17746a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
17756a6160a7SHeiko Carstens 		unsigned long, new_addr)
17766fa5f80bSDavid Howells {
17776fa5f80bSDavid Howells 	unsigned long ret;
17786fa5f80bSDavid Howells 
17796fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
17806fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
17816fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
17826fa5f80bSDavid Howells 	return ret;
17831da177e4SLinus Torvalds }
17841da177e4SLinus Torvalds 
17856aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1786deceb6cdSHugh Dickins 			unsigned int foll_flags)
17871da177e4SLinus Torvalds {
17881da177e4SLinus Torvalds 	return NULL;
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
17921da177e4SLinus Torvalds 		unsigned long to, unsigned long size, pgprot_t prot)
17931da177e4SLinus Torvalds {
179466aa2b4bSGreg Ungerer 	vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
179566aa2b4bSGreg Ungerer 	return 0;
17961da177e4SLinus Torvalds }
179722c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
17981da177e4SLinus Torvalds 
1799f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1800f905bc44SPaul Mundt 			unsigned long pgoff)
1801f905bc44SPaul Mundt {
1802f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1803f905bc44SPaul Mundt 
1804f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1805f905bc44SPaul Mundt 		return -EINVAL;
1806f905bc44SPaul Mundt 
1807f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1808f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1809f905bc44SPaul Mundt 
1810f905bc44SPaul Mundt 	return 0;
1811f905bc44SPaul Mundt }
1812f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1813f905bc44SPaul Mundt 
18141da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
18191da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
18201da177e4SLinus Torvalds {
18211da177e4SLinus Torvalds 	return -ENOMEM;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
18241363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
18251da177e4SLinus Torvalds {
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
18291da177e4SLinus Torvalds 			 loff_t const holebegin, loff_t const holelen,
18301da177e4SLinus Torvalds 			 int even_cows)
18311da177e4SLinus Torvalds {
18321da177e4SLinus Torvalds }
183322c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range);
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /*
18361da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
18371da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
18381da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
18391da177e4SLinus Torvalds  *
18401da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
18411da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
18421da177e4SLinus Torvalds  *
18431da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
18441da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
18451da177e4SLinus Torvalds  *
18461da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
18471da177e4SLinus Torvalds  *
18481da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
18491da177e4SLinus Torvalds  * wish to use this logic.
18501da177e4SLinus Torvalds  */
185134b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds 	unsigned long free, allowed;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	vm_acct_memory(pages);
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	/*
18581da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
18591da177e4SLinus Torvalds 	 */
18601da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
18611da177e4SLinus Torvalds 		return 0;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
18641da177e4SLinus Torvalds 		unsigned long n;
18651da177e4SLinus Torvalds 
1866347ce434SChristoph Lameter 		free = global_page_state(NR_FILE_PAGES);
18671da177e4SLinus Torvalds 		free += nr_swap_pages;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 		/*
18701da177e4SLinus Torvalds 		 * Any slabs which are created with the
18711da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
18721da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
18731da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
18741da177e4SLinus Torvalds 		 */
1875972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 		/*
18781da177e4SLinus Torvalds 		 * Leave the last 3% for root
18791da177e4SLinus Torvalds 		 */
18801da177e4SLinus Torvalds 		if (!cap_sys_admin)
18811da177e4SLinus Torvalds 			free -= free / 32;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 		if (free > pages)
18841da177e4SLinus Torvalds 			return 0;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 		/*
18871da177e4SLinus Torvalds 		 * nr_free_pages() is very expensive on large systems,
18881da177e4SLinus Torvalds 		 * only call if we're about to fail.
18891da177e4SLinus Torvalds 		 */
18901da177e4SLinus Torvalds 		n = nr_free_pages();
1891d5ddc79bSHideo AOKI 
1892d5ddc79bSHideo AOKI 		/*
1893d5ddc79bSHideo AOKI 		 * Leave reserved pages. The pages are not for anonymous pages.
1894d5ddc79bSHideo AOKI 		 */
1895d5ddc79bSHideo AOKI 		if (n <= totalreserve_pages)
1896d5ddc79bSHideo AOKI 			goto error;
1897d5ddc79bSHideo AOKI 		else
1898d5ddc79bSHideo AOKI 			n -= totalreserve_pages;
1899d5ddc79bSHideo AOKI 
1900d5ddc79bSHideo AOKI 		/*
1901d5ddc79bSHideo AOKI 		 * Leave the last 3% for root
1902d5ddc79bSHideo AOKI 		 */
19031da177e4SLinus Torvalds 		if (!cap_sys_admin)
19041da177e4SLinus Torvalds 			n -= n / 32;
19051da177e4SLinus Torvalds 		free += n;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 		if (free > pages)
19081da177e4SLinus Torvalds 			return 0;
1909d5ddc79bSHideo AOKI 
1910d5ddc79bSHideo AOKI 		goto error;
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	allowed = totalram_pages * sysctl_overcommit_ratio / 100;
19141da177e4SLinus Torvalds 	/*
19151da177e4SLinus Torvalds 	 * Leave the last 3% for root
19161da177e4SLinus Torvalds 	 */
19171da177e4SLinus Torvalds 	if (!cap_sys_admin)
19181da177e4SLinus Torvalds 		allowed -= allowed / 32;
19191da177e4SLinus Torvalds 	allowed += total_swap_pages;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
19221da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
1923731572d3SAlan Cox 	if (mm)
1924731572d3SAlan Cox 		allowed -= mm->total_vm / 32;
19251da177e4SLinus Torvalds 
192600a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
19271da177e4SLinus Torvalds 		return 0;
192800a62ce9SKOSAKI Motohiro 
1929d5ddc79bSHideo AOKI error:
19301da177e4SLinus Torvalds 	vm_unacct_memory(pages);
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	return -ENOMEM;
19331da177e4SLinus Torvalds }
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr)
19361da177e4SLinus Torvalds {
19371da177e4SLinus Torvalds 	return 0;
19381da177e4SLinus Torvalds }
1939b0e15190SDavid Howells 
1940d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1941b0e15190SDavid Howells {
1942b0e15190SDavid Howells 	BUG();
1943d0217ac0SNick Piggin 	return 0;
1944b0e15190SDavid Howells }
1945b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
19460ec76a11SDavid Howells 
19470ec76a11SDavid Howells /*
19480ec76a11SDavid Howells  * Access another process' address space.
19490ec76a11SDavid Howells  * - source/target buffer must be kernel space
19500ec76a11SDavid Howells  */
19510ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
19520ec76a11SDavid Howells {
19530ec76a11SDavid Howells 	struct vm_area_struct *vma;
19540ec76a11SDavid Howells 	struct mm_struct *mm;
19550ec76a11SDavid Howells 
19560ec76a11SDavid Howells 	if (addr + len < addr)
19570ec76a11SDavid Howells 		return 0;
19580ec76a11SDavid Howells 
19590ec76a11SDavid Howells 	mm = get_task_mm(tsk);
19600ec76a11SDavid Howells 	if (!mm)
19610ec76a11SDavid Howells 		return 0;
19620ec76a11SDavid Howells 
19630ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
19640ec76a11SDavid Howells 
19650ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
19660159b141SDavid Howells 	vma = find_vma(mm, addr);
19670159b141SDavid Howells 	if (vma) {
19680ec76a11SDavid Howells 		/* don't overrun this mapping */
19690ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
19700ec76a11SDavid Howells 			len = vma->vm_end - addr;
19710ec76a11SDavid Howells 
19720ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1973d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
19747959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
19757959722bSJie Zhang 					 (void *) addr, buf, len);
1976d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
19777959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
19787959722bSJie Zhang 					    buf, (void *) addr, len);
19790ec76a11SDavid Howells 		else
19800ec76a11SDavid Howells 			len = 0;
19810ec76a11SDavid Howells 	} else {
19820ec76a11SDavid Howells 		len = 0;
19830ec76a11SDavid Howells 	}
19840ec76a11SDavid Howells 
19850ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
19860ec76a11SDavid Howells 	mmput(mm);
19870ec76a11SDavid Howells 	return len;
19880ec76a11SDavid Howells }
19897e660872SDavid Howells 
19907e660872SDavid Howells /**
19917e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
19927e660872SDavid Howells  * @inode: The inode to check
19937e660872SDavid Howells  * @size: The current filesize of the inode
19947e660872SDavid Howells  * @newsize: The proposed filesize of the inode
19957e660872SDavid Howells  *
19967e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
19977e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
19987e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
19997e660872SDavid Howells  * automatically grant mappings that are too large.
20007e660872SDavid Howells  */
20017e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
20027e660872SDavid Howells 				size_t newsize)
20037e660872SDavid Howells {
20047e660872SDavid Howells 	struct vm_area_struct *vma;
20057e660872SDavid Howells 	struct prio_tree_iter iter;
20067e660872SDavid Howells 	struct vm_region *region;
20077e660872SDavid Howells 	pgoff_t low, high;
20087e660872SDavid Howells 	size_t r_size, r_top;
20097e660872SDavid Howells 
20107e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
20117e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
20127e660872SDavid Howells 
20137e660872SDavid Howells 	down_write(&nommu_region_sem);
20147e660872SDavid Howells 
20157e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
20167e660872SDavid Howells 	vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
20177e660872SDavid Howells 			      low, high) {
20187e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
20197e660872SDavid Howells 		 * cache */
20207e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
20217e660872SDavid Howells 			up_write(&nommu_region_sem);
20227e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
20237e660872SDavid Howells 		}
20247e660872SDavid Howells 	}
20257e660872SDavid Howells 
20267e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
20277e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
20287e660872SDavid Howells 	 *
20297e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
20307e660872SDavid Howells 	 * shouldn't be any
20317e660872SDavid Howells 	 */
20327e660872SDavid Howells 	vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
20337e660872SDavid Howells 			      0, ULONG_MAX) {
20347e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
20357e660872SDavid Howells 			continue;
20367e660872SDavid Howells 
20377e660872SDavid Howells 		region = vma->vm_region;
20387e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
20397e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
20407e660872SDavid Howells 
20417e660872SDavid Howells 		if (r_top > newsize) {
20427e660872SDavid Howells 			region->vm_top -= r_top - newsize;
20437e660872SDavid Howells 			if (region->vm_end > region->vm_top)
20447e660872SDavid Howells 				region->vm_end = region->vm_top;
20457e660872SDavid Howells 		}
20467e660872SDavid Howells 	}
20477e660872SDavid Howells 
20487e660872SDavid Howells 	up_write(&nommu_region_sem);
20497e660872SDavid Howells 	return 0;
20507e660872SDavid Howells }
2051