xref: /linux/mm/nommu.c (revision bae473a423f65e480db83c85b5e92254f6dfcb28)
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>
20615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
211da177e4SLinus Torvalds #include <linux/mman.h>
221da177e4SLinus Torvalds #include <linux/swap.h>
231da177e4SLinus Torvalds #include <linux/file.h>
241da177e4SLinus Torvalds #include <linux/highmem.h>
251da177e4SLinus Torvalds #include <linux/pagemap.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/vmalloc.h>
281da177e4SLinus Torvalds #include <linux/blkdev.h>
291da177e4SLinus Torvalds #include <linux/backing-dev.h>
303b32123dSGideon Israel Dsouza #include <linux/compiler.h>
311da177e4SLinus Torvalds #include <linux/mount.h>
321da177e4SLinus Torvalds #include <linux/personality.h>
331da177e4SLinus Torvalds #include <linux/security.h>
341da177e4SLinus Torvalds #include <linux/syscalls.h>
35120a795dSAl Viro #include <linux/audit.h>
36b1de0d13SMitchel Humpherys #include <linux/printk.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <asm/uaccess.h>
391da177e4SLinus Torvalds #include <asm/tlb.h>
401da177e4SLinus Torvalds #include <asm/tlbflush.h>
41eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
428feae131SDavid Howells #include "internal.h"
438feae131SDavid Howells 
441da177e4SLinus Torvalds void *high_memory;
45944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
461da177e4SLinus Torvalds struct page *mem_map;
471da177e4SLinus Torvalds unsigned long max_mapnr;
485b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
494266c97aSHugh Dickins unsigned long highest_memmap_pfn;
50fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
511da177e4SLinus Torvalds int heap_stack_gap = 0;
521da177e4SLinus Torvalds 
5333e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
548feae131SDavid Howells 
551da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
561da177e4SLinus Torvalds 
578feae131SDavid Howells /* list of mapped, potentially shareable regions */
588feae131SDavid Howells static struct kmem_cache *vm_region_jar;
598feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
608feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
611da177e4SLinus Torvalds 
62f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
671da177e4SLinus Torvalds  * just what the caller asked for.
681da177e4SLinus Torvalds  *
691da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	struct page *page;
741da177e4SLinus Torvalds 
754016a139SMichael Hennerich 	/*
764016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
774016a139SMichael Hennerich 	 * return size of 0
784016a139SMichael Hennerich 	 */
795a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
806cfd53fcSPaul Mundt 		return 0;
816cfd53fcSPaul Mundt 
826cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
836cfd53fcSPaul Mundt 
846cfd53fcSPaul Mundt 	/*
856cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
866cfd53fcSPaul Mundt 	 * kmalloc().
876cfd53fcSPaul Mundt 	 */
881da177e4SLinus Torvalds 	if (PageSlab(page))
891da177e4SLinus Torvalds 		return ksize(objp);
901da177e4SLinus Torvalds 
916cfd53fcSPaul Mundt 	/*
92ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
93ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
94ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
95ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
96ab2e83eaSPaul Mundt 	 */
97ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
98ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
99ab2e83eaSPaul Mundt 
100ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
101ab2e83eaSPaul Mundt 		if (vma)
102ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
103ab2e83eaSPaul Mundt 	}
104ab2e83eaSPaul Mundt 
105ab2e83eaSPaul Mundt 	/*
1066cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1075a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1086cfd53fcSPaul Mundt 	 */
1095a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(page);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
11228a35716SMichel Lespinasse long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
11328a35716SMichel Lespinasse 		      unsigned long start, unsigned long nr_pages,
11428a35716SMichel Lespinasse 		      unsigned int foll_flags, struct page **pages,
11528a35716SMichel Lespinasse 		      struct vm_area_struct **vmas, int *nonblocking)
1161da177e4SLinus Torvalds {
117910e46daSSonic Zhang 	struct vm_area_struct *vma;
1187b4d5b8bSDavid Howells 	unsigned long vm_flags;
1197b4d5b8bSDavid Howells 	int i;
1207b4d5b8bSDavid Howells 
1217b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
12258fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1237b4d5b8bSDavid Howells 	 */
12458fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
12558fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
12658fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
12758fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1281da177e4SLinus Torvalds 
1299d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
1307561e8caSDavid Howells 		vma = find_vma(mm, start);
131910e46daSSonic Zhang 		if (!vma)
1327b4d5b8bSDavid Howells 			goto finish_or_fault;
1337b4d5b8bSDavid Howells 
1347b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1351c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1361c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1377b4d5b8bSDavid Howells 			goto finish_or_fault;
138910e46daSSonic Zhang 
1391da177e4SLinus Torvalds 		if (pages) {
1401da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1411da177e4SLinus Torvalds 			if (pages[i])
14209cbfeafSKirill A. Shutemov 				get_page(pages[i]);
1431da177e4SLinus Torvalds 		}
1441da177e4SLinus Torvalds 		if (vmas)
145910e46daSSonic Zhang 			vmas[i] = vma;
146e1ee65d8SDavid Howells 		start = (start + PAGE_SIZE) & PAGE_MASK;
1471da177e4SLinus Torvalds 	}
1487b4d5b8bSDavid Howells 
1497b4d5b8bSDavid Howells 	return i;
1507b4d5b8bSDavid Howells 
1517b4d5b8bSDavid Howells finish_or_fault:
1527b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1531da177e4SLinus Torvalds }
154b291f000SNick Piggin 
155b291f000SNick Piggin /*
156b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
157b291f000SNick Piggin  * and indicate the VMA that covers each page
158b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
159b291f000SNick Piggin  *   slab page or a secondary page from a compound page
160b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
161b291f000SNick Piggin  */
162c12d2da5SIngo Molnar long get_user_pages(unsigned long start, unsigned long nr_pages,
16328a35716SMichel Lespinasse 		    int write, int force, struct page **pages,
16428a35716SMichel Lespinasse 		    struct vm_area_struct **vmas)
165b291f000SNick Piggin {
166b291f000SNick Piggin 	int flags = 0;
167b291f000SNick Piggin 
168b291f000SNick Piggin 	if (write)
16958fa879eSHugh Dickins 		flags |= FOLL_WRITE;
170b291f000SNick Piggin 	if (force)
17158fa879eSHugh Dickins 		flags |= FOLL_FORCE;
172b291f000SNick Piggin 
173cde70140SDave Hansen 	return __get_user_pages(current, current->mm, start, nr_pages, flags,
174cde70140SDave Hansen 				pages, vmas, NULL);
175b291f000SNick Piggin }
176c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages);
17766aa2b4bSGreg Ungerer 
178c12d2da5SIngo Molnar long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
179f0818f47SAndrea Arcangeli 			    int write, int force, struct page **pages,
180f0818f47SAndrea Arcangeli 			    int *locked)
181f0818f47SAndrea Arcangeli {
182c12d2da5SIngo Molnar 	return get_user_pages(start, nr_pages, write, force, pages, NULL);
183f0818f47SAndrea Arcangeli }
184c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages_locked);
185f0818f47SAndrea Arcangeli 
1860fd71a56SAndrea Arcangeli long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
187f0818f47SAndrea Arcangeli 			       unsigned long start, unsigned long nr_pages,
1880fd71a56SAndrea Arcangeli 			       int write, int force, struct page **pages,
1890fd71a56SAndrea Arcangeli 			       unsigned int gup_flags)
190f0818f47SAndrea Arcangeli {
191f0818f47SAndrea Arcangeli 	long ret;
192f0818f47SAndrea Arcangeli 	down_read(&mm->mmap_sem);
193cde70140SDave Hansen 	ret = __get_user_pages(tsk, mm, start, nr_pages, gup_flags, pages,
194cde70140SDave Hansen 				NULL, NULL);
195f0818f47SAndrea Arcangeli 	up_read(&mm->mmap_sem);
196f0818f47SAndrea Arcangeli 	return ret;
197f0818f47SAndrea Arcangeli }
1980fd71a56SAndrea Arcangeli EXPORT_SYMBOL(__get_user_pages_unlocked);
1990fd71a56SAndrea Arcangeli 
200c12d2da5SIngo Molnar long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
2010fd71a56SAndrea Arcangeli 			     int write, int force, struct page **pages)
2020fd71a56SAndrea Arcangeli {
203cde70140SDave Hansen 	return __get_user_pages_unlocked(current, current->mm, start, nr_pages,
204cde70140SDave Hansen 					 write, force, pages, 0);
2050fd71a56SAndrea Arcangeli }
206c12d2da5SIngo Molnar EXPORT_SYMBOL(get_user_pages_unlocked);
207f0818f47SAndrea Arcangeli 
208dfc2f91aSPaul Mundt /**
209dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
210dfc2f91aSPaul Mundt  * @vma: memory mapping
211dfc2f91aSPaul Mundt  * @address: user virtual address
212dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
213dfc2f91aSPaul Mundt  *
214dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
215dfc2f91aSPaul Mundt  *
216dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
217dfc2f91aSPaul Mundt  */
218dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
219dfc2f91aSPaul Mundt 	unsigned long *pfn)
220dfc2f91aSPaul Mundt {
221dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
222dfc2f91aSPaul Mundt 		return -EINVAL;
223dfc2f91aSPaul Mundt 
224dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
225dfc2f91aSPaul Mundt 	return 0;
226dfc2f91aSPaul Mundt }
227dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
228dfc2f91aSPaul Mundt 
229f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
2301da177e4SLinus Torvalds 
231b3bdda02SChristoph Lameter void vfree(const void *addr)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	kfree(addr);
2341da177e4SLinus Torvalds }
235b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2361da177e4SLinus Torvalds 
237dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	/*
2408518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2418518609dSRobert P. J. Day 	 * returns only a logical address.
2421da177e4SLinus Torvalds 	 */
24384097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2441da177e4SLinus Torvalds }
245b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2461da177e4SLinus Torvalds 
247f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
248f905bc44SPaul Mundt {
249f905bc44SPaul Mundt 	void *ret;
250f905bc44SPaul Mundt 
251f905bc44SPaul Mundt 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
252f905bc44SPaul Mundt 			PAGE_KERNEL);
253f905bc44SPaul Mundt 	if (ret) {
254f905bc44SPaul Mundt 		struct vm_area_struct *vma;
255f905bc44SPaul Mundt 
256f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
257f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
258f905bc44SPaul Mundt 		if (vma)
259f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
260f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
261f905bc44SPaul Mundt 	}
262f905bc44SPaul Mundt 
263f905bc44SPaul Mundt 	return ret;
264f905bc44SPaul Mundt }
265f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
266f905bc44SPaul Mundt 
267b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	return virt_to_page(addr);
2701da177e4SLinus Torvalds }
271b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
2721da177e4SLinus Torvalds 
273b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2761da177e4SLinus Torvalds }
277b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2801da177e4SLinus Torvalds {
2819bde916bSChen Gang 	/* Don't allow overflow */
2829bde916bSChen Gang 	if ((unsigned long) buf + count < count)
2839bde916bSChen Gang 		count = -(unsigned long) buf;
2849bde916bSChen Gang 
2851da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2861da177e4SLinus Torvalds 	return count;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	/* Don't allow overflow */
2921da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2931da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	memcpy(addr, buf, count);
296ac714904SChoi Gi-yong 	return count;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
300e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
3011da177e4SLinus Torvalds  *
3021da177e4SLinus Torvalds  *	@size:		allocation size
3031da177e4SLinus Torvalds  *
3041da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
305e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
3061da177e4SLinus Torvalds  *
307c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
3081da177e4SLinus Torvalds  *	use __vmalloc() instead.
3091da177e4SLinus Torvalds  */
3101da177e4SLinus Torvalds void *vmalloc(unsigned long size)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
3131da177e4SLinus Torvalds }
314f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
315f6138882SAndrew Morton 
316e1ca7788SDave Young /*
317e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
318e1ca7788SDave Young  *
319e1ca7788SDave Young  *	@size:		allocation size
320e1ca7788SDave Young  *
321e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
322e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
323e1ca7788SDave Young  *	The memory allocated is set to zero.
324e1ca7788SDave Young  *
325e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
326e1ca7788SDave Young  *	use __vmalloc() instead.
327e1ca7788SDave Young  */
328e1ca7788SDave Young void *vzalloc(unsigned long size)
329e1ca7788SDave Young {
330e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
331e1ca7788SDave Young 			PAGE_KERNEL);
332e1ca7788SDave Young }
333e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
334e1ca7788SDave Young 
335e1ca7788SDave Young /**
336e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
337e1ca7788SDave Young  * @size:	allocation size
338e1ca7788SDave Young  * @node:	numa node
339e1ca7788SDave Young  *
340e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
341e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
342e1ca7788SDave Young  *
343e1ca7788SDave Young  * For tight control over page level allocator and protection flags
344e1ca7788SDave Young  * use __vmalloc() instead.
345e1ca7788SDave Young  */
346f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
347f6138882SAndrew Morton {
348f6138882SAndrew Morton 	return vmalloc(size);
349f6138882SAndrew Morton }
3509a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
351e1ca7788SDave Young 
352e1ca7788SDave Young /**
353e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
354e1ca7788SDave Young  * @size:	allocation size
355e1ca7788SDave Young  * @node:	numa node
356e1ca7788SDave Young  *
357e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
358e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
359e1ca7788SDave Young  * The memory allocated is set to zero.
360e1ca7788SDave Young  *
361e1ca7788SDave Young  * For tight control over page level allocator and protection flags
362e1ca7788SDave Young  * use __vmalloc() instead.
363e1ca7788SDave Young  */
364e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
365e1ca7788SDave Young {
366e1ca7788SDave Young 	return vzalloc(size);
367e1ca7788SDave Young }
368e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3691da177e4SLinus Torvalds 
3701af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
3711af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
3721af446edSPaul Mundt #endif
3731af446edSPaul Mundt 
3741af446edSPaul Mundt /**
3751af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
3761af446edSPaul Mundt  *	@size:		allocation size
3771af446edSPaul Mundt  *
3781af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
3791af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
3801af446edSPaul Mundt  *	executable kernel virtual space.
3811af446edSPaul Mundt  *
3821af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3831af446edSPaul Mundt  *	use __vmalloc() instead.
3841af446edSPaul Mundt  */
3851af446edSPaul Mundt 
3861af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3871af446edSPaul Mundt {
3881af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3891af446edSPaul Mundt }
3901af446edSPaul Mundt 
391b5073173SPaul Mundt /**
392b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3931da177e4SLinus Torvalds  *	@size:		allocation size
3941da177e4SLinus Torvalds  *
3951da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
396e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
3971da177e4SLinus Torvalds  */
3981da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
4011da177e4SLinus Torvalds }
402b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
403b5073173SPaul Mundt 
404b5073173SPaul Mundt /**
405b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
406b5073173SPaul Mundt  *	@size:		allocation size
407b5073173SPaul Mundt  *
408b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
409b5073173SPaul Mundt  * mapped to userspace without leaking data.
410f905bc44SPaul Mundt  *
411f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
412f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
413b5073173SPaul Mundt  */
414b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
415b5073173SPaul Mundt {
416f905bc44SPaul Mundt 	/*
417f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
418f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
419f905bc44SPaul Mundt 	 */
420f905bc44SPaul Mundt 	return vmalloc_user(size);
421b5073173SPaul Mundt }
422b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	BUG();
4271da177e4SLinus Torvalds 	return NULL;
4281da177e4SLinus Torvalds }
429b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
4301da177e4SLinus Torvalds 
431b3bdda02SChristoph Lameter void vunmap(const void *addr)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	BUG();
4341da177e4SLinus Torvalds }
435b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
4361da177e4SLinus Torvalds 
437eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
438eb6434d9SPaul Mundt {
439eb6434d9SPaul Mundt 	BUG();
440eb6434d9SPaul Mundt 	return NULL;
441eb6434d9SPaul Mundt }
442eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
443eb6434d9SPaul Mundt 
444eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
445eb6434d9SPaul Mundt {
446eb6434d9SPaul Mundt 	BUG();
447eb6434d9SPaul Mundt }
448eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
449eb6434d9SPaul Mundt 
450eb6434d9SPaul Mundt void vm_unmap_aliases(void)
451eb6434d9SPaul Mundt {
452eb6434d9SPaul Mundt }
453eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
454eb6434d9SPaul Mundt 
4551da177e4SLinus Torvalds /*
4561eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
4571eeb66a1SChristoph Hellwig  * have one.
4581eeb66a1SChristoph Hellwig  */
4593b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
4601eeb66a1SChristoph Hellwig {
4611eeb66a1SChristoph Hellwig }
4621eeb66a1SChristoph Hellwig 
46329c185e5SPaul Mundt /**
46429c185e5SPaul Mundt  *	alloc_vm_area - allocate a range of kernel address space
46529c185e5SPaul Mundt  *	@size:		size of the area
46629c185e5SPaul Mundt  *
46729c185e5SPaul Mundt  *	Returns:	NULL on failure, vm_struct on success
46829c185e5SPaul Mundt  *
46929c185e5SPaul Mundt  *	This function reserves a range of kernel address space, and
47029c185e5SPaul Mundt  *	allocates pagetables to map that range.  No actual mappings
47129c185e5SPaul Mundt  *	are created.  If the kernel address space is not shared
47229c185e5SPaul Mundt  *	between processes, it syncs the pagetable across all
47329c185e5SPaul Mundt  *	processes.
47429c185e5SPaul Mundt  */
475cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
47629c185e5SPaul Mundt {
47729c185e5SPaul Mundt 	BUG();
47829c185e5SPaul Mundt 	return NULL;
47929c185e5SPaul Mundt }
48029c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
48129c185e5SPaul Mundt 
48229c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
48329c185e5SPaul Mundt {
48429c185e5SPaul Mundt 	BUG();
48529c185e5SPaul Mundt }
48629c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
48729c185e5SPaul Mundt 
488b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
489b5073173SPaul Mundt 		   struct page *page)
490b5073173SPaul Mundt {
491b5073173SPaul Mundt 	return -EINVAL;
492b5073173SPaul Mundt }
493b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
494b5073173SPaul Mundt 
4951eeb66a1SChristoph Hellwig /*
4961da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4971da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4981da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4991da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
5001da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
5011da177e4SLinus Torvalds  */
5026a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
5071da177e4SLinus Torvalds 		return mm->brk;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	if (mm->brk == brk)
5101da177e4SLinus Torvalds 		return mm->brk;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	/*
5131da177e4SLinus Torvalds 	 * Always allow shrinking brk
5141da177e4SLinus Torvalds 	 */
5151da177e4SLinus Torvalds 	if (brk <= mm->brk) {
5161da177e4SLinus Torvalds 		mm->brk = brk;
5171da177e4SLinus Torvalds 		return brk;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/*
5211da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
5221da177e4SLinus Torvalds 	 */
523cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
5241da177e4SLinus Torvalds 	return mm->brk = brk;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5278feae131SDavid Howells /*
5288feae131SDavid Howells  * initialise the VMA and region record slabs
5298feae131SDavid Howells  */
5308feae131SDavid Howells void __init mmap_init(void)
5311da177e4SLinus Torvalds {
53200a62ce9SKOSAKI Motohiro 	int ret;
53300a62ce9SKOSAKI Motohiro 
534908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
53500a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
5365d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
5378feae131SDavid Howells }
5381da177e4SLinus Torvalds 
5398feae131SDavid Howells /*
5408feae131SDavid Howells  * validate the region tree
5418feae131SDavid Howells  * - the caller must hold the region lock
5428feae131SDavid Howells  */
5438feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
5448feae131SDavid Howells static noinline void validate_nommu_regions(void)
5458feae131SDavid Howells {
5468feae131SDavid Howells 	struct vm_region *region, *last;
5478feae131SDavid Howells 	struct rb_node *p, *lastp;
5481da177e4SLinus Torvalds 
5498feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
5508feae131SDavid Howells 	if (!lastp)
5518feae131SDavid Howells 		return;
5528feae131SDavid Howells 
5538feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
554c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
555c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
5568feae131SDavid Howells 
5578feae131SDavid Howells 	while ((p = rb_next(lastp))) {
5588feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
5598feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
5608feae131SDavid Howells 
561c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
562c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
563c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
5648feae131SDavid Howells 
5658feae131SDavid Howells 		lastp = p;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds }
5688feae131SDavid Howells #else
56933e5d769SDavid Howells static void validate_nommu_regions(void)
57033e5d769SDavid Howells {
57133e5d769SDavid Howells }
5728feae131SDavid Howells #endif
5738feae131SDavid Howells 
5748feae131SDavid Howells /*
5758feae131SDavid Howells  * add a region into the global tree
5768feae131SDavid Howells  */
5778feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
5788feae131SDavid Howells {
5798feae131SDavid Howells 	struct vm_region *pregion;
5808feae131SDavid Howells 	struct rb_node **p, *parent;
5818feae131SDavid Howells 
5828feae131SDavid Howells 	validate_nommu_regions();
5838feae131SDavid Howells 
5848feae131SDavid Howells 	parent = NULL;
5858feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
5868feae131SDavid Howells 	while (*p) {
5878feae131SDavid Howells 		parent = *p;
5888feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5898feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5908feae131SDavid Howells 			p = &(*p)->rb_left;
5918feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5928feae131SDavid Howells 			p = &(*p)->rb_right;
5938feae131SDavid Howells 		else if (pregion == region)
5948feae131SDavid Howells 			return;
5958feae131SDavid Howells 		else
5968feae131SDavid Howells 			BUG();
5978feae131SDavid Howells 	}
5988feae131SDavid Howells 
5998feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
6008feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
6018feae131SDavid Howells 
6028feae131SDavid Howells 	validate_nommu_regions();
6038feae131SDavid Howells }
6048feae131SDavid Howells 
6058feae131SDavid Howells /*
6068feae131SDavid Howells  * delete a region from the global tree
6078feae131SDavid Howells  */
6088feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
6098feae131SDavid Howells {
6108feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6118feae131SDavid Howells 
6128feae131SDavid Howells 	validate_nommu_regions();
6138feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
6148feae131SDavid Howells 	validate_nommu_regions();
6158feae131SDavid Howells }
6168feae131SDavid Howells 
6178feae131SDavid Howells /*
6188feae131SDavid Howells  * free a contiguous series of pages
6198feae131SDavid Howells  */
6208feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
6218feae131SDavid Howells {
6228feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
6238feae131SDavid Howells 		struct page *page = virt_to_page(from);
6248feae131SDavid Howells 
62533e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
6268feae131SDavid Howells 		put_page(page);
6278feae131SDavid Howells 	}
6288feae131SDavid Howells }
6298feae131SDavid Howells 
6308feae131SDavid Howells /*
6318feae131SDavid Howells  * release a reference to a region
63233e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
633dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
6348feae131SDavid Howells  *   will equal vm_start
6358feae131SDavid Howells  */
6368feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
6378feae131SDavid Howells 	__releases(nommu_region_sem)
6388feae131SDavid Howells {
6398feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6408feae131SDavid Howells 
6411e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
642dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
6438feae131SDavid Howells 			delete_nommu_region(region);
6448feae131SDavid Howells 		up_write(&nommu_region_sem);
6458feae131SDavid Howells 
6468feae131SDavid Howells 		if (region->vm_file)
6478feae131SDavid Howells 			fput(region->vm_file);
6488feae131SDavid Howells 
6498feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
6508feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
65122cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
652dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
6538feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
6548feae131SDavid Howells 	} else {
6558feae131SDavid Howells 		up_write(&nommu_region_sem);
6568feae131SDavid Howells 	}
6578feae131SDavid Howells }
6588feae131SDavid Howells 
6598feae131SDavid Howells /*
6608feae131SDavid Howells  * release a reference to a region
6618feae131SDavid Howells  */
6628feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
6638feae131SDavid Howells {
6648feae131SDavid Howells 	down_write(&nommu_region_sem);
6658feae131SDavid Howells 	__put_nommu_region(region);
6668feae131SDavid Howells }
6671da177e4SLinus Torvalds 
6683034097aSDavid Howells /*
669eb8cdec4SBernd Schmidt  * update protection on a vma
670eb8cdec4SBernd Schmidt  */
671eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
672eb8cdec4SBernd Schmidt {
673eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
674eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
675eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
676eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
677eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
678eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
679eb8cdec4SBernd Schmidt 	}
680eb8cdec4SBernd Schmidt 	update_protections(mm);
681eb8cdec4SBernd Schmidt #endif
682eb8cdec4SBernd Schmidt }
683eb8cdec4SBernd Schmidt 
684eb8cdec4SBernd Schmidt /*
6853034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
6868feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
6878feae131SDavid Howells  * page
6883034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
6893034097aSDavid Howells  */
6908feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
6913034097aSDavid Howells {
6926038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
6938feae131SDavid Howells 	struct address_space *mapping;
6946038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
6953034097aSDavid Howells 
6968feae131SDavid Howells 	BUG_ON(!vma->vm_region);
6978feae131SDavid Howells 
6988feae131SDavid Howells 	mm->map_count++;
6998feae131SDavid Howells 	vma->vm_mm = mm;
7008feae131SDavid Howells 
701eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
702eb8cdec4SBernd Schmidt 
7038feae131SDavid Howells 	/* add the VMA to the mapping */
7048feae131SDavid Howells 	if (vma->vm_file) {
7058feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7068feae131SDavid Howells 
70783cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
7088feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7096b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
7108feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
71183cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
7128feae131SDavid Howells 	}
7138feae131SDavid Howells 
7148feae131SDavid Howells 	/* add the VMA to the tree */
7156038def0SNamhyung Kim 	parent = rb_prev = NULL;
7168feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
7178feae131SDavid Howells 	while (*p) {
7188feae131SDavid Howells 		parent = *p;
7198feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
7208feae131SDavid Howells 
7218feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
7228feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
7238feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
7248feae131SDavid Howells 			p = &(*p)->rb_left;
7256038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
7266038def0SNamhyung Kim 			rb_prev = parent;
7278feae131SDavid Howells 			p = &(*p)->rb_right;
7286038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
7298feae131SDavid Howells 			p = &(*p)->rb_left;
7306038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
7316038def0SNamhyung Kim 			rb_prev = parent;
7328feae131SDavid Howells 			p = &(*p)->rb_right;
7336038def0SNamhyung Kim 		} else if (vma < pvma)
7348feae131SDavid Howells 			p = &(*p)->rb_left;
7356038def0SNamhyung Kim 		else if (vma > pvma) {
7366038def0SNamhyung Kim 			rb_prev = parent;
7378feae131SDavid Howells 			p = &(*p)->rb_right;
7386038def0SNamhyung Kim 		} else
7398feae131SDavid Howells 			BUG();
7408feae131SDavid Howells 	}
7418feae131SDavid Howells 
7428feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
7438feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
7448feae131SDavid Howells 
7458feae131SDavid Howells 	/* add VMA to the VMA list also */
7466038def0SNamhyung Kim 	prev = NULL;
7476038def0SNamhyung Kim 	if (rb_prev)
7486038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
7493034097aSDavid Howells 
7506038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
7518feae131SDavid Howells }
7528feae131SDavid Howells 
7538feae131SDavid Howells /*
7548feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
7558feae131SDavid Howells  */
7568feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
7578feae131SDavid Howells {
758615d6e87SDavidlohr Bueso 	int i;
7598feae131SDavid Howells 	struct address_space *mapping;
7608feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
761615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
7628feae131SDavid Howells 
763eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
764eb8cdec4SBernd Schmidt 
7658feae131SDavid Howells 	mm->map_count--;
766615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
767615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
768615d6e87SDavidlohr Bueso 		if (curr->vmacache[i] == vma) {
769e020d5bdSSteven Miao 			vmacache_invalidate(mm);
770615d6e87SDavidlohr Bueso 			break;
771615d6e87SDavidlohr Bueso 		}
772615d6e87SDavidlohr Bueso 	}
7738feae131SDavid Howells 
7748feae131SDavid Howells 	/* remove the VMA from the mapping */
7758feae131SDavid Howells 	if (vma->vm_file) {
7768feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7778feae131SDavid Howells 
77883cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
7798feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7806b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
7818feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
78283cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
7838feae131SDavid Howells 	}
7848feae131SDavid Howells 
7858feae131SDavid Howells 	/* remove from the MM's tree and list */
7868feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
787b951bf2cSNamhyung Kim 
788b951bf2cSNamhyung Kim 	if (vma->vm_prev)
789b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
790b951bf2cSNamhyung Kim 	else
791b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
792b951bf2cSNamhyung Kim 
793b951bf2cSNamhyung Kim 	if (vma->vm_next)
794b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
7958feae131SDavid Howells }
7968feae131SDavid Howells 
7978feae131SDavid Howells /*
7988feae131SDavid Howells  * destroy a VMA record
7998feae131SDavid Howells  */
8008feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
8018feae131SDavid Howells {
8028feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
8038feae131SDavid Howells 		vma->vm_ops->close(vma);
804e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
8058feae131SDavid Howells 		fput(vma->vm_file);
8068feae131SDavid Howells 	put_nommu_region(vma->vm_region);
8078feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
8083034097aSDavid Howells }
8093034097aSDavid Howells 
8103034097aSDavid Howells /*
8113034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
8123034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8133034097aSDavid Howells  */
8143034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
8153034097aSDavid Howells {
8168feae131SDavid Howells 	struct vm_area_struct *vma;
8173034097aSDavid Howells 
8188feae131SDavid Howells 	/* check the cache first */
819615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
820615d6e87SDavidlohr Bueso 	if (likely(vma))
8218feae131SDavid Howells 		return vma;
8228feae131SDavid Howells 
823e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8248feae131SDavid Howells 	 * resides) */
825e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8268feae131SDavid Howells 		if (vma->vm_start > addr)
8278feae131SDavid Howells 			return NULL;
8288feae131SDavid Howells 		if (vma->vm_end > addr) {
829615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
8308feae131SDavid Howells 			return vma;
8313034097aSDavid Howells 		}
8328feae131SDavid Howells 	}
8333034097aSDavid Howells 
8343034097aSDavid Howells 	return NULL;
8353034097aSDavid Howells }
8363034097aSDavid Howells EXPORT_SYMBOL(find_vma);
8373034097aSDavid Howells 
8383034097aSDavid Howells /*
839930e652aSDavid Howells  * find a VMA
840930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
841930e652aSDavid Howells  */
842930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
843930e652aSDavid Howells {
8447561e8caSDavid Howells 	return find_vma(mm, addr);
845930e652aSDavid Howells }
846930e652aSDavid Howells 
8478feae131SDavid Howells /*
8488feae131SDavid Howells  * expand a stack to a given address
8498feae131SDavid Howells  * - not supported under NOMMU conditions
8508feae131SDavid Howells  */
85157c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
85257c8f63eSGreg Ungerer {
85357c8f63eSGreg Ungerer 	return -ENOMEM;
85457c8f63eSGreg Ungerer }
85557c8f63eSGreg Ungerer 
856930e652aSDavid Howells /*
8576fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
8586fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8596fa5f80bSDavid Howells  */
8608feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
8618feae131SDavid Howells 					     unsigned long addr,
8628feae131SDavid Howells 					     unsigned long len)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct vm_area_struct *vma;
8658feae131SDavid Howells 	unsigned long end = addr + len;
8661da177e4SLinus Torvalds 
8678feae131SDavid Howells 	/* check the cache first */
868615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
869615d6e87SDavidlohr Bueso 	if (vma)
8701da177e4SLinus Torvalds 		return vma;
8718feae131SDavid Howells 
872e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8738feae131SDavid Howells 	 * resides) */
874e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8758feae131SDavid Howells 		if (vma->vm_start < addr)
8768feae131SDavid Howells 			continue;
8778feae131SDavid Howells 		if (vma->vm_start > addr)
8788feae131SDavid Howells 			return NULL;
8798feae131SDavid Howells 		if (vma->vm_end == end) {
880615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
8818feae131SDavid Howells 			return vma;
8828feae131SDavid Howells 		}
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	return NULL;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8883034097aSDavid Howells /*
8891da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
8901da177e4SLinus Torvalds  * mapping we're capable of supporting
8911da177e4SLinus Torvalds  */
8921da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
8931da177e4SLinus Torvalds 				 unsigned long addr,
8941da177e4SLinus Torvalds 				 unsigned long len,
8951da177e4SLinus Torvalds 				 unsigned long prot,
8961da177e4SLinus Torvalds 				 unsigned long flags,
8971da177e4SLinus Torvalds 				 unsigned long pgoff,
8981da177e4SLinus Torvalds 				 unsigned long *_capabilities)
8991da177e4SLinus Torvalds {
9008feae131SDavid Howells 	unsigned long capabilities, rlen;
9011da177e4SLinus Torvalds 	int ret;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	/* do the simple checks first */
90422cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
9051da177e4SLinus Torvalds 		return -EINVAL;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
9081da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
9091da177e4SLinus Torvalds 		return -EINVAL;
9101da177e4SLinus Torvalds 
911f81cff0dSMike Frysinger 	if (!len)
9121da177e4SLinus Torvalds 		return -EINVAL;
9131da177e4SLinus Torvalds 
914f81cff0dSMike Frysinger 	/* Careful about overflows.. */
9158feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
9168feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
917f81cff0dSMike Frysinger 		return -ENOMEM;
918f81cff0dSMike Frysinger 
9191da177e4SLinus Torvalds 	/* offset overflow? */
9208feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
921f81cff0dSMike Frysinger 		return -EOVERFLOW;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	if (file) {
9241da177e4SLinus Torvalds 		/* files must support mmap */
92572c2d531SAl Viro 		if (!file->f_op->mmap)
9261da177e4SLinus Torvalds 			return -ENODEV;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
9291da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
9301da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
9311da177e4SLinus Torvalds 		 */
932b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
933b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
934b4caecd4SChristoph Hellwig 		} else {
9351da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
9361da177e4SLinus Torvalds 			 * defaults */
937496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
9381da177e4SLinus Torvalds 			case S_IFREG:
9391da177e4SLinus Torvalds 			case S_IFBLK:
940b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
9411da177e4SLinus Torvalds 				break;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 			case S_IFCHR:
9441da177e4SLinus Torvalds 				capabilities =
945b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
946b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
947b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
9481da177e4SLinus Torvalds 				break;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 			default:
9511da177e4SLinus Torvalds 				return -EINVAL;
9521da177e4SLinus Torvalds 			}
9531da177e4SLinus Torvalds 		}
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
9561da177e4SLinus Torvalds 		 * device */
9571da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
958b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
9596e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
960b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
9611da177e4SLinus Torvalds 
96228d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
96328d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
96428d7a6aeSGraff Yang 			return -EACCES;
96528d7a6aeSGraff Yang 
9661da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
9671da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
9681da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
9691da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
9701da177e4SLinus Torvalds 				return -EACCES;
9711da177e4SLinus Torvalds 
972496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
9731da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
9741da177e4SLinus Torvalds 				return -EACCES;
9751da177e4SLinus Torvalds 
976d7a06983SJeff Layton 			if (locks_verify_locked(file))
9771da177e4SLinus Torvalds 				return -EAGAIN;
9781da177e4SLinus Torvalds 
979b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
9801da177e4SLinus Torvalds 				return -ENODEV;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
983b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
984ac714904SChoi Gi-yong 		} else {
9851da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
9861da177e4SLinus Torvalds 			 * allocate */
987b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
9881da177e4SLinus Torvalds 				return -ENODEV;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
9911da177e4SLinus Torvalds 			 * shared with the backing device */
9921da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
993b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
9941da177e4SLinus Torvalds 		}
9951da177e4SLinus Torvalds 
996b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
997b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
998b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
999b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
10003c7b2045SBernd Schmidt 			    ) {
1001b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
10023c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
100322cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
10043c7b2045SBernd Schmidt 					return -EINVAL;
10053c7b2045SBernd Schmidt 				}
10063c7b2045SBernd Schmidt 			}
10073c7b2045SBernd Schmidt 		}
10083c7b2045SBernd Schmidt 
10091da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
10101da177e4SLinus Torvalds 		 * mappings */
101190f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
10121da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
10131da177e4SLinus Torvalds 				return -EPERM;
1014ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
10151da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
10161da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
1017b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
10181da177e4SLinus Torvalds 					prot |= PROT_EXEC;
10191da177e4SLinus Torvalds 			}
1020ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
10211da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
1022b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
10231da177e4SLinus Torvalds 			 ) {
10241da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
1025b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
10261da177e4SLinus Torvalds 		}
1027ac714904SChoi Gi-yong 	} else {
10281da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
10291da177e4SLinus Torvalds 		 * privately mapped
10301da177e4SLinus Torvalds 		 */
1031b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
10341da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
10351da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
10361da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	/* allow the security API to have its say */
1040e5467859SAl Viro 	ret = security_mmap_addr(addr);
1041e5467859SAl Viro 	if (ret < 0)
1042e5467859SAl Viro 		return ret;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	/* looks okay */
10451da177e4SLinus Torvalds 	*_capabilities = capabilities;
10461da177e4SLinus Torvalds 	return 0;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /*
10501da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
10511da177e4SLinus Torvalds  * now know into VMA flags
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
10541da177e4SLinus Torvalds 					unsigned long prot,
10551da177e4SLinus Torvalds 					unsigned long flags,
10561da177e4SLinus Torvalds 					unsigned long capabilities)
10571da177e4SLinus Torvalds {
10581da177e4SLinus Torvalds 	unsigned long vm_flags;
10591da177e4SLinus Torvalds 
1060e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
10611da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
10621da177e4SLinus Torvalds 
1063b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
10641da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
10653c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10661da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
10671da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10683c7b2045SBernd Schmidt 	} else {
10691da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
10701da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
10711da177e4SLinus Torvalds 		 * romfs/cramfs */
1072b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
10731da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
10743c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
10781da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
10791da177e4SLinus Torvalds 	 * with another untraced process
10801da177e4SLinus Torvalds 	 */
1081a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
10821da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	return vm_flags;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds /*
10888feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
10898feae131SDavid Howells  * pins the storage)
10901da177e4SLinus Torvalds  */
10918feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	int ret;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1096dd8632a1SPaul Mundt 	if (ret == 0) {
1097dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1098645d83c5SDavid Howells 		return 0;
1099dd8632a1SPaul Mundt 	}
11001da177e4SLinus Torvalds 	if (ret != -ENOSYS)
11011da177e4SLinus Torvalds 		return ret;
11021da177e4SLinus Torvalds 
11033fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
11043fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
11053fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
11061da177e4SLinus Torvalds 	return -ENODEV;
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds /*
11101da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
11111da177e4SLinus Torvalds  */
11128feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
11138feae131SDavid Howells 			   struct vm_region *region,
1114645d83c5SDavid Howells 			   unsigned long len,
1115645d83c5SDavid Howells 			   unsigned long capabilities)
11161da177e4SLinus Torvalds {
1117dbc8358cSJoonsoo Kim 	unsigned long total, point;
11181da177e4SLinus Torvalds 	void *base;
11198feae131SDavid Howells 	int ret, order;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
11221da177e4SLinus Torvalds 	 * shared mappings on devices or memory
11231da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
11241da177e4SLinus Torvalds 	 */
1125b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
11261da177e4SLinus Torvalds 		ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1127dd8632a1SPaul Mundt 		if (ret == 0) {
11281da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1129dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1130dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1131645d83c5SDavid Howells 			return 0;
11321da177e4SLinus Torvalds 		}
1133dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1134dd8632a1SPaul Mundt 			return ret;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
11371da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
11381da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds 
11418feae131SDavid Howells 
11421da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
11431da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
11441da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
11451da177e4SLinus Torvalds 	 */
1146f67d9b15SBob Liu 	order = get_order(len);
11478feae131SDavid Howells 	total = 1 << order;
1148f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1149dd8632a1SPaul Mundt 
1150dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
115122cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1152dbc8358cSJoonsoo Kim 		total = point;
11538feae131SDavid Howells 
1154da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1155dbc8358cSJoonsoo Kim 	if (!base)
1156dbc8358cSJoonsoo Kim 		goto enomem;
11578feae131SDavid Howells 
1158dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1159dbc8358cSJoonsoo Kim 
11608feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
11618feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1162f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1163dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
11648feae131SDavid Howells 
11658feae131SDavid Howells 	vma->vm_start = region->vm_start;
11668feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	if (vma->vm_file) {
11691da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
11701da177e4SLinus Torvalds 		mm_segment_t old_fs;
11711da177e4SLinus Torvalds 		loff_t fpos;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
11741da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		old_fs = get_fs();
11771da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
11786e242a1cSAl Viro 		ret = __vfs_read(vma->vm_file, base, len, &fpos);
11791da177e4SLinus Torvalds 		set_fs(old_fs);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 		if (ret < 0)
11821da177e4SLinus Torvalds 			goto error_free;
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 		/* clear the last little bit */
1185f67d9b15SBob Liu 		if (ret < len)
1186f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	}
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	return 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds error_free:
11937223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
11948feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
11958feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1196dd8632a1SPaul Mundt 	region->vm_top   = 0;
11971da177e4SLinus Torvalds 	return ret;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds enomem:
1200b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
120105ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
12027bf02ea2SDavid Rientjes 	show_free_areas(0);
12031da177e4SLinus Torvalds 	return -ENOMEM;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds /*
12071da177e4SLinus Torvalds  * handle mapping creation for uClinux
12081da177e4SLinus Torvalds  */
12091fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
12101da177e4SLinus Torvalds 			unsigned long addr,
12111da177e4SLinus Torvalds 			unsigned long len,
12121da177e4SLinus Torvalds 			unsigned long prot,
12131da177e4SLinus Torvalds 			unsigned long flags,
12141fcfd8dbSOleg Nesterov 			vm_flags_t vm_flags,
1215bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
121641badc15SMichel Lespinasse 			unsigned long *populate)
12171da177e4SLinus Torvalds {
12188feae131SDavid Howells 	struct vm_area_struct *vma;
12198feae131SDavid Howells 	struct vm_region *region;
12201da177e4SLinus Torvalds 	struct rb_node *rb;
12211fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
12221da177e4SLinus Torvalds 	int ret;
12231da177e4SLinus Torvalds 
122441badc15SMichel Lespinasse 	*populate = 0;
1225bebeb3d6SMichel Lespinasse 
12261da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
12271da177e4SLinus Torvalds 	 * mapping */
12281da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
12291da177e4SLinus Torvalds 				    &capabilities);
123022cc877bSLeon Romanovsky 	if (ret < 0)
12311da177e4SLinus Torvalds 		return ret;
12321da177e4SLinus Torvalds 
123306aab5a3SDavid Howells 	/* we ignore the address hint */
123406aab5a3SDavid Howells 	addr = 0;
1235f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
123606aab5a3SDavid Howells 
12371da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
12381da177e4SLinus Torvalds 	 * now know into VMA flags */
12391fcfd8dbSOleg Nesterov 	vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
12401da177e4SLinus Torvalds 
12418feae131SDavid Howells 	/* we're going to need to record the mapping */
12428feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
12438feae131SDavid Howells 	if (!region)
12448feae131SDavid Howells 		goto error_getting_region;
12451da177e4SLinus Torvalds 
12468feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
12478feae131SDavid Howells 	if (!vma)
12488feae131SDavid Howells 		goto error_getting_vma;
12491da177e4SLinus Torvalds 
12501e2ae599SDavid Howells 	region->vm_usage = 1;
12518feae131SDavid Howells 	region->vm_flags = vm_flags;
12528feae131SDavid Howells 	region->vm_pgoff = pgoff;
12538feae131SDavid Howells 
12545beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
12558feae131SDavid Howells 	vma->vm_flags = vm_flags;
12568feae131SDavid Howells 	vma->vm_pgoff = pgoff;
12578feae131SDavid Howells 
12588feae131SDavid Howells 	if (file) {
1259cb0942b8SAl Viro 		region->vm_file = get_file(file);
1260cb0942b8SAl Viro 		vma->vm_file = get_file(file);
12618feae131SDavid Howells 	}
12628feae131SDavid Howells 
12638feae131SDavid Howells 	down_write(&nommu_region_sem);
12648feae131SDavid Howells 
12658feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
12661da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
12678feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
12681da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
12691da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
12701da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
12711da177e4SLinus Torvalds 	 *   than here
12721da177e4SLinus Torvalds 	 */
12731da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
12748feae131SDavid Howells 		struct vm_region *pregion;
12758feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
12761da177e4SLinus Torvalds 
12778feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
12788feae131SDavid Howells 		pgend = pgoff + pglen;
1279165b2392SDavid Howells 
12808feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
12818feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
12821da177e4SLinus Torvalds 
12838feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
12841da177e4SLinus Torvalds 				continue;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1287496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1288496ad9aaSAl Viro 			    file_inode(file))
12891da177e4SLinus Torvalds 				continue;
12901da177e4SLinus Torvalds 
12918feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
12921da177e4SLinus Torvalds 				continue;
12931da177e4SLinus Torvalds 
12948feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
12958feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
12968feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
12978feae131SDavid Howells 			if (pgoff >= rpgend)
12981da177e4SLinus Torvalds 				continue;
12991da177e4SLinus Torvalds 
13008feae131SDavid Howells 			/* handle inexactly overlapping matches between
13018feae131SDavid Howells 			 * mappings */
13028feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
13038feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
13048feae131SDavid Howells 				/* new mapping is not a subset of the region */
1305b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
13061da177e4SLinus Torvalds 					goto sharing_violation;
13071da177e4SLinus Torvalds 				continue;
13081da177e4SLinus Torvalds 			}
13091da177e4SLinus Torvalds 
13108feae131SDavid Howells 			/* we've found a region we can share */
13111e2ae599SDavid Howells 			pregion->vm_usage++;
13128feae131SDavid Howells 			vma->vm_region = pregion;
13138feae131SDavid Howells 			start = pregion->vm_start;
13148feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
13158feae131SDavid Howells 			vma->vm_start = start;
13168feae131SDavid Howells 			vma->vm_end = start + len;
13171da177e4SLinus Torvalds 
131822cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
13198feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
132022cc877bSLeon Romanovsky 			else {
13218feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
13228feae131SDavid Howells 				if (ret < 0) {
13238feae131SDavid Howells 					vma->vm_region = NULL;
13248feae131SDavid Howells 					vma->vm_start = 0;
13258feae131SDavid Howells 					vma->vm_end = 0;
13261e2ae599SDavid Howells 					pregion->vm_usage--;
13278feae131SDavid Howells 					pregion = NULL;
13288feae131SDavid Howells 					goto error_just_free;
13291da177e4SLinus Torvalds 				}
13308feae131SDavid Howells 			}
13318feae131SDavid Howells 			fput(region->vm_file);
13328feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
13338feae131SDavid Howells 			region = pregion;
13348feae131SDavid Howells 			result = start;
13358feae131SDavid Howells 			goto share;
13368feae131SDavid Howells 		}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
13391da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
13401da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
13411da177e4SLinus Torvalds 		 */
1342b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
13431da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
13441da177e4SLinus Torvalds 							     pgoff, flags);
1345bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
13461da177e4SLinus Torvalds 				ret = addr;
1347bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
13488feae131SDavid Howells 					goto error_just_free;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
13511da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
13521da177e4SLinus Torvalds 				 * it */
1353bb005a59SNamhyung Kim 				ret = -ENODEV;
1354b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
13558feae131SDavid Howells 					goto error_just_free;
13561da177e4SLinus Torvalds 
1357b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
13588feae131SDavid Howells 			} else {
13598feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
13608feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
13611da177e4SLinus Torvalds 			}
13621da177e4SLinus Torvalds 		}
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 
13658feae131SDavid Howells 	vma->vm_region = region;
13661da177e4SLinus Torvalds 
1367645d83c5SDavid Howells 	/* set up the mapping
1368b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1369645d83c5SDavid Howells 	 */
13701da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
13718feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
13721da177e4SLinus Torvalds 	else
1373645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
13741da177e4SLinus Torvalds 	if (ret < 0)
1375645d83c5SDavid Howells 		goto error_just_free;
1376645d83c5SDavid Howells 	add_nommu_region(region);
13778feae131SDavid Howells 
1378ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1379ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1380ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1381ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1382ea637639SJie Zhang 
13831da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
13848feae131SDavid Howells 	result = vma->vm_start;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
13871da177e4SLinus Torvalds 
13888feae131SDavid Howells share:
13898feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
13901da177e4SLinus Torvalds 
1391cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1392cfe79c00SMike Frysinger 	 * mapping of it is made  */
1393cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1394cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1395cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1396cfe79c00SMike Frysinger 	}
13971da177e4SLinus Torvalds 
1398cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
13991da177e4SLinus Torvalds 
14008feae131SDavid Howells 	return result;
14011da177e4SLinus Torvalds 
14028feae131SDavid Howells error_just_free:
14038feae131SDavid Howells 	up_write(&nommu_region_sem);
14048feae131SDavid Howells error:
140589a86402SDavid Howells 	if (region->vm_file)
14068feae131SDavid Howells 		fput(region->vm_file);
14078feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
140889a86402SDavid Howells 	if (vma->vm_file)
14098feae131SDavid Howells 		fput(vma->vm_file);
14108feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
14111da177e4SLinus Torvalds 	return ret;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds sharing_violation:
14148feae131SDavid Howells 	up_write(&nommu_region_sem);
141522cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
14168feae131SDavid Howells 	ret = -EINVAL;
14178feae131SDavid Howells 	goto error;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds error_getting_vma:
14208feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
142122cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
14221da177e4SLinus Torvalds 			len, current->pid);
14237bf02ea2SDavid Rientjes 	show_free_areas(0);
14241da177e4SLinus Torvalds 	return -ENOMEM;
14251da177e4SLinus Torvalds 
14268feae131SDavid Howells error_getting_region:
142722cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
14281da177e4SLinus Torvalds 			len, current->pid);
14297bf02ea2SDavid Rientjes 	show_free_areas(0);
14301da177e4SLinus Torvalds 	return -ENOMEM;
14311da177e4SLinus Torvalds }
14326be5ceb0SLinus Torvalds 
143366f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
143466f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
143566f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
143666f0dc48SHugh Dickins {
143766f0dc48SHugh Dickins 	struct file *file = NULL;
143866f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
143966f0dc48SHugh Dickins 
1440120a795dSAl Viro 	audit_mmap_fd(fd, flags);
144166f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
144266f0dc48SHugh Dickins 		file = fget(fd);
144366f0dc48SHugh Dickins 		if (!file)
144466f0dc48SHugh Dickins 			goto out;
144566f0dc48SHugh Dickins 	}
144666f0dc48SHugh Dickins 
144766f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
144866f0dc48SHugh Dickins 
1449ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
145066f0dc48SHugh Dickins 
145166f0dc48SHugh Dickins 	if (file)
145266f0dc48SHugh Dickins 		fput(file);
145366f0dc48SHugh Dickins out:
145466f0dc48SHugh Dickins 	return retval;
145566f0dc48SHugh Dickins }
145666f0dc48SHugh Dickins 
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 
1476a4679373SChristoph Hellwig 	return sys_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  */
15888feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
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);
16543034097aSDavid Howells 	ret = do_munmap(mm, addr, len);
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 
17981da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
17991da177e4SLinus Torvalds 			 loff_t const holebegin, loff_t const holelen,
18001da177e4SLinus Torvalds 			 int even_cows)
18011da177e4SLinus Torvalds {
18021da177e4SLinus Torvalds }
180322c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range);
18041da177e4SLinus Torvalds 
1805d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1806b0e15190SDavid Howells {
1807b0e15190SDavid Howells 	BUG();
1808d0217ac0SNick Piggin 	return 0;
1809b0e15190SDavid Howells }
1810b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
18110ec76a11SDavid Howells 
1812*bae473a4SKirill A. Shutemov void filemap_map_pages(struct fault_env *fe,
1813*bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1814f1820361SKirill A. Shutemov {
1815f1820361SKirill A. Shutemov 	BUG();
1816f1820361SKirill A. Shutemov }
1817f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1818f1820361SKirill A. Shutemov 
1819f55f199bSMike Frysinger static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1820f55f199bSMike Frysinger 		unsigned long addr, void *buf, int len, int write)
18210ec76a11SDavid Howells {
18220ec76a11SDavid Howells 	struct vm_area_struct *vma;
18230ec76a11SDavid Howells 
18240ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
18250ec76a11SDavid Howells 
18260ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
18270159b141SDavid Howells 	vma = find_vma(mm, addr);
18280159b141SDavid Howells 	if (vma) {
18290ec76a11SDavid Howells 		/* don't overrun this mapping */
18300ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
18310ec76a11SDavid Howells 			len = vma->vm_end - addr;
18320ec76a11SDavid Howells 
18330ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1834d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
18357959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
18367959722bSJie Zhang 					 (void *) addr, buf, len);
1837d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
18387959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
18397959722bSJie Zhang 					    buf, (void *) addr, len);
18400ec76a11SDavid Howells 		else
18410ec76a11SDavid Howells 			len = 0;
18420ec76a11SDavid Howells 	} else {
18430ec76a11SDavid Howells 		len = 0;
18440ec76a11SDavid Howells 	}
18450ec76a11SDavid Howells 
18460ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
1847f55f199bSMike Frysinger 
1848f55f199bSMike Frysinger 	return len;
1849f55f199bSMike Frysinger }
1850f55f199bSMike Frysinger 
1851f55f199bSMike Frysinger /**
1852f55f199bSMike Frysinger  * @access_remote_vm - access another process' address space
1853f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1854f55f199bSMike Frysinger  * @addr:	start address to access
1855f55f199bSMike Frysinger  * @buf:	source or destination buffer
1856f55f199bSMike Frysinger  * @len:	number of bytes to transfer
1857f55f199bSMike Frysinger  * @write:	whether the access is a write
1858f55f199bSMike Frysinger  *
1859f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1860f55f199bSMike Frysinger  */
1861f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
1862f55f199bSMike Frysinger 		void *buf, int len, int write)
1863f55f199bSMike Frysinger {
1864f55f199bSMike Frysinger 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
1865f55f199bSMike Frysinger }
1866f55f199bSMike Frysinger 
1867f55f199bSMike Frysinger /*
1868f55f199bSMike Frysinger  * Access another process' address space.
1869f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1870f55f199bSMike Frysinger  */
1871f55f199bSMike Frysinger int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1872f55f199bSMike Frysinger {
1873f55f199bSMike Frysinger 	struct mm_struct *mm;
1874f55f199bSMike Frysinger 
1875f55f199bSMike Frysinger 	if (addr + len < addr)
1876f55f199bSMike Frysinger 		return 0;
1877f55f199bSMike Frysinger 
1878f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1879f55f199bSMike Frysinger 	if (!mm)
1880f55f199bSMike Frysinger 		return 0;
1881f55f199bSMike Frysinger 
1882f55f199bSMike Frysinger 	len = __access_remote_vm(tsk, mm, addr, buf, len, write);
1883f55f199bSMike Frysinger 
18840ec76a11SDavid Howells 	mmput(mm);
18850ec76a11SDavid Howells 	return len;
18860ec76a11SDavid Howells }
18877e660872SDavid Howells 
18887e660872SDavid Howells /**
18897e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
18907e660872SDavid Howells  * @inode: The inode to check
18917e660872SDavid Howells  * @size: The current filesize of the inode
18927e660872SDavid Howells  * @newsize: The proposed filesize of the inode
18937e660872SDavid Howells  *
18947e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
18957e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
18967e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
18977e660872SDavid Howells  * automatically grant mappings that are too large.
18987e660872SDavid Howells  */
18997e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
19007e660872SDavid Howells 				size_t newsize)
19017e660872SDavid Howells {
19027e660872SDavid Howells 	struct vm_area_struct *vma;
19037e660872SDavid Howells 	struct vm_region *region;
19047e660872SDavid Howells 	pgoff_t low, high;
19057e660872SDavid Howells 	size_t r_size, r_top;
19067e660872SDavid Howells 
19077e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
19087e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
19097e660872SDavid Howells 
19107e660872SDavid Howells 	down_write(&nommu_region_sem);
19111acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
19127e660872SDavid Howells 
19137e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
19146b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
19157e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
19167e660872SDavid Howells 		 * cache */
19177e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
19181acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
19197e660872SDavid Howells 			up_write(&nommu_region_sem);
19207e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
19217e660872SDavid Howells 		}
19227e660872SDavid Howells 	}
19237e660872SDavid Howells 
19247e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
19257e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
19267e660872SDavid Howells 	 *
19277e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
19287e660872SDavid Howells 	 * shouldn't be any
19297e660872SDavid Howells 	 */
19301acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
19317e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
19327e660872SDavid Howells 			continue;
19337e660872SDavid Howells 
19347e660872SDavid Howells 		region = vma->vm_region;
19357e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
19367e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
19377e660872SDavid Howells 
19387e660872SDavid Howells 		if (r_top > newsize) {
19397e660872SDavid Howells 			region->vm_top -= r_top - newsize;
19407e660872SDavid Howells 			if (region->vm_end > region->vm_top)
19417e660872SDavid Howells 				region->vm_end = region->vm_top;
19427e660872SDavid Howells 		}
19437e660872SDavid Howells 	}
19447e660872SDavid Howells 
19451acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
19467e660872SDavid Howells 	up_write(&nommu_region_sem);
19477e660872SDavid Howells 	return 0;
19487e660872SDavid Howells }
1949c9b1d098SAndrew Shewmaker 
1950c9b1d098SAndrew Shewmaker /*
1951c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1952c9b1d098SAndrew Shewmaker  *
1953c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1954c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1955c9b1d098SAndrew Shewmaker  * mode.
1956c9b1d098SAndrew Shewmaker  *
1957c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1958c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1959c9b1d098SAndrew Shewmaker  */
1960c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1961c9b1d098SAndrew Shewmaker {
1962c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1963c9b1d098SAndrew Shewmaker 
1964c9b1d098SAndrew Shewmaker 	free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1965c9b1d098SAndrew Shewmaker 
1966c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1967c9b1d098SAndrew Shewmaker 	return 0;
1968c9b1d098SAndrew Shewmaker }
1969a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
19704eeab4f5SAndrew Shewmaker 
19714eeab4f5SAndrew Shewmaker /*
19724eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
19734eeab4f5SAndrew Shewmaker  *
19744eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
19754eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
19764eeab4f5SAndrew Shewmaker  *
19774eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
19784eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
19794eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
19804eeab4f5SAndrew Shewmaker  */
19814eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
19824eeab4f5SAndrew Shewmaker {
19834eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
19844eeab4f5SAndrew Shewmaker 
19854eeab4f5SAndrew Shewmaker 	free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
19864eeab4f5SAndrew Shewmaker 
19874eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
19884eeab4f5SAndrew Shewmaker 	return 0;
19894eeab4f5SAndrew Shewmaker }
1990a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1991