xref: /linux/mm/nommu.c (revision 4eeab4f5580d11bffedc697684b91b0bca0d5009)
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 
16b95f1b31SPaul Gortmaker #include <linux/export.h>
171da177e4SLinus Torvalds #include <linux/mm.h>
181da177e4SLinus Torvalds #include <linux/mman.h>
191da177e4SLinus Torvalds #include <linux/swap.h>
201da177e4SLinus Torvalds #include <linux/file.h>
211da177e4SLinus Torvalds #include <linux/highmem.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
231da177e4SLinus Torvalds #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/vmalloc.h>
251da177e4SLinus Torvalds #include <linux/blkdev.h>
261da177e4SLinus Torvalds #include <linux/backing-dev.h>
271da177e4SLinus Torvalds #include <linux/mount.h>
281da177e4SLinus Torvalds #include <linux/personality.h>
291da177e4SLinus Torvalds #include <linux/security.h>
301da177e4SLinus Torvalds #include <linux/syscalls.h>
31120a795dSAl Viro #include <linux/audit.h>
32cf4aebc2SClark Williams #include <linux/sched/sysctl.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds #include <asm/tlb.h>
361da177e4SLinus Torvalds #include <asm/tlbflush.h>
37eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
388feae131SDavid Howells #include "internal.h"
398feae131SDavid Howells 
408feae131SDavid Howells #if 0
418feae131SDavid Howells #define kenter(FMT, ...) \
428feae131SDavid Howells 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
438feae131SDavid Howells #define kleave(FMT, ...) \
448feae131SDavid Howells 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
458feae131SDavid Howells #define kdebug(FMT, ...) \
468feae131SDavid Howells 	printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
478feae131SDavid Howells #else
488feae131SDavid Howells #define kenter(FMT, ...) \
498feae131SDavid Howells 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
508feae131SDavid Howells #define kleave(FMT, ...) \
518feae131SDavid Howells 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
528feae131SDavid Howells #define kdebug(FMT, ...) \
538feae131SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
548feae131SDavid Howells #endif
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds void *high_memory;
571da177e4SLinus Torvalds struct page *mem_map;
581da177e4SLinus Torvalds unsigned long max_mapnr;
591da177e4SLinus Torvalds unsigned long num_physpages;
604266c97aSHugh Dickins unsigned long highest_memmap_pfn;
6100a62ce9SKOSAKI Motohiro struct percpu_counter vm_committed_as;
621da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
631da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */
641da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
65fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
66c9b1d098SAndrew Shewmaker unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
67*4eeab4f5SAndrew Shewmaker unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
681da177e4SLinus Torvalds int heap_stack_gap = 0;
691da177e4SLinus Torvalds 
7033e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
718feae131SDavid Howells 
72997071bcSK. Y. Srinivasan /*
73997071bcSK. Y. Srinivasan  * The global memory commitment made in the system can be a metric
74997071bcSK. Y. Srinivasan  * that can be used to drive ballooning decisions when Linux is hosted
75997071bcSK. Y. Srinivasan  * as a guest. On Hyper-V, the host implements a policy engine for dynamically
76997071bcSK. Y. Srinivasan  * balancing memory across competing virtual machines that are hosted.
77997071bcSK. Y. Srinivasan  * Several metrics drive this policy engine including the guest reported
78997071bcSK. Y. Srinivasan  * memory commitment.
79997071bcSK. Y. Srinivasan  */
80997071bcSK. Y. Srinivasan unsigned long vm_memory_committed(void)
81997071bcSK. Y. Srinivasan {
82997071bcSK. Y. Srinivasan 	return percpu_counter_read_positive(&vm_committed_as);
83997071bcSK. Y. Srinivasan }
84997071bcSK. Y. Srinivasan 
85997071bcSK. Y. Srinivasan EXPORT_SYMBOL_GPL(vm_memory_committed);
86997071bcSK. Y. Srinivasan 
871da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
886a04de6dSWu, Bryan EXPORT_SYMBOL(num_physpages);
891da177e4SLinus Torvalds 
908feae131SDavid Howells /* list of mapped, potentially shareable regions */
918feae131SDavid Howells static struct kmem_cache *vm_region_jar;
928feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
938feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
941da177e4SLinus Torvalds 
95f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
1001da177e4SLinus Torvalds  * just what the caller asked for.
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	struct page *page;
1071da177e4SLinus Torvalds 
1084016a139SMichael Hennerich 	/*
1094016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
1104016a139SMichael Hennerich 	 * return size of 0
1114016a139SMichael Hennerich 	 */
1125a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
1136cfd53fcSPaul Mundt 		return 0;
1146cfd53fcSPaul Mundt 
1156cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
1166cfd53fcSPaul Mundt 
1176cfd53fcSPaul Mundt 	/*
1186cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
1196cfd53fcSPaul Mundt 	 * kmalloc().
1206cfd53fcSPaul Mundt 	 */
1211da177e4SLinus Torvalds 	if (PageSlab(page))
1221da177e4SLinus Torvalds 		return ksize(objp);
1231da177e4SLinus Torvalds 
1246cfd53fcSPaul Mundt 	/*
125ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
126ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
127ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
128ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
129ab2e83eaSPaul Mundt 	 */
130ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
131ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
132ab2e83eaSPaul Mundt 
133ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
134ab2e83eaSPaul Mundt 		if (vma)
135ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
136ab2e83eaSPaul Mundt 	}
137ab2e83eaSPaul Mundt 
138ab2e83eaSPaul Mundt 	/*
1396cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1405a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1416cfd53fcSPaul Mundt 	 */
1425a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(page);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
14528a35716SMichel Lespinasse long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
14628a35716SMichel Lespinasse 		      unsigned long start, unsigned long nr_pages,
14728a35716SMichel Lespinasse 		      unsigned int foll_flags, struct page **pages,
14828a35716SMichel Lespinasse 		      struct vm_area_struct **vmas, int *nonblocking)
1491da177e4SLinus Torvalds {
150910e46daSSonic Zhang 	struct vm_area_struct *vma;
1517b4d5b8bSDavid Howells 	unsigned long vm_flags;
1527b4d5b8bSDavid Howells 	int i;
1537b4d5b8bSDavid Howells 
1547b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
15558fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1567b4d5b8bSDavid Howells 	 */
15758fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
15858fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
15958fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
16058fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1611da177e4SLinus Torvalds 
1629d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
1637561e8caSDavid Howells 		vma = find_vma(mm, start);
164910e46daSSonic Zhang 		if (!vma)
1657b4d5b8bSDavid Howells 			goto finish_or_fault;
1667b4d5b8bSDavid Howells 
1677b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1681c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1691c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1707b4d5b8bSDavid Howells 			goto finish_or_fault;
171910e46daSSonic Zhang 
1721da177e4SLinus Torvalds 		if (pages) {
1731da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1741da177e4SLinus Torvalds 			if (pages[i])
1751da177e4SLinus Torvalds 				page_cache_get(pages[i]);
1761da177e4SLinus Torvalds 		}
1771da177e4SLinus Torvalds 		if (vmas)
178910e46daSSonic Zhang 			vmas[i] = vma;
179e1ee65d8SDavid Howells 		start = (start + PAGE_SIZE) & PAGE_MASK;
1801da177e4SLinus Torvalds 	}
1817b4d5b8bSDavid Howells 
1827b4d5b8bSDavid Howells 	return i;
1837b4d5b8bSDavid Howells 
1847b4d5b8bSDavid Howells finish_or_fault:
1857b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1861da177e4SLinus Torvalds }
187b291f000SNick Piggin 
188b291f000SNick Piggin /*
189b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
190b291f000SNick Piggin  * and indicate the VMA that covers each page
191b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
192b291f000SNick Piggin  *   slab page or a secondary page from a compound page
193b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
194b291f000SNick Piggin  */
19528a35716SMichel Lespinasse long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
19628a35716SMichel Lespinasse 		    unsigned long start, unsigned long nr_pages,
19728a35716SMichel Lespinasse 		    int write, int force, struct page **pages,
19828a35716SMichel Lespinasse 		    struct vm_area_struct **vmas)
199b291f000SNick Piggin {
200b291f000SNick Piggin 	int flags = 0;
201b291f000SNick Piggin 
202b291f000SNick Piggin 	if (write)
20358fa879eSHugh Dickins 		flags |= FOLL_WRITE;
204b291f000SNick Piggin 	if (force)
20558fa879eSHugh Dickins 		flags |= FOLL_FORCE;
206b291f000SNick Piggin 
20753a7706dSMichel Lespinasse 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
20853a7706dSMichel Lespinasse 				NULL);
209b291f000SNick Piggin }
21066aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages);
21166aa2b4bSGreg Ungerer 
212dfc2f91aSPaul Mundt /**
213dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
214dfc2f91aSPaul Mundt  * @vma: memory mapping
215dfc2f91aSPaul Mundt  * @address: user virtual address
216dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
217dfc2f91aSPaul Mundt  *
218dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
219dfc2f91aSPaul Mundt  *
220dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
221dfc2f91aSPaul Mundt  */
222dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
223dfc2f91aSPaul Mundt 	unsigned long *pfn)
224dfc2f91aSPaul Mundt {
225dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
226dfc2f91aSPaul Mundt 		return -EINVAL;
227dfc2f91aSPaul Mundt 
228dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
229dfc2f91aSPaul Mundt 	return 0;
230dfc2f91aSPaul Mundt }
231dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
232dfc2f91aSPaul Mundt 
233f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
2341da177e4SLinus Torvalds 
235b3bdda02SChristoph Lameter void vfree(const void *addr)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	kfree(addr);
2381da177e4SLinus Torvalds }
239b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2401da177e4SLinus Torvalds 
241dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	/*
2448518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2458518609dSRobert P. J. Day 	 * returns only a logical address.
2461da177e4SLinus Torvalds 	 */
24784097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2481da177e4SLinus Torvalds }
249b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2501da177e4SLinus Torvalds 
251f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
252f905bc44SPaul Mundt {
253f905bc44SPaul Mundt 	void *ret;
254f905bc44SPaul Mundt 
255f905bc44SPaul Mundt 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
256f905bc44SPaul Mundt 			PAGE_KERNEL);
257f905bc44SPaul Mundt 	if (ret) {
258f905bc44SPaul Mundt 		struct vm_area_struct *vma;
259f905bc44SPaul Mundt 
260f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
261f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
262f905bc44SPaul Mundt 		if (vma)
263f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
264f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
265f905bc44SPaul Mundt 	}
266f905bc44SPaul Mundt 
267f905bc44SPaul Mundt 	return ret;
268f905bc44SPaul Mundt }
269f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
270f905bc44SPaul Mundt 
271b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	return virt_to_page(addr);
2741da177e4SLinus Torvalds }
275b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
2761da177e4SLinus Torvalds 
277b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2801da177e4SLinus Torvalds }
281b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2861da177e4SLinus Torvalds 	return count;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	/* Don't allow overflow */
2921da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2931da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	memcpy(addr, buf, count);
2961da177e4SLinus Torvalds 	return(count);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually continguos memory
3011da177e4SLinus Torvalds  *
3021da177e4SLinus Torvalds  *	@size:		allocation size
3031da177e4SLinus Torvalds  *
3041da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
3051da177e4SLinus Torvalds  *	allocator and map them into continguos kernel virtual space.
3061da177e4SLinus Torvalds  *
307c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
3081da177e4SLinus Torvalds  *	use __vmalloc() instead.
3091da177e4SLinus Torvalds  */
3101da177e4SLinus Torvalds void *vmalloc(unsigned long size)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
3131da177e4SLinus Torvalds }
314f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
315f6138882SAndrew Morton 
316e1ca7788SDave Young /*
317e1ca7788SDave Young  *	vzalloc - allocate virtually continguos memory with zero fill
318e1ca7788SDave Young  *
319e1ca7788SDave Young  *	@size:		allocation size
320e1ca7788SDave Young  *
321e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
322e1ca7788SDave Young  *	allocator and map them into continguos kernel virtual space.
323e1ca7788SDave Young  *	The memory allocated is set to zero.
324e1ca7788SDave Young  *
325e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
326e1ca7788SDave Young  *	use __vmalloc() instead.
327e1ca7788SDave Young  */
328e1ca7788SDave Young void *vzalloc(unsigned long size)
329e1ca7788SDave Young {
330e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
331e1ca7788SDave Young 			PAGE_KERNEL);
332e1ca7788SDave Young }
333e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
334e1ca7788SDave Young 
335e1ca7788SDave Young /**
336e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
337e1ca7788SDave Young  * @size:	allocation size
338e1ca7788SDave Young  * @node:	numa node
339e1ca7788SDave Young  *
340e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
341e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
342e1ca7788SDave Young  *
343e1ca7788SDave Young  * For tight control over page level allocator and protection flags
344e1ca7788SDave Young  * use __vmalloc() instead.
345e1ca7788SDave Young  */
346f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
347f6138882SAndrew Morton {
348f6138882SAndrew Morton 	return vmalloc(size);
349f6138882SAndrew Morton }
3509a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
351e1ca7788SDave Young 
352e1ca7788SDave Young /**
353e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
354e1ca7788SDave Young  * @size:	allocation size
355e1ca7788SDave Young  * @node:	numa node
356e1ca7788SDave Young  *
357e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
358e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
359e1ca7788SDave Young  * The memory allocated is set to zero.
360e1ca7788SDave Young  *
361e1ca7788SDave Young  * For tight control over page level allocator and protection flags
362e1ca7788SDave Young  * use __vmalloc() instead.
363e1ca7788SDave Young  */
364e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
365e1ca7788SDave Young {
366e1ca7788SDave Young 	return vzalloc(size);
367e1ca7788SDave Young }
368e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
3691da177e4SLinus Torvalds 
3701af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
3711af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
3721af446edSPaul Mundt #endif
3731af446edSPaul Mundt 
3741af446edSPaul Mundt /**
3751af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
3761af446edSPaul Mundt  *	@size:		allocation size
3771af446edSPaul Mundt  *
3781af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
3791af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
3801af446edSPaul Mundt  *	executable kernel virtual space.
3811af446edSPaul Mundt  *
3821af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3831af446edSPaul Mundt  *	use __vmalloc() instead.
3841af446edSPaul Mundt  */
3851af446edSPaul Mundt 
3861af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3871af446edSPaul Mundt {
3881af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3891af446edSPaul Mundt }
3901af446edSPaul Mundt 
391b5073173SPaul Mundt /**
392b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3931da177e4SLinus Torvalds  *	@size:		allocation size
3941da177e4SLinus Torvalds  *
3951da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
3961da177e4SLinus Torvalds  *	page level allocator and map them into continguos kernel virtual space.
3971da177e4SLinus Torvalds  */
3981da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
4011da177e4SLinus Torvalds }
402b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
403b5073173SPaul Mundt 
404b5073173SPaul Mundt /**
405b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
406b5073173SPaul Mundt  *	@size:		allocation size
407b5073173SPaul Mundt  *
408b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
409b5073173SPaul Mundt  * mapped to userspace without leaking data.
410f905bc44SPaul Mundt  *
411f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
412f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
413b5073173SPaul Mundt  */
414b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
415b5073173SPaul Mundt {
416f905bc44SPaul Mundt 	/*
417f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
418f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
419f905bc44SPaul Mundt 	 */
420f905bc44SPaul Mundt 	return vmalloc_user(size);
421b5073173SPaul Mundt }
422b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	BUG();
4271da177e4SLinus Torvalds 	return NULL;
4281da177e4SLinus Torvalds }
429b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
4301da177e4SLinus Torvalds 
431b3bdda02SChristoph Lameter void vunmap(const void *addr)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	BUG();
4341da177e4SLinus Torvalds }
435b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
4361da177e4SLinus Torvalds 
437eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
438eb6434d9SPaul Mundt {
439eb6434d9SPaul Mundt 	BUG();
440eb6434d9SPaul Mundt 	return NULL;
441eb6434d9SPaul Mundt }
442eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
443eb6434d9SPaul Mundt 
444eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
445eb6434d9SPaul Mundt {
446eb6434d9SPaul Mundt 	BUG();
447eb6434d9SPaul Mundt }
448eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
449eb6434d9SPaul Mundt 
450eb6434d9SPaul Mundt void vm_unmap_aliases(void)
451eb6434d9SPaul Mundt {
452eb6434d9SPaul Mundt }
453eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
454eb6434d9SPaul Mundt 
4551da177e4SLinus Torvalds /*
4561eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
4571eeb66a1SChristoph Hellwig  * have one.
4581eeb66a1SChristoph Hellwig  */
4591eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
4601eeb66a1SChristoph Hellwig {
4611eeb66a1SChristoph Hellwig }
4621eeb66a1SChristoph Hellwig 
46329c185e5SPaul Mundt /**
46429c185e5SPaul Mundt  *	alloc_vm_area - allocate a range of kernel address space
46529c185e5SPaul Mundt  *	@size:		size of the area
46629c185e5SPaul Mundt  *
46729c185e5SPaul Mundt  *	Returns:	NULL on failure, vm_struct on success
46829c185e5SPaul Mundt  *
46929c185e5SPaul Mundt  *	This function reserves a range of kernel address space, and
47029c185e5SPaul Mundt  *	allocates pagetables to map that range.  No actual mappings
47129c185e5SPaul Mundt  *	are created.  If the kernel address space is not shared
47229c185e5SPaul Mundt  *	between processes, it syncs the pagetable across all
47329c185e5SPaul Mundt  *	processes.
47429c185e5SPaul Mundt  */
475cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
47629c185e5SPaul Mundt {
47729c185e5SPaul Mundt 	BUG();
47829c185e5SPaul Mundt 	return NULL;
47929c185e5SPaul Mundt }
48029c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
48129c185e5SPaul Mundt 
48229c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
48329c185e5SPaul Mundt {
48429c185e5SPaul Mundt 	BUG();
48529c185e5SPaul Mundt }
48629c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
48729c185e5SPaul Mundt 
488b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
489b5073173SPaul Mundt 		   struct page *page)
490b5073173SPaul Mundt {
491b5073173SPaul Mundt 	return -EINVAL;
492b5073173SPaul Mundt }
493b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
494b5073173SPaul Mundt 
4951eeb66a1SChristoph Hellwig /*
4961da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4971da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4981da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4991da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
5001da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
5011da177e4SLinus Torvalds  */
5026a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
5071da177e4SLinus Torvalds 		return mm->brk;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	if (mm->brk == brk)
5101da177e4SLinus Torvalds 		return mm->brk;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	/*
5131da177e4SLinus Torvalds 	 * Always allow shrinking brk
5141da177e4SLinus Torvalds 	 */
5151da177e4SLinus Torvalds 	if (brk <= mm->brk) {
5161da177e4SLinus Torvalds 		mm->brk = brk;
5171da177e4SLinus Torvalds 		return brk;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/*
5211da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
5221da177e4SLinus Torvalds 	 */
523cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
5241da177e4SLinus Torvalds 	return mm->brk = brk;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5278feae131SDavid Howells /*
5288feae131SDavid Howells  * initialise the VMA and region record slabs
5298feae131SDavid Howells  */
5308feae131SDavid Howells void __init mmap_init(void)
5311da177e4SLinus Torvalds {
53200a62ce9SKOSAKI Motohiro 	int ret;
53300a62ce9SKOSAKI Motohiro 
53400a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
53500a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
53633e5d769SDavid Howells 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
5378feae131SDavid Howells }
5381da177e4SLinus Torvalds 
5398feae131SDavid Howells /*
5408feae131SDavid Howells  * validate the region tree
5418feae131SDavid Howells  * - the caller must hold the region lock
5428feae131SDavid Howells  */
5438feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
5448feae131SDavid Howells static noinline void validate_nommu_regions(void)
5458feae131SDavid Howells {
5468feae131SDavid Howells 	struct vm_region *region, *last;
5478feae131SDavid Howells 	struct rb_node *p, *lastp;
5481da177e4SLinus Torvalds 
5498feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
5508feae131SDavid Howells 	if (!lastp)
5518feae131SDavid Howells 		return;
5528feae131SDavid Howells 
5538feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
55433e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_end <= last->vm_start));
55533e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_top < last->vm_end));
5568feae131SDavid Howells 
5578feae131SDavid Howells 	while ((p = rb_next(lastp))) {
5588feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
5598feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
5608feae131SDavid Howells 
56133e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_end <= region->vm_start));
56233e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_top < region->vm_end));
56333e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_start < last->vm_top));
5648feae131SDavid Howells 
5658feae131SDavid Howells 		lastp = p;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds }
5688feae131SDavid Howells #else
56933e5d769SDavid Howells static void validate_nommu_regions(void)
57033e5d769SDavid Howells {
57133e5d769SDavid Howells }
5728feae131SDavid Howells #endif
5738feae131SDavid Howells 
5748feae131SDavid Howells /*
5758feae131SDavid Howells  * add a region into the global tree
5768feae131SDavid Howells  */
5778feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
5788feae131SDavid Howells {
5798feae131SDavid Howells 	struct vm_region *pregion;
5808feae131SDavid Howells 	struct rb_node **p, *parent;
5818feae131SDavid Howells 
5828feae131SDavid Howells 	validate_nommu_regions();
5838feae131SDavid Howells 
5848feae131SDavid Howells 	parent = NULL;
5858feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
5868feae131SDavid Howells 	while (*p) {
5878feae131SDavid Howells 		parent = *p;
5888feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5898feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5908feae131SDavid Howells 			p = &(*p)->rb_left;
5918feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5928feae131SDavid Howells 			p = &(*p)->rb_right;
5938feae131SDavid Howells 		else if (pregion == region)
5948feae131SDavid Howells 			return;
5958feae131SDavid Howells 		else
5968feae131SDavid Howells 			BUG();
5978feae131SDavid Howells 	}
5988feae131SDavid Howells 
5998feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
6008feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
6018feae131SDavid Howells 
6028feae131SDavid Howells 	validate_nommu_regions();
6038feae131SDavid Howells }
6048feae131SDavid Howells 
6058feae131SDavid Howells /*
6068feae131SDavid Howells  * delete a region from the global tree
6078feae131SDavid Howells  */
6088feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
6098feae131SDavid Howells {
6108feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6118feae131SDavid Howells 
6128feae131SDavid Howells 	validate_nommu_regions();
6138feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
6148feae131SDavid Howells 	validate_nommu_regions();
6158feae131SDavid Howells }
6168feae131SDavid Howells 
6178feae131SDavid Howells /*
6188feae131SDavid Howells  * free a contiguous series of pages
6198feae131SDavid Howells  */
6208feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
6218feae131SDavid Howells {
6228feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
6238feae131SDavid Howells 		struct page *page = virt_to_page(from);
6248feae131SDavid Howells 
6258feae131SDavid Howells 		kdebug("- free %lx", from);
62633e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
6278feae131SDavid Howells 		if (page_count(page) != 1)
62833e5d769SDavid Howells 			kdebug("free page %p: refcount not one: %d",
62933e5d769SDavid Howells 			       page, page_count(page));
6308feae131SDavid Howells 		put_page(page);
6318feae131SDavid Howells 	}
6328feae131SDavid Howells }
6338feae131SDavid Howells 
6348feae131SDavid Howells /*
6358feae131SDavid Howells  * release a reference to a region
63633e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
637dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
6388feae131SDavid Howells  *   will equal vm_start
6398feae131SDavid Howells  */
6408feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
6418feae131SDavid Howells 	__releases(nommu_region_sem)
6428feae131SDavid Howells {
6431e2ae599SDavid Howells 	kenter("%p{%d}", region, region->vm_usage);
6448feae131SDavid Howells 
6458feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
6468feae131SDavid Howells 
6471e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
648dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
6498feae131SDavid Howells 			delete_nommu_region(region);
6508feae131SDavid Howells 		up_write(&nommu_region_sem);
6518feae131SDavid Howells 
6528feae131SDavid Howells 		if (region->vm_file)
6538feae131SDavid Howells 			fput(region->vm_file);
6548feae131SDavid Howells 
6558feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
6568feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
6578feae131SDavid Howells 		if (region->vm_flags & VM_MAPPED_COPY) {
6588feae131SDavid Howells 			kdebug("free series");
659dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
6608feae131SDavid Howells 		}
6618feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
6628feae131SDavid Howells 	} else {
6638feae131SDavid Howells 		up_write(&nommu_region_sem);
6648feae131SDavid Howells 	}
6658feae131SDavid Howells }
6668feae131SDavid Howells 
6678feae131SDavid Howells /*
6688feae131SDavid Howells  * release a reference to a region
6698feae131SDavid Howells  */
6708feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
6718feae131SDavid Howells {
6728feae131SDavid Howells 	down_write(&nommu_region_sem);
6738feae131SDavid Howells 	__put_nommu_region(region);
6748feae131SDavid Howells }
6751da177e4SLinus Torvalds 
6763034097aSDavid Howells /*
677eb8cdec4SBernd Schmidt  * update protection on a vma
678eb8cdec4SBernd Schmidt  */
679eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
680eb8cdec4SBernd Schmidt {
681eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
682eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
683eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
684eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
685eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
686eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
687eb8cdec4SBernd Schmidt 	}
688eb8cdec4SBernd Schmidt 	update_protections(mm);
689eb8cdec4SBernd Schmidt #endif
690eb8cdec4SBernd Schmidt }
691eb8cdec4SBernd Schmidt 
692eb8cdec4SBernd Schmidt /*
6933034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
6948feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
6958feae131SDavid Howells  * page
6963034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
6973034097aSDavid Howells  */
6988feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
6993034097aSDavid Howells {
7006038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
7018feae131SDavid Howells 	struct address_space *mapping;
7026038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
7033034097aSDavid Howells 
7048feae131SDavid Howells 	kenter(",%p", vma);
7058feae131SDavid Howells 
7068feae131SDavid Howells 	BUG_ON(!vma->vm_region);
7078feae131SDavid Howells 
7088feae131SDavid Howells 	mm->map_count++;
7098feae131SDavid Howells 	vma->vm_mm = mm;
7108feae131SDavid Howells 
711eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
712eb8cdec4SBernd Schmidt 
7138feae131SDavid Howells 	/* add the VMA to the mapping */
7148feae131SDavid Howells 	if (vma->vm_file) {
7158feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7168feae131SDavid Howells 
717918e556eSDavid Howells 		mutex_lock(&mapping->i_mmap_mutex);
7188feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7196b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
7208feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
721918e556eSDavid Howells 		mutex_unlock(&mapping->i_mmap_mutex);
7228feae131SDavid Howells 	}
7238feae131SDavid Howells 
7248feae131SDavid Howells 	/* add the VMA to the tree */
7256038def0SNamhyung Kim 	parent = rb_prev = NULL;
7268feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
7278feae131SDavid Howells 	while (*p) {
7288feae131SDavid Howells 		parent = *p;
7298feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
7308feae131SDavid Howells 
7318feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
7328feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
7338feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
7348feae131SDavid Howells 			p = &(*p)->rb_left;
7356038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
7366038def0SNamhyung Kim 			rb_prev = parent;
7378feae131SDavid Howells 			p = &(*p)->rb_right;
7386038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
7398feae131SDavid Howells 			p = &(*p)->rb_left;
7406038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
7416038def0SNamhyung Kim 			rb_prev = parent;
7428feae131SDavid Howells 			p = &(*p)->rb_right;
7436038def0SNamhyung Kim 		} else if (vma < pvma)
7448feae131SDavid Howells 			p = &(*p)->rb_left;
7456038def0SNamhyung Kim 		else if (vma > pvma) {
7466038def0SNamhyung Kim 			rb_prev = parent;
7478feae131SDavid Howells 			p = &(*p)->rb_right;
7486038def0SNamhyung Kim 		} else
7498feae131SDavid Howells 			BUG();
7508feae131SDavid Howells 	}
7518feae131SDavid Howells 
7528feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
7538feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
7548feae131SDavid Howells 
7558feae131SDavid Howells 	/* add VMA to the VMA list also */
7566038def0SNamhyung Kim 	prev = NULL;
7576038def0SNamhyung Kim 	if (rb_prev)
7586038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
7593034097aSDavid Howells 
7606038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
7618feae131SDavid Howells }
7628feae131SDavid Howells 
7638feae131SDavid Howells /*
7648feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
7658feae131SDavid Howells  */
7668feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
7678feae131SDavid Howells {
7688feae131SDavid Howells 	struct address_space *mapping;
7698feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
7708feae131SDavid Howells 
7718feae131SDavid Howells 	kenter("%p", vma);
7728feae131SDavid Howells 
773eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
774eb8cdec4SBernd Schmidt 
7758feae131SDavid Howells 	mm->map_count--;
7768feae131SDavid Howells 	if (mm->mmap_cache == vma)
7778feae131SDavid Howells 		mm->mmap_cache = NULL;
7788feae131SDavid Howells 
7798feae131SDavid Howells 	/* remove the VMA from the mapping */
7808feae131SDavid Howells 	if (vma->vm_file) {
7818feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
7828feae131SDavid Howells 
783918e556eSDavid Howells 		mutex_lock(&mapping->i_mmap_mutex);
7848feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
7856b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
7868feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
787918e556eSDavid Howells 		mutex_unlock(&mapping->i_mmap_mutex);
7888feae131SDavid Howells 	}
7898feae131SDavid Howells 
7908feae131SDavid Howells 	/* remove from the MM's tree and list */
7918feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
792b951bf2cSNamhyung Kim 
793b951bf2cSNamhyung Kim 	if (vma->vm_prev)
794b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
795b951bf2cSNamhyung Kim 	else
796b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
797b951bf2cSNamhyung Kim 
798b951bf2cSNamhyung Kim 	if (vma->vm_next)
799b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
8008feae131SDavid Howells }
8018feae131SDavid Howells 
8028feae131SDavid Howells /*
8038feae131SDavid Howells  * destroy a VMA record
8048feae131SDavid Howells  */
8058feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
8068feae131SDavid Howells {
8078feae131SDavid Howells 	kenter("%p", vma);
8088feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
8098feae131SDavid Howells 		vma->vm_ops->close(vma);
810e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
8118feae131SDavid Howells 		fput(vma->vm_file);
8128feae131SDavid Howells 	put_nommu_region(vma->vm_region);
8138feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
8143034097aSDavid Howells }
8153034097aSDavid Howells 
8163034097aSDavid Howells /*
8173034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
8183034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8193034097aSDavid Howells  */
8203034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
8213034097aSDavid Howells {
8228feae131SDavid Howells 	struct vm_area_struct *vma;
8233034097aSDavid Howells 
8248feae131SDavid Howells 	/* check the cache first */
825b6a9b7f6SJan Stancek 	vma = ACCESS_ONCE(mm->mmap_cache);
8268feae131SDavid Howells 	if (vma && vma->vm_start <= addr && vma->vm_end > addr)
8278feae131SDavid Howells 		return vma;
8288feae131SDavid Howells 
829e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8308feae131SDavid Howells 	 * resides) */
831e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8328feae131SDavid Howells 		if (vma->vm_start > addr)
8338feae131SDavid Howells 			return NULL;
8348feae131SDavid Howells 		if (vma->vm_end > addr) {
8358feae131SDavid Howells 			mm->mmap_cache = vma;
8368feae131SDavid Howells 			return vma;
8373034097aSDavid Howells 		}
8388feae131SDavid Howells 	}
8393034097aSDavid Howells 
8403034097aSDavid Howells 	return NULL;
8413034097aSDavid Howells }
8423034097aSDavid Howells EXPORT_SYMBOL(find_vma);
8433034097aSDavid Howells 
8443034097aSDavid Howells /*
845930e652aSDavid Howells  * find a VMA
846930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
847930e652aSDavid Howells  */
848930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
849930e652aSDavid Howells {
8507561e8caSDavid Howells 	return find_vma(mm, addr);
851930e652aSDavid Howells }
852930e652aSDavid Howells 
8538feae131SDavid Howells /*
8548feae131SDavid Howells  * expand a stack to a given address
8558feae131SDavid Howells  * - not supported under NOMMU conditions
8568feae131SDavid Howells  */
85757c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
85857c8f63eSGreg Ungerer {
85957c8f63eSGreg Ungerer 	return -ENOMEM;
86057c8f63eSGreg Ungerer }
86157c8f63eSGreg Ungerer 
862930e652aSDavid Howells /*
8636fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
8646fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
8656fa5f80bSDavid Howells  */
8668feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
8678feae131SDavid Howells 					     unsigned long addr,
8688feae131SDavid Howells 					     unsigned long len)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	struct vm_area_struct *vma;
8718feae131SDavid Howells 	unsigned long end = addr + len;
8721da177e4SLinus Torvalds 
8738feae131SDavid Howells 	/* check the cache first */
8748feae131SDavid Howells 	vma = mm->mmap_cache;
8758feae131SDavid Howells 	if (vma && vma->vm_start == addr && vma->vm_end == end)
8761da177e4SLinus Torvalds 		return vma;
8778feae131SDavid Howells 
878e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
8798feae131SDavid Howells 	 * resides) */
880e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
8818feae131SDavid Howells 		if (vma->vm_start < addr)
8828feae131SDavid Howells 			continue;
8838feae131SDavid Howells 		if (vma->vm_start > addr)
8848feae131SDavid Howells 			return NULL;
8858feae131SDavid Howells 		if (vma->vm_end == end) {
8868feae131SDavid Howells 			mm->mmap_cache = vma;
8878feae131SDavid Howells 			return vma;
8888feae131SDavid Howells 		}
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	return NULL;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8943034097aSDavid Howells /*
8951da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
8961da177e4SLinus Torvalds  * mapping we're capable of supporting
8971da177e4SLinus Torvalds  */
8981da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
8991da177e4SLinus Torvalds 				 unsigned long addr,
9001da177e4SLinus Torvalds 				 unsigned long len,
9011da177e4SLinus Torvalds 				 unsigned long prot,
9021da177e4SLinus Torvalds 				 unsigned long flags,
9031da177e4SLinus Torvalds 				 unsigned long pgoff,
9041da177e4SLinus Torvalds 				 unsigned long *_capabilities)
9051da177e4SLinus Torvalds {
9068feae131SDavid Howells 	unsigned long capabilities, rlen;
9071da177e4SLinus Torvalds 	int ret;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	/* do the simple checks first */
91006aab5a3SDavid Howells 	if (flags & MAP_FIXED) {
9111da177e4SLinus Torvalds 		printk(KERN_DEBUG
9121da177e4SLinus Torvalds 		       "%d: Can't do fixed-address/overlay mmap of RAM\n",
9131da177e4SLinus Torvalds 		       current->pid);
9141da177e4SLinus Torvalds 		return -EINVAL;
9151da177e4SLinus Torvalds 	}
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
9181da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
9191da177e4SLinus Torvalds 		return -EINVAL;
9201da177e4SLinus Torvalds 
921f81cff0dSMike Frysinger 	if (!len)
9221da177e4SLinus Torvalds 		return -EINVAL;
9231da177e4SLinus Torvalds 
924f81cff0dSMike Frysinger 	/* Careful about overflows.. */
9258feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
9268feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
927f81cff0dSMike Frysinger 		return -ENOMEM;
928f81cff0dSMike Frysinger 
9291da177e4SLinus Torvalds 	/* offset overflow? */
9308feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
931f81cff0dSMike Frysinger 		return -EOVERFLOW;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (file) {
9341da177e4SLinus Torvalds 		/* validate file mapping requests */
9351da177e4SLinus Torvalds 		struct address_space *mapping;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 		/* files must support mmap */
9381da177e4SLinus Torvalds 		if (!file->f_op || !file->f_op->mmap)
9391da177e4SLinus Torvalds 			return -ENODEV;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
9421da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
9431da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
9441da177e4SLinus Torvalds 		 */
9451da177e4SLinus Torvalds 		mapping = file->f_mapping;
9461da177e4SLinus Torvalds 		if (!mapping)
947496ad9aaSAl Viro 			mapping = file_inode(file)->i_mapping;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 		capabilities = 0;
9501da177e4SLinus Torvalds 		if (mapping && mapping->backing_dev_info)
9511da177e4SLinus Torvalds 			capabilities = mapping->backing_dev_info->capabilities;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 		if (!capabilities) {
9541da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
9551da177e4SLinus Torvalds 			 * defaults */
956496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
9571da177e4SLinus Torvalds 			case S_IFREG:
9581da177e4SLinus Torvalds 			case S_IFBLK:
9591da177e4SLinus Torvalds 				capabilities = BDI_CAP_MAP_COPY;
9601da177e4SLinus Torvalds 				break;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 			case S_IFCHR:
9631da177e4SLinus Torvalds 				capabilities =
9641da177e4SLinus Torvalds 					BDI_CAP_MAP_DIRECT |
9651da177e4SLinus Torvalds 					BDI_CAP_READ_MAP |
9661da177e4SLinus Torvalds 					BDI_CAP_WRITE_MAP;
9671da177e4SLinus Torvalds 				break;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 			default:
9701da177e4SLinus Torvalds 				return -EINVAL;
9711da177e4SLinus Torvalds 			}
9721da177e4SLinus Torvalds 		}
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
9751da177e4SLinus Torvalds 		 * device */
9761da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
9771da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
9781da177e4SLinus Torvalds 		if (!file->f_op->read)
9791da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
9801da177e4SLinus Torvalds 
98128d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
98228d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
98328d7a6aeSGraff Yang 			return -EACCES;
98428d7a6aeSGraff Yang 
9851da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
9861da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
9871da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
9881da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
9891da177e4SLinus Torvalds 				return -EACCES;
9901da177e4SLinus Torvalds 
991496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
9921da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
9931da177e4SLinus Torvalds 				return -EACCES;
9941da177e4SLinus Torvalds 
995496ad9aaSAl Viro 			if (locks_verify_locked(file_inode(file)))
9961da177e4SLinus Torvalds 				return -EAGAIN;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_DIRECT))
9991da177e4SLinus Torvalds 				return -ENODEV;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
10021da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
10031da177e4SLinus Torvalds 		}
10041da177e4SLinus Torvalds 		else {
10051da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
10061da177e4SLinus Torvalds 			 * allocate */
10071da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_COPY))
10081da177e4SLinus Torvalds 				return -ENODEV;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
10111da177e4SLinus Torvalds 			 * shared with the backing device */
10121da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
10131da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
10141da177e4SLinus Torvalds 		}
10151da177e4SLinus Torvalds 
10163c7b2045SBernd Schmidt 		if (capabilities & BDI_CAP_MAP_DIRECT) {
10173c7b2045SBernd Schmidt 			if (((prot & PROT_READ)  && !(capabilities & BDI_CAP_READ_MAP))  ||
10183c7b2045SBernd Schmidt 			    ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
10193c7b2045SBernd Schmidt 			    ((prot & PROT_EXEC)  && !(capabilities & BDI_CAP_EXEC_MAP))
10203c7b2045SBernd Schmidt 			    ) {
10213c7b2045SBernd Schmidt 				capabilities &= ~BDI_CAP_MAP_DIRECT;
10223c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
10233c7b2045SBernd Schmidt 					printk(KERN_WARNING
10243c7b2045SBernd Schmidt 					       "MAP_SHARED not completely supported on !MMU\n");
10253c7b2045SBernd Schmidt 					return -EINVAL;
10263c7b2045SBernd Schmidt 				}
10273c7b2045SBernd Schmidt 			}
10283c7b2045SBernd Schmidt 		}
10293c7b2045SBernd Schmidt 
10301da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
10311da177e4SLinus Torvalds 		 * mappings */
1032e9536ae7SJosef Sipek 		if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
10331da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
10341da177e4SLinus Torvalds 				return -EPERM;
10351da177e4SLinus Torvalds 		}
10361da177e4SLinus Torvalds 		else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
10371da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
10381da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
10391da177e4SLinus Torvalds 				if (capabilities & BDI_CAP_EXEC_MAP)
10401da177e4SLinus Torvalds 					prot |= PROT_EXEC;
10411da177e4SLinus Torvalds 			}
10421da177e4SLinus Torvalds 		}
10431da177e4SLinus Torvalds 		else if ((prot & PROT_READ) &&
10441da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
10451da177e4SLinus Torvalds 			 !(capabilities & BDI_CAP_EXEC_MAP)
10461da177e4SLinus Torvalds 			 ) {
10471da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
10481da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 	}
10511da177e4SLinus Torvalds 	else {
10521da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
10531da177e4SLinus Torvalds 		 * privately mapped
10541da177e4SLinus Torvalds 		 */
10551da177e4SLinus Torvalds 		capabilities = BDI_CAP_MAP_COPY;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
10581da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
10591da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
10601da177e4SLinus Torvalds 			prot |= PROT_EXEC;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	/* allow the security API to have its say */
1064e5467859SAl Viro 	ret = security_mmap_addr(addr);
1065e5467859SAl Viro 	if (ret < 0)
1066e5467859SAl Viro 		return ret;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	/* looks okay */
10691da177e4SLinus Torvalds 	*_capabilities = capabilities;
10701da177e4SLinus Torvalds 	return 0;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds /*
10741da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
10751da177e4SLinus Torvalds  * now know into VMA flags
10761da177e4SLinus Torvalds  */
10771da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
10781da177e4SLinus Torvalds 					unsigned long prot,
10791da177e4SLinus Torvalds 					unsigned long flags,
10801da177e4SLinus Torvalds 					unsigned long capabilities)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	unsigned long vm_flags;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
10851da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
10881da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
10893c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
10901da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
10911da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10923c7b2045SBernd Schmidt 	} else {
10931da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
10941da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
10951da177e4SLinus Torvalds 		 * romfs/cramfs */
10963c7b2045SBernd Schmidt 		vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
10971da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
10983c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
11021da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
11031da177e4SLinus Torvalds 	 * with another untraced process
11041da177e4SLinus Torvalds 	 */
1105a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
11061da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	return vm_flags;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds /*
11128feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
11138feae131SDavid Howells  * pins the storage)
11141da177e4SLinus Torvalds  */
11158feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	int ret;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1120dd8632a1SPaul Mundt 	if (ret == 0) {
1121dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1122645d83c5SDavid Howells 		return 0;
1123dd8632a1SPaul Mundt 	}
11241da177e4SLinus Torvalds 	if (ret != -ENOSYS)
11251da177e4SLinus Torvalds 		return ret;
11261da177e4SLinus Torvalds 
11273fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
11283fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
11293fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
11301da177e4SLinus Torvalds 	return -ENODEV;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds /*
11341da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
11351da177e4SLinus Torvalds  */
11368feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
11378feae131SDavid Howells 			   struct vm_region *region,
1138645d83c5SDavid Howells 			   unsigned long len,
1139645d83c5SDavid Howells 			   unsigned long capabilities)
11401da177e4SLinus Torvalds {
11418feae131SDavid Howells 	struct page *pages;
1142f67d9b15SBob Liu 	unsigned long total, point, n;
11431da177e4SLinus Torvalds 	void *base;
11448feae131SDavid Howells 	int ret, order;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
11471da177e4SLinus Torvalds 	 * shared mappings on devices or memory
11481da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
11491da177e4SLinus Torvalds 	 */
1150645d83c5SDavid Howells 	if (capabilities & BDI_CAP_MAP_DIRECT) {
11511da177e4SLinus Torvalds 		ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1152dd8632a1SPaul Mundt 		if (ret == 0) {
11531da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1154dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1155dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1156645d83c5SDavid Howells 			return 0;
11571da177e4SLinus Torvalds 		}
1158dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1159dd8632a1SPaul Mundt 			return ret;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
11621da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
11631da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
11641da177e4SLinus Torvalds 	}
11651da177e4SLinus Torvalds 
11668feae131SDavid Howells 
11671da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
11681da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
11691da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
11701da177e4SLinus Torvalds 	 */
1171f67d9b15SBob Liu 	order = get_order(len);
11728feae131SDavid Howells 	kdebug("alloc order %d for %lx", order, len);
11738feae131SDavid Howells 
11748feae131SDavid Howells 	pages = alloc_pages(GFP_KERNEL, order);
11758feae131SDavid Howells 	if (!pages)
11761da177e4SLinus Torvalds 		goto enomem;
11771da177e4SLinus Torvalds 
11788feae131SDavid Howells 	total = 1 << order;
117933e5d769SDavid Howells 	atomic_long_add(total, &mmap_pages_allocated);
11801da177e4SLinus Torvalds 
1181f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1182dd8632a1SPaul Mundt 
1183dd8632a1SPaul Mundt 	/* we allocated a power-of-2 sized page set, so we may want to trim off
1184dd8632a1SPaul Mundt 	 * the excess */
1185dd8632a1SPaul Mundt 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
11868feae131SDavid Howells 		while (total > point) {
11878feae131SDavid Howells 			order = ilog2(total - point);
11888feae131SDavid Howells 			n = 1 << order;
11898feae131SDavid Howells 			kdebug("shave %lu/%lu @%lu", n, total - point, total);
119033e5d769SDavid Howells 			atomic_long_sub(n, &mmap_pages_allocated);
11918feae131SDavid Howells 			total -= n;
11928feae131SDavid Howells 			set_page_refcounted(pages + total);
11938feae131SDavid Howells 			__free_pages(pages + total, order);
11948feae131SDavid Howells 		}
1195dd8632a1SPaul Mundt 	}
11968feae131SDavid Howells 
11978feae131SDavid Howells 	for (point = 1; point < total; point++)
11988feae131SDavid Howells 		set_page_refcounted(&pages[point]);
11998feae131SDavid Howells 
12008feae131SDavid Howells 	base = page_address(pages);
12018feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
12028feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1203f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1204dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
12058feae131SDavid Howells 
12068feae131SDavid Howells 	vma->vm_start = region->vm_start;
12078feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	if (vma->vm_file) {
12101da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
12111da177e4SLinus Torvalds 		mm_segment_t old_fs;
12121da177e4SLinus Torvalds 		loff_t fpos;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
12151da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 		old_fs = get_fs();
12181da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
1219f67d9b15SBob Liu 		ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
12201da177e4SLinus Torvalds 		set_fs(old_fs);
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 		if (ret < 0)
12231da177e4SLinus Torvalds 			goto error_free;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 		/* clear the last little bit */
1226f67d9b15SBob Liu 		if (ret < len)
1227f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	return 0;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds error_free:
12347223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
12358feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
12368feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1237dd8632a1SPaul Mundt 	region->vm_top   = 0;
12381da177e4SLinus Torvalds 	return ret;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds enomem:
124105ae6fa3SGreg Ungerer 	printk("Allocation of length %lu from process %d (%s) failed\n",
124205ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
12437bf02ea2SDavid Rientjes 	show_free_areas(0);
12441da177e4SLinus Torvalds 	return -ENOMEM;
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds /*
12481da177e4SLinus Torvalds  * handle mapping creation for uClinux
12491da177e4SLinus Torvalds  */
1250e3fc629dSAl Viro unsigned long do_mmap_pgoff(struct file *file,
12511da177e4SLinus Torvalds 			    unsigned long addr,
12521da177e4SLinus Torvalds 			    unsigned long len,
12531da177e4SLinus Torvalds 			    unsigned long prot,
12541da177e4SLinus Torvalds 			    unsigned long flags,
1255bebeb3d6SMichel Lespinasse 			    unsigned long pgoff,
125641badc15SMichel Lespinasse 			    unsigned long *populate)
12571da177e4SLinus Torvalds {
12588feae131SDavid Howells 	struct vm_area_struct *vma;
12598feae131SDavid Howells 	struct vm_region *region;
12601da177e4SLinus Torvalds 	struct rb_node *rb;
12618feae131SDavid Howells 	unsigned long capabilities, vm_flags, result;
12621da177e4SLinus Torvalds 	int ret;
12631da177e4SLinus Torvalds 
12648feae131SDavid Howells 	kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
12658feae131SDavid Howells 
126641badc15SMichel Lespinasse 	*populate = 0;
1267bebeb3d6SMichel Lespinasse 
12681da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
12691da177e4SLinus Torvalds 	 * mapping */
12701da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
12711da177e4SLinus Torvalds 				    &capabilities);
12728feae131SDavid Howells 	if (ret < 0) {
12738feae131SDavid Howells 		kleave(" = %d [val]", ret);
12741da177e4SLinus Torvalds 		return ret;
12758feae131SDavid Howells 	}
12761da177e4SLinus Torvalds 
127706aab5a3SDavid Howells 	/* we ignore the address hint */
127806aab5a3SDavid Howells 	addr = 0;
1279f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
128006aab5a3SDavid Howells 
12811da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
12821da177e4SLinus Torvalds 	 * now know into VMA flags */
12831da177e4SLinus Torvalds 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
12841da177e4SLinus Torvalds 
12858feae131SDavid Howells 	/* we're going to need to record the mapping */
12868feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
12878feae131SDavid Howells 	if (!region)
12888feae131SDavid Howells 		goto error_getting_region;
12891da177e4SLinus Torvalds 
12908feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
12918feae131SDavid Howells 	if (!vma)
12928feae131SDavid Howells 		goto error_getting_vma;
12931da177e4SLinus Torvalds 
12941e2ae599SDavid Howells 	region->vm_usage = 1;
12958feae131SDavid Howells 	region->vm_flags = vm_flags;
12968feae131SDavid Howells 	region->vm_pgoff = pgoff;
12978feae131SDavid Howells 
12985beb4930SRik van Riel 	INIT_LIST_HEAD(&vma->anon_vma_chain);
12998feae131SDavid Howells 	vma->vm_flags = vm_flags;
13008feae131SDavid Howells 	vma->vm_pgoff = pgoff;
13018feae131SDavid Howells 
13028feae131SDavid Howells 	if (file) {
1303cb0942b8SAl Viro 		region->vm_file = get_file(file);
1304cb0942b8SAl Viro 		vma->vm_file = get_file(file);
13058feae131SDavid Howells 	}
13068feae131SDavid Howells 
13078feae131SDavid Howells 	down_write(&nommu_region_sem);
13088feae131SDavid Howells 
13098feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
13101da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
13118feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
13121da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
13131da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
13141da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
13151da177e4SLinus Torvalds 	 *   than here
13161da177e4SLinus Torvalds 	 */
13171da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
13188feae131SDavid Howells 		struct vm_region *pregion;
13198feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
13201da177e4SLinus Torvalds 
13218feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
13228feae131SDavid Howells 		pgend = pgoff + pglen;
1323165b2392SDavid Howells 
13248feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
13258feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
13261da177e4SLinus Torvalds 
13278feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
13281da177e4SLinus Torvalds 				continue;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1331496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1332496ad9aaSAl Viro 			    file_inode(file))
13331da177e4SLinus Torvalds 				continue;
13341da177e4SLinus Torvalds 
13358feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
13361da177e4SLinus Torvalds 				continue;
13371da177e4SLinus Torvalds 
13388feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
13398feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
13408feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
13418feae131SDavid Howells 			if (pgoff >= rpgend)
13421da177e4SLinus Torvalds 				continue;
13431da177e4SLinus Torvalds 
13448feae131SDavid Howells 			/* handle inexactly overlapping matches between
13458feae131SDavid Howells 			 * mappings */
13468feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
13478feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
13488feae131SDavid Howells 				/* new mapping is not a subset of the region */
13491da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_DIRECT))
13501da177e4SLinus Torvalds 					goto sharing_violation;
13511da177e4SLinus Torvalds 				continue;
13521da177e4SLinus Torvalds 			}
13531da177e4SLinus Torvalds 
13548feae131SDavid Howells 			/* we've found a region we can share */
13551e2ae599SDavid Howells 			pregion->vm_usage++;
13568feae131SDavid Howells 			vma->vm_region = pregion;
13578feae131SDavid Howells 			start = pregion->vm_start;
13588feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
13598feae131SDavid Howells 			vma->vm_start = start;
13608feae131SDavid Howells 			vma->vm_end = start + len;
13611da177e4SLinus Torvalds 
13628feae131SDavid Howells 			if (pregion->vm_flags & VM_MAPPED_COPY) {
13638feae131SDavid Howells 				kdebug("share copy");
13648feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
13658feae131SDavid Howells 			} else {
13668feae131SDavid Howells 				kdebug("share mmap");
13678feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
13688feae131SDavid Howells 				if (ret < 0) {
13698feae131SDavid Howells 					vma->vm_region = NULL;
13708feae131SDavid Howells 					vma->vm_start = 0;
13718feae131SDavid Howells 					vma->vm_end = 0;
13721e2ae599SDavid Howells 					pregion->vm_usage--;
13738feae131SDavid Howells 					pregion = NULL;
13748feae131SDavid Howells 					goto error_just_free;
13751da177e4SLinus Torvalds 				}
13768feae131SDavid Howells 			}
13778feae131SDavid Howells 			fput(region->vm_file);
13788feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
13798feae131SDavid Howells 			region = pregion;
13808feae131SDavid Howells 			result = start;
13818feae131SDavid Howells 			goto share;
13828feae131SDavid Howells 		}
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
13851da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
13861da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
13871da177e4SLinus Torvalds 		 */
1388645d83c5SDavid Howells 		if (capabilities & BDI_CAP_MAP_DIRECT) {
13891da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
13901da177e4SLinus Torvalds 							     pgoff, flags);
1391bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
13921da177e4SLinus Torvalds 				ret = addr;
1393bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
13948feae131SDavid Howells 					goto error_just_free;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
13971da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
13981da177e4SLinus Torvalds 				 * it */
1399bb005a59SNamhyung Kim 				ret = -ENODEV;
14001da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_COPY))
14018feae131SDavid Howells 					goto error_just_free;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
14048feae131SDavid Howells 			} else {
14058feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
14068feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
14071da177e4SLinus Torvalds 			}
14081da177e4SLinus Torvalds 		}
14091da177e4SLinus Torvalds 	}
14101da177e4SLinus Torvalds 
14118feae131SDavid Howells 	vma->vm_region = region;
14121da177e4SLinus Torvalds 
1413645d83c5SDavid Howells 	/* set up the mapping
1414645d83c5SDavid Howells 	 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1415645d83c5SDavid Howells 	 */
14161da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
14178feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
14181da177e4SLinus Torvalds 	else
1419645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
14201da177e4SLinus Torvalds 	if (ret < 0)
1421645d83c5SDavid Howells 		goto error_just_free;
1422645d83c5SDavid Howells 	add_nommu_region(region);
14238feae131SDavid Howells 
1424ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1425ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1426ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1427ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1428ea637639SJie Zhang 
14291da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
14308feae131SDavid Howells 	result = vma->vm_start;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
14331da177e4SLinus Torvalds 
14348feae131SDavid Howells share:
14358feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
14361da177e4SLinus Torvalds 
1437cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1438cfe79c00SMike Frysinger 	 * mapping of it is made  */
1439cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1440cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1441cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1442cfe79c00SMike Frysinger 	}
14431da177e4SLinus Torvalds 
1444cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
14451da177e4SLinus Torvalds 
14468feae131SDavid Howells 	kleave(" = %lx", result);
14478feae131SDavid Howells 	return result;
14481da177e4SLinus Torvalds 
14498feae131SDavid Howells error_just_free:
14508feae131SDavid Howells 	up_write(&nommu_region_sem);
14518feae131SDavid Howells error:
145289a86402SDavid Howells 	if (region->vm_file)
14538feae131SDavid Howells 		fput(region->vm_file);
14548feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
145589a86402SDavid Howells 	if (vma->vm_file)
14568feae131SDavid Howells 		fput(vma->vm_file);
14578feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
14588feae131SDavid Howells 	kleave(" = %d", ret);
14591da177e4SLinus Torvalds 	return ret;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds sharing_violation:
14628feae131SDavid Howells 	up_write(&nommu_region_sem);
14638feae131SDavid Howells 	printk(KERN_WARNING "Attempt to share mismatched mappings\n");
14648feae131SDavid Howells 	ret = -EINVAL;
14658feae131SDavid Howells 	goto error;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds error_getting_vma:
14688feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
14698feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
14708feae131SDavid Howells 	       " from process %d failed\n",
14711da177e4SLinus Torvalds 	       len, current->pid);
14727bf02ea2SDavid Rientjes 	show_free_areas(0);
14731da177e4SLinus Torvalds 	return -ENOMEM;
14741da177e4SLinus Torvalds 
14758feae131SDavid Howells error_getting_region:
14768feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
14778feae131SDavid Howells 	       " from process %d failed\n",
14781da177e4SLinus Torvalds 	       len, current->pid);
14797bf02ea2SDavid Rientjes 	show_free_areas(0);
14801da177e4SLinus Torvalds 	return -ENOMEM;
14811da177e4SLinus Torvalds }
14826be5ceb0SLinus Torvalds 
148366f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
148466f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
148566f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
148666f0dc48SHugh Dickins {
148766f0dc48SHugh Dickins 	struct file *file = NULL;
148866f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
148966f0dc48SHugh Dickins 
1490120a795dSAl Viro 	audit_mmap_fd(fd, flags);
149166f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
149266f0dc48SHugh Dickins 		file = fget(fd);
149366f0dc48SHugh Dickins 		if (!file)
149466f0dc48SHugh Dickins 			goto out;
149566f0dc48SHugh Dickins 	}
149666f0dc48SHugh Dickins 
149766f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
149866f0dc48SHugh Dickins 
1499ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
150066f0dc48SHugh Dickins 
150166f0dc48SHugh Dickins 	if (file)
150266f0dc48SHugh Dickins 		fput(file);
150366f0dc48SHugh Dickins out:
150466f0dc48SHugh Dickins 	return retval;
150566f0dc48SHugh Dickins }
150666f0dc48SHugh Dickins 
1507a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1508a4679373SChristoph Hellwig struct mmap_arg_struct {
1509a4679373SChristoph Hellwig 	unsigned long addr;
1510a4679373SChristoph Hellwig 	unsigned long len;
1511a4679373SChristoph Hellwig 	unsigned long prot;
1512a4679373SChristoph Hellwig 	unsigned long flags;
1513a4679373SChristoph Hellwig 	unsigned long fd;
1514a4679373SChristoph Hellwig 	unsigned long offset;
1515a4679373SChristoph Hellwig };
1516a4679373SChristoph Hellwig 
1517a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1518a4679373SChristoph Hellwig {
1519a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1520a4679373SChristoph Hellwig 
1521a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1522a4679373SChristoph Hellwig 		return -EFAULT;
1523a4679373SChristoph Hellwig 	if (a.offset & ~PAGE_MASK)
1524a4679373SChristoph Hellwig 		return -EINVAL;
1525a4679373SChristoph Hellwig 
1526a4679373SChristoph Hellwig 	return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1527a4679373SChristoph Hellwig 			      a.offset >> PAGE_SHIFT);
1528a4679373SChristoph Hellwig }
1529a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1530a4679373SChristoph Hellwig 
15311da177e4SLinus Torvalds /*
15328feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
15338feae131SDavid Howells  * for the first part or the tail.
15341da177e4SLinus Torvalds  */
15358feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
15368feae131SDavid Howells 	      unsigned long addr, int new_below)
15371da177e4SLinus Torvalds {
15388feae131SDavid Howells 	struct vm_area_struct *new;
15398feae131SDavid Howells 	struct vm_region *region;
15408feae131SDavid Howells 	unsigned long npages;
15411da177e4SLinus Torvalds 
15428feae131SDavid Howells 	kenter("");
15431da177e4SLinus Torvalds 
1544779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1545779c1023SDavid Howells 	 * only a single usage on the region) */
1546779c1023SDavid Howells 	if (vma->vm_file)
15478feae131SDavid Howells 		return -ENOMEM;
15481da177e4SLinus Torvalds 
15498feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
15508feae131SDavid Howells 		return -ENOMEM;
15511da177e4SLinus Torvalds 
15528feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
15538feae131SDavid Howells 	if (!region)
15548feae131SDavid Howells 		return -ENOMEM;
15558feae131SDavid Howells 
15568feae131SDavid Howells 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
15578feae131SDavid Howells 	if (!new) {
15588feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
15598feae131SDavid Howells 		return -ENOMEM;
15601da177e4SLinus Torvalds 	}
15611da177e4SLinus Torvalds 
15628feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
15638feae131SDavid Howells 	*new = *vma;
15648feae131SDavid Howells 	*region = *vma->vm_region;
15658feae131SDavid Howells 	new->vm_region = region;
15668feae131SDavid Howells 
15678feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
15688feae131SDavid Howells 
15698feae131SDavid Howells 	if (new_below) {
1570dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
15718feae131SDavid Howells 	} else {
15728feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
15738feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
15741da177e4SLinus Torvalds 	}
15758feae131SDavid Howells 
15768feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
15778feae131SDavid Howells 		new->vm_ops->open(new);
15788feae131SDavid Howells 
15798feae131SDavid Howells 	delete_vma_from_mm(vma);
15808feae131SDavid Howells 	down_write(&nommu_region_sem);
15818feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
15828feae131SDavid Howells 	if (new_below) {
15838feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
15848feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
15858feae131SDavid Howells 	} else {
15868feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1587dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
15888feae131SDavid Howells 	}
15898feae131SDavid Howells 	add_nommu_region(vma->vm_region);
15908feae131SDavid Howells 	add_nommu_region(new->vm_region);
15918feae131SDavid Howells 	up_write(&nommu_region_sem);
15928feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15938feae131SDavid Howells 	add_vma_to_mm(mm, new);
15948feae131SDavid Howells 	return 0;
15958feae131SDavid Howells }
15968feae131SDavid Howells 
15978feae131SDavid Howells /*
15988feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
15998feae131SDavid Howells  * the end
16008feae131SDavid Howells  */
16018feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
16028feae131SDavid Howells 		      struct vm_area_struct *vma,
16038feae131SDavid Howells 		      unsigned long from, unsigned long to)
16048feae131SDavid Howells {
16058feae131SDavid Howells 	struct vm_region *region;
16068feae131SDavid Howells 
16078feae131SDavid Howells 	kenter("");
16088feae131SDavid Howells 
16098feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
16108feae131SDavid Howells 	 * and list */
16118feae131SDavid Howells 	delete_vma_from_mm(vma);
16128feae131SDavid Howells 	if (from > vma->vm_start)
16138feae131SDavid Howells 		vma->vm_end = from;
16148feae131SDavid Howells 	else
16158feae131SDavid Howells 		vma->vm_start = to;
16168feae131SDavid Howells 	add_vma_to_mm(mm, vma);
16178feae131SDavid Howells 
16188feae131SDavid Howells 	/* cut the backing region down to size */
16198feae131SDavid Howells 	region = vma->vm_region;
16201e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
16218feae131SDavid Howells 
16228feae131SDavid Howells 	down_write(&nommu_region_sem);
16238feae131SDavid Howells 	delete_nommu_region(region);
1624dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1625dd8632a1SPaul Mundt 		to = region->vm_top;
1626dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1627dd8632a1SPaul Mundt 	} else {
16288feae131SDavid Howells 		region->vm_start = to;
1629dd8632a1SPaul Mundt 	}
16308feae131SDavid Howells 	add_nommu_region(region);
16318feae131SDavid Howells 	up_write(&nommu_region_sem);
16328feae131SDavid Howells 
16338feae131SDavid Howells 	free_page_series(from, to);
16348feae131SDavid Howells 	return 0;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
16373034097aSDavid Howells /*
16383034097aSDavid Howells  * release a mapping
16398feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
16408feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
16413034097aSDavid Howells  */
16428feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
16431da177e4SLinus Torvalds {
16448feae131SDavid Howells 	struct vm_area_struct *vma;
1645f67d9b15SBob Liu 	unsigned long end;
16468feae131SDavid Howells 	int ret;
16471da177e4SLinus Torvalds 
16488feae131SDavid Howells 	kenter(",%lx,%zx", start, len);
16491da177e4SLinus Torvalds 
1650f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
16518feae131SDavid Howells 	if (len == 0)
16521da177e4SLinus Torvalds 		return -EINVAL;
16531da177e4SLinus Torvalds 
1654f67d9b15SBob Liu 	end = start + len;
1655f67d9b15SBob Liu 
16568feae131SDavid Howells 	/* find the first potentially overlapping VMA */
16578feae131SDavid Howells 	vma = find_vma(mm, start);
16588feae131SDavid Howells 	if (!vma) {
165933e5d769SDavid Howells 		static int limit = 0;
166033e5d769SDavid Howells 		if (limit < 5) {
16618feae131SDavid Howells 			printk(KERN_WARNING
166233e5d769SDavid Howells 			       "munmap of memory not mmapped by process %d"
166333e5d769SDavid Howells 			       " (%s): 0x%lx-0x%lx\n",
166433e5d769SDavid Howells 			       current->pid, current->comm,
166533e5d769SDavid Howells 			       start, start + len - 1);
166633e5d769SDavid Howells 			limit++;
166733e5d769SDavid Howells 		}
16688feae131SDavid Howells 		return -EINVAL;
16698feae131SDavid Howells 	}
16701da177e4SLinus Torvalds 
16718feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
16728feae131SDavid Howells 	if (vma->vm_file) {
16738feae131SDavid Howells 		do {
16748feae131SDavid Howells 			if (start > vma->vm_start) {
16758feae131SDavid Howells 				kleave(" = -EINVAL [miss]");
16768feae131SDavid Howells 				return -EINVAL;
16778feae131SDavid Howells 			}
16788feae131SDavid Howells 			if (end == vma->vm_end)
16798feae131SDavid Howells 				goto erase_whole_vma;
1680d75a310cSNamhyung Kim 			vma = vma->vm_next;
1681d75a310cSNamhyung Kim 		} while (vma);
16828feae131SDavid Howells 		kleave(" = -EINVAL [split file]");
16838feae131SDavid Howells 		return -EINVAL;
16848feae131SDavid Howells 	} else {
16858feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
16868feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
16878feae131SDavid Howells 			goto erase_whole_vma;
16888feae131SDavid Howells 		if (start < vma->vm_start || end > vma->vm_end) {
16898feae131SDavid Howells 			kleave(" = -EINVAL [superset]");
16908feae131SDavid Howells 			return -EINVAL;
16918feae131SDavid Howells 		}
16928feae131SDavid Howells 		if (start & ~PAGE_MASK) {
16938feae131SDavid Howells 			kleave(" = -EINVAL [unaligned start]");
16948feae131SDavid Howells 			return -EINVAL;
16958feae131SDavid Howells 		}
16968feae131SDavid Howells 		if (end != vma->vm_end && end & ~PAGE_MASK) {
16978feae131SDavid Howells 			kleave(" = -EINVAL [unaligned split]");
16988feae131SDavid Howells 			return -EINVAL;
16998feae131SDavid Howells 		}
17008feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
17018feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
17028feae131SDavid Howells 			if (ret < 0) {
17038feae131SDavid Howells 				kleave(" = %d [split]", ret);
17048feae131SDavid Howells 				return ret;
17058feae131SDavid Howells 			}
17068feae131SDavid Howells 		}
17078feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
17088feae131SDavid Howells 	}
17091da177e4SLinus Torvalds 
17108feae131SDavid Howells erase_whole_vma:
17118feae131SDavid Howells 	delete_vma_from_mm(vma);
17128feae131SDavid Howells 	delete_vma(mm, vma);
17138feae131SDavid Howells 	kleave(" = 0");
17141da177e4SLinus Torvalds 	return 0;
17151da177e4SLinus Torvalds }
1716b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
17171da177e4SLinus Torvalds 
1718bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
17193034097aSDavid Howells {
1720bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
17213034097aSDavid Howells 	int ret;
17223034097aSDavid Howells 
17233034097aSDavid Howells 	down_write(&mm->mmap_sem);
17243034097aSDavid Howells 	ret = do_munmap(mm, addr, len);
17253034097aSDavid Howells 	up_write(&mm->mmap_sem);
17263034097aSDavid Howells 	return ret;
17273034097aSDavid Howells }
1728a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1729a46ef99dSLinus Torvalds 
1730a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1731a46ef99dSLinus Torvalds {
1732bfce281cSAl Viro 	return vm_munmap(addr, len);
1733a46ef99dSLinus Torvalds }
17343034097aSDavid Howells 
17353034097aSDavid Howells /*
17368feae131SDavid Howells  * release all the mappings made in a process's VM space
17373034097aSDavid Howells  */
17381da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
17391da177e4SLinus Torvalds {
17408feae131SDavid Howells 	struct vm_area_struct *vma;
17411da177e4SLinus Torvalds 
17428feae131SDavid Howells 	if (!mm)
17438feae131SDavid Howells 		return;
17448feae131SDavid Howells 
17458feae131SDavid Howells 	kenter("");
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	mm->total_vm = 0;
17481da177e4SLinus Torvalds 
17498feae131SDavid Howells 	while ((vma = mm->mmap)) {
17508feae131SDavid Howells 		mm->mmap = vma->vm_next;
17518feae131SDavid Howells 		delete_vma_from_mm(vma);
17528feae131SDavid Howells 		delete_vma(mm, vma);
175304c34961SSteven J. Magnani 		cond_resched();
17541da177e4SLinus Torvalds 	}
17551da177e4SLinus Torvalds 
17568feae131SDavid Howells 	kleave("");
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
1759e4eb1ff6SLinus Torvalds unsigned long vm_brk(unsigned long addr, unsigned long len)
17601da177e4SLinus Torvalds {
17611da177e4SLinus Torvalds 	return -ENOMEM;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds /*
17656fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
17666fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
17671da177e4SLinus Torvalds  *
17686fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
17698feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
17708feae131SDavid Howells  * block is not shareable
17711da177e4SLinus Torvalds  *
17726fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
17731da177e4SLinus Torvalds  */
17741da177e4SLinus Torvalds unsigned long do_mremap(unsigned long addr,
17751da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
17761da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
17771da177e4SLinus Torvalds {
17786fa5f80bSDavid Howells 	struct vm_area_struct *vma;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	/* insanity checks first */
1781f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1782f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
17838feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
17841da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17851da177e4SLinus Torvalds 
17868feae131SDavid Howells 	if (addr & ~PAGE_MASK)
17878feae131SDavid Howells 		return -EINVAL;
17888feae131SDavid Howells 
17891da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
17901da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17911da177e4SLinus Torvalds 
17928feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
17936fa5f80bSDavid Howells 	if (!vma)
17941da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
17951da177e4SLinus Torvalds 
17966fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
17971da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
17981da177e4SLinus Torvalds 
17996fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
18001da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
18011da177e4SLinus Torvalds 
18028feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
18031da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	/* all checks complete - do it */
18066fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
18076fa5f80bSDavid Howells 	return vma->vm_start;
18086fa5f80bSDavid Howells }
1809b5073173SPaul Mundt EXPORT_SYMBOL(do_mremap);
18106fa5f80bSDavid Howells 
18116a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
18126a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
18136a6160a7SHeiko Carstens 		unsigned long, new_addr)
18146fa5f80bSDavid Howells {
18156fa5f80bSDavid Howells 	unsigned long ret;
18166fa5f80bSDavid Howells 
18176fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
18186fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
18196fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
18206fa5f80bSDavid Howells 	return ret;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
1823240aadeeSMichel Lespinasse struct page *follow_page_mask(struct vm_area_struct *vma,
1824240aadeeSMichel Lespinasse 			      unsigned long address, unsigned int flags,
1825240aadeeSMichel Lespinasse 			      unsigned int *page_mask)
18261da177e4SLinus Torvalds {
1827240aadeeSMichel Lespinasse 	*page_mask = 0;
18281da177e4SLinus Torvalds 	return NULL;
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
18318f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
18328f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
18331da177e4SLinus Torvalds {
18348f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
18358f3b1327SBob Liu 		return -EINVAL;
18368f3b1327SBob Liu 
1837314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
183866aa2b4bSGreg Ungerer 	return 0;
18391da177e4SLinus Torvalds }
184022c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
18411da177e4SLinus Torvalds 
18423c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
18433c0b9de6SLinus Torvalds {
18443c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
18453c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
18463c0b9de6SLinus Torvalds 
18473c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
18483c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
18493c0b9de6SLinus Torvalds }
18503c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
18513c0b9de6SLinus Torvalds 
1852f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1853f905bc44SPaul Mundt 			unsigned long pgoff)
1854f905bc44SPaul Mundt {
1855f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1856f905bc44SPaul Mundt 
1857f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1858f905bc44SPaul Mundt 		return -EINVAL;
1859f905bc44SPaul Mundt 
1860f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1861f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1862f905bc44SPaul Mundt 
1863f905bc44SPaul Mundt 	return 0;
1864f905bc44SPaul Mundt }
1865f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1866f905bc44SPaul Mundt 
18671da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
18681da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
18691da177e4SLinus Torvalds {
18701da177e4SLinus Torvalds 	return -ENOMEM;
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
18731363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
18781da177e4SLinus Torvalds 			 loff_t const holebegin, loff_t const holelen,
18791da177e4SLinus Torvalds 			 int even_cows)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds }
188222c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range);
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds /*
18851da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
18861da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
18871da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
18881da177e4SLinus Torvalds  *
18891da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
18901da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
18911da177e4SLinus Torvalds  *
18921da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
18931da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
18941da177e4SLinus Torvalds  *
18951da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
18961da177e4SLinus Torvalds  *
18971da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
18981da177e4SLinus Torvalds  * wish to use this logic.
18991da177e4SLinus Torvalds  */
190034b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
19011da177e4SLinus Torvalds {
1902c9b1d098SAndrew Shewmaker 	unsigned long free, allowed, reserve;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	vm_acct_memory(pages);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	/*
19071da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
19081da177e4SLinus Torvalds 	 */
19091da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
19101da177e4SLinus Torvalds 		return 0;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1913c15bef30SDmitry Fink 		free = global_page_state(NR_FREE_PAGES);
1914c15bef30SDmitry Fink 		free += global_page_state(NR_FILE_PAGES);
19151da177e4SLinus Torvalds 
1916c15bef30SDmitry Fink 		/*
1917c15bef30SDmitry Fink 		 * shmem pages shouldn't be counted as free in this
1918c15bef30SDmitry Fink 		 * case, they can't be purged, only swapped out, and
1919c15bef30SDmitry Fink 		 * that won't affect the overall amount of available
1920c15bef30SDmitry Fink 		 * memory in the system.
1921c15bef30SDmitry Fink 		 */
1922c15bef30SDmitry Fink 		free -= global_page_state(NR_SHMEM);
1923c15bef30SDmitry Fink 
1924ec8acf20SShaohua Li 		free += get_nr_swap_pages();
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 		/*
19271da177e4SLinus Torvalds 		 * Any slabs which are created with the
19281da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
19291da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
19301da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
19311da177e4SLinus Torvalds 		 */
1932972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 		/*
1935c15bef30SDmitry Fink 		 * Leave reserved pages. The pages are not for anonymous pages.
1936c15bef30SDmitry Fink 		 */
1937c15bef30SDmitry Fink 		if (free <= totalreserve_pages)
1938c15bef30SDmitry Fink 			goto error;
1939c15bef30SDmitry Fink 		else
1940c15bef30SDmitry Fink 			free -= totalreserve_pages;
1941c15bef30SDmitry Fink 
1942c15bef30SDmitry Fink 		/*
1943*4eeab4f5SAndrew Shewmaker 		 * Reserve some for root
19441da177e4SLinus Torvalds 		 */
19451da177e4SLinus Torvalds 		if (!cap_sys_admin)
1946*4eeab4f5SAndrew Shewmaker 			free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 		if (free > pages)
19491da177e4SLinus Torvalds 			return 0;
19501da177e4SLinus Torvalds 
1951d5ddc79bSHideo AOKI 		goto error;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	allowed = totalram_pages * sysctl_overcommit_ratio / 100;
19551da177e4SLinus Torvalds 	/*
1956*4eeab4f5SAndrew Shewmaker 	 * Reserve some 3% for root
19571da177e4SLinus Torvalds 	 */
19581da177e4SLinus Torvalds 	if (!cap_sys_admin)
1959*4eeab4f5SAndrew Shewmaker 		allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
19601da177e4SLinus Torvalds 	allowed += total_swap_pages;
19611da177e4SLinus Torvalds 
1962c9b1d098SAndrew Shewmaker 	/*
1963c9b1d098SAndrew Shewmaker 	 * Don't let a single process grow so big a user can't recover
1964c9b1d098SAndrew Shewmaker 	 */
1965c9b1d098SAndrew Shewmaker 	if (mm) {
1966c9b1d098SAndrew Shewmaker 		reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1967c9b1d098SAndrew Shewmaker 		allowed -= min(mm->total_vm / 32, reserve);
1968c9b1d098SAndrew Shewmaker 	}
19691da177e4SLinus Torvalds 
197000a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
19711da177e4SLinus Torvalds 		return 0;
197200a62ce9SKOSAKI Motohiro 
1973d5ddc79bSHideo AOKI error:
19741da177e4SLinus Torvalds 	vm_unacct_memory(pages);
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	return -ENOMEM;
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
1979cae5d390SStephen Wilson int in_gate_area_no_mm(unsigned long addr)
19801da177e4SLinus Torvalds {
19811da177e4SLinus Torvalds 	return 0;
19821da177e4SLinus Torvalds }
1983b0e15190SDavid Howells 
1984d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1985b0e15190SDavid Howells {
1986b0e15190SDavid Howells 	BUG();
1987d0217ac0SNick Piggin 	return 0;
1988b0e15190SDavid Howells }
1989b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
19900ec76a11SDavid Howells 
19910b173bc4SKonstantin Khlebnikov int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
19920b173bc4SKonstantin Khlebnikov 			     unsigned long size, pgoff_t pgoff)
19930b173bc4SKonstantin Khlebnikov {
19940b173bc4SKonstantin Khlebnikov 	BUG();
19950b173bc4SKonstantin Khlebnikov 	return 0;
19960b173bc4SKonstantin Khlebnikov }
19970b173bc4SKonstantin Khlebnikov EXPORT_SYMBOL(generic_file_remap_pages);
19980b173bc4SKonstantin Khlebnikov 
1999f55f199bSMike Frysinger static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2000f55f199bSMike Frysinger 		unsigned long addr, void *buf, int len, int write)
20010ec76a11SDavid Howells {
20020ec76a11SDavid Howells 	struct vm_area_struct *vma;
20030ec76a11SDavid Howells 
20040ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
20050ec76a11SDavid Howells 
20060ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
20070159b141SDavid Howells 	vma = find_vma(mm, addr);
20080159b141SDavid Howells 	if (vma) {
20090ec76a11SDavid Howells 		/* don't overrun this mapping */
20100ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
20110ec76a11SDavid Howells 			len = vma->vm_end - addr;
20120ec76a11SDavid Howells 
20130ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
2014d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
20157959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
20167959722bSJie Zhang 					 (void *) addr, buf, len);
2017d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
20187959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
20197959722bSJie Zhang 					    buf, (void *) addr, len);
20200ec76a11SDavid Howells 		else
20210ec76a11SDavid Howells 			len = 0;
20220ec76a11SDavid Howells 	} else {
20230ec76a11SDavid Howells 		len = 0;
20240ec76a11SDavid Howells 	}
20250ec76a11SDavid Howells 
20260ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
2027f55f199bSMike Frysinger 
2028f55f199bSMike Frysinger 	return len;
2029f55f199bSMike Frysinger }
2030f55f199bSMike Frysinger 
2031f55f199bSMike Frysinger /**
2032f55f199bSMike Frysinger  * @access_remote_vm - access another process' address space
2033f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
2034f55f199bSMike Frysinger  * @addr:	start address to access
2035f55f199bSMike Frysinger  * @buf:	source or destination buffer
2036f55f199bSMike Frysinger  * @len:	number of bytes to transfer
2037f55f199bSMike Frysinger  * @write:	whether the access is a write
2038f55f199bSMike Frysinger  *
2039f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
2040f55f199bSMike Frysinger  */
2041f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2042f55f199bSMike Frysinger 		void *buf, int len, int write)
2043f55f199bSMike Frysinger {
2044f55f199bSMike Frysinger 	return __access_remote_vm(NULL, mm, addr, buf, len, write);
2045f55f199bSMike Frysinger }
2046f55f199bSMike Frysinger 
2047f55f199bSMike Frysinger /*
2048f55f199bSMike Frysinger  * Access another process' address space.
2049f55f199bSMike Frysinger  * - source/target buffer must be kernel space
2050f55f199bSMike Frysinger  */
2051f55f199bSMike Frysinger int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2052f55f199bSMike Frysinger {
2053f55f199bSMike Frysinger 	struct mm_struct *mm;
2054f55f199bSMike Frysinger 
2055f55f199bSMike Frysinger 	if (addr + len < addr)
2056f55f199bSMike Frysinger 		return 0;
2057f55f199bSMike Frysinger 
2058f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
2059f55f199bSMike Frysinger 	if (!mm)
2060f55f199bSMike Frysinger 		return 0;
2061f55f199bSMike Frysinger 
2062f55f199bSMike Frysinger 	len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2063f55f199bSMike Frysinger 
20640ec76a11SDavid Howells 	mmput(mm);
20650ec76a11SDavid Howells 	return len;
20660ec76a11SDavid Howells }
20677e660872SDavid Howells 
20687e660872SDavid Howells /**
20697e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
20707e660872SDavid Howells  * @inode: The inode to check
20717e660872SDavid Howells  * @size: The current filesize of the inode
20727e660872SDavid Howells  * @newsize: The proposed filesize of the inode
20737e660872SDavid Howells  *
20747e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
20757e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
20767e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
20777e660872SDavid Howells  * automatically grant mappings that are too large.
20787e660872SDavid Howells  */
20797e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
20807e660872SDavid Howells 				size_t newsize)
20817e660872SDavid Howells {
20827e660872SDavid Howells 	struct vm_area_struct *vma;
20837e660872SDavid Howells 	struct vm_region *region;
20847e660872SDavid Howells 	pgoff_t low, high;
20857e660872SDavid Howells 	size_t r_size, r_top;
20867e660872SDavid Howells 
20877e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
20887e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
20897e660872SDavid Howells 
20907e660872SDavid Howells 	down_write(&nommu_region_sem);
2091918e556eSDavid Howells 	mutex_lock(&inode->i_mapping->i_mmap_mutex);
20927e660872SDavid Howells 
20937e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
20946b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
20957e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
20967e660872SDavid Howells 		 * cache */
20977e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
2098918e556eSDavid Howells 			mutex_unlock(&inode->i_mapping->i_mmap_mutex);
20997e660872SDavid Howells 			up_write(&nommu_region_sem);
21007e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
21017e660872SDavid Howells 		}
21027e660872SDavid Howells 	}
21037e660872SDavid Howells 
21047e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
21057e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
21067e660872SDavid Howells 	 *
21077e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
21087e660872SDavid Howells 	 * shouldn't be any
21097e660872SDavid Howells 	 */
21106b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
21117e660872SDavid Howells 				  0, ULONG_MAX) {
21127e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
21137e660872SDavid Howells 			continue;
21147e660872SDavid Howells 
21157e660872SDavid Howells 		region = vma->vm_region;
21167e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
21177e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
21187e660872SDavid Howells 
21197e660872SDavid Howells 		if (r_top > newsize) {
21207e660872SDavid Howells 			region->vm_top -= r_top - newsize;
21217e660872SDavid Howells 			if (region->vm_end > region->vm_top)
21227e660872SDavid Howells 				region->vm_end = region->vm_top;
21237e660872SDavid Howells 		}
21247e660872SDavid Howells 	}
21257e660872SDavid Howells 
2126918e556eSDavid Howells 	mutex_unlock(&inode->i_mapping->i_mmap_mutex);
21277e660872SDavid Howells 	up_write(&nommu_region_sem);
21287e660872SDavid Howells 	return 0;
21297e660872SDavid Howells }
2130c9b1d098SAndrew Shewmaker 
2131c9b1d098SAndrew Shewmaker /*
2132c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
2133c9b1d098SAndrew Shewmaker  *
2134c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
2135c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2136c9b1d098SAndrew Shewmaker  * mode.
2137c9b1d098SAndrew Shewmaker  *
2138c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
2139c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
2140c9b1d098SAndrew Shewmaker  */
2141c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
2142c9b1d098SAndrew Shewmaker {
2143c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
2144c9b1d098SAndrew Shewmaker 
2145c9b1d098SAndrew Shewmaker 	free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2146c9b1d098SAndrew Shewmaker 
2147c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2148c9b1d098SAndrew Shewmaker 	return 0;
2149c9b1d098SAndrew Shewmaker }
2150c9b1d098SAndrew Shewmaker module_init(init_user_reserve)
2151*4eeab4f5SAndrew Shewmaker 
2152*4eeab4f5SAndrew Shewmaker /*
2153*4eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
2154*4eeab4f5SAndrew Shewmaker  *
2155*4eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2156*4eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
2157*4eeab4f5SAndrew Shewmaker  *
2158*4eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
2159*4eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2160*4eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
2161*4eeab4f5SAndrew Shewmaker  */
2162*4eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
2163*4eeab4f5SAndrew Shewmaker {
2164*4eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
2165*4eeab4f5SAndrew Shewmaker 
2166*4eeab4f5SAndrew Shewmaker 	free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2167*4eeab4f5SAndrew Shewmaker 
2168*4eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2169*4eeab4f5SAndrew Shewmaker 	return 0;
2170*4eeab4f5SAndrew Shewmaker }
2171*4eeab4f5SAndrew Shewmaker module_init(init_admin_reserve)
2172