xref: /linux/mm/nommu.c (revision 524e00b36e8c547f5582eef3fb645a8d9fc5e3df)
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/backing-dev.h>
313b32123dSGideon Israel Dsouza #include <linux/compiler.h>
321da177e4SLinus Torvalds #include <linux/mount.h>
331da177e4SLinus Torvalds #include <linux/personality.h>
341da177e4SLinus Torvalds #include <linux/security.h>
351da177e4SLinus Torvalds #include <linux/syscalls.h>
36120a795dSAl Viro #include <linux/audit.h>
37b1de0d13SMitchel Humpherys #include <linux/printk.h>
381da177e4SLinus Torvalds 
397c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
401da177e4SLinus Torvalds #include <asm/tlb.h>
411da177e4SLinus Torvalds #include <asm/tlbflush.h>
42eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
438feae131SDavid Howells #include "internal.h"
448feae131SDavid Howells 
451da177e4SLinus Torvalds void *high_memory;
46944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
471da177e4SLinus Torvalds struct page *mem_map;
481da177e4SLinus Torvalds unsigned long max_mapnr;
495b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
504266c97aSHugh Dickins unsigned long highest_memmap_pfn;
51fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
521da177e4SLinus Torvalds int heap_stack_gap = 0;
531da177e4SLinus Torvalds 
5433e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
558feae131SDavid Howells 
561da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
571da177e4SLinus Torvalds 
588feae131SDavid Howells /* list of mapped, potentially shareable regions */
598feae131SDavid Howells static struct kmem_cache *vm_region_jar;
608feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
618feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
621da177e4SLinus Torvalds 
63f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
681da177e4SLinus Torvalds  * just what the caller asked for.
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	struct page *page;
751da177e4SLinus Torvalds 
764016a139SMichael Hennerich 	/*
774016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
784016a139SMichael Hennerich 	 * return size of 0
794016a139SMichael Hennerich 	 */
805a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
816cfd53fcSPaul Mundt 		return 0;
826cfd53fcSPaul Mundt 
836cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
846cfd53fcSPaul Mundt 
856cfd53fcSPaul Mundt 	/*
866cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
876cfd53fcSPaul Mundt 	 * kmalloc().
886cfd53fcSPaul Mundt 	 */
891da177e4SLinus Torvalds 	if (PageSlab(page))
901da177e4SLinus Torvalds 		return ksize(objp);
911da177e4SLinus Torvalds 
926cfd53fcSPaul Mundt 	/*
93ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
94ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
95ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
96ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
97ab2e83eaSPaul Mundt 	 */
98ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
99ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
100ab2e83eaSPaul Mundt 
101ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
102ab2e83eaSPaul Mundt 		if (vma)
103ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
104ab2e83eaSPaul Mundt 	}
105ab2e83eaSPaul Mundt 
106ab2e83eaSPaul Mundt 	/*
1076cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1085a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1096cfd53fcSPaul Mundt 	 */
110a50b854eSMatthew Wilcox (Oracle) 	return page_size(page);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
113dfc2f91aSPaul Mundt /**
114dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
115dfc2f91aSPaul Mundt  * @vma: memory mapping
116dfc2f91aSPaul Mundt  * @address: user virtual address
117dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
118dfc2f91aSPaul Mundt  *
119dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
120dfc2f91aSPaul Mundt  *
121dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
122dfc2f91aSPaul Mundt  */
123dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
124dfc2f91aSPaul Mundt 	unsigned long *pfn)
125dfc2f91aSPaul Mundt {
126dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
127dfc2f91aSPaul Mundt 		return -EINVAL;
128dfc2f91aSPaul Mundt 
129dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
130dfc2f91aSPaul Mundt 	return 0;
131dfc2f91aSPaul Mundt }
132dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
133dfc2f91aSPaul Mundt 
134f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
1351da177e4SLinus Torvalds 
136b3bdda02SChristoph Lameter void vfree(const void *addr)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	kfree(addr);
1391da177e4SLinus Torvalds }
140b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
1411da177e4SLinus Torvalds 
14288dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	/*
1458518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
1468518609dSRobert P. J. Day 	 * returns only a logical address.
1471da177e4SLinus Torvalds 	 */
14884097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1491da177e4SLinus Torvalds }
150b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
1511da177e4SLinus Torvalds 
152041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align,
153041de93fSChristoph Hellwig 		unsigned long start, unsigned long end, gfp_t gfp_mask,
154041de93fSChristoph Hellwig 		pgprot_t prot, unsigned long vm_flags, int node,
155041de93fSChristoph Hellwig 		const void *caller)
156041de93fSChristoph Hellwig {
157041de93fSChristoph Hellwig 	return __vmalloc(size, gfp_mask);
158041de93fSChristoph Hellwig }
159041de93fSChristoph Hellwig 
1602b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
1612b905948SChristoph Hellwig 		int node, const void *caller)
162a7c3e901SMichal Hocko {
1632b905948SChristoph Hellwig 	return __vmalloc(size, gfp_mask);
164a7c3e901SMichal Hocko }
165a7c3e901SMichal Hocko 
166ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
167f905bc44SPaul Mundt {
168f905bc44SPaul Mundt 	void *ret;
169f905bc44SPaul Mundt 
17088dca4caSChristoph Hellwig 	ret = __vmalloc(size, flags);
171f905bc44SPaul Mundt 	if (ret) {
172f905bc44SPaul Mundt 		struct vm_area_struct *vma;
173f905bc44SPaul Mundt 
174d8ed45c5SMichel Lespinasse 		mmap_write_lock(current->mm);
175f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
176f905bc44SPaul Mundt 		if (vma)
177f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
178d8ed45c5SMichel Lespinasse 		mmap_write_unlock(current->mm);
179f905bc44SPaul Mundt 	}
180f905bc44SPaul Mundt 
181f905bc44SPaul Mundt 	return ret;
182f905bc44SPaul Mundt }
183ed81745aSAndrii Nakryiko 
184ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size)
185ed81745aSAndrii Nakryiko {
186ed81745aSAndrii Nakryiko 	return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
187ed81745aSAndrii Nakryiko }
188f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
189f905bc44SPaul Mundt 
190b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	return virt_to_page(addr);
1931da177e4SLinus Torvalds }
194b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
1951da177e4SLinus Torvalds 
196b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
1991da177e4SLinus Torvalds }
200b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2031da177e4SLinus Torvalds {
2049bde916bSChen Gang 	/* Don't allow overflow */
2059bde916bSChen Gang 	if ((unsigned long) buf + count < count)
2069bde916bSChen Gang 		count = -(unsigned long) buf;
2079bde916bSChen Gang 
2081da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2091da177e4SLinus Torvalds 	return count;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
213e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
2141da177e4SLinus Torvalds  *
2151da177e4SLinus Torvalds  *	@size:		allocation size
2161da177e4SLinus Torvalds  *
2171da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
218e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
2191da177e4SLinus Torvalds  *
220c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2211da177e4SLinus Torvalds  *	use __vmalloc() instead.
2221da177e4SLinus Torvalds  */
2231da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2241da177e4SLinus Torvalds {
225176056fdSChen Li 	return __vmalloc(size, GFP_KERNEL);
2261da177e4SLinus Torvalds }
227f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
228f6138882SAndrew Morton 
2290fc74d82SLinus Torvalds void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __weak __alias(__vmalloc);
2300fc74d82SLinus Torvalds 
231e1ca7788SDave Young /*
232e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
233e1ca7788SDave Young  *
234e1ca7788SDave Young  *	@size:		allocation size
235e1ca7788SDave Young  *
236e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
237e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
238e1ca7788SDave Young  *	The memory allocated is set to zero.
239e1ca7788SDave Young  *
240e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
241e1ca7788SDave Young  *	use __vmalloc() instead.
242e1ca7788SDave Young  */
243e1ca7788SDave Young void *vzalloc(unsigned long size)
244e1ca7788SDave Young {
245176056fdSChen Li 	return __vmalloc(size, GFP_KERNEL | __GFP_ZERO);
246e1ca7788SDave Young }
247e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
248e1ca7788SDave Young 
249e1ca7788SDave Young /**
250e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
251e1ca7788SDave Young  * @size:	allocation size
252e1ca7788SDave Young  * @node:	numa node
253e1ca7788SDave Young  *
254e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
255e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
256e1ca7788SDave Young  *
257e1ca7788SDave Young  * For tight control over page level allocator and protection flags
258e1ca7788SDave Young  * use __vmalloc() instead.
259e1ca7788SDave Young  */
260f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
261f6138882SAndrew Morton {
262f6138882SAndrew Morton 	return vmalloc(size);
263f6138882SAndrew Morton }
2649a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
265e1ca7788SDave Young 
266e1ca7788SDave Young /**
267e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
268e1ca7788SDave Young  * @size:	allocation size
269e1ca7788SDave Young  * @node:	numa node
270e1ca7788SDave Young  *
271e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
272e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
273e1ca7788SDave Young  * The memory allocated is set to zero.
274e1ca7788SDave Young  *
275e1ca7788SDave Young  * For tight control over page level allocator and protection flags
276e1ca7788SDave Young  * use __vmalloc() instead.
277e1ca7788SDave Young  */
278e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
279e1ca7788SDave Young {
280e1ca7788SDave Young 	return vzalloc(size);
281e1ca7788SDave Young }
282e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2831da177e4SLinus Torvalds 
2841af446edSPaul Mundt /**
285b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
2861da177e4SLinus Torvalds  *	@size:		allocation size
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
289e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
2901da177e4SLinus Torvalds  */
2911da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
2921da177e4SLinus Torvalds {
29388dca4caSChristoph Hellwig 	return __vmalloc(size, GFP_KERNEL);
2941da177e4SLinus Torvalds }
295b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
296b5073173SPaul Mundt 
297b5073173SPaul Mundt /**
298b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
299b5073173SPaul Mundt  *	@size:		allocation size
300b5073173SPaul Mundt  *
301b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
302b5073173SPaul Mundt  * mapped to userspace without leaking data.
303f905bc44SPaul Mundt  *
304f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
305f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
306b5073173SPaul Mundt  */
307b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
308b5073173SPaul Mundt {
309f905bc44SPaul Mundt 	/*
310f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
311f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
312f905bc44SPaul Mundt 	 */
313f905bc44SPaul Mundt 	return vmalloc_user(size);
314b5073173SPaul Mundt }
315b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	BUG();
3201da177e4SLinus Torvalds 	return NULL;
3211da177e4SLinus Torvalds }
322b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3231da177e4SLinus Torvalds 
324b3bdda02SChristoph Lameter void vunmap(const void *addr)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	BUG();
3271da177e4SLinus Torvalds }
328b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3291da177e4SLinus Torvalds 
330d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
331eb6434d9SPaul Mundt {
332eb6434d9SPaul Mundt 	BUG();
333eb6434d9SPaul Mundt 	return NULL;
334eb6434d9SPaul Mundt }
335eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
336eb6434d9SPaul Mundt 
337eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
338eb6434d9SPaul Mundt {
339eb6434d9SPaul Mundt 	BUG();
340eb6434d9SPaul Mundt }
341eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
342eb6434d9SPaul Mundt 
343eb6434d9SPaul Mundt void vm_unmap_aliases(void)
344eb6434d9SPaul Mundt {
345eb6434d9SPaul Mundt }
346eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
347eb6434d9SPaul Mundt 
34829c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
34929c185e5SPaul Mundt {
35029c185e5SPaul Mundt 	BUG();
35129c185e5SPaul Mundt }
35229c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
35329c185e5SPaul Mundt 
354b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
355b5073173SPaul Mundt 		   struct page *page)
356b5073173SPaul Mundt {
357b5073173SPaul Mundt 	return -EINVAL;
358b5073173SPaul Mundt }
359b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
360b5073173SPaul Mundt 
361a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
362a667d745SSouptick Joarder 			unsigned long num)
363a667d745SSouptick Joarder {
364a667d745SSouptick Joarder 	return -EINVAL;
365a667d745SSouptick Joarder }
366a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages);
367a667d745SSouptick Joarder 
368a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
369a667d745SSouptick Joarder 				unsigned long num)
370a667d745SSouptick Joarder {
371a667d745SSouptick Joarder 	return -EINVAL;
372a667d745SSouptick Joarder }
373a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero);
374a667d745SSouptick Joarder 
3751eeb66a1SChristoph Hellwig /*
3761da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
3771da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
3781da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
3791da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
3801da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
3811da177e4SLinus Torvalds  */
3826a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
3871da177e4SLinus Torvalds 		return mm->brk;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (mm->brk == brk)
3901da177e4SLinus Torvalds 		return mm->brk;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/*
3931da177e4SLinus Torvalds 	 * Always allow shrinking brk
3941da177e4SLinus Torvalds 	 */
3951da177e4SLinus Torvalds 	if (brk <= mm->brk) {
3961da177e4SLinus Torvalds 		mm->brk = brk;
3971da177e4SLinus Torvalds 		return brk;
3981da177e4SLinus Torvalds 	}
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	/*
4011da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4021da177e4SLinus Torvalds 	 */
403a75a2df6SChristoph Hellwig 	flush_icache_user_range(mm->brk, brk);
4041da177e4SLinus Torvalds 	return mm->brk = brk;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4078feae131SDavid Howells /*
4083edf41d8Sseokhoon.yoon  * initialise the percpu counter for VM and region record slabs
4098feae131SDavid Howells  */
4108feae131SDavid Howells void __init mmap_init(void)
4111da177e4SLinus Torvalds {
41200a62ce9SKOSAKI Motohiro 	int ret;
41300a62ce9SKOSAKI Motohiro 
414908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
41500a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
4165d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
4178feae131SDavid Howells }
4181da177e4SLinus Torvalds 
4198feae131SDavid Howells /*
4208feae131SDavid Howells  * validate the region tree
4218feae131SDavid Howells  * - the caller must hold the region lock
4228feae131SDavid Howells  */
4238feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4248feae131SDavid Howells static noinline void validate_nommu_regions(void)
4258feae131SDavid Howells {
4268feae131SDavid Howells 	struct vm_region *region, *last;
4278feae131SDavid Howells 	struct rb_node *p, *lastp;
4281da177e4SLinus Torvalds 
4298feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
4308feae131SDavid Howells 	if (!lastp)
4318feae131SDavid Howells 		return;
4328feae131SDavid Howells 
4338feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
434c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
435c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
4368feae131SDavid Howells 
4378feae131SDavid Howells 	while ((p = rb_next(lastp))) {
4388feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
4398feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
4408feae131SDavid Howells 
441c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
442c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
443c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
4448feae131SDavid Howells 
4458feae131SDavid Howells 		lastp = p;
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds }
4488feae131SDavid Howells #else
44933e5d769SDavid Howells static void validate_nommu_regions(void)
45033e5d769SDavid Howells {
45133e5d769SDavid Howells }
4528feae131SDavid Howells #endif
4538feae131SDavid Howells 
4548feae131SDavid Howells /*
4558feae131SDavid Howells  * add a region into the global tree
4568feae131SDavid Howells  */
4578feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4588feae131SDavid Howells {
4598feae131SDavid Howells 	struct vm_region *pregion;
4608feae131SDavid Howells 	struct rb_node **p, *parent;
4618feae131SDavid Howells 
4628feae131SDavid Howells 	validate_nommu_regions();
4638feae131SDavid Howells 
4648feae131SDavid Howells 	parent = NULL;
4658feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
4668feae131SDavid Howells 	while (*p) {
4678feae131SDavid Howells 		parent = *p;
4688feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
4698feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
4708feae131SDavid Howells 			p = &(*p)->rb_left;
4718feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
4728feae131SDavid Howells 			p = &(*p)->rb_right;
4738feae131SDavid Howells 		else if (pregion == region)
4748feae131SDavid Howells 			return;
4758feae131SDavid Howells 		else
4768feae131SDavid Howells 			BUG();
4778feae131SDavid Howells 	}
4788feae131SDavid Howells 
4798feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
4808feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
4818feae131SDavid Howells 
4828feae131SDavid Howells 	validate_nommu_regions();
4838feae131SDavid Howells }
4848feae131SDavid Howells 
4858feae131SDavid Howells /*
4868feae131SDavid Howells  * delete a region from the global tree
4878feae131SDavid Howells  */
4888feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
4898feae131SDavid Howells {
4908feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
4918feae131SDavid Howells 
4928feae131SDavid Howells 	validate_nommu_regions();
4938feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
4948feae131SDavid Howells 	validate_nommu_regions();
4958feae131SDavid Howells }
4968feae131SDavid Howells 
4978feae131SDavid Howells /*
4988feae131SDavid Howells  * free a contiguous series of pages
4998feae131SDavid Howells  */
5008feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5018feae131SDavid Howells {
5028feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5039330723cSLinus Walleij 		struct page *page = virt_to_page((void *)from);
5048feae131SDavid Howells 
50533e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5068feae131SDavid Howells 		put_page(page);
5078feae131SDavid Howells 	}
5088feae131SDavid Howells }
5098feae131SDavid Howells 
5108feae131SDavid Howells /*
5118feae131SDavid Howells  * release a reference to a region
51233e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
513dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5148feae131SDavid Howells  *   will equal vm_start
5158feae131SDavid Howells  */
5168feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5178feae131SDavid Howells 	__releases(nommu_region_sem)
5188feae131SDavid Howells {
5198feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5208feae131SDavid Howells 
5211e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
522dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
5238feae131SDavid Howells 			delete_nommu_region(region);
5248feae131SDavid Howells 		up_write(&nommu_region_sem);
5258feae131SDavid Howells 
5268feae131SDavid Howells 		if (region->vm_file)
5278feae131SDavid Howells 			fput(region->vm_file);
5288feae131SDavid Howells 
5298feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
5308feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
53122cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
532dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
5338feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
5348feae131SDavid Howells 	} else {
5358feae131SDavid Howells 		up_write(&nommu_region_sem);
5368feae131SDavid Howells 	}
5378feae131SDavid Howells }
5388feae131SDavid Howells 
5398feae131SDavid Howells /*
5408feae131SDavid Howells  * release a reference to a region
5418feae131SDavid Howells  */
5428feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
5438feae131SDavid Howells {
5448feae131SDavid Howells 	down_write(&nommu_region_sem);
5458feae131SDavid Howells 	__put_nommu_region(region);
5468feae131SDavid Howells }
5471da177e4SLinus Torvalds 
548d4af56c5SLiam R. Howlett void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas)
549d4af56c5SLiam R. Howlett {
550d4af56c5SLiam R. Howlett 	mas_set_range(mas, vma->vm_start, vma->vm_end - 1);
551d4af56c5SLiam R. Howlett 	mas_store_prealloc(mas, vma);
552d4af56c5SLiam R. Howlett }
553d4af56c5SLiam R. Howlett 
554d4af56c5SLiam R. Howlett void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas)
555d4af56c5SLiam R. Howlett {
556d4af56c5SLiam R. Howlett 	mas->index = vma->vm_start;
557d4af56c5SLiam R. Howlett 	mas->last = vma->vm_end - 1;
558d4af56c5SLiam R. Howlett 	mas_store_prealloc(mas, NULL);
559d4af56c5SLiam R. Howlett }
560d4af56c5SLiam R. Howlett 
5613034097aSDavid Howells /*
5623034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
5638feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
5648feae131SDavid Howells  * page
565c1e8d7c6SMichel Lespinasse  * - should be called with mm->mmap_lock held writelocked
5663034097aSDavid Howells  */
5678feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
5683034097aSDavid Howells {
5698feae131SDavid Howells 	struct address_space *mapping;
570*524e00b3SLiam R. Howlett 	struct vm_area_struct *prev;
571*524e00b3SLiam R. Howlett 	MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end);
5723034097aSDavid Howells 
5738feae131SDavid Howells 	BUG_ON(!vma->vm_region);
5748feae131SDavid Howells 
5758feae131SDavid Howells 	mm->map_count++;
5768feae131SDavid Howells 	vma->vm_mm = mm;
5778feae131SDavid Howells 
5788feae131SDavid Howells 	/* add the VMA to the mapping */
5798feae131SDavid Howells 	if (vma->vm_file) {
5808feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
5818feae131SDavid Howells 
58283cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
5838feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
5846b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
5858feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
58683cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
5878feae131SDavid Howells 	}
5888feae131SDavid Howells 
589*524e00b3SLiam R. Howlett 	prev = mas_prev(&mas, 0);
590*524e00b3SLiam R. Howlett 	mas_reset(&mas);
5918feae131SDavid Howells 	/* add the VMA to the tree */
592*524e00b3SLiam R. Howlett 	vma_mas_store(vma, &mas);
593aba6dfb7SWei Yang 	__vma_link_list(mm, vma, prev);
5948feae131SDavid Howells }
5958feae131SDavid Howells 
5968feae131SDavid Howells /*
5978feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
5988feae131SDavid Howells  */
5998feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
6008feae131SDavid Howells {
601615d6e87SDavidlohr Bueso 	int i;
6028feae131SDavid Howells 	struct address_space *mapping;
6038feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
604615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
605*524e00b3SLiam R. Howlett 	MA_STATE(mas, &vma->vm_mm->mm_mt, 0, 0);
6068feae131SDavid Howells 
6078feae131SDavid Howells 	mm->map_count--;
608615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
609615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
610314ff785SIngo Molnar 		if (curr->vmacache.vmas[i] == vma) {
611e020d5bdSSteven Miao 			vmacache_invalidate(mm);
612615d6e87SDavidlohr Bueso 			break;
613615d6e87SDavidlohr Bueso 		}
614615d6e87SDavidlohr Bueso 	}
6158feae131SDavid Howells 
6168feae131SDavid Howells 	/* remove the VMA from the mapping */
6178feae131SDavid Howells 	if (vma->vm_file) {
6188feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6198feae131SDavid Howells 
62083cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6218feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6226b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
6238feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
62483cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6258feae131SDavid Howells 	}
6268feae131SDavid Howells 
6278feae131SDavid Howells 	/* remove from the MM's tree and list */
628*524e00b3SLiam R. Howlett 	vma_mas_remove(vma, &mas);
6291b9fc5b2SWei Yang 	__vma_unlink_list(mm, vma);
6308feae131SDavid Howells }
6318feae131SDavid Howells 
6328feae131SDavid Howells /*
6338feae131SDavid Howells  * destroy a VMA record
6348feae131SDavid Howells  */
6358feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
6368feae131SDavid Howells {
6378feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
6388feae131SDavid Howells 		vma->vm_ops->close(vma);
639e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
6408feae131SDavid Howells 		fput(vma->vm_file);
6418feae131SDavid Howells 	put_nommu_region(vma->vm_region);
6423928d4f5SLinus Torvalds 	vm_area_free(vma);
6433034097aSDavid Howells }
6443034097aSDavid Howells 
6453034097aSDavid Howells /*
6463034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
647c1e8d7c6SMichel Lespinasse  * - should be called with mm->mmap_lock at least held readlocked
6483034097aSDavid Howells  */
6493034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
6503034097aSDavid Howells {
6518feae131SDavid Howells 	struct vm_area_struct *vma;
652*524e00b3SLiam R. Howlett 	MA_STATE(mas, &mm->mm_mt, addr, addr);
6533034097aSDavid Howells 
6548feae131SDavid Howells 	/* check the cache first */
655615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
656615d6e87SDavidlohr Bueso 	if (likely(vma))
6578feae131SDavid Howells 		return vma;
6588feae131SDavid Howells 
659*524e00b3SLiam R. Howlett 	vma = mas_walk(&mas);
6603034097aSDavid Howells 
661*524e00b3SLiam R. Howlett 	if (vma)
662*524e00b3SLiam R. Howlett 		vmacache_update(addr, vma);
663*524e00b3SLiam R. Howlett 
664*524e00b3SLiam R. Howlett 	return vma;
6653034097aSDavid Howells }
6663034097aSDavid Howells EXPORT_SYMBOL(find_vma);
6673034097aSDavid Howells 
6683034097aSDavid Howells /*
669930e652aSDavid Howells  * find a VMA
670930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
671930e652aSDavid Howells  */
672930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
673930e652aSDavid Howells {
6747561e8caSDavid Howells 	return find_vma(mm, addr);
675930e652aSDavid Howells }
676930e652aSDavid Howells 
6778feae131SDavid Howells /*
6788feae131SDavid Howells  * expand a stack to a given address
6798feae131SDavid Howells  * - not supported under NOMMU conditions
6808feae131SDavid Howells  */
68157c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
68257c8f63eSGreg Ungerer {
68357c8f63eSGreg Ungerer 	return -ENOMEM;
68457c8f63eSGreg Ungerer }
68557c8f63eSGreg Ungerer 
686930e652aSDavid Howells /*
6876fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
688c1e8d7c6SMichel Lespinasse  * - should be called with mm->mmap_lock at least held readlocked
6896fa5f80bSDavid Howells  */
6908feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
6918feae131SDavid Howells 					     unsigned long addr,
6928feae131SDavid Howells 					     unsigned long len)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct vm_area_struct *vma;
6958feae131SDavid Howells 	unsigned long end = addr + len;
696*524e00b3SLiam R. Howlett 	MA_STATE(mas, &mm->mm_mt, addr, addr);
6971da177e4SLinus Torvalds 
6988feae131SDavid Howells 	/* check the cache first */
699615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
700615d6e87SDavidlohr Bueso 	if (vma)
7011da177e4SLinus Torvalds 		return vma;
7028feae131SDavid Howells 
703*524e00b3SLiam R. Howlett 	vma = mas_walk(&mas);
704*524e00b3SLiam R. Howlett 	if (!vma)
7058feae131SDavid Howells 		return NULL;
706*524e00b3SLiam R. Howlett 	if (vma->vm_start != addr)
707*524e00b3SLiam R. Howlett 		return NULL;
708*524e00b3SLiam R. Howlett 	if (vma->vm_end != end)
709*524e00b3SLiam R. Howlett 		return NULL;
710*524e00b3SLiam R. Howlett 
711615d6e87SDavidlohr Bueso 	vmacache_update(addr, vma);
7128feae131SDavid Howells 	return vma;
7138feae131SDavid Howells }
7141da177e4SLinus Torvalds 
7153034097aSDavid Howells /*
7161da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
7171da177e4SLinus Torvalds  * mapping we're capable of supporting
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
7201da177e4SLinus Torvalds 				 unsigned long addr,
7211da177e4SLinus Torvalds 				 unsigned long len,
7221da177e4SLinus Torvalds 				 unsigned long prot,
7231da177e4SLinus Torvalds 				 unsigned long flags,
7241da177e4SLinus Torvalds 				 unsigned long pgoff,
7251da177e4SLinus Torvalds 				 unsigned long *_capabilities)
7261da177e4SLinus Torvalds {
7278feae131SDavid Howells 	unsigned long capabilities, rlen;
7281da177e4SLinus Torvalds 	int ret;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	/* do the simple checks first */
73122cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
7321da177e4SLinus Torvalds 		return -EINVAL;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
7351da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
7361da177e4SLinus Torvalds 		return -EINVAL;
7371da177e4SLinus Torvalds 
738f81cff0dSMike Frysinger 	if (!len)
7391da177e4SLinus Torvalds 		return -EINVAL;
7401da177e4SLinus Torvalds 
741f81cff0dSMike Frysinger 	/* Careful about overflows.. */
7428feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
7438feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
744f81cff0dSMike Frysinger 		return -ENOMEM;
745f81cff0dSMike Frysinger 
7461da177e4SLinus Torvalds 	/* offset overflow? */
7478feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
748f81cff0dSMike Frysinger 		return -EOVERFLOW;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	if (file) {
7511da177e4SLinus Torvalds 		/* files must support mmap */
75272c2d531SAl Viro 		if (!file->f_op->mmap)
7531da177e4SLinus Torvalds 			return -ENODEV;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
7561da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
7571da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
7581da177e4SLinus Torvalds 		 */
759b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
760b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
761b4caecd4SChristoph Hellwig 		} else {
7621da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
7631da177e4SLinus Torvalds 			 * defaults */
764496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
7651da177e4SLinus Torvalds 			case S_IFREG:
7661da177e4SLinus Torvalds 			case S_IFBLK:
767b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
7681da177e4SLinus Torvalds 				break;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 			case S_IFCHR:
7711da177e4SLinus Torvalds 				capabilities =
772b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
773b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
774b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
7751da177e4SLinus Torvalds 				break;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 			default:
7781da177e4SLinus Torvalds 				return -EINVAL;
7791da177e4SLinus Torvalds 			}
7801da177e4SLinus Torvalds 		}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
7831da177e4SLinus Torvalds 		 * device */
7841da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
785b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
7866e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
787b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
7881da177e4SLinus Torvalds 
78928d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
79028d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
79128d7a6aeSGraff Yang 			return -EACCES;
79228d7a6aeSGraff Yang 
7931da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
7941da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
7951da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
7961da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
7971da177e4SLinus Torvalds 				return -EACCES;
7981da177e4SLinus Torvalds 
799496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
8001da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
8011da177e4SLinus Torvalds 				return -EACCES;
8021da177e4SLinus Torvalds 
803b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
8041da177e4SLinus Torvalds 				return -ENODEV;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
807b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
808ac714904SChoi Gi-yong 		} else {
8091da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
8101da177e4SLinus Torvalds 			 * allocate */
811b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
8121da177e4SLinus Torvalds 				return -ENODEV;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
8151da177e4SLinus Torvalds 			 * shared with the backing device */
8161da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
817b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8181da177e4SLinus Torvalds 		}
8191da177e4SLinus Torvalds 
820b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
821b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
822b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
823b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
8243c7b2045SBernd Schmidt 			    ) {
825b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8263c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
82722cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
8283c7b2045SBernd Schmidt 					return -EINVAL;
8293c7b2045SBernd Schmidt 				}
8303c7b2045SBernd Schmidt 			}
8313c7b2045SBernd Schmidt 		}
8323c7b2045SBernd Schmidt 
8331da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
8341da177e4SLinus Torvalds 		 * mappings */
83590f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
8361da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
8371da177e4SLinus Torvalds 				return -EPERM;
838ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
8391da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
8401da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
841b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
8421da177e4SLinus Torvalds 					prot |= PROT_EXEC;
8431da177e4SLinus Torvalds 			}
844ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
8451da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
846b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
8471da177e4SLinus Torvalds 			 ) {
8481da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
849b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
8501da177e4SLinus Torvalds 		}
851ac714904SChoi Gi-yong 	} else {
8521da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
8531da177e4SLinus Torvalds 		 * privately mapped
8541da177e4SLinus Torvalds 		 */
855b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
8581da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
8591da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
8601da177e4SLinus Torvalds 			prot |= PROT_EXEC;
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	/* allow the security API to have its say */
864e5467859SAl Viro 	ret = security_mmap_addr(addr);
865e5467859SAl Viro 	if (ret < 0)
866e5467859SAl Viro 		return ret;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/* looks okay */
8691da177e4SLinus Torvalds 	*_capabilities = capabilities;
8701da177e4SLinus Torvalds 	return 0;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds /*
8741da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
8751da177e4SLinus Torvalds  * now know into VMA flags
8761da177e4SLinus Torvalds  */
8771da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
8781da177e4SLinus Torvalds 					unsigned long prot,
8791da177e4SLinus Torvalds 					unsigned long flags,
8801da177e4SLinus Torvalds 					unsigned long capabilities)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	unsigned long vm_flags;
8831da177e4SLinus Torvalds 
884e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
8851da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
8861da177e4SLinus Torvalds 
887b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
8881da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
8893c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
8901da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
8911da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
8923c7b2045SBernd Schmidt 	} else {
8931da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
8941da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
8951da177e4SLinus Torvalds 		 * romfs/cramfs */
896b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
8971da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
8983c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
9021da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
9031da177e4SLinus Torvalds 	 * with another untraced process
9041da177e4SLinus Torvalds 	 */
905a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
9061da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	return vm_flags;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds /*
9128feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
9138feae131SDavid Howells  * pins the storage)
9141da177e4SLinus Torvalds  */
9158feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
9161da177e4SLinus Torvalds {
9171da177e4SLinus Torvalds 	int ret;
9181da177e4SLinus Torvalds 
919f74ac015SMiklos Szeredi 	ret = call_mmap(vma->vm_file, vma);
920dd8632a1SPaul Mundt 	if (ret == 0) {
921dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
922645d83c5SDavid Howells 		return 0;
923dd8632a1SPaul Mundt 	}
9241da177e4SLinus Torvalds 	if (ret != -ENOSYS)
9251da177e4SLinus Torvalds 		return ret;
9261da177e4SLinus Torvalds 
9273fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
9283fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
9293fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
9301da177e4SLinus Torvalds 	return -ENODEV;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds /*
9341da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
9351da177e4SLinus Torvalds  */
9368feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
9378feae131SDavid Howells 			   struct vm_region *region,
938645d83c5SDavid Howells 			   unsigned long len,
939645d83c5SDavid Howells 			   unsigned long capabilities)
9401da177e4SLinus Torvalds {
941dbc8358cSJoonsoo Kim 	unsigned long total, point;
9421da177e4SLinus Torvalds 	void *base;
9438feae131SDavid Howells 	int ret, order;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
9461da177e4SLinus Torvalds 	 * shared mappings on devices or memory
9471da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
9481da177e4SLinus Torvalds 	 */
949b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
950f74ac015SMiklos Szeredi 		ret = call_mmap(vma->vm_file, vma);
951dd8632a1SPaul Mundt 		if (ret == 0) {
9521da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
953dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
954dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
955645d83c5SDavid Howells 			return 0;
9561da177e4SLinus Torvalds 		}
957dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
958dd8632a1SPaul Mundt 			return ret;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
9611da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
9621da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9658feae131SDavid Howells 
9661da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
9671da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
9681da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
9691da177e4SLinus Torvalds 	 */
970f67d9b15SBob Liu 	order = get_order(len);
9718feae131SDavid Howells 	total = 1 << order;
972f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
973dd8632a1SPaul Mundt 
974dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
97522cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
976dbc8358cSJoonsoo Kim 		total = point;
9778feae131SDavid Howells 
978da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
979dbc8358cSJoonsoo Kim 	if (!base)
980dbc8358cSJoonsoo Kim 		goto enomem;
9818feae131SDavid Howells 
982dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
983dbc8358cSJoonsoo Kim 
9848feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
9858feae131SDavid Howells 	region->vm_start = (unsigned long) base;
986f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
987dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
9888feae131SDavid Howells 
9898feae131SDavid Howells 	vma->vm_start = region->vm_start;
9908feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (vma->vm_file) {
9931da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
9941da177e4SLinus Torvalds 		loff_t fpos;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
9971da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
9981da177e4SLinus Torvalds 
999b4bf802aSChristoph Hellwig 		ret = kernel_read(vma->vm_file, base, len, &fpos);
10001da177e4SLinus Torvalds 		if (ret < 0)
10011da177e4SLinus Torvalds 			goto error_free;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 		/* clear the last little bit */
1004f67d9b15SBob Liu 		if (ret < len)
1005f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
10061da177e4SLinus Torvalds 
1007bfd40eafSKirill A. Shutemov 	} else {
1008bfd40eafSKirill A. Shutemov 		vma_set_anonymous(vma);
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	return 0;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds error_free:
10147223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
10158feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
10168feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1017dd8632a1SPaul Mundt 	region->vm_top   = 0;
10181da177e4SLinus Torvalds 	return ret;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds enomem:
1021b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
102205ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
10239af744d7SMichal Hocko 	show_free_areas(0, NULL);
10241da177e4SLinus Torvalds 	return -ENOMEM;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds /*
10281da177e4SLinus Torvalds  * handle mapping creation for uClinux
10291da177e4SLinus Torvalds  */
10301fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
10311da177e4SLinus Torvalds 			unsigned long addr,
10321da177e4SLinus Torvalds 			unsigned long len,
10331da177e4SLinus Torvalds 			unsigned long prot,
10341da177e4SLinus Torvalds 			unsigned long flags,
1035bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
1036897ab3e0SMike Rapoport 			unsigned long *populate,
1037897ab3e0SMike Rapoport 			struct list_head *uf)
10381da177e4SLinus Torvalds {
10398feae131SDavid Howells 	struct vm_area_struct *vma;
10408feae131SDavid Howells 	struct vm_region *region;
10411da177e4SLinus Torvalds 	struct rb_node *rb;
104245e55300SPeter Collingbourne 	vm_flags_t vm_flags;
10431fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
10441da177e4SLinus Torvalds 	int ret;
10451da177e4SLinus Torvalds 
104641badc15SMichel Lespinasse 	*populate = 0;
1047bebeb3d6SMichel Lespinasse 
10481da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
10491da177e4SLinus Torvalds 	 * mapping */
10501da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
10511da177e4SLinus Torvalds 				    &capabilities);
105222cc877bSLeon Romanovsky 	if (ret < 0)
10531da177e4SLinus Torvalds 		return ret;
10541da177e4SLinus Torvalds 
105506aab5a3SDavid Howells 	/* we ignore the address hint */
105606aab5a3SDavid Howells 	addr = 0;
1057f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
105806aab5a3SDavid Howells 
10591da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
10601da177e4SLinus Torvalds 	 * now know into VMA flags */
106145e55300SPeter Collingbourne 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
10621da177e4SLinus Torvalds 
10638feae131SDavid Howells 	/* we're going to need to record the mapping */
10648feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
10658feae131SDavid Howells 	if (!region)
10668feae131SDavid Howells 		goto error_getting_region;
10671da177e4SLinus Torvalds 
1068490fc053SLinus Torvalds 	vma = vm_area_alloc(current->mm);
10698feae131SDavid Howells 	if (!vma)
10708feae131SDavid Howells 		goto error_getting_vma;
10711da177e4SLinus Torvalds 
10721e2ae599SDavid Howells 	region->vm_usage = 1;
10738feae131SDavid Howells 	region->vm_flags = vm_flags;
10748feae131SDavid Howells 	region->vm_pgoff = pgoff;
10758feae131SDavid Howells 
10768feae131SDavid Howells 	vma->vm_flags = vm_flags;
10778feae131SDavid Howells 	vma->vm_pgoff = pgoff;
10788feae131SDavid Howells 
10798feae131SDavid Howells 	if (file) {
1080cb0942b8SAl Viro 		region->vm_file = get_file(file);
1081cb0942b8SAl Viro 		vma->vm_file = get_file(file);
10828feae131SDavid Howells 	}
10838feae131SDavid Howells 
10848feae131SDavid Howells 	down_write(&nommu_region_sem);
10858feae131SDavid Howells 
10868feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
10871da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
10888feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
10891da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
10901da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
10911da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
10921da177e4SLinus Torvalds 	 *   than here
10931da177e4SLinus Torvalds 	 */
10941da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
10958feae131SDavid Howells 		struct vm_region *pregion;
10968feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
10971da177e4SLinus Torvalds 
10988feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
10998feae131SDavid Howells 		pgend = pgoff + pglen;
1100165b2392SDavid Howells 
11018feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
11028feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
11031da177e4SLinus Torvalds 
11048feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
11051da177e4SLinus Torvalds 				continue;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1108496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1109496ad9aaSAl Viro 			    file_inode(file))
11101da177e4SLinus Torvalds 				continue;
11111da177e4SLinus Torvalds 
11128feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
11131da177e4SLinus Torvalds 				continue;
11141da177e4SLinus Torvalds 
11158feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
11168feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
11178feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
11188feae131SDavid Howells 			if (pgoff >= rpgend)
11191da177e4SLinus Torvalds 				continue;
11201da177e4SLinus Torvalds 
11218feae131SDavid Howells 			/* handle inexactly overlapping matches between
11228feae131SDavid Howells 			 * mappings */
11238feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
11248feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
11258feae131SDavid Howells 				/* new mapping is not a subset of the region */
1126b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
11271da177e4SLinus Torvalds 					goto sharing_violation;
11281da177e4SLinus Torvalds 				continue;
11291da177e4SLinus Torvalds 			}
11301da177e4SLinus Torvalds 
11318feae131SDavid Howells 			/* we've found a region we can share */
11321e2ae599SDavid Howells 			pregion->vm_usage++;
11338feae131SDavid Howells 			vma->vm_region = pregion;
11348feae131SDavid Howells 			start = pregion->vm_start;
11358feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
11368feae131SDavid Howells 			vma->vm_start = start;
11378feae131SDavid Howells 			vma->vm_end = start + len;
11381da177e4SLinus Torvalds 
113922cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
11408feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
114122cc877bSLeon Romanovsky 			else {
11428feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
11438feae131SDavid Howells 				if (ret < 0) {
11448feae131SDavid Howells 					vma->vm_region = NULL;
11458feae131SDavid Howells 					vma->vm_start = 0;
11468feae131SDavid Howells 					vma->vm_end = 0;
11471e2ae599SDavid Howells 					pregion->vm_usage--;
11488feae131SDavid Howells 					pregion = NULL;
11498feae131SDavid Howells 					goto error_just_free;
11501da177e4SLinus Torvalds 				}
11518feae131SDavid Howells 			}
11528feae131SDavid Howells 			fput(region->vm_file);
11538feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
11548feae131SDavid Howells 			region = pregion;
11558feae131SDavid Howells 			result = start;
11568feae131SDavid Howells 			goto share;
11578feae131SDavid Howells 		}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
11601da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
11611da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
11621da177e4SLinus Torvalds 		 */
1163b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
11641da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
11651da177e4SLinus Torvalds 							     pgoff, flags);
1166bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
11671da177e4SLinus Torvalds 				ret = addr;
1168bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
11698feae131SDavid Howells 					goto error_just_free;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
11721da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
11731da177e4SLinus Torvalds 				 * it */
1174bb005a59SNamhyung Kim 				ret = -ENODEV;
1175b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
11768feae131SDavid Howells 					goto error_just_free;
11771da177e4SLinus Torvalds 
1178b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
11798feae131SDavid Howells 			} else {
11808feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
11818feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
11821da177e4SLinus Torvalds 			}
11831da177e4SLinus Torvalds 		}
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 
11868feae131SDavid Howells 	vma->vm_region = region;
11871da177e4SLinus Torvalds 
1188645d83c5SDavid Howells 	/* set up the mapping
1189b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1190645d83c5SDavid Howells 	 */
11911da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
11928feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
11931da177e4SLinus Torvalds 	else
1194645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
11951da177e4SLinus Torvalds 	if (ret < 0)
1196645d83c5SDavid Howells 		goto error_just_free;
1197645d83c5SDavid Howells 	add_nommu_region(region);
11988feae131SDavid Howells 
1199ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
12000bf5f949SChristoph Hellwig 	if (!vma->vm_file &&
12010bf5f949SChristoph Hellwig 	    (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
12020bf5f949SChristoph Hellwig 	     !(flags & MAP_UNINITIALIZED)))
1203ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1204ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1205ea637639SJie Zhang 
12061da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
12078feae131SDavid Howells 	result = vma->vm_start;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
12101da177e4SLinus Torvalds 
12118feae131SDavid Howells share:
12128feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
12131da177e4SLinus Torvalds 
1214cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1215cfe79c00SMike Frysinger 	 * mapping of it is made  */
1216cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1217a75a2df6SChristoph Hellwig 		flush_icache_user_range(region->vm_start, region->vm_end);
1218cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1219cfe79c00SMike Frysinger 	}
12201da177e4SLinus Torvalds 
1221cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
12221da177e4SLinus Torvalds 
12238feae131SDavid Howells 	return result;
12241da177e4SLinus Torvalds 
12258feae131SDavid Howells error_just_free:
12268feae131SDavid Howells 	up_write(&nommu_region_sem);
12278feae131SDavid Howells error:
122889a86402SDavid Howells 	if (region->vm_file)
12298feae131SDavid Howells 		fput(region->vm_file);
12308feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
123189a86402SDavid Howells 	if (vma->vm_file)
12328feae131SDavid Howells 		fput(vma->vm_file);
12333928d4f5SLinus Torvalds 	vm_area_free(vma);
12341da177e4SLinus Torvalds 	return ret;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds sharing_violation:
12378feae131SDavid Howells 	up_write(&nommu_region_sem);
123822cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
12398feae131SDavid Howells 	ret = -EINVAL;
12408feae131SDavid Howells 	goto error;
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds error_getting_vma:
12438feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
124422cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
12451da177e4SLinus Torvalds 			len, current->pid);
12469af744d7SMichal Hocko 	show_free_areas(0, NULL);
12471da177e4SLinus Torvalds 	return -ENOMEM;
12481da177e4SLinus Torvalds 
12498feae131SDavid Howells error_getting_region:
125022cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
12511da177e4SLinus Torvalds 			len, current->pid);
12529af744d7SMichal Hocko 	show_free_areas(0, NULL);
12531da177e4SLinus Torvalds 	return -ENOMEM;
12541da177e4SLinus Torvalds }
12556be5ceb0SLinus Torvalds 
1256a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1257a90f590aSDominik Brodowski 			      unsigned long prot, unsigned long flags,
1258a90f590aSDominik Brodowski 			      unsigned long fd, unsigned long pgoff)
125966f0dc48SHugh Dickins {
126066f0dc48SHugh Dickins 	struct file *file = NULL;
126166f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
126266f0dc48SHugh Dickins 
1263120a795dSAl Viro 	audit_mmap_fd(fd, flags);
126466f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
126566f0dc48SHugh Dickins 		file = fget(fd);
126666f0dc48SHugh Dickins 		if (!file)
126766f0dc48SHugh Dickins 			goto out;
126866f0dc48SHugh Dickins 	}
126966f0dc48SHugh Dickins 
1270ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
127166f0dc48SHugh Dickins 
127266f0dc48SHugh Dickins 	if (file)
127366f0dc48SHugh Dickins 		fput(file);
127466f0dc48SHugh Dickins out:
127566f0dc48SHugh Dickins 	return retval;
127666f0dc48SHugh Dickins }
127766f0dc48SHugh Dickins 
1278a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1279a90f590aSDominik Brodowski 		unsigned long, prot, unsigned long, flags,
1280a90f590aSDominik Brodowski 		unsigned long, fd, unsigned long, pgoff)
1281a90f590aSDominik Brodowski {
1282a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1283a90f590aSDominik Brodowski }
1284a90f590aSDominik Brodowski 
1285a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1286a4679373SChristoph Hellwig struct mmap_arg_struct {
1287a4679373SChristoph Hellwig 	unsigned long addr;
1288a4679373SChristoph Hellwig 	unsigned long len;
1289a4679373SChristoph Hellwig 	unsigned long prot;
1290a4679373SChristoph Hellwig 	unsigned long flags;
1291a4679373SChristoph Hellwig 	unsigned long fd;
1292a4679373SChristoph Hellwig 	unsigned long offset;
1293a4679373SChristoph Hellwig };
1294a4679373SChristoph Hellwig 
1295a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1296a4679373SChristoph Hellwig {
1297a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1298a4679373SChristoph Hellwig 
1299a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1300a4679373SChristoph Hellwig 		return -EFAULT;
13011824cb75SAlexander Kuleshov 	if (offset_in_page(a.offset))
1302a4679373SChristoph Hellwig 		return -EINVAL;
1303a4679373SChristoph Hellwig 
1304a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1305a4679373SChristoph Hellwig 			       a.offset >> PAGE_SHIFT);
1306a4679373SChristoph Hellwig }
1307a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1308a4679373SChristoph Hellwig 
13091da177e4SLinus Torvalds /*
13108feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
13118feae131SDavid Howells  * for the first part or the tail.
13121da177e4SLinus Torvalds  */
13138feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
13148feae131SDavid Howells 	      unsigned long addr, int new_below)
13151da177e4SLinus Torvalds {
13168feae131SDavid Howells 	struct vm_area_struct *new;
13178feae131SDavid Howells 	struct vm_region *region;
13188feae131SDavid Howells 	unsigned long npages;
13191da177e4SLinus Torvalds 
1320779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1321779c1023SDavid Howells 	 * only a single usage on the region) */
1322779c1023SDavid Howells 	if (vma->vm_file)
13238feae131SDavid Howells 		return -ENOMEM;
13241da177e4SLinus Torvalds 
13258feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
13268feae131SDavid Howells 		return -ENOMEM;
13271da177e4SLinus Torvalds 
13288feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
13298feae131SDavid Howells 	if (!region)
13308feae131SDavid Howells 		return -ENOMEM;
13318feae131SDavid Howells 
13323928d4f5SLinus Torvalds 	new = vm_area_dup(vma);
13338feae131SDavid Howells 	if (!new) {
13348feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
13358feae131SDavid Howells 		return -ENOMEM;
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 
13388feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
13398feae131SDavid Howells 	*region = *vma->vm_region;
13408feae131SDavid Howells 	new->vm_region = region;
13418feae131SDavid Howells 
13428feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
13438feae131SDavid Howells 
13448feae131SDavid Howells 	if (new_below) {
1345dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
13468feae131SDavid Howells 	} else {
13478feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
13488feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
13491da177e4SLinus Torvalds 	}
13508feae131SDavid Howells 
13518feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
13528feae131SDavid Howells 		new->vm_ops->open(new);
13538feae131SDavid Howells 
13548feae131SDavid Howells 	delete_vma_from_mm(vma);
13558feae131SDavid Howells 	down_write(&nommu_region_sem);
13568feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
13578feae131SDavid Howells 	if (new_below) {
13588feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
13598feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
13608feae131SDavid Howells 	} else {
13618feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1362dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
13638feae131SDavid Howells 	}
13648feae131SDavid Howells 	add_nommu_region(vma->vm_region);
13658feae131SDavid Howells 	add_nommu_region(new->vm_region);
13668feae131SDavid Howells 	up_write(&nommu_region_sem);
13678feae131SDavid Howells 	add_vma_to_mm(mm, vma);
13688feae131SDavid Howells 	add_vma_to_mm(mm, new);
13698feae131SDavid Howells 	return 0;
13708feae131SDavid Howells }
13718feae131SDavid Howells 
13728feae131SDavid Howells /*
13738feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
13748feae131SDavid Howells  * the end
13758feae131SDavid Howells  */
13768feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
13778feae131SDavid Howells 		      struct vm_area_struct *vma,
13788feae131SDavid Howells 		      unsigned long from, unsigned long to)
13798feae131SDavid Howells {
13808feae131SDavid Howells 	struct vm_region *region;
13818feae131SDavid Howells 
13828feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
13838feae131SDavid Howells 	 * and list */
13848feae131SDavid Howells 	delete_vma_from_mm(vma);
13858feae131SDavid Howells 	if (from > vma->vm_start)
13868feae131SDavid Howells 		vma->vm_end = from;
13878feae131SDavid Howells 	else
13888feae131SDavid Howells 		vma->vm_start = to;
13898feae131SDavid Howells 	add_vma_to_mm(mm, vma);
13908feae131SDavid Howells 
13918feae131SDavid Howells 	/* cut the backing region down to size */
13928feae131SDavid Howells 	region = vma->vm_region;
13931e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
13948feae131SDavid Howells 
13958feae131SDavid Howells 	down_write(&nommu_region_sem);
13968feae131SDavid Howells 	delete_nommu_region(region);
1397dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1398dd8632a1SPaul Mundt 		to = region->vm_top;
1399dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1400dd8632a1SPaul Mundt 	} else {
14018feae131SDavid Howells 		region->vm_start = to;
1402dd8632a1SPaul Mundt 	}
14038feae131SDavid Howells 	add_nommu_region(region);
14048feae131SDavid Howells 	up_write(&nommu_region_sem);
14058feae131SDavid Howells 
14068feae131SDavid Howells 	free_page_series(from, to);
14078feae131SDavid Howells 	return 0;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14103034097aSDavid Howells /*
14113034097aSDavid Howells  * release a mapping
14128feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
14138feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
14143034097aSDavid Howells  */
1415897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
14161da177e4SLinus Torvalds {
14178feae131SDavid Howells 	struct vm_area_struct *vma;
1418f67d9b15SBob Liu 	unsigned long end;
14198feae131SDavid Howells 	int ret;
14201da177e4SLinus Torvalds 
1421f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
14228feae131SDavid Howells 	if (len == 0)
14231da177e4SLinus Torvalds 		return -EINVAL;
14241da177e4SLinus Torvalds 
1425f67d9b15SBob Liu 	end = start + len;
1426f67d9b15SBob Liu 
14278feae131SDavid Howells 	/* find the first potentially overlapping VMA */
14288feae131SDavid Howells 	vma = find_vma(mm, start);
14298feae131SDavid Howells 	if (!vma) {
1430ac714904SChoi Gi-yong 		static int limit;
143133e5d769SDavid Howells 		if (limit < 5) {
143222cc877bSLeon Romanovsky 			pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
143333e5d769SDavid Howells 					current->pid, current->comm,
143433e5d769SDavid Howells 					start, start + len - 1);
143533e5d769SDavid Howells 			limit++;
143633e5d769SDavid Howells 		}
14378feae131SDavid Howells 		return -EINVAL;
14388feae131SDavid Howells 	}
14391da177e4SLinus Torvalds 
14408feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
14418feae131SDavid Howells 	if (vma->vm_file) {
14428feae131SDavid Howells 		do {
144322cc877bSLeon Romanovsky 			if (start > vma->vm_start)
14448feae131SDavid Howells 				return -EINVAL;
14458feae131SDavid Howells 			if (end == vma->vm_end)
14468feae131SDavid Howells 				goto erase_whole_vma;
1447d75a310cSNamhyung Kim 			vma = vma->vm_next;
1448d75a310cSNamhyung Kim 		} while (vma);
14498feae131SDavid Howells 		return -EINVAL;
14508feae131SDavid Howells 	} else {
14518feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
14528feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
14538feae131SDavid Howells 			goto erase_whole_vma;
145422cc877bSLeon Romanovsky 		if (start < vma->vm_start || end > vma->vm_end)
14558feae131SDavid Howells 			return -EINVAL;
14561824cb75SAlexander Kuleshov 		if (offset_in_page(start))
14578feae131SDavid Howells 			return -EINVAL;
14581824cb75SAlexander Kuleshov 		if (end != vma->vm_end && offset_in_page(end))
14598feae131SDavid Howells 			return -EINVAL;
14608feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
14618feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
146222cc877bSLeon Romanovsky 			if (ret < 0)
14638feae131SDavid Howells 				return ret;
14648feae131SDavid Howells 		}
14658feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
14668feae131SDavid Howells 	}
14671da177e4SLinus Torvalds 
14688feae131SDavid Howells erase_whole_vma:
14698feae131SDavid Howells 	delete_vma_from_mm(vma);
14708feae131SDavid Howells 	delete_vma(mm, vma);
14711da177e4SLinus Torvalds 	return 0;
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds 
1474bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
14753034097aSDavid Howells {
1476bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
14773034097aSDavid Howells 	int ret;
14783034097aSDavid Howells 
1479d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
1480897ab3e0SMike Rapoport 	ret = do_munmap(mm, addr, len, NULL);
1481d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
14823034097aSDavid Howells 	return ret;
14833034097aSDavid Howells }
1484a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1485a46ef99dSLinus Torvalds 
1486a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1487a46ef99dSLinus Torvalds {
1488bfce281cSAl Viro 	return vm_munmap(addr, len);
1489a46ef99dSLinus Torvalds }
14903034097aSDavid Howells 
14913034097aSDavid Howells /*
14928feae131SDavid Howells  * release all the mappings made in a process's VM space
14933034097aSDavid Howells  */
14941da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
14951da177e4SLinus Torvalds {
14968feae131SDavid Howells 	struct vm_area_struct *vma;
14971da177e4SLinus Torvalds 
14988feae131SDavid Howells 	if (!mm)
14998feae131SDavid Howells 		return;
15008feae131SDavid Howells 
15011da177e4SLinus Torvalds 	mm->total_vm = 0;
15021da177e4SLinus Torvalds 
15038feae131SDavid Howells 	while ((vma = mm->mmap)) {
15048feae131SDavid Howells 		mm->mmap = vma->vm_next;
15058feae131SDavid Howells 		delete_vma_from_mm(vma);
15068feae131SDavid Howells 		delete_vma(mm, vma);
150704c34961SSteven J. Magnani 		cond_resched();
15081da177e4SLinus Torvalds 	}
1509*524e00b3SLiam R. Howlett 	__mt_destroy(&mm->mm_mt);
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15125d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
15131da177e4SLinus Torvalds {
15141da177e4SLinus Torvalds 	return -ENOMEM;
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds /*
15186fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
15196fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
15201da177e4SLinus Torvalds  *
15216fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
15228feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
15238feae131SDavid Howells  * block is not shareable
15241da177e4SLinus Torvalds  *
15256fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
15261da177e4SLinus Torvalds  */
15274b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
15281da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
15291da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
15301da177e4SLinus Torvalds {
15316fa5f80bSDavid Howells 	struct vm_area_struct *vma;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	/* insanity checks first */
1534f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1535f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
15368feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
15371da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
15381da177e4SLinus Torvalds 
15391824cb75SAlexander Kuleshov 	if (offset_in_page(addr))
15408feae131SDavid Howells 		return -EINVAL;
15418feae131SDavid Howells 
15421da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
15431da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
15441da177e4SLinus Torvalds 
15458feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
15466fa5f80bSDavid Howells 	if (!vma)
15471da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
15481da177e4SLinus Torvalds 
15496fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
15501da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
15511da177e4SLinus Torvalds 
15526fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
15531da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
15541da177e4SLinus Torvalds 
15558feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
15561da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	/* all checks complete - do it */
15596fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
15606fa5f80bSDavid Howells 	return vma->vm_start;
15616fa5f80bSDavid Howells }
15626fa5f80bSDavid Howells 
15636a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
15646a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
15656a6160a7SHeiko Carstens 		unsigned long, new_addr)
15666fa5f80bSDavid Howells {
15676fa5f80bSDavid Howells 	unsigned long ret;
15686fa5f80bSDavid Howells 
1569d8ed45c5SMichel Lespinasse 	mmap_write_lock(current->mm);
15706fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1571d8ed45c5SMichel Lespinasse 	mmap_write_unlock(current->mm);
15726fa5f80bSDavid Howells 	return ret;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
1575df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1576df06b37fSKeith Busch 			 unsigned int foll_flags)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	return NULL;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15818f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
15828f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
15831da177e4SLinus Torvalds {
15848f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
15858f3b1327SBob Liu 		return -EINVAL;
15868f3b1327SBob Liu 
1587314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
158866aa2b4bSGreg Ungerer 	return 0;
15891da177e4SLinus Torvalds }
159022c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
15911da177e4SLinus Torvalds 
15923c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
15933c0b9de6SLinus Torvalds {
15943c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
15953c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
15963c0b9de6SLinus Torvalds 
15973c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
15983c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
15993c0b9de6SLinus Torvalds }
16003c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
16013c0b9de6SLinus Torvalds 
1602f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1603f905bc44SPaul Mundt 			unsigned long pgoff)
1604f905bc44SPaul Mundt {
1605f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1606f905bc44SPaul Mundt 
1607f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1608f905bc44SPaul Mundt 		return -EINVAL;
1609f905bc44SPaul Mundt 
1610f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1611f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1612f905bc44SPaul Mundt 
1613f905bc44SPaul Mundt 	return 0;
1614f905bc44SPaul Mundt }
1615f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1616f905bc44SPaul Mundt 
16172bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
1618b0e15190SDavid Howells {
1619b0e15190SDavid Howells 	BUG();
1620d0217ac0SNick Piggin 	return 0;
1621b0e15190SDavid Howells }
1622b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
16230ec76a11SDavid Howells 
16243f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf,
1625bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1626f1820361SKirill A. Shutemov {
1627f1820361SKirill A. Shutemov 	BUG();
16283f98a28cSGeert Uytterhoeven 	return 0;
1629f1820361SKirill A. Shutemov }
1630f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1631f1820361SKirill A. Shutemov 
1632d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
1633d3f5ffcaSJohn Hubbard 		       int len, unsigned int gup_flags)
16340ec76a11SDavid Howells {
16350ec76a11SDavid Howells 	struct vm_area_struct *vma;
1636442486ecSLorenzo Stoakes 	int write = gup_flags & FOLL_WRITE;
16370ec76a11SDavid Howells 
1638d8ed45c5SMichel Lespinasse 	if (mmap_read_lock_killable(mm))
16391e426fe2SKonstantin Khlebnikov 		return 0;
16400ec76a11SDavid Howells 
16410ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
16420159b141SDavid Howells 	vma = find_vma(mm, addr);
16430159b141SDavid Howells 	if (vma) {
16440ec76a11SDavid Howells 		/* don't overrun this mapping */
16450ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
16460ec76a11SDavid Howells 			len = vma->vm_end - addr;
16470ec76a11SDavid Howells 
16480ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1649d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
16507959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
16517959722bSJie Zhang 					 (void *) addr, buf, len);
1652d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
16537959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
16547959722bSJie Zhang 					    buf, (void *) addr, len);
16550ec76a11SDavid Howells 		else
16560ec76a11SDavid Howells 			len = 0;
16570ec76a11SDavid Howells 	} else {
16580ec76a11SDavid Howells 		len = 0;
16590ec76a11SDavid Howells 	}
16600ec76a11SDavid Howells 
1661d8ed45c5SMichel Lespinasse 	mmap_read_unlock(mm);
1662f55f199bSMike Frysinger 
1663f55f199bSMike Frysinger 	return len;
1664f55f199bSMike Frysinger }
1665f55f199bSMike Frysinger 
1666f55f199bSMike Frysinger /**
1667b7701a5fSMike Rapoport  * access_remote_vm - access another process' address space
1668f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1669f55f199bSMike Frysinger  * @addr:	start address to access
1670f55f199bSMike Frysinger  * @buf:	source or destination buffer
1671f55f199bSMike Frysinger  * @len:	number of bytes to transfer
16726347e8d5SLorenzo Stoakes  * @gup_flags:	flags modifying lookup behaviour
1673f55f199bSMike Frysinger  *
1674f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1675f55f199bSMike Frysinger  */
1676f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
16776347e8d5SLorenzo Stoakes 		void *buf, int len, unsigned int gup_flags)
1678f55f199bSMike Frysinger {
1679d3f5ffcaSJohn Hubbard 	return __access_remote_vm(mm, addr, buf, len, gup_flags);
1680f55f199bSMike Frysinger }
1681f55f199bSMike Frysinger 
1682f55f199bSMike Frysinger /*
1683f55f199bSMike Frysinger  * Access another process' address space.
1684f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1685f55f199bSMike Frysinger  */
1686f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1687f307ab6dSLorenzo Stoakes 		unsigned int gup_flags)
1688f55f199bSMike Frysinger {
1689f55f199bSMike Frysinger 	struct mm_struct *mm;
1690f55f199bSMike Frysinger 
1691f55f199bSMike Frysinger 	if (addr + len < addr)
1692f55f199bSMike Frysinger 		return 0;
1693f55f199bSMike Frysinger 
1694f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1695f55f199bSMike Frysinger 	if (!mm)
1696f55f199bSMike Frysinger 		return 0;
1697f55f199bSMike Frysinger 
1698d3f5ffcaSJohn Hubbard 	len = __access_remote_vm(mm, addr, buf, len, gup_flags);
1699f55f199bSMike Frysinger 
17000ec76a11SDavid Howells 	mmput(mm);
17010ec76a11SDavid Howells 	return len;
17020ec76a11SDavid Howells }
1703fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
17047e660872SDavid Howells 
17057e660872SDavid Howells /**
17067e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
17077e660872SDavid Howells  * @inode: The inode to check
17087e660872SDavid Howells  * @size: The current filesize of the inode
17097e660872SDavid Howells  * @newsize: The proposed filesize of the inode
17107e660872SDavid Howells  *
17117e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
1712c08b342cSRandy Dunlap  * make sure that any outstanding VMAs aren't broken and then shrink the
1713c08b342cSRandy Dunlap  * vm_regions that extend beyond so that do_mmap() doesn't
17147e660872SDavid Howells  * automatically grant mappings that are too large.
17157e660872SDavid Howells  */
17167e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
17177e660872SDavid Howells 				size_t newsize)
17187e660872SDavid Howells {
17197e660872SDavid Howells 	struct vm_area_struct *vma;
17207e660872SDavid Howells 	struct vm_region *region;
17217e660872SDavid Howells 	pgoff_t low, high;
17227e660872SDavid Howells 	size_t r_size, r_top;
17237e660872SDavid Howells 
17247e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
17257e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
17267e660872SDavid Howells 
17277e660872SDavid Howells 	down_write(&nommu_region_sem);
17281acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
17297e660872SDavid Howells 
17307e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
17316b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
17327e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
17337e660872SDavid Howells 		 * cache */
17347e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
17351acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
17367e660872SDavid Howells 			up_write(&nommu_region_sem);
17377e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
17387e660872SDavid Howells 		}
17397e660872SDavid Howells 	}
17407e660872SDavid Howells 
17417e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
17427e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
17437e660872SDavid Howells 	 *
17447e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
17457e660872SDavid Howells 	 * shouldn't be any
17467e660872SDavid Howells 	 */
17471acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
17487e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
17497e660872SDavid Howells 			continue;
17507e660872SDavid Howells 
17517e660872SDavid Howells 		region = vma->vm_region;
17527e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
17537e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
17547e660872SDavid Howells 
17557e660872SDavid Howells 		if (r_top > newsize) {
17567e660872SDavid Howells 			region->vm_top -= r_top - newsize;
17577e660872SDavid Howells 			if (region->vm_end > region->vm_top)
17587e660872SDavid Howells 				region->vm_end = region->vm_top;
17597e660872SDavid Howells 		}
17607e660872SDavid Howells 	}
17617e660872SDavid Howells 
17621acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
17637e660872SDavid Howells 	up_write(&nommu_region_sem);
17647e660872SDavid Howells 	return 0;
17657e660872SDavid Howells }
1766c9b1d098SAndrew Shewmaker 
1767c9b1d098SAndrew Shewmaker /*
1768c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1769c9b1d098SAndrew Shewmaker  *
1770c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1771c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1772c9b1d098SAndrew Shewmaker  * mode.
1773c9b1d098SAndrew Shewmaker  *
1774c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1775c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1776c9b1d098SAndrew Shewmaker  */
1777c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1778c9b1d098SAndrew Shewmaker {
1779c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1780c9b1d098SAndrew Shewmaker 
1781c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1782c9b1d098SAndrew Shewmaker 
1783c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1784c9b1d098SAndrew Shewmaker 	return 0;
1785c9b1d098SAndrew Shewmaker }
1786a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
17874eeab4f5SAndrew Shewmaker 
17884eeab4f5SAndrew Shewmaker /*
17894eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
17904eeab4f5SAndrew Shewmaker  *
17914eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
17924eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
17934eeab4f5SAndrew Shewmaker  *
17944eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
17954eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
17964eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
17974eeab4f5SAndrew Shewmaker  */
17984eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
17994eeab4f5SAndrew Shewmaker {
18004eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
18014eeab4f5SAndrew Shewmaker 
1802c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
18034eeab4f5SAndrew Shewmaker 
18044eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
18054eeab4f5SAndrew Shewmaker 	return 0;
18064eeab4f5SAndrew Shewmaker }
1807a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1808