xref: /linux/mm/nommu.c (revision 6e242a1ceeb1bcf55ffefa84d3079f711fe8a667)
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>
36cf4aebc2SClark Williams #include <linux/sched/sysctl.h>
37b1de0d13SMitchel Humpherys #include <linux/printk.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <asm/uaccess.h>
401da177e4SLinus Torvalds #include <asm/tlb.h>
411da177e4SLinus Torvalds #include <asm/tlbflush.h>
42eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
438feae131SDavid Howells #include "internal.h"
448feae131SDavid Howells 
458feae131SDavid Howells #if 0
468feae131SDavid Howells #define kenter(FMT, ...) \
478feae131SDavid Howells 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
488feae131SDavid Howells #define kleave(FMT, ...) \
498feae131SDavid Howells 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
508feae131SDavid Howells #define kdebug(FMT, ...) \
518feae131SDavid Howells 	printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
528feae131SDavid Howells #else
538feae131SDavid Howells #define kenter(FMT, ...) \
548feae131SDavid Howells 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
558feae131SDavid Howells #define kleave(FMT, ...) \
568feae131SDavid Howells 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
578feae131SDavid Howells #define kdebug(FMT, ...) \
588feae131SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
598feae131SDavid Howells #endif
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds void *high_memory;
62944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
631da177e4SLinus Torvalds struct page *mem_map;
641da177e4SLinus Torvalds unsigned long max_mapnr;
655b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
664266c97aSHugh Dickins unsigned long highest_memmap_pfn;
6700a62ce9SKOSAKI Motohiro struct percpu_counter vm_committed_as;
681da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
691da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */
7049f0ce5fSJerome Marchand unsigned long sysctl_overcommit_kbytes __read_mostly;
711da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
72fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
73c9b1d098SAndrew Shewmaker unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
744eeab4f5SAndrew Shewmaker unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
751da177e4SLinus Torvalds int heap_stack_gap = 0;
761da177e4SLinus Torvalds 
7733e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
788feae131SDavid Howells 
79997071bcSK. Y. Srinivasan /*
80997071bcSK. Y. Srinivasan  * The global memory commitment made in the system can be a metric
81997071bcSK. Y. Srinivasan  * that can be used to drive ballooning decisions when Linux is hosted
82997071bcSK. Y. Srinivasan  * as a guest. On Hyper-V, the host implements a policy engine for dynamically
83997071bcSK. Y. Srinivasan  * balancing memory across competing virtual machines that are hosted.
84997071bcSK. Y. Srinivasan  * Several metrics drive this policy engine including the guest reported
85997071bcSK. Y. Srinivasan  * memory commitment.
86997071bcSK. Y. Srinivasan  */
87997071bcSK. Y. Srinivasan unsigned long vm_memory_committed(void)
88997071bcSK. Y. Srinivasan {
89997071bcSK. Y. Srinivasan 	return percpu_counter_read_positive(&vm_committed_as);
90997071bcSK. Y. Srinivasan }
91997071bcSK. Y. Srinivasan 
92997071bcSK. Y. Srinivasan EXPORT_SYMBOL_GPL(vm_memory_committed);
93997071bcSK. Y. Srinivasan 
941da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
951da177e4SLinus Torvalds 
968feae131SDavid Howells /* list of mapped, potentially shareable regions */
978feae131SDavid Howells static struct kmem_cache *vm_region_jar;
988feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
998feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
1001da177e4SLinus Torvalds 
101f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
1021da177e4SLinus Torvalds };
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
1061da177e4SLinus Torvalds  * just what the caller asked for.
1071da177e4SLinus Torvalds  *
1081da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	struct page *page;
1131da177e4SLinus Torvalds 
1144016a139SMichael Hennerich 	/*
1154016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
1164016a139SMichael Hennerich 	 * return size of 0
1174016a139SMichael Hennerich 	 */
1185a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
1196cfd53fcSPaul Mundt 		return 0;
1206cfd53fcSPaul Mundt 
1216cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
1226cfd53fcSPaul Mundt 
1236cfd53fcSPaul Mundt 	/*
1246cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
1256cfd53fcSPaul Mundt 	 * kmalloc().
1266cfd53fcSPaul Mundt 	 */
1271da177e4SLinus Torvalds 	if (PageSlab(page))
1281da177e4SLinus Torvalds 		return ksize(objp);
1291da177e4SLinus Torvalds 
1306cfd53fcSPaul Mundt 	/*
131ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
132ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
133ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
134ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
135ab2e83eaSPaul Mundt 	 */
136ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
137ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
138ab2e83eaSPaul Mundt 
139ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
140ab2e83eaSPaul Mundt 		if (vma)
141ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
142ab2e83eaSPaul Mundt 	}
143ab2e83eaSPaul Mundt 
144ab2e83eaSPaul Mundt 	/*
1456cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1465a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1476cfd53fcSPaul Mundt 	 */
1485a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(page);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
15128a35716SMichel Lespinasse long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
15228a35716SMichel Lespinasse 		      unsigned long start, unsigned long nr_pages,
15328a35716SMichel Lespinasse 		      unsigned int foll_flags, struct page **pages,
15428a35716SMichel Lespinasse 		      struct vm_area_struct **vmas, int *nonblocking)
1551da177e4SLinus Torvalds {
156910e46daSSonic Zhang 	struct vm_area_struct *vma;
1577b4d5b8bSDavid Howells 	unsigned long vm_flags;
1587b4d5b8bSDavid Howells 	int i;
1597b4d5b8bSDavid Howells 
1607b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
16158fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1627b4d5b8bSDavid Howells 	 */
16358fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
16458fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
16558fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
16658fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1671da177e4SLinus Torvalds 
1689d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
1697561e8caSDavid Howells 		vma = find_vma(mm, start);
170910e46daSSonic Zhang 		if (!vma)
1717b4d5b8bSDavid Howells 			goto finish_or_fault;
1727b4d5b8bSDavid Howells 
1737b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1741c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1751c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1767b4d5b8bSDavid Howells 			goto finish_or_fault;
177910e46daSSonic Zhang 
1781da177e4SLinus Torvalds 		if (pages) {
1791da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1801da177e4SLinus Torvalds 			if (pages[i])
1811da177e4SLinus Torvalds 				page_cache_get(pages[i]);
1821da177e4SLinus Torvalds 		}
1831da177e4SLinus Torvalds 		if (vmas)
184910e46daSSonic Zhang 			vmas[i] = vma;
185e1ee65d8SDavid Howells 		start = (start + PAGE_SIZE) & PAGE_MASK;
1861da177e4SLinus Torvalds 	}
1877b4d5b8bSDavid Howells 
1887b4d5b8bSDavid Howells 	return i;
1897b4d5b8bSDavid Howells 
1907b4d5b8bSDavid Howells finish_or_fault:
1917b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1921da177e4SLinus Torvalds }
193b291f000SNick Piggin 
194b291f000SNick Piggin /*
195b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
196b291f000SNick Piggin  * and indicate the VMA that covers each page
197b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
198b291f000SNick Piggin  *   slab page or a secondary page from a compound page
199b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
200b291f000SNick Piggin  */
20128a35716SMichel Lespinasse long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
20228a35716SMichel Lespinasse 		    unsigned long start, unsigned long nr_pages,
20328a35716SMichel Lespinasse 		    int write, int force, struct page **pages,
20428a35716SMichel Lespinasse 		    struct vm_area_struct **vmas)
205b291f000SNick Piggin {
206b291f000SNick Piggin 	int flags = 0;
207b291f000SNick Piggin 
208b291f000SNick Piggin 	if (write)
20958fa879eSHugh Dickins 		flags |= FOLL_WRITE;
210b291f000SNick Piggin 	if (force)
21158fa879eSHugh Dickins 		flags |= FOLL_FORCE;
212b291f000SNick Piggin 
21353a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
21453a7706dSMichel Lespinasse 				NULL);
215b291f000SNick Piggin }
21666aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages);
21766aa2b4bSGreg Ungerer 
218f0818f47SAndrea Arcangeli long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
219f0818f47SAndrea Arcangeli 			   unsigned long start, unsigned long nr_pages,
220f0818f47SAndrea Arcangeli 			   int write, int force, struct page **pages,
221f0818f47SAndrea Arcangeli 			   int *locked)
222f0818f47SAndrea Arcangeli {
223f0818f47SAndrea Arcangeli 	return get_user_pages(tsk, mm, start, nr_pages, write, force,
224f0818f47SAndrea Arcangeli 			      pages, NULL);
225f0818f47SAndrea Arcangeli }
226f0818f47SAndrea Arcangeli EXPORT_SYMBOL(get_user_pages_locked);
227f0818f47SAndrea Arcangeli 
2280fd71a56SAndrea Arcangeli long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
229f0818f47SAndrea Arcangeli 			       unsigned long start, unsigned long nr_pages,
2300fd71a56SAndrea Arcangeli 			       int write, int force, struct page **pages,
2310fd71a56SAndrea Arcangeli 			       unsigned int gup_flags)
232f0818f47SAndrea Arcangeli {
233f0818f47SAndrea Arcangeli 	long ret;
234f0818f47SAndrea Arcangeli 	down_read(&mm->mmap_sem);
235f0818f47SAndrea Arcangeli 	ret = get_user_pages(tsk, mm, start, nr_pages, write, force,
236f0818f47SAndrea Arcangeli 			     pages, NULL);
237f0818f47SAndrea Arcangeli 	up_read(&mm->mmap_sem);
238f0818f47SAndrea Arcangeli 	return ret;
239f0818f47SAndrea Arcangeli }
2400fd71a56SAndrea Arcangeli EXPORT_SYMBOL(__get_user_pages_unlocked);
2410fd71a56SAndrea Arcangeli 
2420fd71a56SAndrea Arcangeli long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
2430fd71a56SAndrea Arcangeli 			     unsigned long start, unsigned long nr_pages,
2440fd71a56SAndrea Arcangeli 			     int write, int force, struct page **pages)
2450fd71a56SAndrea Arcangeli {
2460fd71a56SAndrea Arcangeli 	return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
2470fd71a56SAndrea Arcangeli 					 force, pages, 0);
2480fd71a56SAndrea Arcangeli }
249f0818f47SAndrea Arcangeli EXPORT_SYMBOL(get_user_pages_unlocked);
250f0818f47SAndrea Arcangeli 
251dfc2f91aSPaul Mundt /**
252dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
253dfc2f91aSPaul Mundt  * @vma: memory mapping
254dfc2f91aSPaul Mundt  * @address: user virtual address
255dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
256dfc2f91aSPaul Mundt  *
257dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
258dfc2f91aSPaul Mundt  *
259dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
260dfc2f91aSPaul Mundt  */
261dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
262dfc2f91aSPaul Mundt 	unsigned long *pfn)
263dfc2f91aSPaul Mundt {
264dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
265dfc2f91aSPaul Mundt 		return -EINVAL;
266dfc2f91aSPaul Mundt 
267dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
268dfc2f91aSPaul Mundt 	return 0;
269dfc2f91aSPaul Mundt }
270dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
271dfc2f91aSPaul Mundt 
272f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
2731da177e4SLinus Torvalds 
274b3bdda02SChristoph Lameter void vfree(const void *addr)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	kfree(addr);
2771da177e4SLinus Torvalds }
278b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2791da177e4SLinus Torvalds 
280dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	/*
2838518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2848518609dSRobert P. J. Day 	 * returns only a logical address.
2851da177e4SLinus Torvalds 	 */
28684097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2871da177e4SLinus Torvalds }
288b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2891da177e4SLinus Torvalds 
290f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
291f905bc44SPaul Mundt {
292f905bc44SPaul Mundt 	void *ret;
293f905bc44SPaul Mundt 
294f905bc44SPaul Mundt 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
295f905bc44SPaul Mundt 			PAGE_KERNEL);
296f905bc44SPaul Mundt 	if (ret) {
297f905bc44SPaul Mundt 		struct vm_area_struct *vma;
298f905bc44SPaul Mundt 
299f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
300f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
301f905bc44SPaul Mundt 		if (vma)
302f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
303f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
304f905bc44SPaul Mundt 	}
305f905bc44SPaul Mundt 
306f905bc44SPaul Mundt 	return ret;
307f905bc44SPaul Mundt }
308f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
309f905bc44SPaul Mundt 
310b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	return virt_to_page(addr);
3131da177e4SLinus Torvalds }
314b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
3151da177e4SLinus Torvalds 
316b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
3191da177e4SLinus Torvalds }
320b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
3231da177e4SLinus Torvalds {
3249bde916bSChen Gang 	/* Don't allow overflow */
3259bde916bSChen Gang 	if ((unsigned long) buf + count < count)
3269bde916bSChen Gang 		count = -(unsigned long) buf;
3279bde916bSChen Gang 
3281da177e4SLinus Torvalds 	memcpy(buf, addr, count);
3291da177e4SLinus Torvalds 	return count;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	/* Don't allow overflow */
3351da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
3361da177e4SLinus Torvalds 		count = -(unsigned long) addr;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	memcpy(addr, buf, count);
339ac714904SChoi Gi-yong 	return count;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds /*
3431da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually continguos memory
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  *	@size:		allocation size
3461da177e4SLinus Torvalds  *
3471da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
3481da177e4SLinus Torvalds  *	allocator and map them into continguos kernel virtual space.
3491da177e4SLinus Torvalds  *
350c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
3511da177e4SLinus Torvalds  *	use __vmalloc() instead.
3521da177e4SLinus Torvalds  */
3531da177e4SLinus Torvalds void *vmalloc(unsigned long size)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
3561da177e4SLinus Torvalds }
357f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
358f6138882SAndrew Morton 
359e1ca7788SDave Young /*
360e1ca7788SDave Young  *	vzalloc - allocate virtually continguos memory with zero fill
361e1ca7788SDave Young  *
362e1ca7788SDave Young  *	@size:		allocation size
363e1ca7788SDave Young  *
364e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
365e1ca7788SDave Young  *	allocator and map them into continguos kernel virtual space.
366e1ca7788SDave Young  *	The memory allocated is set to zero.
367e1ca7788SDave Young  *
368e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
369e1ca7788SDave Young  *	use __vmalloc() instead.
370e1ca7788SDave Young  */
371e1ca7788SDave Young void *vzalloc(unsigned long size)
372e1ca7788SDave Young {
373e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
374e1ca7788SDave Young 			PAGE_KERNEL);
375e1ca7788SDave Young }
376e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
377e1ca7788SDave Young 
378e1ca7788SDave Young /**
379e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
380e1ca7788SDave Young  * @size:	allocation size
381e1ca7788SDave Young  * @node:	numa node
382e1ca7788SDave Young  *
383e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
384e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
385e1ca7788SDave Young  *
386e1ca7788SDave Young  * For tight control over page level allocator and protection flags
387e1ca7788SDave Young  * use __vmalloc() instead.
388e1ca7788SDave Young  */
389f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
390f6138882SAndrew Morton {
391f6138882SAndrew Morton 	return vmalloc(size);
392f6138882SAndrew Morton }
3939a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
394e1ca7788SDave Young 
395e1ca7788SDave Young /**
396e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
397e1ca7788SDave Young  * @size:	allocation size
398e1ca7788SDave Young  * @node:	numa node
399e1ca7788SDave Young  *
400e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
401e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
402e1ca7788SDave Young  * The memory allocated is set to zero.
403e1ca7788SDave Young  *
404e1ca7788SDave Young  * For tight control over page level allocator and protection flags
405e1ca7788SDave Young  * use __vmalloc() instead.
406e1ca7788SDave Young  */
407e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
408e1ca7788SDave Young {
409e1ca7788SDave Young 	return vzalloc(size);
410e1ca7788SDave Young }
411e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
4121da177e4SLinus Torvalds 
4131af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
4141af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
4151af446edSPaul Mundt #endif
4161af446edSPaul Mundt 
4171af446edSPaul Mundt /**
4181af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
4191af446edSPaul Mundt  *	@size:		allocation size
4201af446edSPaul Mundt  *
4211af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
4221af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
4231af446edSPaul Mundt  *	executable kernel virtual space.
4241af446edSPaul Mundt  *
4251af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
4261af446edSPaul Mundt  *	use __vmalloc() instead.
4271af446edSPaul Mundt  */
4281af446edSPaul Mundt 
4291af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
4301af446edSPaul Mundt {
4311af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
4321af446edSPaul Mundt }
4331af446edSPaul Mundt 
434b5073173SPaul Mundt /**
435b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
4361da177e4SLinus Torvalds  *	@size:		allocation size
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
4391da177e4SLinus Torvalds  *	page level allocator and map them into continguos kernel virtual space.
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
4441da177e4SLinus Torvalds }
445b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
446b5073173SPaul Mundt 
447b5073173SPaul Mundt /**
448b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
449b5073173SPaul Mundt  *	@size:		allocation size
450b5073173SPaul Mundt  *
451b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
452b5073173SPaul Mundt  * mapped to userspace without leaking data.
453f905bc44SPaul Mundt  *
454f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
455f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
456b5073173SPaul Mundt  */
457b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
458b5073173SPaul Mundt {
459f905bc44SPaul Mundt 	/*
460f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
461f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
462f905bc44SPaul Mundt 	 */
463f905bc44SPaul Mundt 	return vmalloc_user(size);
464b5073173SPaul Mundt }
465b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	BUG();
4701da177e4SLinus Torvalds 	return NULL;
4711da177e4SLinus Torvalds }
472b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
4731da177e4SLinus Torvalds 
474b3bdda02SChristoph Lameter void vunmap(const void *addr)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	BUG();
4771da177e4SLinus Torvalds }
478b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
4791da177e4SLinus Torvalds 
480eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
481eb6434d9SPaul Mundt {
482eb6434d9SPaul Mundt 	BUG();
483eb6434d9SPaul Mundt 	return NULL;
484eb6434d9SPaul Mundt }
485eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
486eb6434d9SPaul Mundt 
487eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
488eb6434d9SPaul Mundt {
489eb6434d9SPaul Mundt 	BUG();
490eb6434d9SPaul Mundt }
491eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
492eb6434d9SPaul Mundt 
493eb6434d9SPaul Mundt void vm_unmap_aliases(void)
494eb6434d9SPaul Mundt {
495eb6434d9SPaul Mundt }
496eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
497eb6434d9SPaul Mundt 
4981da177e4SLinus Torvalds /*
4991eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
5001eeb66a1SChristoph Hellwig  * have one.
5011eeb66a1SChristoph Hellwig  */
5023b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
5031eeb66a1SChristoph Hellwig {
5041eeb66a1SChristoph Hellwig }
5051eeb66a1SChristoph Hellwig 
50629c185e5SPaul Mundt /**
50729c185e5SPaul Mundt  *	alloc_vm_area - allocate a range of kernel address space
50829c185e5SPaul Mundt  *	@size:		size of the area
50929c185e5SPaul Mundt  *
51029c185e5SPaul Mundt  *	Returns:	NULL on failure, vm_struct on success
51129c185e5SPaul Mundt  *
51229c185e5SPaul Mundt  *	This function reserves a range of kernel address space, and
51329c185e5SPaul Mundt  *	allocates pagetables to map that range.  No actual mappings
51429c185e5SPaul Mundt  *	are created.  If the kernel address space is not shared
51529c185e5SPaul Mundt  *	between processes, it syncs the pagetable across all
51629c185e5SPaul Mundt  *	processes.
51729c185e5SPaul Mundt  */
518cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
51929c185e5SPaul Mundt {
52029c185e5SPaul Mundt 	BUG();
52129c185e5SPaul Mundt 	return NULL;
52229c185e5SPaul Mundt }
52329c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
52429c185e5SPaul Mundt 
52529c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
52629c185e5SPaul Mundt {
52729c185e5SPaul Mundt 	BUG();
52829c185e5SPaul Mundt }
52929c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
53029c185e5SPaul Mundt 
531b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
532b5073173SPaul Mundt 		   struct page *page)
533b5073173SPaul Mundt {
534b5073173SPaul Mundt 	return -EINVAL;
535b5073173SPaul Mundt }
536b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
537b5073173SPaul Mundt 
5381eeb66a1SChristoph Hellwig /*
5391da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
5401da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
5411da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
5421da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
5431da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
5441da177e4SLinus Torvalds  */
5456a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
5501da177e4SLinus Torvalds 		return mm->brk;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	if (mm->brk == brk)
5531da177e4SLinus Torvalds 		return mm->brk;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	/*
5561da177e4SLinus Torvalds 	 * Always allow shrinking brk
5571da177e4SLinus Torvalds 	 */
5581da177e4SLinus Torvalds 	if (brk <= mm->brk) {
5591da177e4SLinus Torvalds 		mm->brk = brk;
5601da177e4SLinus Torvalds 		return brk;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	/*
5641da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
5651da177e4SLinus Torvalds 	 */
566cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
5671da177e4SLinus Torvalds 	return mm->brk = brk;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5708feae131SDavid Howells /*
5718feae131SDavid Howells  * initialise the VMA and region record slabs
5728feae131SDavid Howells  */
5738feae131SDavid Howells void __init mmap_init(void)
5741da177e4SLinus Torvalds {
57500a62ce9SKOSAKI Motohiro 	int ret;
57600a62ce9SKOSAKI Motohiro 
577908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
57800a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
57933e5d769SDavid Howells 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
5808feae131SDavid Howells }
5811da177e4SLinus Torvalds 
5828feae131SDavid Howells /*
5838feae131SDavid Howells  * validate the region tree
5848feae131SDavid Howells  * - the caller must hold the region lock
5858feae131SDavid Howells  */
5868feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
5878feae131SDavid Howells static noinline void validate_nommu_regions(void)
5888feae131SDavid Howells {
5898feae131SDavid Howells 	struct vm_region *region, *last;
5908feae131SDavid Howells 	struct rb_node *p, *lastp;
5911da177e4SLinus Torvalds 
5928feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
5938feae131SDavid Howells 	if (!lastp)
5948feae131SDavid Howells 		return;
5958feae131SDavid Howells 
5968feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
59733e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_end <= last->vm_start));
59833e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_top < last->vm_end));
5998feae131SDavid Howells 
6008feae131SDavid Howells 	while ((p = rb_next(lastp))) {
6018feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
6028feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
6038feae131SDavid Howells 
60433e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_end <= region->vm_start));
60533e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_top < region->vm_end));
60633e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_start < last->vm_top));
6078feae131SDavid Howells 
6088feae131SDavid Howells 		lastp = p;
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds }
6118feae131SDavid Howells #else
61233e5d769SDavid Howells static void validate_nommu_regions(void)
61333e5d769SDavid Howells {
61433e5d769SDavid Howells }
6158feae131SDavid Howells #endif
6168feae131SDavid Howells 
6178feae131SDavid Howells /*
6188feae131SDavid Howells  * add a region into the global tree
6198feae131SDavid Howells  */
6208feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
6218feae131SDavid Howells {
6228feae131SDavid Howells 	struct vm_region *pregion;
6238feae131SDavid Howells 	struct rb_node **p, *parent;
6248feae131SDavid Howells 
6258feae131SDavid Howells 	validate_nommu_regions();
6268feae131SDavid Howells 
6278feae131SDavid Howells 	parent = NULL;
6288feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
6298feae131SDavid Howells 	while (*p) {
6308feae131SDavid Howells 		parent = *p;
6318feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
6328feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
6338feae131SDavid Howells 			p = &(*p)->rb_left;
6348feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
6358feae131SDavid Howells 			p = &(*p)->rb_right;
6368feae131SDavid Howells 		else if (pregion == region)
6378feae131SDavid Howells 			return;
6388feae131SDavid Howells 		else
6398feae131SDavid Howells 			BUG();
6408feae131SDavid Howells 	}
6418feae131SDavid Howells 
6428feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
6438feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
6448feae131SDavid Howells 
6458feae131SDavid Howells 	validate_nommu_regions();
6468feae131SDavid Howells }
6478feae131SDavid Howells 
6488feae131SDavid Howells /*
6498feae131SDavid Howells  * delete a region from the global tree
6508feae131SDavid Howells  */
6518feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
6528feae131SDavid Howells {
6538feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6548feae131SDavid Howells 
6558feae131SDavid Howells 	validate_nommu_regions();
6568feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
6578feae131SDavid Howells 	validate_nommu_regions();
6588feae131SDavid Howells }
6598feae131SDavid Howells 
6608feae131SDavid Howells /*
6618feae131SDavid Howells  * free a contiguous series of pages
6628feae131SDavid Howells  */
6638feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
6648feae131SDavid Howells {
6658feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
6668feae131SDavid Howells 		struct page *page = virt_to_page(from);
6678feae131SDavid Howells 
6688feae131SDavid Howells 		kdebug("- free %lx", from);
66933e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
6708feae131SDavid Howells 		if (page_count(page) != 1)
67133e5d769SDavid Howells 			kdebug("free page %p: refcount not one: %d",
67233e5d769SDavid Howells 			       page, page_count(page));
6738feae131SDavid Howells 		put_page(page);
6748feae131SDavid Howells 	}
6758feae131SDavid Howells }
6768feae131SDavid Howells 
6778feae131SDavid Howells /*
6788feae131SDavid Howells  * release a reference to a region
67933e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
680dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
6818feae131SDavid Howells  *   will equal vm_start
6828feae131SDavid Howells  */
6838feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
6848feae131SDavid Howells 	__releases(nommu_region_sem)
6858feae131SDavid Howells {
6861e2ae599SDavid Howells 	kenter("%p{%d}", region, region->vm_usage);
6878feae131SDavid Howells 
6888feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6898feae131SDavid Howells 
6901e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
691dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
6928feae131SDavid Howells 			delete_nommu_region(region);
6938feae131SDavid Howells 		up_write(&nommu_region_sem);
6948feae131SDavid Howells 
6958feae131SDavid Howells 		if (region->vm_file)
6968feae131SDavid Howells 			fput(region->vm_file);
6978feae131SDavid Howells 
6988feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
6998feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
7008feae131SDavid Howells 		if (region->vm_flags & VM_MAPPED_COPY) {
7018feae131SDavid Howells 			kdebug("free series");
702dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
7038feae131SDavid Howells 		}
7048feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
7058feae131SDavid Howells 	} else {
7068feae131SDavid Howells 		up_write(&nommu_region_sem);
7078feae131SDavid Howells 	}
7088feae131SDavid Howells }
7098feae131SDavid Howells 
7108feae131SDavid Howells /*
7118feae131SDavid Howells  * release a reference to a region
7128feae131SDavid Howells  */
7138feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
7148feae131SDavid Howells {
7158feae131SDavid Howells 	down_write(&nommu_region_sem);
7168feae131SDavid Howells 	__put_nommu_region(region);
7178feae131SDavid Howells }
7181da177e4SLinus Torvalds 
7193034097aSDavid Howells /*
720eb8cdec4SBernd Schmidt  * update protection on a vma
721eb8cdec4SBernd Schmidt  */
722eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
723eb8cdec4SBernd Schmidt {
724eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
725eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
726eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
727eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
728eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
729eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
730eb8cdec4SBernd Schmidt 	}
731eb8cdec4SBernd Schmidt 	update_protections(mm);
732eb8cdec4SBernd Schmidt #endif
733eb8cdec4SBernd Schmidt }
734eb8cdec4SBernd Schmidt 
735eb8cdec4SBernd Schmidt /*
7363034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
7378feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
7388feae131SDavid Howells  * page
7393034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
7403034097aSDavid Howells  */
7418feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
7423034097aSDavid Howells {
7436038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
7448feae131SDavid Howells 	struct address_space *mapping;
7456038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
7463034097aSDavid Howells 
7478feae131SDavid Howells 	kenter(",%p", vma);
7488feae131SDavid Howells 
7498feae131SDavid Howells 	BUG_ON(!vma->vm_region);
7508feae131SDavid Howells 
7518feae131SDavid Howells 	mm->map_count++;
7528feae131SDavid Howells 	vma->vm_mm = mm;
7538feae131SDavid Howells 
754eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
755eb8cdec4SBernd Schmidt 
7568feae131SDavid Howells 	/* add the VMA to the mapping */
7578feae131SDavid Howells 	if (vma->vm_file) {
7588feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7598feae131SDavid Howells 
76083cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
7618feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7626b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
7638feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
76483cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
7658feae131SDavid Howells 	}
7668feae131SDavid Howells 
7678feae131SDavid Howells 	/* add the VMA to the tree */
7686038def0SNamhyung Kim 	parent = rb_prev = NULL;
7698feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
7708feae131SDavid Howells 	while (*p) {
7718feae131SDavid Howells 		parent = *p;
7728feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
7738feae131SDavid Howells 
7748feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
7758feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
7768feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
7778feae131SDavid Howells 			p = &(*p)->rb_left;
7786038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
7796038def0SNamhyung Kim 			rb_prev = parent;
7808feae131SDavid Howells 			p = &(*p)->rb_right;
7816038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
7828feae131SDavid Howells 			p = &(*p)->rb_left;
7836038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
7846038def0SNamhyung Kim 			rb_prev = parent;
7858feae131SDavid Howells 			p = &(*p)->rb_right;
7866038def0SNamhyung Kim 		} else if (vma < pvma)
7878feae131SDavid Howells 			p = &(*p)->rb_left;
7886038def0SNamhyung Kim 		else if (vma > pvma) {
7896038def0SNamhyung Kim 			rb_prev = parent;
7908feae131SDavid Howells 			p = &(*p)->rb_right;
7916038def0SNamhyung Kim 		} else
7928feae131SDavid Howells 			BUG();
7938feae131SDavid Howells 	}
7948feae131SDavid Howells 
7958feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
7968feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
7978feae131SDavid Howells 
7988feae131SDavid Howells 	/* add VMA to the VMA list also */
7996038def0SNamhyung Kim 	prev = NULL;
8006038def0SNamhyung Kim 	if (rb_prev)
8016038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
8023034097aSDavid Howells 
8036038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
8048feae131SDavid Howells }
8058feae131SDavid Howells 
8068feae131SDavid Howells /*
8078feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
8088feae131SDavid Howells  */
8098feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
8108feae131SDavid Howells {
811615d6e87SDavidlohr Bueso 	int i;
8128feae131SDavid Howells 	struct address_space *mapping;
8138feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
814615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
8158feae131SDavid Howells 
8168feae131SDavid Howells 	kenter("%p", vma);
8178feae131SDavid Howells 
818eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
819eb8cdec4SBernd Schmidt 
8208feae131SDavid Howells 	mm->map_count--;
821615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
822615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
823615d6e87SDavidlohr Bueso 		if (curr->vmacache[i] == vma) {
824e020d5bdSSteven Miao 			vmacache_invalidate(mm);
825615d6e87SDavidlohr Bueso 			break;
826615d6e87SDavidlohr Bueso 		}
827615d6e87SDavidlohr Bueso 	}
8288feae131SDavid Howells 
8298feae131SDavid Howells 	/* remove the VMA from the mapping */
8308feae131SDavid Howells 	if (vma->vm_file) {
8318feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
8328feae131SDavid Howells 
83383cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
8348feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
8356b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
8368feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
83783cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
8388feae131SDavid Howells 	}
8398feae131SDavid Howells 
8408feae131SDavid Howells 	/* remove from the MM's tree and list */
8418feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
842b951bf2cSNamhyung Kim 
843b951bf2cSNamhyung Kim 	if (vma->vm_prev)
844b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
845b951bf2cSNamhyung Kim 	else
846b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
847b951bf2cSNamhyung Kim 
848b951bf2cSNamhyung Kim 	if (vma->vm_next)
849b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
8508feae131SDavid Howells }
8518feae131SDavid Howells 
8528feae131SDavid Howells /*
8538feae131SDavid Howells  * destroy a VMA record
8548feae131SDavid Howells  */
8558feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
8568feae131SDavid Howells {
8578feae131SDavid Howells 	kenter("%p", vma);
8588feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
8598feae131SDavid Howells 		vma->vm_ops->close(vma);
860e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
8618feae131SDavid Howells 		fput(vma->vm_file);
8628feae131SDavid Howells 	put_nommu_region(vma->vm_region);
8638feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
8643034097aSDavid Howells }
8653034097aSDavid Howells 
8663034097aSDavid Howells /*
8673034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
8683034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8693034097aSDavid Howells  */
8703034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
8713034097aSDavid Howells {
8728feae131SDavid Howells 	struct vm_area_struct *vma;
8733034097aSDavid Howells 
8748feae131SDavid Howells 	/* check the cache first */
875615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
876615d6e87SDavidlohr Bueso 	if (likely(vma))
8778feae131SDavid Howells 		return vma;
8788feae131SDavid Howells 
879e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8808feae131SDavid Howells 	 * resides) */
881e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8828feae131SDavid Howells 		if (vma->vm_start > addr)
8838feae131SDavid Howells 			return NULL;
8848feae131SDavid Howells 		if (vma->vm_end > addr) {
885615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
8868feae131SDavid Howells 			return vma;
8873034097aSDavid Howells 		}
8888feae131SDavid Howells 	}
8893034097aSDavid Howells 
8903034097aSDavid Howells 	return NULL;
8913034097aSDavid Howells }
8923034097aSDavid Howells EXPORT_SYMBOL(find_vma);
8933034097aSDavid Howells 
8943034097aSDavid Howells /*
895930e652aSDavid Howells  * find a VMA
896930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
897930e652aSDavid Howells  */
898930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
899930e652aSDavid Howells {
9007561e8caSDavid Howells 	return find_vma(mm, addr);
901930e652aSDavid Howells }
902930e652aSDavid Howells 
9038feae131SDavid Howells /*
9048feae131SDavid Howells  * expand a stack to a given address
9058feae131SDavid Howells  * - not supported under NOMMU conditions
9068feae131SDavid Howells  */
90757c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
90857c8f63eSGreg Ungerer {
90957c8f63eSGreg Ungerer 	return -ENOMEM;
91057c8f63eSGreg Ungerer }
91157c8f63eSGreg Ungerer 
912930e652aSDavid Howells /*
9136fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
9146fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
9156fa5f80bSDavid Howells  */
9168feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
9178feae131SDavid Howells 					     unsigned long addr,
9188feae131SDavid Howells 					     unsigned long len)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	struct vm_area_struct *vma;
9218feae131SDavid Howells 	unsigned long end = addr + len;
9221da177e4SLinus Torvalds 
9238feae131SDavid Howells 	/* check the cache first */
924615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
925615d6e87SDavidlohr Bueso 	if (vma)
9261da177e4SLinus Torvalds 		return vma;
9278feae131SDavid Howells 
928e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
9298feae131SDavid Howells 	 * resides) */
930e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
9318feae131SDavid Howells 		if (vma->vm_start < addr)
9328feae131SDavid Howells 			continue;
9338feae131SDavid Howells 		if (vma->vm_start > addr)
9348feae131SDavid Howells 			return NULL;
9358feae131SDavid Howells 		if (vma->vm_end == end) {
936615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
9378feae131SDavid Howells 			return vma;
9388feae131SDavid Howells 		}
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	return NULL;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
9443034097aSDavid Howells /*
9451da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
9461da177e4SLinus Torvalds  * mapping we're capable of supporting
9471da177e4SLinus Torvalds  */
9481da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
9491da177e4SLinus Torvalds 				 unsigned long addr,
9501da177e4SLinus Torvalds 				 unsigned long len,
9511da177e4SLinus Torvalds 				 unsigned long prot,
9521da177e4SLinus Torvalds 				 unsigned long flags,
9531da177e4SLinus Torvalds 				 unsigned long pgoff,
9541da177e4SLinus Torvalds 				 unsigned long *_capabilities)
9551da177e4SLinus Torvalds {
9568feae131SDavid Howells 	unsigned long capabilities, rlen;
9571da177e4SLinus Torvalds 	int ret;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/* do the simple checks first */
96006aab5a3SDavid Howells 	if (flags & MAP_FIXED) {
9611da177e4SLinus Torvalds 		printk(KERN_DEBUG
9621da177e4SLinus Torvalds 		       "%d: Can't do fixed-address/overlay mmap of RAM\n",
9631da177e4SLinus Torvalds 		       current->pid);
9641da177e4SLinus Torvalds 		return -EINVAL;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
9681da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
9691da177e4SLinus Torvalds 		return -EINVAL;
9701da177e4SLinus Torvalds 
971f81cff0dSMike Frysinger 	if (!len)
9721da177e4SLinus Torvalds 		return -EINVAL;
9731da177e4SLinus Torvalds 
974f81cff0dSMike Frysinger 	/* Careful about overflows.. */
9758feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
9768feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
977f81cff0dSMike Frysinger 		return -ENOMEM;
978f81cff0dSMike Frysinger 
9791da177e4SLinus Torvalds 	/* offset overflow? */
9808feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
981f81cff0dSMike Frysinger 		return -EOVERFLOW;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	if (file) {
9841da177e4SLinus Torvalds 		/* files must support mmap */
98572c2d531SAl Viro 		if (!file->f_op->mmap)
9861da177e4SLinus Torvalds 			return -ENODEV;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
9891da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
9901da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
9911da177e4SLinus Torvalds 		 */
992b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
993b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
994b4caecd4SChristoph Hellwig 		} else {
9951da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
9961da177e4SLinus Torvalds 			 * defaults */
997496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
9981da177e4SLinus Torvalds 			case S_IFREG:
9991da177e4SLinus Torvalds 			case S_IFBLK:
1000b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
10011da177e4SLinus Torvalds 				break;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 			case S_IFCHR:
10041da177e4SLinus Torvalds 				capabilities =
1005b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
1006b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
1007b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
10081da177e4SLinus Torvalds 				break;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 			default:
10111da177e4SLinus Torvalds 				return -EINVAL;
10121da177e4SLinus Torvalds 			}
10131da177e4SLinus Torvalds 		}
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
10161da177e4SLinus Torvalds 		 * device */
10171da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
1018b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
1019*6e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
1020b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
10211da177e4SLinus Torvalds 
102228d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
102328d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
102428d7a6aeSGraff Yang 			return -EACCES;
102528d7a6aeSGraff Yang 
10261da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
10271da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
10281da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
10291da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
10301da177e4SLinus Torvalds 				return -EACCES;
10311da177e4SLinus Torvalds 
1032496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
10331da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
10341da177e4SLinus Torvalds 				return -EACCES;
10351da177e4SLinus Torvalds 
1036d7a06983SJeff Layton 			if (locks_verify_locked(file))
10371da177e4SLinus Torvalds 				return -EAGAIN;
10381da177e4SLinus Torvalds 
1039b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
10401da177e4SLinus Torvalds 				return -ENODEV;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
1043b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
1044ac714904SChoi Gi-yong 		} else {
10451da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
10461da177e4SLinus Torvalds 			 * allocate */
1047b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
10481da177e4SLinus Torvalds 				return -ENODEV;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
10511da177e4SLinus Torvalds 			 * shared with the backing device */
10521da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
1053b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
10541da177e4SLinus Torvalds 		}
10551da177e4SLinus Torvalds 
1056b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
1057b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
1058b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
1059b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
10603c7b2045SBernd Schmidt 			    ) {
1061b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
10623c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
10633c7b2045SBernd Schmidt 					printk(KERN_WARNING
10643c7b2045SBernd Schmidt 					       "MAP_SHARED not completely supported on !MMU\n");
10653c7b2045SBernd Schmidt 					return -EINVAL;
10663c7b2045SBernd Schmidt 				}
10673c7b2045SBernd Schmidt 			}
10683c7b2045SBernd Schmidt 		}
10693c7b2045SBernd Schmidt 
10701da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
10711da177e4SLinus Torvalds 		 * mappings */
1072e9536ae7SJosef Sipek 		if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
10731da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
10741da177e4SLinus Torvalds 				return -EPERM;
1075ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
10761da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
10771da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
1078b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
10791da177e4SLinus Torvalds 					prot |= PROT_EXEC;
10801da177e4SLinus Torvalds 			}
1081ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
10821da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
1083b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
10841da177e4SLinus Torvalds 			 ) {
10851da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
1086b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
10871da177e4SLinus Torvalds 		}
1088ac714904SChoi Gi-yong 	} else {
10891da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
10901da177e4SLinus Torvalds 		 * privately mapped
10911da177e4SLinus Torvalds 		 */
1092b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
10951da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
10961da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
10971da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	/* allow the security API to have its say */
1101e5467859SAl Viro 	ret = security_mmap_addr(addr);
1102e5467859SAl Viro 	if (ret < 0)
1103e5467859SAl Viro 		return ret;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	/* looks okay */
11061da177e4SLinus Torvalds 	*_capabilities = capabilities;
11071da177e4SLinus Torvalds 	return 0;
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds /*
11111da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
11121da177e4SLinus Torvalds  * now know into VMA flags
11131da177e4SLinus Torvalds  */
11141da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
11151da177e4SLinus Torvalds 					unsigned long prot,
11161da177e4SLinus Torvalds 					unsigned long flags,
11171da177e4SLinus Torvalds 					unsigned long capabilities)
11181da177e4SLinus Torvalds {
11191da177e4SLinus Torvalds 	unsigned long vm_flags;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
11221da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
11231da177e4SLinus Torvalds 
1124b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
11251da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
11263c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
11271da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
11281da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
11293c7b2045SBernd Schmidt 	} else {
11301da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
11311da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
11321da177e4SLinus Torvalds 		 * romfs/cramfs */
1133b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
11341da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
11353c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
11391da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
11401da177e4SLinus Torvalds 	 * with another untraced process
11411da177e4SLinus Torvalds 	 */
1142a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
11431da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	return vm_flags;
11461da177e4SLinus Torvalds }
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /*
11498feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
11508feae131SDavid Howells  * pins the storage)
11511da177e4SLinus Torvalds  */
11528feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	int ret;
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1157dd8632a1SPaul Mundt 	if (ret == 0) {
1158dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1159645d83c5SDavid Howells 		return 0;
1160dd8632a1SPaul Mundt 	}
11611da177e4SLinus Torvalds 	if (ret != -ENOSYS)
11621da177e4SLinus Torvalds 		return ret;
11631da177e4SLinus Torvalds 
11643fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
11653fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
11663fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
11671da177e4SLinus Torvalds 	return -ENODEV;
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds /*
11711da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
11721da177e4SLinus Torvalds  */
11738feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
11748feae131SDavid Howells 			   struct vm_region *region,
1175645d83c5SDavid Howells 			   unsigned long len,
1176645d83c5SDavid Howells 			   unsigned long capabilities)
11771da177e4SLinus Torvalds {
1178dbc8358cSJoonsoo Kim 	unsigned long total, point;
11791da177e4SLinus Torvalds 	void *base;
11808feae131SDavid Howells 	int ret, order;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
11831da177e4SLinus Torvalds 	 * shared mappings on devices or memory
11841da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
11851da177e4SLinus Torvalds 	 */
1186b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
11871da177e4SLinus Torvalds 		ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1188dd8632a1SPaul Mundt 		if (ret == 0) {
11891da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1190dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1191dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1192645d83c5SDavid Howells 			return 0;
11931da177e4SLinus Torvalds 		}
1194dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1195dd8632a1SPaul Mundt 			return ret;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
11981da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
11991da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 
12028feae131SDavid Howells 
12031da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
12041da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
12051da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
12061da177e4SLinus Torvalds 	 */
1207f67d9b15SBob Liu 	order = get_order(len);
12088feae131SDavid Howells 	kdebug("alloc order %d for %lx", order, len);
12098feae131SDavid Howells 
12108feae131SDavid Howells 	total = 1 << order;
1211f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1212dd8632a1SPaul Mundt 
1213dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
1214dd8632a1SPaul Mundt 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1215dbc8358cSJoonsoo Kim 		total = point;
1216dbc8358cSJoonsoo Kim 		kdebug("try to alloc exact %lu pages", total);
1217dd8632a1SPaul Mundt 	}
12188feae131SDavid Howells 
1219da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1220dbc8358cSJoonsoo Kim 	if (!base)
1221dbc8358cSJoonsoo Kim 		goto enomem;
12228feae131SDavid Howells 
1223dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1224dbc8358cSJoonsoo Kim 
12258feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
12268feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1227f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1228dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
12298feae131SDavid Howells 
12308feae131SDavid Howells 	vma->vm_start = region->vm_start;
12318feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	if (vma->vm_file) {
12341da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
12351da177e4SLinus Torvalds 		mm_segment_t old_fs;
12361da177e4SLinus Torvalds 		loff_t fpos;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
12391da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 		old_fs = get_fs();
12421da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
1243*6e242a1cSAl Viro 		ret = __vfs_read(vma->vm_file, base, len, &fpos);
12441da177e4SLinus Torvalds 		set_fs(old_fs);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 		if (ret < 0)
12471da177e4SLinus Torvalds 			goto error_free;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		/* clear the last little bit */
1250f67d9b15SBob Liu 		if (ret < len)
1251f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	return 0;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds error_free:
12587223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
12598feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
12608feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1261dd8632a1SPaul Mundt 	region->vm_top   = 0;
12621da177e4SLinus Torvalds 	return ret;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds enomem:
1265b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
126605ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
12677bf02ea2SDavid Rientjes 	show_free_areas(0);
12681da177e4SLinus Torvalds 	return -ENOMEM;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds /*
12721da177e4SLinus Torvalds  * handle mapping creation for uClinux
12731da177e4SLinus Torvalds  */
1274e3fc629dSAl Viro unsigned long do_mmap_pgoff(struct file *file,
12751da177e4SLinus Torvalds 			    unsigned long addr,
12761da177e4SLinus Torvalds 			    unsigned long len,
12771da177e4SLinus Torvalds 			    unsigned long prot,
12781da177e4SLinus Torvalds 			    unsigned long flags,
1279bebeb3d6SMichel Lespinasse 			    unsigned long pgoff,
128041badc15SMichel Lespinasse 			    unsigned long *populate)
12811da177e4SLinus Torvalds {
12828feae131SDavid Howells 	struct vm_area_struct *vma;
12838feae131SDavid Howells 	struct vm_region *region;
12841da177e4SLinus Torvalds 	struct rb_node *rb;
12858feae131SDavid Howells 	unsigned long capabilities, vm_flags, result;
12861da177e4SLinus Torvalds 	int ret;
12871da177e4SLinus Torvalds 
12888feae131SDavid Howells 	kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
12898feae131SDavid Howells 
129041badc15SMichel Lespinasse 	*populate = 0;
1291bebeb3d6SMichel Lespinasse 
12921da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
12931da177e4SLinus Torvalds 	 * mapping */
12941da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
12951da177e4SLinus Torvalds 				    &capabilities);
12968feae131SDavid Howells 	if (ret < 0) {
12978feae131SDavid Howells 		kleave(" = %d [val]", ret);
12981da177e4SLinus Torvalds 		return ret;
12998feae131SDavid Howells 	}
13001da177e4SLinus Torvalds 
130106aab5a3SDavid Howells 	/* we ignore the address hint */
130206aab5a3SDavid Howells 	addr = 0;
1303f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
130406aab5a3SDavid Howells 
13051da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
13061da177e4SLinus Torvalds 	 * now know into VMA flags */
13071da177e4SLinus Torvalds 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
13081da177e4SLinus Torvalds 
13098feae131SDavid Howells 	/* we're going to need to record the mapping */
13108feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
13118feae131SDavid Howells 	if (!region)
13128feae131SDavid Howells 		goto error_getting_region;
13131da177e4SLinus Torvalds 
13148feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
13158feae131SDavid Howells 	if (!vma)
13168feae131SDavid Howells 		goto error_getting_vma;
13171da177e4SLinus Torvalds 
13181e2ae599SDavid Howells 	region->vm_usage = 1;
13198feae131SDavid Howells 	region->vm_flags = vm_flags;
13208feae131SDavid Howells 	region->vm_pgoff = pgoff;
13218feae131SDavid Howells 
13225beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
13238feae131SDavid Howells 	vma->vm_flags = vm_flags;
13248feae131SDavid Howells 	vma->vm_pgoff = pgoff;
13258feae131SDavid Howells 
13268feae131SDavid Howells 	if (file) {
1327cb0942b8SAl Viro 		region->vm_file = get_file(file);
1328cb0942b8SAl Viro 		vma->vm_file = get_file(file);
13298feae131SDavid Howells 	}
13308feae131SDavid Howells 
13318feae131SDavid Howells 	down_write(&nommu_region_sem);
13328feae131SDavid Howells 
13338feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
13341da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
13358feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
13361da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
13371da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
13381da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
13391da177e4SLinus Torvalds 	 *   than here
13401da177e4SLinus Torvalds 	 */
13411da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
13428feae131SDavid Howells 		struct vm_region *pregion;
13438feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
13441da177e4SLinus Torvalds 
13458feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
13468feae131SDavid Howells 		pgend = pgoff + pglen;
1347165b2392SDavid Howells 
13488feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
13498feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
13501da177e4SLinus Torvalds 
13518feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
13521da177e4SLinus Torvalds 				continue;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1355496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1356496ad9aaSAl Viro 			    file_inode(file))
13571da177e4SLinus Torvalds 				continue;
13581da177e4SLinus Torvalds 
13598feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
13601da177e4SLinus Torvalds 				continue;
13611da177e4SLinus Torvalds 
13628feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
13638feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
13648feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
13658feae131SDavid Howells 			if (pgoff >= rpgend)
13661da177e4SLinus Torvalds 				continue;
13671da177e4SLinus Torvalds 
13688feae131SDavid Howells 			/* handle inexactly overlapping matches between
13698feae131SDavid Howells 			 * mappings */
13708feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
13718feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
13728feae131SDavid Howells 				/* new mapping is not a subset of the region */
1373b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
13741da177e4SLinus Torvalds 					goto sharing_violation;
13751da177e4SLinus Torvalds 				continue;
13761da177e4SLinus Torvalds 			}
13771da177e4SLinus Torvalds 
13788feae131SDavid Howells 			/* we've found a region we can share */
13791e2ae599SDavid Howells 			pregion->vm_usage++;
13808feae131SDavid Howells 			vma->vm_region = pregion;
13818feae131SDavid Howells 			start = pregion->vm_start;
13828feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
13838feae131SDavid Howells 			vma->vm_start = start;
13848feae131SDavid Howells 			vma->vm_end = start + len;
13851da177e4SLinus Torvalds 
13868feae131SDavid Howells 			if (pregion->vm_flags & VM_MAPPED_COPY) {
13878feae131SDavid Howells 				kdebug("share copy");
13888feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
13898feae131SDavid Howells 			} else {
13908feae131SDavid Howells 				kdebug("share mmap");
13918feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
13928feae131SDavid Howells 				if (ret < 0) {
13938feae131SDavid Howells 					vma->vm_region = NULL;
13948feae131SDavid Howells 					vma->vm_start = 0;
13958feae131SDavid Howells 					vma->vm_end = 0;
13961e2ae599SDavid Howells 					pregion->vm_usage--;
13978feae131SDavid Howells 					pregion = NULL;
13988feae131SDavid Howells 					goto error_just_free;
13991da177e4SLinus Torvalds 				}
14008feae131SDavid Howells 			}
14018feae131SDavid Howells 			fput(region->vm_file);
14028feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
14038feae131SDavid Howells 			region = pregion;
14048feae131SDavid Howells 			result = start;
14058feae131SDavid Howells 			goto share;
14068feae131SDavid Howells 		}
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
14091da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
14101da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
14111da177e4SLinus Torvalds 		 */
1412b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
14131da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
14141da177e4SLinus Torvalds 							     pgoff, flags);
1415bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
14161da177e4SLinus Torvalds 				ret = addr;
1417bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
14188feae131SDavid Howells 					goto error_just_free;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
14211da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
14221da177e4SLinus Torvalds 				 * it */
1423bb005a59SNamhyung Kim 				ret = -ENODEV;
1424b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
14258feae131SDavid Howells 					goto error_just_free;
14261da177e4SLinus Torvalds 
1427b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
14288feae131SDavid Howells 			} else {
14298feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
14308feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
14311da177e4SLinus Torvalds 			}
14321da177e4SLinus Torvalds 		}
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds 
14358feae131SDavid Howells 	vma->vm_region = region;
14361da177e4SLinus Torvalds 
1437645d83c5SDavid Howells 	/* set up the mapping
1438b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1439645d83c5SDavid Howells 	 */
14401da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
14418feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
14421da177e4SLinus Torvalds 	else
1443645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
14441da177e4SLinus Torvalds 	if (ret < 0)
1445645d83c5SDavid Howells 		goto error_just_free;
1446645d83c5SDavid Howells 	add_nommu_region(region);
14478feae131SDavid Howells 
1448ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1449ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1450ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1451ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1452ea637639SJie Zhang 
14531da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
14548feae131SDavid Howells 	result = vma->vm_start;
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
14571da177e4SLinus Torvalds 
14588feae131SDavid Howells share:
14598feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
14601da177e4SLinus Torvalds 
1461cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1462cfe79c00SMike Frysinger 	 * mapping of it is made  */
1463cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1464cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1465cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1466cfe79c00SMike Frysinger 	}
14671da177e4SLinus Torvalds 
1468cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
14691da177e4SLinus Torvalds 
14708feae131SDavid Howells 	kleave(" = %lx", result);
14718feae131SDavid Howells 	return result;
14721da177e4SLinus Torvalds 
14738feae131SDavid Howells error_just_free:
14748feae131SDavid Howells 	up_write(&nommu_region_sem);
14758feae131SDavid Howells error:
147689a86402SDavid Howells 	if (region->vm_file)
14778feae131SDavid Howells 		fput(region->vm_file);
14788feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
147989a86402SDavid Howells 	if (vma->vm_file)
14808feae131SDavid Howells 		fput(vma->vm_file);
14818feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
14828feae131SDavid Howells 	kleave(" = %d", ret);
14831da177e4SLinus Torvalds 	return ret;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds sharing_violation:
14868feae131SDavid Howells 	up_write(&nommu_region_sem);
14878feae131SDavid Howells 	printk(KERN_WARNING "Attempt to share mismatched mappings\n");
14888feae131SDavid Howells 	ret = -EINVAL;
14898feae131SDavid Howells 	goto error;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds error_getting_vma:
14928feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
14938feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
14948feae131SDavid Howells 	       " from process %d failed\n",
14951da177e4SLinus Torvalds 	       len, current->pid);
14967bf02ea2SDavid Rientjes 	show_free_areas(0);
14971da177e4SLinus Torvalds 	return -ENOMEM;
14981da177e4SLinus Torvalds 
14998feae131SDavid Howells error_getting_region:
15008feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
15018feae131SDavid Howells 	       " from process %d failed\n",
15021da177e4SLinus Torvalds 	       len, current->pid);
15037bf02ea2SDavid Rientjes 	show_free_areas(0);
15041da177e4SLinus Torvalds 	return -ENOMEM;
15051da177e4SLinus Torvalds }
15066be5ceb0SLinus Torvalds 
150766f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
150866f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
150966f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
151066f0dc48SHugh Dickins {
151166f0dc48SHugh Dickins 	struct file *file = NULL;
151266f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
151366f0dc48SHugh Dickins 
1514120a795dSAl Viro 	audit_mmap_fd(fd, flags);
151566f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
151666f0dc48SHugh Dickins 		file = fget(fd);
151766f0dc48SHugh Dickins 		if (!file)
151866f0dc48SHugh Dickins 			goto out;
151966f0dc48SHugh Dickins 	}
152066f0dc48SHugh Dickins 
152166f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
152266f0dc48SHugh Dickins 
1523ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
152466f0dc48SHugh Dickins 
152566f0dc48SHugh Dickins 	if (file)
152666f0dc48SHugh Dickins 		fput(file);
152766f0dc48SHugh Dickins out:
152866f0dc48SHugh Dickins 	return retval;
152966f0dc48SHugh Dickins }
153066f0dc48SHugh Dickins 
1531a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1532a4679373SChristoph Hellwig struct mmap_arg_struct {
1533a4679373SChristoph Hellwig 	unsigned long addr;
1534a4679373SChristoph Hellwig 	unsigned long len;
1535a4679373SChristoph Hellwig 	unsigned long prot;
1536a4679373SChristoph Hellwig 	unsigned long flags;
1537a4679373SChristoph Hellwig 	unsigned long fd;
1538a4679373SChristoph Hellwig 	unsigned long offset;
1539a4679373SChristoph Hellwig };
1540a4679373SChristoph Hellwig 
1541a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1542a4679373SChristoph Hellwig {
1543a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1544a4679373SChristoph Hellwig 
1545a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1546a4679373SChristoph Hellwig 		return -EFAULT;
1547a4679373SChristoph Hellwig 	if (a.offset & ~PAGE_MASK)
1548a4679373SChristoph Hellwig 		return -EINVAL;
1549a4679373SChristoph Hellwig 
1550a4679373SChristoph Hellwig 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1551a4679373SChristoph Hellwig 			      a.offset >> PAGE_SHIFT);
1552a4679373SChristoph Hellwig }
1553a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1554a4679373SChristoph Hellwig 
15551da177e4SLinus Torvalds /*
15568feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
15578feae131SDavid Howells  * for the first part or the tail.
15581da177e4SLinus Torvalds  */
15598feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
15608feae131SDavid Howells 	      unsigned long addr, int new_below)
15611da177e4SLinus Torvalds {
15628feae131SDavid Howells 	struct vm_area_struct *new;
15638feae131SDavid Howells 	struct vm_region *region;
15648feae131SDavid Howells 	unsigned long npages;
15651da177e4SLinus Torvalds 
15668feae131SDavid Howells 	kenter("");
15671da177e4SLinus Torvalds 
1568779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1569779c1023SDavid Howells 	 * only a single usage on the region) */
1570779c1023SDavid Howells 	if (vma->vm_file)
15718feae131SDavid Howells 		return -ENOMEM;
15721da177e4SLinus Torvalds 
15738feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
15748feae131SDavid Howells 		return -ENOMEM;
15751da177e4SLinus Torvalds 
15768feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
15778feae131SDavid Howells 	if (!region)
15788feae131SDavid Howells 		return -ENOMEM;
15798feae131SDavid Howells 
15808feae131SDavid Howells 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
15818feae131SDavid Howells 	if (!new) {
15828feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
15838feae131SDavid Howells 		return -ENOMEM;
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds 
15868feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
15878feae131SDavid Howells 	*new = *vma;
15888feae131SDavid Howells 	*region = *vma->vm_region;
15898feae131SDavid Howells 	new->vm_region = region;
15908feae131SDavid Howells 
15918feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
15928feae131SDavid Howells 
15938feae131SDavid Howells 	if (new_below) {
1594dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
15958feae131SDavid Howells 	} else {
15968feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
15978feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
15981da177e4SLinus Torvalds 	}
15998feae131SDavid Howells 
16008feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
16018feae131SDavid Howells 		new->vm_ops->open(new);
16028feae131SDavid Howells 
16038feae131SDavid Howells 	delete_vma_from_mm(vma);
16048feae131SDavid Howells 	down_write(&nommu_region_sem);
16058feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
16068feae131SDavid Howells 	if (new_below) {
16078feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
16088feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
16098feae131SDavid Howells 	} else {
16108feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1611dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
16128feae131SDavid Howells 	}
16138feae131SDavid Howells 	add_nommu_region(vma->vm_region);
16148feae131SDavid Howells 	add_nommu_region(new->vm_region);
16158feae131SDavid Howells 	up_write(&nommu_region_sem);
16168feae131SDavid Howells 	add_vma_to_mm(mm, vma);
16178feae131SDavid Howells 	add_vma_to_mm(mm, new);
16188feae131SDavid Howells 	return 0;
16198feae131SDavid Howells }
16208feae131SDavid Howells 
16218feae131SDavid Howells /*
16228feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
16238feae131SDavid Howells  * the end
16248feae131SDavid Howells  */
16258feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
16268feae131SDavid Howells 		      struct vm_area_struct *vma,
16278feae131SDavid Howells 		      unsigned long from, unsigned long to)
16288feae131SDavid Howells {
16298feae131SDavid Howells 	struct vm_region *region;
16308feae131SDavid Howells 
16318feae131SDavid Howells 	kenter("");
16328feae131SDavid Howells 
16338feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
16348feae131SDavid Howells 	 * and list */
16358feae131SDavid Howells 	delete_vma_from_mm(vma);
16368feae131SDavid Howells 	if (from > vma->vm_start)
16378feae131SDavid Howells 		vma->vm_end = from;
16388feae131SDavid Howells 	else
16398feae131SDavid Howells 		vma->vm_start = to;
16408feae131SDavid Howells 	add_vma_to_mm(mm, vma);
16418feae131SDavid Howells 
16428feae131SDavid Howells 	/* cut the backing region down to size */
16438feae131SDavid Howells 	region = vma->vm_region;
16441e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
16458feae131SDavid Howells 
16468feae131SDavid Howells 	down_write(&nommu_region_sem);
16478feae131SDavid Howells 	delete_nommu_region(region);
1648dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1649dd8632a1SPaul Mundt 		to = region->vm_top;
1650dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1651dd8632a1SPaul Mundt 	} else {
16528feae131SDavid Howells 		region->vm_start = to;
1653dd8632a1SPaul Mundt 	}
16548feae131SDavid Howells 	add_nommu_region(region);
16558feae131SDavid Howells 	up_write(&nommu_region_sem);
16568feae131SDavid Howells 
16578feae131SDavid Howells 	free_page_series(from, to);
16588feae131SDavid Howells 	return 0;
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
16613034097aSDavid Howells /*
16623034097aSDavid Howells  * release a mapping
16638feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
16648feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
16653034097aSDavid Howells  */
16668feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
16671da177e4SLinus Torvalds {
16688feae131SDavid Howells 	struct vm_area_struct *vma;
1669f67d9b15SBob Liu 	unsigned long end;
16708feae131SDavid Howells 	int ret;
16711da177e4SLinus Torvalds 
16728feae131SDavid Howells 	kenter(",%lx,%zx", start, len);
16731da177e4SLinus Torvalds 
1674f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
16758feae131SDavid Howells 	if (len == 0)
16761da177e4SLinus Torvalds 		return -EINVAL;
16771da177e4SLinus Torvalds 
1678f67d9b15SBob Liu 	end = start + len;
1679f67d9b15SBob Liu 
16808feae131SDavid Howells 	/* find the first potentially overlapping VMA */
16818feae131SDavid Howells 	vma = find_vma(mm, start);
16828feae131SDavid Howells 	if (!vma) {
1683ac714904SChoi Gi-yong 		static int limit;
168433e5d769SDavid Howells 		if (limit < 5) {
16858feae131SDavid Howells 			printk(KERN_WARNING
168633e5d769SDavid Howells 			       "munmap of memory not mmapped by process %d"
168733e5d769SDavid Howells 			       " (%s): 0x%lx-0x%lx\n",
168833e5d769SDavid Howells 			       current->pid, current->comm,
168933e5d769SDavid Howells 			       start, start + len - 1);
169033e5d769SDavid Howells 			limit++;
169133e5d769SDavid Howells 		}
16928feae131SDavid Howells 		return -EINVAL;
16938feae131SDavid Howells 	}
16941da177e4SLinus Torvalds 
16958feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
16968feae131SDavid Howells 	if (vma->vm_file) {
16978feae131SDavid Howells 		do {
16988feae131SDavid Howells 			if (start > vma->vm_start) {
16998feae131SDavid Howells 				kleave(" = -EINVAL [miss]");
17008feae131SDavid Howells 				return -EINVAL;
17018feae131SDavid Howells 			}
17028feae131SDavid Howells 			if (end == vma->vm_end)
17038feae131SDavid Howells 				goto erase_whole_vma;
1704d75a310cSNamhyung Kim 			vma = vma->vm_next;
1705d75a310cSNamhyung Kim 		} while (vma);
17068feae131SDavid Howells 		kleave(" = -EINVAL [split file]");
17078feae131SDavid Howells 		return -EINVAL;
17088feae131SDavid Howells 	} else {
17098feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
17108feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
17118feae131SDavid Howells 			goto erase_whole_vma;
17128feae131SDavid Howells 		if (start < vma->vm_start || end > vma->vm_end) {
17138feae131SDavid Howells 			kleave(" = -EINVAL [superset]");
17148feae131SDavid Howells 			return -EINVAL;
17158feae131SDavid Howells 		}
17168feae131SDavid Howells 		if (start & ~PAGE_MASK) {
17178feae131SDavid Howells 			kleave(" = -EINVAL [unaligned start]");
17188feae131SDavid Howells 			return -EINVAL;
17198feae131SDavid Howells 		}
17208feae131SDavid Howells 		if (end != vma->vm_end && end & ~PAGE_MASK) {
17218feae131SDavid Howells 			kleave(" = -EINVAL [unaligned split]");
17228feae131SDavid Howells 			return -EINVAL;
17238feae131SDavid Howells 		}
17248feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
17258feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
17268feae131SDavid Howells 			if (ret < 0) {
17278feae131SDavid Howells 				kleave(" = %d [split]", ret);
17288feae131SDavid Howells 				return ret;
17298feae131SDavid Howells 			}
17308feae131SDavid Howells 		}
17318feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
17328feae131SDavid Howells 	}
17331da177e4SLinus Torvalds 
17348feae131SDavid Howells erase_whole_vma:
17358feae131SDavid Howells 	delete_vma_from_mm(vma);
17368feae131SDavid Howells 	delete_vma(mm, vma);
17378feae131SDavid Howells 	kleave(" = 0");
17381da177e4SLinus Torvalds 	return 0;
17391da177e4SLinus Torvalds }
1740b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
17411da177e4SLinus Torvalds 
1742bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
17433034097aSDavid Howells {
1744bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
17453034097aSDavid Howells 	int ret;
17463034097aSDavid Howells 
17473034097aSDavid Howells 	down_write(&mm->mmap_sem);
17483034097aSDavid Howells 	ret = do_munmap(mm, addr, len);
17493034097aSDavid Howells 	up_write(&mm->mmap_sem);
17503034097aSDavid Howells 	return ret;
17513034097aSDavid Howells }
1752a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1753a46ef99dSLinus Torvalds 
1754a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1755a46ef99dSLinus Torvalds {
1756bfce281cSAl Viro 	return vm_munmap(addr, len);
1757a46ef99dSLinus Torvalds }
17583034097aSDavid Howells 
17593034097aSDavid Howells /*
17608feae131SDavid Howells  * release all the mappings made in a process's VM space
17613034097aSDavid Howells  */
17621da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
17631da177e4SLinus Torvalds {
17648feae131SDavid Howells 	struct vm_area_struct *vma;
17651da177e4SLinus Torvalds 
17668feae131SDavid Howells 	if (!mm)
17678feae131SDavid Howells 		return;
17688feae131SDavid Howells 
17698feae131SDavid Howells 	kenter("");
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	mm->total_vm = 0;
17721da177e4SLinus Torvalds 
17738feae131SDavid Howells 	while ((vma = mm->mmap)) {
17748feae131SDavid Howells 		mm->mmap = vma->vm_next;
17758feae131SDavid Howells 		delete_vma_from_mm(vma);
17768feae131SDavid Howells 		delete_vma(mm, vma);
177704c34961SSteven J. Magnani 		cond_resched();
17781da177e4SLinus Torvalds 	}
17791da177e4SLinus Torvalds 
17808feae131SDavid Howells 	kleave("");
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
1783e4eb1ff6SLinus Torvalds unsigned long vm_brk(unsigned long addr, unsigned long len)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	return -ENOMEM;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds /*
17896fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
17906fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
17911da177e4SLinus Torvalds  *
17926fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
17938feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
17948feae131SDavid Howells  * block is not shareable
17951da177e4SLinus Torvalds  *
17966fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
17971da177e4SLinus Torvalds  */
17984b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
17991da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
18001da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
18011da177e4SLinus Torvalds {
18026fa5f80bSDavid Howells 	struct vm_area_struct *vma;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 	/* insanity checks first */
1805f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1806f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
18078feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
18081da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
18091da177e4SLinus Torvalds 
18108feae131SDavid Howells 	if (addr & ~PAGE_MASK)
18118feae131SDavid Howells 		return -EINVAL;
18128feae131SDavid Howells 
18131da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
18141da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
18151da177e4SLinus Torvalds 
18168feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
18176fa5f80bSDavid Howells 	if (!vma)
18181da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
18191da177e4SLinus Torvalds 
18206fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
18211da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
18221da177e4SLinus Torvalds 
18236fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
18241da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
18251da177e4SLinus Torvalds 
18268feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
18271da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	/* all checks complete - do it */
18306fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
18316fa5f80bSDavid Howells 	return vma->vm_start;
18326fa5f80bSDavid Howells }
18336fa5f80bSDavid Howells 
18346a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
18356a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
18366a6160a7SHeiko Carstens 		unsigned long, new_addr)
18376fa5f80bSDavid Howells {
18386fa5f80bSDavid Howells 	unsigned long ret;
18396fa5f80bSDavid Howells 
18406fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
18416fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
18426fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
18436fa5f80bSDavid Howells 	return ret;
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds 
1846240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma,
1847240aadeeSMichel Lespinasse 			      unsigned long address, unsigned int flags,
1848240aadeeSMichel Lespinasse 			      unsigned int *page_mask)
18491da177e4SLinus Torvalds {
1850240aadeeSMichel Lespinasse 	*page_mask = 0;
18511da177e4SLinus Torvalds 	return NULL;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
18548f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
18558f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
18561da177e4SLinus Torvalds {
18578f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
18588f3b1327SBob Liu 		return -EINVAL;
18598f3b1327SBob Liu 
1860314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
186166aa2b4bSGreg Ungerer 	return 0;
18621da177e4SLinus Torvalds }
186322c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
18641da177e4SLinus Torvalds 
18653c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
18663c0b9de6SLinus Torvalds {
18673c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
18683c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
18693c0b9de6SLinus Torvalds 
18703c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
18713c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
18723c0b9de6SLinus Torvalds }
18733c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
18743c0b9de6SLinus Torvalds 
1875f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1876f905bc44SPaul Mundt 			unsigned long pgoff)
1877f905bc44SPaul Mundt {
1878f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1879f905bc44SPaul Mundt 
1880f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1881f905bc44SPaul Mundt 		return -EINVAL;
1882f905bc44SPaul Mundt 
1883f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1884f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1885f905bc44SPaul Mundt 
1886f905bc44SPaul Mundt 	return 0;
1887f905bc44SPaul Mundt }
1888f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1889f905bc44SPaul Mundt 
18901da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
18911da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	return -ENOMEM;
18941da177e4SLinus Torvalds }
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
18971da177e4SLinus Torvalds 			 loff_t const holebegin, loff_t const holelen,
18981da177e4SLinus Torvalds 			 int even_cows)
18991da177e4SLinus Torvalds {
19001da177e4SLinus Torvalds }
190122c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range);
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds /*
19041da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
19051da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
19061da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
19071da177e4SLinus Torvalds  *
19081da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
19091da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
19101da177e4SLinus Torvalds  *
19111da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
19121da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
19131da177e4SLinus Torvalds  *
19141da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
19151da177e4SLinus Torvalds  *
19161da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
19171da177e4SLinus Torvalds  * wish to use this logic.
19181da177e4SLinus Torvalds  */
191934b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
19201da177e4SLinus Torvalds {
19218138a67aSRoman Gushchin 	long free, allowed, reserve;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	vm_acct_memory(pages);
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	/*
19261da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
19271da177e4SLinus Torvalds 	 */
19281da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
19291da177e4SLinus Torvalds 		return 0;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1932c15bef30SDmitry Fink 		free = global_page_state(NR_FREE_PAGES);
1933c15bef30SDmitry Fink 		free += global_page_state(NR_FILE_PAGES);
19341da177e4SLinus Torvalds 
1935c15bef30SDmitry Fink 		/*
1936c15bef30SDmitry Fink 		 * shmem pages shouldn't be counted as free in this
1937c15bef30SDmitry Fink 		 * case, they can't be purged, only swapped out, and
1938c15bef30SDmitry Fink 		 * that won't affect the overall amount of available
1939c15bef30SDmitry Fink 		 * memory in the system.
1940c15bef30SDmitry Fink 		 */
1941c15bef30SDmitry Fink 		free -= global_page_state(NR_SHMEM);
1942c15bef30SDmitry Fink 
1943ec8acf20SShaohua Li 		free += get_nr_swap_pages();
19441da177e4SLinus Torvalds 
19451da177e4SLinus Torvalds 		/*
19461da177e4SLinus Torvalds 		 * Any slabs which are created with the
19471da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
19481da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
19491da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
19501da177e4SLinus Torvalds 		 */
1951972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 		/*
1954c15bef30SDmitry Fink 		 * Leave reserved pages. The pages are not for anonymous pages.
1955c15bef30SDmitry Fink 		 */
1956c15bef30SDmitry Fink 		if (free <= totalreserve_pages)
1957c15bef30SDmitry Fink 			goto error;
1958c15bef30SDmitry Fink 		else
1959c15bef30SDmitry Fink 			free -= totalreserve_pages;
1960c15bef30SDmitry Fink 
1961c15bef30SDmitry Fink 		/*
19624eeab4f5SAndrew Shewmaker 		 * Reserve some for root
19631da177e4SLinus Torvalds 		 */
19641da177e4SLinus Torvalds 		if (!cap_sys_admin)
19654eeab4f5SAndrew Shewmaker 			free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 		if (free > pages)
19681da177e4SLinus Torvalds 			return 0;
19691da177e4SLinus Torvalds 
1970d5ddc79bSHideo AOKI 		goto error;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds 
197300619bccSJerome Marchand 	allowed = vm_commit_limit();
19741da177e4SLinus Torvalds 	/*
19754eeab4f5SAndrew Shewmaker 	 * Reserve some 3% for root
19761da177e4SLinus Torvalds 	 */
19771da177e4SLinus Torvalds 	if (!cap_sys_admin)
19784eeab4f5SAndrew Shewmaker 		allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
19791da177e4SLinus Torvalds 
1980c9b1d098SAndrew Shewmaker 	/*
1981c9b1d098SAndrew Shewmaker 	 * Don't let a single process grow so big a user can't recover
1982c9b1d098SAndrew Shewmaker 	 */
1983c9b1d098SAndrew Shewmaker 	if (mm) {
1984c9b1d098SAndrew Shewmaker 		reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
19858138a67aSRoman Gushchin 		allowed -= min_t(long, mm->total_vm / 32, reserve);
1986c9b1d098SAndrew Shewmaker 	}
19871da177e4SLinus Torvalds 
198800a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
19891da177e4SLinus Torvalds 		return 0;
199000a62ce9SKOSAKI Motohiro 
1991d5ddc79bSHideo AOKI error:
19921da177e4SLinus Torvalds 	vm_unacct_memory(pages);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	return -ENOMEM;
19951da177e4SLinus Torvalds }
19961da177e4SLinus Torvalds 
1997d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1998b0e15190SDavid Howells {
1999b0e15190SDavid Howells 	BUG();
2000d0217ac0SNick Piggin 	return 0;
2001b0e15190SDavid Howells }
2002b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
20030ec76a11SDavid Howells 
2004f1820361SKirill A. Shutemov void filemap_map_pages(struct vm_area_struct *vma, struct vm_fault *vmf)
2005f1820361SKirill A. Shutemov {
2006f1820361SKirill A. Shutemov 	BUG();
2007f1820361SKirill A. Shutemov }
2008f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
2009f1820361SKirill A. Shutemov 
2010f55f199bSMike Frysinger static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2011f55f199bSMike Frysinger 		unsigned long addr, void *buf, int len, int write)
20120ec76a11SDavid Howells {
20130ec76a11SDavid Howells 	struct vm_area_struct *vma;
20140ec76a11SDavid Howells 
20150ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
20160ec76a11SDavid Howells 
20170ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
20180159b141SDavid Howells 	vma = find_vma(mm, addr);
20190159b141SDavid Howells 	if (vma) {
20200ec76a11SDavid Howells 		/* don't overrun this mapping */
20210ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
20220ec76a11SDavid Howells 			len = vma->vm_end - addr;
20230ec76a11SDavid Howells 
20240ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
2025d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
20267959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
20277959722bSJie Zhang 					 (void *) addr, buf, len);
2028d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
20297959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
20307959722bSJie Zhang 					    buf, (void *) addr, len);
20310ec76a11SDavid Howells 		else
20320ec76a11SDavid Howells 			len = 0;
20330ec76a11SDavid Howells 	} else {
20340ec76a11SDavid Howells 		len = 0;
20350ec76a11SDavid Howells 	}
20360ec76a11SDavid Howells 
20370ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
2038f55f199bSMike Frysinger 
2039f55f199bSMike Frysinger 	return len;
2040f55f199bSMike Frysinger }
2041f55f199bSMike Frysinger 
2042f55f199bSMike Frysinger /**
2043f55f199bSMike Frysinger  * @access_remote_vm - access another process' address space
2044f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
2045f55f199bSMike Frysinger  * @addr:	start address to access
2046f55f199bSMike Frysinger  * @buf:	source or destination buffer
2047f55f199bSMike Frysinger  * @len:	number of bytes to transfer
2048f55f199bSMike Frysinger  * @write:	whether the access is a write
2049f55f199bSMike Frysinger  *
2050f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
2051f55f199bSMike Frysinger  */
2052f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2053f55f199bSMike Frysinger 		void *buf, int len, int write)
2054f55f199bSMike Frysinger {
2055f55f199bSMike Frysinger 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
2056f55f199bSMike Frysinger }
2057f55f199bSMike Frysinger 
2058f55f199bSMike Frysinger /*
2059f55f199bSMike Frysinger  * Access another process' address space.
2060f55f199bSMike Frysinger  * - source/target buffer must be kernel space
2061f55f199bSMike Frysinger  */
2062f55f199bSMike Frysinger int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2063f55f199bSMike Frysinger {
2064f55f199bSMike Frysinger 	struct mm_struct *mm;
2065f55f199bSMike Frysinger 
2066f55f199bSMike Frysinger 	if (addr + len < addr)
2067f55f199bSMike Frysinger 		return 0;
2068f55f199bSMike Frysinger 
2069f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
2070f55f199bSMike Frysinger 	if (!mm)
2071f55f199bSMike Frysinger 		return 0;
2072f55f199bSMike Frysinger 
2073f55f199bSMike Frysinger 	len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2074f55f199bSMike Frysinger 
20750ec76a11SDavid Howells 	mmput(mm);
20760ec76a11SDavid Howells 	return len;
20770ec76a11SDavid Howells }
20787e660872SDavid Howells 
20797e660872SDavid Howells /**
20807e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
20817e660872SDavid Howells  * @inode: The inode to check
20827e660872SDavid Howells  * @size: The current filesize of the inode
20837e660872SDavid Howells  * @newsize: The proposed filesize of the inode
20847e660872SDavid Howells  *
20857e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
20867e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
20877e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
20887e660872SDavid Howells  * automatically grant mappings that are too large.
20897e660872SDavid Howells  */
20907e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
20917e660872SDavid Howells 				size_t newsize)
20927e660872SDavid Howells {
20937e660872SDavid Howells 	struct vm_area_struct *vma;
20947e660872SDavid Howells 	struct vm_region *region;
20957e660872SDavid Howells 	pgoff_t low, high;
20967e660872SDavid Howells 	size_t r_size, r_top;
20977e660872SDavid Howells 
20987e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
20997e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
21007e660872SDavid Howells 
21017e660872SDavid Howells 	down_write(&nommu_region_sem);
21021acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
21037e660872SDavid Howells 
21047e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
21056b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
21067e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
21077e660872SDavid Howells 		 * cache */
21087e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
21091acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
21107e660872SDavid Howells 			up_write(&nommu_region_sem);
21117e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
21127e660872SDavid Howells 		}
21137e660872SDavid Howells 	}
21147e660872SDavid Howells 
21157e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
21167e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
21177e660872SDavid Howells 	 *
21187e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
21197e660872SDavid Howells 	 * shouldn't be any
21207e660872SDavid Howells 	 */
21211acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
21227e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
21237e660872SDavid Howells 			continue;
21247e660872SDavid Howells 
21257e660872SDavid Howells 		region = vma->vm_region;
21267e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
21277e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
21287e660872SDavid Howells 
21297e660872SDavid Howells 		if (r_top > newsize) {
21307e660872SDavid Howells 			region->vm_top -= r_top - newsize;
21317e660872SDavid Howells 			if (region->vm_end > region->vm_top)
21327e660872SDavid Howells 				region->vm_end = region->vm_top;
21337e660872SDavid Howells 		}
21347e660872SDavid Howells 	}
21357e660872SDavid Howells 
21361acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
21377e660872SDavid Howells 	up_write(&nommu_region_sem);
21387e660872SDavid Howells 	return 0;
21397e660872SDavid Howells }
2140c9b1d098SAndrew Shewmaker 
2141c9b1d098SAndrew Shewmaker /*
2142c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
2143c9b1d098SAndrew Shewmaker  *
2144c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
2145c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2146c9b1d098SAndrew Shewmaker  * mode.
2147c9b1d098SAndrew Shewmaker  *
2148c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
2149c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
2150c9b1d098SAndrew Shewmaker  */
2151c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
2152c9b1d098SAndrew Shewmaker {
2153c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
2154c9b1d098SAndrew Shewmaker 
2155c9b1d098SAndrew Shewmaker 	free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2156c9b1d098SAndrew Shewmaker 
2157c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2158c9b1d098SAndrew Shewmaker 	return 0;
2159c9b1d098SAndrew Shewmaker }
2160c9b1d098SAndrew Shewmaker module_init(init_user_reserve)
21614eeab4f5SAndrew Shewmaker 
21624eeab4f5SAndrew Shewmaker /*
21634eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
21644eeab4f5SAndrew Shewmaker  *
21654eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
21664eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
21674eeab4f5SAndrew Shewmaker  *
21684eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
21694eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
21704eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
21714eeab4f5SAndrew Shewmaker  */
21724eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
21734eeab4f5SAndrew Shewmaker {
21744eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
21754eeab4f5SAndrew Shewmaker 
21764eeab4f5SAndrew Shewmaker 	free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
21774eeab4f5SAndrew Shewmaker 
21784eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
21794eeab4f5SAndrew Shewmaker 	return 0;
21804eeab4f5SAndrew Shewmaker }
21814eeab4f5SAndrew Shewmaker module_init(init_admin_reserve)
2182