xref: /linux/mm/nommu.c (revision a90f590a1bee36fc2129cfb38ceec24a555bb12d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/nommu.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Replacement code for mm functions to support CPU's that don't
51da177e4SLinus Torvalds  *  have any form of memory management unit (thus no virtual memory).
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  See Documentation/nommu-mmap.txt
81da177e4SLinus Torvalds  *
98feae131SDavid Howells  *  Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
101da177e4SLinus Torvalds  *  Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
111da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
121da177e4SLinus Torvalds  *  Copyright (c) 2002      Greg Ungerer <gerg@snapgear.com>
1329c185e5SPaul Mundt  *  Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
16b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17b1de0d13SMitchel Humpherys 
18b95f1b31SPaul Gortmaker #include <linux/export.h>
191da177e4SLinus Torvalds #include <linux/mm.h>
206e84f315SIngo Molnar #include <linux/sched/mm.h>
21615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
221da177e4SLinus Torvalds #include <linux/mman.h>
231da177e4SLinus Torvalds #include <linux/swap.h>
241da177e4SLinus Torvalds #include <linux/file.h>
251da177e4SLinus Torvalds #include <linux/highmem.h>
261da177e4SLinus Torvalds #include <linux/pagemap.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/vmalloc.h>
291da177e4SLinus Torvalds #include <linux/blkdev.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 	 */
1105a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(page);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1130d731759SLorenzo Stoakes static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
11428a35716SMichel Lespinasse 		      unsigned long start, unsigned long nr_pages,
11528a35716SMichel Lespinasse 		      unsigned int foll_flags, struct page **pages,
11628a35716SMichel Lespinasse 		      struct vm_area_struct **vmas, int *nonblocking)
1171da177e4SLinus Torvalds {
118910e46daSSonic Zhang 	struct vm_area_struct *vma;
1197b4d5b8bSDavid Howells 	unsigned long vm_flags;
1207b4d5b8bSDavid Howells 	int i;
1217b4d5b8bSDavid Howells 
1227b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
12358fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1247b4d5b8bSDavid Howells 	 */
12558fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
12658fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
12758fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
12858fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1291da177e4SLinus Torvalds 
1309d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
1317561e8caSDavid Howells 		vma = find_vma(mm, start);
132910e46daSSonic Zhang 		if (!vma)
1337b4d5b8bSDavid Howells 			goto finish_or_fault;
1347b4d5b8bSDavid Howells 
1357b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1361c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1371c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1387b4d5b8bSDavid Howells 			goto finish_or_fault;
139910e46daSSonic Zhang 
1401da177e4SLinus Torvalds 		if (pages) {
1411da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1421da177e4SLinus Torvalds 			if (pages[i])
14309cbfeafSKirill A. Shutemov 				get_page(pages[i]);
1441da177e4SLinus Torvalds 		}
1451da177e4SLinus Torvalds 		if (vmas)
146910e46daSSonic Zhang 			vmas[i] = vma;
147e1ee65d8SDavid Howells 		start = (start + PAGE_SIZE) & PAGE_MASK;
1481da177e4SLinus Torvalds 	}
1497b4d5b8bSDavid Howells 
1507b4d5b8bSDavid Howells 	return i;
1517b4d5b8bSDavid Howells 
1527b4d5b8bSDavid Howells finish_or_fault:
1537b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1541da177e4SLinus Torvalds }
155b291f000SNick Piggin 
156b291f000SNick Piggin /*
157b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
158b291f000SNick Piggin  * and indicate the VMA that covers each page
159b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
160b291f000SNick Piggin  *   slab page or a secondary page from a compound page
161b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
162b291f000SNick Piggin  */
163c12d2da5SIngo Molnar long get_user_pages(unsigned long start, unsigned long nr_pages,
164768ae309SLorenzo Stoakes 		    unsigned int gup_flags, struct page **pages,
16528a35716SMichel Lespinasse 		    struct vm_area_struct **vmas)
166b291f000SNick Piggin {
167768ae309SLorenzo Stoakes 	return __get_user_pages(current, current->mm, start, nr_pages,
168768ae309SLorenzo Stoakes 				gup_flags, pages, vmas, NULL);
169b291f000SNick Piggin }
170c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages);
17166aa2b4bSGreg Ungerer 
172c12d2da5SIngo Molnar long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
1733b913179SLorenzo Stoakes 			    unsigned int gup_flags, struct page **pages,
174f0818f47SAndrea Arcangeli 			    int *locked)
175f0818f47SAndrea Arcangeli {
176768ae309SLorenzo Stoakes 	return get_user_pages(start, nr_pages, gup_flags, pages, NULL);
177f0818f47SAndrea Arcangeli }
178c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages_locked);
179f0818f47SAndrea Arcangeli 
1808b7457efSLorenzo Stoakes static long __get_user_pages_unlocked(struct task_struct *tsk,
1818b7457efSLorenzo Stoakes 			struct mm_struct *mm, unsigned long start,
1828b7457efSLorenzo Stoakes 			unsigned long nr_pages, struct page **pages,
1838b7457efSLorenzo Stoakes 			unsigned int gup_flags)
184f0818f47SAndrea Arcangeli {
185f0818f47SAndrea Arcangeli 	long ret;
186f0818f47SAndrea Arcangeli 	down_read(&mm->mmap_sem);
187cde70140SDave Hansen 	ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages,
188cde70140SDave Hansen 				NULL, NULL);
189f0818f47SAndrea Arcangeli 	up_read(&mm->mmap_sem);
190f0818f47SAndrea Arcangeli 	return ret;
191f0818f47SAndrea Arcangeli }
1920fd71a56SAndrea Arcangeli 
193c12d2da5SIngo Molnar long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
194c164154fSLorenzo Stoakes 			     struct page **pages, unsigned int gup_flags)
1950fd71a56SAndrea Arcangeli {
196cde70140SDave Hansen 	return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
197c164154fSLorenzo Stoakes 					 pages, gup_flags);
1980fd71a56SAndrea Arcangeli }
199c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages_unlocked);
200f0818f47SAndrea Arcangeli 
201dfc2f91aSPaul Mundt /**
202dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
203dfc2f91aSPaul Mundt  * @vma: memory mapping
204dfc2f91aSPaul Mundt  * @address: user virtual address
205dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
206dfc2f91aSPaul Mundt  *
207dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
208dfc2f91aSPaul Mundt  *
209dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
210dfc2f91aSPaul Mundt  */
211dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
212dfc2f91aSPaul Mundt 	unsigned long *pfn)
213dfc2f91aSPaul Mundt {
214dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
215dfc2f91aSPaul Mundt 		return -EINVAL;
216dfc2f91aSPaul Mundt 
217dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
218dfc2f91aSPaul Mundt 	return 0;
219dfc2f91aSPaul Mundt }
220dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
221dfc2f91aSPaul Mundt 
222f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
2231da177e4SLinus Torvalds 
224b3bdda02SChristoph Lameter void vfree(const void *addr)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	kfree(addr);
2271da177e4SLinus Torvalds }
228b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2291da177e4SLinus Torvalds 
230dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	/*
2338518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2348518609dSRobert P. J. Day 	 * returns only a logical address.
2351da177e4SLinus Torvalds 	 */
23684097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2371da177e4SLinus Torvalds }
238b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2391da177e4SLinus Torvalds 
240a7c3e901SMichal Hocko void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
241a7c3e901SMichal Hocko {
242a7c3e901SMichal Hocko 	return __vmalloc(size, flags, PAGE_KERNEL);
243a7c3e901SMichal Hocko }
244a7c3e901SMichal Hocko 
245f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
246f905bc44SPaul Mundt {
247f905bc44SPaul Mundt 	void *ret;
248f905bc44SPaul Mundt 
24919809c2dSMichal Hocko 	ret = __vmalloc(size, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL);
250f905bc44SPaul Mundt 	if (ret) {
251f905bc44SPaul Mundt 		struct vm_area_struct *vma;
252f905bc44SPaul Mundt 
253f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
254f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
255f905bc44SPaul Mundt 		if (vma)
256f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
257f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
258f905bc44SPaul Mundt 	}
259f905bc44SPaul Mundt 
260f905bc44SPaul Mundt 	return ret;
261f905bc44SPaul Mundt }
262f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
263f905bc44SPaul Mundt 
264b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	return virt_to_page(addr);
2671da177e4SLinus Torvalds }
268b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
2691da177e4SLinus Torvalds 
270b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2731da177e4SLinus Torvalds }
274b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2771da177e4SLinus Torvalds {
2789bde916bSChen Gang 	/* Don't allow overflow */
2799bde916bSChen Gang 	if ((unsigned long) buf + count < count)
2809bde916bSChen Gang 		count = -(unsigned long) buf;
2819bde916bSChen Gang 
2821da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2831da177e4SLinus Torvalds 	return count;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	/* Don't allow overflow */
2891da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2901da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	memcpy(addr, buf, count);
293ac714904SChoi Gi-yong 	return count;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
297e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
2981da177e4SLinus Torvalds  *
2991da177e4SLinus Torvalds  *	@size:		allocation size
3001da177e4SLinus Torvalds  *
3011da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
302e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
3031da177e4SLinus Torvalds  *
304c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
3051da177e4SLinus Torvalds  *	use __vmalloc() instead.
3061da177e4SLinus Torvalds  */
3071da177e4SLinus Torvalds void *vmalloc(unsigned long size)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
3101da177e4SLinus Torvalds }
311f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
312f6138882SAndrew Morton 
313e1ca7788SDave Young /*
314e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
315e1ca7788SDave Young  *
316e1ca7788SDave Young  *	@size:		allocation size
317e1ca7788SDave Young  *
318e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
319e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
320e1ca7788SDave Young  *	The memory allocated is set to zero.
321e1ca7788SDave Young  *
322e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
323e1ca7788SDave Young  *	use __vmalloc() instead.
324e1ca7788SDave Young  */
325e1ca7788SDave Young void *vzalloc(unsigned long size)
326e1ca7788SDave Young {
327e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
328e1ca7788SDave Young 			PAGE_KERNEL);
329e1ca7788SDave Young }
330e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
331e1ca7788SDave Young 
332e1ca7788SDave Young /**
333e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
334e1ca7788SDave Young  * @size:	allocation size
335e1ca7788SDave Young  * @node:	numa node
336e1ca7788SDave Young  *
337e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
338e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
339e1ca7788SDave Young  *
340e1ca7788SDave Young  * For tight control over page level allocator and protection flags
341e1ca7788SDave Young  * use __vmalloc() instead.
342e1ca7788SDave Young  */
343f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
344f6138882SAndrew Morton {
345f6138882SAndrew Morton 	return vmalloc(size);
346f6138882SAndrew Morton }
3479a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
348e1ca7788SDave Young 
349e1ca7788SDave Young /**
350e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
351e1ca7788SDave Young  * @size:	allocation size
352e1ca7788SDave Young  * @node:	numa node
353e1ca7788SDave Young  *
354e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
355e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
356e1ca7788SDave Young  * The memory allocated is set to zero.
357e1ca7788SDave Young  *
358e1ca7788SDave Young  * For tight control over page level allocator and protection flags
359e1ca7788SDave Young  * use __vmalloc() instead.
360e1ca7788SDave Young  */
361e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
362e1ca7788SDave Young {
363e1ca7788SDave Young 	return vzalloc(size);
364e1ca7788SDave Young }
365e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3661da177e4SLinus Torvalds 
3671af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
3681af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
3691af446edSPaul Mundt #endif
3701af446edSPaul Mundt 
3711af446edSPaul Mundt /**
3721af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
3731af446edSPaul Mundt  *	@size:		allocation size
3741af446edSPaul Mundt  *
3751af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
3761af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
3771af446edSPaul Mundt  *	executable kernel virtual space.
3781af446edSPaul Mundt  *
3791af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3801af446edSPaul Mundt  *	use __vmalloc() instead.
3811af446edSPaul Mundt  */
3821af446edSPaul Mundt 
3831af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3841af446edSPaul Mundt {
3851af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3861af446edSPaul Mundt }
3871af446edSPaul Mundt 
388b5073173SPaul Mundt /**
389b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3901da177e4SLinus Torvalds  *	@size:		allocation size
3911da177e4SLinus Torvalds  *
3921da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
393e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
3981da177e4SLinus Torvalds }
399b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
400b5073173SPaul Mundt 
401b5073173SPaul Mundt /**
402b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
403b5073173SPaul Mundt  *	@size:		allocation size
404b5073173SPaul Mundt  *
405b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
406b5073173SPaul Mundt  * mapped to userspace without leaking data.
407f905bc44SPaul Mundt  *
408f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
409f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
410b5073173SPaul Mundt  */
411b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
412b5073173SPaul Mundt {
413f905bc44SPaul Mundt 	/*
414f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
415f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
416f905bc44SPaul Mundt 	 */
417f905bc44SPaul Mundt 	return vmalloc_user(size);
418b5073173SPaul Mundt }
419b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	BUG();
4241da177e4SLinus Torvalds 	return NULL;
4251da177e4SLinus Torvalds }
426b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
4271da177e4SLinus Torvalds 
428b3bdda02SChristoph Lameter void vunmap(const void *addr)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	BUG();
4311da177e4SLinus Torvalds }
432b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
4331da177e4SLinus Torvalds 
434eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
435eb6434d9SPaul Mundt {
436eb6434d9SPaul Mundt 	BUG();
437eb6434d9SPaul Mundt 	return NULL;
438eb6434d9SPaul Mundt }
439eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
440eb6434d9SPaul Mundt 
441eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
442eb6434d9SPaul Mundt {
443eb6434d9SPaul Mundt 	BUG();
444eb6434d9SPaul Mundt }
445eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
446eb6434d9SPaul Mundt 
447eb6434d9SPaul Mundt void vm_unmap_aliases(void)
448eb6434d9SPaul Mundt {
449eb6434d9SPaul Mundt }
450eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
451eb6434d9SPaul Mundt 
4521da177e4SLinus Torvalds /*
4531eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
4541eeb66a1SChristoph Hellwig  * have one.
4551eeb66a1SChristoph Hellwig  */
4563b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
4571eeb66a1SChristoph Hellwig {
4581eeb66a1SChristoph Hellwig }
4591eeb66a1SChristoph Hellwig 
46029c185e5SPaul Mundt /**
46129c185e5SPaul Mundt  *	alloc_vm_area - allocate a range of kernel address space
46229c185e5SPaul Mundt  *	@size:		size of the area
46329c185e5SPaul Mundt  *
46429c185e5SPaul Mundt  *	Returns:	NULL on failure, vm_struct on success
46529c185e5SPaul Mundt  *
46629c185e5SPaul Mundt  *	This function reserves a range of kernel address space, and
46729c185e5SPaul Mundt  *	allocates pagetables to map that range.  No actual mappings
46829c185e5SPaul Mundt  *	are created.  If the kernel address space is not shared
46929c185e5SPaul Mundt  *	between processes, it syncs the pagetable across all
47029c185e5SPaul Mundt  *	processes.
47129c185e5SPaul Mundt  */
472cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
47329c185e5SPaul Mundt {
47429c185e5SPaul Mundt 	BUG();
47529c185e5SPaul Mundt 	return NULL;
47629c185e5SPaul Mundt }
47729c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
47829c185e5SPaul Mundt 
47929c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
48029c185e5SPaul Mundt {
48129c185e5SPaul Mundt 	BUG();
48229c185e5SPaul Mundt }
48329c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
48429c185e5SPaul Mundt 
485b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
486b5073173SPaul Mundt 		   struct page *page)
487b5073173SPaul Mundt {
488b5073173SPaul Mundt 	return -EINVAL;
489b5073173SPaul Mundt }
490b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
491b5073173SPaul Mundt 
4921eeb66a1SChristoph Hellwig /*
4931da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4941da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4951da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4961da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4971da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4981da177e4SLinus Torvalds  */
4996a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
5041da177e4SLinus Torvalds 		return mm->brk;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (mm->brk == brk)
5071da177e4SLinus Torvalds 		return mm->brk;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/*
5101da177e4SLinus Torvalds 	 * Always allow shrinking brk
5111da177e4SLinus Torvalds 	 */
5121da177e4SLinus Torvalds 	if (brk <= mm->brk) {
5131da177e4SLinus Torvalds 		mm->brk = brk;
5141da177e4SLinus Torvalds 		return brk;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/*
5181da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
5191da177e4SLinus Torvalds 	 */
520cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
5211da177e4SLinus Torvalds 	return mm->brk = brk;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
5248feae131SDavid Howells /*
5253edf41d8Sseokhoon.yoon  * initialise the percpu counter for VM and region record slabs
5268feae131SDavid Howells  */
5278feae131SDavid Howells void __init mmap_init(void)
5281da177e4SLinus Torvalds {
52900a62ce9SKOSAKI Motohiro 	int ret;
53000a62ce9SKOSAKI Motohiro 
531908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
53200a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
5335d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
5348feae131SDavid Howells }
5351da177e4SLinus Torvalds 
5368feae131SDavid Howells /*
5378feae131SDavid Howells  * validate the region tree
5388feae131SDavid Howells  * - the caller must hold the region lock
5398feae131SDavid Howells  */
5408feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
5418feae131SDavid Howells static noinline void validate_nommu_regions(void)
5428feae131SDavid Howells {
5438feae131SDavid Howells 	struct vm_region *region, *last;
5448feae131SDavid Howells 	struct rb_node *p, *lastp;
5451da177e4SLinus Torvalds 
5468feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
5478feae131SDavid Howells 	if (!lastp)
5488feae131SDavid Howells 		return;
5498feae131SDavid Howells 
5508feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
551c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
552c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
5538feae131SDavid Howells 
5548feae131SDavid Howells 	while ((p = rb_next(lastp))) {
5558feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
5568feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
5578feae131SDavid Howells 
558c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
559c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
560c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
5618feae131SDavid Howells 
5628feae131SDavid Howells 		lastp = p;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds }
5658feae131SDavid Howells #else
56633e5d769SDavid Howells static void validate_nommu_regions(void)
56733e5d769SDavid Howells {
56833e5d769SDavid Howells }
5698feae131SDavid Howells #endif
5708feae131SDavid Howells 
5718feae131SDavid Howells /*
5728feae131SDavid Howells  * add a region into the global tree
5738feae131SDavid Howells  */
5748feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
5758feae131SDavid Howells {
5768feae131SDavid Howells 	struct vm_region *pregion;
5778feae131SDavid Howells 	struct rb_node **p, *parent;
5788feae131SDavid Howells 
5798feae131SDavid Howells 	validate_nommu_regions();
5808feae131SDavid Howells 
5818feae131SDavid Howells 	parent = NULL;
5828feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
5838feae131SDavid Howells 	while (*p) {
5848feae131SDavid Howells 		parent = *p;
5858feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5868feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5878feae131SDavid Howells 			p = &(*p)->rb_left;
5888feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5898feae131SDavid Howells 			p = &(*p)->rb_right;
5908feae131SDavid Howells 		else if (pregion == region)
5918feae131SDavid Howells 			return;
5928feae131SDavid Howells 		else
5938feae131SDavid Howells 			BUG();
5948feae131SDavid Howells 	}
5958feae131SDavid Howells 
5968feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5978feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5988feae131SDavid Howells 
5998feae131SDavid Howells 	validate_nommu_regions();
6008feae131SDavid Howells }
6018feae131SDavid Howells 
6028feae131SDavid Howells /*
6038feae131SDavid Howells  * delete a region from the global tree
6048feae131SDavid Howells  */
6058feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
6068feae131SDavid Howells {
6078feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6088feae131SDavid Howells 
6098feae131SDavid Howells 	validate_nommu_regions();
6108feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
6118feae131SDavid Howells 	validate_nommu_regions();
6128feae131SDavid Howells }
6138feae131SDavid Howells 
6148feae131SDavid Howells /*
6158feae131SDavid Howells  * free a contiguous series of pages
6168feae131SDavid Howells  */
6178feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
6188feae131SDavid Howells {
6198feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
6208feae131SDavid Howells 		struct page *page = virt_to_page(from);
6218feae131SDavid Howells 
62233e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
6238feae131SDavid Howells 		put_page(page);
6248feae131SDavid Howells 	}
6258feae131SDavid Howells }
6268feae131SDavid Howells 
6278feae131SDavid Howells /*
6288feae131SDavid Howells  * release a reference to a region
62933e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
630dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
6318feae131SDavid Howells  *   will equal vm_start
6328feae131SDavid Howells  */
6338feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
6348feae131SDavid Howells 	__releases(nommu_region_sem)
6358feae131SDavid Howells {
6368feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6378feae131SDavid Howells 
6381e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
639dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
6408feae131SDavid Howells 			delete_nommu_region(region);
6418feae131SDavid Howells 		up_write(&nommu_region_sem);
6428feae131SDavid Howells 
6438feae131SDavid Howells 		if (region->vm_file)
6448feae131SDavid Howells 			fput(region->vm_file);
6458feae131SDavid Howells 
6468feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
6478feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
64822cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
649dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
6508feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
6518feae131SDavid Howells 	} else {
6528feae131SDavid Howells 		up_write(&nommu_region_sem);
6538feae131SDavid Howells 	}
6548feae131SDavid Howells }
6558feae131SDavid Howells 
6568feae131SDavid Howells /*
6578feae131SDavid Howells  * release a reference to a region
6588feae131SDavid Howells  */
6598feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
6608feae131SDavid Howells {
6618feae131SDavid Howells 	down_write(&nommu_region_sem);
6628feae131SDavid Howells 	__put_nommu_region(region);
6638feae131SDavid Howells }
6641da177e4SLinus Torvalds 
6653034097aSDavid Howells /*
666eb8cdec4SBernd Schmidt  * update protection on a vma
667eb8cdec4SBernd Schmidt  */
668eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
669eb8cdec4SBernd Schmidt {
670eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
671eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
672eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
673eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
674eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
675eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
676eb8cdec4SBernd Schmidt 	}
677eb8cdec4SBernd Schmidt 	update_protections(mm);
678eb8cdec4SBernd Schmidt #endif
679eb8cdec4SBernd Schmidt }
680eb8cdec4SBernd Schmidt 
681eb8cdec4SBernd Schmidt /*
6823034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
6838feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
6848feae131SDavid Howells  * page
6853034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
6863034097aSDavid Howells  */
6878feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
6883034097aSDavid Howells {
6896038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
6908feae131SDavid Howells 	struct address_space *mapping;
6916038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
6923034097aSDavid Howells 
6938feae131SDavid Howells 	BUG_ON(!vma->vm_region);
6948feae131SDavid Howells 
6958feae131SDavid Howells 	mm->map_count++;
6968feae131SDavid Howells 	vma->vm_mm = mm;
6978feae131SDavid Howells 
698eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
699eb8cdec4SBernd Schmidt 
7008feae131SDavid Howells 	/* add the VMA to the mapping */
7018feae131SDavid Howells 	if (vma->vm_file) {
7028feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7038feae131SDavid Howells 
70483cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
7058feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7066b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
7078feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
70883cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
7098feae131SDavid Howells 	}
7108feae131SDavid Howells 
7118feae131SDavid Howells 	/* add the VMA to the tree */
7126038def0SNamhyung Kim 	parent = rb_prev = NULL;
7138feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
7148feae131SDavid Howells 	while (*p) {
7158feae131SDavid Howells 		parent = *p;
7168feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
7178feae131SDavid Howells 
7188feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
7198feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
7208feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
7218feae131SDavid Howells 			p = &(*p)->rb_left;
7226038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
7236038def0SNamhyung Kim 			rb_prev = parent;
7248feae131SDavid Howells 			p = &(*p)->rb_right;
7256038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
7268feae131SDavid Howells 			p = &(*p)->rb_left;
7276038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
7286038def0SNamhyung Kim 			rb_prev = parent;
7298feae131SDavid Howells 			p = &(*p)->rb_right;
7306038def0SNamhyung Kim 		} else if (vma < pvma)
7318feae131SDavid Howells 			p = &(*p)->rb_left;
7326038def0SNamhyung Kim 		else if (vma > pvma) {
7336038def0SNamhyung Kim 			rb_prev = parent;
7348feae131SDavid Howells 			p = &(*p)->rb_right;
7356038def0SNamhyung Kim 		} else
7368feae131SDavid Howells 			BUG();
7378feae131SDavid Howells 	}
7388feae131SDavid Howells 
7398feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
7408feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
7418feae131SDavid Howells 
7428feae131SDavid Howells 	/* add VMA to the VMA list also */
7436038def0SNamhyung Kim 	prev = NULL;
7446038def0SNamhyung Kim 	if (rb_prev)
7456038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
7463034097aSDavid Howells 
7476038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
7488feae131SDavid Howells }
7498feae131SDavid Howells 
7508feae131SDavid Howells /*
7518feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
7528feae131SDavid Howells  */
7538feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
7548feae131SDavid Howells {
755615d6e87SDavidlohr Bueso 	int i;
7568feae131SDavid Howells 	struct address_space *mapping;
7578feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
758615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
7598feae131SDavid Howells 
760eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
761eb8cdec4SBernd Schmidt 
7628feae131SDavid Howells 	mm->map_count--;
763615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
764615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
765314ff785SIngo Molnar 		if (curr->vmacache.vmas[i] == vma) {
766e020d5bdSSteven Miao 			vmacache_invalidate(mm);
767615d6e87SDavidlohr Bueso 			break;
768615d6e87SDavidlohr Bueso 		}
769615d6e87SDavidlohr Bueso 	}
7708feae131SDavid Howells 
7718feae131SDavid Howells 	/* remove the VMA from the mapping */
7728feae131SDavid Howells 	if (vma->vm_file) {
7738feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7748feae131SDavid Howells 
77583cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
7768feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7776b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
7788feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
77983cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
7808feae131SDavid Howells 	}
7818feae131SDavid Howells 
7828feae131SDavid Howells 	/* remove from the MM's tree and list */
7838feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
784b951bf2cSNamhyung Kim 
785b951bf2cSNamhyung Kim 	if (vma->vm_prev)
786b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
787b951bf2cSNamhyung Kim 	else
788b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
789b951bf2cSNamhyung Kim 
790b951bf2cSNamhyung Kim 	if (vma->vm_next)
791b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
7928feae131SDavid Howells }
7938feae131SDavid Howells 
7948feae131SDavid Howells /*
7958feae131SDavid Howells  * destroy a VMA record
7968feae131SDavid Howells  */
7978feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
7988feae131SDavid Howells {
7998feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
8008feae131SDavid Howells 		vma->vm_ops->close(vma);
801e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
8028feae131SDavid Howells 		fput(vma->vm_file);
8038feae131SDavid Howells 	put_nommu_region(vma->vm_region);
8048feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
8053034097aSDavid Howells }
8063034097aSDavid Howells 
8073034097aSDavid Howells /*
8083034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
8093034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8103034097aSDavid Howells  */
8113034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
8123034097aSDavid Howells {
8138feae131SDavid Howells 	struct vm_area_struct *vma;
8143034097aSDavid Howells 
8158feae131SDavid Howells 	/* check the cache first */
816615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
817615d6e87SDavidlohr Bueso 	if (likely(vma))
8188feae131SDavid Howells 		return vma;
8198feae131SDavid Howells 
820e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8218feae131SDavid Howells 	 * resides) */
822e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8238feae131SDavid Howells 		if (vma->vm_start > addr)
8248feae131SDavid Howells 			return NULL;
8258feae131SDavid Howells 		if (vma->vm_end > addr) {
826615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
8278feae131SDavid Howells 			return vma;
8283034097aSDavid Howells 		}
8298feae131SDavid Howells 	}
8303034097aSDavid Howells 
8313034097aSDavid Howells 	return NULL;
8323034097aSDavid Howells }
8333034097aSDavid Howells EXPORT_SYMBOL(find_vma);
8343034097aSDavid Howells 
8353034097aSDavid Howells /*
836930e652aSDavid Howells  * find a VMA
837930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
838930e652aSDavid Howells  */
839930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
840930e652aSDavid Howells {
8417561e8caSDavid Howells 	return find_vma(mm, addr);
842930e652aSDavid Howells }
843930e652aSDavid Howells 
8448feae131SDavid Howells /*
8458feae131SDavid Howells  * expand a stack to a given address
8468feae131SDavid Howells  * - not supported under NOMMU conditions
8478feae131SDavid Howells  */
84857c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
84957c8f63eSGreg Ungerer {
85057c8f63eSGreg Ungerer 	return -ENOMEM;
85157c8f63eSGreg Ungerer }
85257c8f63eSGreg Ungerer 
853930e652aSDavid Howells /*
8546fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
8556fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8566fa5f80bSDavid Howells  */
8578feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
8588feae131SDavid Howells 					     unsigned long addr,
8598feae131SDavid Howells 					     unsigned long len)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct vm_area_struct *vma;
8628feae131SDavid Howells 	unsigned long end = addr + len;
8631da177e4SLinus Torvalds 
8648feae131SDavid Howells 	/* check the cache first */
865615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
866615d6e87SDavidlohr Bueso 	if (vma)
8671da177e4SLinus Torvalds 		return vma;
8688feae131SDavid Howells 
869e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8708feae131SDavid Howells 	 * resides) */
871e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8728feae131SDavid Howells 		if (vma->vm_start < addr)
8738feae131SDavid Howells 			continue;
8748feae131SDavid Howells 		if (vma->vm_start > addr)
8758feae131SDavid Howells 			return NULL;
8768feae131SDavid Howells 		if (vma->vm_end == end) {
877615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
8788feae131SDavid Howells 			return vma;
8798feae131SDavid Howells 		}
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	return NULL;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8853034097aSDavid Howells /*
8861da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
8871da177e4SLinus Torvalds  * mapping we're capable of supporting
8881da177e4SLinus Torvalds  */
8891da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
8901da177e4SLinus Torvalds 				 unsigned long addr,
8911da177e4SLinus Torvalds 				 unsigned long len,
8921da177e4SLinus Torvalds 				 unsigned long prot,
8931da177e4SLinus Torvalds 				 unsigned long flags,
8941da177e4SLinus Torvalds 				 unsigned long pgoff,
8951da177e4SLinus Torvalds 				 unsigned long *_capabilities)
8961da177e4SLinus Torvalds {
8978feae131SDavid Howells 	unsigned long capabilities, rlen;
8981da177e4SLinus Torvalds 	int ret;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	/* do the simple checks first */
90122cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
9021da177e4SLinus Torvalds 		return -EINVAL;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
9051da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
9061da177e4SLinus Torvalds 		return -EINVAL;
9071da177e4SLinus Torvalds 
908f81cff0dSMike Frysinger 	if (!len)
9091da177e4SLinus Torvalds 		return -EINVAL;
9101da177e4SLinus Torvalds 
911f81cff0dSMike Frysinger 	/* Careful about overflows.. */
9128feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
9138feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
914f81cff0dSMike Frysinger 		return -ENOMEM;
915f81cff0dSMike Frysinger 
9161da177e4SLinus Torvalds 	/* offset overflow? */
9178feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
918f81cff0dSMike Frysinger 		return -EOVERFLOW;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	if (file) {
9211da177e4SLinus Torvalds 		/* files must support mmap */
92272c2d531SAl Viro 		if (!file->f_op->mmap)
9231da177e4SLinus Torvalds 			return -ENODEV;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
9261da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
9271da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
9281da177e4SLinus Torvalds 		 */
929b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
930b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
931b4caecd4SChristoph Hellwig 		} else {
9321da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
9331da177e4SLinus Torvalds 			 * defaults */
934496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
9351da177e4SLinus Torvalds 			case S_IFREG:
9361da177e4SLinus Torvalds 			case S_IFBLK:
937b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
9381da177e4SLinus Torvalds 				break;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 			case S_IFCHR:
9411da177e4SLinus Torvalds 				capabilities =
942b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
943b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
944b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
9451da177e4SLinus Torvalds 				break;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 			default:
9481da177e4SLinus Torvalds 				return -EINVAL;
9491da177e4SLinus Torvalds 			}
9501da177e4SLinus Torvalds 		}
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
9531da177e4SLinus Torvalds 		 * device */
9541da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
955b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
9566e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
957b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
9581da177e4SLinus Torvalds 
95928d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
96028d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
96128d7a6aeSGraff Yang 			return -EACCES;
96228d7a6aeSGraff Yang 
9631da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
9641da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
9651da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
9661da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
9671da177e4SLinus Torvalds 				return -EACCES;
9681da177e4SLinus Torvalds 
969496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
9701da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
9711da177e4SLinus Torvalds 				return -EACCES;
9721da177e4SLinus Torvalds 
973d7a06983SJeff Layton 			if (locks_verify_locked(file))
9741da177e4SLinus Torvalds 				return -EAGAIN;
9751da177e4SLinus Torvalds 
976b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
9771da177e4SLinus Torvalds 				return -ENODEV;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
980b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
981ac714904SChoi Gi-yong 		} else {
9821da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
9831da177e4SLinus Torvalds 			 * allocate */
984b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
9851da177e4SLinus Torvalds 				return -ENODEV;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
9881da177e4SLinus Torvalds 			 * shared with the backing device */
9891da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
990b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
9911da177e4SLinus Torvalds 		}
9921da177e4SLinus Torvalds 
993b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
994b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
995b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
996b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
9973c7b2045SBernd Schmidt 			    ) {
998b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
9993c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
100022cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
10013c7b2045SBernd Schmidt 					return -EINVAL;
10023c7b2045SBernd Schmidt 				}
10033c7b2045SBernd Schmidt 			}
10043c7b2045SBernd Schmidt 		}
10053c7b2045SBernd Schmidt 
10061da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
10071da177e4SLinus Torvalds 		 * mappings */
100890f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
10091da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
10101da177e4SLinus Torvalds 				return -EPERM;
1011ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
10121da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
10131da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
1014b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
10151da177e4SLinus Torvalds 					prot |= PROT_EXEC;
10161da177e4SLinus Torvalds 			}
1017ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
10181da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
1019b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
10201da177e4SLinus Torvalds 			 ) {
10211da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
1022b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
10231da177e4SLinus Torvalds 		}
1024ac714904SChoi Gi-yong 	} else {
10251da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
10261da177e4SLinus Torvalds 		 * privately mapped
10271da177e4SLinus Torvalds 		 */
1028b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
10311da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
10321da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
10331da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/* allow the security API to have its say */
1037e5467859SAl Viro 	ret = security_mmap_addr(addr);
1038e5467859SAl Viro 	if (ret < 0)
1039e5467859SAl Viro 		return ret;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	/* looks okay */
10421da177e4SLinus Torvalds 	*_capabilities = capabilities;
10431da177e4SLinus Torvalds 	return 0;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds /*
10471da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
10481da177e4SLinus Torvalds  * now know into VMA flags
10491da177e4SLinus Torvalds  */
10501da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
10511da177e4SLinus Torvalds 					unsigned long prot,
10521da177e4SLinus Torvalds 					unsigned long flags,
10531da177e4SLinus Torvalds 					unsigned long capabilities)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	unsigned long vm_flags;
10561da177e4SLinus Torvalds 
1057e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
10581da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
10591da177e4SLinus Torvalds 
1060b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
10611da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
10623c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10631da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
10641da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10653c7b2045SBernd Schmidt 	} else {
10661da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
10671da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
10681da177e4SLinus Torvalds 		 * romfs/cramfs */
1069b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
10701da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
10713c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
10721da177e4SLinus Torvalds 	}
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
10751da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
10761da177e4SLinus Torvalds 	 * with another untraced process
10771da177e4SLinus Torvalds 	 */
1078a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
10791da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	return vm_flags;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds /*
10858feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
10868feae131SDavid Howells  * pins the storage)
10871da177e4SLinus Torvalds  */
10888feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	int ret;
10911da177e4SLinus Torvalds 
1092f74ac015SMiklos Szeredi 	ret = call_mmap(vma->vm_file, vma);
1093dd8632a1SPaul Mundt 	if (ret == 0) {
1094dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1095645d83c5SDavid Howells 		return 0;
1096dd8632a1SPaul Mundt 	}
10971da177e4SLinus Torvalds 	if (ret != -ENOSYS)
10981da177e4SLinus Torvalds 		return ret;
10991da177e4SLinus Torvalds 
11003fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
11013fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
11023fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
11031da177e4SLinus Torvalds 	return -ENODEV;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds /*
11071da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
11081da177e4SLinus Torvalds  */
11098feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
11108feae131SDavid Howells 			   struct vm_region *region,
1111645d83c5SDavid Howells 			   unsigned long len,
1112645d83c5SDavid Howells 			   unsigned long capabilities)
11131da177e4SLinus Torvalds {
1114dbc8358cSJoonsoo Kim 	unsigned long total, point;
11151da177e4SLinus Torvalds 	void *base;
11168feae131SDavid Howells 	int ret, order;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
11191da177e4SLinus Torvalds 	 * shared mappings on devices or memory
11201da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
11211da177e4SLinus Torvalds 	 */
1122b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
1123f74ac015SMiklos Szeredi 		ret = call_mmap(vma->vm_file, vma);
1124dd8632a1SPaul Mundt 		if (ret == 0) {
11251da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1126dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1127dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1128645d83c5SDavid Howells 			return 0;
11291da177e4SLinus Torvalds 		}
1130dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1131dd8632a1SPaul Mundt 			return ret;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
11341da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
11351da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
11388feae131SDavid Howells 
11391da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
11401da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
11411da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
11421da177e4SLinus Torvalds 	 */
1143f67d9b15SBob Liu 	order = get_order(len);
11448feae131SDavid Howells 	total = 1 << order;
1145f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1146dd8632a1SPaul Mundt 
1147dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
114822cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1149dbc8358cSJoonsoo Kim 		total = point;
11508feae131SDavid Howells 
1151da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1152dbc8358cSJoonsoo Kim 	if (!base)
1153dbc8358cSJoonsoo Kim 		goto enomem;
11548feae131SDavid Howells 
1155dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1156dbc8358cSJoonsoo Kim 
11578feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
11588feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1159f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1160dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
11618feae131SDavid Howells 
11628feae131SDavid Howells 	vma->vm_start = region->vm_start;
11638feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (vma->vm_file) {
11661da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
11671da177e4SLinus Torvalds 		loff_t fpos;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
11701da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
11711da177e4SLinus Torvalds 
1172b4bf802aSChristoph Hellwig 		ret = kernel_read(vma->vm_file, base, len, &fpos);
11731da177e4SLinus Torvalds 		if (ret < 0)
11741da177e4SLinus Torvalds 			goto error_free;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		/* clear the last little bit */
1177f67d9b15SBob Liu 		if (ret < len)
1178f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	return 0;
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds error_free:
11857223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
11868feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
11878feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1188dd8632a1SPaul Mundt 	region->vm_top   = 0;
11891da177e4SLinus Torvalds 	return ret;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds enomem:
1192b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
119305ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
11949af744d7SMichal Hocko 	show_free_areas(0, NULL);
11951da177e4SLinus Torvalds 	return -ENOMEM;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds /*
11991da177e4SLinus Torvalds  * handle mapping creation for uClinux
12001da177e4SLinus Torvalds  */
12011fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
12021da177e4SLinus Torvalds 			unsigned long addr,
12031da177e4SLinus Torvalds 			unsigned long len,
12041da177e4SLinus Torvalds 			unsigned long prot,
12051da177e4SLinus Torvalds 			unsigned long flags,
12061fcfd8dbSOleg Nesterov 			vm_flags_t vm_flags,
1207bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
1208897ab3e0SMike Rapoport 			unsigned long *populate,
1209897ab3e0SMike Rapoport 			struct list_head *uf)
12101da177e4SLinus Torvalds {
12118feae131SDavid Howells 	struct vm_area_struct *vma;
12128feae131SDavid Howells 	struct vm_region *region;
12131da177e4SLinus Torvalds 	struct rb_node *rb;
12141fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
12151da177e4SLinus Torvalds 	int ret;
12161da177e4SLinus Torvalds 
121741badc15SMichel Lespinasse 	*populate = 0;
1218bebeb3d6SMichel Lespinasse 
12191da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
12201da177e4SLinus Torvalds 	 * mapping */
12211da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
12221da177e4SLinus Torvalds 				    &capabilities);
122322cc877bSLeon Romanovsky 	if (ret < 0)
12241da177e4SLinus Torvalds 		return ret;
12251da177e4SLinus Torvalds 
122606aab5a3SDavid Howells 	/* we ignore the address hint */
122706aab5a3SDavid Howells 	addr = 0;
1228f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
122906aab5a3SDavid Howells 
12301da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
12311da177e4SLinus Torvalds 	 * now know into VMA flags */
12321fcfd8dbSOleg Nesterov 	vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
12331da177e4SLinus Torvalds 
12348feae131SDavid Howells 	/* we're going to need to record the mapping */
12358feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
12368feae131SDavid Howells 	if (!region)
12378feae131SDavid Howells 		goto error_getting_region;
12381da177e4SLinus Torvalds 
12398feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
12408feae131SDavid Howells 	if (!vma)
12418feae131SDavid Howells 		goto error_getting_vma;
12421da177e4SLinus Torvalds 
12431e2ae599SDavid Howells 	region->vm_usage = 1;
12448feae131SDavid Howells 	region->vm_flags = vm_flags;
12458feae131SDavid Howells 	region->vm_pgoff = pgoff;
12468feae131SDavid Howells 
12475beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
12488feae131SDavid Howells 	vma->vm_flags = vm_flags;
12498feae131SDavid Howells 	vma->vm_pgoff = pgoff;
12508feae131SDavid Howells 
12518feae131SDavid Howells 	if (file) {
1252cb0942b8SAl Viro 		region->vm_file = get_file(file);
1253cb0942b8SAl Viro 		vma->vm_file = get_file(file);
12548feae131SDavid Howells 	}
12558feae131SDavid Howells 
12568feae131SDavid Howells 	down_write(&nommu_region_sem);
12578feae131SDavid Howells 
12588feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
12591da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
12608feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
12611da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
12621da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
12631da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
12641da177e4SLinus Torvalds 	 *   than here
12651da177e4SLinus Torvalds 	 */
12661da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
12678feae131SDavid Howells 		struct vm_region *pregion;
12688feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
12691da177e4SLinus Torvalds 
12708feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
12718feae131SDavid Howells 		pgend = pgoff + pglen;
1272165b2392SDavid Howells 
12738feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
12748feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
12751da177e4SLinus Torvalds 
12768feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
12771da177e4SLinus Torvalds 				continue;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1280496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1281496ad9aaSAl Viro 			    file_inode(file))
12821da177e4SLinus Torvalds 				continue;
12831da177e4SLinus Torvalds 
12848feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
12851da177e4SLinus Torvalds 				continue;
12861da177e4SLinus Torvalds 
12878feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
12888feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
12898feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
12908feae131SDavid Howells 			if (pgoff >= rpgend)
12911da177e4SLinus Torvalds 				continue;
12921da177e4SLinus Torvalds 
12938feae131SDavid Howells 			/* handle inexactly overlapping matches between
12948feae131SDavid Howells 			 * mappings */
12958feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
12968feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
12978feae131SDavid Howells 				/* new mapping is not a subset of the region */
1298b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
12991da177e4SLinus Torvalds 					goto sharing_violation;
13001da177e4SLinus Torvalds 				continue;
13011da177e4SLinus Torvalds 			}
13021da177e4SLinus Torvalds 
13038feae131SDavid Howells 			/* we've found a region we can share */
13041e2ae599SDavid Howells 			pregion->vm_usage++;
13058feae131SDavid Howells 			vma->vm_region = pregion;
13068feae131SDavid Howells 			start = pregion->vm_start;
13078feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
13088feae131SDavid Howells 			vma->vm_start = start;
13098feae131SDavid Howells 			vma->vm_end = start + len;
13101da177e4SLinus Torvalds 
131122cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
13128feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
131322cc877bSLeon Romanovsky 			else {
13148feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
13158feae131SDavid Howells 				if (ret < 0) {
13168feae131SDavid Howells 					vma->vm_region = NULL;
13178feae131SDavid Howells 					vma->vm_start = 0;
13188feae131SDavid Howells 					vma->vm_end = 0;
13191e2ae599SDavid Howells 					pregion->vm_usage--;
13208feae131SDavid Howells 					pregion = NULL;
13218feae131SDavid Howells 					goto error_just_free;
13221da177e4SLinus Torvalds 				}
13238feae131SDavid Howells 			}
13248feae131SDavid Howells 			fput(region->vm_file);
13258feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
13268feae131SDavid Howells 			region = pregion;
13278feae131SDavid Howells 			result = start;
13288feae131SDavid Howells 			goto share;
13298feae131SDavid Howells 		}
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
13321da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
13331da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
13341da177e4SLinus Torvalds 		 */
1335b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
13361da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
13371da177e4SLinus Torvalds 							     pgoff, flags);
1338bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
13391da177e4SLinus Torvalds 				ret = addr;
1340bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
13418feae131SDavid Howells 					goto error_just_free;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
13441da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
13451da177e4SLinus Torvalds 				 * it */
1346bb005a59SNamhyung Kim 				ret = -ENODEV;
1347b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
13488feae131SDavid Howells 					goto error_just_free;
13491da177e4SLinus Torvalds 
1350b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
13518feae131SDavid Howells 			} else {
13528feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
13538feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
13541da177e4SLinus Torvalds 			}
13551da177e4SLinus Torvalds 		}
13561da177e4SLinus Torvalds 	}
13571da177e4SLinus Torvalds 
13588feae131SDavid Howells 	vma->vm_region = region;
13591da177e4SLinus Torvalds 
1360645d83c5SDavid Howells 	/* set up the mapping
1361b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1362645d83c5SDavid Howells 	 */
13631da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
13648feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
13651da177e4SLinus Torvalds 	else
1366645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
13671da177e4SLinus Torvalds 	if (ret < 0)
1368645d83c5SDavid Howells 		goto error_just_free;
1369645d83c5SDavid Howells 	add_nommu_region(region);
13708feae131SDavid Howells 
1371ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1372ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1373ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1374ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1375ea637639SJie Zhang 
13761da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
13778feae131SDavid Howells 	result = vma->vm_start;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
13801da177e4SLinus Torvalds 
13818feae131SDavid Howells share:
13828feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
13831da177e4SLinus Torvalds 
1384cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1385cfe79c00SMike Frysinger 	 * mapping of it is made  */
1386cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1387cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1388cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1389cfe79c00SMike Frysinger 	}
13901da177e4SLinus Torvalds 
1391cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
13921da177e4SLinus Torvalds 
13938feae131SDavid Howells 	return result;
13941da177e4SLinus Torvalds 
13958feae131SDavid Howells error_just_free:
13968feae131SDavid Howells 	up_write(&nommu_region_sem);
13978feae131SDavid Howells error:
139889a86402SDavid Howells 	if (region->vm_file)
13998feae131SDavid Howells 		fput(region->vm_file);
14008feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
140189a86402SDavid Howells 	if (vma->vm_file)
14028feae131SDavid Howells 		fput(vma->vm_file);
14038feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
14041da177e4SLinus Torvalds 	return ret;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds sharing_violation:
14078feae131SDavid Howells 	up_write(&nommu_region_sem);
140822cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
14098feae131SDavid Howells 	ret = -EINVAL;
14108feae131SDavid Howells 	goto error;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds error_getting_vma:
14138feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
141422cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
14151da177e4SLinus Torvalds 			len, current->pid);
14169af744d7SMichal Hocko 	show_free_areas(0, NULL);
14171da177e4SLinus Torvalds 	return -ENOMEM;
14181da177e4SLinus Torvalds 
14198feae131SDavid Howells error_getting_region:
142022cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
14211da177e4SLinus Torvalds 			len, current->pid);
14229af744d7SMichal Hocko 	show_free_areas(0, NULL);
14231da177e4SLinus Torvalds 	return -ENOMEM;
14241da177e4SLinus Torvalds }
14256be5ceb0SLinus Torvalds 
1426*a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1427*a90f590aSDominik Brodowski 			      unsigned long prot, unsigned long flags,
1428*a90f590aSDominik Brodowski 			      unsigned long fd, unsigned long pgoff)
142966f0dc48SHugh Dickins {
143066f0dc48SHugh Dickins 	struct file *file = NULL;
143166f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
143266f0dc48SHugh Dickins 
1433120a795dSAl Viro 	audit_mmap_fd(fd, flags);
143466f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
143566f0dc48SHugh Dickins 		file = fget(fd);
143666f0dc48SHugh Dickins 		if (!file)
143766f0dc48SHugh Dickins 			goto out;
143866f0dc48SHugh Dickins 	}
143966f0dc48SHugh Dickins 
144066f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
144166f0dc48SHugh Dickins 
1442ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
144366f0dc48SHugh Dickins 
144466f0dc48SHugh Dickins 	if (file)
144566f0dc48SHugh Dickins 		fput(file);
144666f0dc48SHugh Dickins out:
144766f0dc48SHugh Dickins 	return retval;
144866f0dc48SHugh Dickins }
144966f0dc48SHugh Dickins 
1450*a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1451*a90f590aSDominik Brodowski 		unsigned long, prot, unsigned long, flags,
1452*a90f590aSDominik Brodowski 		unsigned long, fd, unsigned long, pgoff)
1453*a90f590aSDominik Brodowski {
1454*a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1455*a90f590aSDominik Brodowski }
1456*a90f590aSDominik Brodowski 
1457a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1458a4679373SChristoph Hellwig struct mmap_arg_struct {
1459a4679373SChristoph Hellwig 	unsigned long addr;
1460a4679373SChristoph Hellwig 	unsigned long len;
1461a4679373SChristoph Hellwig 	unsigned long prot;
1462a4679373SChristoph Hellwig 	unsigned long flags;
1463a4679373SChristoph Hellwig 	unsigned long fd;
1464a4679373SChristoph Hellwig 	unsigned long offset;
1465a4679373SChristoph Hellwig };
1466a4679373SChristoph Hellwig 
1467a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1468a4679373SChristoph Hellwig {
1469a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1470a4679373SChristoph Hellwig 
1471a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1472a4679373SChristoph Hellwig 		return -EFAULT;
14731824cb75SAlexander Kuleshov 	if (offset_in_page(a.offset))
1474a4679373SChristoph Hellwig 		return -EINVAL;
1475a4679373SChristoph Hellwig 
1476*a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1477a4679373SChristoph Hellwig 			       a.offset >> PAGE_SHIFT);
1478a4679373SChristoph Hellwig }
1479a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1480a4679373SChristoph Hellwig 
14811da177e4SLinus Torvalds /*
14828feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
14838feae131SDavid Howells  * for the first part or the tail.
14841da177e4SLinus Torvalds  */
14858feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
14868feae131SDavid Howells 	      unsigned long addr, int new_below)
14871da177e4SLinus Torvalds {
14888feae131SDavid Howells 	struct vm_area_struct *new;
14898feae131SDavid Howells 	struct vm_region *region;
14908feae131SDavid Howells 	unsigned long npages;
14911da177e4SLinus Torvalds 
1492779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1493779c1023SDavid Howells 	 * only a single usage on the region) */
1494779c1023SDavid Howells 	if (vma->vm_file)
14958feae131SDavid Howells 		return -ENOMEM;
14961da177e4SLinus Torvalds 
14978feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
14988feae131SDavid Howells 		return -ENOMEM;
14991da177e4SLinus Torvalds 
15008feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
15018feae131SDavid Howells 	if (!region)
15028feae131SDavid Howells 		return -ENOMEM;
15038feae131SDavid Howells 
15048feae131SDavid Howells 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
15058feae131SDavid Howells 	if (!new) {
15068feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
15078feae131SDavid Howells 		return -ENOMEM;
15081da177e4SLinus Torvalds 	}
15091da177e4SLinus Torvalds 
15108feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
15118feae131SDavid Howells 	*new = *vma;
15128feae131SDavid Howells 	*region = *vma->vm_region;
15138feae131SDavid Howells 	new->vm_region = region;
15148feae131SDavid Howells 
15158feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
15168feae131SDavid Howells 
15178feae131SDavid Howells 	if (new_below) {
1518dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
15198feae131SDavid Howells 	} else {
15208feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
15218feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
15221da177e4SLinus Torvalds 	}
15238feae131SDavid Howells 
15248feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
15258feae131SDavid Howells 		new->vm_ops->open(new);
15268feae131SDavid Howells 
15278feae131SDavid Howells 	delete_vma_from_mm(vma);
15288feae131SDavid Howells 	down_write(&nommu_region_sem);
15298feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
15308feae131SDavid Howells 	if (new_below) {
15318feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
15328feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
15338feae131SDavid Howells 	} else {
15348feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1535dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
15368feae131SDavid Howells 	}
15378feae131SDavid Howells 	add_nommu_region(vma->vm_region);
15388feae131SDavid Howells 	add_nommu_region(new->vm_region);
15398feae131SDavid Howells 	up_write(&nommu_region_sem);
15408feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15418feae131SDavid Howells 	add_vma_to_mm(mm, new);
15428feae131SDavid Howells 	return 0;
15438feae131SDavid Howells }
15448feae131SDavid Howells 
15458feae131SDavid Howells /*
15468feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
15478feae131SDavid Howells  * the end
15488feae131SDavid Howells  */
15498feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
15508feae131SDavid Howells 		      struct vm_area_struct *vma,
15518feae131SDavid Howells 		      unsigned long from, unsigned long to)
15528feae131SDavid Howells {
15538feae131SDavid Howells 	struct vm_region *region;
15548feae131SDavid Howells 
15558feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
15568feae131SDavid Howells 	 * and list */
15578feae131SDavid Howells 	delete_vma_from_mm(vma);
15588feae131SDavid Howells 	if (from > vma->vm_start)
15598feae131SDavid Howells 		vma->vm_end = from;
15608feae131SDavid Howells 	else
15618feae131SDavid Howells 		vma->vm_start = to;
15628feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15638feae131SDavid Howells 
15648feae131SDavid Howells 	/* cut the backing region down to size */
15658feae131SDavid Howells 	region = vma->vm_region;
15661e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
15678feae131SDavid Howells 
15688feae131SDavid Howells 	down_write(&nommu_region_sem);
15698feae131SDavid Howells 	delete_nommu_region(region);
1570dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1571dd8632a1SPaul Mundt 		to = region->vm_top;
1572dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1573dd8632a1SPaul Mundt 	} else {
15748feae131SDavid Howells 		region->vm_start = to;
1575dd8632a1SPaul Mundt 	}
15768feae131SDavid Howells 	add_nommu_region(region);
15778feae131SDavid Howells 	up_write(&nommu_region_sem);
15788feae131SDavid Howells 
15798feae131SDavid Howells 	free_page_series(from, to);
15808feae131SDavid Howells 	return 0;
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15833034097aSDavid Howells /*
15843034097aSDavid Howells  * release a mapping
15858feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
15868feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
15873034097aSDavid Howells  */
1588897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
15891da177e4SLinus Torvalds {
15908feae131SDavid Howells 	struct vm_area_struct *vma;
1591f67d9b15SBob Liu 	unsigned long end;
15928feae131SDavid Howells 	int ret;
15931da177e4SLinus Torvalds 
1594f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
15958feae131SDavid Howells 	if (len == 0)
15961da177e4SLinus Torvalds 		return -EINVAL;
15971da177e4SLinus Torvalds 
1598f67d9b15SBob Liu 	end = start + len;
1599f67d9b15SBob Liu 
16008feae131SDavid Howells 	/* find the first potentially overlapping VMA */
16018feae131SDavid Howells 	vma = find_vma(mm, start);
16028feae131SDavid Howells 	if (!vma) {
1603ac714904SChoi Gi-yong 		static int limit;
160433e5d769SDavid Howells 		if (limit < 5) {
160522cc877bSLeon Romanovsky 			pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
160633e5d769SDavid Howells 					current->pid, current->comm,
160733e5d769SDavid Howells 					start, start + len - 1);
160833e5d769SDavid Howells 			limit++;
160933e5d769SDavid Howells 		}
16108feae131SDavid Howells 		return -EINVAL;
16118feae131SDavid Howells 	}
16121da177e4SLinus Torvalds 
16138feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
16148feae131SDavid Howells 	if (vma->vm_file) {
16158feae131SDavid Howells 		do {
161622cc877bSLeon Romanovsky 			if (start > vma->vm_start)
16178feae131SDavid Howells 				return -EINVAL;
16188feae131SDavid Howells 			if (end == vma->vm_end)
16198feae131SDavid Howells 				goto erase_whole_vma;
1620d75a310cSNamhyung Kim 			vma = vma->vm_next;
1621d75a310cSNamhyung Kim 		} while (vma);
16228feae131SDavid Howells 		return -EINVAL;
16238feae131SDavid Howells 	} else {
16248feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
16258feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
16268feae131SDavid Howells 			goto erase_whole_vma;
162722cc877bSLeon Romanovsky 		if (start < vma->vm_start || end > vma->vm_end)
16288feae131SDavid Howells 			return -EINVAL;
16291824cb75SAlexander Kuleshov 		if (offset_in_page(start))
16308feae131SDavid Howells 			return -EINVAL;
16311824cb75SAlexander Kuleshov 		if (end != vma->vm_end && offset_in_page(end))
16328feae131SDavid Howells 			return -EINVAL;
16338feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
16348feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
163522cc877bSLeon Romanovsky 			if (ret < 0)
16368feae131SDavid Howells 				return ret;
16378feae131SDavid Howells 		}
16388feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
16398feae131SDavid Howells 	}
16401da177e4SLinus Torvalds 
16418feae131SDavid Howells erase_whole_vma:
16428feae131SDavid Howells 	delete_vma_from_mm(vma);
16438feae131SDavid Howells 	delete_vma(mm, vma);
16441da177e4SLinus Torvalds 	return 0;
16451da177e4SLinus Torvalds }
1646b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
16471da177e4SLinus Torvalds 
1648bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
16493034097aSDavid Howells {
1650bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
16513034097aSDavid Howells 	int ret;
16523034097aSDavid Howells 
16533034097aSDavid Howells 	down_write(&mm->mmap_sem);
1654897ab3e0SMike Rapoport 	ret = do_munmap(mm, addr, len, NULL);
16553034097aSDavid Howells 	up_write(&mm->mmap_sem);
16563034097aSDavid Howells 	return ret;
16573034097aSDavid Howells }
1658a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1659a46ef99dSLinus Torvalds 
1660a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1661a46ef99dSLinus Torvalds {
1662bfce281cSAl Viro 	return vm_munmap(addr, len);
1663a46ef99dSLinus Torvalds }
16643034097aSDavid Howells 
16653034097aSDavid Howells /*
16668feae131SDavid Howells  * release all the mappings made in a process's VM space
16673034097aSDavid Howells  */
16681da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
16691da177e4SLinus Torvalds {
16708feae131SDavid Howells 	struct vm_area_struct *vma;
16711da177e4SLinus Torvalds 
16728feae131SDavid Howells 	if (!mm)
16738feae131SDavid Howells 		return;
16748feae131SDavid Howells 
16751da177e4SLinus Torvalds 	mm->total_vm = 0;
16761da177e4SLinus Torvalds 
16778feae131SDavid Howells 	while ((vma = mm->mmap)) {
16788feae131SDavid Howells 		mm->mmap = vma->vm_next;
16798feae131SDavid Howells 		delete_vma_from_mm(vma);
16808feae131SDavid Howells 		delete_vma(mm, vma);
168104c34961SSteven J. Magnani 		cond_resched();
16821da177e4SLinus Torvalds 	}
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
16855d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
16861da177e4SLinus Torvalds {
16871da177e4SLinus Torvalds 	return -ENOMEM;
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds /*
16916fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
16926fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
16931da177e4SLinus Torvalds  *
16946fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
16958feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
16968feae131SDavid Howells  * block is not shareable
16971da177e4SLinus Torvalds  *
16986fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
16991da177e4SLinus Torvalds  */
17004b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
17011da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
17021da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
17031da177e4SLinus Torvalds {
17046fa5f80bSDavid Howells 	struct vm_area_struct *vma;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	/* insanity checks first */
1707f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1708f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
17098feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
17101da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17111da177e4SLinus Torvalds 
17121824cb75SAlexander Kuleshov 	if (offset_in_page(addr))
17138feae131SDavid Howells 		return -EINVAL;
17148feae131SDavid Howells 
17151da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
17161da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17171da177e4SLinus Torvalds 
17188feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
17196fa5f80bSDavid Howells 	if (!vma)
17201da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17211da177e4SLinus Torvalds 
17226fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
17231da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
17241da177e4SLinus Torvalds 
17256fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
17261da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
17271da177e4SLinus Torvalds 
17288feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
17291da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	/* all checks complete - do it */
17326fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
17336fa5f80bSDavid Howells 	return vma->vm_start;
17346fa5f80bSDavid Howells }
17356fa5f80bSDavid Howells 
17366a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
17376a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
17386a6160a7SHeiko Carstens 		unsigned long, new_addr)
17396fa5f80bSDavid Howells {
17406fa5f80bSDavid Howells 	unsigned long ret;
17416fa5f80bSDavid Howells 
17426fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
17436fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
17446fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
17456fa5f80bSDavid Howells 	return ret;
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
1748240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma,
1749240aadeeSMichel Lespinasse 			      unsigned long address, unsigned int flags,
1750240aadeeSMichel Lespinasse 			      unsigned int *page_mask)
17511da177e4SLinus Torvalds {
1752240aadeeSMichel Lespinasse 	*page_mask = 0;
17531da177e4SLinus Torvalds 	return NULL;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
17568f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
17578f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
17581da177e4SLinus Torvalds {
17598f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
17608f3b1327SBob Liu 		return -EINVAL;
17618f3b1327SBob Liu 
1762314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
176366aa2b4bSGreg Ungerer 	return 0;
17641da177e4SLinus Torvalds }
176522c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
17661da177e4SLinus Torvalds 
17673c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
17683c0b9de6SLinus Torvalds {
17693c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
17703c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
17713c0b9de6SLinus Torvalds 
17723c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
17733c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
17743c0b9de6SLinus Torvalds }
17753c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
17763c0b9de6SLinus Torvalds 
1777f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1778f905bc44SPaul Mundt 			unsigned long pgoff)
1779f905bc44SPaul Mundt {
1780f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1781f905bc44SPaul Mundt 
1782f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1783f905bc44SPaul Mundt 		return -EINVAL;
1784f905bc44SPaul Mundt 
1785f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1786f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1787f905bc44SPaul Mundt 
1788f905bc44SPaul Mundt 	return 0;
1789f905bc44SPaul Mundt }
1790f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1791f905bc44SPaul Mundt 
17921da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
17931da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
17941da177e4SLinus Torvalds {
17951da177e4SLinus Torvalds 	return -ENOMEM;
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
179811bac800SDave Jiang int filemap_fault(struct vm_fault *vmf)
1799b0e15190SDavid Howells {
1800b0e15190SDavid Howells 	BUG();
1801d0217ac0SNick Piggin 	return 0;
1802b0e15190SDavid Howells }
1803b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
18040ec76a11SDavid Howells 
180582b0f8c3SJan Kara void filemap_map_pages(struct vm_fault *vmf,
1806bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1807f1820361SKirill A. Shutemov {
1808f1820361SKirill A. Shutemov 	BUG();
1809f1820361SKirill A. Shutemov }
1810f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1811f1820361SKirill A. Shutemov 
181284d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1813442486ecSLorenzo Stoakes 		unsigned long addr, void *buf, int len, unsigned int gup_flags)
18140ec76a11SDavid Howells {
18150ec76a11SDavid Howells 	struct vm_area_struct *vma;
1816442486ecSLorenzo Stoakes 	int write = gup_flags & FOLL_WRITE;
18170ec76a11SDavid Howells 
18180ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
18190ec76a11SDavid Howells 
18200ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
18210159b141SDavid Howells 	vma = find_vma(mm, addr);
18220159b141SDavid Howells 	if (vma) {
18230ec76a11SDavid Howells 		/* don't overrun this mapping */
18240ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
18250ec76a11SDavid Howells 			len = vma->vm_end - addr;
18260ec76a11SDavid Howells 
18270ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1828d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
18297959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
18307959722bSJie Zhang 					 (void *) addr, buf, len);
1831d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
18327959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
18337959722bSJie Zhang 					    buf, (void *) addr, len);
18340ec76a11SDavid Howells 		else
18350ec76a11SDavid Howells 			len = 0;
18360ec76a11SDavid Howells 	} else {
18370ec76a11SDavid Howells 		len = 0;
18380ec76a11SDavid Howells 	}
18390ec76a11SDavid Howells 
18400ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
1841f55f199bSMike Frysinger 
1842f55f199bSMike Frysinger 	return len;
1843f55f199bSMike Frysinger }
1844f55f199bSMike Frysinger 
1845f55f199bSMike Frysinger /**
1846b7701a5fSMike Rapoport  * access_remote_vm - access another process' address space
1847f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1848f55f199bSMike Frysinger  * @addr:	start address to access
1849f55f199bSMike Frysinger  * @buf:	source or destination buffer
1850f55f199bSMike Frysinger  * @len:	number of bytes to transfer
18516347e8d5SLorenzo Stoakes  * @gup_flags:	flags modifying lookup behaviour
1852f55f199bSMike Frysinger  *
1853f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1854f55f199bSMike Frysinger  */
1855f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
18566347e8d5SLorenzo Stoakes 		void *buf, int len, unsigned int gup_flags)
1857f55f199bSMike Frysinger {
18586347e8d5SLorenzo Stoakes 	return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
1859f55f199bSMike Frysinger }
1860f55f199bSMike Frysinger 
1861f55f199bSMike Frysinger /*
1862f55f199bSMike Frysinger  * Access another process' address space.
1863f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1864f55f199bSMike Frysinger  */
1865f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1866f307ab6dSLorenzo Stoakes 		unsigned int gup_flags)
1867f55f199bSMike Frysinger {
1868f55f199bSMike Frysinger 	struct mm_struct *mm;
1869f55f199bSMike Frysinger 
1870f55f199bSMike Frysinger 	if (addr + len < addr)
1871f55f199bSMike Frysinger 		return 0;
1872f55f199bSMike Frysinger 
1873f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1874f55f199bSMike Frysinger 	if (!mm)
1875f55f199bSMike Frysinger 		return 0;
1876f55f199bSMike Frysinger 
1877f307ab6dSLorenzo Stoakes 	len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
1878f55f199bSMike Frysinger 
18790ec76a11SDavid Howells 	mmput(mm);
18800ec76a11SDavid Howells 	return len;
18810ec76a11SDavid Howells }
1882fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
18837e660872SDavid Howells 
18847e660872SDavid Howells /**
18857e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
18867e660872SDavid Howells  * @inode: The inode to check
18877e660872SDavid Howells  * @size: The current filesize of the inode
18887e660872SDavid Howells  * @newsize: The proposed filesize of the inode
18897e660872SDavid Howells  *
18907e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
18917e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
18927e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
18937e660872SDavid Howells  * automatically grant mappings that are too large.
18947e660872SDavid Howells  */
18957e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
18967e660872SDavid Howells 				size_t newsize)
18977e660872SDavid Howells {
18987e660872SDavid Howells 	struct vm_area_struct *vma;
18997e660872SDavid Howells 	struct vm_region *region;
19007e660872SDavid Howells 	pgoff_t low, high;
19017e660872SDavid Howells 	size_t r_size, r_top;
19027e660872SDavid Howells 
19037e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
19047e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
19057e660872SDavid Howells 
19067e660872SDavid Howells 	down_write(&nommu_region_sem);
19071acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
19087e660872SDavid Howells 
19097e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
19106b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
19117e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
19127e660872SDavid Howells 		 * cache */
19137e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
19141acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
19157e660872SDavid Howells 			up_write(&nommu_region_sem);
19167e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
19177e660872SDavid Howells 		}
19187e660872SDavid Howells 	}
19197e660872SDavid Howells 
19207e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
19217e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
19227e660872SDavid Howells 	 *
19237e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
19247e660872SDavid Howells 	 * shouldn't be any
19257e660872SDavid Howells 	 */
19261acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
19277e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
19287e660872SDavid Howells 			continue;
19297e660872SDavid Howells 
19307e660872SDavid Howells 		region = vma->vm_region;
19317e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
19327e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
19337e660872SDavid Howells 
19347e660872SDavid Howells 		if (r_top > newsize) {
19357e660872SDavid Howells 			region->vm_top -= r_top - newsize;
19367e660872SDavid Howells 			if (region->vm_end > region->vm_top)
19377e660872SDavid Howells 				region->vm_end = region->vm_top;
19387e660872SDavid Howells 		}
19397e660872SDavid Howells 	}
19407e660872SDavid Howells 
19411acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
19427e660872SDavid Howells 	up_write(&nommu_region_sem);
19437e660872SDavid Howells 	return 0;
19447e660872SDavid Howells }
1945c9b1d098SAndrew Shewmaker 
1946c9b1d098SAndrew Shewmaker /*
1947c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1948c9b1d098SAndrew Shewmaker  *
1949c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1950c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1951c9b1d098SAndrew Shewmaker  * mode.
1952c9b1d098SAndrew Shewmaker  *
1953c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1954c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1955c9b1d098SAndrew Shewmaker  */
1956c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1957c9b1d098SAndrew Shewmaker {
1958c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1959c9b1d098SAndrew Shewmaker 
1960c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1961c9b1d098SAndrew Shewmaker 
1962c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1963c9b1d098SAndrew Shewmaker 	return 0;
1964c9b1d098SAndrew Shewmaker }
1965a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
19664eeab4f5SAndrew Shewmaker 
19674eeab4f5SAndrew Shewmaker /*
19684eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
19694eeab4f5SAndrew Shewmaker  *
19704eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
19714eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
19724eeab4f5SAndrew Shewmaker  *
19734eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
19744eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
19754eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
19764eeab4f5SAndrew Shewmaker  */
19774eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
19784eeab4f5SAndrew Shewmaker {
19794eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
19804eeab4f5SAndrew Shewmaker 
1981c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
19824eeab4f5SAndrew Shewmaker 
19834eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
19844eeab4f5SAndrew Shewmaker 	return 0;
19854eeab4f5SAndrew Shewmaker }
1986a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1987