xref: /linux/mm/nommu.c (revision 1e426fe28261b03f297992e89da3320b42816f4e)
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 	 */
1115a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(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 
143dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
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 
153a7c3e901SMichal Hocko void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
154a7c3e901SMichal Hocko {
155a7c3e901SMichal Hocko 	return __vmalloc(size, flags, PAGE_KERNEL);
156a7c3e901SMichal Hocko }
157a7c3e901SMichal Hocko 
158f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
159f905bc44SPaul Mundt {
160f905bc44SPaul Mundt 	void *ret;
161f905bc44SPaul Mundt 
16219809c2dSMichal Hocko 	ret = __vmalloc(size, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL);
163f905bc44SPaul Mundt 	if (ret) {
164f905bc44SPaul Mundt 		struct vm_area_struct *vma;
165f905bc44SPaul Mundt 
166f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
167f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
168f905bc44SPaul Mundt 		if (vma)
169f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
170f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
171f905bc44SPaul Mundt 	}
172f905bc44SPaul Mundt 
173f905bc44SPaul Mundt 	return ret;
174f905bc44SPaul Mundt }
175f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
176f905bc44SPaul Mundt 
177b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	return virt_to_page(addr);
1801da177e4SLinus Torvalds }
181b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
1821da177e4SLinus Torvalds 
183b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
1861da177e4SLinus Torvalds }
187b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
1901da177e4SLinus Torvalds {
1919bde916bSChen Gang 	/* Don't allow overflow */
1929bde916bSChen Gang 	if ((unsigned long) buf + count < count)
1939bde916bSChen Gang 		count = -(unsigned long) buf;
1949bde916bSChen Gang 
1951da177e4SLinus Torvalds 	memcpy(buf, addr, count);
1961da177e4SLinus Torvalds 	return count;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	/* Don't allow overflow */
2021da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2031da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	memcpy(addr, buf, count);
206ac714904SChoi Gi-yong 	return count;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /*
210e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
2111da177e4SLinus Torvalds  *
2121da177e4SLinus Torvalds  *	@size:		allocation size
2131da177e4SLinus Torvalds  *
2141da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
215e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
2161da177e4SLinus Torvalds  *
217c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2181da177e4SLinus Torvalds  *	use __vmalloc() instead.
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
2231da177e4SLinus Torvalds }
224f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
225f6138882SAndrew Morton 
226e1ca7788SDave Young /*
227e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
228e1ca7788SDave Young  *
229e1ca7788SDave Young  *	@size:		allocation size
230e1ca7788SDave Young  *
231e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
232e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
233e1ca7788SDave Young  *	The memory allocated is set to zero.
234e1ca7788SDave Young  *
235e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
236e1ca7788SDave Young  *	use __vmalloc() instead.
237e1ca7788SDave Young  */
238e1ca7788SDave Young void *vzalloc(unsigned long size)
239e1ca7788SDave Young {
240e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
241e1ca7788SDave Young 			PAGE_KERNEL);
242e1ca7788SDave Young }
243e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
244e1ca7788SDave Young 
245e1ca7788SDave Young /**
246e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
247e1ca7788SDave Young  * @size:	allocation size
248e1ca7788SDave Young  * @node:	numa node
249e1ca7788SDave Young  *
250e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
251e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
252e1ca7788SDave Young  *
253e1ca7788SDave Young  * For tight control over page level allocator and protection flags
254e1ca7788SDave Young  * use __vmalloc() instead.
255e1ca7788SDave Young  */
256f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
257f6138882SAndrew Morton {
258f6138882SAndrew Morton 	return vmalloc(size);
259f6138882SAndrew Morton }
2609a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
261e1ca7788SDave Young 
262e1ca7788SDave Young /**
263e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
264e1ca7788SDave Young  * @size:	allocation size
265e1ca7788SDave Young  * @node:	numa node
266e1ca7788SDave Young  *
267e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
268e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
269e1ca7788SDave Young  * The memory allocated is set to zero.
270e1ca7788SDave Young  *
271e1ca7788SDave Young  * For tight control over page level allocator and protection flags
272e1ca7788SDave Young  * use __vmalloc() instead.
273e1ca7788SDave Young  */
274e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
275e1ca7788SDave Young {
276e1ca7788SDave Young 	return vzalloc(size);
277e1ca7788SDave Young }
278e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2791da177e4SLinus Torvalds 
2801af446edSPaul Mundt /**
2811af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
2821af446edSPaul Mundt  *	@size:		allocation size
2831af446edSPaul Mundt  *
2841af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
2851af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
2861af446edSPaul Mundt  *	executable kernel virtual space.
2871af446edSPaul Mundt  *
2881af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
2891af446edSPaul Mundt  *	use __vmalloc() instead.
2901af446edSPaul Mundt  */
2911af446edSPaul Mundt 
2921af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
2931af446edSPaul Mundt {
2941af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
2951af446edSPaul Mundt }
2961af446edSPaul Mundt 
297b5073173SPaul Mundt /**
298b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
2991da177e4SLinus Torvalds  *	@size:		allocation size
3001da177e4SLinus Torvalds  *
3011da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
302e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
3071da177e4SLinus Torvalds }
308b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
309b5073173SPaul Mundt 
310b5073173SPaul Mundt /**
311b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
312b5073173SPaul Mundt  *	@size:		allocation size
313b5073173SPaul Mundt  *
314b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
315b5073173SPaul Mundt  * mapped to userspace without leaking data.
316f905bc44SPaul Mundt  *
317f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
318f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
319b5073173SPaul Mundt  */
320b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
321b5073173SPaul Mundt {
322f905bc44SPaul Mundt 	/*
323f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
324f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
325f905bc44SPaul Mundt 	 */
326f905bc44SPaul Mundt 	return vmalloc_user(size);
327b5073173SPaul Mundt }
328b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	BUG();
3331da177e4SLinus Torvalds 	return NULL;
3341da177e4SLinus Torvalds }
335b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3361da177e4SLinus Torvalds 
337b3bdda02SChristoph Lameter void vunmap(const void *addr)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	BUG();
3401da177e4SLinus Torvalds }
341b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3421da177e4SLinus Torvalds 
343eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
344eb6434d9SPaul Mundt {
345eb6434d9SPaul Mundt 	BUG();
346eb6434d9SPaul Mundt 	return NULL;
347eb6434d9SPaul Mundt }
348eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
349eb6434d9SPaul Mundt 
350eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
351eb6434d9SPaul Mundt {
352eb6434d9SPaul Mundt 	BUG();
353eb6434d9SPaul Mundt }
354eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
355eb6434d9SPaul Mundt 
356eb6434d9SPaul Mundt void vm_unmap_aliases(void)
357eb6434d9SPaul Mundt {
358eb6434d9SPaul Mundt }
359eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
360eb6434d9SPaul Mundt 
3611da177e4SLinus Torvalds /*
3621eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
3631eeb66a1SChristoph Hellwig  * have one.
3641eeb66a1SChristoph Hellwig  */
3653b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
3661eeb66a1SChristoph Hellwig {
3671eeb66a1SChristoph Hellwig }
3681eeb66a1SChristoph Hellwig 
369cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
37029c185e5SPaul Mundt {
37129c185e5SPaul Mundt 	BUG();
37229c185e5SPaul Mundt 	return NULL;
37329c185e5SPaul Mundt }
37429c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
37529c185e5SPaul Mundt 
37629c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
37729c185e5SPaul Mundt {
37829c185e5SPaul Mundt 	BUG();
37929c185e5SPaul Mundt }
38029c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
38129c185e5SPaul Mundt 
382b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
383b5073173SPaul Mundt 		   struct page *page)
384b5073173SPaul Mundt {
385b5073173SPaul Mundt 	return -EINVAL;
386b5073173SPaul Mundt }
387b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
388b5073173SPaul Mundt 
389a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
390a667d745SSouptick Joarder 			unsigned long num)
391a667d745SSouptick Joarder {
392a667d745SSouptick Joarder 	return -EINVAL;
393a667d745SSouptick Joarder }
394a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages);
395a667d745SSouptick Joarder 
396a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
397a667d745SSouptick Joarder 				unsigned long num)
398a667d745SSouptick Joarder {
399a667d745SSouptick Joarder 	return -EINVAL;
400a667d745SSouptick Joarder }
401a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero);
402a667d745SSouptick Joarder 
4031eeb66a1SChristoph Hellwig /*
4041da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4051da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4061da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4071da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4081da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4091da177e4SLinus Torvalds  */
4106a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
4151da177e4SLinus Torvalds 		return mm->brk;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (mm->brk == brk)
4181da177e4SLinus Torvalds 		return mm->brk;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	/*
4211da177e4SLinus Torvalds 	 * Always allow shrinking brk
4221da177e4SLinus Torvalds 	 */
4231da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4241da177e4SLinus Torvalds 		mm->brk = brk;
4251da177e4SLinus Torvalds 		return brk;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	/*
4291da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4301da177e4SLinus Torvalds 	 */
431cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
4321da177e4SLinus Torvalds 	return mm->brk = brk;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4358feae131SDavid Howells /*
4363edf41d8Sseokhoon.yoon  * initialise the percpu counter for VM and region record slabs
4378feae131SDavid Howells  */
4388feae131SDavid Howells void __init mmap_init(void)
4391da177e4SLinus Torvalds {
44000a62ce9SKOSAKI Motohiro 	int ret;
44100a62ce9SKOSAKI Motohiro 
442908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
44300a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
4445d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
4458feae131SDavid Howells }
4461da177e4SLinus Torvalds 
4478feae131SDavid Howells /*
4488feae131SDavid Howells  * validate the region tree
4498feae131SDavid Howells  * - the caller must hold the region lock
4508feae131SDavid Howells  */
4518feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4528feae131SDavid Howells static noinline void validate_nommu_regions(void)
4538feae131SDavid Howells {
4548feae131SDavid Howells 	struct vm_region *region, *last;
4558feae131SDavid Howells 	struct rb_node *p, *lastp;
4561da177e4SLinus Torvalds 
4578feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
4588feae131SDavid Howells 	if (!lastp)
4598feae131SDavid Howells 		return;
4608feae131SDavid Howells 
4618feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
462c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
463c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
4648feae131SDavid Howells 
4658feae131SDavid Howells 	while ((p = rb_next(lastp))) {
4668feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
4678feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
4688feae131SDavid Howells 
469c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
470c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
471c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
4728feae131SDavid Howells 
4738feae131SDavid Howells 		lastp = p;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds }
4768feae131SDavid Howells #else
47733e5d769SDavid Howells static void validate_nommu_regions(void)
47833e5d769SDavid Howells {
47933e5d769SDavid Howells }
4808feae131SDavid Howells #endif
4818feae131SDavid Howells 
4828feae131SDavid Howells /*
4838feae131SDavid Howells  * add a region into the global tree
4848feae131SDavid Howells  */
4858feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4868feae131SDavid Howells {
4878feae131SDavid Howells 	struct vm_region *pregion;
4888feae131SDavid Howells 	struct rb_node **p, *parent;
4898feae131SDavid Howells 
4908feae131SDavid Howells 	validate_nommu_regions();
4918feae131SDavid Howells 
4928feae131SDavid Howells 	parent = NULL;
4938feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
4948feae131SDavid Howells 	while (*p) {
4958feae131SDavid Howells 		parent = *p;
4968feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
4978feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
4988feae131SDavid Howells 			p = &(*p)->rb_left;
4998feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5008feae131SDavid Howells 			p = &(*p)->rb_right;
5018feae131SDavid Howells 		else if (pregion == region)
5028feae131SDavid Howells 			return;
5038feae131SDavid Howells 		else
5048feae131SDavid Howells 			BUG();
5058feae131SDavid Howells 	}
5068feae131SDavid Howells 
5078feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5088feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5098feae131SDavid Howells 
5108feae131SDavid Howells 	validate_nommu_regions();
5118feae131SDavid Howells }
5128feae131SDavid Howells 
5138feae131SDavid Howells /*
5148feae131SDavid Howells  * delete a region from the global tree
5158feae131SDavid Howells  */
5168feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
5178feae131SDavid Howells {
5188feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5198feae131SDavid Howells 
5208feae131SDavid Howells 	validate_nommu_regions();
5218feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5228feae131SDavid Howells 	validate_nommu_regions();
5238feae131SDavid Howells }
5248feae131SDavid Howells 
5258feae131SDavid Howells /*
5268feae131SDavid Howells  * free a contiguous series of pages
5278feae131SDavid Howells  */
5288feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5298feae131SDavid Howells {
5308feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5318feae131SDavid Howells 		struct page *page = virt_to_page(from);
5328feae131SDavid Howells 
53333e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5348feae131SDavid Howells 		put_page(page);
5358feae131SDavid Howells 	}
5368feae131SDavid Howells }
5378feae131SDavid Howells 
5388feae131SDavid Howells /*
5398feae131SDavid Howells  * release a reference to a region
54033e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
541dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5428feae131SDavid Howells  *   will equal vm_start
5438feae131SDavid Howells  */
5448feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5458feae131SDavid Howells 	__releases(nommu_region_sem)
5468feae131SDavid Howells {
5478feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5488feae131SDavid Howells 
5491e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
550dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
5518feae131SDavid Howells 			delete_nommu_region(region);
5528feae131SDavid Howells 		up_write(&nommu_region_sem);
5538feae131SDavid Howells 
5548feae131SDavid Howells 		if (region->vm_file)
5558feae131SDavid Howells 			fput(region->vm_file);
5568feae131SDavid Howells 
5578feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
5588feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
55922cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
560dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
5618feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
5628feae131SDavid Howells 	} else {
5638feae131SDavid Howells 		up_write(&nommu_region_sem);
5648feae131SDavid Howells 	}
5658feae131SDavid Howells }
5668feae131SDavid Howells 
5678feae131SDavid Howells /*
5688feae131SDavid Howells  * release a reference to a region
5698feae131SDavid Howells  */
5708feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
5718feae131SDavid Howells {
5728feae131SDavid Howells 	down_write(&nommu_region_sem);
5738feae131SDavid Howells 	__put_nommu_region(region);
5748feae131SDavid Howells }
5751da177e4SLinus Torvalds 
5763034097aSDavid Howells /*
5773034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
5788feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
5798feae131SDavid Howells  * page
5803034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
5813034097aSDavid Howells  */
5828feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
5833034097aSDavid Howells {
5846038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
5858feae131SDavid Howells 	struct address_space *mapping;
5866038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
5873034097aSDavid Howells 
5888feae131SDavid Howells 	BUG_ON(!vma->vm_region);
5898feae131SDavid Howells 
5908feae131SDavid Howells 	mm->map_count++;
5918feae131SDavid Howells 	vma->vm_mm = mm;
5928feae131SDavid Howells 
5938feae131SDavid Howells 	/* add the VMA to the mapping */
5948feae131SDavid Howells 	if (vma->vm_file) {
5958feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
5968feae131SDavid Howells 
59783cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
5988feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
5996b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
6008feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
60183cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6028feae131SDavid Howells 	}
6038feae131SDavid Howells 
6048feae131SDavid Howells 	/* add the VMA to the tree */
6056038def0SNamhyung Kim 	parent = rb_prev = NULL;
6068feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
6078feae131SDavid Howells 	while (*p) {
6088feae131SDavid Howells 		parent = *p;
6098feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
6108feae131SDavid Howells 
6118feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
6128feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
6138feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
6148feae131SDavid Howells 			p = &(*p)->rb_left;
6156038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
6166038def0SNamhyung Kim 			rb_prev = parent;
6178feae131SDavid Howells 			p = &(*p)->rb_right;
6186038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
6198feae131SDavid Howells 			p = &(*p)->rb_left;
6206038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
6216038def0SNamhyung Kim 			rb_prev = parent;
6228feae131SDavid Howells 			p = &(*p)->rb_right;
6236038def0SNamhyung Kim 		} else if (vma < pvma)
6248feae131SDavid Howells 			p = &(*p)->rb_left;
6256038def0SNamhyung Kim 		else if (vma > pvma) {
6266038def0SNamhyung Kim 			rb_prev = parent;
6278feae131SDavid Howells 			p = &(*p)->rb_right;
6286038def0SNamhyung Kim 		} else
6298feae131SDavid Howells 			BUG();
6308feae131SDavid Howells 	}
6318feae131SDavid Howells 
6328feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
6338feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
6348feae131SDavid Howells 
6358feae131SDavid Howells 	/* add VMA to the VMA list also */
6366038def0SNamhyung Kim 	prev = NULL;
6376038def0SNamhyung Kim 	if (rb_prev)
6386038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
6393034097aSDavid Howells 
6406038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
6418feae131SDavid Howells }
6428feae131SDavid Howells 
6438feae131SDavid Howells /*
6448feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
6458feae131SDavid Howells  */
6468feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
6478feae131SDavid Howells {
648615d6e87SDavidlohr Bueso 	int i;
6498feae131SDavid Howells 	struct address_space *mapping;
6508feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
651615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
6528feae131SDavid Howells 
6538feae131SDavid Howells 	mm->map_count--;
654615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
655615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
656314ff785SIngo Molnar 		if (curr->vmacache.vmas[i] == vma) {
657e020d5bdSSteven Miao 			vmacache_invalidate(mm);
658615d6e87SDavidlohr Bueso 			break;
659615d6e87SDavidlohr Bueso 		}
660615d6e87SDavidlohr Bueso 	}
6618feae131SDavid Howells 
6628feae131SDavid Howells 	/* remove the VMA from the mapping */
6638feae131SDavid Howells 	if (vma->vm_file) {
6648feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6658feae131SDavid Howells 
66683cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6678feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6686b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
6698feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
67083cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6718feae131SDavid Howells 	}
6728feae131SDavid Howells 
6738feae131SDavid Howells 	/* remove from the MM's tree and list */
6748feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
675b951bf2cSNamhyung Kim 
676b951bf2cSNamhyung Kim 	if (vma->vm_prev)
677b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
678b951bf2cSNamhyung Kim 	else
679b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
680b951bf2cSNamhyung Kim 
681b951bf2cSNamhyung Kim 	if (vma->vm_next)
682b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
6838feae131SDavid Howells }
6848feae131SDavid Howells 
6858feae131SDavid Howells /*
6868feae131SDavid Howells  * destroy a VMA record
6878feae131SDavid Howells  */
6888feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
6898feae131SDavid Howells {
6908feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
6918feae131SDavid Howells 		vma->vm_ops->close(vma);
692e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
6938feae131SDavid Howells 		fput(vma->vm_file);
6948feae131SDavid Howells 	put_nommu_region(vma->vm_region);
6953928d4f5SLinus Torvalds 	vm_area_free(vma);
6963034097aSDavid Howells }
6973034097aSDavid Howells 
6983034097aSDavid Howells /*
6993034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
7003034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7013034097aSDavid Howells  */
7023034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
7033034097aSDavid Howells {
7048feae131SDavid Howells 	struct vm_area_struct *vma;
7053034097aSDavid Howells 
7068feae131SDavid Howells 	/* check the cache first */
707615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
708615d6e87SDavidlohr Bueso 	if (likely(vma))
7098feae131SDavid Howells 		return vma;
7108feae131SDavid Howells 
711e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7128feae131SDavid Howells 	 * resides) */
713e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7148feae131SDavid Howells 		if (vma->vm_start > addr)
7158feae131SDavid Howells 			return NULL;
7168feae131SDavid Howells 		if (vma->vm_end > addr) {
717615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7188feae131SDavid Howells 			return vma;
7193034097aSDavid Howells 		}
7208feae131SDavid Howells 	}
7213034097aSDavid Howells 
7223034097aSDavid Howells 	return NULL;
7233034097aSDavid Howells }
7243034097aSDavid Howells EXPORT_SYMBOL(find_vma);
7253034097aSDavid Howells 
7263034097aSDavid Howells /*
727930e652aSDavid Howells  * find a VMA
728930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
729930e652aSDavid Howells  */
730930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
731930e652aSDavid Howells {
7327561e8caSDavid Howells 	return find_vma(mm, addr);
733930e652aSDavid Howells }
734930e652aSDavid Howells 
7358feae131SDavid Howells /*
7368feae131SDavid Howells  * expand a stack to a given address
7378feae131SDavid Howells  * - not supported under NOMMU conditions
7388feae131SDavid Howells  */
73957c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
74057c8f63eSGreg Ungerer {
74157c8f63eSGreg Ungerer 	return -ENOMEM;
74257c8f63eSGreg Ungerer }
74357c8f63eSGreg Ungerer 
744930e652aSDavid Howells /*
7456fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
7466fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7476fa5f80bSDavid Howells  */
7488feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
7498feae131SDavid Howells 					     unsigned long addr,
7508feae131SDavid Howells 					     unsigned long len)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	struct vm_area_struct *vma;
7538feae131SDavid Howells 	unsigned long end = addr + len;
7541da177e4SLinus Torvalds 
7558feae131SDavid Howells 	/* check the cache first */
756615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
757615d6e87SDavidlohr Bueso 	if (vma)
7581da177e4SLinus Torvalds 		return vma;
7598feae131SDavid Howells 
760e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7618feae131SDavid Howells 	 * resides) */
762e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7638feae131SDavid Howells 		if (vma->vm_start < addr)
7648feae131SDavid Howells 			continue;
7658feae131SDavid Howells 		if (vma->vm_start > addr)
7668feae131SDavid Howells 			return NULL;
7678feae131SDavid Howells 		if (vma->vm_end == end) {
768615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7698feae131SDavid Howells 			return vma;
7708feae131SDavid Howells 		}
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	return NULL;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7763034097aSDavid Howells /*
7771da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
7781da177e4SLinus Torvalds  * mapping we're capable of supporting
7791da177e4SLinus Torvalds  */
7801da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
7811da177e4SLinus Torvalds 				 unsigned long addr,
7821da177e4SLinus Torvalds 				 unsigned long len,
7831da177e4SLinus Torvalds 				 unsigned long prot,
7841da177e4SLinus Torvalds 				 unsigned long flags,
7851da177e4SLinus Torvalds 				 unsigned long pgoff,
7861da177e4SLinus Torvalds 				 unsigned long *_capabilities)
7871da177e4SLinus Torvalds {
7888feae131SDavid Howells 	unsigned long capabilities, rlen;
7891da177e4SLinus Torvalds 	int ret;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/* do the simple checks first */
79222cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
7931da177e4SLinus Torvalds 		return -EINVAL;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
7961da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
7971da177e4SLinus Torvalds 		return -EINVAL;
7981da177e4SLinus Torvalds 
799f81cff0dSMike Frysinger 	if (!len)
8001da177e4SLinus Torvalds 		return -EINVAL;
8011da177e4SLinus Torvalds 
802f81cff0dSMike Frysinger 	/* Careful about overflows.. */
8038feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
8048feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
805f81cff0dSMike Frysinger 		return -ENOMEM;
806f81cff0dSMike Frysinger 
8071da177e4SLinus Torvalds 	/* offset overflow? */
8088feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
809f81cff0dSMike Frysinger 		return -EOVERFLOW;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	if (file) {
8121da177e4SLinus Torvalds 		/* files must support mmap */
81372c2d531SAl Viro 		if (!file->f_op->mmap)
8141da177e4SLinus Torvalds 			return -ENODEV;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
8171da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
8181da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
8191da177e4SLinus Torvalds 		 */
820b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
821b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
822b4caecd4SChristoph Hellwig 		} else {
8231da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
8241da177e4SLinus Torvalds 			 * defaults */
825496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
8261da177e4SLinus Torvalds 			case S_IFREG:
8271da177e4SLinus Torvalds 			case S_IFBLK:
828b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
8291da177e4SLinus Torvalds 				break;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 			case S_IFCHR:
8321da177e4SLinus Torvalds 				capabilities =
833b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
834b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
835b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
8361da177e4SLinus Torvalds 				break;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 			default:
8391da177e4SLinus Torvalds 				return -EINVAL;
8401da177e4SLinus Torvalds 			}
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
8441da177e4SLinus Torvalds 		 * device */
8451da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
846b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
8476e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
848b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
8491da177e4SLinus Torvalds 
85028d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
85128d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
85228d7a6aeSGraff Yang 			return -EACCES;
85328d7a6aeSGraff Yang 
8541da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
8551da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
8561da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
8571da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
8581da177e4SLinus Torvalds 				return -EACCES;
8591da177e4SLinus Torvalds 
860496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
8611da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
8621da177e4SLinus Torvalds 				return -EACCES;
8631da177e4SLinus Torvalds 
864d7a06983SJeff Layton 			if (locks_verify_locked(file))
8651da177e4SLinus Torvalds 				return -EAGAIN;
8661da177e4SLinus Torvalds 
867b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
8681da177e4SLinus Torvalds 				return -ENODEV;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
871b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
872ac714904SChoi Gi-yong 		} else {
8731da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
8741da177e4SLinus Torvalds 			 * allocate */
875b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
8761da177e4SLinus Torvalds 				return -ENODEV;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
8791da177e4SLinus Torvalds 			 * shared with the backing device */
8801da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
881b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8821da177e4SLinus Torvalds 		}
8831da177e4SLinus Torvalds 
884b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
885b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
886b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
887b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
8883c7b2045SBernd Schmidt 			    ) {
889b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8903c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
89122cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
8923c7b2045SBernd Schmidt 					return -EINVAL;
8933c7b2045SBernd Schmidt 				}
8943c7b2045SBernd Schmidt 			}
8953c7b2045SBernd Schmidt 		}
8963c7b2045SBernd Schmidt 
8971da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
8981da177e4SLinus Torvalds 		 * mappings */
89990f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
9001da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
9011da177e4SLinus Torvalds 				return -EPERM;
902ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
9031da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
9041da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
905b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
9061da177e4SLinus Torvalds 					prot |= PROT_EXEC;
9071da177e4SLinus Torvalds 			}
908ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
9091da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
910b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
9111da177e4SLinus Torvalds 			 ) {
9121da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
913b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
9141da177e4SLinus Torvalds 		}
915ac714904SChoi Gi-yong 	} else {
9161da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
9171da177e4SLinus Torvalds 		 * privately mapped
9181da177e4SLinus Torvalds 		 */
919b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
9221da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
9231da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
9241da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	/* allow the security API to have its say */
928e5467859SAl Viro 	ret = security_mmap_addr(addr);
929e5467859SAl Viro 	if (ret < 0)
930e5467859SAl Viro 		return ret;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	/* looks okay */
9331da177e4SLinus Torvalds 	*_capabilities = capabilities;
9341da177e4SLinus Torvalds 	return 0;
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds /*
9381da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
9391da177e4SLinus Torvalds  * now know into VMA flags
9401da177e4SLinus Torvalds  */
9411da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
9421da177e4SLinus Torvalds 					unsigned long prot,
9431da177e4SLinus Torvalds 					unsigned long flags,
9441da177e4SLinus Torvalds 					unsigned long capabilities)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	unsigned long vm_flags;
9471da177e4SLinus Torvalds 
948e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
9491da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
9501da177e4SLinus Torvalds 
951b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
9521da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
9533c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9541da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
9551da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
9563c7b2045SBernd Schmidt 	} else {
9571da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
9581da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
9591da177e4SLinus Torvalds 		 * romfs/cramfs */
960b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
9611da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
9623c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
9661da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
9671da177e4SLinus Torvalds 	 * with another untraced process
9681da177e4SLinus Torvalds 	 */
969a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
9701da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	return vm_flags;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds /*
9768feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
9778feae131SDavid Howells  * pins the storage)
9781da177e4SLinus Torvalds  */
9798feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	int ret;
9821da177e4SLinus Torvalds 
983f74ac015SMiklos Szeredi 	ret = call_mmap(vma->vm_file, vma);
984dd8632a1SPaul Mundt 	if (ret == 0) {
985dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
986645d83c5SDavid Howells 		return 0;
987dd8632a1SPaul Mundt 	}
9881da177e4SLinus Torvalds 	if (ret != -ENOSYS)
9891da177e4SLinus Torvalds 		return ret;
9901da177e4SLinus Torvalds 
9913fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
9923fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
9933fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
9941da177e4SLinus Torvalds 	return -ENODEV;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds /*
9981da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
9991da177e4SLinus Torvalds  */
10008feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
10018feae131SDavid Howells 			   struct vm_region *region,
1002645d83c5SDavid Howells 			   unsigned long len,
1003645d83c5SDavid Howells 			   unsigned long capabilities)
10041da177e4SLinus Torvalds {
1005dbc8358cSJoonsoo Kim 	unsigned long total, point;
10061da177e4SLinus Torvalds 	void *base;
10078feae131SDavid Howells 	int ret, order;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
10101da177e4SLinus Torvalds 	 * shared mappings on devices or memory
10111da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
10121da177e4SLinus Torvalds 	 */
1013b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
1014f74ac015SMiklos Szeredi 		ret = call_mmap(vma->vm_file, vma);
1015dd8632a1SPaul Mundt 		if (ret == 0) {
10161da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1017dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1018dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1019645d83c5SDavid Howells 			return 0;
10201da177e4SLinus Torvalds 		}
1021dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1022dd8632a1SPaul Mundt 			return ret;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
10251da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
10261da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 
10298feae131SDavid Howells 
10301da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
10311da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
10321da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
10331da177e4SLinus Torvalds 	 */
1034f67d9b15SBob Liu 	order = get_order(len);
10358feae131SDavid Howells 	total = 1 << order;
1036f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1037dd8632a1SPaul Mundt 
1038dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
103922cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1040dbc8358cSJoonsoo Kim 		total = point;
10418feae131SDavid Howells 
1042da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1043dbc8358cSJoonsoo Kim 	if (!base)
1044dbc8358cSJoonsoo Kim 		goto enomem;
10458feae131SDavid Howells 
1046dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1047dbc8358cSJoonsoo Kim 
10488feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
10498feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1050f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1051dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
10528feae131SDavid Howells 
10538feae131SDavid Howells 	vma->vm_start = region->vm_start;
10548feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	if (vma->vm_file) {
10571da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
10581da177e4SLinus Torvalds 		loff_t fpos;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
10611da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
10621da177e4SLinus Torvalds 
1063b4bf802aSChristoph Hellwig 		ret = kernel_read(vma->vm_file, base, len, &fpos);
10641da177e4SLinus Torvalds 		if (ret < 0)
10651da177e4SLinus Torvalds 			goto error_free;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 		/* clear the last little bit */
1068f67d9b15SBob Liu 		if (ret < len)
1069f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
10701da177e4SLinus Torvalds 
1071bfd40eafSKirill A. Shutemov 	} else {
1072bfd40eafSKirill A. Shutemov 		vma_set_anonymous(vma);
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	return 0;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds error_free:
10787223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
10798feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
10808feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1081dd8632a1SPaul Mundt 	region->vm_top   = 0;
10821da177e4SLinus Torvalds 	return ret;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds enomem:
1085b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
108605ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
10879af744d7SMichal Hocko 	show_free_areas(0, NULL);
10881da177e4SLinus Torvalds 	return -ENOMEM;
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds /*
10921da177e4SLinus Torvalds  * handle mapping creation for uClinux
10931da177e4SLinus Torvalds  */
10941fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
10951da177e4SLinus Torvalds 			unsigned long addr,
10961da177e4SLinus Torvalds 			unsigned long len,
10971da177e4SLinus Torvalds 			unsigned long prot,
10981da177e4SLinus Torvalds 			unsigned long flags,
10991fcfd8dbSOleg Nesterov 			vm_flags_t vm_flags,
1100bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
1101897ab3e0SMike Rapoport 			unsigned long *populate,
1102897ab3e0SMike Rapoport 			struct list_head *uf)
11031da177e4SLinus Torvalds {
11048feae131SDavid Howells 	struct vm_area_struct *vma;
11058feae131SDavid Howells 	struct vm_region *region;
11061da177e4SLinus Torvalds 	struct rb_node *rb;
11071fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
11081da177e4SLinus Torvalds 	int ret;
11091da177e4SLinus Torvalds 
111041badc15SMichel Lespinasse 	*populate = 0;
1111bebeb3d6SMichel Lespinasse 
11121da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
11131da177e4SLinus Torvalds 	 * mapping */
11141da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
11151da177e4SLinus Torvalds 				    &capabilities);
111622cc877bSLeon Romanovsky 	if (ret < 0)
11171da177e4SLinus Torvalds 		return ret;
11181da177e4SLinus Torvalds 
111906aab5a3SDavid Howells 	/* we ignore the address hint */
112006aab5a3SDavid Howells 	addr = 0;
1121f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
112206aab5a3SDavid Howells 
11231da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
11241da177e4SLinus Torvalds 	 * now know into VMA flags */
11251fcfd8dbSOleg Nesterov 	vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
11261da177e4SLinus Torvalds 
11278feae131SDavid Howells 	/* we're going to need to record the mapping */
11288feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
11298feae131SDavid Howells 	if (!region)
11308feae131SDavid Howells 		goto error_getting_region;
11311da177e4SLinus Torvalds 
1132490fc053SLinus Torvalds 	vma = vm_area_alloc(current->mm);
11338feae131SDavid Howells 	if (!vma)
11348feae131SDavid Howells 		goto error_getting_vma;
11351da177e4SLinus Torvalds 
11361e2ae599SDavid Howells 	region->vm_usage = 1;
11378feae131SDavid Howells 	region->vm_flags = vm_flags;
11388feae131SDavid Howells 	region->vm_pgoff = pgoff;
11398feae131SDavid Howells 
11408feae131SDavid Howells 	vma->vm_flags = vm_flags;
11418feae131SDavid Howells 	vma->vm_pgoff = pgoff;
11428feae131SDavid Howells 
11438feae131SDavid Howells 	if (file) {
1144cb0942b8SAl Viro 		region->vm_file = get_file(file);
1145cb0942b8SAl Viro 		vma->vm_file = get_file(file);
11468feae131SDavid Howells 	}
11478feae131SDavid Howells 
11488feae131SDavid Howells 	down_write(&nommu_region_sem);
11498feae131SDavid Howells 
11508feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
11511da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
11528feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
11531da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
11541da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
11551da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
11561da177e4SLinus Torvalds 	 *   than here
11571da177e4SLinus Torvalds 	 */
11581da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
11598feae131SDavid Howells 		struct vm_region *pregion;
11608feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
11611da177e4SLinus Torvalds 
11628feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
11638feae131SDavid Howells 		pgend = pgoff + pglen;
1164165b2392SDavid Howells 
11658feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
11668feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
11671da177e4SLinus Torvalds 
11688feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
11691da177e4SLinus Torvalds 				continue;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1172496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1173496ad9aaSAl Viro 			    file_inode(file))
11741da177e4SLinus Torvalds 				continue;
11751da177e4SLinus Torvalds 
11768feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
11771da177e4SLinus Torvalds 				continue;
11781da177e4SLinus Torvalds 
11798feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
11808feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
11818feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
11828feae131SDavid Howells 			if (pgoff >= rpgend)
11831da177e4SLinus Torvalds 				continue;
11841da177e4SLinus Torvalds 
11858feae131SDavid Howells 			/* handle inexactly overlapping matches between
11868feae131SDavid Howells 			 * mappings */
11878feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
11888feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
11898feae131SDavid Howells 				/* new mapping is not a subset of the region */
1190b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
11911da177e4SLinus Torvalds 					goto sharing_violation;
11921da177e4SLinus Torvalds 				continue;
11931da177e4SLinus Torvalds 			}
11941da177e4SLinus Torvalds 
11958feae131SDavid Howells 			/* we've found a region we can share */
11961e2ae599SDavid Howells 			pregion->vm_usage++;
11978feae131SDavid Howells 			vma->vm_region = pregion;
11988feae131SDavid Howells 			start = pregion->vm_start;
11998feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
12008feae131SDavid Howells 			vma->vm_start = start;
12018feae131SDavid Howells 			vma->vm_end = start + len;
12021da177e4SLinus Torvalds 
120322cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
12048feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
120522cc877bSLeon Romanovsky 			else {
12068feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
12078feae131SDavid Howells 				if (ret < 0) {
12088feae131SDavid Howells 					vma->vm_region = NULL;
12098feae131SDavid Howells 					vma->vm_start = 0;
12108feae131SDavid Howells 					vma->vm_end = 0;
12111e2ae599SDavid Howells 					pregion->vm_usage--;
12128feae131SDavid Howells 					pregion = NULL;
12138feae131SDavid Howells 					goto error_just_free;
12141da177e4SLinus Torvalds 				}
12158feae131SDavid Howells 			}
12168feae131SDavid Howells 			fput(region->vm_file);
12178feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
12188feae131SDavid Howells 			region = pregion;
12198feae131SDavid Howells 			result = start;
12208feae131SDavid Howells 			goto share;
12218feae131SDavid Howells 		}
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
12241da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
12251da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
12261da177e4SLinus Torvalds 		 */
1227b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
12281da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
12291da177e4SLinus Torvalds 							     pgoff, flags);
1230bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
12311da177e4SLinus Torvalds 				ret = addr;
1232bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
12338feae131SDavid Howells 					goto error_just_free;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
12361da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
12371da177e4SLinus Torvalds 				 * it */
1238bb005a59SNamhyung Kim 				ret = -ENODEV;
1239b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
12408feae131SDavid Howells 					goto error_just_free;
12411da177e4SLinus Torvalds 
1242b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
12438feae131SDavid Howells 			} else {
12448feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
12458feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
12461da177e4SLinus Torvalds 			}
12471da177e4SLinus Torvalds 		}
12481da177e4SLinus Torvalds 	}
12491da177e4SLinus Torvalds 
12508feae131SDavid Howells 	vma->vm_region = region;
12511da177e4SLinus Torvalds 
1252645d83c5SDavid Howells 	/* set up the mapping
1253b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1254645d83c5SDavid Howells 	 */
12551da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
12568feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
12571da177e4SLinus Torvalds 	else
1258645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
12591da177e4SLinus Torvalds 	if (ret < 0)
1260645d83c5SDavid Howells 		goto error_just_free;
1261645d83c5SDavid Howells 	add_nommu_region(region);
12628feae131SDavid Howells 
1263ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1264ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1265ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1266ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1267ea637639SJie Zhang 
12681da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
12698feae131SDavid Howells 	result = vma->vm_start;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
12721da177e4SLinus Torvalds 
12738feae131SDavid Howells share:
12748feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
12751da177e4SLinus Torvalds 
1276cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1277cfe79c00SMike Frysinger 	 * mapping of it is made  */
1278cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1279cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1280cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1281cfe79c00SMike Frysinger 	}
12821da177e4SLinus Torvalds 
1283cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
12841da177e4SLinus Torvalds 
12858feae131SDavid Howells 	return result;
12861da177e4SLinus Torvalds 
12878feae131SDavid Howells error_just_free:
12888feae131SDavid Howells 	up_write(&nommu_region_sem);
12898feae131SDavid Howells error:
129089a86402SDavid Howells 	if (region->vm_file)
12918feae131SDavid Howells 		fput(region->vm_file);
12928feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
129389a86402SDavid Howells 	if (vma->vm_file)
12948feae131SDavid Howells 		fput(vma->vm_file);
12953928d4f5SLinus Torvalds 	vm_area_free(vma);
12961da177e4SLinus Torvalds 	return ret;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds sharing_violation:
12998feae131SDavid Howells 	up_write(&nommu_region_sem);
130022cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
13018feae131SDavid Howells 	ret = -EINVAL;
13028feae131SDavid Howells 	goto error;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds error_getting_vma:
13058feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
130622cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
13071da177e4SLinus Torvalds 			len, current->pid);
13089af744d7SMichal Hocko 	show_free_areas(0, NULL);
13091da177e4SLinus Torvalds 	return -ENOMEM;
13101da177e4SLinus Torvalds 
13118feae131SDavid Howells error_getting_region:
131222cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
13131da177e4SLinus Torvalds 			len, current->pid);
13149af744d7SMichal Hocko 	show_free_areas(0, NULL);
13151da177e4SLinus Torvalds 	return -ENOMEM;
13161da177e4SLinus Torvalds }
13176be5ceb0SLinus Torvalds 
1318a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1319a90f590aSDominik Brodowski 			      unsigned long prot, unsigned long flags,
1320a90f590aSDominik Brodowski 			      unsigned long fd, unsigned long pgoff)
132166f0dc48SHugh Dickins {
132266f0dc48SHugh Dickins 	struct file *file = NULL;
132366f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
132466f0dc48SHugh Dickins 
1325120a795dSAl Viro 	audit_mmap_fd(fd, flags);
132666f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
132766f0dc48SHugh Dickins 		file = fget(fd);
132866f0dc48SHugh Dickins 		if (!file)
132966f0dc48SHugh Dickins 			goto out;
133066f0dc48SHugh Dickins 	}
133166f0dc48SHugh Dickins 
133266f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
133366f0dc48SHugh Dickins 
1334ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
133566f0dc48SHugh Dickins 
133666f0dc48SHugh Dickins 	if (file)
133766f0dc48SHugh Dickins 		fput(file);
133866f0dc48SHugh Dickins out:
133966f0dc48SHugh Dickins 	return retval;
134066f0dc48SHugh Dickins }
134166f0dc48SHugh Dickins 
1342a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1343a90f590aSDominik Brodowski 		unsigned long, prot, unsigned long, flags,
1344a90f590aSDominik Brodowski 		unsigned long, fd, unsigned long, pgoff)
1345a90f590aSDominik Brodowski {
1346a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1347a90f590aSDominik Brodowski }
1348a90f590aSDominik Brodowski 
1349a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1350a4679373SChristoph Hellwig struct mmap_arg_struct {
1351a4679373SChristoph Hellwig 	unsigned long addr;
1352a4679373SChristoph Hellwig 	unsigned long len;
1353a4679373SChristoph Hellwig 	unsigned long prot;
1354a4679373SChristoph Hellwig 	unsigned long flags;
1355a4679373SChristoph Hellwig 	unsigned long fd;
1356a4679373SChristoph Hellwig 	unsigned long offset;
1357a4679373SChristoph Hellwig };
1358a4679373SChristoph Hellwig 
1359a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1360a4679373SChristoph Hellwig {
1361a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1362a4679373SChristoph Hellwig 
1363a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1364a4679373SChristoph Hellwig 		return -EFAULT;
13651824cb75SAlexander Kuleshov 	if (offset_in_page(a.offset))
1366a4679373SChristoph Hellwig 		return -EINVAL;
1367a4679373SChristoph Hellwig 
1368a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1369a4679373SChristoph Hellwig 			       a.offset >> PAGE_SHIFT);
1370a4679373SChristoph Hellwig }
1371a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1372a4679373SChristoph Hellwig 
13731da177e4SLinus Torvalds /*
13748feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
13758feae131SDavid Howells  * for the first part or the tail.
13761da177e4SLinus Torvalds  */
13778feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
13788feae131SDavid Howells 	      unsigned long addr, int new_below)
13791da177e4SLinus Torvalds {
13808feae131SDavid Howells 	struct vm_area_struct *new;
13818feae131SDavid Howells 	struct vm_region *region;
13828feae131SDavid Howells 	unsigned long npages;
13831da177e4SLinus Torvalds 
1384779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1385779c1023SDavid Howells 	 * only a single usage on the region) */
1386779c1023SDavid Howells 	if (vma->vm_file)
13878feae131SDavid Howells 		return -ENOMEM;
13881da177e4SLinus Torvalds 
13898feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
13908feae131SDavid Howells 		return -ENOMEM;
13911da177e4SLinus Torvalds 
13928feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
13938feae131SDavid Howells 	if (!region)
13948feae131SDavid Howells 		return -ENOMEM;
13958feae131SDavid Howells 
13963928d4f5SLinus Torvalds 	new = vm_area_dup(vma);
13978feae131SDavid Howells 	if (!new) {
13988feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
13998feae131SDavid Howells 		return -ENOMEM;
14001da177e4SLinus Torvalds 	}
14011da177e4SLinus Torvalds 
14028feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
14038feae131SDavid Howells 	*region = *vma->vm_region;
14048feae131SDavid Howells 	new->vm_region = region;
14058feae131SDavid Howells 
14068feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
14078feae131SDavid Howells 
14088feae131SDavid Howells 	if (new_below) {
1409dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
14108feae131SDavid Howells 	} else {
14118feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
14128feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
14131da177e4SLinus Torvalds 	}
14148feae131SDavid Howells 
14158feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
14168feae131SDavid Howells 		new->vm_ops->open(new);
14178feae131SDavid Howells 
14188feae131SDavid Howells 	delete_vma_from_mm(vma);
14198feae131SDavid Howells 	down_write(&nommu_region_sem);
14208feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
14218feae131SDavid Howells 	if (new_below) {
14228feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
14238feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
14248feae131SDavid Howells 	} else {
14258feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1426dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
14278feae131SDavid Howells 	}
14288feae131SDavid Howells 	add_nommu_region(vma->vm_region);
14298feae131SDavid Howells 	add_nommu_region(new->vm_region);
14308feae131SDavid Howells 	up_write(&nommu_region_sem);
14318feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14328feae131SDavid Howells 	add_vma_to_mm(mm, new);
14338feae131SDavid Howells 	return 0;
14348feae131SDavid Howells }
14358feae131SDavid Howells 
14368feae131SDavid Howells /*
14378feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
14388feae131SDavid Howells  * the end
14398feae131SDavid Howells  */
14408feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
14418feae131SDavid Howells 		      struct vm_area_struct *vma,
14428feae131SDavid Howells 		      unsigned long from, unsigned long to)
14438feae131SDavid Howells {
14448feae131SDavid Howells 	struct vm_region *region;
14458feae131SDavid Howells 
14468feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
14478feae131SDavid Howells 	 * and list */
14488feae131SDavid Howells 	delete_vma_from_mm(vma);
14498feae131SDavid Howells 	if (from > vma->vm_start)
14508feae131SDavid Howells 		vma->vm_end = from;
14518feae131SDavid Howells 	else
14528feae131SDavid Howells 		vma->vm_start = to;
14538feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14548feae131SDavid Howells 
14558feae131SDavid Howells 	/* cut the backing region down to size */
14568feae131SDavid Howells 	region = vma->vm_region;
14571e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
14588feae131SDavid Howells 
14598feae131SDavid Howells 	down_write(&nommu_region_sem);
14608feae131SDavid Howells 	delete_nommu_region(region);
1461dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1462dd8632a1SPaul Mundt 		to = region->vm_top;
1463dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1464dd8632a1SPaul Mundt 	} else {
14658feae131SDavid Howells 		region->vm_start = to;
1466dd8632a1SPaul Mundt 	}
14678feae131SDavid Howells 	add_nommu_region(region);
14688feae131SDavid Howells 	up_write(&nommu_region_sem);
14698feae131SDavid Howells 
14708feae131SDavid Howells 	free_page_series(from, to);
14718feae131SDavid Howells 	return 0;
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds 
14743034097aSDavid Howells /*
14753034097aSDavid Howells  * release a mapping
14768feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
14778feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
14783034097aSDavid Howells  */
1479897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
14801da177e4SLinus Torvalds {
14818feae131SDavid Howells 	struct vm_area_struct *vma;
1482f67d9b15SBob Liu 	unsigned long end;
14838feae131SDavid Howells 	int ret;
14841da177e4SLinus Torvalds 
1485f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
14868feae131SDavid Howells 	if (len == 0)
14871da177e4SLinus Torvalds 		return -EINVAL;
14881da177e4SLinus Torvalds 
1489f67d9b15SBob Liu 	end = start + len;
1490f67d9b15SBob Liu 
14918feae131SDavid Howells 	/* find the first potentially overlapping VMA */
14928feae131SDavid Howells 	vma = find_vma(mm, start);
14938feae131SDavid Howells 	if (!vma) {
1494ac714904SChoi Gi-yong 		static int limit;
149533e5d769SDavid Howells 		if (limit < 5) {
149622cc877bSLeon Romanovsky 			pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
149733e5d769SDavid Howells 					current->pid, current->comm,
149833e5d769SDavid Howells 					start, start + len - 1);
149933e5d769SDavid Howells 			limit++;
150033e5d769SDavid Howells 		}
15018feae131SDavid Howells 		return -EINVAL;
15028feae131SDavid Howells 	}
15031da177e4SLinus Torvalds 
15048feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
15058feae131SDavid Howells 	if (vma->vm_file) {
15068feae131SDavid Howells 		do {
150722cc877bSLeon Romanovsky 			if (start > vma->vm_start)
15088feae131SDavid Howells 				return -EINVAL;
15098feae131SDavid Howells 			if (end == vma->vm_end)
15108feae131SDavid Howells 				goto erase_whole_vma;
1511d75a310cSNamhyung Kim 			vma = vma->vm_next;
1512d75a310cSNamhyung Kim 		} while (vma);
15138feae131SDavid Howells 		return -EINVAL;
15148feae131SDavid Howells 	} else {
15158feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
15168feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
15178feae131SDavid Howells 			goto erase_whole_vma;
151822cc877bSLeon Romanovsky 		if (start < vma->vm_start || end > vma->vm_end)
15198feae131SDavid Howells 			return -EINVAL;
15201824cb75SAlexander Kuleshov 		if (offset_in_page(start))
15218feae131SDavid Howells 			return -EINVAL;
15221824cb75SAlexander Kuleshov 		if (end != vma->vm_end && offset_in_page(end))
15238feae131SDavid Howells 			return -EINVAL;
15248feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
15258feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
152622cc877bSLeon Romanovsky 			if (ret < 0)
15278feae131SDavid Howells 				return ret;
15288feae131SDavid Howells 		}
15298feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
15308feae131SDavid Howells 	}
15311da177e4SLinus Torvalds 
15328feae131SDavid Howells erase_whole_vma:
15338feae131SDavid Howells 	delete_vma_from_mm(vma);
15348feae131SDavid Howells 	delete_vma(mm, vma);
15351da177e4SLinus Torvalds 	return 0;
15361da177e4SLinus Torvalds }
1537b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
15381da177e4SLinus Torvalds 
1539bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
15403034097aSDavid Howells {
1541bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
15423034097aSDavid Howells 	int ret;
15433034097aSDavid Howells 
15443034097aSDavid Howells 	down_write(&mm->mmap_sem);
1545897ab3e0SMike Rapoport 	ret = do_munmap(mm, addr, len, NULL);
15463034097aSDavid Howells 	up_write(&mm->mmap_sem);
15473034097aSDavid Howells 	return ret;
15483034097aSDavid Howells }
1549a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1550a46ef99dSLinus Torvalds 
1551a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1552a46ef99dSLinus Torvalds {
1553bfce281cSAl Viro 	return vm_munmap(addr, len);
1554a46ef99dSLinus Torvalds }
15553034097aSDavid Howells 
15563034097aSDavid Howells /*
15578feae131SDavid Howells  * release all the mappings made in a process's VM space
15583034097aSDavid Howells  */
15591da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
15601da177e4SLinus Torvalds {
15618feae131SDavid Howells 	struct vm_area_struct *vma;
15621da177e4SLinus Torvalds 
15638feae131SDavid Howells 	if (!mm)
15648feae131SDavid Howells 		return;
15658feae131SDavid Howells 
15661da177e4SLinus Torvalds 	mm->total_vm = 0;
15671da177e4SLinus Torvalds 
15688feae131SDavid Howells 	while ((vma = mm->mmap)) {
15698feae131SDavid Howells 		mm->mmap = vma->vm_next;
15708feae131SDavid Howells 		delete_vma_from_mm(vma);
15718feae131SDavid Howells 		delete_vma(mm, vma);
157204c34961SSteven J. Magnani 		cond_resched();
15731da177e4SLinus Torvalds 	}
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
15765d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	return -ENOMEM;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds /*
15826fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
15836fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
15841da177e4SLinus Torvalds  *
15856fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
15868feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
15878feae131SDavid Howells  * block is not shareable
15881da177e4SLinus Torvalds  *
15896fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
15901da177e4SLinus Torvalds  */
15914b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
15921da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
15931da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
15941da177e4SLinus Torvalds {
15956fa5f80bSDavid Howells 	struct vm_area_struct *vma;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	/* insanity checks first */
1598f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1599f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
16008feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
16011da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16021da177e4SLinus Torvalds 
16031824cb75SAlexander Kuleshov 	if (offset_in_page(addr))
16048feae131SDavid Howells 		return -EINVAL;
16058feae131SDavid Howells 
16061da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
16071da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16081da177e4SLinus Torvalds 
16098feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
16106fa5f80bSDavid Howells 	if (!vma)
16111da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16121da177e4SLinus Torvalds 
16136fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
16141da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
16151da177e4SLinus Torvalds 
16166fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
16171da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
16181da177e4SLinus Torvalds 
16198feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
16201da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	/* all checks complete - do it */
16236fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
16246fa5f80bSDavid Howells 	return vma->vm_start;
16256fa5f80bSDavid Howells }
16266fa5f80bSDavid Howells 
16276a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
16286a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
16296a6160a7SHeiko Carstens 		unsigned long, new_addr)
16306fa5f80bSDavid Howells {
16316fa5f80bSDavid Howells 	unsigned long ret;
16326fa5f80bSDavid Howells 
16336fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
16346fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
16356fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
16366fa5f80bSDavid Howells 	return ret;
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds 
1639df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1640df06b37fSKeith Busch 			 unsigned int foll_flags)
16411da177e4SLinus Torvalds {
16421da177e4SLinus Torvalds 	return NULL;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
16458f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16468f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
16471da177e4SLinus Torvalds {
16488f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
16498f3b1327SBob Liu 		return -EINVAL;
16508f3b1327SBob Liu 
1651314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
165266aa2b4bSGreg Ungerer 	return 0;
16531da177e4SLinus Torvalds }
165422c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
16551da177e4SLinus Torvalds 
16563c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
16573c0b9de6SLinus Torvalds {
16583c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
16593c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
16603c0b9de6SLinus Torvalds 
16613c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
16623c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
16633c0b9de6SLinus Torvalds }
16643c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
16653c0b9de6SLinus Torvalds 
1666f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1667f905bc44SPaul Mundt 			unsigned long pgoff)
1668f905bc44SPaul Mundt {
1669f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1670f905bc44SPaul Mundt 
1671f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1672f905bc44SPaul Mundt 		return -EINVAL;
1673f905bc44SPaul Mundt 
1674f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1675f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1676f905bc44SPaul Mundt 
1677f905bc44SPaul Mundt 	return 0;
1678f905bc44SPaul Mundt }
1679f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1680f905bc44SPaul Mundt 
16811da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
16821da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	return -ENOMEM;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
16872bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
1688b0e15190SDavid Howells {
1689b0e15190SDavid Howells 	BUG();
1690d0217ac0SNick Piggin 	return 0;
1691b0e15190SDavid Howells }
1692b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
16930ec76a11SDavid Howells 
169482b0f8c3SJan Kara void filemap_map_pages(struct vm_fault *vmf,
1695bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1696f1820361SKirill A. Shutemov {
1697f1820361SKirill A. Shutemov 	BUG();
1698f1820361SKirill A. Shutemov }
1699f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1700f1820361SKirill A. Shutemov 
170184d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1702442486ecSLorenzo Stoakes 		unsigned long addr, void *buf, int len, unsigned int gup_flags)
17030ec76a11SDavid Howells {
17040ec76a11SDavid Howells 	struct vm_area_struct *vma;
1705442486ecSLorenzo Stoakes 	int write = gup_flags & FOLL_WRITE;
17060ec76a11SDavid Howells 
1707*1e426fe2SKonstantin Khlebnikov 	if (down_read_killable(&mm->mmap_sem))
1708*1e426fe2SKonstantin Khlebnikov 		return 0;
17090ec76a11SDavid Howells 
17100ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
17110159b141SDavid Howells 	vma = find_vma(mm, addr);
17120159b141SDavid Howells 	if (vma) {
17130ec76a11SDavid Howells 		/* don't overrun this mapping */
17140ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
17150ec76a11SDavid Howells 			len = vma->vm_end - addr;
17160ec76a11SDavid Howells 
17170ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1718d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
17197959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
17207959722bSJie Zhang 					 (void *) addr, buf, len);
1721d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
17227959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
17237959722bSJie Zhang 					    buf, (void *) addr, len);
17240ec76a11SDavid Howells 		else
17250ec76a11SDavid Howells 			len = 0;
17260ec76a11SDavid Howells 	} else {
17270ec76a11SDavid Howells 		len = 0;
17280ec76a11SDavid Howells 	}
17290ec76a11SDavid Howells 
17300ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
1731f55f199bSMike Frysinger 
1732f55f199bSMike Frysinger 	return len;
1733f55f199bSMike Frysinger }
1734f55f199bSMike Frysinger 
1735f55f199bSMike Frysinger /**
1736b7701a5fSMike Rapoport  * access_remote_vm - access another process' address space
1737f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1738f55f199bSMike Frysinger  * @addr:	start address to access
1739f55f199bSMike Frysinger  * @buf:	source or destination buffer
1740f55f199bSMike Frysinger  * @len:	number of bytes to transfer
17416347e8d5SLorenzo Stoakes  * @gup_flags:	flags modifying lookup behaviour
1742f55f199bSMike Frysinger  *
1743f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1744f55f199bSMike Frysinger  */
1745f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
17466347e8d5SLorenzo Stoakes 		void *buf, int len, unsigned int gup_flags)
1747f55f199bSMike Frysinger {
17486347e8d5SLorenzo Stoakes 	return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
1749f55f199bSMike Frysinger }
1750f55f199bSMike Frysinger 
1751f55f199bSMike Frysinger /*
1752f55f199bSMike Frysinger  * Access another process' address space.
1753f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1754f55f199bSMike Frysinger  */
1755f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1756f307ab6dSLorenzo Stoakes 		unsigned int gup_flags)
1757f55f199bSMike Frysinger {
1758f55f199bSMike Frysinger 	struct mm_struct *mm;
1759f55f199bSMike Frysinger 
1760f55f199bSMike Frysinger 	if (addr + len < addr)
1761f55f199bSMike Frysinger 		return 0;
1762f55f199bSMike Frysinger 
1763f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1764f55f199bSMike Frysinger 	if (!mm)
1765f55f199bSMike Frysinger 		return 0;
1766f55f199bSMike Frysinger 
1767f307ab6dSLorenzo Stoakes 	len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
1768f55f199bSMike Frysinger 
17690ec76a11SDavid Howells 	mmput(mm);
17700ec76a11SDavid Howells 	return len;
17710ec76a11SDavid Howells }
1772fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
17737e660872SDavid Howells 
17747e660872SDavid Howells /**
17757e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
17767e660872SDavid Howells  * @inode: The inode to check
17777e660872SDavid Howells  * @size: The current filesize of the inode
17787e660872SDavid Howells  * @newsize: The proposed filesize of the inode
17797e660872SDavid Howells  *
17807e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
17817e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
17827e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
17837e660872SDavid Howells  * automatically grant mappings that are too large.
17847e660872SDavid Howells  */
17857e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
17867e660872SDavid Howells 				size_t newsize)
17877e660872SDavid Howells {
17887e660872SDavid Howells 	struct vm_area_struct *vma;
17897e660872SDavid Howells 	struct vm_region *region;
17907e660872SDavid Howells 	pgoff_t low, high;
17917e660872SDavid Howells 	size_t r_size, r_top;
17927e660872SDavid Howells 
17937e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
17947e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
17957e660872SDavid Howells 
17967e660872SDavid Howells 	down_write(&nommu_region_sem);
17971acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
17987e660872SDavid Howells 
17997e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
18006b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
18017e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
18027e660872SDavid Howells 		 * cache */
18037e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
18041acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
18057e660872SDavid Howells 			up_write(&nommu_region_sem);
18067e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
18077e660872SDavid Howells 		}
18087e660872SDavid Howells 	}
18097e660872SDavid Howells 
18107e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
18117e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
18127e660872SDavid Howells 	 *
18137e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
18147e660872SDavid Howells 	 * shouldn't be any
18157e660872SDavid Howells 	 */
18161acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
18177e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
18187e660872SDavid Howells 			continue;
18197e660872SDavid Howells 
18207e660872SDavid Howells 		region = vma->vm_region;
18217e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
18227e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
18237e660872SDavid Howells 
18247e660872SDavid Howells 		if (r_top > newsize) {
18257e660872SDavid Howells 			region->vm_top -= r_top - newsize;
18267e660872SDavid Howells 			if (region->vm_end > region->vm_top)
18277e660872SDavid Howells 				region->vm_end = region->vm_top;
18287e660872SDavid Howells 		}
18297e660872SDavid Howells 	}
18307e660872SDavid Howells 
18311acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
18327e660872SDavid Howells 	up_write(&nommu_region_sem);
18337e660872SDavid Howells 	return 0;
18347e660872SDavid Howells }
1835c9b1d098SAndrew Shewmaker 
1836c9b1d098SAndrew Shewmaker /*
1837c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1838c9b1d098SAndrew Shewmaker  *
1839c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1840c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1841c9b1d098SAndrew Shewmaker  * mode.
1842c9b1d098SAndrew Shewmaker  *
1843c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1844c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1845c9b1d098SAndrew Shewmaker  */
1846c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1847c9b1d098SAndrew Shewmaker {
1848c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1849c9b1d098SAndrew Shewmaker 
1850c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1851c9b1d098SAndrew Shewmaker 
1852c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1853c9b1d098SAndrew Shewmaker 	return 0;
1854c9b1d098SAndrew Shewmaker }
1855a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
18564eeab4f5SAndrew Shewmaker 
18574eeab4f5SAndrew Shewmaker /*
18584eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
18594eeab4f5SAndrew Shewmaker  *
18604eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
18614eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
18624eeab4f5SAndrew Shewmaker  *
18634eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
18644eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
18654eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
18664eeab4f5SAndrew Shewmaker  */
18674eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
18684eeab4f5SAndrew Shewmaker {
18694eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
18704eeab4f5SAndrew Shewmaker 
1871c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
18724eeab4f5SAndrew Shewmaker 
18734eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
18744eeab4f5SAndrew Shewmaker 	return 0;
18754eeab4f5SAndrew Shewmaker }
1876a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1877