xref: /linux/mm/nommu.c (revision 66f0dc481e5b802ab363b979fc1753410c7d82b5)
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>
13eb6434d9SPaul Mundt  *  Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
16f2b8544fSDavid Howells #include <linux/module.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>
25fa8e26ccSRoland McGrath #include <linux/tracehook.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/personality.h>
301da177e4SLinus Torvalds #include <linux/security.h>
311da177e4SLinus Torvalds #include <linux/syscalls.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <asm/uaccess.h>
341da177e4SLinus Torvalds #include <asm/tlb.h>
351da177e4SLinus Torvalds #include <asm/tlbflush.h>
36eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
378feae131SDavid Howells #include "internal.h"
388feae131SDavid Howells 
398feae131SDavid Howells static inline __attribute__((format(printf, 1, 2)))
408feae131SDavid Howells void no_printk(const char *fmt, ...)
418feae131SDavid Howells {
428feae131SDavid Howells }
438feae131SDavid Howells 
448feae131SDavid Howells #if 0
458feae131SDavid Howells #define kenter(FMT, ...) \
468feae131SDavid Howells 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
478feae131SDavid Howells #define kleave(FMT, ...) \
488feae131SDavid Howells 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
498feae131SDavid Howells #define kdebug(FMT, ...) \
508feae131SDavid Howells 	printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
518feae131SDavid Howells #else
528feae131SDavid Howells #define kenter(FMT, ...) \
538feae131SDavid Howells 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
548feae131SDavid Howells #define kleave(FMT, ...) \
558feae131SDavid Howells 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
568feae131SDavid Howells #define kdebug(FMT, ...) \
578feae131SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
588feae131SDavid Howells #endif
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds void *high_memory;
611da177e4SLinus Torvalds struct page *mem_map;
621da177e4SLinus Torvalds unsigned long max_mapnr;
631da177e4SLinus Torvalds unsigned long num_physpages;
644266c97aSHugh Dickins unsigned long highest_memmap_pfn;
6500a62ce9SKOSAKI Motohiro struct percpu_counter vm_committed_as;
661da177e4SLinus Torvalds int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
671da177e4SLinus Torvalds int sysctl_overcommit_ratio = 50; /* default is 50% */
681da177e4SLinus Torvalds int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
69fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
701da177e4SLinus Torvalds int heap_stack_gap = 0;
711da177e4SLinus Torvalds 
7233e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
738feae131SDavid Howells 
741da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
756a04de6dSWu, Bryan EXPORT_SYMBOL(num_physpages);
761da177e4SLinus Torvalds 
778feae131SDavid Howells /* list of mapped, potentially shareable regions */
788feae131SDavid Howells static struct kmem_cache *vm_region_jar;
798feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
808feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
811da177e4SLinus Torvalds 
82f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
871da177e4SLinus Torvalds  * just what the caller asked for.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	struct page *page;
941da177e4SLinus Torvalds 
954016a139SMichael Hennerich 	/*
964016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
974016a139SMichael Hennerich 	 * return size of 0
984016a139SMichael Hennerich 	 */
995a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
1006cfd53fcSPaul Mundt 		return 0;
1016cfd53fcSPaul Mundt 
1026cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
1036cfd53fcSPaul Mundt 
1046cfd53fcSPaul Mundt 	/*
1056cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
1066cfd53fcSPaul Mundt 	 * kmalloc().
1076cfd53fcSPaul Mundt 	 */
1081da177e4SLinus Torvalds 	if (PageSlab(page))
1091da177e4SLinus Torvalds 		return ksize(objp);
1101da177e4SLinus Torvalds 
1116cfd53fcSPaul Mundt 	/*
112ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
113ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
114ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
115ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
116ab2e83eaSPaul Mundt 	 */
117ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
118ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
119ab2e83eaSPaul Mundt 
120ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
121ab2e83eaSPaul Mundt 		if (vma)
122ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
123ab2e83eaSPaul Mundt 	}
124ab2e83eaSPaul Mundt 
125ab2e83eaSPaul Mundt 	/*
1266cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1275a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1286cfd53fcSPaul Mundt 	 */
1295a1603beSPaul Mundt 	return PAGE_SIZE << compound_order(page);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
132b291f000SNick Piggin int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1334266c97aSHugh Dickins 		     unsigned long start, int nr_pages, unsigned int foll_flags,
1341da177e4SLinus Torvalds 		     struct page **pages, struct vm_area_struct **vmas)
1351da177e4SLinus Torvalds {
136910e46daSSonic Zhang 	struct vm_area_struct *vma;
1377b4d5b8bSDavid Howells 	unsigned long vm_flags;
1387b4d5b8bSDavid Howells 	int i;
1397b4d5b8bSDavid Howells 
1407b4d5b8bSDavid Howells 	/* calculate required read or write permissions.
14158fa879eSHugh Dickins 	 * If FOLL_FORCE is set, we only require the "MAY" flags.
1427b4d5b8bSDavid Howells 	 */
14358fa879eSHugh Dickins 	vm_flags  = (foll_flags & FOLL_WRITE) ?
14458fa879eSHugh Dickins 			(VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
14558fa879eSHugh Dickins 	vm_flags &= (foll_flags & FOLL_FORCE) ?
14658fa879eSHugh Dickins 			(VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1471da177e4SLinus Torvalds 
1489d73777eSPeter Zijlstra 	for (i = 0; i < nr_pages; i++) {
149910e46daSSonic Zhang 		vma = find_vma(mm, start);
150910e46daSSonic Zhang 		if (!vma)
1517b4d5b8bSDavid Howells 			goto finish_or_fault;
1527b4d5b8bSDavid Howells 
1537b4d5b8bSDavid Howells 		/* protect what we can, including chardevs */
1541c3aff1cSHugh Dickins 		if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
1551c3aff1cSHugh Dickins 		    !(vm_flags & vma->vm_flags))
1567b4d5b8bSDavid Howells 			goto finish_or_fault;
157910e46daSSonic Zhang 
1581da177e4SLinus Torvalds 		if (pages) {
1591da177e4SLinus Torvalds 			pages[i] = virt_to_page(start);
1601da177e4SLinus Torvalds 			if (pages[i])
1611da177e4SLinus Torvalds 				page_cache_get(pages[i]);
1621da177e4SLinus Torvalds 		}
1631da177e4SLinus Torvalds 		if (vmas)
164910e46daSSonic Zhang 			vmas[i] = vma;
1651da177e4SLinus Torvalds 		start += PAGE_SIZE;
1661da177e4SLinus Torvalds 	}
1677b4d5b8bSDavid Howells 
1687b4d5b8bSDavid Howells 	return i;
1697b4d5b8bSDavid Howells 
1707b4d5b8bSDavid Howells finish_or_fault:
1717b4d5b8bSDavid Howells 	return i ? : -EFAULT;
1721da177e4SLinus Torvalds }
173b291f000SNick Piggin 
174b291f000SNick Piggin /*
175b291f000SNick Piggin  * get a list of pages in an address range belonging to the specified process
176b291f000SNick Piggin  * and indicate the VMA that covers each page
177b291f000SNick Piggin  * - this is potentially dodgy as we may end incrementing the page count of a
178b291f000SNick Piggin  *   slab page or a secondary page from a compound page
179b291f000SNick Piggin  * - don't permit access to VMAs that don't support it, such as I/O mappings
180b291f000SNick Piggin  */
181b291f000SNick Piggin int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
1829d73777eSPeter Zijlstra 	unsigned long start, int nr_pages, int write, int force,
183b291f000SNick Piggin 	struct page **pages, struct vm_area_struct **vmas)
184b291f000SNick Piggin {
185b291f000SNick Piggin 	int flags = 0;
186b291f000SNick Piggin 
187b291f000SNick Piggin 	if (write)
18858fa879eSHugh Dickins 		flags |= FOLL_WRITE;
189b291f000SNick Piggin 	if (force)
19058fa879eSHugh Dickins 		flags |= FOLL_FORCE;
191b291f000SNick Piggin 
1929d73777eSPeter Zijlstra 	return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
193b291f000SNick Piggin }
19466aa2b4bSGreg Ungerer EXPORT_SYMBOL(get_user_pages);
19566aa2b4bSGreg Ungerer 
196dfc2f91aSPaul Mundt /**
197dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
198dfc2f91aSPaul Mundt  * @vma: memory mapping
199dfc2f91aSPaul Mundt  * @address: user virtual address
200dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
201dfc2f91aSPaul Mundt  *
202dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
203dfc2f91aSPaul Mundt  *
204dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
205dfc2f91aSPaul Mundt  */
206dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
207dfc2f91aSPaul Mundt 	unsigned long *pfn)
208dfc2f91aSPaul Mundt {
209dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
210dfc2f91aSPaul Mundt 		return -EINVAL;
211dfc2f91aSPaul Mundt 
212dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
213dfc2f91aSPaul Mundt 	return 0;
214dfc2f91aSPaul Mundt }
215dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
216dfc2f91aSPaul Mundt 
2171da177e4SLinus Torvalds DEFINE_RWLOCK(vmlist_lock);
2181da177e4SLinus Torvalds struct vm_struct *vmlist;
2191da177e4SLinus Torvalds 
220b3bdda02SChristoph Lameter void vfree(const void *addr)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	kfree(addr);
2231da177e4SLinus Torvalds }
224b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
2251da177e4SLinus Torvalds 
226dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	/*
2298518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
2308518609dSRobert P. J. Day 	 * returns only a logical address.
2311da177e4SLinus Torvalds 	 */
23284097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
2331da177e4SLinus Torvalds }
234b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
2351da177e4SLinus Torvalds 
236f905bc44SPaul Mundt void *vmalloc_user(unsigned long size)
237f905bc44SPaul Mundt {
238f905bc44SPaul Mundt 	void *ret;
239f905bc44SPaul Mundt 
240f905bc44SPaul Mundt 	ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
241f905bc44SPaul Mundt 			PAGE_KERNEL);
242f905bc44SPaul Mundt 	if (ret) {
243f905bc44SPaul Mundt 		struct vm_area_struct *vma;
244f905bc44SPaul Mundt 
245f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
246f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
247f905bc44SPaul Mundt 		if (vma)
248f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
249f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
250f905bc44SPaul Mundt 	}
251f905bc44SPaul Mundt 
252f905bc44SPaul Mundt 	return ret;
253f905bc44SPaul Mundt }
254f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
255f905bc44SPaul Mundt 
256b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	return virt_to_page(addr);
2591da177e4SLinus Torvalds }
260b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
2611da177e4SLinus Torvalds 
262b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
2651da177e4SLinus Torvalds }
266b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2711da177e4SLinus Torvalds 	return count;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	/* Don't allow overflow */
2771da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2781da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	memcpy(addr, buf, count);
2811da177e4SLinus Torvalds 	return(count);
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  *	vmalloc  -  allocate virtually continguos memory
2861da177e4SLinus Torvalds  *
2871da177e4SLinus Torvalds  *	@size:		allocation size
2881da177e4SLinus Torvalds  *
2891da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
2901da177e4SLinus Torvalds  *	allocator and map them into continguos kernel virtual space.
2911da177e4SLinus Torvalds  *
292c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2931da177e4SLinus Torvalds  *	use __vmalloc() instead.
2941da177e4SLinus Torvalds  */
2951da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
2981da177e4SLinus Torvalds }
299f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
300f6138882SAndrew Morton 
301f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
302f6138882SAndrew Morton {
303f6138882SAndrew Morton 	return vmalloc(size);
304f6138882SAndrew Morton }
305f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc_node);
3061da177e4SLinus Torvalds 
3071af446edSPaul Mundt #ifndef PAGE_KERNEL_EXEC
3081af446edSPaul Mundt # define PAGE_KERNEL_EXEC PAGE_KERNEL
3091af446edSPaul Mundt #endif
3101af446edSPaul Mundt 
3111af446edSPaul Mundt /**
3121af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
3131af446edSPaul Mundt  *	@size:		allocation size
3141af446edSPaul Mundt  *
3151af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
3161af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
3171af446edSPaul Mundt  *	executable kernel virtual space.
3181af446edSPaul Mundt  *
3191af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3201af446edSPaul Mundt  *	use __vmalloc() instead.
3211af446edSPaul Mundt  */
3221af446edSPaul Mundt 
3231af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3241af446edSPaul Mundt {
3251af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3261af446edSPaul Mundt }
3271af446edSPaul Mundt 
328b5073173SPaul Mundt /**
329b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3301da177e4SLinus Torvalds  *	@size:		allocation size
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
3331da177e4SLinus Torvalds  *	page level allocator and map them into continguos kernel virtual space.
3341da177e4SLinus Torvalds  */
3351da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
3381da177e4SLinus Torvalds }
339b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
340b5073173SPaul Mundt 
341b5073173SPaul Mundt /**
342b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
343b5073173SPaul Mundt  *	@size:		allocation size
344b5073173SPaul Mundt  *
345b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
346b5073173SPaul Mundt  * mapped to userspace without leaking data.
347f905bc44SPaul Mundt  *
348f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
349f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
350b5073173SPaul Mundt  */
351b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
352b5073173SPaul Mundt {
353f905bc44SPaul Mundt 	/*
354f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
355f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
356f905bc44SPaul Mundt 	 */
357f905bc44SPaul Mundt 	return vmalloc_user(size);
358b5073173SPaul Mundt }
359b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	BUG();
3641da177e4SLinus Torvalds 	return NULL;
3651da177e4SLinus Torvalds }
366b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3671da177e4SLinus Torvalds 
368b3bdda02SChristoph Lameter void vunmap(const void *addr)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	BUG();
3711da177e4SLinus Torvalds }
372b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3731da177e4SLinus Torvalds 
374eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
375eb6434d9SPaul Mundt {
376eb6434d9SPaul Mundt 	BUG();
377eb6434d9SPaul Mundt 	return NULL;
378eb6434d9SPaul Mundt }
379eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
380eb6434d9SPaul Mundt 
381eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
382eb6434d9SPaul Mundt {
383eb6434d9SPaul Mundt 	BUG();
384eb6434d9SPaul Mundt }
385eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
386eb6434d9SPaul Mundt 
387eb6434d9SPaul Mundt void vm_unmap_aliases(void)
388eb6434d9SPaul Mundt {
389eb6434d9SPaul Mundt }
390eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
391eb6434d9SPaul Mundt 
3921da177e4SLinus Torvalds /*
3931eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
3941eeb66a1SChristoph Hellwig  * have one.
3951eeb66a1SChristoph Hellwig  */
3961eeb66a1SChristoph Hellwig void  __attribute__((weak)) vmalloc_sync_all(void)
3971eeb66a1SChristoph Hellwig {
3981eeb66a1SChristoph Hellwig }
3991eeb66a1SChristoph Hellwig 
400b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
401b5073173SPaul Mundt 		   struct page *page)
402b5073173SPaul Mundt {
403b5073173SPaul Mundt 	return -EINVAL;
404b5073173SPaul Mundt }
405b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
406b5073173SPaul Mundt 
4071eeb66a1SChristoph Hellwig /*
4081da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4091da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4101da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4111da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4121da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4131da177e4SLinus Torvalds  */
4146a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
4191da177e4SLinus Torvalds 		return mm->brk;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (mm->brk == brk)
4221da177e4SLinus Torvalds 		return mm->brk;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	/*
4251da177e4SLinus Torvalds 	 * Always allow shrinking brk
4261da177e4SLinus Torvalds 	 */
4271da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4281da177e4SLinus Torvalds 		mm->brk = brk;
4291da177e4SLinus Torvalds 		return brk;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/*
4331da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4341da177e4SLinus Torvalds 	 */
4351da177e4SLinus Torvalds 	return mm->brk = brk;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4388feae131SDavid Howells /*
4398feae131SDavid Howells  * initialise the VMA and region record slabs
4408feae131SDavid Howells  */
4418feae131SDavid Howells void __init mmap_init(void)
4421da177e4SLinus Torvalds {
44300a62ce9SKOSAKI Motohiro 	int ret;
44400a62ce9SKOSAKI Motohiro 
44500a62ce9SKOSAKI Motohiro 	ret = percpu_counter_init(&vm_committed_as, 0);
44600a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
44733e5d769SDavid Howells 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
4488feae131SDavid Howells }
4491da177e4SLinus Torvalds 
4508feae131SDavid Howells /*
4518feae131SDavid Howells  * validate the region tree
4528feae131SDavid Howells  * - the caller must hold the region lock
4538feae131SDavid Howells  */
4548feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4558feae131SDavid Howells static noinline void validate_nommu_regions(void)
4568feae131SDavid Howells {
4578feae131SDavid Howells 	struct vm_region *region, *last;
4588feae131SDavid Howells 	struct rb_node *p, *lastp;
4591da177e4SLinus Torvalds 
4608feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
4618feae131SDavid Howells 	if (!lastp)
4628feae131SDavid Howells 		return;
4638feae131SDavid Howells 
4648feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
46533e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_end <= last->vm_start));
46633e5d769SDavid Howells 	BUG_ON(unlikely(last->vm_top < last->vm_end));
4678feae131SDavid Howells 
4688feae131SDavid Howells 	while ((p = rb_next(lastp))) {
4698feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
4708feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
4718feae131SDavid Howells 
47233e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_end <= region->vm_start));
47333e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_top < region->vm_end));
47433e5d769SDavid Howells 		BUG_ON(unlikely(region->vm_start < last->vm_top));
4758feae131SDavid Howells 
4768feae131SDavid Howells 		lastp = p;
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds }
4798feae131SDavid Howells #else
48033e5d769SDavid Howells static void validate_nommu_regions(void)
48133e5d769SDavid Howells {
48233e5d769SDavid Howells }
4838feae131SDavid Howells #endif
4848feae131SDavid Howells 
4858feae131SDavid Howells /*
4868feae131SDavid Howells  * add a region into the global tree
4878feae131SDavid Howells  */
4888feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4898feae131SDavid Howells {
4908feae131SDavid Howells 	struct vm_region *pregion;
4918feae131SDavid Howells 	struct rb_node **p, *parent;
4928feae131SDavid Howells 
4938feae131SDavid Howells 	validate_nommu_regions();
4948feae131SDavid Howells 
4958feae131SDavid Howells 	parent = NULL;
4968feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
4978feae131SDavid Howells 	while (*p) {
4988feae131SDavid Howells 		parent = *p;
4998feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5008feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5018feae131SDavid Howells 			p = &(*p)->rb_left;
5028feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5038feae131SDavid Howells 			p = &(*p)->rb_right;
5048feae131SDavid Howells 		else if (pregion == region)
5058feae131SDavid Howells 			return;
5068feae131SDavid Howells 		else
5078feae131SDavid Howells 			BUG();
5088feae131SDavid Howells 	}
5098feae131SDavid Howells 
5108feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5118feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5128feae131SDavid Howells 
5138feae131SDavid Howells 	validate_nommu_regions();
5148feae131SDavid Howells }
5158feae131SDavid Howells 
5168feae131SDavid Howells /*
5178feae131SDavid Howells  * delete a region from the global tree
5188feae131SDavid Howells  */
5198feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
5208feae131SDavid Howells {
5218feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5228feae131SDavid Howells 
5238feae131SDavid Howells 	validate_nommu_regions();
5248feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5258feae131SDavid Howells 	validate_nommu_regions();
5268feae131SDavid Howells }
5278feae131SDavid Howells 
5288feae131SDavid Howells /*
5298feae131SDavid Howells  * free a contiguous series of pages
5308feae131SDavid Howells  */
5318feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5328feae131SDavid Howells {
5338feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5348feae131SDavid Howells 		struct page *page = virt_to_page(from);
5358feae131SDavid Howells 
5368feae131SDavid Howells 		kdebug("- free %lx", from);
53733e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5388feae131SDavid Howells 		if (page_count(page) != 1)
53933e5d769SDavid Howells 			kdebug("free page %p: refcount not one: %d",
54033e5d769SDavid Howells 			       page, page_count(page));
5418feae131SDavid Howells 		put_page(page);
5428feae131SDavid Howells 	}
5438feae131SDavid Howells }
5448feae131SDavid Howells 
5458feae131SDavid Howells /*
5468feae131SDavid Howells  * release a reference to a region
54733e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
548dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5498feae131SDavid Howells  *   will equal vm_start
5508feae131SDavid Howells  */
5518feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5528feae131SDavid Howells 	__releases(nommu_region_sem)
5538feae131SDavid Howells {
5548feae131SDavid Howells 	kenter("%p{%d}", region, atomic_read(&region->vm_usage));
5558feae131SDavid Howells 
5568feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5578feae131SDavid Howells 
5588feae131SDavid Howells 	if (atomic_dec_and_test(&region->vm_usage)) {
559dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
5608feae131SDavid Howells 			delete_nommu_region(region);
5618feae131SDavid Howells 		up_write(&nommu_region_sem);
5628feae131SDavid Howells 
5638feae131SDavid Howells 		if (region->vm_file)
5648feae131SDavid Howells 			fput(region->vm_file);
5658feae131SDavid Howells 
5668feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
5678feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
5688feae131SDavid Howells 		if (region->vm_flags & VM_MAPPED_COPY) {
5698feae131SDavid Howells 			kdebug("free series");
570dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
5718feae131SDavid Howells 		}
5728feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
5738feae131SDavid Howells 	} else {
5748feae131SDavid Howells 		up_write(&nommu_region_sem);
5758feae131SDavid Howells 	}
5768feae131SDavid Howells }
5778feae131SDavid Howells 
5788feae131SDavid Howells /*
5798feae131SDavid Howells  * release a reference to a region
5808feae131SDavid Howells  */
5818feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
5828feae131SDavid Howells {
5838feae131SDavid Howells 	down_write(&nommu_region_sem);
5848feae131SDavid Howells 	__put_nommu_region(region);
5858feae131SDavid Howells }
5861da177e4SLinus Torvalds 
5873034097aSDavid Howells /*
588eb8cdec4SBernd Schmidt  * update protection on a vma
589eb8cdec4SBernd Schmidt  */
590eb8cdec4SBernd Schmidt static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
591eb8cdec4SBernd Schmidt {
592eb8cdec4SBernd Schmidt #ifdef CONFIG_MPU
593eb8cdec4SBernd Schmidt 	struct mm_struct *mm = vma->vm_mm;
594eb8cdec4SBernd Schmidt 	long start = vma->vm_start & PAGE_MASK;
595eb8cdec4SBernd Schmidt 	while (start < vma->vm_end) {
596eb8cdec4SBernd Schmidt 		protect_page(mm, start, flags);
597eb8cdec4SBernd Schmidt 		start += PAGE_SIZE;
598eb8cdec4SBernd Schmidt 	}
599eb8cdec4SBernd Schmidt 	update_protections(mm);
600eb8cdec4SBernd Schmidt #endif
601eb8cdec4SBernd Schmidt }
602eb8cdec4SBernd Schmidt 
603eb8cdec4SBernd Schmidt /*
6043034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
6058feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
6068feae131SDavid Howells  * page
6073034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
6083034097aSDavid Howells  */
6098feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
6103034097aSDavid Howells {
6118feae131SDavid Howells 	struct vm_area_struct *pvma, **pp;
6128feae131SDavid Howells 	struct address_space *mapping;
6138feae131SDavid Howells 	struct rb_node **p, *parent;
6143034097aSDavid Howells 
6158feae131SDavid Howells 	kenter(",%p", vma);
6168feae131SDavid Howells 
6178feae131SDavid Howells 	BUG_ON(!vma->vm_region);
6188feae131SDavid Howells 
6198feae131SDavid Howells 	mm->map_count++;
6208feae131SDavid Howells 	vma->vm_mm = mm;
6218feae131SDavid Howells 
622eb8cdec4SBernd Schmidt 	protect_vma(vma, vma->vm_flags);
623eb8cdec4SBernd Schmidt 
6248feae131SDavid Howells 	/* add the VMA to the mapping */
6258feae131SDavid Howells 	if (vma->vm_file) {
6268feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6278feae131SDavid Howells 
6288feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6298feae131SDavid Howells 		vma_prio_tree_insert(vma, &mapping->i_mmap);
6308feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
6318feae131SDavid Howells 	}
6328feae131SDavid Howells 
6338feae131SDavid Howells 	/* add the VMA to the tree */
6348feae131SDavid Howells 	parent = NULL;
6358feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
6368feae131SDavid Howells 	while (*p) {
6378feae131SDavid Howells 		parent = *p;
6388feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
6398feae131SDavid Howells 
6408feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
6418feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
6428feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
6438feae131SDavid Howells 			p = &(*p)->rb_left;
6448feae131SDavid Howells 		else if (vma->vm_start > pvma->vm_start)
6458feae131SDavid Howells 			p = &(*p)->rb_right;
6468feae131SDavid Howells 		else if (vma->vm_end < pvma->vm_end)
6478feae131SDavid Howells 			p = &(*p)->rb_left;
6488feae131SDavid Howells 		else if (vma->vm_end > pvma->vm_end)
6498feae131SDavid Howells 			p = &(*p)->rb_right;
6508feae131SDavid Howells 		else if (vma < pvma)
6518feae131SDavid Howells 			p = &(*p)->rb_left;
6528feae131SDavid Howells 		else if (vma > pvma)
6538feae131SDavid Howells 			p = &(*p)->rb_right;
6548feae131SDavid Howells 		else
6558feae131SDavid Howells 			BUG();
6568feae131SDavid Howells 	}
6578feae131SDavid Howells 
6588feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
6598feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
6608feae131SDavid Howells 
6618feae131SDavid Howells 	/* add VMA to the VMA list also */
6628feae131SDavid Howells 	for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
6638feae131SDavid Howells 		if (pvma->vm_start > vma->vm_start)
6643034097aSDavid Howells 			break;
6658feae131SDavid Howells 		if (pvma->vm_start < vma->vm_start)
6668feae131SDavid Howells 			continue;
6678feae131SDavid Howells 		if (pvma->vm_end < vma->vm_end)
6688feae131SDavid Howells 			break;
6698feae131SDavid Howells 	}
6703034097aSDavid Howells 
6718feae131SDavid Howells 	vma->vm_next = *pp;
6728feae131SDavid Howells 	*pp = vma;
6738feae131SDavid Howells }
6748feae131SDavid Howells 
6758feae131SDavid Howells /*
6768feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
6778feae131SDavid Howells  */
6788feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
6798feae131SDavid Howells {
6808feae131SDavid Howells 	struct vm_area_struct **pp;
6818feae131SDavid Howells 	struct address_space *mapping;
6828feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
6838feae131SDavid Howells 
6848feae131SDavid Howells 	kenter("%p", vma);
6858feae131SDavid Howells 
686eb8cdec4SBernd Schmidt 	protect_vma(vma, 0);
687eb8cdec4SBernd Schmidt 
6888feae131SDavid Howells 	mm->map_count--;
6898feae131SDavid Howells 	if (mm->mmap_cache == vma)
6908feae131SDavid Howells 		mm->mmap_cache = NULL;
6918feae131SDavid Howells 
6928feae131SDavid Howells 	/* remove the VMA from the mapping */
6938feae131SDavid Howells 	if (vma->vm_file) {
6948feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6958feae131SDavid Howells 
6968feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6978feae131SDavid Howells 		vma_prio_tree_remove(vma, &mapping->i_mmap);
6988feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
6998feae131SDavid Howells 	}
7008feae131SDavid Howells 
7018feae131SDavid Howells 	/* remove from the MM's tree and list */
7028feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
7038feae131SDavid Howells 	for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
7048feae131SDavid Howells 		if (*pp == vma) {
7058feae131SDavid Howells 			*pp = vma->vm_next;
7068feae131SDavid Howells 			break;
7078feae131SDavid Howells 		}
7088feae131SDavid Howells 	}
7098feae131SDavid Howells 
7108feae131SDavid Howells 	vma->vm_mm = NULL;
7118feae131SDavid Howells }
7128feae131SDavid Howells 
7138feae131SDavid Howells /*
7148feae131SDavid Howells  * destroy a VMA record
7158feae131SDavid Howells  */
7168feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
7178feae131SDavid Howells {
7188feae131SDavid Howells 	kenter("%p", vma);
7198feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
7208feae131SDavid Howells 		vma->vm_ops->close(vma);
7218feae131SDavid Howells 	if (vma->vm_file) {
7228feae131SDavid Howells 		fput(vma->vm_file);
7238feae131SDavid Howells 		if (vma->vm_flags & VM_EXECUTABLE)
7248feae131SDavid Howells 			removed_exe_file_vma(mm);
7258feae131SDavid Howells 	}
7268feae131SDavid Howells 	put_nommu_region(vma->vm_region);
7278feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
7283034097aSDavid Howells }
7293034097aSDavid Howells 
7303034097aSDavid Howells /*
7313034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
7323034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7333034097aSDavid Howells  */
7343034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
7353034097aSDavid Howells {
7368feae131SDavid Howells 	struct vm_area_struct *vma;
7378feae131SDavid Howells 	struct rb_node *n = mm->mm_rb.rb_node;
7383034097aSDavid Howells 
7398feae131SDavid Howells 	/* check the cache first */
7408feae131SDavid Howells 	vma = mm->mmap_cache;
7418feae131SDavid Howells 	if (vma && vma->vm_start <= addr && vma->vm_end > addr)
7428feae131SDavid Howells 		return vma;
7438feae131SDavid Howells 
7448feae131SDavid Howells 	/* trawl the tree (there may be multiple mappings in which addr
7458feae131SDavid Howells 	 * resides) */
7468feae131SDavid Howells 	for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
7478feae131SDavid Howells 		vma = rb_entry(n, struct vm_area_struct, vm_rb);
7488feae131SDavid Howells 		if (vma->vm_start > addr)
7498feae131SDavid Howells 			return NULL;
7508feae131SDavid Howells 		if (vma->vm_end > addr) {
7518feae131SDavid Howells 			mm->mmap_cache = vma;
7528feae131SDavid Howells 			return vma;
7533034097aSDavid Howells 		}
7548feae131SDavid Howells 	}
7553034097aSDavid Howells 
7563034097aSDavid Howells 	return NULL;
7573034097aSDavid Howells }
7583034097aSDavid Howells EXPORT_SYMBOL(find_vma);
7593034097aSDavid Howells 
7603034097aSDavid Howells /*
761930e652aSDavid Howells  * find a VMA
762930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
763930e652aSDavid Howells  */
764930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
765930e652aSDavid Howells {
766930e652aSDavid Howells 	return find_vma(mm, addr);
767930e652aSDavid Howells }
768930e652aSDavid Howells 
7698feae131SDavid Howells /*
7708feae131SDavid Howells  * expand a stack to a given address
7718feae131SDavid Howells  * - not supported under NOMMU conditions
7728feae131SDavid Howells  */
77357c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
77457c8f63eSGreg Ungerer {
77557c8f63eSGreg Ungerer 	return -ENOMEM;
77657c8f63eSGreg Ungerer }
77757c8f63eSGreg Ungerer 
778930e652aSDavid Howells /*
7796fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
7806fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7816fa5f80bSDavid Howells  */
7828feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
7838feae131SDavid Howells 					     unsigned long addr,
7848feae131SDavid Howells 					     unsigned long len)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	struct vm_area_struct *vma;
7878feae131SDavid Howells 	struct rb_node *n = mm->mm_rb.rb_node;
7888feae131SDavid Howells 	unsigned long end = addr + len;
7891da177e4SLinus Torvalds 
7908feae131SDavid Howells 	/* check the cache first */
7918feae131SDavid Howells 	vma = mm->mmap_cache;
7928feae131SDavid Howells 	if (vma && vma->vm_start == addr && vma->vm_end == end)
7931da177e4SLinus Torvalds 		return vma;
7948feae131SDavid Howells 
7958feae131SDavid Howells 	/* trawl the tree (there may be multiple mappings in which addr
7968feae131SDavid Howells 	 * resides) */
7978feae131SDavid Howells 	for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
7988feae131SDavid Howells 		vma = rb_entry(n, struct vm_area_struct, vm_rb);
7998feae131SDavid Howells 		if (vma->vm_start < addr)
8008feae131SDavid Howells 			continue;
8018feae131SDavid Howells 		if (vma->vm_start > addr)
8028feae131SDavid Howells 			return NULL;
8038feae131SDavid Howells 		if (vma->vm_end == end) {
8048feae131SDavid Howells 			mm->mmap_cache = vma;
8058feae131SDavid Howells 			return vma;
8068feae131SDavid Howells 		}
8071da177e4SLinus Torvalds 	}
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	return NULL;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8123034097aSDavid Howells /*
8131da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
8141da177e4SLinus Torvalds  * mapping we're capable of supporting
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
8171da177e4SLinus Torvalds 				 unsigned long addr,
8181da177e4SLinus Torvalds 				 unsigned long len,
8191da177e4SLinus Torvalds 				 unsigned long prot,
8201da177e4SLinus Torvalds 				 unsigned long flags,
8211da177e4SLinus Torvalds 				 unsigned long pgoff,
8221da177e4SLinus Torvalds 				 unsigned long *_capabilities)
8231da177e4SLinus Torvalds {
8248feae131SDavid Howells 	unsigned long capabilities, rlen;
8251da177e4SLinus Torvalds 	unsigned long reqprot = prot;
8261da177e4SLinus Torvalds 	int ret;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	/* do the simple checks first */
82906aab5a3SDavid Howells 	if (flags & MAP_FIXED) {
8301da177e4SLinus Torvalds 		printk(KERN_DEBUG
8311da177e4SLinus Torvalds 		       "%d: Can't do fixed-address/overlay mmap of RAM\n",
8321da177e4SLinus Torvalds 		       current->pid);
8331da177e4SLinus Torvalds 		return -EINVAL;
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
8371da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
8381da177e4SLinus Torvalds 		return -EINVAL;
8391da177e4SLinus Torvalds 
840f81cff0dSMike Frysinger 	if (!len)
8411da177e4SLinus Torvalds 		return -EINVAL;
8421da177e4SLinus Torvalds 
843f81cff0dSMike Frysinger 	/* Careful about overflows.. */
8448feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
8458feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
846f81cff0dSMike Frysinger 		return -ENOMEM;
847f81cff0dSMike Frysinger 
8481da177e4SLinus Torvalds 	/* offset overflow? */
8498feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
850f81cff0dSMike Frysinger 		return -EOVERFLOW;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (file) {
8531da177e4SLinus Torvalds 		/* validate file mapping requests */
8541da177e4SLinus Torvalds 		struct address_space *mapping;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		/* files must support mmap */
8571da177e4SLinus Torvalds 		if (!file->f_op || !file->f_op->mmap)
8581da177e4SLinus Torvalds 			return -ENODEV;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
8611da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
8621da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
8631da177e4SLinus Torvalds 		 */
8641da177e4SLinus Torvalds 		mapping = file->f_mapping;
8651da177e4SLinus Torvalds 		if (!mapping)
866e9536ae7SJosef Sipek 			mapping = file->f_path.dentry->d_inode->i_mapping;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 		capabilities = 0;
8691da177e4SLinus Torvalds 		if (mapping && mapping->backing_dev_info)
8701da177e4SLinus Torvalds 			capabilities = mapping->backing_dev_info->capabilities;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 		if (!capabilities) {
8731da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
8741da177e4SLinus Torvalds 			 * defaults */
875e9536ae7SJosef Sipek 			switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
8761da177e4SLinus Torvalds 			case S_IFREG:
8771da177e4SLinus Torvalds 			case S_IFBLK:
8781da177e4SLinus Torvalds 				capabilities = BDI_CAP_MAP_COPY;
8791da177e4SLinus Torvalds 				break;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 			case S_IFCHR:
8821da177e4SLinus Torvalds 				capabilities =
8831da177e4SLinus Torvalds 					BDI_CAP_MAP_DIRECT |
8841da177e4SLinus Torvalds 					BDI_CAP_READ_MAP |
8851da177e4SLinus Torvalds 					BDI_CAP_WRITE_MAP;
8861da177e4SLinus Torvalds 				break;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 			default:
8891da177e4SLinus Torvalds 				return -EINVAL;
8901da177e4SLinus Torvalds 			}
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
8941da177e4SLinus Torvalds 		 * device */
8951da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
8961da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
8971da177e4SLinus Torvalds 		if (!file->f_op->read)
8981da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
8991da177e4SLinus Torvalds 
90028d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
90128d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
90228d7a6aeSGraff Yang 			return -EACCES;
90328d7a6aeSGraff Yang 
9041da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
9051da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
9061da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
9071da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
9081da177e4SLinus Torvalds 				return -EACCES;
9091da177e4SLinus Torvalds 
910e9536ae7SJosef Sipek 			if (IS_APPEND(file->f_path.dentry->d_inode) &&
9111da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
9121da177e4SLinus Torvalds 				return -EACCES;
9131da177e4SLinus Torvalds 
914e9536ae7SJosef Sipek 			if (locks_verify_locked(file->f_path.dentry->d_inode))
9151da177e4SLinus Torvalds 				return -EAGAIN;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_DIRECT))
9181da177e4SLinus Torvalds 				return -ENODEV;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 			if (((prot & PROT_READ)  && !(capabilities & BDI_CAP_READ_MAP))  ||
9211da177e4SLinus Torvalds 			    ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
9221da177e4SLinus Torvalds 			    ((prot & PROT_EXEC)  && !(capabilities & BDI_CAP_EXEC_MAP))
9231da177e4SLinus Torvalds 			    ) {
9241da177e4SLinus Torvalds 				printk("MAP_SHARED not completely supported on !MMU\n");
9251da177e4SLinus Torvalds 				return -EINVAL;
9261da177e4SLinus Torvalds 			}
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
9291da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_COPY;
9301da177e4SLinus Torvalds 		}
9311da177e4SLinus Torvalds 		else {
9321da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
9331da177e4SLinus Torvalds 			 * allocate */
9341da177e4SLinus Torvalds 			if (!(capabilities & BDI_CAP_MAP_COPY))
9351da177e4SLinus Torvalds 				return -ENODEV;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
9381da177e4SLinus Torvalds 			 * shared with the backing device */
9391da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
9401da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
9441da177e4SLinus Torvalds 		 * mappings */
945e9536ae7SJosef Sipek 		if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
9461da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
9471da177e4SLinus Torvalds 				return -EPERM;
9481da177e4SLinus Torvalds 		}
9491da177e4SLinus Torvalds 		else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
9501da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
9511da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
9521da177e4SLinus Torvalds 				if (capabilities & BDI_CAP_EXEC_MAP)
9531da177e4SLinus Torvalds 					prot |= PROT_EXEC;
9541da177e4SLinus Torvalds 			}
9551da177e4SLinus Torvalds 		}
9561da177e4SLinus Torvalds 		else if ((prot & PROT_READ) &&
9571da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
9581da177e4SLinus Torvalds 			 !(capabilities & BDI_CAP_EXEC_MAP)
9591da177e4SLinus Torvalds 			 ) {
9601da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
9611da177e4SLinus Torvalds 			capabilities &= ~BDI_CAP_MAP_DIRECT;
9621da177e4SLinus Torvalds 		}
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 	else {
9651da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
9661da177e4SLinus Torvalds 		 * privately mapped
9671da177e4SLinus Torvalds 		 */
9681da177e4SLinus Torvalds 		capabilities = BDI_CAP_MAP_COPY;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
9711da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
9721da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
9731da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	/* allow the security API to have its say */
977ed032189SEric Paris 	ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
9781da177e4SLinus Torvalds 	if (ret < 0)
9791da177e4SLinus Torvalds 		return ret;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	/* looks okay */
9821da177e4SLinus Torvalds 	*_capabilities = capabilities;
9831da177e4SLinus Torvalds 	return 0;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds /*
9871da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
9881da177e4SLinus Torvalds  * now know into VMA flags
9891da177e4SLinus Torvalds  */
9901da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
9911da177e4SLinus Torvalds 					unsigned long prot,
9921da177e4SLinus Torvalds 					unsigned long flags,
9931da177e4SLinus Torvalds 					unsigned long capabilities)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	unsigned long vm_flags;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
9981da177e4SLinus Torvalds 	vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9991da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
10021da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
10031da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
10041da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 	else {
10071da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
10081da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
10091da177e4SLinus Torvalds 		 * romfs/cramfs */
10101da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
10111da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE | VM_SHARED;
10121da177e4SLinus Torvalds 		else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
10131da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
10141da177e4SLinus Torvalds 	}
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
10171da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
10181da177e4SLinus Torvalds 	 * with another untraced process
10191da177e4SLinus Torvalds 	 */
1020fa8e26ccSRoland McGrath 	if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
10211da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	return vm_flags;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds /*
10278feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
10288feae131SDavid Howells  * pins the storage)
10291da177e4SLinus Torvalds  */
10308feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	int ret;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1035dd8632a1SPaul Mundt 	if (ret == 0) {
1036dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
1037645d83c5SDavid Howells 		return 0;
1038dd8632a1SPaul Mundt 	}
10391da177e4SLinus Torvalds 	if (ret != -ENOSYS)
10401da177e4SLinus Torvalds 		return ret;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	/* getting an ENOSYS error indicates that direct mmap isn't
10431da177e4SLinus Torvalds 	 * possible (as opposed to tried but failed) so we'll fall
10441da177e4SLinus Torvalds 	 * through to making a private copy of the data and mapping
10451da177e4SLinus Torvalds 	 * that if we can */
10461da177e4SLinus Torvalds 	return -ENODEV;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /*
10501da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
10511da177e4SLinus Torvalds  */
10528feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
10538feae131SDavid Howells 			   struct vm_region *region,
1054645d83c5SDavid Howells 			   unsigned long len,
1055645d83c5SDavid Howells 			   unsigned long capabilities)
10561da177e4SLinus Torvalds {
10578feae131SDavid Howells 	struct page *pages;
10588feae131SDavid Howells 	unsigned long total, point, n, rlen;
10591da177e4SLinus Torvalds 	void *base;
10608feae131SDavid Howells 	int ret, order;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
10631da177e4SLinus Torvalds 	 * shared mappings on devices or memory
10641da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
10651da177e4SLinus Torvalds 	 */
1066645d83c5SDavid Howells 	if (capabilities & BDI_CAP_MAP_DIRECT) {
10671da177e4SLinus Torvalds 		ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
1068dd8632a1SPaul Mundt 		if (ret == 0) {
10691da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1070dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1071dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1072645d83c5SDavid Howells 			return 0;
10731da177e4SLinus Torvalds 		}
1074dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1075dd8632a1SPaul Mundt 			return ret;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
10781da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
10791da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
10828feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
10838feae131SDavid Howells 
10841da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
10851da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
10861da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
10871da177e4SLinus Torvalds 	 */
10888feae131SDavid Howells 	order = get_order(rlen);
10898feae131SDavid Howells 	kdebug("alloc order %d for %lx", order, len);
10908feae131SDavid Howells 
10918feae131SDavid Howells 	pages = alloc_pages(GFP_KERNEL, order);
10928feae131SDavid Howells 	if (!pages)
10931da177e4SLinus Torvalds 		goto enomem;
10941da177e4SLinus Torvalds 
10958feae131SDavid Howells 	total = 1 << order;
109633e5d769SDavid Howells 	atomic_long_add(total, &mmap_pages_allocated);
10971da177e4SLinus Torvalds 
10988feae131SDavid Howells 	point = rlen >> PAGE_SHIFT;
1099dd8632a1SPaul Mundt 
1100dd8632a1SPaul Mundt 	/* we allocated a power-of-2 sized page set, so we may want to trim off
1101dd8632a1SPaul Mundt 	 * the excess */
1102dd8632a1SPaul Mundt 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
11038feae131SDavid Howells 		while (total > point) {
11048feae131SDavid Howells 			order = ilog2(total - point);
11058feae131SDavid Howells 			n = 1 << order;
11068feae131SDavid Howells 			kdebug("shave %lu/%lu @%lu", n, total - point, total);
110733e5d769SDavid Howells 			atomic_long_sub(n, &mmap_pages_allocated);
11088feae131SDavid Howells 			total -= n;
11098feae131SDavid Howells 			set_page_refcounted(pages + total);
11108feae131SDavid Howells 			__free_pages(pages + total, order);
11118feae131SDavid Howells 		}
1112dd8632a1SPaul Mundt 	}
11138feae131SDavid Howells 
11148feae131SDavid Howells 	for (point = 1; point < total; point++)
11158feae131SDavid Howells 		set_page_refcounted(&pages[point]);
11168feae131SDavid Howells 
11178feae131SDavid Howells 	base = page_address(pages);
11188feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
11198feae131SDavid Howells 	region->vm_start = (unsigned long) base;
11208feae131SDavid Howells 	region->vm_end   = region->vm_start + rlen;
1121dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
11228feae131SDavid Howells 
11238feae131SDavid Howells 	vma->vm_start = region->vm_start;
11248feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	if (vma->vm_file) {
11271da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
11281da177e4SLinus Torvalds 		mm_segment_t old_fs;
11291da177e4SLinus Torvalds 		loff_t fpos;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
11321da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 		old_fs = get_fs();
11351da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
11368feae131SDavid Howells 		ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
11371da177e4SLinus Torvalds 		set_fs(old_fs);
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 		if (ret < 0)
11401da177e4SLinus Torvalds 			goto error_free;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 		/* clear the last little bit */
11438feae131SDavid Howells 		if (ret < rlen)
11448feae131SDavid Howells 			memset(base + ret, 0, rlen - ret);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	return 0;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds error_free:
11518feae131SDavid Howells 	free_page_series(region->vm_start, region->vm_end);
11528feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
11538feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1154dd8632a1SPaul Mundt 	region->vm_top   = 0;
11551da177e4SLinus Torvalds 	return ret;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds enomem:
115805ae6fa3SGreg Ungerer 	printk("Allocation of length %lu from process %d (%s) failed\n",
115905ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
11601da177e4SLinus Torvalds 	show_free_areas();
11611da177e4SLinus Torvalds 	return -ENOMEM;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds /*
11651da177e4SLinus Torvalds  * handle mapping creation for uClinux
11661da177e4SLinus Torvalds  */
11671da177e4SLinus Torvalds unsigned long do_mmap_pgoff(struct file *file,
11681da177e4SLinus Torvalds 			    unsigned long addr,
11691da177e4SLinus Torvalds 			    unsigned long len,
11701da177e4SLinus Torvalds 			    unsigned long prot,
11711da177e4SLinus Torvalds 			    unsigned long flags,
11721da177e4SLinus Torvalds 			    unsigned long pgoff)
11731da177e4SLinus Torvalds {
11748feae131SDavid Howells 	struct vm_area_struct *vma;
11758feae131SDavid Howells 	struct vm_region *region;
11761da177e4SLinus Torvalds 	struct rb_node *rb;
11778feae131SDavid Howells 	unsigned long capabilities, vm_flags, result;
11781da177e4SLinus Torvalds 	int ret;
11791da177e4SLinus Torvalds 
11808feae131SDavid Howells 	kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
11818feae131SDavid Howells 
11821da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
11831da177e4SLinus Torvalds 	 * mapping */
11841da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
11851da177e4SLinus Torvalds 				    &capabilities);
11868feae131SDavid Howells 	if (ret < 0) {
11878feae131SDavid Howells 		kleave(" = %d [val]", ret);
11881da177e4SLinus Torvalds 		return ret;
11898feae131SDavid Howells 	}
11901da177e4SLinus Torvalds 
119106aab5a3SDavid Howells 	/* we ignore the address hint */
119206aab5a3SDavid Howells 	addr = 0;
119306aab5a3SDavid Howells 
11941da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
11951da177e4SLinus Torvalds 	 * now know into VMA flags */
11961da177e4SLinus Torvalds 	vm_flags = determine_vm_flags(file, prot, flags, capabilities);
11971da177e4SLinus Torvalds 
11988feae131SDavid Howells 	/* we're going to need to record the mapping */
11998feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
12008feae131SDavid Howells 	if (!region)
12018feae131SDavid Howells 		goto error_getting_region;
12021da177e4SLinus Torvalds 
12038feae131SDavid Howells 	vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
12048feae131SDavid Howells 	if (!vma)
12058feae131SDavid Howells 		goto error_getting_vma;
12061da177e4SLinus Torvalds 
12078feae131SDavid Howells 	atomic_set(&region->vm_usage, 1);
12088feae131SDavid Howells 	region->vm_flags = vm_flags;
12098feae131SDavid Howells 	region->vm_pgoff = pgoff;
12108feae131SDavid Howells 
12118feae131SDavid Howells 	INIT_LIST_HEAD(&vma->anon_vma_node);
12128feae131SDavid Howells 	vma->vm_flags = vm_flags;
12138feae131SDavid Howells 	vma->vm_pgoff = pgoff;
12148feae131SDavid Howells 
12158feae131SDavid Howells 	if (file) {
12168feae131SDavid Howells 		region->vm_file = file;
12178feae131SDavid Howells 		get_file(file);
12188feae131SDavid Howells 		vma->vm_file = file;
12198feae131SDavid Howells 		get_file(file);
12208feae131SDavid Howells 		if (vm_flags & VM_EXECUTABLE) {
12218feae131SDavid Howells 			added_exe_file_vma(current->mm);
12228feae131SDavid Howells 			vma->vm_mm = current->mm;
12238feae131SDavid Howells 		}
12248feae131SDavid Howells 	}
12258feae131SDavid Howells 
12268feae131SDavid Howells 	down_write(&nommu_region_sem);
12278feae131SDavid Howells 
12288feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
12291da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
12308feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
12311da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
12321da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
12331da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
12341da177e4SLinus Torvalds 	 *   than here
12351da177e4SLinus Torvalds 	 */
12361da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
12378feae131SDavid Howells 		struct vm_region *pregion;
12388feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
12391da177e4SLinus Torvalds 
12408feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
12418feae131SDavid Howells 		pgend = pgoff + pglen;
1242165b2392SDavid Howells 
12438feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
12448feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
12451da177e4SLinus Torvalds 
12468feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
12471da177e4SLinus Torvalds 				continue;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
12508feae131SDavid Howells 			if (pregion->vm_file->f_path.dentry->d_inode !=
12518feae131SDavid Howells 			    file->f_path.dentry->d_inode)
12521da177e4SLinus Torvalds 				continue;
12531da177e4SLinus Torvalds 
12548feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
12551da177e4SLinus Torvalds 				continue;
12561da177e4SLinus Torvalds 
12578feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
12588feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
12598feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
12608feae131SDavid Howells 			if (pgoff >= rpgend)
12611da177e4SLinus Torvalds 				continue;
12621da177e4SLinus Torvalds 
12638feae131SDavid Howells 			/* handle inexactly overlapping matches between
12648feae131SDavid Howells 			 * mappings */
12658feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
12668feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
12678feae131SDavid Howells 				/* new mapping is not a subset of the region */
12681da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_DIRECT))
12691da177e4SLinus Torvalds 					goto sharing_violation;
12701da177e4SLinus Torvalds 				continue;
12711da177e4SLinus Torvalds 			}
12721da177e4SLinus Torvalds 
12738feae131SDavid Howells 			/* we've found a region we can share */
12748feae131SDavid Howells 			atomic_inc(&pregion->vm_usage);
12758feae131SDavid Howells 			vma->vm_region = pregion;
12768feae131SDavid Howells 			start = pregion->vm_start;
12778feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
12788feae131SDavid Howells 			vma->vm_start = start;
12798feae131SDavid Howells 			vma->vm_end = start + len;
12801da177e4SLinus Torvalds 
12818feae131SDavid Howells 			if (pregion->vm_flags & VM_MAPPED_COPY) {
12828feae131SDavid Howells 				kdebug("share copy");
12838feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
12848feae131SDavid Howells 			} else {
12858feae131SDavid Howells 				kdebug("share mmap");
12868feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
12878feae131SDavid Howells 				if (ret < 0) {
12888feae131SDavid Howells 					vma->vm_region = NULL;
12898feae131SDavid Howells 					vma->vm_start = 0;
12908feae131SDavid Howells 					vma->vm_end = 0;
12918feae131SDavid Howells 					atomic_dec(&pregion->vm_usage);
12928feae131SDavid Howells 					pregion = NULL;
12938feae131SDavid Howells 					goto error_just_free;
12941da177e4SLinus Torvalds 				}
12958feae131SDavid Howells 			}
12968feae131SDavid Howells 			fput(region->vm_file);
12978feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
12988feae131SDavid Howells 			region = pregion;
12998feae131SDavid Howells 			result = start;
13008feae131SDavid Howells 			goto share;
13018feae131SDavid Howells 		}
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
13041da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
13051da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
13061da177e4SLinus Torvalds 		 */
1307645d83c5SDavid Howells 		if (capabilities & BDI_CAP_MAP_DIRECT) {
13081da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
13091da177e4SLinus Torvalds 							     pgoff, flags);
13101da177e4SLinus Torvalds 			if (IS_ERR((void *) addr)) {
13111da177e4SLinus Torvalds 				ret = addr;
13121da177e4SLinus Torvalds 				if (ret != (unsigned long) -ENOSYS)
13138feae131SDavid Howells 					goto error_just_free;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
13161da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
13171da177e4SLinus Torvalds 				 * it */
13181da177e4SLinus Torvalds 				ret = (unsigned long) -ENODEV;
13191da177e4SLinus Torvalds 				if (!(capabilities & BDI_CAP_MAP_COPY))
13208feae131SDavid Howells 					goto error_just_free;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 				capabilities &= ~BDI_CAP_MAP_DIRECT;
13238feae131SDavid Howells 			} else {
13248feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
13258feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
13261da177e4SLinus Torvalds 			}
13271da177e4SLinus Torvalds 		}
13281da177e4SLinus Torvalds 	}
13291da177e4SLinus Torvalds 
13308feae131SDavid Howells 	vma->vm_region = region;
13311da177e4SLinus Torvalds 
1332645d83c5SDavid Howells 	/* set up the mapping
1333645d83c5SDavid Howells 	 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1334645d83c5SDavid Howells 	 */
13351da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
13368feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
13371da177e4SLinus Torvalds 	else
1338645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
13391da177e4SLinus Torvalds 	if (ret < 0)
1340645d83c5SDavid Howells 		goto error_just_free;
1341645d83c5SDavid Howells 	add_nommu_region(region);
13428feae131SDavid Howells 
1343ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
1344ea637639SJie Zhang 	if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1345ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1346ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1347ea637639SJie Zhang 
13481da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
13498feae131SDavid Howells 	result = vma->vm_start;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
13521da177e4SLinus Torvalds 
13538feae131SDavid Howells share:
13548feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
13551da177e4SLinus Torvalds 
13568feae131SDavid Howells 	up_write(&nommu_region_sem);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	if (prot & PROT_EXEC)
13598feae131SDavid Howells 		flush_icache_range(result, result + len);
13601da177e4SLinus Torvalds 
13618feae131SDavid Howells 	kleave(" = %lx", result);
13628feae131SDavid Howells 	return result;
13631da177e4SLinus Torvalds 
13648feae131SDavid Howells error_just_free:
13658feae131SDavid Howells 	up_write(&nommu_region_sem);
13668feae131SDavid Howells error:
136789a86402SDavid Howells 	if (region->vm_file)
13688feae131SDavid Howells 		fput(region->vm_file);
13698feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
137089a86402SDavid Howells 	if (vma->vm_file)
13718feae131SDavid Howells 		fput(vma->vm_file);
13728feae131SDavid Howells 	if (vma->vm_flags & VM_EXECUTABLE)
13738feae131SDavid Howells 		removed_exe_file_vma(vma->vm_mm);
13748feae131SDavid Howells 	kmem_cache_free(vm_area_cachep, vma);
13758feae131SDavid Howells 	kleave(" = %d", ret);
13761da177e4SLinus Torvalds 	return ret;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds sharing_violation:
13798feae131SDavid Howells 	up_write(&nommu_region_sem);
13808feae131SDavid Howells 	printk(KERN_WARNING "Attempt to share mismatched mappings\n");
13818feae131SDavid Howells 	ret = -EINVAL;
13828feae131SDavid Howells 	goto error;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds error_getting_vma:
13858feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
13868feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
13878feae131SDavid Howells 	       " from process %d failed\n",
13881da177e4SLinus Torvalds 	       len, current->pid);
13891da177e4SLinus Torvalds 	show_free_areas();
13901da177e4SLinus Torvalds 	return -ENOMEM;
13911da177e4SLinus Torvalds 
13928feae131SDavid Howells error_getting_region:
13938feae131SDavid Howells 	printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
13948feae131SDavid Howells 	       " from process %d failed\n",
13951da177e4SLinus Torvalds 	       len, current->pid);
13961da177e4SLinus Torvalds 	show_free_areas();
13971da177e4SLinus Torvalds 	return -ENOMEM;
13981da177e4SLinus Torvalds }
1399b5073173SPaul Mundt EXPORT_SYMBOL(do_mmap_pgoff);
14001da177e4SLinus Torvalds 
1401*66f0dc48SHugh Dickins SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1402*66f0dc48SHugh Dickins 		unsigned long, prot, unsigned long, flags,
1403*66f0dc48SHugh Dickins 		unsigned long, fd, unsigned long, pgoff)
1404*66f0dc48SHugh Dickins {
1405*66f0dc48SHugh Dickins 	struct file *file = NULL;
1406*66f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
1407*66f0dc48SHugh Dickins 
1408*66f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
1409*66f0dc48SHugh Dickins 		file = fget(fd);
1410*66f0dc48SHugh Dickins 		if (!file)
1411*66f0dc48SHugh Dickins 			goto out;
1412*66f0dc48SHugh Dickins 	}
1413*66f0dc48SHugh Dickins 
1414*66f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1415*66f0dc48SHugh Dickins 
1416*66f0dc48SHugh Dickins 	down_write(&current->mm->mmap_sem);
1417*66f0dc48SHugh Dickins 	retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1418*66f0dc48SHugh Dickins 	up_write(&current->mm->mmap_sem);
1419*66f0dc48SHugh Dickins 
1420*66f0dc48SHugh Dickins 	if (file)
1421*66f0dc48SHugh Dickins 		fput(file);
1422*66f0dc48SHugh Dickins out:
1423*66f0dc48SHugh Dickins 	return retval;
1424*66f0dc48SHugh Dickins }
1425*66f0dc48SHugh Dickins 
14261da177e4SLinus Torvalds /*
14278feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
14288feae131SDavid Howells  * for the first part or the tail.
14291da177e4SLinus Torvalds  */
14308feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
14318feae131SDavid Howells 	      unsigned long addr, int new_below)
14321da177e4SLinus Torvalds {
14338feae131SDavid Howells 	struct vm_area_struct *new;
14348feae131SDavid Howells 	struct vm_region *region;
14358feae131SDavid Howells 	unsigned long npages;
14361da177e4SLinus Torvalds 
14378feae131SDavid Howells 	kenter("");
14381da177e4SLinus Torvalds 
14398feae131SDavid Howells 	/* we're only permitted to split anonymous regions that have a single
14408feae131SDavid Howells 	 * owner */
14418feae131SDavid Howells 	if (vma->vm_file ||
14428feae131SDavid Howells 	    atomic_read(&vma->vm_region->vm_usage) != 1)
14438feae131SDavid Howells 		return -ENOMEM;
14441da177e4SLinus Torvalds 
14458feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
14468feae131SDavid Howells 		return -ENOMEM;
14471da177e4SLinus Torvalds 
14488feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
14498feae131SDavid Howells 	if (!region)
14508feae131SDavid Howells 		return -ENOMEM;
14518feae131SDavid Howells 
14528feae131SDavid Howells 	new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
14538feae131SDavid Howells 	if (!new) {
14548feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
14558feae131SDavid Howells 		return -ENOMEM;
14561da177e4SLinus Torvalds 	}
14571da177e4SLinus Torvalds 
14588feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
14598feae131SDavid Howells 	*new = *vma;
14608feae131SDavid Howells 	*region = *vma->vm_region;
14618feae131SDavid Howells 	new->vm_region = region;
14628feae131SDavid Howells 
14638feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
14648feae131SDavid Howells 
14658feae131SDavid Howells 	if (new_below) {
1466dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
14678feae131SDavid Howells 	} else {
14688feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
14698feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
14701da177e4SLinus Torvalds 	}
14718feae131SDavid Howells 
14728feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
14738feae131SDavid Howells 		new->vm_ops->open(new);
14748feae131SDavid Howells 
14758feae131SDavid Howells 	delete_vma_from_mm(vma);
14768feae131SDavid Howells 	down_write(&nommu_region_sem);
14778feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
14788feae131SDavid Howells 	if (new_below) {
14798feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
14808feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
14818feae131SDavid Howells 	} else {
14828feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1483dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
14848feae131SDavid Howells 	}
14858feae131SDavid Howells 	add_nommu_region(vma->vm_region);
14868feae131SDavid Howells 	add_nommu_region(new->vm_region);
14878feae131SDavid Howells 	up_write(&nommu_region_sem);
14888feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14898feae131SDavid Howells 	add_vma_to_mm(mm, new);
14908feae131SDavid Howells 	return 0;
14918feae131SDavid Howells }
14928feae131SDavid Howells 
14938feae131SDavid Howells /*
14948feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
14958feae131SDavid Howells  * the end
14968feae131SDavid Howells  */
14978feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
14988feae131SDavid Howells 		      struct vm_area_struct *vma,
14998feae131SDavid Howells 		      unsigned long from, unsigned long to)
15008feae131SDavid Howells {
15018feae131SDavid Howells 	struct vm_region *region;
15028feae131SDavid Howells 
15038feae131SDavid Howells 	kenter("");
15048feae131SDavid Howells 
15058feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
15068feae131SDavid Howells 	 * and list */
15078feae131SDavid Howells 	delete_vma_from_mm(vma);
15088feae131SDavid Howells 	if (from > vma->vm_start)
15098feae131SDavid Howells 		vma->vm_end = from;
15108feae131SDavid Howells 	else
15118feae131SDavid Howells 		vma->vm_start = to;
15128feae131SDavid Howells 	add_vma_to_mm(mm, vma);
15138feae131SDavid Howells 
15148feae131SDavid Howells 	/* cut the backing region down to size */
15158feae131SDavid Howells 	region = vma->vm_region;
15168feae131SDavid Howells 	BUG_ON(atomic_read(&region->vm_usage) != 1);
15178feae131SDavid Howells 
15188feae131SDavid Howells 	down_write(&nommu_region_sem);
15198feae131SDavid Howells 	delete_nommu_region(region);
1520dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1521dd8632a1SPaul Mundt 		to = region->vm_top;
1522dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1523dd8632a1SPaul Mundt 	} else {
15248feae131SDavid Howells 		region->vm_start = to;
1525dd8632a1SPaul Mundt 	}
15268feae131SDavid Howells 	add_nommu_region(region);
15278feae131SDavid Howells 	up_write(&nommu_region_sem);
15288feae131SDavid Howells 
15298feae131SDavid Howells 	free_page_series(from, to);
15308feae131SDavid Howells 	return 0;
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15333034097aSDavid Howells /*
15343034097aSDavid Howells  * release a mapping
15358feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
15368feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
15373034097aSDavid Howells  */
15388feae131SDavid Howells int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
15391da177e4SLinus Torvalds {
15408feae131SDavid Howells 	struct vm_area_struct *vma;
15418feae131SDavid Howells 	struct rb_node *rb;
15428feae131SDavid Howells 	unsigned long end = start + len;
15438feae131SDavid Howells 	int ret;
15441da177e4SLinus Torvalds 
15458feae131SDavid Howells 	kenter(",%lx,%zx", start, len);
15461da177e4SLinus Torvalds 
15478feae131SDavid Howells 	if (len == 0)
15481da177e4SLinus Torvalds 		return -EINVAL;
15491da177e4SLinus Torvalds 
15508feae131SDavid Howells 	/* find the first potentially overlapping VMA */
15518feae131SDavid Howells 	vma = find_vma(mm, start);
15528feae131SDavid Howells 	if (!vma) {
155333e5d769SDavid Howells 		static int limit = 0;
155433e5d769SDavid Howells 		if (limit < 5) {
15558feae131SDavid Howells 			printk(KERN_WARNING
155633e5d769SDavid Howells 			       "munmap of memory not mmapped by process %d"
155733e5d769SDavid Howells 			       " (%s): 0x%lx-0x%lx\n",
155833e5d769SDavid Howells 			       current->pid, current->comm,
155933e5d769SDavid Howells 			       start, start + len - 1);
156033e5d769SDavid Howells 			limit++;
156133e5d769SDavid Howells 		}
15628feae131SDavid Howells 		return -EINVAL;
15638feae131SDavid Howells 	}
15641da177e4SLinus Torvalds 
15658feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
15668feae131SDavid Howells 	if (vma->vm_file) {
15678feae131SDavid Howells 		do {
15688feae131SDavid Howells 			if (start > vma->vm_start) {
15698feae131SDavid Howells 				kleave(" = -EINVAL [miss]");
15708feae131SDavid Howells 				return -EINVAL;
15718feae131SDavid Howells 			}
15728feae131SDavid Howells 			if (end == vma->vm_end)
15738feae131SDavid Howells 				goto erase_whole_vma;
15748feae131SDavid Howells 			rb = rb_next(&vma->vm_rb);
15758feae131SDavid Howells 			vma = rb_entry(rb, struct vm_area_struct, vm_rb);
15768feae131SDavid Howells 		} while (rb);
15778feae131SDavid Howells 		kleave(" = -EINVAL [split file]");
15788feae131SDavid Howells 		return -EINVAL;
15798feae131SDavid Howells 	} else {
15808feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
15818feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
15828feae131SDavid Howells 			goto erase_whole_vma;
15838feae131SDavid Howells 		if (start < vma->vm_start || end > vma->vm_end) {
15848feae131SDavid Howells 			kleave(" = -EINVAL [superset]");
15858feae131SDavid Howells 			return -EINVAL;
15868feae131SDavid Howells 		}
15878feae131SDavid Howells 		if (start & ~PAGE_MASK) {
15888feae131SDavid Howells 			kleave(" = -EINVAL [unaligned start]");
15898feae131SDavid Howells 			return -EINVAL;
15908feae131SDavid Howells 		}
15918feae131SDavid Howells 		if (end != vma->vm_end && end & ~PAGE_MASK) {
15928feae131SDavid Howells 			kleave(" = -EINVAL [unaligned split]");
15938feae131SDavid Howells 			return -EINVAL;
15948feae131SDavid Howells 		}
15958feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
15968feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
15978feae131SDavid Howells 			if (ret < 0) {
15988feae131SDavid Howells 				kleave(" = %d [split]", ret);
15998feae131SDavid Howells 				return ret;
16008feae131SDavid Howells 			}
16018feae131SDavid Howells 		}
16028feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
16038feae131SDavid Howells 	}
16041da177e4SLinus Torvalds 
16058feae131SDavid Howells erase_whole_vma:
16068feae131SDavid Howells 	delete_vma_from_mm(vma);
16078feae131SDavid Howells 	delete_vma(mm, vma);
16088feae131SDavid Howells 	kleave(" = 0");
16091da177e4SLinus Torvalds 	return 0;
16101da177e4SLinus Torvalds }
1611b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
16121da177e4SLinus Torvalds 
16136a6160a7SHeiko Carstens SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
16143034097aSDavid Howells {
16153034097aSDavid Howells 	int ret;
16163034097aSDavid Howells 	struct mm_struct *mm = current->mm;
16173034097aSDavid Howells 
16183034097aSDavid Howells 	down_write(&mm->mmap_sem);
16193034097aSDavid Howells 	ret = do_munmap(mm, addr, len);
16203034097aSDavid Howells 	up_write(&mm->mmap_sem);
16213034097aSDavid Howells 	return ret;
16223034097aSDavid Howells }
16233034097aSDavid Howells 
16243034097aSDavid Howells /*
16258feae131SDavid Howells  * release all the mappings made in a process's VM space
16263034097aSDavid Howells  */
16271da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
16281da177e4SLinus Torvalds {
16298feae131SDavid Howells 	struct vm_area_struct *vma;
16301da177e4SLinus Torvalds 
16318feae131SDavid Howells 	if (!mm)
16328feae131SDavid Howells 		return;
16338feae131SDavid Howells 
16348feae131SDavid Howells 	kenter("");
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	mm->total_vm = 0;
16371da177e4SLinus Torvalds 
16388feae131SDavid Howells 	while ((vma = mm->mmap)) {
16398feae131SDavid Howells 		mm->mmap = vma->vm_next;
16408feae131SDavid Howells 		delete_vma_from_mm(vma);
16418feae131SDavid Howells 		delete_vma(mm, vma);
16421da177e4SLinus Torvalds 	}
16431da177e4SLinus Torvalds 
16448feae131SDavid Howells 	kleave("");
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds unsigned long do_brk(unsigned long addr, unsigned long len)
16481da177e4SLinus Torvalds {
16491da177e4SLinus Torvalds 	return -ENOMEM;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds /*
16536fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
16546fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
16551da177e4SLinus Torvalds  *
16566fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
16578feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
16588feae131SDavid Howells  * block is not shareable
16591da177e4SLinus Torvalds  *
16606fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
16611da177e4SLinus Torvalds  */
16621da177e4SLinus Torvalds unsigned long do_mremap(unsigned long addr,
16631da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
16641da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
16651da177e4SLinus Torvalds {
16666fa5f80bSDavid Howells 	struct vm_area_struct *vma;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	/* insanity checks first */
16698feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
16701da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16711da177e4SLinus Torvalds 
16728feae131SDavid Howells 	if (addr & ~PAGE_MASK)
16738feae131SDavid Howells 		return -EINVAL;
16748feae131SDavid Howells 
16751da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
16761da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16771da177e4SLinus Torvalds 
16788feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
16796fa5f80bSDavid Howells 	if (!vma)
16801da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16811da177e4SLinus Torvalds 
16826fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
16831da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
16841da177e4SLinus Torvalds 
16856fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
16861da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
16871da177e4SLinus Torvalds 
16888feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
16891da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	/* all checks complete - do it */
16926fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
16936fa5f80bSDavid Howells 	return vma->vm_start;
16946fa5f80bSDavid Howells }
1695b5073173SPaul Mundt EXPORT_SYMBOL(do_mremap);
16966fa5f80bSDavid Howells 
16976a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
16986a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
16996a6160a7SHeiko Carstens 		unsigned long, new_addr)
17006fa5f80bSDavid Howells {
17016fa5f80bSDavid Howells 	unsigned long ret;
17026fa5f80bSDavid Howells 
17036fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
17046fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
17056fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
17066fa5f80bSDavid Howells 	return ret;
17071da177e4SLinus Torvalds }
17081da177e4SLinus Torvalds 
17096aab341eSLinus Torvalds struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1710deceb6cdSHugh Dickins 			unsigned int foll_flags)
17111da177e4SLinus Torvalds {
17121da177e4SLinus Torvalds 	return NULL;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
17161da177e4SLinus Torvalds 		unsigned long to, unsigned long size, pgprot_t prot)
17171da177e4SLinus Torvalds {
171866aa2b4bSGreg Ungerer 	vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
171966aa2b4bSGreg Ungerer 	return 0;
17201da177e4SLinus Torvalds }
172122c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
17221da177e4SLinus Torvalds 
1723f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1724f905bc44SPaul Mundt 			unsigned long pgoff)
1725f905bc44SPaul Mundt {
1726f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1727f905bc44SPaul Mundt 
1728f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1729f905bc44SPaul Mundt 		return -EINVAL;
1730f905bc44SPaul Mundt 
1731f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1732f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1733f905bc44SPaul Mundt 
1734f905bc44SPaul Mundt 	return 0;
1735f905bc44SPaul Mundt }
1736f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1737f905bc44SPaul Mundt 
17381da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
17391da177e4SLinus Torvalds {
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
17431da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
17441da177e4SLinus Torvalds {
17451da177e4SLinus Torvalds 	return -ENOMEM;
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
17481363c3cdSWolfgang Wander void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
17491da177e4SLinus Torvalds {
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds void unmap_mapping_range(struct address_space *mapping,
17531da177e4SLinus Torvalds 			 loff_t const holebegin, loff_t const holelen,
17541da177e4SLinus Torvalds 			 int even_cows)
17551da177e4SLinus Torvalds {
17561da177e4SLinus Torvalds }
175722c4af40SLuke Yang EXPORT_SYMBOL(unmap_mapping_range);
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds /*
1760d56e03cdSDavid Howells  * ask for an unmapped area at which to create a mapping on a file
1761d56e03cdSDavid Howells  */
1762d56e03cdSDavid Howells unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1763d56e03cdSDavid Howells 				unsigned long len, unsigned long pgoff,
1764d56e03cdSDavid Howells 				unsigned long flags)
1765d56e03cdSDavid Howells {
1766d56e03cdSDavid Howells 	unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1767d56e03cdSDavid Howells 				  unsigned long, unsigned long);
1768d56e03cdSDavid Howells 
1769d56e03cdSDavid Howells 	get_area = current->mm->get_unmapped_area;
1770d56e03cdSDavid Howells 	if (file && file->f_op && file->f_op->get_unmapped_area)
1771d56e03cdSDavid Howells 		get_area = file->f_op->get_unmapped_area;
1772d56e03cdSDavid Howells 
1773d56e03cdSDavid Howells 	if (!get_area)
1774d56e03cdSDavid Howells 		return -ENOSYS;
1775d56e03cdSDavid Howells 
1776d56e03cdSDavid Howells 	return get_area(file, addr, len, pgoff, flags);
1777d56e03cdSDavid Howells }
1778d56e03cdSDavid Howells EXPORT_SYMBOL(get_unmapped_area);
1779d56e03cdSDavid Howells 
1780d56e03cdSDavid Howells /*
17811da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
17821da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
17831da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
17841da177e4SLinus Torvalds  *
17851da177e4SLinus Torvalds  * We currently support three overcommit policies, which are set via the
17861da177e4SLinus Torvalds  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
17871da177e4SLinus Torvalds  *
17881da177e4SLinus Torvalds  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
17891da177e4SLinus Torvalds  * Additional code 2002 Jul 20 by Robert Love.
17901da177e4SLinus Torvalds  *
17911da177e4SLinus Torvalds  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
17921da177e4SLinus Torvalds  *
17931da177e4SLinus Torvalds  * Note this is a helper function intended to be used by LSMs which
17941da177e4SLinus Torvalds  * wish to use this logic.
17951da177e4SLinus Torvalds  */
179634b4e4aaSAlan Cox int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
17971da177e4SLinus Torvalds {
17981da177e4SLinus Torvalds 	unsigned long free, allowed;
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	vm_acct_memory(pages);
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	/*
18031da177e4SLinus Torvalds 	 * Sometimes we want to use more memory than we have
18041da177e4SLinus Torvalds 	 */
18051da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
18061da177e4SLinus Torvalds 		return 0;
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
18091da177e4SLinus Torvalds 		unsigned long n;
18101da177e4SLinus Torvalds 
1811347ce434SChristoph Lameter 		free = global_page_state(NR_FILE_PAGES);
18121da177e4SLinus Torvalds 		free += nr_swap_pages;
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 		/*
18151da177e4SLinus Torvalds 		 * Any slabs which are created with the
18161da177e4SLinus Torvalds 		 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
18171da177e4SLinus Torvalds 		 * which are reclaimable, under pressure.  The dentry
18181da177e4SLinus Torvalds 		 * cache and most inode caches should fall into this
18191da177e4SLinus Torvalds 		 */
1820972d1a7bSChristoph Lameter 		free += global_page_state(NR_SLAB_RECLAIMABLE);
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 		/*
18231da177e4SLinus Torvalds 		 * Leave the last 3% for root
18241da177e4SLinus Torvalds 		 */
18251da177e4SLinus Torvalds 		if (!cap_sys_admin)
18261da177e4SLinus Torvalds 			free -= free / 32;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 		if (free > pages)
18291da177e4SLinus Torvalds 			return 0;
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 		/*
18321da177e4SLinus Torvalds 		 * nr_free_pages() is very expensive on large systems,
18331da177e4SLinus Torvalds 		 * only call if we're about to fail.
18341da177e4SLinus Torvalds 		 */
18351da177e4SLinus Torvalds 		n = nr_free_pages();
1836d5ddc79bSHideo AOKI 
1837d5ddc79bSHideo AOKI 		/*
1838d5ddc79bSHideo AOKI 		 * Leave reserved pages. The pages are not for anonymous pages.
1839d5ddc79bSHideo AOKI 		 */
1840d5ddc79bSHideo AOKI 		if (n <= totalreserve_pages)
1841d5ddc79bSHideo AOKI 			goto error;
1842d5ddc79bSHideo AOKI 		else
1843d5ddc79bSHideo AOKI 			n -= totalreserve_pages;
1844d5ddc79bSHideo AOKI 
1845d5ddc79bSHideo AOKI 		/*
1846d5ddc79bSHideo AOKI 		 * Leave the last 3% for root
1847d5ddc79bSHideo AOKI 		 */
18481da177e4SLinus Torvalds 		if (!cap_sys_admin)
18491da177e4SLinus Torvalds 			n -= n / 32;
18501da177e4SLinus Torvalds 		free += n;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 		if (free > pages)
18531da177e4SLinus Torvalds 			return 0;
1854d5ddc79bSHideo AOKI 
1855d5ddc79bSHideo AOKI 		goto error;
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	allowed = totalram_pages * sysctl_overcommit_ratio / 100;
18591da177e4SLinus Torvalds 	/*
18601da177e4SLinus Torvalds 	 * Leave the last 3% for root
18611da177e4SLinus Torvalds 	 */
18621da177e4SLinus Torvalds 	if (!cap_sys_admin)
18631da177e4SLinus Torvalds 		allowed -= allowed / 32;
18641da177e4SLinus Torvalds 	allowed += total_swap_pages;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	/* Don't let a single process grow too big:
18671da177e4SLinus Torvalds 	   leave 3% of the size of this process for other processes */
1868731572d3SAlan Cox 	if (mm)
1869731572d3SAlan Cox 		allowed -= mm->total_vm / 32;
18701da177e4SLinus Torvalds 
187100a62ce9SKOSAKI Motohiro 	if (percpu_counter_read_positive(&vm_committed_as) < allowed)
18721da177e4SLinus Torvalds 		return 0;
187300a62ce9SKOSAKI Motohiro 
1874d5ddc79bSHideo AOKI error:
18751da177e4SLinus Torvalds 	vm_unacct_memory(pages);
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	return -ENOMEM;
18781da177e4SLinus Torvalds }
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds int in_gate_area_no_task(unsigned long addr)
18811da177e4SLinus Torvalds {
18821da177e4SLinus Torvalds 	return 0;
18831da177e4SLinus Torvalds }
1884b0e15190SDavid Howells 
1885d0217ac0SNick Piggin int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1886b0e15190SDavid Howells {
1887b0e15190SDavid Howells 	BUG();
1888d0217ac0SNick Piggin 	return 0;
1889b0e15190SDavid Howells }
1890b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
18910ec76a11SDavid Howells 
18920ec76a11SDavid Howells /*
18930ec76a11SDavid Howells  * Access another process' address space.
18940ec76a11SDavid Howells  * - source/target buffer must be kernel space
18950ec76a11SDavid Howells  */
18960ec76a11SDavid Howells int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
18970ec76a11SDavid Howells {
18980ec76a11SDavid Howells 	struct vm_area_struct *vma;
18990ec76a11SDavid Howells 	struct mm_struct *mm;
19000ec76a11SDavid Howells 
19010ec76a11SDavid Howells 	if (addr + len < addr)
19020ec76a11SDavid Howells 		return 0;
19030ec76a11SDavid Howells 
19040ec76a11SDavid Howells 	mm = get_task_mm(tsk);
19050ec76a11SDavid Howells 	if (!mm)
19060ec76a11SDavid Howells 		return 0;
19070ec76a11SDavid Howells 
19080ec76a11SDavid Howells 	down_read(&mm->mmap_sem);
19090ec76a11SDavid Howells 
19100ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
19110159b141SDavid Howells 	vma = find_vma(mm, addr);
19120159b141SDavid Howells 	if (vma) {
19130ec76a11SDavid Howells 		/* don't overrun this mapping */
19140ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
19150ec76a11SDavid Howells 			len = vma->vm_end - addr;
19160ec76a11SDavid Howells 
19170ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1918d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
19190ec76a11SDavid Howells 			len -= copy_to_user((void *) addr, buf, len);
1920d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
19210ec76a11SDavid Howells 			len -= copy_from_user(buf, (void *) addr, len);
19220ec76a11SDavid Howells 		else
19230ec76a11SDavid Howells 			len = 0;
19240ec76a11SDavid Howells 	} else {
19250ec76a11SDavid Howells 		len = 0;
19260ec76a11SDavid Howells 	}
19270ec76a11SDavid Howells 
19280ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
19290ec76a11SDavid Howells 	mmput(mm);
19300ec76a11SDavid Howells 	return len;
19310ec76a11SDavid Howells }
1932