xref: /linux/mm/nommu.c (revision d3f5ffcacd1528736471bc78f03f06da6c4551cc)
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  *
8dd19d293SStephen Kitt  *  See Documentation/admin-guide/mm/nommu-mmap.rst
91da177e4SLinus Torvalds  *
108feae131SDavid Howells  *  Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
111da177e4SLinus Torvalds  *  Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
121da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
131da177e4SLinus Torvalds  *  Copyright (c) 2002      Greg Ungerer <gerg@snapgear.com>
1429c185e5SPaul Mundt  *  Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
17b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18b1de0d13SMitchel Humpherys 
19b95f1b31SPaul Gortmaker #include <linux/export.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
216e84f315SIngo Molnar #include <linux/sched/mm.h>
22615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
231da177e4SLinus Torvalds #include <linux/mman.h>
241da177e4SLinus Torvalds #include <linux/swap.h>
251da177e4SLinus Torvalds #include <linux/file.h>
261da177e4SLinus Torvalds #include <linux/highmem.h>
271da177e4SLinus Torvalds #include <linux/pagemap.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/vmalloc.h>
301da177e4SLinus Torvalds #include <linux/blkdev.h>
311da177e4SLinus Torvalds #include <linux/backing-dev.h>
323b32123dSGideon Israel Dsouza #include <linux/compiler.h>
331da177e4SLinus Torvalds #include <linux/mount.h>
341da177e4SLinus Torvalds #include <linux/personality.h>
351da177e4SLinus Torvalds #include <linux/security.h>
361da177e4SLinus Torvalds #include <linux/syscalls.h>
37120a795dSAl Viro #include <linux/audit.h>
38b1de0d13SMitchel Humpherys #include <linux/printk.h>
391da177e4SLinus Torvalds 
407c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
411da177e4SLinus Torvalds #include <asm/tlb.h>
421da177e4SLinus Torvalds #include <asm/tlbflush.h>
43eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
448feae131SDavid Howells #include "internal.h"
458feae131SDavid Howells 
461da177e4SLinus Torvalds void *high_memory;
47944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
481da177e4SLinus Torvalds struct page *mem_map;
491da177e4SLinus Torvalds unsigned long max_mapnr;
505b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
514266c97aSHugh Dickins unsigned long highest_memmap_pfn;
52fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
531da177e4SLinus Torvalds int heap_stack_gap = 0;
541da177e4SLinus Torvalds 
5533e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
568feae131SDavid Howells 
571da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
581da177e4SLinus Torvalds 
598feae131SDavid Howells /* list of mapped, potentially shareable regions */
608feae131SDavid Howells static struct kmem_cache *vm_region_jar;
618feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
628feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
631da177e4SLinus Torvalds 
64f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
691da177e4SLinus Torvalds  * just what the caller asked for.
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct page *page;
761da177e4SLinus Torvalds 
774016a139SMichael Hennerich 	/*
784016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
794016a139SMichael Hennerich 	 * return size of 0
804016a139SMichael Hennerich 	 */
815a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
826cfd53fcSPaul Mundt 		return 0;
836cfd53fcSPaul Mundt 
846cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
856cfd53fcSPaul Mundt 
866cfd53fcSPaul Mundt 	/*
876cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
886cfd53fcSPaul Mundt 	 * kmalloc().
896cfd53fcSPaul Mundt 	 */
901da177e4SLinus Torvalds 	if (PageSlab(page))
911da177e4SLinus Torvalds 		return ksize(objp);
921da177e4SLinus Torvalds 
936cfd53fcSPaul Mundt 	/*
94ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
95ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
96ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
97ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
98ab2e83eaSPaul Mundt 	 */
99ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
100ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
101ab2e83eaSPaul Mundt 
102ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
103ab2e83eaSPaul Mundt 		if (vma)
104ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
105ab2e83eaSPaul Mundt 	}
106ab2e83eaSPaul Mundt 
107ab2e83eaSPaul Mundt 	/*
1086cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1095a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1106cfd53fcSPaul Mundt 	 */
111a50b854eSMatthew Wilcox (Oracle) 	return page_size(page);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114dfc2f91aSPaul Mundt /**
115dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
116dfc2f91aSPaul Mundt  * @vma: memory mapping
117dfc2f91aSPaul Mundt  * @address: user virtual address
118dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
119dfc2f91aSPaul Mundt  *
120dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
121dfc2f91aSPaul Mundt  *
122dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
123dfc2f91aSPaul Mundt  */
124dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
125dfc2f91aSPaul Mundt 	unsigned long *pfn)
126dfc2f91aSPaul Mundt {
127dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
128dfc2f91aSPaul Mundt 		return -EINVAL;
129dfc2f91aSPaul Mundt 
130dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
131dfc2f91aSPaul Mundt 	return 0;
132dfc2f91aSPaul Mundt }
133dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
134dfc2f91aSPaul Mundt 
135f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
1361da177e4SLinus Torvalds 
137b3bdda02SChristoph Lameter void vfree(const void *addr)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	kfree(addr);
1401da177e4SLinus Torvalds }
141b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
1421da177e4SLinus Torvalds 
14388dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	/*
1468518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
1478518609dSRobert P. J. Day 	 * returns only a logical address.
1481da177e4SLinus Torvalds 	 */
14984097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1501da177e4SLinus Torvalds }
151b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
1521da177e4SLinus Torvalds 
153041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align,
154041de93fSChristoph Hellwig 		unsigned long start, unsigned long end, gfp_t gfp_mask,
155041de93fSChristoph Hellwig 		pgprot_t prot, unsigned long vm_flags, int node,
156041de93fSChristoph Hellwig 		const void *caller)
157041de93fSChristoph Hellwig {
158041de93fSChristoph Hellwig 	return __vmalloc(size, gfp_mask);
159041de93fSChristoph Hellwig }
160041de93fSChristoph Hellwig 
1612b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
1622b905948SChristoph Hellwig 		int node, const void *caller)
163a7c3e901SMichal Hocko {
1642b905948SChristoph Hellwig 	return __vmalloc(size, gfp_mask);
165a7c3e901SMichal Hocko }
166a7c3e901SMichal Hocko 
167ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
168f905bc44SPaul Mundt {
169f905bc44SPaul Mundt 	void *ret;
170f905bc44SPaul Mundt 
17188dca4caSChristoph Hellwig 	ret = __vmalloc(size, flags);
172f905bc44SPaul Mundt 	if (ret) {
173f905bc44SPaul Mundt 		struct vm_area_struct *vma;
174f905bc44SPaul Mundt 
175d8ed45c5SMichel Lespinasse 		mmap_write_lock(current->mm);
176f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
177f905bc44SPaul Mundt 		if (vma)
178f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
179d8ed45c5SMichel Lespinasse 		mmap_write_unlock(current->mm);
180f905bc44SPaul Mundt 	}
181f905bc44SPaul Mundt 
182f905bc44SPaul Mundt 	return ret;
183f905bc44SPaul Mundt }
184ed81745aSAndrii Nakryiko 
185ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size)
186ed81745aSAndrii Nakryiko {
187ed81745aSAndrii Nakryiko 	return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
188ed81745aSAndrii Nakryiko }
189f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
190f905bc44SPaul Mundt 
191b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	return virt_to_page(addr);
1941da177e4SLinus Torvalds }
195b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
1961da177e4SLinus Torvalds 
197b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2001da177e4SLinus Torvalds }
201b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2041da177e4SLinus Torvalds {
2059bde916bSChen Gang 	/* Don't allow overflow */
2069bde916bSChen Gang 	if ((unsigned long) buf + count < count)
2079bde916bSChen Gang 		count = -(unsigned long) buf;
2089bde916bSChen Gang 
2091da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2101da177e4SLinus Torvalds 	return count;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	/* Don't allow overflow */
2161da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2171da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	memcpy(addr, buf, count);
220ac714904SChoi Gi-yong 	return count;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
224e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
2251da177e4SLinus Torvalds  *
2261da177e4SLinus Torvalds  *	@size:		allocation size
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
229e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
2301da177e4SLinus Torvalds  *
231c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2321da177e4SLinus Torvalds  *	use __vmalloc() instead.
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2351da177e4SLinus Torvalds {
23688dca4caSChristoph Hellwig        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM);
2371da177e4SLinus Torvalds }
238f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
239f6138882SAndrew Morton 
240e1ca7788SDave Young /*
241e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
242e1ca7788SDave Young  *
243e1ca7788SDave Young  *	@size:		allocation size
244e1ca7788SDave Young  *
245e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
246e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
247e1ca7788SDave Young  *	The memory allocated is set to zero.
248e1ca7788SDave Young  *
249e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
250e1ca7788SDave Young  *	use __vmalloc() instead.
251e1ca7788SDave Young  */
252e1ca7788SDave Young void *vzalloc(unsigned long size)
253e1ca7788SDave Young {
25488dca4caSChristoph Hellwig 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
255e1ca7788SDave Young }
256e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
257e1ca7788SDave Young 
258e1ca7788SDave Young /**
259e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
260e1ca7788SDave Young  * @size:	allocation size
261e1ca7788SDave Young  * @node:	numa node
262e1ca7788SDave Young  *
263e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
264e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
265e1ca7788SDave Young  *
266e1ca7788SDave Young  * For tight control over page level allocator and protection flags
267e1ca7788SDave Young  * use __vmalloc() instead.
268e1ca7788SDave Young  */
269f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
270f6138882SAndrew Morton {
271f6138882SAndrew Morton 	return vmalloc(size);
272f6138882SAndrew Morton }
2739a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
274e1ca7788SDave Young 
275e1ca7788SDave Young /**
276e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
277e1ca7788SDave Young  * @size:	allocation size
278e1ca7788SDave Young  * @node:	numa node
279e1ca7788SDave Young  *
280e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
281e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
282e1ca7788SDave Young  * The memory allocated is set to zero.
283e1ca7788SDave Young  *
284e1ca7788SDave Young  * For tight control over page level allocator and protection flags
285e1ca7788SDave Young  * use __vmalloc() instead.
286e1ca7788SDave Young  */
287e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
288e1ca7788SDave Young {
289e1ca7788SDave Young 	return vzalloc(size);
290e1ca7788SDave Young }
291e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2921da177e4SLinus Torvalds 
2931af446edSPaul Mundt /**
294b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
2951da177e4SLinus Torvalds  *	@size:		allocation size
2961da177e4SLinus Torvalds  *
2971da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
298e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
2991da177e4SLinus Torvalds  */
3001da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3011da177e4SLinus Torvalds {
30288dca4caSChristoph Hellwig 	return __vmalloc(size, GFP_KERNEL);
3031da177e4SLinus Torvalds }
304b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
305b5073173SPaul Mundt 
306b5073173SPaul Mundt /**
307b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
308b5073173SPaul Mundt  *	@size:		allocation size
309b5073173SPaul Mundt  *
310b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
311b5073173SPaul Mundt  * mapped to userspace without leaking data.
312f905bc44SPaul Mundt  *
313f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
314f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
315b5073173SPaul Mundt  */
316b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
317b5073173SPaul Mundt {
318f905bc44SPaul Mundt 	/*
319f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
320f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
321f905bc44SPaul Mundt 	 */
322f905bc44SPaul Mundt 	return vmalloc_user(size);
323b5073173SPaul Mundt }
324b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	BUG();
3291da177e4SLinus Torvalds 	return NULL;
3301da177e4SLinus Torvalds }
331b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3321da177e4SLinus Torvalds 
333b3bdda02SChristoph Lameter void vunmap(const void *addr)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	BUG();
3361da177e4SLinus Torvalds }
337b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3381da177e4SLinus Torvalds 
339d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
340eb6434d9SPaul Mundt {
341eb6434d9SPaul Mundt 	BUG();
342eb6434d9SPaul Mundt 	return NULL;
343eb6434d9SPaul Mundt }
344eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
345eb6434d9SPaul Mundt 
346eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
347eb6434d9SPaul Mundt {
348eb6434d9SPaul Mundt 	BUG();
349eb6434d9SPaul Mundt }
350eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
351eb6434d9SPaul Mundt 
352eb6434d9SPaul Mundt void vm_unmap_aliases(void)
353eb6434d9SPaul Mundt {
354eb6434d9SPaul Mundt }
355eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
356eb6434d9SPaul Mundt 
35729c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
35829c185e5SPaul Mundt {
35929c185e5SPaul Mundt 	BUG();
36029c185e5SPaul Mundt }
36129c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
36229c185e5SPaul Mundt 
363b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
364b5073173SPaul Mundt 		   struct page *page)
365b5073173SPaul Mundt {
366b5073173SPaul Mundt 	return -EINVAL;
367b5073173SPaul Mundt }
368b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
369b5073173SPaul Mundt 
370a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
371a667d745SSouptick Joarder 			unsigned long num)
372a667d745SSouptick Joarder {
373a667d745SSouptick Joarder 	return -EINVAL;
374a667d745SSouptick Joarder }
375a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages);
376a667d745SSouptick Joarder 
377a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
378a667d745SSouptick Joarder 				unsigned long num)
379a667d745SSouptick Joarder {
380a667d745SSouptick Joarder 	return -EINVAL;
381a667d745SSouptick Joarder }
382a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero);
383a667d745SSouptick Joarder 
3841eeb66a1SChristoph Hellwig /*
3851da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
3861da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
3871da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
3881da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
3891da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
3901da177e4SLinus Torvalds  */
3916a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
3961da177e4SLinus Torvalds 		return mm->brk;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	if (mm->brk == brk)
3991da177e4SLinus Torvalds 		return mm->brk;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/*
4021da177e4SLinus Torvalds 	 * Always allow shrinking brk
4031da177e4SLinus Torvalds 	 */
4041da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4051da177e4SLinus Torvalds 		mm->brk = brk;
4061da177e4SLinus Torvalds 		return brk;
4071da177e4SLinus Torvalds 	}
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/*
4101da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4111da177e4SLinus Torvalds 	 */
412a75a2df6SChristoph Hellwig 	flush_icache_user_range(mm->brk, brk);
4131da177e4SLinus Torvalds 	return mm->brk = brk;
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4168feae131SDavid Howells /*
4173edf41d8Sseokhoon.yoon  * initialise the percpu counter for VM and region record slabs
4188feae131SDavid Howells  */
4198feae131SDavid Howells void __init mmap_init(void)
4201da177e4SLinus Torvalds {
42100a62ce9SKOSAKI Motohiro 	int ret;
42200a62ce9SKOSAKI Motohiro 
423908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
42400a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
4255d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
4268feae131SDavid Howells }
4271da177e4SLinus Torvalds 
4288feae131SDavid Howells /*
4298feae131SDavid Howells  * validate the region tree
4308feae131SDavid Howells  * - the caller must hold the region lock
4318feae131SDavid Howells  */
4328feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4338feae131SDavid Howells static noinline void validate_nommu_regions(void)
4348feae131SDavid Howells {
4358feae131SDavid Howells 	struct vm_region *region, *last;
4368feae131SDavid Howells 	struct rb_node *p, *lastp;
4371da177e4SLinus Torvalds 
4388feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
4398feae131SDavid Howells 	if (!lastp)
4408feae131SDavid Howells 		return;
4418feae131SDavid Howells 
4428feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
443c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
444c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
4458feae131SDavid Howells 
4468feae131SDavid Howells 	while ((p = rb_next(lastp))) {
4478feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
4488feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
4498feae131SDavid Howells 
450c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
451c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
452c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
4538feae131SDavid Howells 
4548feae131SDavid Howells 		lastp = p;
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds }
4578feae131SDavid Howells #else
45833e5d769SDavid Howells static void validate_nommu_regions(void)
45933e5d769SDavid Howells {
46033e5d769SDavid Howells }
4618feae131SDavid Howells #endif
4628feae131SDavid Howells 
4638feae131SDavid Howells /*
4648feae131SDavid Howells  * add a region into the global tree
4658feae131SDavid Howells  */
4668feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4678feae131SDavid Howells {
4688feae131SDavid Howells 	struct vm_region *pregion;
4698feae131SDavid Howells 	struct rb_node **p, *parent;
4708feae131SDavid Howells 
4718feae131SDavid Howells 	validate_nommu_regions();
4728feae131SDavid Howells 
4738feae131SDavid Howells 	parent = NULL;
4748feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
4758feae131SDavid Howells 	while (*p) {
4768feae131SDavid Howells 		parent = *p;
4778feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
4788feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
4798feae131SDavid Howells 			p = &(*p)->rb_left;
4808feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
4818feae131SDavid Howells 			p = &(*p)->rb_right;
4828feae131SDavid Howells 		else if (pregion == region)
4838feae131SDavid Howells 			return;
4848feae131SDavid Howells 		else
4858feae131SDavid Howells 			BUG();
4868feae131SDavid Howells 	}
4878feae131SDavid Howells 
4888feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
4898feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
4908feae131SDavid Howells 
4918feae131SDavid Howells 	validate_nommu_regions();
4928feae131SDavid Howells }
4938feae131SDavid Howells 
4948feae131SDavid Howells /*
4958feae131SDavid Howells  * delete a region from the global tree
4968feae131SDavid Howells  */
4978feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
4988feae131SDavid Howells {
4998feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5008feae131SDavid Howells 
5018feae131SDavid Howells 	validate_nommu_regions();
5028feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5038feae131SDavid Howells 	validate_nommu_regions();
5048feae131SDavid Howells }
5058feae131SDavid Howells 
5068feae131SDavid Howells /*
5078feae131SDavid Howells  * free a contiguous series of pages
5088feae131SDavid Howells  */
5098feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5108feae131SDavid Howells {
5118feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5128feae131SDavid Howells 		struct page *page = virt_to_page(from);
5138feae131SDavid Howells 
51433e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5158feae131SDavid Howells 		put_page(page);
5168feae131SDavid Howells 	}
5178feae131SDavid Howells }
5188feae131SDavid Howells 
5198feae131SDavid Howells /*
5208feae131SDavid Howells  * release a reference to a region
52133e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
522dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5238feae131SDavid Howells  *   will equal vm_start
5248feae131SDavid Howells  */
5258feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5268feae131SDavid Howells 	__releases(nommu_region_sem)
5278feae131SDavid Howells {
5288feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5298feae131SDavid Howells 
5301e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
531dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
5328feae131SDavid Howells 			delete_nommu_region(region);
5338feae131SDavid Howells 		up_write(&nommu_region_sem);
5348feae131SDavid Howells 
5358feae131SDavid Howells 		if (region->vm_file)
5368feae131SDavid Howells 			fput(region->vm_file);
5378feae131SDavid Howells 
5388feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
5398feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
54022cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
541dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
5428feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
5438feae131SDavid Howells 	} else {
5448feae131SDavid Howells 		up_write(&nommu_region_sem);
5458feae131SDavid Howells 	}
5468feae131SDavid Howells }
5478feae131SDavid Howells 
5488feae131SDavid Howells /*
5498feae131SDavid Howells  * release a reference to a region
5508feae131SDavid Howells  */
5518feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
5528feae131SDavid Howells {
5538feae131SDavid Howells 	down_write(&nommu_region_sem);
5548feae131SDavid Howells 	__put_nommu_region(region);
5558feae131SDavid Howells }
5561da177e4SLinus Torvalds 
5573034097aSDavid Howells /*
5583034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
5598feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
5608feae131SDavid Howells  * page
561c1e8d7c6SMichel Lespinasse  * - should be called with mm->mmap_lock held writelocked
5623034097aSDavid Howells  */
5638feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
5643034097aSDavid Howells {
5656038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
5668feae131SDavid Howells 	struct address_space *mapping;
5676038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
5683034097aSDavid Howells 
5698feae131SDavid Howells 	BUG_ON(!vma->vm_region);
5708feae131SDavid Howells 
5718feae131SDavid Howells 	mm->map_count++;
5728feae131SDavid Howells 	vma->vm_mm = mm;
5738feae131SDavid Howells 
5748feae131SDavid Howells 	/* add the VMA to the mapping */
5758feae131SDavid Howells 	if (vma->vm_file) {
5768feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
5778feae131SDavid Howells 
57883cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
5798feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
5806b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
5818feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
58283cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
5838feae131SDavid Howells 	}
5848feae131SDavid Howells 
5858feae131SDavid Howells 	/* add the VMA to the tree */
5866038def0SNamhyung Kim 	parent = rb_prev = NULL;
5878feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
5888feae131SDavid Howells 	while (*p) {
5898feae131SDavid Howells 		parent = *p;
5908feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
5918feae131SDavid Howells 
5928feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
5938feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
5948feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
5958feae131SDavid Howells 			p = &(*p)->rb_left;
5966038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
5976038def0SNamhyung Kim 			rb_prev = parent;
5988feae131SDavid Howells 			p = &(*p)->rb_right;
5996038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
6008feae131SDavid Howells 			p = &(*p)->rb_left;
6016038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
6026038def0SNamhyung Kim 			rb_prev = parent;
6038feae131SDavid Howells 			p = &(*p)->rb_right;
6046038def0SNamhyung Kim 		} else if (vma < pvma)
6058feae131SDavid Howells 			p = &(*p)->rb_left;
6066038def0SNamhyung Kim 		else if (vma > pvma) {
6076038def0SNamhyung Kim 			rb_prev = parent;
6088feae131SDavid Howells 			p = &(*p)->rb_right;
6096038def0SNamhyung Kim 		} else
6108feae131SDavid Howells 			BUG();
6118feae131SDavid Howells 	}
6128feae131SDavid Howells 
6138feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
6148feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
6158feae131SDavid Howells 
6168feae131SDavid Howells 	/* add VMA to the VMA list also */
6176038def0SNamhyung Kim 	prev = NULL;
6186038def0SNamhyung Kim 	if (rb_prev)
6196038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
6203034097aSDavid Howells 
621aba6dfb7SWei Yang 	__vma_link_list(mm, vma, prev);
6228feae131SDavid Howells }
6238feae131SDavid Howells 
6248feae131SDavid Howells /*
6258feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
6268feae131SDavid Howells  */
6278feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
6288feae131SDavid Howells {
629615d6e87SDavidlohr Bueso 	int i;
6308feae131SDavid Howells 	struct address_space *mapping;
6318feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
632615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
6338feae131SDavid Howells 
6348feae131SDavid Howells 	mm->map_count--;
635615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
636615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
637314ff785SIngo Molnar 		if (curr->vmacache.vmas[i] == vma) {
638e020d5bdSSteven Miao 			vmacache_invalidate(mm);
639615d6e87SDavidlohr Bueso 			break;
640615d6e87SDavidlohr Bueso 		}
641615d6e87SDavidlohr Bueso 	}
6428feae131SDavid Howells 
6438feae131SDavid Howells 	/* remove the VMA from the mapping */
6448feae131SDavid Howells 	if (vma->vm_file) {
6458feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6468feae131SDavid Howells 
64783cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6488feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6496b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
6508feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
65183cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6528feae131SDavid Howells 	}
6538feae131SDavid Howells 
6548feae131SDavid Howells 	/* remove from the MM's tree and list */
6558feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
656b951bf2cSNamhyung Kim 
6571b9fc5b2SWei Yang 	__vma_unlink_list(mm, vma);
6588feae131SDavid Howells }
6598feae131SDavid Howells 
6608feae131SDavid Howells /*
6618feae131SDavid Howells  * destroy a VMA record
6628feae131SDavid Howells  */
6638feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
6648feae131SDavid Howells {
6658feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
6668feae131SDavid Howells 		vma->vm_ops->close(vma);
667e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
6688feae131SDavid Howells 		fput(vma->vm_file);
6698feae131SDavid Howells 	put_nommu_region(vma->vm_region);
6703928d4f5SLinus Torvalds 	vm_area_free(vma);
6713034097aSDavid Howells }
6723034097aSDavid Howells 
6733034097aSDavid Howells /*
6743034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
675c1e8d7c6SMichel Lespinasse  * - should be called with mm->mmap_lock at least held readlocked
6763034097aSDavid Howells  */
6773034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
6783034097aSDavid Howells {
6798feae131SDavid Howells 	struct vm_area_struct *vma;
6803034097aSDavid Howells 
6818feae131SDavid Howells 	/* check the cache first */
682615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
683615d6e87SDavidlohr Bueso 	if (likely(vma))
6848feae131SDavid Howells 		return vma;
6858feae131SDavid Howells 
686e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
6878feae131SDavid Howells 	 * resides) */
688e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
6898feae131SDavid Howells 		if (vma->vm_start > addr)
6908feae131SDavid Howells 			return NULL;
6918feae131SDavid Howells 		if (vma->vm_end > addr) {
692615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
6938feae131SDavid Howells 			return vma;
6943034097aSDavid Howells 		}
6958feae131SDavid Howells 	}
6963034097aSDavid Howells 
6973034097aSDavid Howells 	return NULL;
6983034097aSDavid Howells }
6993034097aSDavid Howells EXPORT_SYMBOL(find_vma);
7003034097aSDavid Howells 
7013034097aSDavid Howells /*
702930e652aSDavid Howells  * find a VMA
703930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
704930e652aSDavid Howells  */
705930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
706930e652aSDavid Howells {
7077561e8caSDavid Howells 	return find_vma(mm, addr);
708930e652aSDavid Howells }
709930e652aSDavid Howells 
7108feae131SDavid Howells /*
7118feae131SDavid Howells  * expand a stack to a given address
7128feae131SDavid Howells  * - not supported under NOMMU conditions
7138feae131SDavid Howells  */
71457c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
71557c8f63eSGreg Ungerer {
71657c8f63eSGreg Ungerer 	return -ENOMEM;
71757c8f63eSGreg Ungerer }
71857c8f63eSGreg Ungerer 
719930e652aSDavid Howells /*
7206fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
721c1e8d7c6SMichel Lespinasse  * - should be called with mm->mmap_lock at least held readlocked
7226fa5f80bSDavid Howells  */
7238feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
7248feae131SDavid Howells 					     unsigned long addr,
7258feae131SDavid Howells 					     unsigned long len)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	struct vm_area_struct *vma;
7288feae131SDavid Howells 	unsigned long end = addr + len;
7291da177e4SLinus Torvalds 
7308feae131SDavid Howells 	/* check the cache first */
731615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
732615d6e87SDavidlohr Bueso 	if (vma)
7331da177e4SLinus Torvalds 		return vma;
7348feae131SDavid Howells 
735e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7368feae131SDavid Howells 	 * resides) */
737e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7388feae131SDavid Howells 		if (vma->vm_start < addr)
7398feae131SDavid Howells 			continue;
7408feae131SDavid Howells 		if (vma->vm_start > addr)
7418feae131SDavid Howells 			return NULL;
7428feae131SDavid Howells 		if (vma->vm_end == end) {
743615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7448feae131SDavid Howells 			return vma;
7458feae131SDavid Howells 		}
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	return NULL;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7513034097aSDavid Howells /*
7521da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
7531da177e4SLinus Torvalds  * mapping we're capable of supporting
7541da177e4SLinus Torvalds  */
7551da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
7561da177e4SLinus Torvalds 				 unsigned long addr,
7571da177e4SLinus Torvalds 				 unsigned long len,
7581da177e4SLinus Torvalds 				 unsigned long prot,
7591da177e4SLinus Torvalds 				 unsigned long flags,
7601da177e4SLinus Torvalds 				 unsigned long pgoff,
7611da177e4SLinus Torvalds 				 unsigned long *_capabilities)
7621da177e4SLinus Torvalds {
7638feae131SDavid Howells 	unsigned long capabilities, rlen;
7641da177e4SLinus Torvalds 	int ret;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	/* do the simple checks first */
76722cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
7681da177e4SLinus Torvalds 		return -EINVAL;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
7711da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
7721da177e4SLinus Torvalds 		return -EINVAL;
7731da177e4SLinus Torvalds 
774f81cff0dSMike Frysinger 	if (!len)
7751da177e4SLinus Torvalds 		return -EINVAL;
7761da177e4SLinus Torvalds 
777f81cff0dSMike Frysinger 	/* Careful about overflows.. */
7788feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
7798feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
780f81cff0dSMike Frysinger 		return -ENOMEM;
781f81cff0dSMike Frysinger 
7821da177e4SLinus Torvalds 	/* offset overflow? */
7838feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
784f81cff0dSMike Frysinger 		return -EOVERFLOW;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	if (file) {
7871da177e4SLinus Torvalds 		/* files must support mmap */
78872c2d531SAl Viro 		if (!file->f_op->mmap)
7891da177e4SLinus Torvalds 			return -ENODEV;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
7921da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
7931da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
7941da177e4SLinus Torvalds 		 */
795b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
796b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
797b4caecd4SChristoph Hellwig 		} else {
7981da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
7991da177e4SLinus Torvalds 			 * defaults */
800496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
8011da177e4SLinus Torvalds 			case S_IFREG:
8021da177e4SLinus Torvalds 			case S_IFBLK:
803b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
8041da177e4SLinus Torvalds 				break;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 			case S_IFCHR:
8071da177e4SLinus Torvalds 				capabilities =
808b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
809b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
810b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
8111da177e4SLinus Torvalds 				break;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 			default:
8141da177e4SLinus Torvalds 				return -EINVAL;
8151da177e4SLinus Torvalds 			}
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
8191da177e4SLinus Torvalds 		 * device */
8201da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
821b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
8226e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
823b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
8241da177e4SLinus Torvalds 
82528d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
82628d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
82728d7a6aeSGraff Yang 			return -EACCES;
82828d7a6aeSGraff Yang 
8291da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
8301da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
8311da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
8321da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
8331da177e4SLinus Torvalds 				return -EACCES;
8341da177e4SLinus Torvalds 
835496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
8361da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
8371da177e4SLinus Torvalds 				return -EACCES;
8381da177e4SLinus Torvalds 
839d7a06983SJeff Layton 			if (locks_verify_locked(file))
8401da177e4SLinus Torvalds 				return -EAGAIN;
8411da177e4SLinus Torvalds 
842b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
8431da177e4SLinus Torvalds 				return -ENODEV;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
846b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
847ac714904SChoi Gi-yong 		} else {
8481da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
8491da177e4SLinus Torvalds 			 * allocate */
850b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
8511da177e4SLinus Torvalds 				return -ENODEV;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
8541da177e4SLinus Torvalds 			 * shared with the backing device */
8551da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
856b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 
859b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
860b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
861b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
862b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
8633c7b2045SBernd Schmidt 			    ) {
864b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8653c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
86622cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
8673c7b2045SBernd Schmidt 					return -EINVAL;
8683c7b2045SBernd Schmidt 				}
8693c7b2045SBernd Schmidt 			}
8703c7b2045SBernd Schmidt 		}
8713c7b2045SBernd Schmidt 
8721da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
8731da177e4SLinus Torvalds 		 * mappings */
87490f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
8751da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
8761da177e4SLinus Torvalds 				return -EPERM;
877ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
8781da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
8791da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
880b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
8811da177e4SLinus Torvalds 					prot |= PROT_EXEC;
8821da177e4SLinus Torvalds 			}
883ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
8841da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
885b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
8861da177e4SLinus Torvalds 			 ) {
8871da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
888b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
8891da177e4SLinus Torvalds 		}
890ac714904SChoi Gi-yong 	} else {
8911da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
8921da177e4SLinus Torvalds 		 * privately mapped
8931da177e4SLinus Torvalds 		 */
894b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
8971da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
8981da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
8991da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	/* allow the security API to have its say */
903e5467859SAl Viro 	ret = security_mmap_addr(addr);
904e5467859SAl Viro 	if (ret < 0)
905e5467859SAl Viro 		return ret;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* looks okay */
9081da177e4SLinus Torvalds 	*_capabilities = capabilities;
9091da177e4SLinus Torvalds 	return 0;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds /*
9131da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
9141da177e4SLinus Torvalds  * now know into VMA flags
9151da177e4SLinus Torvalds  */
9161da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
9171da177e4SLinus Torvalds 					unsigned long prot,
9181da177e4SLinus Torvalds 					unsigned long flags,
9191da177e4SLinus Torvalds 					unsigned long capabilities)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	unsigned long vm_flags;
9221da177e4SLinus Torvalds 
923e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
9241da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
9251da177e4SLinus Torvalds 
926b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
9271da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
9283c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9291da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
9301da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
9313c7b2045SBernd Schmidt 	} else {
9321da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
9331da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
9341da177e4SLinus Torvalds 		 * romfs/cramfs */
935b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
9361da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
9373c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
9411da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
9421da177e4SLinus Torvalds 	 * with another untraced process
9431da177e4SLinus Torvalds 	 */
944a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
9451da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	return vm_flags;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds /*
9518feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
9528feae131SDavid Howells  * pins the storage)
9531da177e4SLinus Torvalds  */
9548feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	int ret;
9571da177e4SLinus Torvalds 
958f74ac015SMiklos Szeredi 	ret = call_mmap(vma->vm_file, vma);
959dd8632a1SPaul Mundt 	if (ret == 0) {
960dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
961645d83c5SDavid Howells 		return 0;
962dd8632a1SPaul Mundt 	}
9631da177e4SLinus Torvalds 	if (ret != -ENOSYS)
9641da177e4SLinus Torvalds 		return ret;
9651da177e4SLinus Torvalds 
9663fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
9673fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
9683fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
9691da177e4SLinus Torvalds 	return -ENODEV;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds /*
9731da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
9741da177e4SLinus Torvalds  */
9758feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
9768feae131SDavid Howells 			   struct vm_region *region,
977645d83c5SDavid Howells 			   unsigned long len,
978645d83c5SDavid Howells 			   unsigned long capabilities)
9791da177e4SLinus Torvalds {
980dbc8358cSJoonsoo Kim 	unsigned long total, point;
9811da177e4SLinus Torvalds 	void *base;
9828feae131SDavid Howells 	int ret, order;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
9851da177e4SLinus Torvalds 	 * shared mappings on devices or memory
9861da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
9871da177e4SLinus Torvalds 	 */
988b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
989f74ac015SMiklos Szeredi 		ret = call_mmap(vma->vm_file, vma);
990dd8632a1SPaul Mundt 		if (ret == 0) {
9911da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
992dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
993dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
994645d83c5SDavid Howells 			return 0;
9951da177e4SLinus Torvalds 		}
996dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
997dd8632a1SPaul Mundt 			return ret;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
10001da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
10011da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10048feae131SDavid Howells 
10051da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
10061da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
10071da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
10081da177e4SLinus Torvalds 	 */
1009f67d9b15SBob Liu 	order = get_order(len);
10108feae131SDavid Howells 	total = 1 << order;
1011f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1012dd8632a1SPaul Mundt 
1013dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
101422cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1015dbc8358cSJoonsoo Kim 		total = point;
10168feae131SDavid Howells 
1017da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1018dbc8358cSJoonsoo Kim 	if (!base)
1019dbc8358cSJoonsoo Kim 		goto enomem;
10208feae131SDavid Howells 
1021dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1022dbc8358cSJoonsoo Kim 
10238feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
10248feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1025f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1026dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
10278feae131SDavid Howells 
10288feae131SDavid Howells 	vma->vm_start = region->vm_start;
10298feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	if (vma->vm_file) {
10321da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
10331da177e4SLinus Torvalds 		loff_t fpos;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
10361da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
10371da177e4SLinus Torvalds 
1038b4bf802aSChristoph Hellwig 		ret = kernel_read(vma->vm_file, base, len, &fpos);
10391da177e4SLinus Torvalds 		if (ret < 0)
10401da177e4SLinus Torvalds 			goto error_free;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 		/* clear the last little bit */
1043f67d9b15SBob Liu 		if (ret < len)
1044f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
10451da177e4SLinus Torvalds 
1046bfd40eafSKirill A. Shutemov 	} else {
1047bfd40eafSKirill A. Shutemov 		vma_set_anonymous(vma);
10481da177e4SLinus Torvalds 	}
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	return 0;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds error_free:
10537223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
10548feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
10558feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1056dd8632a1SPaul Mundt 	region->vm_top   = 0;
10571da177e4SLinus Torvalds 	return ret;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds enomem:
1060b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
106105ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
10629af744d7SMichal Hocko 	show_free_areas(0, NULL);
10631da177e4SLinus Torvalds 	return -ENOMEM;
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds /*
10671da177e4SLinus Torvalds  * handle mapping creation for uClinux
10681da177e4SLinus Torvalds  */
10691fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
10701da177e4SLinus Torvalds 			unsigned long addr,
10711da177e4SLinus Torvalds 			unsigned long len,
10721da177e4SLinus Torvalds 			unsigned long prot,
10731da177e4SLinus Torvalds 			unsigned long flags,
1074bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
1075897ab3e0SMike Rapoport 			unsigned long *populate,
1076897ab3e0SMike Rapoport 			struct list_head *uf)
10771da177e4SLinus Torvalds {
10788feae131SDavid Howells 	struct vm_area_struct *vma;
10798feae131SDavid Howells 	struct vm_region *region;
10801da177e4SLinus Torvalds 	struct rb_node *rb;
108145e55300SPeter Collingbourne 	vm_flags_t vm_flags;
10821fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
10831da177e4SLinus Torvalds 	int ret;
10841da177e4SLinus Torvalds 
108541badc15SMichel Lespinasse 	*populate = 0;
1086bebeb3d6SMichel Lespinasse 
10871da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
10881da177e4SLinus Torvalds 	 * mapping */
10891da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
10901da177e4SLinus Torvalds 				    &capabilities);
109122cc877bSLeon Romanovsky 	if (ret < 0)
10921da177e4SLinus Torvalds 		return ret;
10931da177e4SLinus Torvalds 
109406aab5a3SDavid Howells 	/* we ignore the address hint */
109506aab5a3SDavid Howells 	addr = 0;
1096f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
109706aab5a3SDavid Howells 
10981da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
10991da177e4SLinus Torvalds 	 * now know into VMA flags */
110045e55300SPeter Collingbourne 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
11011da177e4SLinus Torvalds 
11028feae131SDavid Howells 	/* we're going to need to record the mapping */
11038feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
11048feae131SDavid Howells 	if (!region)
11058feae131SDavid Howells 		goto error_getting_region;
11061da177e4SLinus Torvalds 
1107490fc053SLinus Torvalds 	vma = vm_area_alloc(current->mm);
11088feae131SDavid Howells 	if (!vma)
11098feae131SDavid Howells 		goto error_getting_vma;
11101da177e4SLinus Torvalds 
11111e2ae599SDavid Howells 	region->vm_usage = 1;
11128feae131SDavid Howells 	region->vm_flags = vm_flags;
11138feae131SDavid Howells 	region->vm_pgoff = pgoff;
11148feae131SDavid Howells 
11158feae131SDavid Howells 	vma->vm_flags = vm_flags;
11168feae131SDavid Howells 	vma->vm_pgoff = pgoff;
11178feae131SDavid Howells 
11188feae131SDavid Howells 	if (file) {
1119cb0942b8SAl Viro 		region->vm_file = get_file(file);
1120cb0942b8SAl Viro 		vma->vm_file = get_file(file);
11218feae131SDavid Howells 	}
11228feae131SDavid Howells 
11238feae131SDavid Howells 	down_write(&nommu_region_sem);
11248feae131SDavid Howells 
11258feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
11261da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
11278feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
11281da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
11291da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
11301da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
11311da177e4SLinus Torvalds 	 *   than here
11321da177e4SLinus Torvalds 	 */
11331da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
11348feae131SDavid Howells 		struct vm_region *pregion;
11358feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
11361da177e4SLinus Torvalds 
11378feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
11388feae131SDavid Howells 		pgend = pgoff + pglen;
1139165b2392SDavid Howells 
11408feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
11418feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
11421da177e4SLinus Torvalds 
11438feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
11441da177e4SLinus Torvalds 				continue;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1147496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1148496ad9aaSAl Viro 			    file_inode(file))
11491da177e4SLinus Torvalds 				continue;
11501da177e4SLinus Torvalds 
11518feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
11521da177e4SLinus Torvalds 				continue;
11531da177e4SLinus Torvalds 
11548feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
11558feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
11568feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
11578feae131SDavid Howells 			if (pgoff >= rpgend)
11581da177e4SLinus Torvalds 				continue;
11591da177e4SLinus Torvalds 
11608feae131SDavid Howells 			/* handle inexactly overlapping matches between
11618feae131SDavid Howells 			 * mappings */
11628feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
11638feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
11648feae131SDavid Howells 				/* new mapping is not a subset of the region */
1165b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
11661da177e4SLinus Torvalds 					goto sharing_violation;
11671da177e4SLinus Torvalds 				continue;
11681da177e4SLinus Torvalds 			}
11691da177e4SLinus Torvalds 
11708feae131SDavid Howells 			/* we've found a region we can share */
11711e2ae599SDavid Howells 			pregion->vm_usage++;
11728feae131SDavid Howells 			vma->vm_region = pregion;
11738feae131SDavid Howells 			start = pregion->vm_start;
11748feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
11758feae131SDavid Howells 			vma->vm_start = start;
11768feae131SDavid Howells 			vma->vm_end = start + len;
11771da177e4SLinus Torvalds 
117822cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
11798feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
118022cc877bSLeon Romanovsky 			else {
11818feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
11828feae131SDavid Howells 				if (ret < 0) {
11838feae131SDavid Howells 					vma->vm_region = NULL;
11848feae131SDavid Howells 					vma->vm_start = 0;
11858feae131SDavid Howells 					vma->vm_end = 0;
11861e2ae599SDavid Howells 					pregion->vm_usage--;
11878feae131SDavid Howells 					pregion = NULL;
11888feae131SDavid Howells 					goto error_just_free;
11891da177e4SLinus Torvalds 				}
11908feae131SDavid Howells 			}
11918feae131SDavid Howells 			fput(region->vm_file);
11928feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
11938feae131SDavid Howells 			region = pregion;
11948feae131SDavid Howells 			result = start;
11958feae131SDavid Howells 			goto share;
11968feae131SDavid Howells 		}
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
11991da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
12001da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
12011da177e4SLinus Torvalds 		 */
1202b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
12031da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
12041da177e4SLinus Torvalds 							     pgoff, flags);
1205bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
12061da177e4SLinus Torvalds 				ret = addr;
1207bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
12088feae131SDavid Howells 					goto error_just_free;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
12111da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
12121da177e4SLinus Torvalds 				 * it */
1213bb005a59SNamhyung Kim 				ret = -ENODEV;
1214b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
12158feae131SDavid Howells 					goto error_just_free;
12161da177e4SLinus Torvalds 
1217b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
12188feae131SDavid Howells 			} else {
12198feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
12208feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
12211da177e4SLinus Torvalds 			}
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds 
12258feae131SDavid Howells 	vma->vm_region = region;
12261da177e4SLinus Torvalds 
1227645d83c5SDavid Howells 	/* set up the mapping
1228b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1229645d83c5SDavid Howells 	 */
12301da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
12318feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
12321da177e4SLinus Torvalds 	else
1233645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
12341da177e4SLinus Torvalds 	if (ret < 0)
1235645d83c5SDavid Howells 		goto error_just_free;
1236645d83c5SDavid Howells 	add_nommu_region(region);
12378feae131SDavid Howells 
1238ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
12390bf5f949SChristoph Hellwig 	if (!vma->vm_file &&
12400bf5f949SChristoph Hellwig 	    (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
12410bf5f949SChristoph Hellwig 	     !(flags & MAP_UNINITIALIZED)))
1242ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1243ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1244ea637639SJie Zhang 
12451da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
12468feae131SDavid Howells 	result = vma->vm_start;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
12491da177e4SLinus Torvalds 
12508feae131SDavid Howells share:
12518feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
12521da177e4SLinus Torvalds 
1253cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1254cfe79c00SMike Frysinger 	 * mapping of it is made  */
1255cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1256a75a2df6SChristoph Hellwig 		flush_icache_user_range(region->vm_start, region->vm_end);
1257cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1258cfe79c00SMike Frysinger 	}
12591da177e4SLinus Torvalds 
1260cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
12611da177e4SLinus Torvalds 
12628feae131SDavid Howells 	return result;
12631da177e4SLinus Torvalds 
12648feae131SDavid Howells error_just_free:
12658feae131SDavid Howells 	up_write(&nommu_region_sem);
12668feae131SDavid Howells error:
126789a86402SDavid Howells 	if (region->vm_file)
12688feae131SDavid Howells 		fput(region->vm_file);
12698feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
127089a86402SDavid Howells 	if (vma->vm_file)
12718feae131SDavid Howells 		fput(vma->vm_file);
12723928d4f5SLinus Torvalds 	vm_area_free(vma);
12731da177e4SLinus Torvalds 	return ret;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds sharing_violation:
12768feae131SDavid Howells 	up_write(&nommu_region_sem);
127722cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
12788feae131SDavid Howells 	ret = -EINVAL;
12798feae131SDavid Howells 	goto error;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds error_getting_vma:
12828feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
128322cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
12841da177e4SLinus Torvalds 			len, current->pid);
12859af744d7SMichal Hocko 	show_free_areas(0, NULL);
12861da177e4SLinus Torvalds 	return -ENOMEM;
12871da177e4SLinus Torvalds 
12888feae131SDavid Howells error_getting_region:
128922cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
12901da177e4SLinus Torvalds 			len, current->pid);
12919af744d7SMichal Hocko 	show_free_areas(0, NULL);
12921da177e4SLinus Torvalds 	return -ENOMEM;
12931da177e4SLinus Torvalds }
12946be5ceb0SLinus Torvalds 
1295a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1296a90f590aSDominik Brodowski 			      unsigned long prot, unsigned long flags,
1297a90f590aSDominik Brodowski 			      unsigned long fd, unsigned long pgoff)
129866f0dc48SHugh Dickins {
129966f0dc48SHugh Dickins 	struct file *file = NULL;
130066f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
130166f0dc48SHugh Dickins 
1302120a795dSAl Viro 	audit_mmap_fd(fd, flags);
130366f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
130466f0dc48SHugh Dickins 		file = fget(fd);
130566f0dc48SHugh Dickins 		if (!file)
130666f0dc48SHugh Dickins 			goto out;
130766f0dc48SHugh Dickins 	}
130866f0dc48SHugh Dickins 
130966f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
131066f0dc48SHugh Dickins 
1311ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
131266f0dc48SHugh Dickins 
131366f0dc48SHugh Dickins 	if (file)
131466f0dc48SHugh Dickins 		fput(file);
131566f0dc48SHugh Dickins out:
131666f0dc48SHugh Dickins 	return retval;
131766f0dc48SHugh Dickins }
131866f0dc48SHugh Dickins 
1319a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1320a90f590aSDominik Brodowski 		unsigned long, prot, unsigned long, flags,
1321a90f590aSDominik Brodowski 		unsigned long, fd, unsigned long, pgoff)
1322a90f590aSDominik Brodowski {
1323a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1324a90f590aSDominik Brodowski }
1325a90f590aSDominik Brodowski 
1326a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1327a4679373SChristoph Hellwig struct mmap_arg_struct {
1328a4679373SChristoph Hellwig 	unsigned long addr;
1329a4679373SChristoph Hellwig 	unsigned long len;
1330a4679373SChristoph Hellwig 	unsigned long prot;
1331a4679373SChristoph Hellwig 	unsigned long flags;
1332a4679373SChristoph Hellwig 	unsigned long fd;
1333a4679373SChristoph Hellwig 	unsigned long offset;
1334a4679373SChristoph Hellwig };
1335a4679373SChristoph Hellwig 
1336a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1337a4679373SChristoph Hellwig {
1338a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1339a4679373SChristoph Hellwig 
1340a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1341a4679373SChristoph Hellwig 		return -EFAULT;
13421824cb75SAlexander Kuleshov 	if (offset_in_page(a.offset))
1343a4679373SChristoph Hellwig 		return -EINVAL;
1344a4679373SChristoph Hellwig 
1345a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1346a4679373SChristoph Hellwig 			       a.offset >> PAGE_SHIFT);
1347a4679373SChristoph Hellwig }
1348a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1349a4679373SChristoph Hellwig 
13501da177e4SLinus Torvalds /*
13518feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
13528feae131SDavid Howells  * for the first part or the tail.
13531da177e4SLinus Torvalds  */
13548feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
13558feae131SDavid Howells 	      unsigned long addr, int new_below)
13561da177e4SLinus Torvalds {
13578feae131SDavid Howells 	struct vm_area_struct *new;
13588feae131SDavid Howells 	struct vm_region *region;
13598feae131SDavid Howells 	unsigned long npages;
13601da177e4SLinus Torvalds 
1361779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1362779c1023SDavid Howells 	 * only a single usage on the region) */
1363779c1023SDavid Howells 	if (vma->vm_file)
13648feae131SDavid Howells 		return -ENOMEM;
13651da177e4SLinus Torvalds 
13668feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
13678feae131SDavid Howells 		return -ENOMEM;
13681da177e4SLinus Torvalds 
13698feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
13708feae131SDavid Howells 	if (!region)
13718feae131SDavid Howells 		return -ENOMEM;
13728feae131SDavid Howells 
13733928d4f5SLinus Torvalds 	new = vm_area_dup(vma);
13748feae131SDavid Howells 	if (!new) {
13758feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
13768feae131SDavid Howells 		return -ENOMEM;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13798feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
13808feae131SDavid Howells 	*region = *vma->vm_region;
13818feae131SDavid Howells 	new->vm_region = region;
13828feae131SDavid Howells 
13838feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
13848feae131SDavid Howells 
13858feae131SDavid Howells 	if (new_below) {
1386dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
13878feae131SDavid Howells 	} else {
13888feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
13898feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
13901da177e4SLinus Torvalds 	}
13918feae131SDavid Howells 
13928feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
13938feae131SDavid Howells 		new->vm_ops->open(new);
13948feae131SDavid Howells 
13958feae131SDavid Howells 	delete_vma_from_mm(vma);
13968feae131SDavid Howells 	down_write(&nommu_region_sem);
13978feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
13988feae131SDavid Howells 	if (new_below) {
13998feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
14008feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
14018feae131SDavid Howells 	} else {
14028feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1403dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
14048feae131SDavid Howells 	}
14058feae131SDavid Howells 	add_nommu_region(vma->vm_region);
14068feae131SDavid Howells 	add_nommu_region(new->vm_region);
14078feae131SDavid Howells 	up_write(&nommu_region_sem);
14088feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14098feae131SDavid Howells 	add_vma_to_mm(mm, new);
14108feae131SDavid Howells 	return 0;
14118feae131SDavid Howells }
14128feae131SDavid Howells 
14138feae131SDavid Howells /*
14148feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
14158feae131SDavid Howells  * the end
14168feae131SDavid Howells  */
14178feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
14188feae131SDavid Howells 		      struct vm_area_struct *vma,
14198feae131SDavid Howells 		      unsigned long from, unsigned long to)
14208feae131SDavid Howells {
14218feae131SDavid Howells 	struct vm_region *region;
14228feae131SDavid Howells 
14238feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
14248feae131SDavid Howells 	 * and list */
14258feae131SDavid Howells 	delete_vma_from_mm(vma);
14268feae131SDavid Howells 	if (from > vma->vm_start)
14278feae131SDavid Howells 		vma->vm_end = from;
14288feae131SDavid Howells 	else
14298feae131SDavid Howells 		vma->vm_start = to;
14308feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14318feae131SDavid Howells 
14328feae131SDavid Howells 	/* cut the backing region down to size */
14338feae131SDavid Howells 	region = vma->vm_region;
14341e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
14358feae131SDavid Howells 
14368feae131SDavid Howells 	down_write(&nommu_region_sem);
14378feae131SDavid Howells 	delete_nommu_region(region);
1438dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1439dd8632a1SPaul Mundt 		to = region->vm_top;
1440dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1441dd8632a1SPaul Mundt 	} else {
14428feae131SDavid Howells 		region->vm_start = to;
1443dd8632a1SPaul Mundt 	}
14448feae131SDavid Howells 	add_nommu_region(region);
14458feae131SDavid Howells 	up_write(&nommu_region_sem);
14468feae131SDavid Howells 
14478feae131SDavid Howells 	free_page_series(from, to);
14488feae131SDavid Howells 	return 0;
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
14513034097aSDavid Howells /*
14523034097aSDavid Howells  * release a mapping
14538feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
14548feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
14553034097aSDavid Howells  */
1456897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
14571da177e4SLinus Torvalds {
14588feae131SDavid Howells 	struct vm_area_struct *vma;
1459f67d9b15SBob Liu 	unsigned long end;
14608feae131SDavid Howells 	int ret;
14611da177e4SLinus Torvalds 
1462f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
14638feae131SDavid Howells 	if (len == 0)
14641da177e4SLinus Torvalds 		return -EINVAL;
14651da177e4SLinus Torvalds 
1466f67d9b15SBob Liu 	end = start + len;
1467f67d9b15SBob Liu 
14688feae131SDavid Howells 	/* find the first potentially overlapping VMA */
14698feae131SDavid Howells 	vma = find_vma(mm, start);
14708feae131SDavid Howells 	if (!vma) {
1471ac714904SChoi Gi-yong 		static int limit;
147233e5d769SDavid Howells 		if (limit < 5) {
147322cc877bSLeon Romanovsky 			pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
147433e5d769SDavid Howells 					current->pid, current->comm,
147533e5d769SDavid Howells 					start, start + len - 1);
147633e5d769SDavid Howells 			limit++;
147733e5d769SDavid Howells 		}
14788feae131SDavid Howells 		return -EINVAL;
14798feae131SDavid Howells 	}
14801da177e4SLinus Torvalds 
14818feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
14828feae131SDavid Howells 	if (vma->vm_file) {
14838feae131SDavid Howells 		do {
148422cc877bSLeon Romanovsky 			if (start > vma->vm_start)
14858feae131SDavid Howells 				return -EINVAL;
14868feae131SDavid Howells 			if (end == vma->vm_end)
14878feae131SDavid Howells 				goto erase_whole_vma;
1488d75a310cSNamhyung Kim 			vma = vma->vm_next;
1489d75a310cSNamhyung Kim 		} while (vma);
14908feae131SDavid Howells 		return -EINVAL;
14918feae131SDavid Howells 	} else {
14928feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
14938feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
14948feae131SDavid Howells 			goto erase_whole_vma;
149522cc877bSLeon Romanovsky 		if (start < vma->vm_start || end > vma->vm_end)
14968feae131SDavid Howells 			return -EINVAL;
14971824cb75SAlexander Kuleshov 		if (offset_in_page(start))
14988feae131SDavid Howells 			return -EINVAL;
14991824cb75SAlexander Kuleshov 		if (end != vma->vm_end && offset_in_page(end))
15008feae131SDavid Howells 			return -EINVAL;
15018feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
15028feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
150322cc877bSLeon Romanovsky 			if (ret < 0)
15048feae131SDavid Howells 				return ret;
15058feae131SDavid Howells 		}
15068feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
15078feae131SDavid Howells 	}
15081da177e4SLinus Torvalds 
15098feae131SDavid Howells erase_whole_vma:
15108feae131SDavid Howells 	delete_vma_from_mm(vma);
15118feae131SDavid Howells 	delete_vma(mm, vma);
15121da177e4SLinus Torvalds 	return 0;
15131da177e4SLinus Torvalds }
1514b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
15151da177e4SLinus Torvalds 
1516bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
15173034097aSDavid Howells {
1518bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
15193034097aSDavid Howells 	int ret;
15203034097aSDavid Howells 
1521d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
1522897ab3e0SMike Rapoport 	ret = do_munmap(mm, addr, len, NULL);
1523d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
15243034097aSDavid Howells 	return ret;
15253034097aSDavid Howells }
1526a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1527a46ef99dSLinus Torvalds 
1528a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1529a46ef99dSLinus Torvalds {
1530bfce281cSAl Viro 	return vm_munmap(addr, len);
1531a46ef99dSLinus Torvalds }
15323034097aSDavid Howells 
15333034097aSDavid Howells /*
15348feae131SDavid Howells  * release all the mappings made in a process's VM space
15353034097aSDavid Howells  */
15361da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
15371da177e4SLinus Torvalds {
15388feae131SDavid Howells 	struct vm_area_struct *vma;
15391da177e4SLinus Torvalds 
15408feae131SDavid Howells 	if (!mm)
15418feae131SDavid Howells 		return;
15428feae131SDavid Howells 
15431da177e4SLinus Torvalds 	mm->total_vm = 0;
15441da177e4SLinus Torvalds 
15458feae131SDavid Howells 	while ((vma = mm->mmap)) {
15468feae131SDavid Howells 		mm->mmap = vma->vm_next;
15478feae131SDavid Howells 		delete_vma_from_mm(vma);
15488feae131SDavid Howells 		delete_vma(mm, vma);
154904c34961SSteven J. Magnani 		cond_resched();
15501da177e4SLinus Torvalds 	}
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15535d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
15541da177e4SLinus Torvalds {
15551da177e4SLinus Torvalds 	return -ENOMEM;
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds /*
15596fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
15606fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
15611da177e4SLinus Torvalds  *
15626fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
15638feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
15648feae131SDavid Howells  * block is not shareable
15651da177e4SLinus Torvalds  *
15666fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
15671da177e4SLinus Torvalds  */
15684b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
15691da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
15701da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
15711da177e4SLinus Torvalds {
15726fa5f80bSDavid Howells 	struct vm_area_struct *vma;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	/* insanity checks first */
1575f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1576f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
15778feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
15781da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
15791da177e4SLinus Torvalds 
15801824cb75SAlexander Kuleshov 	if (offset_in_page(addr))
15818feae131SDavid Howells 		return -EINVAL;
15828feae131SDavid Howells 
15831da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
15841da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
15851da177e4SLinus Torvalds 
15868feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
15876fa5f80bSDavid Howells 	if (!vma)
15881da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
15891da177e4SLinus Torvalds 
15906fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
15911da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
15921da177e4SLinus Torvalds 
15936fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
15941da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
15951da177e4SLinus Torvalds 
15968feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
15971da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	/* all checks complete - do it */
16006fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
16016fa5f80bSDavid Howells 	return vma->vm_start;
16026fa5f80bSDavid Howells }
16036fa5f80bSDavid Howells 
16046a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
16056a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
16066a6160a7SHeiko Carstens 		unsigned long, new_addr)
16076fa5f80bSDavid Howells {
16086fa5f80bSDavid Howells 	unsigned long ret;
16096fa5f80bSDavid Howells 
1610d8ed45c5SMichel Lespinasse 	mmap_write_lock(current->mm);
16116fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1612d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
16136fa5f80bSDavid Howells 	return ret;
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds 
1616df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1617df06b37fSKeith Busch 			 unsigned int foll_flags)
16181da177e4SLinus Torvalds {
16191da177e4SLinus Torvalds 	return NULL;
16201da177e4SLinus Torvalds }
16211da177e4SLinus Torvalds 
16228f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16238f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
16241da177e4SLinus Torvalds {
16258f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
16268f3b1327SBob Liu 		return -EINVAL;
16278f3b1327SBob Liu 
1628314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
162966aa2b4bSGreg Ungerer 	return 0;
16301da177e4SLinus Torvalds }
163122c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
16321da177e4SLinus Torvalds 
16333c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
16343c0b9de6SLinus Torvalds {
16353c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
16363c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
16373c0b9de6SLinus Torvalds 
16383c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
16393c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
16403c0b9de6SLinus Torvalds }
16413c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
16423c0b9de6SLinus Torvalds 
1643f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1644f905bc44SPaul Mundt 			unsigned long pgoff)
1645f905bc44SPaul Mundt {
1646f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1647f905bc44SPaul Mundt 
1648f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1649f905bc44SPaul Mundt 		return -EINVAL;
1650f905bc44SPaul Mundt 
1651f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1652f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1653f905bc44SPaul Mundt 
1654f905bc44SPaul Mundt 	return 0;
1655f905bc44SPaul Mundt }
1656f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1657f905bc44SPaul Mundt 
16581da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
16591da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	return -ENOMEM;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16642bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
1665b0e15190SDavid Howells {
1666b0e15190SDavid Howells 	BUG();
1667d0217ac0SNick Piggin 	return 0;
1668b0e15190SDavid Howells }
1669b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
16700ec76a11SDavid Howells 
167182b0f8c3SJan Kara void filemap_map_pages(struct vm_fault *vmf,
1672bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1673f1820361SKirill A. Shutemov {
1674f1820361SKirill A. Shutemov 	BUG();
1675f1820361SKirill A. Shutemov }
1676f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1677f1820361SKirill A. Shutemov 
1678*d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
1679*d3f5ffcaSJohn Hubbard 		       int len, unsigned int gup_flags)
16800ec76a11SDavid Howells {
16810ec76a11SDavid Howells 	struct vm_area_struct *vma;
1682442486ecSLorenzo Stoakes 	int write = gup_flags & FOLL_WRITE;
16830ec76a11SDavid Howells 
1684d8ed45c5SMichel Lespinasse 	if (mmap_read_lock_killable(mm))
16851e426fe2SKonstantin Khlebnikov 		return 0;
16860ec76a11SDavid Howells 
16870ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
16880159b141SDavid Howells 	vma = find_vma(mm, addr);
16890159b141SDavid Howells 	if (vma) {
16900ec76a11SDavid Howells 		/* don't overrun this mapping */
16910ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
16920ec76a11SDavid Howells 			len = vma->vm_end - addr;
16930ec76a11SDavid Howells 
16940ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1695d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
16967959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
16977959722bSJie Zhang 					 (void *) addr, buf, len);
1698d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
16997959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
17007959722bSJie Zhang 					    buf, (void *) addr, len);
17010ec76a11SDavid Howells 		else
17020ec76a11SDavid Howells 			len = 0;
17030ec76a11SDavid Howells 	} else {
17040ec76a11SDavid Howells 		len = 0;
17050ec76a11SDavid Howells 	}
17060ec76a11SDavid Howells 
1707d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
1708f55f199bSMike Frysinger 
1709f55f199bSMike Frysinger 	return len;
1710f55f199bSMike Frysinger }
1711f55f199bSMike Frysinger 
1712f55f199bSMike Frysinger /**
1713b7701a5fSMike Rapoport  * access_remote_vm - access another process' address space
1714f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1715f55f199bSMike Frysinger  * @addr:	start address to access
1716f55f199bSMike Frysinger  * @buf:	source or destination buffer
1717f55f199bSMike Frysinger  * @len:	number of bytes to transfer
17186347e8d5SLorenzo Stoakes  * @gup_flags:	flags modifying lookup behaviour
1719f55f199bSMike Frysinger  *
1720f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1721f55f199bSMike Frysinger  */
1722f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
17236347e8d5SLorenzo Stoakes 		void *buf, int len, unsigned int gup_flags)
1724f55f199bSMike Frysinger {
1725*d3f5ffcaSJohn Hubbard 	return __access_remote_vm(mm, addr, buf, len, gup_flags);
1726f55f199bSMike Frysinger }
1727f55f199bSMike Frysinger 
1728f55f199bSMike Frysinger /*
1729f55f199bSMike Frysinger  * Access another process' address space.
1730f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1731f55f199bSMike Frysinger  */
1732f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1733f307ab6dSLorenzo Stoakes 		unsigned int gup_flags)
1734f55f199bSMike Frysinger {
1735f55f199bSMike Frysinger 	struct mm_struct *mm;
1736f55f199bSMike Frysinger 
1737f55f199bSMike Frysinger 	if (addr + len < addr)
1738f55f199bSMike Frysinger 		return 0;
1739f55f199bSMike Frysinger 
1740f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1741f55f199bSMike Frysinger 	if (!mm)
1742f55f199bSMike Frysinger 		return 0;
1743f55f199bSMike Frysinger 
1744*d3f5ffcaSJohn Hubbard 	len = __access_remote_vm(mm, addr, buf, len, gup_flags);
1745f55f199bSMike Frysinger 
17460ec76a11SDavid Howells 	mmput(mm);
17470ec76a11SDavid Howells 	return len;
17480ec76a11SDavid Howells }
1749fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
17507e660872SDavid Howells 
17517e660872SDavid Howells /**
17527e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
17537e660872SDavid Howells  * @inode: The inode to check
17547e660872SDavid Howells  * @size: The current filesize of the inode
17557e660872SDavid Howells  * @newsize: The proposed filesize of the inode
17567e660872SDavid Howells  *
17577e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
1758c08b342cSRandy Dunlap  * make sure that any outstanding VMAs aren't broken and then shrink the
1759c08b342cSRandy Dunlap  * vm_regions that extend beyond so that do_mmap() doesn't
17607e660872SDavid Howells  * automatically grant mappings that are too large.
17617e660872SDavid Howells  */
17627e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
17637e660872SDavid Howells 				size_t newsize)
17647e660872SDavid Howells {
17657e660872SDavid Howells 	struct vm_area_struct *vma;
17667e660872SDavid Howells 	struct vm_region *region;
17677e660872SDavid Howells 	pgoff_t low, high;
17687e660872SDavid Howells 	size_t r_size, r_top;
17697e660872SDavid Howells 
17707e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
17717e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
17727e660872SDavid Howells 
17737e660872SDavid Howells 	down_write(&nommu_region_sem);
17741acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
17757e660872SDavid Howells 
17767e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
17776b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
17787e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
17797e660872SDavid Howells 		 * cache */
17807e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
17811acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
17827e660872SDavid Howells 			up_write(&nommu_region_sem);
17837e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
17847e660872SDavid Howells 		}
17857e660872SDavid Howells 	}
17867e660872SDavid Howells 
17877e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
17887e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
17897e660872SDavid Howells 	 *
17907e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
17917e660872SDavid Howells 	 * shouldn't be any
17927e660872SDavid Howells 	 */
17931acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
17947e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
17957e660872SDavid Howells 			continue;
17967e660872SDavid Howells 
17977e660872SDavid Howells 		region = vma->vm_region;
17987e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
17997e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
18007e660872SDavid Howells 
18017e660872SDavid Howells 		if (r_top > newsize) {
18027e660872SDavid Howells 			region->vm_top -= r_top - newsize;
18037e660872SDavid Howells 			if (region->vm_end > region->vm_top)
18047e660872SDavid Howells 				region->vm_end = region->vm_top;
18057e660872SDavid Howells 		}
18067e660872SDavid Howells 	}
18077e660872SDavid Howells 
18081acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
18097e660872SDavid Howells 	up_write(&nommu_region_sem);
18107e660872SDavid Howells 	return 0;
18117e660872SDavid Howells }
1812c9b1d098SAndrew Shewmaker 
1813c9b1d098SAndrew Shewmaker /*
1814c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1815c9b1d098SAndrew Shewmaker  *
1816c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1817c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1818c9b1d098SAndrew Shewmaker  * mode.
1819c9b1d098SAndrew Shewmaker  *
1820c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1821c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1822c9b1d098SAndrew Shewmaker  */
1823c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1824c9b1d098SAndrew Shewmaker {
1825c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1826c9b1d098SAndrew Shewmaker 
1827c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1828c9b1d098SAndrew Shewmaker 
1829c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1830c9b1d098SAndrew Shewmaker 	return 0;
1831c9b1d098SAndrew Shewmaker }
1832a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
18334eeab4f5SAndrew Shewmaker 
18344eeab4f5SAndrew Shewmaker /*
18354eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
18364eeab4f5SAndrew Shewmaker  *
18374eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
18384eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
18394eeab4f5SAndrew Shewmaker  *
18404eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
18414eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
18424eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
18434eeab4f5SAndrew Shewmaker  */
18444eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
18454eeab4f5SAndrew Shewmaker {
18464eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
18474eeab4f5SAndrew Shewmaker 
1848c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
18494eeab4f5SAndrew Shewmaker 
18504eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
18514eeab4f5SAndrew Shewmaker 	return 0;
18524eeab4f5SAndrew Shewmaker }
1853a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1854