xref: /linux/mm/nommu.c (revision d8ed45c5dcd455fc5848d47f86883a1b872ac0d0)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/mm/nommu.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Replacement code for mm functions to support CPU's that don't
61da177e4SLinus Torvalds  *  have any form of memory management unit (thus no virtual memory).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  See Documentation/nommu-mmap.txt
91da177e4SLinus Torvalds  *
108feae131SDavid Howells  *  Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
111da177e4SLinus Torvalds  *  Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
121da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
131da177e4SLinus Torvalds  *  Copyright (c) 2002      Greg Ungerer <gerg@snapgear.com>
1429c185e5SPaul Mundt  *  Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
17b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18b1de0d13SMitchel Humpherys 
19b95f1b31SPaul Gortmaker #include <linux/export.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
216e84f315SIngo Molnar #include <linux/sched/mm.h>
22615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
231da177e4SLinus Torvalds #include <linux/mman.h>
241da177e4SLinus Torvalds #include <linux/swap.h>
251da177e4SLinus Torvalds #include <linux/file.h>
261da177e4SLinus Torvalds #include <linux/highmem.h>
271da177e4SLinus Torvalds #include <linux/pagemap.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/vmalloc.h>
301da177e4SLinus Torvalds #include <linux/blkdev.h>
311da177e4SLinus Torvalds #include <linux/backing-dev.h>
323b32123dSGideon Israel Dsouza #include <linux/compiler.h>
331da177e4SLinus Torvalds #include <linux/mount.h>
341da177e4SLinus Torvalds #include <linux/personality.h>
351da177e4SLinus Torvalds #include <linux/security.h>
361da177e4SLinus Torvalds #include <linux/syscalls.h>
37120a795dSAl Viro #include <linux/audit.h>
38b1de0d13SMitchel Humpherys #include <linux/printk.h>
391da177e4SLinus Torvalds 
407c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
411da177e4SLinus Torvalds #include <asm/tlb.h>
421da177e4SLinus Torvalds #include <asm/tlbflush.h>
43eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
448feae131SDavid Howells #include "internal.h"
458feae131SDavid Howells 
461da177e4SLinus Torvalds void *high_memory;
47944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
481da177e4SLinus Torvalds struct page *mem_map;
491da177e4SLinus Torvalds unsigned long max_mapnr;
505b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
514266c97aSHugh Dickins unsigned long highest_memmap_pfn;
52fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
531da177e4SLinus Torvalds int heap_stack_gap = 0;
541da177e4SLinus Torvalds 
5533e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
568feae131SDavid Howells 
571da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
581da177e4SLinus Torvalds 
598feae131SDavid Howells /* list of mapped, potentially shareable regions */
608feae131SDavid Howells static struct kmem_cache *vm_region_jar;
618feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
628feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
631da177e4SLinus Torvalds 
64f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
691da177e4SLinus Torvalds  * just what the caller asked for.
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct page *page;
761da177e4SLinus Torvalds 
774016a139SMichael Hennerich 	/*
784016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
794016a139SMichael Hennerich 	 * return size of 0
804016a139SMichael Hennerich 	 */
815a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
826cfd53fcSPaul Mundt 		return 0;
836cfd53fcSPaul Mundt 
846cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
856cfd53fcSPaul Mundt 
866cfd53fcSPaul Mundt 	/*
876cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
886cfd53fcSPaul Mundt 	 * kmalloc().
896cfd53fcSPaul Mundt 	 */
901da177e4SLinus Torvalds 	if (PageSlab(page))
911da177e4SLinus Torvalds 		return ksize(objp);
921da177e4SLinus Torvalds 
936cfd53fcSPaul Mundt 	/*
94ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
95ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
96ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
97ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
98ab2e83eaSPaul Mundt 	 */
99ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
100ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
101ab2e83eaSPaul Mundt 
102ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
103ab2e83eaSPaul Mundt 		if (vma)
104ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
105ab2e83eaSPaul Mundt 	}
106ab2e83eaSPaul Mundt 
107ab2e83eaSPaul Mundt 	/*
1086cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1095a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1106cfd53fcSPaul Mundt 	 */
111a50b854eSMatthew Wilcox (Oracle) 	return page_size(page);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114dfc2f91aSPaul Mundt /**
115dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
116dfc2f91aSPaul Mundt  * @vma: memory mapping
117dfc2f91aSPaul Mundt  * @address: user virtual address
118dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
119dfc2f91aSPaul Mundt  *
120dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
121dfc2f91aSPaul Mundt  *
122dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
123dfc2f91aSPaul Mundt  */
124dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
125dfc2f91aSPaul Mundt 	unsigned long *pfn)
126dfc2f91aSPaul Mundt {
127dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
128dfc2f91aSPaul Mundt 		return -EINVAL;
129dfc2f91aSPaul Mundt 
130dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
131dfc2f91aSPaul Mundt 	return 0;
132dfc2f91aSPaul Mundt }
133dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
134dfc2f91aSPaul Mundt 
135f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
1361da177e4SLinus Torvalds 
137b3bdda02SChristoph Lameter void vfree(const void *addr)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	kfree(addr);
1401da177e4SLinus Torvalds }
141b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
1421da177e4SLinus Torvalds 
14388dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	/*
1468518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
1478518609dSRobert P. J. Day 	 * returns only a logical address.
1481da177e4SLinus Torvalds 	 */
14984097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1501da177e4SLinus Torvalds }
151b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
1521da177e4SLinus Torvalds 
153041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align,
154041de93fSChristoph Hellwig 		unsigned long start, unsigned long end, gfp_t gfp_mask,
155041de93fSChristoph Hellwig 		pgprot_t prot, unsigned long vm_flags, int node,
156041de93fSChristoph Hellwig 		const void *caller)
157041de93fSChristoph Hellwig {
158041de93fSChristoph Hellwig 	return __vmalloc(size, gfp_mask);
159041de93fSChristoph Hellwig }
160041de93fSChristoph Hellwig 
1612b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
1622b905948SChristoph Hellwig 		int node, const void *caller)
163a7c3e901SMichal Hocko {
1642b905948SChristoph Hellwig 	return __vmalloc(size, gfp_mask);
165a7c3e901SMichal Hocko }
166a7c3e901SMichal Hocko 
167ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
168f905bc44SPaul Mundt {
169f905bc44SPaul Mundt 	void *ret;
170f905bc44SPaul Mundt 
17188dca4caSChristoph Hellwig 	ret = __vmalloc(size, flags);
172f905bc44SPaul Mundt 	if (ret) {
173f905bc44SPaul Mundt 		struct vm_area_struct *vma;
174f905bc44SPaul Mundt 
175*d8ed45c5SMichel Lespinasse 		mmap_write_lock(current->mm);
176f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
177f905bc44SPaul Mundt 		if (vma)
178f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
179*d8ed45c5SMichel Lespinasse 		mmap_write_unlock(current->mm);
180f905bc44SPaul Mundt 	}
181f905bc44SPaul Mundt 
182f905bc44SPaul Mundt 	return ret;
183f905bc44SPaul Mundt }
184ed81745aSAndrii Nakryiko 
185ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size)
186ed81745aSAndrii Nakryiko {
187ed81745aSAndrii Nakryiko 	return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
188ed81745aSAndrii Nakryiko }
189f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
190f905bc44SPaul Mundt 
191b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	return virt_to_page(addr);
1941da177e4SLinus Torvalds }
195b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
1961da177e4SLinus Torvalds 
197b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2001da177e4SLinus Torvalds }
201b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2041da177e4SLinus Torvalds {
2059bde916bSChen Gang 	/* Don't allow overflow */
2069bde916bSChen Gang 	if ((unsigned long) buf + count < count)
2079bde916bSChen Gang 		count = -(unsigned long) buf;
2089bde916bSChen Gang 
2091da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2101da177e4SLinus Torvalds 	return count;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	/* Don't allow overflow */
2161da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2171da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	memcpy(addr, buf, count);
220ac714904SChoi Gi-yong 	return count;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
224e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
2251da177e4SLinus Torvalds  *
2261da177e4SLinus Torvalds  *	@size:		allocation size
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
229e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
2301da177e4SLinus Torvalds  *
231c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2321da177e4SLinus Torvalds  *	use __vmalloc() instead.
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2351da177e4SLinus Torvalds {
23688dca4caSChristoph Hellwig        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM);
2371da177e4SLinus Torvalds }
238f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
239f6138882SAndrew Morton 
240e1ca7788SDave Young /*
241e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
242e1ca7788SDave Young  *
243e1ca7788SDave Young  *	@size:		allocation size
244e1ca7788SDave Young  *
245e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
246e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
247e1ca7788SDave Young  *	The memory allocated is set to zero.
248e1ca7788SDave Young  *
249e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
250e1ca7788SDave Young  *	use __vmalloc() instead.
251e1ca7788SDave Young  */
252e1ca7788SDave Young void *vzalloc(unsigned long size)
253e1ca7788SDave Young {
25488dca4caSChristoph Hellwig 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
255e1ca7788SDave Young }
256e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
257e1ca7788SDave Young 
258e1ca7788SDave Young /**
259e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
260e1ca7788SDave Young  * @size:	allocation size
261e1ca7788SDave Young  * @node:	numa node
262e1ca7788SDave Young  *
263e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
264e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
265e1ca7788SDave Young  *
266e1ca7788SDave Young  * For tight control over page level allocator and protection flags
267e1ca7788SDave Young  * use __vmalloc() instead.
268e1ca7788SDave Young  */
269f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
270f6138882SAndrew Morton {
271f6138882SAndrew Morton 	return vmalloc(size);
272f6138882SAndrew Morton }
2739a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
274e1ca7788SDave Young 
275e1ca7788SDave Young /**
276e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
277e1ca7788SDave Young  * @size:	allocation size
278e1ca7788SDave Young  * @node:	numa node
279e1ca7788SDave Young  *
280e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
281e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
282e1ca7788SDave Young  * The memory allocated is set to zero.
283e1ca7788SDave Young  *
284e1ca7788SDave Young  * For tight control over page level allocator and protection flags
285e1ca7788SDave Young  * use __vmalloc() instead.
286e1ca7788SDave Young  */
287e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
288e1ca7788SDave Young {
289e1ca7788SDave Young 	return vzalloc(size);
290e1ca7788SDave Young }
291e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2921da177e4SLinus Torvalds 
2931af446edSPaul Mundt /**
2941af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
2951af446edSPaul Mundt  *	@size:		allocation size
2961af446edSPaul Mundt  *
2971af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
2981af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
2991af446edSPaul Mundt  *	executable kernel virtual space.
3001af446edSPaul Mundt  *
3011af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3021af446edSPaul Mundt  *	use __vmalloc() instead.
3031af446edSPaul Mundt  */
3041af446edSPaul Mundt 
3051af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3061af446edSPaul Mundt {
30788dca4caSChristoph Hellwig 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM);
3081af446edSPaul Mundt }
3091af446edSPaul Mundt 
310b5073173SPaul Mundt /**
311b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3121da177e4SLinus Torvalds  *	@size:		allocation size
3131da177e4SLinus Torvalds  *
3141da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
315e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
3161da177e4SLinus Torvalds  */
3171da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3181da177e4SLinus Torvalds {
31988dca4caSChristoph Hellwig 	return __vmalloc(size, GFP_KERNEL);
3201da177e4SLinus Torvalds }
321b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
322b5073173SPaul Mundt 
323b5073173SPaul Mundt /**
324b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
325b5073173SPaul Mundt  *	@size:		allocation size
326b5073173SPaul Mundt  *
327b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
328b5073173SPaul Mundt  * mapped to userspace without leaking data.
329f905bc44SPaul Mundt  *
330f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
331f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
332b5073173SPaul Mundt  */
333b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
334b5073173SPaul Mundt {
335f905bc44SPaul Mundt 	/*
336f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
337f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
338f905bc44SPaul Mundt 	 */
339f905bc44SPaul Mundt 	return vmalloc_user(size);
340b5073173SPaul Mundt }
341b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	BUG();
3461da177e4SLinus Torvalds 	return NULL;
3471da177e4SLinus Torvalds }
348b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3491da177e4SLinus Torvalds 
350b3bdda02SChristoph Lameter void vunmap(const void *addr)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	BUG();
3531da177e4SLinus Torvalds }
354b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3551da177e4SLinus Torvalds 
356d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
357eb6434d9SPaul Mundt {
358eb6434d9SPaul Mundt 	BUG();
359eb6434d9SPaul Mundt 	return NULL;
360eb6434d9SPaul Mundt }
361eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
362eb6434d9SPaul Mundt 
363eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
364eb6434d9SPaul Mundt {
365eb6434d9SPaul Mundt 	BUG();
366eb6434d9SPaul Mundt }
367eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
368eb6434d9SPaul Mundt 
369eb6434d9SPaul Mundt void vm_unmap_aliases(void)
370eb6434d9SPaul Mundt {
371eb6434d9SPaul Mundt }
372eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
373eb6434d9SPaul Mundt 
374cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
37529c185e5SPaul Mundt {
37629c185e5SPaul Mundt 	BUG();
37729c185e5SPaul Mundt 	return NULL;
37829c185e5SPaul Mundt }
37929c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
38029c185e5SPaul Mundt 
38129c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
38229c185e5SPaul Mundt {
38329c185e5SPaul Mundt 	BUG();
38429c185e5SPaul Mundt }
38529c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
38629c185e5SPaul Mundt 
387b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
388b5073173SPaul Mundt 		   struct page *page)
389b5073173SPaul Mundt {
390b5073173SPaul Mundt 	return -EINVAL;
391b5073173SPaul Mundt }
392b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
393b5073173SPaul Mundt 
394a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
395a667d745SSouptick Joarder 			unsigned long num)
396a667d745SSouptick Joarder {
397a667d745SSouptick Joarder 	return -EINVAL;
398a667d745SSouptick Joarder }
399a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages);
400a667d745SSouptick Joarder 
401a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
402a667d745SSouptick Joarder 				unsigned long num)
403a667d745SSouptick Joarder {
404a667d745SSouptick Joarder 	return -EINVAL;
405a667d745SSouptick Joarder }
406a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero);
407a667d745SSouptick Joarder 
4081eeb66a1SChristoph Hellwig /*
4091da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4101da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4111da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4121da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4131da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4141da177e4SLinus Torvalds  */
4156a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
4201da177e4SLinus Torvalds 		return mm->brk;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	if (mm->brk == brk)
4231da177e4SLinus Torvalds 		return mm->brk;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/*
4261da177e4SLinus Torvalds 	 * Always allow shrinking brk
4271da177e4SLinus Torvalds 	 */
4281da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4291da177e4SLinus Torvalds 		mm->brk = brk;
4301da177e4SLinus Torvalds 		return brk;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/*
4341da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4351da177e4SLinus Torvalds 	 */
436a75a2df6SChristoph Hellwig 	flush_icache_user_range(mm->brk, brk);
4371da177e4SLinus Torvalds 	return mm->brk = brk;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4408feae131SDavid Howells /*
4413edf41d8Sseokhoon.yoon  * initialise the percpu counter for VM and region record slabs
4428feae131SDavid Howells  */
4438feae131SDavid Howells void __init mmap_init(void)
4441da177e4SLinus Torvalds {
44500a62ce9SKOSAKI Motohiro 	int ret;
44600a62ce9SKOSAKI Motohiro 
447908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
44800a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
4495d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
4508feae131SDavid Howells }
4511da177e4SLinus Torvalds 
4528feae131SDavid Howells /*
4538feae131SDavid Howells  * validate the region tree
4548feae131SDavid Howells  * - the caller must hold the region lock
4558feae131SDavid Howells  */
4568feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4578feae131SDavid Howells static noinline void validate_nommu_regions(void)
4588feae131SDavid Howells {
4598feae131SDavid Howells 	struct vm_region *region, *last;
4608feae131SDavid Howells 	struct rb_node *p, *lastp;
4611da177e4SLinus Torvalds 
4628feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
4638feae131SDavid Howells 	if (!lastp)
4648feae131SDavid Howells 		return;
4658feae131SDavid Howells 
4668feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
467c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
468c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
4698feae131SDavid Howells 
4708feae131SDavid Howells 	while ((p = rb_next(lastp))) {
4718feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
4728feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
4738feae131SDavid Howells 
474c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
475c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
476c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
4778feae131SDavid Howells 
4788feae131SDavid Howells 		lastp = p;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds }
4818feae131SDavid Howells #else
48233e5d769SDavid Howells static void validate_nommu_regions(void)
48333e5d769SDavid Howells {
48433e5d769SDavid Howells }
4858feae131SDavid Howells #endif
4868feae131SDavid Howells 
4878feae131SDavid Howells /*
4888feae131SDavid Howells  * add a region into the global tree
4898feae131SDavid Howells  */
4908feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4918feae131SDavid Howells {
4928feae131SDavid Howells 	struct vm_region *pregion;
4938feae131SDavid Howells 	struct rb_node **p, *parent;
4948feae131SDavid Howells 
4958feae131SDavid Howells 	validate_nommu_regions();
4968feae131SDavid Howells 
4978feae131SDavid Howells 	parent = NULL;
4988feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
4998feae131SDavid Howells 	while (*p) {
5008feae131SDavid Howells 		parent = *p;
5018feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5028feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5038feae131SDavid Howells 			p = &(*p)->rb_left;
5048feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5058feae131SDavid Howells 			p = &(*p)->rb_right;
5068feae131SDavid Howells 		else if (pregion == region)
5078feae131SDavid Howells 			return;
5088feae131SDavid Howells 		else
5098feae131SDavid Howells 			BUG();
5108feae131SDavid Howells 	}
5118feae131SDavid Howells 
5128feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5138feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5148feae131SDavid Howells 
5158feae131SDavid Howells 	validate_nommu_regions();
5168feae131SDavid Howells }
5178feae131SDavid Howells 
5188feae131SDavid Howells /*
5198feae131SDavid Howells  * delete a region from the global tree
5208feae131SDavid Howells  */
5218feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
5228feae131SDavid Howells {
5238feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5248feae131SDavid Howells 
5258feae131SDavid Howells 	validate_nommu_regions();
5268feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5278feae131SDavid Howells 	validate_nommu_regions();
5288feae131SDavid Howells }
5298feae131SDavid Howells 
5308feae131SDavid Howells /*
5318feae131SDavid Howells  * free a contiguous series of pages
5328feae131SDavid Howells  */
5338feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5348feae131SDavid Howells {
5358feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5368feae131SDavid Howells 		struct page *page = virt_to_page(from);
5378feae131SDavid Howells 
53833e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5398feae131SDavid Howells 		put_page(page);
5408feae131SDavid Howells 	}
5418feae131SDavid Howells }
5428feae131SDavid Howells 
5438feae131SDavid Howells /*
5448feae131SDavid Howells  * release a reference to a region
54533e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
546dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5478feae131SDavid Howells  *   will equal vm_start
5488feae131SDavid Howells  */
5498feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5508feae131SDavid Howells 	__releases(nommu_region_sem)
5518feae131SDavid Howells {
5528feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5538feae131SDavid Howells 
5541e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
555dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
5568feae131SDavid Howells 			delete_nommu_region(region);
5578feae131SDavid Howells 		up_write(&nommu_region_sem);
5588feae131SDavid Howells 
5598feae131SDavid Howells 		if (region->vm_file)
5608feae131SDavid Howells 			fput(region->vm_file);
5618feae131SDavid Howells 
5628feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
5638feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
56422cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
565dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
5668feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
5678feae131SDavid Howells 	} else {
5688feae131SDavid Howells 		up_write(&nommu_region_sem);
5698feae131SDavid Howells 	}
5708feae131SDavid Howells }
5718feae131SDavid Howells 
5728feae131SDavid Howells /*
5738feae131SDavid Howells  * release a reference to a region
5748feae131SDavid Howells  */
5758feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
5768feae131SDavid Howells {
5778feae131SDavid Howells 	down_write(&nommu_region_sem);
5788feae131SDavid Howells 	__put_nommu_region(region);
5798feae131SDavid Howells }
5801da177e4SLinus Torvalds 
5813034097aSDavid Howells /*
5823034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
5838feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
5848feae131SDavid Howells  * page
5853034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
5863034097aSDavid Howells  */
5878feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
5883034097aSDavid Howells {
5896038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
5908feae131SDavid Howells 	struct address_space *mapping;
5916038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
5923034097aSDavid Howells 
5938feae131SDavid Howells 	BUG_ON(!vma->vm_region);
5948feae131SDavid Howells 
5958feae131SDavid Howells 	mm->map_count++;
5968feae131SDavid Howells 	vma->vm_mm = mm;
5978feae131SDavid Howells 
5988feae131SDavid Howells 	/* add the VMA to the mapping */
5998feae131SDavid Howells 	if (vma->vm_file) {
6008feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6018feae131SDavid Howells 
60283cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6038feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6046b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
6058feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
60683cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6078feae131SDavid Howells 	}
6088feae131SDavid Howells 
6098feae131SDavid Howells 	/* add the VMA to the tree */
6106038def0SNamhyung Kim 	parent = rb_prev = NULL;
6118feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
6128feae131SDavid Howells 	while (*p) {
6138feae131SDavid Howells 		parent = *p;
6148feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
6158feae131SDavid Howells 
6168feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
6178feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
6188feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
6198feae131SDavid Howells 			p = &(*p)->rb_left;
6206038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
6216038def0SNamhyung Kim 			rb_prev = parent;
6228feae131SDavid Howells 			p = &(*p)->rb_right;
6236038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
6248feae131SDavid Howells 			p = &(*p)->rb_left;
6256038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
6266038def0SNamhyung Kim 			rb_prev = parent;
6278feae131SDavid Howells 			p = &(*p)->rb_right;
6286038def0SNamhyung Kim 		} else if (vma < pvma)
6298feae131SDavid Howells 			p = &(*p)->rb_left;
6306038def0SNamhyung Kim 		else if (vma > pvma) {
6316038def0SNamhyung Kim 			rb_prev = parent;
6328feae131SDavid Howells 			p = &(*p)->rb_right;
6336038def0SNamhyung Kim 		} else
6348feae131SDavid Howells 			BUG();
6358feae131SDavid Howells 	}
6368feae131SDavid Howells 
6378feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
6388feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
6398feae131SDavid Howells 
6408feae131SDavid Howells 	/* add VMA to the VMA list also */
6416038def0SNamhyung Kim 	prev = NULL;
6426038def0SNamhyung Kim 	if (rb_prev)
6436038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
6443034097aSDavid Howells 
645aba6dfb7SWei Yang 	__vma_link_list(mm, vma, prev);
6468feae131SDavid Howells }
6478feae131SDavid Howells 
6488feae131SDavid Howells /*
6498feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
6508feae131SDavid Howells  */
6518feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
6528feae131SDavid Howells {
653615d6e87SDavidlohr Bueso 	int i;
6548feae131SDavid Howells 	struct address_space *mapping;
6558feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
656615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
6578feae131SDavid Howells 
6588feae131SDavid Howells 	mm->map_count--;
659615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
660615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
661314ff785SIngo Molnar 		if (curr->vmacache.vmas[i] == vma) {
662e020d5bdSSteven Miao 			vmacache_invalidate(mm);
663615d6e87SDavidlohr Bueso 			break;
664615d6e87SDavidlohr Bueso 		}
665615d6e87SDavidlohr Bueso 	}
6668feae131SDavid Howells 
6678feae131SDavid Howells 	/* remove the VMA from the mapping */
6688feae131SDavid Howells 	if (vma->vm_file) {
6698feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6708feae131SDavid Howells 
67183cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6728feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6736b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
6748feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
67583cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6768feae131SDavid Howells 	}
6778feae131SDavid Howells 
6788feae131SDavid Howells 	/* remove from the MM's tree and list */
6798feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
680b951bf2cSNamhyung Kim 
6811b9fc5b2SWei Yang 	__vma_unlink_list(mm, vma);
6828feae131SDavid Howells }
6838feae131SDavid Howells 
6848feae131SDavid Howells /*
6858feae131SDavid Howells  * destroy a VMA record
6868feae131SDavid Howells  */
6878feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
6888feae131SDavid Howells {
6898feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
6908feae131SDavid Howells 		vma->vm_ops->close(vma);
691e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
6928feae131SDavid Howells 		fput(vma->vm_file);
6938feae131SDavid Howells 	put_nommu_region(vma->vm_region);
6943928d4f5SLinus Torvalds 	vm_area_free(vma);
6953034097aSDavid Howells }
6963034097aSDavid Howells 
6973034097aSDavid Howells /*
6983034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
6993034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7003034097aSDavid Howells  */
7013034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
7023034097aSDavid Howells {
7038feae131SDavid Howells 	struct vm_area_struct *vma;
7043034097aSDavid Howells 
7058feae131SDavid Howells 	/* check the cache first */
706615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
707615d6e87SDavidlohr Bueso 	if (likely(vma))
7088feae131SDavid Howells 		return vma;
7098feae131SDavid Howells 
710e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7118feae131SDavid Howells 	 * resides) */
712e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7138feae131SDavid Howells 		if (vma->vm_start > addr)
7148feae131SDavid Howells 			return NULL;
7158feae131SDavid Howells 		if (vma->vm_end > addr) {
716615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7178feae131SDavid Howells 			return vma;
7183034097aSDavid Howells 		}
7198feae131SDavid Howells 	}
7203034097aSDavid Howells 
7213034097aSDavid Howells 	return NULL;
7223034097aSDavid Howells }
7233034097aSDavid Howells EXPORT_SYMBOL(find_vma);
7243034097aSDavid Howells 
7253034097aSDavid Howells /*
726930e652aSDavid Howells  * find a VMA
727930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
728930e652aSDavid Howells  */
729930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
730930e652aSDavid Howells {
7317561e8caSDavid Howells 	return find_vma(mm, addr);
732930e652aSDavid Howells }
733930e652aSDavid Howells 
7348feae131SDavid Howells /*
7358feae131SDavid Howells  * expand a stack to a given address
7368feae131SDavid Howells  * - not supported under NOMMU conditions
7378feae131SDavid Howells  */
73857c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
73957c8f63eSGreg Ungerer {
74057c8f63eSGreg Ungerer 	return -ENOMEM;
74157c8f63eSGreg Ungerer }
74257c8f63eSGreg Ungerer 
743930e652aSDavid Howells /*
7446fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
7456fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7466fa5f80bSDavid Howells  */
7478feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
7488feae131SDavid Howells 					     unsigned long addr,
7498feae131SDavid Howells 					     unsigned long len)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct vm_area_struct *vma;
7528feae131SDavid Howells 	unsigned long end = addr + len;
7531da177e4SLinus Torvalds 
7548feae131SDavid Howells 	/* check the cache first */
755615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
756615d6e87SDavidlohr Bueso 	if (vma)
7571da177e4SLinus Torvalds 		return vma;
7588feae131SDavid Howells 
759e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7608feae131SDavid Howells 	 * resides) */
761e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7628feae131SDavid Howells 		if (vma->vm_start < addr)
7638feae131SDavid Howells 			continue;
7648feae131SDavid Howells 		if (vma->vm_start > addr)
7658feae131SDavid Howells 			return NULL;
7668feae131SDavid Howells 		if (vma->vm_end == end) {
767615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7688feae131SDavid Howells 			return vma;
7698feae131SDavid Howells 		}
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	return NULL;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7753034097aSDavid Howells /*
7761da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
7771da177e4SLinus Torvalds  * mapping we're capable of supporting
7781da177e4SLinus Torvalds  */
7791da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
7801da177e4SLinus Torvalds 				 unsigned long addr,
7811da177e4SLinus Torvalds 				 unsigned long len,
7821da177e4SLinus Torvalds 				 unsigned long prot,
7831da177e4SLinus Torvalds 				 unsigned long flags,
7841da177e4SLinus Torvalds 				 unsigned long pgoff,
7851da177e4SLinus Torvalds 				 unsigned long *_capabilities)
7861da177e4SLinus Torvalds {
7878feae131SDavid Howells 	unsigned long capabilities, rlen;
7881da177e4SLinus Torvalds 	int ret;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	/* do the simple checks first */
79122cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
7921da177e4SLinus Torvalds 		return -EINVAL;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
7951da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
7961da177e4SLinus Torvalds 		return -EINVAL;
7971da177e4SLinus Torvalds 
798f81cff0dSMike Frysinger 	if (!len)
7991da177e4SLinus Torvalds 		return -EINVAL;
8001da177e4SLinus Torvalds 
801f81cff0dSMike Frysinger 	/* Careful about overflows.. */
8028feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
8038feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
804f81cff0dSMike Frysinger 		return -ENOMEM;
805f81cff0dSMike Frysinger 
8061da177e4SLinus Torvalds 	/* offset overflow? */
8078feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
808f81cff0dSMike Frysinger 		return -EOVERFLOW;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	if (file) {
8111da177e4SLinus Torvalds 		/* files must support mmap */
81272c2d531SAl Viro 		if (!file->f_op->mmap)
8131da177e4SLinus Torvalds 			return -ENODEV;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
8161da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
8171da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
8181da177e4SLinus Torvalds 		 */
819b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
820b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
821b4caecd4SChristoph Hellwig 		} else {
8221da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
8231da177e4SLinus Torvalds 			 * defaults */
824496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
8251da177e4SLinus Torvalds 			case S_IFREG:
8261da177e4SLinus Torvalds 			case S_IFBLK:
827b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
8281da177e4SLinus Torvalds 				break;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 			case S_IFCHR:
8311da177e4SLinus Torvalds 				capabilities =
832b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
833b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
834b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
8351da177e4SLinus Torvalds 				break;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 			default:
8381da177e4SLinus Torvalds 				return -EINVAL;
8391da177e4SLinus Torvalds 			}
8401da177e4SLinus Torvalds 		}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
8431da177e4SLinus Torvalds 		 * device */
8441da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
845b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
8466e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
847b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
8481da177e4SLinus Torvalds 
84928d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
85028d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
85128d7a6aeSGraff Yang 			return -EACCES;
85228d7a6aeSGraff Yang 
8531da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
8541da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
8551da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
8561da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
8571da177e4SLinus Torvalds 				return -EACCES;
8581da177e4SLinus Torvalds 
859496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
8601da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
8611da177e4SLinus Torvalds 				return -EACCES;
8621da177e4SLinus Torvalds 
863d7a06983SJeff Layton 			if (locks_verify_locked(file))
8641da177e4SLinus Torvalds 				return -EAGAIN;
8651da177e4SLinus Torvalds 
866b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
8671da177e4SLinus Torvalds 				return -ENODEV;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
870b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
871ac714904SChoi Gi-yong 		} else {
8721da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
8731da177e4SLinus Torvalds 			 * allocate */
874b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
8751da177e4SLinus Torvalds 				return -ENODEV;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
8781da177e4SLinus Torvalds 			 * shared with the backing device */
8791da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
880b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8811da177e4SLinus Torvalds 		}
8821da177e4SLinus Torvalds 
883b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
884b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
885b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
886b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
8873c7b2045SBernd Schmidt 			    ) {
888b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8893c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
89022cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
8913c7b2045SBernd Schmidt 					return -EINVAL;
8923c7b2045SBernd Schmidt 				}
8933c7b2045SBernd Schmidt 			}
8943c7b2045SBernd Schmidt 		}
8953c7b2045SBernd Schmidt 
8961da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
8971da177e4SLinus Torvalds 		 * mappings */
89890f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
8991da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
9001da177e4SLinus Torvalds 				return -EPERM;
901ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
9021da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
9031da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
904b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
9051da177e4SLinus Torvalds 					prot |= PROT_EXEC;
9061da177e4SLinus Torvalds 			}
907ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
9081da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
909b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
9101da177e4SLinus Torvalds 			 ) {
9111da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
912b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
9131da177e4SLinus Torvalds 		}
914ac714904SChoi Gi-yong 	} else {
9151da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
9161da177e4SLinus Torvalds 		 * privately mapped
9171da177e4SLinus Torvalds 		 */
918b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
9211da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
9221da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
9231da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	/* allow the security API to have its say */
927e5467859SAl Viro 	ret = security_mmap_addr(addr);
928e5467859SAl Viro 	if (ret < 0)
929e5467859SAl Viro 		return ret;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	/* looks okay */
9321da177e4SLinus Torvalds 	*_capabilities = capabilities;
9331da177e4SLinus Torvalds 	return 0;
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds /*
9371da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
9381da177e4SLinus Torvalds  * now know into VMA flags
9391da177e4SLinus Torvalds  */
9401da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
9411da177e4SLinus Torvalds 					unsigned long prot,
9421da177e4SLinus Torvalds 					unsigned long flags,
9431da177e4SLinus Torvalds 					unsigned long capabilities)
9441da177e4SLinus Torvalds {
9451da177e4SLinus Torvalds 	unsigned long vm_flags;
9461da177e4SLinus Torvalds 
947e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
9481da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
9491da177e4SLinus Torvalds 
950b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
9511da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
9523c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9531da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
9541da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
9553c7b2045SBernd Schmidt 	} else {
9561da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
9571da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
9581da177e4SLinus Torvalds 		 * romfs/cramfs */
959b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
9601da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
9613c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
9621da177e4SLinus Torvalds 	}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
9651da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
9661da177e4SLinus Torvalds 	 * with another untraced process
9671da177e4SLinus Torvalds 	 */
968a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
9691da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	return vm_flags;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds /*
9758feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
9768feae131SDavid Howells  * pins the storage)
9771da177e4SLinus Torvalds  */
9788feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	int ret;
9811da177e4SLinus Torvalds 
982f74ac015SMiklos Szeredi 	ret = call_mmap(vma->vm_file, vma);
983dd8632a1SPaul Mundt 	if (ret == 0) {
984dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
985645d83c5SDavid Howells 		return 0;
986dd8632a1SPaul Mundt 	}
9871da177e4SLinus Torvalds 	if (ret != -ENOSYS)
9881da177e4SLinus Torvalds 		return ret;
9891da177e4SLinus Torvalds 
9903fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
9913fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
9923fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
9931da177e4SLinus Torvalds 	return -ENODEV;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds /*
9971da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
9981da177e4SLinus Torvalds  */
9998feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
10008feae131SDavid Howells 			   struct vm_region *region,
1001645d83c5SDavid Howells 			   unsigned long len,
1002645d83c5SDavid Howells 			   unsigned long capabilities)
10031da177e4SLinus Torvalds {
1004dbc8358cSJoonsoo Kim 	unsigned long total, point;
10051da177e4SLinus Torvalds 	void *base;
10068feae131SDavid Howells 	int ret, order;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
10091da177e4SLinus Torvalds 	 * shared mappings on devices or memory
10101da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
10111da177e4SLinus Torvalds 	 */
1012b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
1013f74ac015SMiklos Szeredi 		ret = call_mmap(vma->vm_file, vma);
1014dd8632a1SPaul Mundt 		if (ret == 0) {
10151da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1016dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1017dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1018645d83c5SDavid Howells 			return 0;
10191da177e4SLinus Torvalds 		}
1020dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1021dd8632a1SPaul Mundt 			return ret;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
10241da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
10251da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds 
10288feae131SDavid Howells 
10291da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
10301da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
10311da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
10321da177e4SLinus Torvalds 	 */
1033f67d9b15SBob Liu 	order = get_order(len);
10348feae131SDavid Howells 	total = 1 << order;
1035f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1036dd8632a1SPaul Mundt 
1037dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
103822cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1039dbc8358cSJoonsoo Kim 		total = point;
10408feae131SDavid Howells 
1041da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1042dbc8358cSJoonsoo Kim 	if (!base)
1043dbc8358cSJoonsoo Kim 		goto enomem;
10448feae131SDavid Howells 
1045dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1046dbc8358cSJoonsoo Kim 
10478feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
10488feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1049f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1050dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
10518feae131SDavid Howells 
10528feae131SDavid Howells 	vma->vm_start = region->vm_start;
10538feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	if (vma->vm_file) {
10561da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
10571da177e4SLinus Torvalds 		loff_t fpos;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
10601da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
10611da177e4SLinus Torvalds 
1062b4bf802aSChristoph Hellwig 		ret = kernel_read(vma->vm_file, base, len, &fpos);
10631da177e4SLinus Torvalds 		if (ret < 0)
10641da177e4SLinus Torvalds 			goto error_free;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 		/* clear the last little bit */
1067f67d9b15SBob Liu 		if (ret < len)
1068f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
10691da177e4SLinus Torvalds 
1070bfd40eafSKirill A. Shutemov 	} else {
1071bfd40eafSKirill A. Shutemov 		vma_set_anonymous(vma);
10721da177e4SLinus Torvalds 	}
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	return 0;
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds error_free:
10777223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
10788feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
10798feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1080dd8632a1SPaul Mundt 	region->vm_top   = 0;
10811da177e4SLinus Torvalds 	return ret;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds enomem:
1084b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
108505ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
10869af744d7SMichal Hocko 	show_free_areas(0, NULL);
10871da177e4SLinus Torvalds 	return -ENOMEM;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /*
10911da177e4SLinus Torvalds  * handle mapping creation for uClinux
10921da177e4SLinus Torvalds  */
10931fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
10941da177e4SLinus Torvalds 			unsigned long addr,
10951da177e4SLinus Torvalds 			unsigned long len,
10961da177e4SLinus Torvalds 			unsigned long prot,
10971da177e4SLinus Torvalds 			unsigned long flags,
10981fcfd8dbSOleg Nesterov 			vm_flags_t vm_flags,
1099bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
1100897ab3e0SMike Rapoport 			unsigned long *populate,
1101897ab3e0SMike Rapoport 			struct list_head *uf)
11021da177e4SLinus Torvalds {
11038feae131SDavid Howells 	struct vm_area_struct *vma;
11048feae131SDavid Howells 	struct vm_region *region;
11051da177e4SLinus Torvalds 	struct rb_node *rb;
11061fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
11071da177e4SLinus Torvalds 	int ret;
11081da177e4SLinus Torvalds 
110941badc15SMichel Lespinasse 	*populate = 0;
1110bebeb3d6SMichel Lespinasse 
11111da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
11121da177e4SLinus Torvalds 	 * mapping */
11131da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
11141da177e4SLinus Torvalds 				    &capabilities);
111522cc877bSLeon Romanovsky 	if (ret < 0)
11161da177e4SLinus Torvalds 		return ret;
11171da177e4SLinus Torvalds 
111806aab5a3SDavid Howells 	/* we ignore the address hint */
111906aab5a3SDavid Howells 	addr = 0;
1120f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
112106aab5a3SDavid Howells 
11221da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
11231da177e4SLinus Torvalds 	 * now know into VMA flags */
11241fcfd8dbSOleg Nesterov 	vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
11251da177e4SLinus Torvalds 
11268feae131SDavid Howells 	/* we're going to need to record the mapping */
11278feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
11288feae131SDavid Howells 	if (!region)
11298feae131SDavid Howells 		goto error_getting_region;
11301da177e4SLinus Torvalds 
1131490fc053SLinus Torvalds 	vma = vm_area_alloc(current->mm);
11328feae131SDavid Howells 	if (!vma)
11338feae131SDavid Howells 		goto error_getting_vma;
11341da177e4SLinus Torvalds 
11351e2ae599SDavid Howells 	region->vm_usage = 1;
11368feae131SDavid Howells 	region->vm_flags = vm_flags;
11378feae131SDavid Howells 	region->vm_pgoff = pgoff;
11388feae131SDavid Howells 
11398feae131SDavid Howells 	vma->vm_flags = vm_flags;
11408feae131SDavid Howells 	vma->vm_pgoff = pgoff;
11418feae131SDavid Howells 
11428feae131SDavid Howells 	if (file) {
1143cb0942b8SAl Viro 		region->vm_file = get_file(file);
1144cb0942b8SAl Viro 		vma->vm_file = get_file(file);
11458feae131SDavid Howells 	}
11468feae131SDavid Howells 
11478feae131SDavid Howells 	down_write(&nommu_region_sem);
11488feae131SDavid Howells 
11498feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
11501da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
11518feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
11521da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
11531da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
11541da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
11551da177e4SLinus Torvalds 	 *   than here
11561da177e4SLinus Torvalds 	 */
11571da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
11588feae131SDavid Howells 		struct vm_region *pregion;
11598feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
11601da177e4SLinus Torvalds 
11618feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
11628feae131SDavid Howells 		pgend = pgoff + pglen;
1163165b2392SDavid Howells 
11648feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
11658feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
11661da177e4SLinus Torvalds 
11678feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
11681da177e4SLinus Torvalds 				continue;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1171496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1172496ad9aaSAl Viro 			    file_inode(file))
11731da177e4SLinus Torvalds 				continue;
11741da177e4SLinus Torvalds 
11758feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
11761da177e4SLinus Torvalds 				continue;
11771da177e4SLinus Torvalds 
11788feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
11798feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
11808feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
11818feae131SDavid Howells 			if (pgoff >= rpgend)
11821da177e4SLinus Torvalds 				continue;
11831da177e4SLinus Torvalds 
11848feae131SDavid Howells 			/* handle inexactly overlapping matches between
11858feae131SDavid Howells 			 * mappings */
11868feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
11878feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
11888feae131SDavid Howells 				/* new mapping is not a subset of the region */
1189b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
11901da177e4SLinus Torvalds 					goto sharing_violation;
11911da177e4SLinus Torvalds 				continue;
11921da177e4SLinus Torvalds 			}
11931da177e4SLinus Torvalds 
11948feae131SDavid Howells 			/* we've found a region we can share */
11951e2ae599SDavid Howells 			pregion->vm_usage++;
11968feae131SDavid Howells 			vma->vm_region = pregion;
11978feae131SDavid Howells 			start = pregion->vm_start;
11988feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
11998feae131SDavid Howells 			vma->vm_start = start;
12008feae131SDavid Howells 			vma->vm_end = start + len;
12011da177e4SLinus Torvalds 
120222cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
12038feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
120422cc877bSLeon Romanovsky 			else {
12058feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
12068feae131SDavid Howells 				if (ret < 0) {
12078feae131SDavid Howells 					vma->vm_region = NULL;
12088feae131SDavid Howells 					vma->vm_start = 0;
12098feae131SDavid Howells 					vma->vm_end = 0;
12101e2ae599SDavid Howells 					pregion->vm_usage--;
12118feae131SDavid Howells 					pregion = NULL;
12128feae131SDavid Howells 					goto error_just_free;
12131da177e4SLinus Torvalds 				}
12148feae131SDavid Howells 			}
12158feae131SDavid Howells 			fput(region->vm_file);
12168feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
12178feae131SDavid Howells 			region = pregion;
12188feae131SDavid Howells 			result = start;
12198feae131SDavid Howells 			goto share;
12208feae131SDavid Howells 		}
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
12231da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
12241da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
12251da177e4SLinus Torvalds 		 */
1226b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
12271da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
12281da177e4SLinus Torvalds 							     pgoff, flags);
1229bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
12301da177e4SLinus Torvalds 				ret = addr;
1231bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
12328feae131SDavid Howells 					goto error_just_free;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
12351da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
12361da177e4SLinus Torvalds 				 * it */
1237bb005a59SNamhyung Kim 				ret = -ENODEV;
1238b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
12398feae131SDavid Howells 					goto error_just_free;
12401da177e4SLinus Torvalds 
1241b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
12428feae131SDavid Howells 			} else {
12438feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
12448feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
12451da177e4SLinus Torvalds 			}
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12498feae131SDavid Howells 	vma->vm_region = region;
12501da177e4SLinus Torvalds 
1251645d83c5SDavid Howells 	/* set up the mapping
1252b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1253645d83c5SDavid Howells 	 */
12541da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
12558feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
12561da177e4SLinus Torvalds 	else
1257645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
12581da177e4SLinus Torvalds 	if (ret < 0)
1259645d83c5SDavid Howells 		goto error_just_free;
1260645d83c5SDavid Howells 	add_nommu_region(region);
12618feae131SDavid Howells 
1262ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
12630bf5f949SChristoph Hellwig 	if (!vma->vm_file &&
12640bf5f949SChristoph Hellwig 	    (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
12650bf5f949SChristoph Hellwig 	     !(flags & MAP_UNINITIALIZED)))
1266ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1267ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1268ea637639SJie Zhang 
12691da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
12708feae131SDavid Howells 	result = vma->vm_start;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
12731da177e4SLinus Torvalds 
12748feae131SDavid Howells share:
12758feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
12761da177e4SLinus Torvalds 
1277cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1278cfe79c00SMike Frysinger 	 * mapping of it is made  */
1279cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1280a75a2df6SChristoph Hellwig 		flush_icache_user_range(region->vm_start, region->vm_end);
1281cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1282cfe79c00SMike Frysinger 	}
12831da177e4SLinus Torvalds 
1284cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
12851da177e4SLinus Torvalds 
12868feae131SDavid Howells 	return result;
12871da177e4SLinus Torvalds 
12888feae131SDavid Howells error_just_free:
12898feae131SDavid Howells 	up_write(&nommu_region_sem);
12908feae131SDavid Howells error:
129189a86402SDavid Howells 	if (region->vm_file)
12928feae131SDavid Howells 		fput(region->vm_file);
12938feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
129489a86402SDavid Howells 	if (vma->vm_file)
12958feae131SDavid Howells 		fput(vma->vm_file);
12963928d4f5SLinus Torvalds 	vm_area_free(vma);
12971da177e4SLinus Torvalds 	return ret;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds sharing_violation:
13008feae131SDavid Howells 	up_write(&nommu_region_sem);
130122cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
13028feae131SDavid Howells 	ret = -EINVAL;
13038feae131SDavid Howells 	goto error;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds error_getting_vma:
13068feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
130722cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
13081da177e4SLinus Torvalds 			len, current->pid);
13099af744d7SMichal Hocko 	show_free_areas(0, NULL);
13101da177e4SLinus Torvalds 	return -ENOMEM;
13111da177e4SLinus Torvalds 
13128feae131SDavid Howells error_getting_region:
131322cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
13141da177e4SLinus Torvalds 			len, current->pid);
13159af744d7SMichal Hocko 	show_free_areas(0, NULL);
13161da177e4SLinus Torvalds 	return -ENOMEM;
13171da177e4SLinus Torvalds }
13186be5ceb0SLinus Torvalds 
1319a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1320a90f590aSDominik Brodowski 			      unsigned long prot, unsigned long flags,
1321a90f590aSDominik Brodowski 			      unsigned long fd, unsigned long pgoff)
132266f0dc48SHugh Dickins {
132366f0dc48SHugh Dickins 	struct file *file = NULL;
132466f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
132566f0dc48SHugh Dickins 
1326120a795dSAl Viro 	audit_mmap_fd(fd, flags);
132766f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
132866f0dc48SHugh Dickins 		file = fget(fd);
132966f0dc48SHugh Dickins 		if (!file)
133066f0dc48SHugh Dickins 			goto out;
133166f0dc48SHugh Dickins 	}
133266f0dc48SHugh Dickins 
133366f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
133466f0dc48SHugh Dickins 
1335ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
133666f0dc48SHugh Dickins 
133766f0dc48SHugh Dickins 	if (file)
133866f0dc48SHugh Dickins 		fput(file);
133966f0dc48SHugh Dickins out:
134066f0dc48SHugh Dickins 	return retval;
134166f0dc48SHugh Dickins }
134266f0dc48SHugh Dickins 
1343a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1344a90f590aSDominik Brodowski 		unsigned long, prot, unsigned long, flags,
1345a90f590aSDominik Brodowski 		unsigned long, fd, unsigned long, pgoff)
1346a90f590aSDominik Brodowski {
1347a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1348a90f590aSDominik Brodowski }
1349a90f590aSDominik Brodowski 
1350a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1351a4679373SChristoph Hellwig struct mmap_arg_struct {
1352a4679373SChristoph Hellwig 	unsigned long addr;
1353a4679373SChristoph Hellwig 	unsigned long len;
1354a4679373SChristoph Hellwig 	unsigned long prot;
1355a4679373SChristoph Hellwig 	unsigned long flags;
1356a4679373SChristoph Hellwig 	unsigned long fd;
1357a4679373SChristoph Hellwig 	unsigned long offset;
1358a4679373SChristoph Hellwig };
1359a4679373SChristoph Hellwig 
1360a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1361a4679373SChristoph Hellwig {
1362a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1363a4679373SChristoph Hellwig 
1364a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1365a4679373SChristoph Hellwig 		return -EFAULT;
13661824cb75SAlexander Kuleshov 	if (offset_in_page(a.offset))
1367a4679373SChristoph Hellwig 		return -EINVAL;
1368a4679373SChristoph Hellwig 
1369a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1370a4679373SChristoph Hellwig 			       a.offset >> PAGE_SHIFT);
1371a4679373SChristoph Hellwig }
1372a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1373a4679373SChristoph Hellwig 
13741da177e4SLinus Torvalds /*
13758feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
13768feae131SDavid Howells  * for the first part or the tail.
13771da177e4SLinus Torvalds  */
13788feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
13798feae131SDavid Howells 	      unsigned long addr, int new_below)
13801da177e4SLinus Torvalds {
13818feae131SDavid Howells 	struct vm_area_struct *new;
13828feae131SDavid Howells 	struct vm_region *region;
13838feae131SDavid Howells 	unsigned long npages;
13841da177e4SLinus Torvalds 
1385779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1386779c1023SDavid Howells 	 * only a single usage on the region) */
1387779c1023SDavid Howells 	if (vma->vm_file)
13888feae131SDavid Howells 		return -ENOMEM;
13891da177e4SLinus Torvalds 
13908feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
13918feae131SDavid Howells 		return -ENOMEM;
13921da177e4SLinus Torvalds 
13938feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
13948feae131SDavid Howells 	if (!region)
13958feae131SDavid Howells 		return -ENOMEM;
13968feae131SDavid Howells 
13973928d4f5SLinus Torvalds 	new = vm_area_dup(vma);
13988feae131SDavid Howells 	if (!new) {
13998feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
14008feae131SDavid Howells 		return -ENOMEM;
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds 
14038feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
14048feae131SDavid Howells 	*region = *vma->vm_region;
14058feae131SDavid Howells 	new->vm_region = region;
14068feae131SDavid Howells 
14078feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
14088feae131SDavid Howells 
14098feae131SDavid Howells 	if (new_below) {
1410dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
14118feae131SDavid Howells 	} else {
14128feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
14138feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
14141da177e4SLinus Torvalds 	}
14158feae131SDavid Howells 
14168feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
14178feae131SDavid Howells 		new->vm_ops->open(new);
14188feae131SDavid Howells 
14198feae131SDavid Howells 	delete_vma_from_mm(vma);
14208feae131SDavid Howells 	down_write(&nommu_region_sem);
14218feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
14228feae131SDavid Howells 	if (new_below) {
14238feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
14248feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
14258feae131SDavid Howells 	} else {
14268feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1427dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
14288feae131SDavid Howells 	}
14298feae131SDavid Howells 	add_nommu_region(vma->vm_region);
14308feae131SDavid Howells 	add_nommu_region(new->vm_region);
14318feae131SDavid Howells 	up_write(&nommu_region_sem);
14328feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14338feae131SDavid Howells 	add_vma_to_mm(mm, new);
14348feae131SDavid Howells 	return 0;
14358feae131SDavid Howells }
14368feae131SDavid Howells 
14378feae131SDavid Howells /*
14388feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
14398feae131SDavid Howells  * the end
14408feae131SDavid Howells  */
14418feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
14428feae131SDavid Howells 		      struct vm_area_struct *vma,
14438feae131SDavid Howells 		      unsigned long from, unsigned long to)
14448feae131SDavid Howells {
14458feae131SDavid Howells 	struct vm_region *region;
14468feae131SDavid Howells 
14478feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
14488feae131SDavid Howells 	 * and list */
14498feae131SDavid Howells 	delete_vma_from_mm(vma);
14508feae131SDavid Howells 	if (from > vma->vm_start)
14518feae131SDavid Howells 		vma->vm_end = from;
14528feae131SDavid Howells 	else
14538feae131SDavid Howells 		vma->vm_start = to;
14548feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14558feae131SDavid Howells 
14568feae131SDavid Howells 	/* cut the backing region down to size */
14578feae131SDavid Howells 	region = vma->vm_region;
14581e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
14598feae131SDavid Howells 
14608feae131SDavid Howells 	down_write(&nommu_region_sem);
14618feae131SDavid Howells 	delete_nommu_region(region);
1462dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1463dd8632a1SPaul Mundt 		to = region->vm_top;
1464dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1465dd8632a1SPaul Mundt 	} else {
14668feae131SDavid Howells 		region->vm_start = to;
1467dd8632a1SPaul Mundt 	}
14688feae131SDavid Howells 	add_nommu_region(region);
14698feae131SDavid Howells 	up_write(&nommu_region_sem);
14708feae131SDavid Howells 
14718feae131SDavid Howells 	free_page_series(from, to);
14728feae131SDavid Howells 	return 0;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14753034097aSDavid Howells /*
14763034097aSDavid Howells  * release a mapping
14778feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
14788feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
14793034097aSDavid Howells  */
1480897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
14811da177e4SLinus Torvalds {
14828feae131SDavid Howells 	struct vm_area_struct *vma;
1483f67d9b15SBob Liu 	unsigned long end;
14848feae131SDavid Howells 	int ret;
14851da177e4SLinus Torvalds 
1486f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
14878feae131SDavid Howells 	if (len == 0)
14881da177e4SLinus Torvalds 		return -EINVAL;
14891da177e4SLinus Torvalds 
1490f67d9b15SBob Liu 	end = start + len;
1491f67d9b15SBob Liu 
14928feae131SDavid Howells 	/* find the first potentially overlapping VMA */
14938feae131SDavid Howells 	vma = find_vma(mm, start);
14948feae131SDavid Howells 	if (!vma) {
1495ac714904SChoi Gi-yong 		static int limit;
149633e5d769SDavid Howells 		if (limit < 5) {
149722cc877bSLeon Romanovsky 			pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
149833e5d769SDavid Howells 					current->pid, current->comm,
149933e5d769SDavid Howells 					start, start + len - 1);
150033e5d769SDavid Howells 			limit++;
150133e5d769SDavid Howells 		}
15028feae131SDavid Howells 		return -EINVAL;
15038feae131SDavid Howells 	}
15041da177e4SLinus Torvalds 
15058feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
15068feae131SDavid Howells 	if (vma->vm_file) {
15078feae131SDavid Howells 		do {
150822cc877bSLeon Romanovsky 			if (start > vma->vm_start)
15098feae131SDavid Howells 				return -EINVAL;
15108feae131SDavid Howells 			if (end == vma->vm_end)
15118feae131SDavid Howells 				goto erase_whole_vma;
1512d75a310cSNamhyung Kim 			vma = vma->vm_next;
1513d75a310cSNamhyung Kim 		} while (vma);
15148feae131SDavid Howells 		return -EINVAL;
15158feae131SDavid Howells 	} else {
15168feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
15178feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
15188feae131SDavid Howells 			goto erase_whole_vma;
151922cc877bSLeon Romanovsky 		if (start < vma->vm_start || end > vma->vm_end)
15208feae131SDavid Howells 			return -EINVAL;
15211824cb75SAlexander Kuleshov 		if (offset_in_page(start))
15228feae131SDavid Howells 			return -EINVAL;
15231824cb75SAlexander Kuleshov 		if (end != vma->vm_end && offset_in_page(end))
15248feae131SDavid Howells 			return -EINVAL;
15258feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
15268feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
152722cc877bSLeon Romanovsky 			if (ret < 0)
15288feae131SDavid Howells 				return ret;
15298feae131SDavid Howells 		}
15308feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
15318feae131SDavid Howells 	}
15321da177e4SLinus Torvalds 
15338feae131SDavid Howells erase_whole_vma:
15348feae131SDavid Howells 	delete_vma_from_mm(vma);
15358feae131SDavid Howells 	delete_vma(mm, vma);
15361da177e4SLinus Torvalds 	return 0;
15371da177e4SLinus Torvalds }
1538b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
15391da177e4SLinus Torvalds 
1540bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
15413034097aSDavid Howells {
1542bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
15433034097aSDavid Howells 	int ret;
15443034097aSDavid Howells 
1545*d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
1546897ab3e0SMike Rapoport 	ret = do_munmap(mm, addr, len, NULL);
1547*d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
15483034097aSDavid Howells 	return ret;
15493034097aSDavid Howells }
1550a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1551a46ef99dSLinus Torvalds 
1552a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1553a46ef99dSLinus Torvalds {
1554bfce281cSAl Viro 	return vm_munmap(addr, len);
1555a46ef99dSLinus Torvalds }
15563034097aSDavid Howells 
15573034097aSDavid Howells /*
15588feae131SDavid Howells  * release all the mappings made in a process's VM space
15593034097aSDavid Howells  */
15601da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
15611da177e4SLinus Torvalds {
15628feae131SDavid Howells 	struct vm_area_struct *vma;
15631da177e4SLinus Torvalds 
15648feae131SDavid Howells 	if (!mm)
15658feae131SDavid Howells 		return;
15668feae131SDavid Howells 
15671da177e4SLinus Torvalds 	mm->total_vm = 0;
15681da177e4SLinus Torvalds 
15698feae131SDavid Howells 	while ((vma = mm->mmap)) {
15708feae131SDavid Howells 		mm->mmap = vma->vm_next;
15718feae131SDavid Howells 		delete_vma_from_mm(vma);
15728feae131SDavid Howells 		delete_vma(mm, vma);
157304c34961SSteven J. Magnani 		cond_resched();
15741da177e4SLinus Torvalds 	}
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
15775d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	return -ENOMEM;
15801da177e4SLinus Torvalds }
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds /*
15836fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
15846fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
15851da177e4SLinus Torvalds  *
15866fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
15878feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
15888feae131SDavid Howells  * block is not shareable
15891da177e4SLinus Torvalds  *
15906fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
15911da177e4SLinus Torvalds  */
15924b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
15931da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
15941da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
15951da177e4SLinus Torvalds {
15966fa5f80bSDavid Howells 	struct vm_area_struct *vma;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	/* insanity checks first */
1599f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1600f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
16018feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
16021da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16031da177e4SLinus Torvalds 
16041824cb75SAlexander Kuleshov 	if (offset_in_page(addr))
16058feae131SDavid Howells 		return -EINVAL;
16068feae131SDavid Howells 
16071da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
16081da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16091da177e4SLinus Torvalds 
16108feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
16116fa5f80bSDavid Howells 	if (!vma)
16121da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16131da177e4SLinus Torvalds 
16146fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
16151da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
16161da177e4SLinus Torvalds 
16176fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
16181da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
16191da177e4SLinus Torvalds 
16208feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
16211da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	/* all checks complete - do it */
16246fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
16256fa5f80bSDavid Howells 	return vma->vm_start;
16266fa5f80bSDavid Howells }
16276fa5f80bSDavid Howells 
16286a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
16296a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
16306a6160a7SHeiko Carstens 		unsigned long, new_addr)
16316fa5f80bSDavid Howells {
16326fa5f80bSDavid Howells 	unsigned long ret;
16336fa5f80bSDavid Howells 
1634*d8ed45c5SMichel Lespinasse 	mmap_write_lock(current->mm);
16356fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1636*d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
16376fa5f80bSDavid Howells 	return ret;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
1640df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1641df06b37fSKeith Busch 			 unsigned int foll_flags)
16421da177e4SLinus Torvalds {
16431da177e4SLinus Torvalds 	return NULL;
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
16468f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16478f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
16481da177e4SLinus Torvalds {
16498f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
16508f3b1327SBob Liu 		return -EINVAL;
16518f3b1327SBob Liu 
1652314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
165366aa2b4bSGreg Ungerer 	return 0;
16541da177e4SLinus Torvalds }
165522c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
16561da177e4SLinus Torvalds 
16573c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
16583c0b9de6SLinus Torvalds {
16593c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
16603c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
16613c0b9de6SLinus Torvalds 
16623c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
16633c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
16643c0b9de6SLinus Torvalds }
16653c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
16663c0b9de6SLinus Torvalds 
1667f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1668f905bc44SPaul Mundt 			unsigned long pgoff)
1669f905bc44SPaul Mundt {
1670f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1671f905bc44SPaul Mundt 
1672f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1673f905bc44SPaul Mundt 		return -EINVAL;
1674f905bc44SPaul Mundt 
1675f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1676f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1677f905bc44SPaul Mundt 
1678f905bc44SPaul Mundt 	return 0;
1679f905bc44SPaul Mundt }
1680f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1681f905bc44SPaul Mundt 
16821da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
16831da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
16841da177e4SLinus Torvalds {
16851da177e4SLinus Torvalds 	return -ENOMEM;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16882bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
1689b0e15190SDavid Howells {
1690b0e15190SDavid Howells 	BUG();
1691d0217ac0SNick Piggin 	return 0;
1692b0e15190SDavid Howells }
1693b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
16940ec76a11SDavid Howells 
169582b0f8c3SJan Kara void filemap_map_pages(struct vm_fault *vmf,
1696bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1697f1820361SKirill A. Shutemov {
1698f1820361SKirill A. Shutemov 	BUG();
1699f1820361SKirill A. Shutemov }
1700f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1701f1820361SKirill A. Shutemov 
170284d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1703442486ecSLorenzo Stoakes 		unsigned long addr, void *buf, int len, unsigned int gup_flags)
17040ec76a11SDavid Howells {
17050ec76a11SDavid Howells 	struct vm_area_struct *vma;
1706442486ecSLorenzo Stoakes 	int write = gup_flags & FOLL_WRITE;
17070ec76a11SDavid Howells 
1708*d8ed45c5SMichel Lespinasse 	if (mmap_read_lock_killable(mm))
17091e426fe2SKonstantin Khlebnikov 		return 0;
17100ec76a11SDavid Howells 
17110ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
17120159b141SDavid Howells 	vma = find_vma(mm, addr);
17130159b141SDavid Howells 	if (vma) {
17140ec76a11SDavid Howells 		/* don't overrun this mapping */
17150ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
17160ec76a11SDavid Howells 			len = vma->vm_end - addr;
17170ec76a11SDavid Howells 
17180ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1719d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
17207959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
17217959722bSJie Zhang 					 (void *) addr, buf, len);
1722d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
17237959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
17247959722bSJie Zhang 					    buf, (void *) addr, len);
17250ec76a11SDavid Howells 		else
17260ec76a11SDavid Howells 			len = 0;
17270ec76a11SDavid Howells 	} else {
17280ec76a11SDavid Howells 		len = 0;
17290ec76a11SDavid Howells 	}
17300ec76a11SDavid Howells 
1731*d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
1732f55f199bSMike Frysinger 
1733f55f199bSMike Frysinger 	return len;
1734f55f199bSMike Frysinger }
1735f55f199bSMike Frysinger 
1736f55f199bSMike Frysinger /**
1737b7701a5fSMike Rapoport  * access_remote_vm - access another process' address space
1738f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1739f55f199bSMike Frysinger  * @addr:	start address to access
1740f55f199bSMike Frysinger  * @buf:	source or destination buffer
1741f55f199bSMike Frysinger  * @len:	number of bytes to transfer
17426347e8d5SLorenzo Stoakes  * @gup_flags:	flags modifying lookup behaviour
1743f55f199bSMike Frysinger  *
1744f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1745f55f199bSMike Frysinger  */
1746f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
17476347e8d5SLorenzo Stoakes 		void *buf, int len, unsigned int gup_flags)
1748f55f199bSMike Frysinger {
17496347e8d5SLorenzo Stoakes 	return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
1750f55f199bSMike Frysinger }
1751f55f199bSMike Frysinger 
1752f55f199bSMike Frysinger /*
1753f55f199bSMike Frysinger  * Access another process' address space.
1754f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1755f55f199bSMike Frysinger  */
1756f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1757f307ab6dSLorenzo Stoakes 		unsigned int gup_flags)
1758f55f199bSMike Frysinger {
1759f55f199bSMike Frysinger 	struct mm_struct *mm;
1760f55f199bSMike Frysinger 
1761f55f199bSMike Frysinger 	if (addr + len < addr)
1762f55f199bSMike Frysinger 		return 0;
1763f55f199bSMike Frysinger 
1764f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1765f55f199bSMike Frysinger 	if (!mm)
1766f55f199bSMike Frysinger 		return 0;
1767f55f199bSMike Frysinger 
1768f307ab6dSLorenzo Stoakes 	len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
1769f55f199bSMike Frysinger 
17700ec76a11SDavid Howells 	mmput(mm);
17710ec76a11SDavid Howells 	return len;
17720ec76a11SDavid Howells }
1773fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
17747e660872SDavid Howells 
17757e660872SDavid Howells /**
17767e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
17777e660872SDavid Howells  * @inode: The inode to check
17787e660872SDavid Howells  * @size: The current filesize of the inode
17797e660872SDavid Howells  * @newsize: The proposed filesize of the inode
17807e660872SDavid Howells  *
17817e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
17827e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
17837e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
17847e660872SDavid Howells  * automatically grant mappings that are too large.
17857e660872SDavid Howells  */
17867e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
17877e660872SDavid Howells 				size_t newsize)
17887e660872SDavid Howells {
17897e660872SDavid Howells 	struct vm_area_struct *vma;
17907e660872SDavid Howells 	struct vm_region *region;
17917e660872SDavid Howells 	pgoff_t low, high;
17927e660872SDavid Howells 	size_t r_size, r_top;
17937e660872SDavid Howells 
17947e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
17957e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
17967e660872SDavid Howells 
17977e660872SDavid Howells 	down_write(&nommu_region_sem);
17981acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
17997e660872SDavid Howells 
18007e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
18016b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
18027e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
18037e660872SDavid Howells 		 * cache */
18047e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
18051acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
18067e660872SDavid Howells 			up_write(&nommu_region_sem);
18077e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
18087e660872SDavid Howells 		}
18097e660872SDavid Howells 	}
18107e660872SDavid Howells 
18117e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
18127e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
18137e660872SDavid Howells 	 *
18147e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
18157e660872SDavid Howells 	 * shouldn't be any
18167e660872SDavid Howells 	 */
18171acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
18187e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
18197e660872SDavid Howells 			continue;
18207e660872SDavid Howells 
18217e660872SDavid Howells 		region = vma->vm_region;
18227e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
18237e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
18247e660872SDavid Howells 
18257e660872SDavid Howells 		if (r_top > newsize) {
18267e660872SDavid Howells 			region->vm_top -= r_top - newsize;
18277e660872SDavid Howells 			if (region->vm_end > region->vm_top)
18287e660872SDavid Howells 				region->vm_end = region->vm_top;
18297e660872SDavid Howells 		}
18307e660872SDavid Howells 	}
18317e660872SDavid Howells 
18321acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
18337e660872SDavid Howells 	up_write(&nommu_region_sem);
18347e660872SDavid Howells 	return 0;
18357e660872SDavid Howells }
1836c9b1d098SAndrew Shewmaker 
1837c9b1d098SAndrew Shewmaker /*
1838c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1839c9b1d098SAndrew Shewmaker  *
1840c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1841c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1842c9b1d098SAndrew Shewmaker  * mode.
1843c9b1d098SAndrew Shewmaker  *
1844c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1845c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1846c9b1d098SAndrew Shewmaker  */
1847c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1848c9b1d098SAndrew Shewmaker {
1849c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1850c9b1d098SAndrew Shewmaker 
1851c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1852c9b1d098SAndrew Shewmaker 
1853c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1854c9b1d098SAndrew Shewmaker 	return 0;
1855c9b1d098SAndrew Shewmaker }
1856a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
18574eeab4f5SAndrew Shewmaker 
18584eeab4f5SAndrew Shewmaker /*
18594eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
18604eeab4f5SAndrew Shewmaker  *
18614eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
18624eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
18634eeab4f5SAndrew Shewmaker  *
18644eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
18654eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
18664eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
18674eeab4f5SAndrew Shewmaker  */
18684eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
18694eeab4f5SAndrew Shewmaker {
18704eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
18714eeab4f5SAndrew Shewmaker 
1872c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
18734eeab4f5SAndrew Shewmaker 
18744eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
18754eeab4f5SAndrew Shewmaker 	return 0;
18764eeab4f5SAndrew Shewmaker }
1877a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1878