xref: /linux/mm/nommu.c (revision cb0942b81249798e15c3f04eee2946ef543e8115)
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>
1329c185e5SPaul Mundt  *  Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
16b95f1b31SPaul Gortmaker #include <linux/export.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>
251da177e4SLinus Torvalds #include <linux/blkdev.h>
261da177e4SLinus Torvalds #include <linux/backing-dev.h>
271da177e4SLinus Torvalds #include <linux/mount.h>
281da177e4SLinus Torvalds #include <linux/personality.h>
291da177e4SLinus Torvalds #include <linux/security.h>
301da177e4SLinus Torvalds #include <linux/syscalls.h>
31120a795dSAl Viro #include <linux/audit.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,
12953a7706dSMichel Lespinasse 		     struct page **pages, struct vm_area_struct **vmas,
13053a7706dSMichel Lespinasse 		     int *retry)
1311da177e4SLinus Torvalds {
132910e46daSSonic Zhang 	struct vm_area_struct *vma;
1337b4d5b8bSDavid Howells 	unsigned long vm_flags;
1347b4d5b8bSDavid Howells 	int i;
1357b4d5b8bSDavid Howells 
1367b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
13758fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1387b4d5b8bSDavid Howells 	 */
13958fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
14058fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
14158fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
14258fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1431da177e4SLinus Torvalds 
1449d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
1457561e8caSDavid Howells 		vma = find_vma(mm, start);
146910e46daSSonic Zhang 		if (!vma)
1477b4d5b8bSDavid Howells 			goto finish_or_fault;
1487b4d5b8bSDavid Howells 
1497b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1501c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1511c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1527b4d5b8bSDavid Howells 			goto finish_or_fault;
153910e46daSSonic Zhang 
1541da177e4SLinus Torvalds 		if (pages) {
1551da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1561da177e4SLinus Torvalds 			if (pages[i])
1571da177e4SLinus Torvalds 				page_cache_get(pages[i]);
1581da177e4SLinus Torvalds 		}
1591da177e4SLinus Torvalds 		if (vmas)
160910e46daSSonic Zhang 			vmas[i] = vma;
161e1ee65d8SDavid Howells 		start = (start + PAGE_SIZE) & PAGE_MASK;
1621da177e4SLinus Torvalds 	}
1637b4d5b8bSDavid Howells 
1647b4d5b8bSDavid Howells 	return i;
1657b4d5b8bSDavid Howells 
1667b4d5b8bSDavid Howells finish_or_fault:
1677b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1681da177e4SLinus Torvalds }
169b291f000SNick Piggin 
170b291f000SNick Piggin /*
171b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
172b291f000SNick Piggin  * and indicate the VMA that covers each page
173b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
174b291f000SNick Piggin  *   slab page or a secondary page from a compound page
175b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
176b291f000SNick Piggin  */
177b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1789d73777eSPeter Zijlstra 	unsigned long start, int nr_pages, int write, int force,
179b291f000SNick Piggin 	struct page **pages, struct vm_area_struct **vmas)
180b291f000SNick Piggin {
181b291f000SNick Piggin 	int flags = 0;
182b291f000SNick Piggin 
183b291f000SNick Piggin 	if (write)
18458fa879eSHugh Dickins 		flags |= FOLL_WRITE;
185b291f000SNick Piggin 	if (force)
18658fa879eSHugh Dickins 		flags |= FOLL_FORCE;
187b291f000SNick Piggin 
18853a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
18953a7706dSMichel Lespinasse 				NULL);
190b291f000SNick Piggin }
19166aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages);
19266aa2b4bSGreg Ungerer 
193dfc2f91aSPaul Mundt /**
194dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
195dfc2f91aSPaul Mundt  * @vma: memory mapping
196dfc2f91aSPaul Mundt  * @address: user virtual address
197dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
198dfc2f91aSPaul Mundt  *
199dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
200dfc2f91aSPaul Mundt  *
201dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
202dfc2f91aSPaul Mundt  */
203dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
204dfc2f91aSPaul Mundt 	unsigned long *pfn)
205dfc2f91aSPaul Mundt {
206dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
207dfc2f91aSPaul Mundt 		return -EINVAL;
208dfc2f91aSPaul Mundt 
209dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
210dfc2f91aSPaul Mundt 	return 0;
211dfc2f91aSPaul Mundt }
212dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
213dfc2f91aSPaul Mundt 
2141da177e4SLinus Torvalds DEFINE_RWLOCK(vmlist_lock);
2151da177e4SLinus Torvalds struct vm_struct *vmlist;
2161da177e4SLinus Torvalds 
217b3bdda02SChristoph Lameter void vfree(const void *addr)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	kfree(addr);
2201da177e4SLinus Torvalds }
221b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2221da177e4SLinus Torvalds 
223dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	/*
2268518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2278518609dSRobert P. J. Day 	 * returns only a logical address.
2281da177e4SLinus Torvalds 	 */
22984097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2301da177e4SLinus Torvalds }
231b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2321da177e4SLinus Torvalds 
233f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
234f905bc44SPaul Mundt {
235f905bc44SPaul Mundt 	void *ret;
236f905bc44SPaul Mundt 
237f905bc44SPaul Mundt 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
238f905bc44SPaul Mundt 			PAGE_KERNEL);
239f905bc44SPaul Mundt 	if (ret) {
240f905bc44SPaul Mundt 		struct vm_area_struct *vma;
241f905bc44SPaul Mundt 
242f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
243f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
244f905bc44SPaul Mundt 		if (vma)
245f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
246f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
247f905bc44SPaul Mundt 	}
248f905bc44SPaul Mundt 
249f905bc44SPaul Mundt 	return ret;
250f905bc44SPaul Mundt }
251f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
252f905bc44SPaul Mundt 
253b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	return virt_to_page(addr);
2561da177e4SLinus Torvalds }
257b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
2581da177e4SLinus Torvalds 
259b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2621da177e4SLinus Torvalds }
263b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2681da177e4SLinus Torvalds 	return count;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	/* Don't allow overflow */
2741da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2751da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	memcpy(addr, buf, count);
2781da177e4SLinus Torvalds 	return(count);
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /*
2821da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually continguos memory
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  *	@size:		allocation size
2851da177e4SLinus Torvalds  *
2861da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
2871da177e4SLinus Torvalds  *	allocator and map them into continguos kernel virtual space.
2881da177e4SLinus Torvalds  *
289c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2901da177e4SLinus Torvalds  *	use __vmalloc() instead.
2911da177e4SLinus Torvalds  */
2921da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
2951da177e4SLinus Torvalds }
296f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
297f6138882SAndrew Morton 
298e1ca7788SDave Young /*
299e1ca7788SDave Young  *	vzalloc - allocate virtually continguos memory with zero fill
300e1ca7788SDave Young  *
301e1ca7788SDave Young  *	@size:		allocation size
302e1ca7788SDave Young  *
303e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
304e1ca7788SDave Young  *	allocator and map them into continguos kernel virtual space.
305e1ca7788SDave Young  *	The memory allocated is set to zero.
306e1ca7788SDave Young  *
307e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
308e1ca7788SDave Young  *	use __vmalloc() instead.
309e1ca7788SDave Young  */
310e1ca7788SDave Young void *vzalloc(unsigned long size)
311e1ca7788SDave Young {
312e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
313e1ca7788SDave Young 			PAGE_KERNEL);
314e1ca7788SDave Young }
315e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
316e1ca7788SDave Young 
317e1ca7788SDave Young /**
318e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
319e1ca7788SDave Young  * @size:	allocation size
320e1ca7788SDave Young  * @node:	numa node
321e1ca7788SDave Young  *
322e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
323e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
324e1ca7788SDave Young  *
325e1ca7788SDave Young  * For tight control over page level allocator and protection flags
326e1ca7788SDave Young  * use __vmalloc() instead.
327e1ca7788SDave Young  */
328f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
329f6138882SAndrew Morton {
330f6138882SAndrew Morton 	return vmalloc(size);
331f6138882SAndrew Morton }
3329a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
333e1ca7788SDave Young 
334e1ca7788SDave Young /**
335e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
336e1ca7788SDave Young  * @size:	allocation size
337e1ca7788SDave Young  * @node:	numa node
338e1ca7788SDave Young  *
339e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
340e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
341e1ca7788SDave Young  * The memory allocated is set to zero.
342e1ca7788SDave Young  *
343e1ca7788SDave Young  * For tight control over page level allocator and protection flags
344e1ca7788SDave Young  * use __vmalloc() instead.
345e1ca7788SDave Young  */
346e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
347e1ca7788SDave Young {
348e1ca7788SDave Young 	return vzalloc(size);
349e1ca7788SDave Young }
350e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3511da177e4SLinus Torvalds 
3521af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
3531af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
3541af446edSPaul Mundt #endif
3551af446edSPaul Mundt 
3561af446edSPaul Mundt /**
3571af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
3581af446edSPaul Mundt  *	@size:		allocation size
3591af446edSPaul Mundt  *
3601af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
3611af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
3621af446edSPaul Mundt  *	executable kernel virtual space.
3631af446edSPaul Mundt  *
3641af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3651af446edSPaul Mundt  *	use __vmalloc() instead.
3661af446edSPaul Mundt  */
3671af446edSPaul Mundt 
3681af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3691af446edSPaul Mundt {
3701af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3711af446edSPaul Mundt }
3721af446edSPaul Mundt 
373b5073173SPaul Mundt /**
374b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3751da177e4SLinus Torvalds  *	@size:		allocation size
3761da177e4SLinus Torvalds  *
3771da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
3781da177e4SLinus Torvalds  *	page level allocator and map them into continguos kernel virtual space.
3791da177e4SLinus Torvalds  */
3801da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
3831da177e4SLinus Torvalds }
384b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
385b5073173SPaul Mundt 
386b5073173SPaul Mundt /**
387b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
388b5073173SPaul Mundt  *	@size:		allocation size
389b5073173SPaul Mundt  *
390b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
391b5073173SPaul Mundt  * mapped to userspace without leaking data.
392f905bc44SPaul Mundt  *
393f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
394f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
395b5073173SPaul Mundt  */
396b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
397b5073173SPaul Mundt {
398f905bc44SPaul Mundt 	/*
399f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
400f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
401f905bc44SPaul Mundt 	 */
402f905bc44SPaul Mundt 	return vmalloc_user(size);
403b5073173SPaul Mundt }
404b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	BUG();
4091da177e4SLinus Torvalds 	return NULL;
4101da177e4SLinus Torvalds }
411b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
4121da177e4SLinus Torvalds 
413b3bdda02SChristoph Lameter void vunmap(const void *addr)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	BUG();
4161da177e4SLinus Torvalds }
417b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
4181da177e4SLinus Torvalds 
419eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
420eb6434d9SPaul Mundt {
421eb6434d9SPaul Mundt 	BUG();
422eb6434d9SPaul Mundt 	return NULL;
423eb6434d9SPaul Mundt }
424eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
425eb6434d9SPaul Mundt 
426eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
427eb6434d9SPaul Mundt {
428eb6434d9SPaul Mundt 	BUG();
429eb6434d9SPaul Mundt }
430eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
431eb6434d9SPaul Mundt 
432eb6434d9SPaul Mundt void vm_unmap_aliases(void)
433eb6434d9SPaul Mundt {
434eb6434d9SPaul Mundt }
435eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
436eb6434d9SPaul Mundt 
4371da177e4SLinus Torvalds /*
4381eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
4391eeb66a1SChristoph Hellwig  * have one.
4401eeb66a1SChristoph Hellwig  */
4411eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
4421eeb66a1SChristoph Hellwig {
4431eeb66a1SChristoph Hellwig }
4441eeb66a1SChristoph Hellwig 
44529c185e5SPaul Mundt /**
44629c185e5SPaul Mundt  *	alloc_vm_area - allocate a range of kernel address space
44729c185e5SPaul Mundt  *	@size:		size of the area
44829c185e5SPaul Mundt  *
44929c185e5SPaul Mundt  *	Returns:	NULL on failure, vm_struct on success
45029c185e5SPaul Mundt  *
45129c185e5SPaul Mundt  *	This function reserves a range of kernel address space, and
45229c185e5SPaul Mundt  *	allocates pagetables to map that range.  No actual mappings
45329c185e5SPaul Mundt  *	are created.  If the kernel address space is not shared
45429c185e5SPaul Mundt  *	between processes, it syncs the pagetable across all
45529c185e5SPaul Mundt  *	processes.
45629c185e5SPaul Mundt  */
457cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
45829c185e5SPaul Mundt {
45929c185e5SPaul Mundt 	BUG();
46029c185e5SPaul Mundt 	return NULL;
46129c185e5SPaul Mundt }
46229c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
46329c185e5SPaul Mundt 
46429c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
46529c185e5SPaul Mundt {
46629c185e5SPaul Mundt 	BUG();
46729c185e5SPaul Mundt }
46829c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
46929c185e5SPaul Mundt 
470b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
471b5073173SPaul Mundt 		   struct page *page)
472b5073173SPaul Mundt {
473b5073173SPaul Mundt 	return -EINVAL;
474b5073173SPaul Mundt }
475b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
476b5073173SPaul Mundt 
4771eeb66a1SChristoph Hellwig /*
4781da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4791da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4801da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4811da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4821da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4831da177e4SLinus Torvalds  */
4846a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
4891da177e4SLinus Torvalds 		return mm->brk;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (mm->brk == brk)
4921da177e4SLinus Torvalds 		return mm->brk;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	/*
4951da177e4SLinus Torvalds 	 * Always allow shrinking brk
4961da177e4SLinus Torvalds 	 */
4971da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4981da177e4SLinus Torvalds 		mm->brk = brk;
4991da177e4SLinus Torvalds 		return brk;
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/*
5031da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
5041da177e4SLinus Torvalds 	 */
505cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
5061da177e4SLinus Torvalds 	return mm->brk = brk;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
5098feae131SDavid Howells /*
5108feae131SDavid Howells  * initialise the VMA and region record slabs
5118feae131SDavid Howells  */
5128feae131SDavid Howells void __init mmap_init(void)
5131da177e4SLinus Torvalds {
51400a62ce9SKOSAKI Motohiro 	int ret;
51500a62ce9SKOSAKI Motohiro 
51600a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
51700a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
51833e5d769SDavid Howells 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
5198feae131SDavid Howells }
5201da177e4SLinus Torvalds 
5218feae131SDavid Howells /*
5228feae131SDavid Howells  * validate the region tree
5238feae131SDavid Howells  * - the caller must hold the region lock
5248feae131SDavid Howells  */
5258feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
5268feae131SDavid Howells static noinline void validate_nommu_regions(void)
5278feae131SDavid Howells {
5288feae131SDavid Howells 	struct vm_region *region, *last;
5298feae131SDavid Howells 	struct rb_node *p, *lastp;
5301da177e4SLinus Torvalds 
5318feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
5328feae131SDavid Howells 	if (!lastp)
5338feae131SDavid Howells 		return;
5348feae131SDavid Howells 
5358feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
53633e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_end <= last->vm_start));
53733e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_top < last->vm_end));
5388feae131SDavid Howells 
5398feae131SDavid Howells 	while ((p = rb_next(lastp))) {
5408feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
5418feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
5428feae131SDavid Howells 
54333e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_end <= region->vm_start));
54433e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_top < region->vm_end));
54533e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_start < last->vm_top));
5468feae131SDavid Howells 
5478feae131SDavid Howells 		lastp = p;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds }
5508feae131SDavid Howells #else
55133e5d769SDavid Howells static void validate_nommu_regions(void)
55233e5d769SDavid Howells {
55333e5d769SDavid Howells }
5548feae131SDavid Howells #endif
5558feae131SDavid Howells 
5568feae131SDavid Howells /*
5578feae131SDavid Howells  * add a region into the global tree
5588feae131SDavid Howells  */
5598feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
5608feae131SDavid Howells {
5618feae131SDavid Howells 	struct vm_region *pregion;
5628feae131SDavid Howells 	struct rb_node **p, *parent;
5638feae131SDavid Howells 
5648feae131SDavid Howells 	validate_nommu_regions();
5658feae131SDavid Howells 
5668feae131SDavid Howells 	parent = NULL;
5678feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
5688feae131SDavid Howells 	while (*p) {
5698feae131SDavid Howells 		parent = *p;
5708feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5718feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5728feae131SDavid Howells 			p = &(*p)->rb_left;
5738feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5748feae131SDavid Howells 			p = &(*p)->rb_right;
5758feae131SDavid Howells 		else if (pregion == region)
5768feae131SDavid Howells 			return;
5778feae131SDavid Howells 		else
5788feae131SDavid Howells 			BUG();
5798feae131SDavid Howells 	}
5808feae131SDavid Howells 
5818feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5828feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5838feae131SDavid Howells 
5848feae131SDavid Howells 	validate_nommu_regions();
5858feae131SDavid Howells }
5868feae131SDavid Howells 
5878feae131SDavid Howells /*
5888feae131SDavid Howells  * delete a region from the global tree
5898feae131SDavid Howells  */
5908feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
5918feae131SDavid Howells {
5928feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5938feae131SDavid Howells 
5948feae131SDavid Howells 	validate_nommu_regions();
5958feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5968feae131SDavid Howells 	validate_nommu_regions();
5978feae131SDavid Howells }
5988feae131SDavid Howells 
5998feae131SDavid Howells /*
6008feae131SDavid Howells  * free a contiguous series of pages
6018feae131SDavid Howells  */
6028feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
6038feae131SDavid Howells {
6048feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
6058feae131SDavid Howells 		struct page *page = virt_to_page(from);
6068feae131SDavid Howells 
6078feae131SDavid Howells 		kdebug("- free %lx", from);
60833e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
6098feae131SDavid Howells 		if (page_count(page) != 1)
61033e5d769SDavid Howells 			kdebug("free page %p: refcount not one: %d",
61133e5d769SDavid Howells 			       page, page_count(page));
6128feae131SDavid Howells 		put_page(page);
6138feae131SDavid Howells 	}
6148feae131SDavid Howells }
6158feae131SDavid Howells 
6168feae131SDavid Howells /*
6178feae131SDavid Howells  * release a reference to a region
61833e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
619dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
6208feae131SDavid Howells  *   will equal vm_start
6218feae131SDavid Howells  */
6228feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
6238feae131SDavid Howells 	__releases(nommu_region_sem)
6248feae131SDavid Howells {
6251e2ae599SDavid Howells 	kenter("%p{%d}", region, region->vm_usage);
6268feae131SDavid Howells 
6278feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6288feae131SDavid Howells 
6291e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
630dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
6318feae131SDavid Howells 			delete_nommu_region(region);
6328feae131SDavid Howells 		up_write(&nommu_region_sem);
6338feae131SDavid Howells 
6348feae131SDavid Howells 		if (region->vm_file)
6358feae131SDavid Howells 			fput(region->vm_file);
6368feae131SDavid Howells 
6378feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
6388feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
6398feae131SDavid Howells 		if (region->vm_flags & VM_MAPPED_COPY) {
6408feae131SDavid Howells 			kdebug("free series");
641dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
6428feae131SDavid Howells 		}
6438feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
6448feae131SDavid Howells 	} else {
6458feae131SDavid Howells 		up_write(&nommu_region_sem);
6468feae131SDavid Howells 	}
6478feae131SDavid Howells }
6488feae131SDavid Howells 
6498feae131SDavid Howells /*
6508feae131SDavid Howells  * release a reference to a region
6518feae131SDavid Howells  */
6528feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
6538feae131SDavid Howells {
6548feae131SDavid Howells 	down_write(&nommu_region_sem);
6558feae131SDavid Howells 	__put_nommu_region(region);
6568feae131SDavid Howells }
6571da177e4SLinus Torvalds 
6583034097aSDavid Howells /*
659eb8cdec4SBernd Schmidt  * update protection on a vma
660eb8cdec4SBernd Schmidt  */
661eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
662eb8cdec4SBernd Schmidt {
663eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
664eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
665eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
666eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
667eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
668eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
669eb8cdec4SBernd Schmidt 	}
670eb8cdec4SBernd Schmidt 	update_protections(mm);
671eb8cdec4SBernd Schmidt #endif
672eb8cdec4SBernd Schmidt }
673eb8cdec4SBernd Schmidt 
674eb8cdec4SBernd Schmidt /*
6753034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
6768feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
6778feae131SDavid Howells  * page
6783034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
6793034097aSDavid Howells  */
6808feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
6813034097aSDavid Howells {
6826038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
6838feae131SDavid Howells 	struct address_space *mapping;
6846038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
6853034097aSDavid Howells 
6868feae131SDavid Howells 	kenter(",%p", vma);
6878feae131SDavid Howells 
6888feae131SDavid Howells 	BUG_ON(!vma->vm_region);
6898feae131SDavid Howells 
6908feae131SDavid Howells 	mm->map_count++;
6918feae131SDavid Howells 	vma->vm_mm = mm;
6928feae131SDavid Howells 
693eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
694eb8cdec4SBernd Schmidt 
6958feae131SDavid Howells 	/* add the VMA to the mapping */
6968feae131SDavid Howells 	if (vma->vm_file) {
6978feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6988feae131SDavid Howells 
699918e556eSDavid Howells 		mutex_lock(&mapping->i_mmap_mutex);
7008feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7018feae131SDavid Howells 		vma_prio_tree_insert(vma, &mapping->i_mmap);
7028feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
703918e556eSDavid Howells 		mutex_unlock(&mapping->i_mmap_mutex);
7048feae131SDavid Howells 	}
7058feae131SDavid Howells 
7068feae131SDavid Howells 	/* add the VMA to the tree */
7076038def0SNamhyung Kim 	parent = rb_prev = NULL;
7088feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
7098feae131SDavid Howells 	while (*p) {
7108feae131SDavid Howells 		parent = *p;
7118feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
7128feae131SDavid Howells 
7138feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
7148feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
7158feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
7168feae131SDavid Howells 			p = &(*p)->rb_left;
7176038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
7186038def0SNamhyung Kim 			rb_prev = parent;
7198feae131SDavid Howells 			p = &(*p)->rb_right;
7206038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
7218feae131SDavid Howells 			p = &(*p)->rb_left;
7226038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
7236038def0SNamhyung Kim 			rb_prev = parent;
7248feae131SDavid Howells 			p = &(*p)->rb_right;
7256038def0SNamhyung Kim 		} else if (vma < pvma)
7268feae131SDavid Howells 			p = &(*p)->rb_left;
7276038def0SNamhyung Kim 		else if (vma > pvma) {
7286038def0SNamhyung Kim 			rb_prev = parent;
7298feae131SDavid Howells 			p = &(*p)->rb_right;
7306038def0SNamhyung Kim 		} else
7318feae131SDavid Howells 			BUG();
7328feae131SDavid Howells 	}
7338feae131SDavid Howells 
7348feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
7358feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
7368feae131SDavid Howells 
7378feae131SDavid Howells 	/* add VMA to the VMA list also */
7386038def0SNamhyung Kim 	prev = NULL;
7396038def0SNamhyung Kim 	if (rb_prev)
7406038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
7413034097aSDavid Howells 
7426038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
7438feae131SDavid Howells }
7448feae131SDavid Howells 
7458feae131SDavid Howells /*
7468feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
7478feae131SDavid Howells  */
7488feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
7498feae131SDavid Howells {
7508feae131SDavid Howells 	struct address_space *mapping;
7518feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
7528feae131SDavid Howells 
7538feae131SDavid Howells 	kenter("%p", vma);
7548feae131SDavid Howells 
755eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
756eb8cdec4SBernd Schmidt 
7578feae131SDavid Howells 	mm->map_count--;
7588feae131SDavid Howells 	if (mm->mmap_cache == vma)
7598feae131SDavid Howells 		mm->mmap_cache = NULL;
7608feae131SDavid Howells 
7618feae131SDavid Howells 	/* remove the VMA from the mapping */
7628feae131SDavid Howells 	if (vma->vm_file) {
7638feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7648feae131SDavid Howells 
765918e556eSDavid Howells 		mutex_lock(&mapping->i_mmap_mutex);
7668feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7678feae131SDavid Howells 		vma_prio_tree_remove(vma, &mapping->i_mmap);
7688feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
769918e556eSDavid Howells 		mutex_unlock(&mapping->i_mmap_mutex);
7708feae131SDavid Howells 	}
7718feae131SDavid Howells 
7728feae131SDavid Howells 	/* remove from the MM's tree and list */
7738feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
774b951bf2cSNamhyung Kim 
775b951bf2cSNamhyung Kim 	if (vma->vm_prev)
776b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
777b951bf2cSNamhyung Kim 	else
778b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
779b951bf2cSNamhyung Kim 
780b951bf2cSNamhyung Kim 	if (vma->vm_next)
781b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
7828feae131SDavid Howells }
7838feae131SDavid Howells 
7848feae131SDavid Howells /*
7858feae131SDavid Howells  * destroy a VMA record
7868feae131SDavid Howells  */
7878feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
7888feae131SDavid Howells {
7898feae131SDavid Howells 	kenter("%p", vma);
7908feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
7918feae131SDavid Howells 		vma->vm_ops->close(vma);
7928feae131SDavid Howells 	if (vma->vm_file) {
7938feae131SDavid Howells 		fput(vma->vm_file);
7948feae131SDavid Howells 		if (vma->vm_flags & VM_EXECUTABLE)
7958feae131SDavid Howells 			removed_exe_file_vma(mm);
7968feae131SDavid Howells 	}
7978feae131SDavid Howells 	put_nommu_region(vma->vm_region);
7988feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
7993034097aSDavid Howells }
8003034097aSDavid Howells 
8013034097aSDavid Howells /*
8023034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
8033034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8043034097aSDavid Howells  */
8053034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
8063034097aSDavid Howells {
8078feae131SDavid Howells 	struct vm_area_struct *vma;
8083034097aSDavid Howells 
8098feae131SDavid Howells 	/* check the cache first */
8108feae131SDavid Howells 	vma = mm->mmap_cache;
8118feae131SDavid Howells 	if (vma && vma->vm_start <= addr && vma->vm_end > addr)
8128feae131SDavid Howells 		return vma;
8138feae131SDavid Howells 
814e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8158feae131SDavid Howells 	 * resides) */
816e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8178feae131SDavid Howells 		if (vma->vm_start > addr)
8188feae131SDavid Howells 			return NULL;
8198feae131SDavid Howells 		if (vma->vm_end > addr) {
8208feae131SDavid Howells 			mm->mmap_cache = vma;
8218feae131SDavid Howells 			return vma;
8223034097aSDavid Howells 		}
8238feae131SDavid Howells 	}
8243034097aSDavid Howells 
8253034097aSDavid Howells 	return NULL;
8263034097aSDavid Howells }
8273034097aSDavid Howells EXPORT_SYMBOL(find_vma);
8283034097aSDavid Howells 
8293034097aSDavid Howells /*
830930e652aSDavid Howells  * find a VMA
831930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
832930e652aSDavid Howells  */
833930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
834930e652aSDavid Howells {
8357561e8caSDavid Howells 	return find_vma(mm, addr);
836930e652aSDavid Howells }
837930e652aSDavid Howells 
8388feae131SDavid Howells /*
8398feae131SDavid Howells  * expand a stack to a given address
8408feae131SDavid Howells  * - not supported under NOMMU conditions
8418feae131SDavid Howells  */
84257c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
84357c8f63eSGreg Ungerer {
84457c8f63eSGreg Ungerer 	return -ENOMEM;
84557c8f63eSGreg Ungerer }
84657c8f63eSGreg Ungerer 
847930e652aSDavid Howells /*
8486fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
8496fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8506fa5f80bSDavid Howells  */
8518feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
8528feae131SDavid Howells 					     unsigned long addr,
8538feae131SDavid Howells 					     unsigned long len)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	struct vm_area_struct *vma;
8568feae131SDavid Howells 	unsigned long end = addr + len;
8571da177e4SLinus Torvalds 
8588feae131SDavid Howells 	/* check the cache first */
8598feae131SDavid Howells 	vma = mm->mmap_cache;
8608feae131SDavid Howells 	if (vma && vma->vm_start == addr && vma->vm_end == end)
8611da177e4SLinus Torvalds 		return vma;
8628feae131SDavid Howells 
863e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8648feae131SDavid Howells 	 * resides) */
865e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8668feae131SDavid Howells 		if (vma->vm_start < addr)
8678feae131SDavid Howells 			continue;
8688feae131SDavid Howells 		if (vma->vm_start > addr)
8698feae131SDavid Howells 			return NULL;
8708feae131SDavid Howells 		if (vma->vm_end == end) {
8718feae131SDavid Howells 			mm->mmap_cache = vma;
8728feae131SDavid Howells 			return vma;
8738feae131SDavid Howells 		}
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	return NULL;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8793034097aSDavid Howells /*
8801da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
8811da177e4SLinus Torvalds  * mapping we're capable of supporting
8821da177e4SLinus Torvalds  */
8831da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
8841da177e4SLinus Torvalds 				 unsigned long addr,
8851da177e4SLinus Torvalds 				 unsigned long len,
8861da177e4SLinus Torvalds 				 unsigned long prot,
8871da177e4SLinus Torvalds 				 unsigned long flags,
8881da177e4SLinus Torvalds 				 unsigned long pgoff,
8891da177e4SLinus Torvalds 				 unsigned long *_capabilities)
8901da177e4SLinus Torvalds {
8918feae131SDavid Howells 	unsigned long capabilities, rlen;
8921da177e4SLinus Torvalds 	int ret;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	/* do the simple checks first */
89506aab5a3SDavid Howells 	if (flags & MAP_FIXED) {
8961da177e4SLinus Torvalds 		printk(KERN_DEBUG
8971da177e4SLinus Torvalds 		       "%d: Can't do fixed-address/overlay mmap of RAM\n",
8981da177e4SLinus Torvalds 		       current->pid);
8991da177e4SLinus Torvalds 		return -EINVAL;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
9031da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
9041da177e4SLinus Torvalds 		return -EINVAL;
9051da177e4SLinus Torvalds 
906f81cff0dSMike Frysinger 	if (!len)
9071da177e4SLinus Torvalds 		return -EINVAL;
9081da177e4SLinus Torvalds 
909f81cff0dSMike Frysinger 	/* Careful about overflows.. */
9108feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
9118feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
912f81cff0dSMike Frysinger 		return -ENOMEM;
913f81cff0dSMike Frysinger 
9141da177e4SLinus Torvalds 	/* offset overflow? */
9158feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
916f81cff0dSMike Frysinger 		return -EOVERFLOW;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	if (file) {
9191da177e4SLinus Torvalds 		/* validate file mapping requests */
9201da177e4SLinus Torvalds 		struct address_space *mapping;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		/* files must support mmap */
9231da177e4SLinus Torvalds 		if (!file->f_op || !file->f_op->mmap)
9241da177e4SLinus Torvalds 			return -ENODEV;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
9271da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
9281da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
9291da177e4SLinus Torvalds 		 */
9301da177e4SLinus Torvalds 		mapping = file->f_mapping;
9311da177e4SLinus Torvalds 		if (!mapping)
932e9536ae7SJosef Sipek 			mapping = file->f_path.dentry->d_inode->i_mapping;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 		capabilities = 0;
9351da177e4SLinus Torvalds 		if (mapping && mapping->backing_dev_info)
9361da177e4SLinus Torvalds 			capabilities = mapping->backing_dev_info->capabilities;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 		if (!capabilities) {
9391da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
9401da177e4SLinus Torvalds 			 * defaults */
941e9536ae7SJosef Sipek 			switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
9421da177e4SLinus Torvalds 			case S_IFREG:
9431da177e4SLinus Torvalds 			case S_IFBLK:
9441da177e4SLinus Torvalds 				capabilities = BDI_CAP_MAP_COPY;
9451da177e4SLinus Torvalds 				break;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 			case S_IFCHR:
9481da177e4SLinus Torvalds 				capabilities =
9491da177e4SLinus Torvalds 					BDI_CAP_MAP_DIRECT |
9501da177e4SLinus Torvalds 					BDI_CAP_READ_MAP |
9511da177e4SLinus Torvalds 					BDI_CAP_WRITE_MAP;
9521da177e4SLinus Torvalds 				break;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 			default:
9551da177e4SLinus Torvalds 				return -EINVAL;
9561da177e4SLinus Torvalds 			}
9571da177e4SLinus Torvalds 		}
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
9601da177e4SLinus Torvalds 		 * device */
9611da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
9621da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
9631da177e4SLinus Torvalds 		if (!file->f_op->read)
9641da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
9651da177e4SLinus Torvalds 
96628d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
96728d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
96828d7a6aeSGraff Yang 			return -EACCES;
96928d7a6aeSGraff Yang 
9701da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
9711da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
9721da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
9731da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
9741da177e4SLinus Torvalds 				return -EACCES;
9751da177e4SLinus Torvalds 
976e9536ae7SJosef Sipek 			if (IS_APPEND(file->f_path.dentry->d_inode) &&
9771da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
9781da177e4SLinus Torvalds 				return -EACCES;
9791da177e4SLinus Torvalds 
980e9536ae7SJosef Sipek 			if (locks_verify_locked(file->f_path.dentry->d_inode))
9811da177e4SLinus Torvalds 				return -EAGAIN;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_DIRECT))
9841da177e4SLinus Torvalds 				return -ENODEV;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
9871da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
9881da177e4SLinus Torvalds 		}
9891da177e4SLinus Torvalds 		else {
9901da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
9911da177e4SLinus Torvalds 			 * allocate */
9921da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_COPY))
9931da177e4SLinus Torvalds 				return -ENODEV;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
9961da177e4SLinus Torvalds 			 * shared with the backing device */
9971da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
9981da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 
10013c7b2045SBernd Schmidt 		if (capabilities & BDI_CAP_MAP_DIRECT) {
10023c7b2045SBernd Schmidt 			if (((prot & PROT_READ)  && !(capabilities & BDI_CAP_READ_MAP))  ||
10033c7b2045SBernd Schmidt 			    ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
10043c7b2045SBernd Schmidt 			    ((prot & PROT_EXEC)  && !(capabilities & BDI_CAP_EXEC_MAP))
10053c7b2045SBernd Schmidt 			    ) {
10063c7b2045SBernd Schmidt 				capabilities &= ~BDI_CAP_MAP_DIRECT;
10073c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
10083c7b2045SBernd Schmidt 					printk(KERN_WARNING
10093c7b2045SBernd Schmidt 					       "MAP_SHARED not completely supported on !MMU\n");
10103c7b2045SBernd Schmidt 					return -EINVAL;
10113c7b2045SBernd Schmidt 				}
10123c7b2045SBernd Schmidt 			}
10133c7b2045SBernd Schmidt 		}
10143c7b2045SBernd Schmidt 
10151da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
10161da177e4SLinus Torvalds 		 * mappings */
1017e9536ae7SJosef Sipek 		if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
10181da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
10191da177e4SLinus Torvalds 				return -EPERM;
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 		else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
10221da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
10231da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
10241da177e4SLinus Torvalds 				if (capabilities & BDI_CAP_EXEC_MAP)
10251da177e4SLinus Torvalds 					prot |= PROT_EXEC;
10261da177e4SLinus Torvalds 			}
10271da177e4SLinus Torvalds 		}
10281da177e4SLinus Torvalds 		else if ((prot & PROT_READ) &&
10291da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
10301da177e4SLinus Torvalds 			 !(capabilities & BDI_CAP_EXEC_MAP)
10311da177e4SLinus Torvalds 			 ) {
10321da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
10331da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
10341da177e4SLinus Torvalds 		}
10351da177e4SLinus Torvalds 	}
10361da177e4SLinus Torvalds 	else {
10371da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
10381da177e4SLinus Torvalds 		 * privately mapped
10391da177e4SLinus Torvalds 		 */
10401da177e4SLinus Torvalds 		capabilities = BDI_CAP_MAP_COPY;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
10431da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
10441da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
10451da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	/* allow the security API to have its say */
1049e5467859SAl Viro 	ret = security_mmap_addr(addr);
1050e5467859SAl Viro 	if (ret < 0)
1051e5467859SAl Viro 		return ret;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	/* looks okay */
10541da177e4SLinus Torvalds 	*_capabilities = capabilities;
10551da177e4SLinus Torvalds 	return 0;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /*
10591da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
10601da177e4SLinus Torvalds  * now know into VMA flags
10611da177e4SLinus Torvalds  */
10621da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
10631da177e4SLinus Torvalds 					unsigned long prot,
10641da177e4SLinus Torvalds 					unsigned long flags,
10651da177e4SLinus Torvalds 					unsigned long capabilities)
10661da177e4SLinus Torvalds {
10671da177e4SLinus Torvalds 	unsigned long vm_flags;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
10701da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
10731da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
10743c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10751da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
10761da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10773c7b2045SBernd Schmidt 	} else {
10781da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
10791da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
10801da177e4SLinus Torvalds 		 * romfs/cramfs */
10813c7b2045SBernd Schmidt 		vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
10821da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
10833c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
10871da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
10881da177e4SLinus Torvalds 	 * with another untraced process
10891da177e4SLinus Torvalds 	 */
1090a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
10911da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	return vm_flags;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds /*
10978feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
10988feae131SDavid Howells  * pins the storage)
10991da177e4SLinus Torvalds  */
11008feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
11011da177e4SLinus Torvalds {
11021da177e4SLinus Torvalds 	int ret;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1105dd8632a1SPaul Mundt 	if (ret == 0) {
1106dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1107645d83c5SDavid Howells 		return 0;
1108dd8632a1SPaul Mundt 	}
11091da177e4SLinus Torvalds 	if (ret != -ENOSYS)
11101da177e4SLinus Torvalds 		return ret;
11111da177e4SLinus Torvalds 
11123fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
11133fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
11143fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
11151da177e4SLinus Torvalds 	return -ENODEV;
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds /*
11191da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
11201da177e4SLinus Torvalds  */
11218feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
11228feae131SDavid Howells 			   struct vm_region *region,
1123645d83c5SDavid Howells 			   unsigned long len,
1124645d83c5SDavid Howells 			   unsigned long capabilities)
11251da177e4SLinus Torvalds {
11268feae131SDavid Howells 	struct page *pages;
1127f67d9b15SBob Liu 	unsigned long total, point, n;
11281da177e4SLinus Torvalds 	void *base;
11298feae131SDavid Howells 	int ret, order;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
11321da177e4SLinus Torvalds 	 * shared mappings on devices or memory
11331da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
11341da177e4SLinus Torvalds 	 */
1135645d83c5SDavid Howells 	if (capabilities & BDI_CAP_MAP_DIRECT) {
11361da177e4SLinus Torvalds 		ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1137dd8632a1SPaul Mundt 		if (ret == 0) {
11381da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1139dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1140dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1141645d83c5SDavid Howells 			return 0;
11421da177e4SLinus Torvalds 		}
1143dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1144dd8632a1SPaul Mundt 			return ret;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
11471da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
11481da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
11518feae131SDavid Howells 
11521da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
11531da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
11541da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
11551da177e4SLinus Torvalds 	 */
1156f67d9b15SBob Liu 	order = get_order(len);
11578feae131SDavid Howells 	kdebug("alloc order %d for %lx", order, len);
11588feae131SDavid Howells 
11598feae131SDavid Howells 	pages = alloc_pages(GFP_KERNEL, order);
11608feae131SDavid Howells 	if (!pages)
11611da177e4SLinus Torvalds 		goto enomem;
11621da177e4SLinus Torvalds 
11638feae131SDavid Howells 	total = 1 << order;
116433e5d769SDavid Howells 	atomic_long_add(total, &mmap_pages_allocated);
11651da177e4SLinus Torvalds 
1166f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1167dd8632a1SPaul Mundt 
1168dd8632a1SPaul Mundt 	/* we allocated a power-of-2 sized page set, so we may want to trim off
1169dd8632a1SPaul Mundt 	 * the excess */
1170dd8632a1SPaul Mundt 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
11718feae131SDavid Howells 		while (total > point) {
11728feae131SDavid Howells 			order = ilog2(total - point);
11738feae131SDavid Howells 			n = 1 << order;
11748feae131SDavid Howells 			kdebug("shave %lu/%lu @%lu", n, total - point, total);
117533e5d769SDavid Howells 			atomic_long_sub(n, &mmap_pages_allocated);
11768feae131SDavid Howells 			total -= n;
11778feae131SDavid Howells 			set_page_refcounted(pages + total);
11788feae131SDavid Howells 			__free_pages(pages + total, order);
11798feae131SDavid Howells 		}
1180dd8632a1SPaul Mundt 	}
11818feae131SDavid Howells 
11828feae131SDavid Howells 	for (point = 1; point < total; point++)
11838feae131SDavid Howells 		set_page_refcounted(&pages[point]);
11848feae131SDavid Howells 
11858feae131SDavid Howells 	base = page_address(pages);
11868feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
11878feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1188f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1189dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
11908feae131SDavid Howells 
11918feae131SDavid Howells 	vma->vm_start = region->vm_start;
11928feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	if (vma->vm_file) {
11951da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
11961da177e4SLinus Torvalds 		mm_segment_t old_fs;
11971da177e4SLinus Torvalds 		loff_t fpos;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
12001da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 		old_fs = get_fs();
12031da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
1204f67d9b15SBob Liu 		ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
12051da177e4SLinus Torvalds 		set_fs(old_fs);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 		if (ret < 0)
12081da177e4SLinus Torvalds 			goto error_free;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 		/* clear the last little bit */
1211f67d9b15SBob Liu 		if (ret < len)
1212f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	return 0;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds error_free:
12197223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
12208feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
12218feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1222dd8632a1SPaul Mundt 	region->vm_top   = 0;
12231da177e4SLinus Torvalds 	return ret;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds enomem:
122605ae6fa3SGreg Ungerer 	printk("Allocation of length %lu from process %d (%s) failed\n",
122705ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
12287bf02ea2SDavid Rientjes 	show_free_areas(0);
12291da177e4SLinus Torvalds 	return -ENOMEM;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds /*
12331da177e4SLinus Torvalds  * handle mapping creation for uClinux
12341da177e4SLinus Torvalds  */
1235e3fc629dSAl Viro unsigned long do_mmap_pgoff(struct file *file,
12361da177e4SLinus Torvalds 			    unsigned long addr,
12371da177e4SLinus Torvalds 			    unsigned long len,
12381da177e4SLinus Torvalds 			    unsigned long prot,
12391da177e4SLinus Torvalds 			    unsigned long flags,
12401da177e4SLinus Torvalds 			    unsigned long pgoff)
12411da177e4SLinus Torvalds {
12428feae131SDavid Howells 	struct vm_area_struct *vma;
12438feae131SDavid Howells 	struct vm_region *region;
12441da177e4SLinus Torvalds 	struct rb_node *rb;
12458feae131SDavid Howells 	unsigned long capabilities, vm_flags, result;
12461da177e4SLinus Torvalds 	int ret;
12471da177e4SLinus Torvalds 
12488feae131SDavid Howells 	kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
12498feae131SDavid Howells 
12501da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
12511da177e4SLinus Torvalds 	 * mapping */
12521da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
12531da177e4SLinus Torvalds 				    &capabilities);
12548feae131SDavid Howells 	if (ret < 0) {
12558feae131SDavid Howells 		kleave(" = %d [val]", ret);
12561da177e4SLinus Torvalds 		return ret;
12578feae131SDavid Howells 	}
12581da177e4SLinus Torvalds 
125906aab5a3SDavid Howells 	/* we ignore the address hint */
126006aab5a3SDavid Howells 	addr = 0;
1261f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
126206aab5a3SDavid Howells 
12631da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
12641da177e4SLinus Torvalds 	 * now know into VMA flags */
12651da177e4SLinus Torvalds 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
12661da177e4SLinus Torvalds 
12678feae131SDavid Howells 	/* we're going to need to record the mapping */
12688feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
12698feae131SDavid Howells 	if (!region)
12708feae131SDavid Howells 		goto error_getting_region;
12711da177e4SLinus Torvalds 
12728feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
12738feae131SDavid Howells 	if (!vma)
12748feae131SDavid Howells 		goto error_getting_vma;
12751da177e4SLinus Torvalds 
12761e2ae599SDavid Howells 	region->vm_usage = 1;
12778feae131SDavid Howells 	region->vm_flags = vm_flags;
12788feae131SDavid Howells 	region->vm_pgoff = pgoff;
12798feae131SDavid Howells 
12805beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
12818feae131SDavid Howells 	vma->vm_flags = vm_flags;
12828feae131SDavid Howells 	vma->vm_pgoff = pgoff;
12838feae131SDavid Howells 
12848feae131SDavid Howells 	if (file) {
1285*cb0942b8SAl Viro 		region->vm_file = get_file(file);
1286*cb0942b8SAl Viro 		vma->vm_file = get_file(file);
12878feae131SDavid Howells 		if (vm_flags & VM_EXECUTABLE) {
12888feae131SDavid Howells 			added_exe_file_vma(current->mm);
12898feae131SDavid Howells 			vma->vm_mm = current->mm;
12908feae131SDavid Howells 		}
12918feae131SDavid Howells 	}
12928feae131SDavid Howells 
12938feae131SDavid Howells 	down_write(&nommu_region_sem);
12948feae131SDavid Howells 
12958feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
12961da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
12978feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
12981da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
12991da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
13001da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
13011da177e4SLinus Torvalds 	 *   than here
13021da177e4SLinus Torvalds 	 */
13031da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
13048feae131SDavid Howells 		struct vm_region *pregion;
13058feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
13061da177e4SLinus Torvalds 
13078feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
13088feae131SDavid Howells 		pgend = pgoff + pglen;
1309165b2392SDavid Howells 
13108feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
13118feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
13121da177e4SLinus Torvalds 
13138feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
13141da177e4SLinus Torvalds 				continue;
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
13178feae131SDavid Howells 			if (pregion->vm_file->f_path.dentry->d_inode !=
13188feae131SDavid Howells 			    file->f_path.dentry->d_inode)
13191da177e4SLinus Torvalds 				continue;
13201da177e4SLinus Torvalds 
13218feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
13221da177e4SLinus Torvalds 				continue;
13231da177e4SLinus Torvalds 
13248feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
13258feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
13268feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
13278feae131SDavid Howells 			if (pgoff >= rpgend)
13281da177e4SLinus Torvalds 				continue;
13291da177e4SLinus Torvalds 
13308feae131SDavid Howells 			/* handle inexactly overlapping matches between
13318feae131SDavid Howells 			 * mappings */
13328feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
13338feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
13348feae131SDavid Howells 				/* new mapping is not a subset of the region */
13351da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_DIRECT))
13361da177e4SLinus Torvalds 					goto sharing_violation;
13371da177e4SLinus Torvalds 				continue;
13381da177e4SLinus Torvalds 			}
13391da177e4SLinus Torvalds 
13408feae131SDavid Howells 			/* we've found a region we can share */
13411e2ae599SDavid Howells 			pregion->vm_usage++;
13428feae131SDavid Howells 			vma->vm_region = pregion;
13438feae131SDavid Howells 			start = pregion->vm_start;
13448feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
13458feae131SDavid Howells 			vma->vm_start = start;
13468feae131SDavid Howells 			vma->vm_end = start + len;
13471da177e4SLinus Torvalds 
13488feae131SDavid Howells 			if (pregion->vm_flags & VM_MAPPED_COPY) {
13498feae131SDavid Howells 				kdebug("share copy");
13508feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
13518feae131SDavid Howells 			} else {
13528feae131SDavid Howells 				kdebug("share mmap");
13538feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
13548feae131SDavid Howells 				if (ret < 0) {
13558feae131SDavid Howells 					vma->vm_region = NULL;
13568feae131SDavid Howells 					vma->vm_start = 0;
13578feae131SDavid Howells 					vma->vm_end = 0;
13581e2ae599SDavid Howells 					pregion->vm_usage--;
13598feae131SDavid Howells 					pregion = NULL;
13608feae131SDavid Howells 					goto error_just_free;
13611da177e4SLinus Torvalds 				}
13628feae131SDavid Howells 			}
13638feae131SDavid Howells 			fput(region->vm_file);
13648feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
13658feae131SDavid Howells 			region = pregion;
13668feae131SDavid Howells 			result = start;
13678feae131SDavid Howells 			goto share;
13688feae131SDavid Howells 		}
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
13711da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
13721da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
13731da177e4SLinus Torvalds 		 */
1374645d83c5SDavid Howells 		if (capabilities & BDI_CAP_MAP_DIRECT) {
13751da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
13761da177e4SLinus Torvalds 							     pgoff, flags);
1377bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
13781da177e4SLinus Torvalds 				ret = addr;
1379bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
13808feae131SDavid Howells 					goto error_just_free;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
13831da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
13841da177e4SLinus Torvalds 				 * it */
1385bb005a59SNamhyung Kim 				ret = -ENODEV;
13861da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_COPY))
13878feae131SDavid Howells 					goto error_just_free;
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
13908feae131SDavid Howells 			} else {
13918feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
13928feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
13931da177e4SLinus Torvalds 			}
13941da177e4SLinus Torvalds 		}
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 
13978feae131SDavid Howells 	vma->vm_region = region;
13981da177e4SLinus Torvalds 
1399645d83c5SDavid Howells 	/* set up the mapping
1400645d83c5SDavid Howells 	 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1401645d83c5SDavid Howells 	 */
14021da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
14038feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
14041da177e4SLinus Torvalds 	else
1405645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
14061da177e4SLinus Torvalds 	if (ret < 0)
1407645d83c5SDavid Howells 		goto error_just_free;
1408645d83c5SDavid Howells 	add_nommu_region(region);
14098feae131SDavid Howells 
1410ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1411ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1412ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1413ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1414ea637639SJie Zhang 
14151da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
14168feae131SDavid Howells 	result = vma->vm_start;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
14191da177e4SLinus Torvalds 
14208feae131SDavid Howells share:
14218feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
14221da177e4SLinus Torvalds 
1423cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1424cfe79c00SMike Frysinger 	 * mapping of it is made  */
1425cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1426cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1427cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1428cfe79c00SMike Frysinger 	}
14291da177e4SLinus Torvalds 
1430cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
14311da177e4SLinus Torvalds 
14328feae131SDavid Howells 	kleave(" = %lx", result);
14338feae131SDavid Howells 	return result;
14341da177e4SLinus Torvalds 
14358feae131SDavid Howells error_just_free:
14368feae131SDavid Howells 	up_write(&nommu_region_sem);
14378feae131SDavid Howells error:
143889a86402SDavid Howells 	if (region->vm_file)
14398feae131SDavid Howells 		fput(region->vm_file);
14408feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
144189a86402SDavid Howells 	if (vma->vm_file)
14428feae131SDavid Howells 		fput(vma->vm_file);
14438feae131SDavid Howells 	if (vma->vm_flags & VM_EXECUTABLE)
14448feae131SDavid Howells 		removed_exe_file_vma(vma->vm_mm);
14458feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
14468feae131SDavid Howells 	kleave(" = %d", ret);
14471da177e4SLinus Torvalds 	return ret;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds sharing_violation:
14508feae131SDavid Howells 	up_write(&nommu_region_sem);
14518feae131SDavid Howells 	printk(KERN_WARNING "Attempt to share mismatched mappings\n");
14528feae131SDavid Howells 	ret = -EINVAL;
14538feae131SDavid Howells 	goto error;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds error_getting_vma:
14568feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
14578feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
14588feae131SDavid Howells 	       " from process %d failed\n",
14591da177e4SLinus Torvalds 	       len, current->pid);
14607bf02ea2SDavid Rientjes 	show_free_areas(0);
14611da177e4SLinus Torvalds 	return -ENOMEM;
14621da177e4SLinus Torvalds 
14638feae131SDavid Howells error_getting_region:
14648feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
14658feae131SDavid Howells 	       " from process %d failed\n",
14661da177e4SLinus Torvalds 	       len, current->pid);
14677bf02ea2SDavid Rientjes 	show_free_areas(0);
14681da177e4SLinus Torvalds 	return -ENOMEM;
14691da177e4SLinus Torvalds }
14706be5ceb0SLinus Torvalds 
147166f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
147266f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
147366f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
147466f0dc48SHugh Dickins {
147566f0dc48SHugh Dickins 	struct file *file = NULL;
147666f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
147766f0dc48SHugh Dickins 
1478120a795dSAl Viro 	audit_mmap_fd(fd, flags);
147966f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
148066f0dc48SHugh Dickins 		file = fget(fd);
148166f0dc48SHugh Dickins 		if (!file)
148266f0dc48SHugh Dickins 			goto out;
148366f0dc48SHugh Dickins 	}
148466f0dc48SHugh Dickins 
148566f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
148666f0dc48SHugh Dickins 
1487ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
148866f0dc48SHugh Dickins 
148966f0dc48SHugh Dickins 	if (file)
149066f0dc48SHugh Dickins 		fput(file);
149166f0dc48SHugh Dickins out:
149266f0dc48SHugh Dickins 	return retval;
149366f0dc48SHugh Dickins }
149466f0dc48SHugh Dickins 
1495a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1496a4679373SChristoph Hellwig struct mmap_arg_struct {
1497a4679373SChristoph Hellwig 	unsigned long addr;
1498a4679373SChristoph Hellwig 	unsigned long len;
1499a4679373SChristoph Hellwig 	unsigned long prot;
1500a4679373SChristoph Hellwig 	unsigned long flags;
1501a4679373SChristoph Hellwig 	unsigned long fd;
1502a4679373SChristoph Hellwig 	unsigned long offset;
1503a4679373SChristoph Hellwig };
1504a4679373SChristoph Hellwig 
1505a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1506a4679373SChristoph Hellwig {
1507a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1508a4679373SChristoph Hellwig 
1509a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1510a4679373SChristoph Hellwig 		return -EFAULT;
1511a4679373SChristoph Hellwig 	if (a.offset & ~PAGE_MASK)
1512a4679373SChristoph Hellwig 		return -EINVAL;
1513a4679373SChristoph Hellwig 
1514a4679373SChristoph Hellwig 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1515a4679373SChristoph Hellwig 			      a.offset >> PAGE_SHIFT);
1516a4679373SChristoph Hellwig }
1517a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1518a4679373SChristoph Hellwig 
15191da177e4SLinus Torvalds /*
15208feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
15218feae131SDavid Howells  * for the first part or the tail.
15221da177e4SLinus Torvalds  */
15238feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
15248feae131SDavid Howells 	      unsigned long addr, int new_below)
15251da177e4SLinus Torvalds {
15268feae131SDavid Howells 	struct vm_area_struct *new;
15278feae131SDavid Howells 	struct vm_region *region;
15288feae131SDavid Howells 	unsigned long npages;
15291da177e4SLinus Torvalds 
15308feae131SDavid Howells 	kenter("");
15311da177e4SLinus Torvalds 
1532779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1533779c1023SDavid Howells 	 * only a single usage on the region) */
1534779c1023SDavid Howells 	if (vma->vm_file)
15358feae131SDavid Howells 		return -ENOMEM;
15361da177e4SLinus Torvalds 
15378feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
15388feae131SDavid Howells 		return -ENOMEM;
15391da177e4SLinus Torvalds 
15408feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
15418feae131SDavid Howells 	if (!region)
15428feae131SDavid Howells 		return -ENOMEM;
15438feae131SDavid Howells 
15448feae131SDavid Howells 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
15458feae131SDavid Howells 	if (!new) {
15468feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
15478feae131SDavid Howells 		return -ENOMEM;
15481da177e4SLinus Torvalds 	}
15491da177e4SLinus Torvalds 
15508feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
15518feae131SDavid Howells 	*new = *vma;
15528feae131SDavid Howells 	*region = *vma->vm_region;
15538feae131SDavid Howells 	new->vm_region = region;
15548feae131SDavid Howells 
15558feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
15568feae131SDavid Howells 
15578feae131SDavid Howells 	if (new_below) {
1558dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
15598feae131SDavid Howells 	} else {
15608feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
15618feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
15621da177e4SLinus Torvalds 	}
15638feae131SDavid Howells 
15648feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
15658feae131SDavid Howells 		new->vm_ops->open(new);
15668feae131SDavid Howells 
15678feae131SDavid Howells 	delete_vma_from_mm(vma);
15688feae131SDavid Howells 	down_write(&nommu_region_sem);
15698feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
15708feae131SDavid Howells 	if (new_below) {
15718feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
15728feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
15738feae131SDavid Howells 	} else {
15748feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1575dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
15768feae131SDavid Howells 	}
15778feae131SDavid Howells 	add_nommu_region(vma->vm_region);
15788feae131SDavid Howells 	add_nommu_region(new->vm_region);
15798feae131SDavid Howells 	up_write(&nommu_region_sem);
15808feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15818feae131SDavid Howells 	add_vma_to_mm(mm, new);
15828feae131SDavid Howells 	return 0;
15838feae131SDavid Howells }
15848feae131SDavid Howells 
15858feae131SDavid Howells /*
15868feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
15878feae131SDavid Howells  * the end
15888feae131SDavid Howells  */
15898feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
15908feae131SDavid Howells 		      struct vm_area_struct *vma,
15918feae131SDavid Howells 		      unsigned long from, unsigned long to)
15928feae131SDavid Howells {
15938feae131SDavid Howells 	struct vm_region *region;
15948feae131SDavid Howells 
15958feae131SDavid Howells 	kenter("");
15968feae131SDavid Howells 
15978feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
15988feae131SDavid Howells 	 * and list */
15998feae131SDavid Howells 	delete_vma_from_mm(vma);
16008feae131SDavid Howells 	if (from > vma->vm_start)
16018feae131SDavid Howells 		vma->vm_end = from;
16028feae131SDavid Howells 	else
16038feae131SDavid Howells 		vma->vm_start = to;
16048feae131SDavid Howells 	add_vma_to_mm(mm, vma);
16058feae131SDavid Howells 
16068feae131SDavid Howells 	/* cut the backing region down to size */
16078feae131SDavid Howells 	region = vma->vm_region;
16081e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
16098feae131SDavid Howells 
16108feae131SDavid Howells 	down_write(&nommu_region_sem);
16118feae131SDavid Howells 	delete_nommu_region(region);
1612dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1613dd8632a1SPaul Mundt 		to = region->vm_top;
1614dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1615dd8632a1SPaul Mundt 	} else {
16168feae131SDavid Howells 		region->vm_start = to;
1617dd8632a1SPaul Mundt 	}
16188feae131SDavid Howells 	add_nommu_region(region);
16198feae131SDavid Howells 	up_write(&nommu_region_sem);
16208feae131SDavid Howells 
16218feae131SDavid Howells 	free_page_series(from, to);
16228feae131SDavid Howells 	return 0;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
16253034097aSDavid Howells /*
16263034097aSDavid Howells  * release a mapping
16278feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
16288feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
16293034097aSDavid Howells  */
16308feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
16311da177e4SLinus Torvalds {
16328feae131SDavid Howells 	struct vm_area_struct *vma;
1633f67d9b15SBob Liu 	unsigned long end;
16348feae131SDavid Howells 	int ret;
16351da177e4SLinus Torvalds 
16368feae131SDavid Howells 	kenter(",%lx,%zx", start, len);
16371da177e4SLinus Torvalds 
1638f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
16398feae131SDavid Howells 	if (len == 0)
16401da177e4SLinus Torvalds 		return -EINVAL;
16411da177e4SLinus Torvalds 
1642f67d9b15SBob Liu 	end = start + len;
1643f67d9b15SBob Liu 
16448feae131SDavid Howells 	/* find the first potentially overlapping VMA */
16458feae131SDavid Howells 	vma = find_vma(mm, start);
16468feae131SDavid Howells 	if (!vma) {
164733e5d769SDavid Howells 		static int limit = 0;
164833e5d769SDavid Howells 		if (limit < 5) {
16498feae131SDavid Howells 			printk(KERN_WARNING
165033e5d769SDavid Howells 			       "munmap of memory not mmapped by process %d"
165133e5d769SDavid Howells 			       " (%s): 0x%lx-0x%lx\n",
165233e5d769SDavid Howells 			       current->pid, current->comm,
165333e5d769SDavid Howells 			       start, start + len - 1);
165433e5d769SDavid Howells 			limit++;
165533e5d769SDavid Howells 		}
16568feae131SDavid Howells 		return -EINVAL;
16578feae131SDavid Howells 	}
16581da177e4SLinus Torvalds 
16598feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
16608feae131SDavid Howells 	if (vma->vm_file) {
16618feae131SDavid Howells 		do {
16628feae131SDavid Howells 			if (start > vma->vm_start) {
16638feae131SDavid Howells 				kleave(" = -EINVAL [miss]");
16648feae131SDavid Howells 				return -EINVAL;
16658feae131SDavid Howells 			}
16668feae131SDavid Howells 			if (end == vma->vm_end)
16678feae131SDavid Howells 				goto erase_whole_vma;
1668d75a310cSNamhyung Kim 			vma = vma->vm_next;
1669d75a310cSNamhyung Kim 		} while (vma);
16708feae131SDavid Howells 		kleave(" = -EINVAL [split file]");
16718feae131SDavid Howells 		return -EINVAL;
16728feae131SDavid Howells 	} else {
16738feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
16748feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
16758feae131SDavid Howells 			goto erase_whole_vma;
16768feae131SDavid Howells 		if (start < vma->vm_start || end > vma->vm_end) {
16778feae131SDavid Howells 			kleave(" = -EINVAL [superset]");
16788feae131SDavid Howells 			return -EINVAL;
16798feae131SDavid Howells 		}
16808feae131SDavid Howells 		if (start & ~PAGE_MASK) {
16818feae131SDavid Howells 			kleave(" = -EINVAL [unaligned start]");
16828feae131SDavid Howells 			return -EINVAL;
16838feae131SDavid Howells 		}
16848feae131SDavid Howells 		if (end != vma->vm_end && end & ~PAGE_MASK) {
16858feae131SDavid Howells 			kleave(" = -EINVAL [unaligned split]");
16868feae131SDavid Howells 			return -EINVAL;
16878feae131SDavid Howells 		}
16888feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
16898feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
16908feae131SDavid Howells 			if (ret < 0) {
16918feae131SDavid Howells 				kleave(" = %d [split]", ret);
16928feae131SDavid Howells 				return ret;
16938feae131SDavid Howells 			}
16948feae131SDavid Howells 		}
16958feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
16968feae131SDavid Howells 	}
16971da177e4SLinus Torvalds 
16988feae131SDavid Howells erase_whole_vma:
16998feae131SDavid Howells 	delete_vma_from_mm(vma);
17008feae131SDavid Howells 	delete_vma(mm, vma);
17018feae131SDavid Howells 	kleave(" = 0");
17021da177e4SLinus Torvalds 	return 0;
17031da177e4SLinus Torvalds }
1704b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
17051da177e4SLinus Torvalds 
1706bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
17073034097aSDavid Howells {
1708bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
17093034097aSDavid Howells 	int ret;
17103034097aSDavid Howells 
17113034097aSDavid Howells 	down_write(&mm->mmap_sem);
17123034097aSDavid Howells 	ret = do_munmap(mm, addr, len);
17133034097aSDavid Howells 	up_write(&mm->mmap_sem);
17143034097aSDavid Howells 	return ret;
17153034097aSDavid Howells }
1716a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1717a46ef99dSLinus Torvalds 
1718a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1719a46ef99dSLinus Torvalds {
1720bfce281cSAl Viro 	return vm_munmap(addr, len);
1721a46ef99dSLinus Torvalds }
17223034097aSDavid Howells 
17233034097aSDavid Howells /*
17248feae131SDavid Howells  * release all the mappings made in a process's VM space
17253034097aSDavid Howells  */
17261da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
17271da177e4SLinus Torvalds {
17288feae131SDavid Howells 	struct vm_area_struct *vma;
17291da177e4SLinus Torvalds 
17308feae131SDavid Howells 	if (!mm)
17318feae131SDavid Howells 		return;
17328feae131SDavid Howells 
17338feae131SDavid Howells 	kenter("");
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	mm->total_vm = 0;
17361da177e4SLinus Torvalds 
17378feae131SDavid Howells 	while ((vma = mm->mmap)) {
17388feae131SDavid Howells 		mm->mmap = vma->vm_next;
17398feae131SDavid Howells 		delete_vma_from_mm(vma);
17408feae131SDavid Howells 		delete_vma(mm, vma);
174104c34961SSteven J. Magnani 		cond_resched();
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds 
17448feae131SDavid Howells 	kleave("");
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
1747e4eb1ff6SLinus Torvalds unsigned long vm_brk(unsigned long addr, unsigned long len)
17481da177e4SLinus Torvalds {
17491da177e4SLinus Torvalds 	return -ENOMEM;
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds /*
17536fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
17546fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
17551da177e4SLinus Torvalds  *
17566fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
17578feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
17588feae131SDavid Howells  * block is not shareable
17591da177e4SLinus Torvalds  *
17606fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
17611da177e4SLinus Torvalds  */
17621da177e4SLinus Torvalds unsigned long do_mremap(unsigned long addr,
17631da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
17641da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
17651da177e4SLinus Torvalds {
17666fa5f80bSDavid Howells 	struct vm_area_struct *vma;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	/* insanity checks first */
1769f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1770f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
17718feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
17721da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17731da177e4SLinus Torvalds 
17748feae131SDavid Howells 	if (addr & ~PAGE_MASK)
17758feae131SDavid Howells 		return -EINVAL;
17768feae131SDavid Howells 
17771da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
17781da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17791da177e4SLinus Torvalds 
17808feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
17816fa5f80bSDavid Howells 	if (!vma)
17821da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17831da177e4SLinus Torvalds 
17846fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
17851da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
17861da177e4SLinus Torvalds 
17876fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
17881da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
17891da177e4SLinus Torvalds 
17908feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
17911da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	/* all checks complete - do it */
17946fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
17956fa5f80bSDavid Howells 	return vma->vm_start;
17966fa5f80bSDavid Howells }
1797b5073173SPaul Mundt EXPORT_SYMBOL(do_mremap);
17986fa5f80bSDavid Howells 
17996a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
18006a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
18016a6160a7SHeiko Carstens 		unsigned long, new_addr)
18026fa5f80bSDavid Howells {
18036fa5f80bSDavid Howells 	unsigned long ret;
18046fa5f80bSDavid Howells 
18056fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
18066fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
18076fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
18086fa5f80bSDavid Howells 	return ret;
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
18116aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1812deceb6cdSHugh Dickins 			unsigned int foll_flags)
18131da177e4SLinus Torvalds {
18141da177e4SLinus Torvalds 	return NULL;
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
18178f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
18188f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
18191da177e4SLinus Torvalds {
18208f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
18218f3b1327SBob Liu 		return -EINVAL;
18228f3b1327SBob Liu 
18238f3b1327SBob Liu 	vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
182466aa2b4bSGreg Ungerer 	return 0;
18251da177e4SLinus Torvalds }
182622c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
18271da177e4SLinus Torvalds 
1828f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1829f905bc44SPaul Mundt 			unsigned long pgoff)
1830f905bc44SPaul Mundt {
1831f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1832f905bc44SPaul Mundt 
1833f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1834f905bc44SPaul Mundt 		return -EINVAL;
1835f905bc44SPaul Mundt 
1836f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1837f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1838f905bc44SPaul Mundt 
1839f905bc44SPaul Mundt 	return 0;
1840f905bc44SPaul Mundt }
1841f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1842f905bc44SPaul Mundt 
18431da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
18441da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
18451da177e4SLinus Torvalds {
18461da177e4SLinus Torvalds 	return -ENOMEM;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
18491363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
18541da177e4SLinus Torvalds 			 loff_t const holebegin, loff_t const holelen,
18551da177e4SLinus Torvalds 			 int even_cows)
18561da177e4SLinus Torvalds {
18571da177e4SLinus Torvalds }
185822c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range);
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds /*
18611da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
18621da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
18631da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
18641da177e4SLinus Torvalds  *
18651da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
18661da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
18671da177e4SLinus Torvalds  *
18681da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
18691da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
18701da177e4SLinus Torvalds  *
18711da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
18721da177e4SLinus Torvalds  *
18731da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
18741da177e4SLinus Torvalds  * wish to use this logic.
18751da177e4SLinus Torvalds  */
187634b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
18771da177e4SLinus Torvalds {
18781da177e4SLinus Torvalds 	unsigned long free, allowed;
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	vm_acct_memory(pages);
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	/*
18831da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
18841da177e4SLinus Torvalds 	 */
18851da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
18861da177e4SLinus Torvalds 		return 0;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1889c15bef30SDmitry Fink 		free = global_page_state(NR_FREE_PAGES);
1890c15bef30SDmitry Fink 		free += global_page_state(NR_FILE_PAGES);
18911da177e4SLinus Torvalds 
1892c15bef30SDmitry Fink 		/*
1893c15bef30SDmitry Fink 		 * shmem pages shouldn't be counted as free in this
1894c15bef30SDmitry Fink 		 * case, they can't be purged, only swapped out, and
1895c15bef30SDmitry Fink 		 * that won't affect the overall amount of available
1896c15bef30SDmitry Fink 		 * memory in the system.
1897c15bef30SDmitry Fink 		 */
1898c15bef30SDmitry Fink 		free -= global_page_state(NR_SHMEM);
1899c15bef30SDmitry Fink 
19001da177e4SLinus Torvalds 		free += nr_swap_pages;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 		/*
19031da177e4SLinus Torvalds 		 * Any slabs which are created with the
19041da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
19051da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
19061da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
19071da177e4SLinus Torvalds 		 */
1908972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 		/*
1911c15bef30SDmitry Fink 		 * Leave reserved pages. The pages are not for anonymous pages.
1912c15bef30SDmitry Fink 		 */
1913c15bef30SDmitry Fink 		if (free <= totalreserve_pages)
1914c15bef30SDmitry Fink 			goto error;
1915c15bef30SDmitry Fink 		else
1916c15bef30SDmitry Fink 			free -= totalreserve_pages;
1917c15bef30SDmitry Fink 
1918c15bef30SDmitry Fink 		/*
19191da177e4SLinus Torvalds 		 * Leave the last 3% for root
19201da177e4SLinus Torvalds 		 */
19211da177e4SLinus Torvalds 		if (!cap_sys_admin)
19221da177e4SLinus Torvalds 			free -= free / 32;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 		if (free > pages)
19251da177e4SLinus Torvalds 			return 0;
19261da177e4SLinus Torvalds 
1927d5ddc79bSHideo AOKI 		goto error;
19281da177e4SLinus Torvalds 	}
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	allowed = totalram_pages * sysctl_overcommit_ratio / 100;
19311da177e4SLinus Torvalds 	/*
19321da177e4SLinus Torvalds 	 * Leave the last 3% for root
19331da177e4SLinus Torvalds 	 */
19341da177e4SLinus Torvalds 	if (!cap_sys_admin)
19351da177e4SLinus Torvalds 		allowed -= allowed / 32;
19361da177e4SLinus Torvalds 	allowed += total_swap_pages;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
19391da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
1940731572d3SAlan Cox 	if (mm)
1941731572d3SAlan Cox 		allowed -= mm->total_vm / 32;
19421da177e4SLinus Torvalds 
194300a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
19441da177e4SLinus Torvalds 		return 0;
194500a62ce9SKOSAKI Motohiro 
1946d5ddc79bSHideo AOKI error:
19471da177e4SLinus Torvalds 	vm_unacct_memory(pages);
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	return -ENOMEM;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
1952cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr)
19531da177e4SLinus Torvalds {
19541da177e4SLinus Torvalds 	return 0;
19551da177e4SLinus Torvalds }
1956b0e15190SDavid Howells 
1957d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1958b0e15190SDavid Howells {
1959b0e15190SDavid Howells 	BUG();
1960d0217ac0SNick Piggin 	return 0;
1961b0e15190SDavid Howells }
1962b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
19630ec76a11SDavid Howells 
1964f55f199bSMike Frysinger static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1965f55f199bSMike Frysinger 		unsigned long addr, void *buf, int len, int write)
19660ec76a11SDavid Howells {
19670ec76a11SDavid Howells 	struct vm_area_struct *vma;
19680ec76a11SDavid Howells 
19690ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
19700ec76a11SDavid Howells 
19710ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
19720159b141SDavid Howells 	vma = find_vma(mm, addr);
19730159b141SDavid Howells 	if (vma) {
19740ec76a11SDavid Howells 		/* don't overrun this mapping */
19750ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
19760ec76a11SDavid Howells 			len = vma->vm_end - addr;
19770ec76a11SDavid Howells 
19780ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1979d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
19807959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
19817959722bSJie Zhang 					 (void *) addr, buf, len);
1982d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
19837959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
19847959722bSJie Zhang 					    buf, (void *) addr, len);
19850ec76a11SDavid Howells 		else
19860ec76a11SDavid Howells 			len = 0;
19870ec76a11SDavid Howells 	} else {
19880ec76a11SDavid Howells 		len = 0;
19890ec76a11SDavid Howells 	}
19900ec76a11SDavid Howells 
19910ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
1992f55f199bSMike Frysinger 
1993f55f199bSMike Frysinger 	return len;
1994f55f199bSMike Frysinger }
1995f55f199bSMike Frysinger 
1996f55f199bSMike Frysinger /**
1997f55f199bSMike Frysinger  * @access_remote_vm - access another process' address space
1998f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1999f55f199bSMike Frysinger  * @addr:	start address to access
2000f55f199bSMike Frysinger  * @buf:	source or destination buffer
2001f55f199bSMike Frysinger  * @len:	number of bytes to transfer
2002f55f199bSMike Frysinger  * @write:	whether the access is a write
2003f55f199bSMike Frysinger  *
2004f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
2005f55f199bSMike Frysinger  */
2006f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2007f55f199bSMike Frysinger 		void *buf, int len, int write)
2008f55f199bSMike Frysinger {
2009f55f199bSMike Frysinger 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
2010f55f199bSMike Frysinger }
2011f55f199bSMike Frysinger 
2012f55f199bSMike Frysinger /*
2013f55f199bSMike Frysinger  * Access another process' address space.
2014f55f199bSMike Frysinger  * - source/target buffer must be kernel space
2015f55f199bSMike Frysinger  */
2016f55f199bSMike Frysinger int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2017f55f199bSMike Frysinger {
2018f55f199bSMike Frysinger 	struct mm_struct *mm;
2019f55f199bSMike Frysinger 
2020f55f199bSMike Frysinger 	if (addr + len < addr)
2021f55f199bSMike Frysinger 		return 0;
2022f55f199bSMike Frysinger 
2023f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
2024f55f199bSMike Frysinger 	if (!mm)
2025f55f199bSMike Frysinger 		return 0;
2026f55f199bSMike Frysinger 
2027f55f199bSMike Frysinger 	len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2028f55f199bSMike Frysinger 
20290ec76a11SDavid Howells 	mmput(mm);
20300ec76a11SDavid Howells 	return len;
20310ec76a11SDavid Howells }
20327e660872SDavid Howells 
20337e660872SDavid Howells /**
20347e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
20357e660872SDavid Howells  * @inode: The inode to check
20367e660872SDavid Howells  * @size: The current filesize of the inode
20377e660872SDavid Howells  * @newsize: The proposed filesize of the inode
20387e660872SDavid Howells  *
20397e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
20407e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
20417e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
20427e660872SDavid Howells  * automatically grant mappings that are too large.
20437e660872SDavid Howells  */
20447e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
20457e660872SDavid Howells 				size_t newsize)
20467e660872SDavid Howells {
20477e660872SDavid Howells 	struct vm_area_struct *vma;
20487e660872SDavid Howells 	struct prio_tree_iter iter;
20497e660872SDavid Howells 	struct vm_region *region;
20507e660872SDavid Howells 	pgoff_t low, high;
20517e660872SDavid Howells 	size_t r_size, r_top;
20527e660872SDavid Howells 
20537e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
20547e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
20557e660872SDavid Howells 
20567e660872SDavid Howells 	down_write(&nommu_region_sem);
2057918e556eSDavid Howells 	mutex_lock(&inode->i_mapping->i_mmap_mutex);
20587e660872SDavid Howells 
20597e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
20607e660872SDavid Howells 	vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
20617e660872SDavid Howells 			      low, high) {
20627e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
20637e660872SDavid Howells 		 * cache */
20647e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
2065918e556eSDavid Howells 			mutex_unlock(&inode->i_mapping->i_mmap_mutex);
20667e660872SDavid Howells 			up_write(&nommu_region_sem);
20677e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
20687e660872SDavid Howells 		}
20697e660872SDavid Howells 	}
20707e660872SDavid Howells 
20717e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
20727e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
20737e660872SDavid Howells 	 *
20747e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
20757e660872SDavid Howells 	 * shouldn't be any
20767e660872SDavid Howells 	 */
20777e660872SDavid Howells 	vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
20787e660872SDavid Howells 			      0, ULONG_MAX) {
20797e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
20807e660872SDavid Howells 			continue;
20817e660872SDavid Howells 
20827e660872SDavid Howells 		region = vma->vm_region;
20837e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
20847e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
20857e660872SDavid Howells 
20867e660872SDavid Howells 		if (r_top > newsize) {
20877e660872SDavid Howells 			region->vm_top -= r_top - newsize;
20887e660872SDavid Howells 			if (region->vm_end > region->vm_top)
20897e660872SDavid Howells 				region->vm_end = region->vm_top;
20907e660872SDavid Howells 		}
20917e660872SDavid Howells 	}
20927e660872SDavid Howells 
2093918e556eSDavid Howells 	mutex_unlock(&inode->i_mapping->i_mmap_mutex);
20947e660872SDavid Howells 	up_write(&nommu_region_sem);
20957e660872SDavid Howells 	return 0;
20967e660872SDavid Howells }
2097