xref: /linux/mm/nommu.c (revision ed81745a4c96841937f1da35c0eb66ac312e1480)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/mm/nommu.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Replacement code for mm functions to support CPU's that don't
61da177e4SLinus Torvalds  *  have any form of memory management unit (thus no virtual memory).
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  See Documentation/nommu-mmap.txt
91da177e4SLinus Torvalds  *
108feae131SDavid Howells  *  Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
111da177e4SLinus Torvalds  *  Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
121da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
131da177e4SLinus Torvalds  *  Copyright (c) 2002      Greg Ungerer <gerg@snapgear.com>
1429c185e5SPaul Mundt  *  Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
17b1de0d13SMitchel Humpherys #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18b1de0d13SMitchel Humpherys 
19b95f1b31SPaul Gortmaker #include <linux/export.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
216e84f315SIngo Molnar #include <linux/sched/mm.h>
22615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
231da177e4SLinus Torvalds #include <linux/mman.h>
241da177e4SLinus Torvalds #include <linux/swap.h>
251da177e4SLinus Torvalds #include <linux/file.h>
261da177e4SLinus Torvalds #include <linux/highmem.h>
271da177e4SLinus Torvalds #include <linux/pagemap.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/vmalloc.h>
301da177e4SLinus Torvalds #include <linux/blkdev.h>
311da177e4SLinus Torvalds #include <linux/backing-dev.h>
323b32123dSGideon Israel Dsouza #include <linux/compiler.h>
331da177e4SLinus Torvalds #include <linux/mount.h>
341da177e4SLinus Torvalds #include <linux/personality.h>
351da177e4SLinus Torvalds #include <linux/security.h>
361da177e4SLinus Torvalds #include <linux/syscalls.h>
37120a795dSAl Viro #include <linux/audit.h>
38b1de0d13SMitchel Humpherys #include <linux/printk.h>
391da177e4SLinus Torvalds 
407c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
411da177e4SLinus Torvalds #include <asm/tlb.h>
421da177e4SLinus Torvalds #include <asm/tlbflush.h>
43eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
448feae131SDavid Howells #include "internal.h"
458feae131SDavid Howells 
461da177e4SLinus Torvalds void *high_memory;
47944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
481da177e4SLinus Torvalds struct page *mem_map;
491da177e4SLinus Torvalds unsigned long max_mapnr;
505b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
514266c97aSHugh Dickins unsigned long highest_memmap_pfn;
52fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
531da177e4SLinus Torvalds int heap_stack_gap = 0;
541da177e4SLinus Torvalds 
5533e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
568feae131SDavid Howells 
571da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
581da177e4SLinus Torvalds 
598feae131SDavid Howells /* list of mapped, potentially shareable regions */
608feae131SDavid Howells static struct kmem_cache *vm_region_jar;
618feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
628feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
631da177e4SLinus Torvalds 
64f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Return the total memory allocated for this pointer, not
691da177e4SLinus Torvalds  * just what the caller asked for.
701da177e4SLinus Torvalds  *
711da177e4SLinus Torvalds  * Doesn't have to be accurate, i.e. may have races.
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct page *page;
761da177e4SLinus Torvalds 
774016a139SMichael Hennerich 	/*
784016a139SMichael Hennerich 	 * If the object we have should not have ksize performed on it,
794016a139SMichael Hennerich 	 * return size of 0
804016a139SMichael Hennerich 	 */
815a1603beSPaul Mundt 	if (!objp || !virt_addr_valid(objp))
826cfd53fcSPaul Mundt 		return 0;
836cfd53fcSPaul Mundt 
846cfd53fcSPaul Mundt 	page = virt_to_head_page(objp);
856cfd53fcSPaul Mundt 
866cfd53fcSPaul Mundt 	/*
876cfd53fcSPaul Mundt 	 * If the allocator sets PageSlab, we know the pointer came from
886cfd53fcSPaul Mundt 	 * kmalloc().
896cfd53fcSPaul Mundt 	 */
901da177e4SLinus Torvalds 	if (PageSlab(page))
911da177e4SLinus Torvalds 		return ksize(objp);
921da177e4SLinus Torvalds 
936cfd53fcSPaul Mundt 	/*
94ab2e83eaSPaul Mundt 	 * If it's not a compound page, see if we have a matching VMA
95ab2e83eaSPaul Mundt 	 * region. This test is intentionally done in reverse order,
96ab2e83eaSPaul Mundt 	 * so if there's no VMA, we still fall through and hand back
97ab2e83eaSPaul Mundt 	 * PAGE_SIZE for 0-order pages.
98ab2e83eaSPaul Mundt 	 */
99ab2e83eaSPaul Mundt 	if (!PageCompound(page)) {
100ab2e83eaSPaul Mundt 		struct vm_area_struct *vma;
101ab2e83eaSPaul Mundt 
102ab2e83eaSPaul Mundt 		vma = find_vma(current->mm, (unsigned long)objp);
103ab2e83eaSPaul Mundt 		if (vma)
104ab2e83eaSPaul Mundt 			return vma->vm_end - vma->vm_start;
105ab2e83eaSPaul Mundt 	}
106ab2e83eaSPaul Mundt 
107ab2e83eaSPaul Mundt 	/*
1086cfd53fcSPaul Mundt 	 * The ksize() function is only guaranteed to work for pointers
1095a1603beSPaul Mundt 	 * returned by kmalloc(). So handle arbitrary pointers here.
1106cfd53fcSPaul Mundt 	 */
111a50b854eSMatthew Wilcox (Oracle) 	return page_size(page);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114dfc2f91aSPaul Mundt /**
115dfc2f91aSPaul Mundt  * follow_pfn - look up PFN at a user virtual address
116dfc2f91aSPaul Mundt  * @vma: memory mapping
117dfc2f91aSPaul Mundt  * @address: user virtual address
118dfc2f91aSPaul Mundt  * @pfn: location to store found PFN
119dfc2f91aSPaul Mundt  *
120dfc2f91aSPaul Mundt  * Only IO mappings and raw PFN mappings are allowed.
121dfc2f91aSPaul Mundt  *
122dfc2f91aSPaul Mundt  * Returns zero and the pfn at @pfn on success, -ve otherwise.
123dfc2f91aSPaul Mundt  */
124dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
125dfc2f91aSPaul Mundt 	unsigned long *pfn)
126dfc2f91aSPaul Mundt {
127dfc2f91aSPaul Mundt 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
128dfc2f91aSPaul Mundt 		return -EINVAL;
129dfc2f91aSPaul Mundt 
130dfc2f91aSPaul Mundt 	*pfn = address >> PAGE_SHIFT;
131dfc2f91aSPaul Mundt 	return 0;
132dfc2f91aSPaul Mundt }
133dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
134dfc2f91aSPaul Mundt 
135f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
1361da177e4SLinus Torvalds 
137b3bdda02SChristoph Lameter void vfree(const void *addr)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	kfree(addr);
1401da177e4SLinus Torvalds }
141b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
1421da177e4SLinus Torvalds 
143dd0fc66fSAl Viro void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	/*
1468518609dSRobert P. J. Day 	 *  You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
1478518609dSRobert P. J. Day 	 * returns only a logical address.
1481da177e4SLinus Torvalds 	 */
14984097518SNick Piggin 	return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1501da177e4SLinus Torvalds }
151b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
1521da177e4SLinus Torvalds 
153a7c3e901SMichal Hocko void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags)
154a7c3e901SMichal Hocko {
155a7c3e901SMichal Hocko 	return __vmalloc(size, flags, PAGE_KERNEL);
156a7c3e901SMichal Hocko }
157a7c3e901SMichal Hocko 
158*ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
159f905bc44SPaul Mundt {
160f905bc44SPaul Mundt 	void *ret;
161f905bc44SPaul Mundt 
162*ed81745aSAndrii Nakryiko 	ret = __vmalloc(size, flags, PAGE_KERNEL);
163f905bc44SPaul Mundt 	if (ret) {
164f905bc44SPaul Mundt 		struct vm_area_struct *vma;
165f905bc44SPaul Mundt 
166f905bc44SPaul Mundt 		down_write(&current->mm->mmap_sem);
167f905bc44SPaul Mundt 		vma = find_vma(current->mm, (unsigned long)ret);
168f905bc44SPaul Mundt 		if (vma)
169f905bc44SPaul Mundt 			vma->vm_flags |= VM_USERMAP;
170f905bc44SPaul Mundt 		up_write(&current->mm->mmap_sem);
171f905bc44SPaul Mundt 	}
172f905bc44SPaul Mundt 
173f905bc44SPaul Mundt 	return ret;
174f905bc44SPaul Mundt }
175*ed81745aSAndrii Nakryiko 
176*ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size)
177*ed81745aSAndrii Nakryiko {
178*ed81745aSAndrii Nakryiko 	return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
179*ed81745aSAndrii Nakryiko }
180f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
181f905bc44SPaul Mundt 
182*ed81745aSAndrii Nakryiko void *vmalloc_user_node_flags(unsigned long size, int node, gfp_t flags)
183*ed81745aSAndrii Nakryiko {
184*ed81745aSAndrii Nakryiko 	return __vmalloc_user_flags(size, flags | __GFP_ZERO);
185*ed81745aSAndrii Nakryiko }
186*ed81745aSAndrii Nakryiko EXPORT_SYMBOL(vmalloc_user_node_flags);
187*ed81745aSAndrii Nakryiko 
188b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	return virt_to_page(addr);
1911da177e4SLinus Torvalds }
192b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
1931da177e4SLinus Torvalds 
194b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	return page_to_pfn(virt_to_page(addr));
1971da177e4SLinus Torvalds }
198b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds long vread(char *buf, char *addr, unsigned long count)
2011da177e4SLinus Torvalds {
2029bde916bSChen Gang 	/* Don't allow overflow */
2039bde916bSChen Gang 	if ((unsigned long) buf + count < count)
2049bde916bSChen Gang 		count = -(unsigned long) buf;
2059bde916bSChen Gang 
2061da177e4SLinus Torvalds 	memcpy(buf, addr, count);
2071da177e4SLinus Torvalds 	return count;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds long vwrite(char *buf, char *addr, unsigned long count)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	/* Don't allow overflow */
2131da177e4SLinus Torvalds 	if ((unsigned long) addr + count < count)
2141da177e4SLinus Torvalds 		count = -(unsigned long) addr;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	memcpy(addr, buf, count);
217ac714904SChoi Gi-yong 	return count;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /*
221e1c05067SMasahiro Yamada  *	vmalloc  -  allocate virtually contiguous memory
2221da177e4SLinus Torvalds  *
2231da177e4SLinus Torvalds  *	@size:		allocation size
2241da177e4SLinus Torvalds  *
2251da177e4SLinus Torvalds  *	Allocate enough pages to cover @size from the page level
226e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
2271da177e4SLinus Torvalds  *
228c1c8897fSMichael Opdenacker  *	For tight control over page level allocator and protection flags
2291da177e4SLinus Torvalds  *	use __vmalloc() instead.
2301da177e4SLinus Torvalds  */
2311da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
2341da177e4SLinus Torvalds }
235f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
236f6138882SAndrew Morton 
237e1ca7788SDave Young /*
238e1c05067SMasahiro Yamada  *	vzalloc - allocate virtually contiguous memory with zero fill
239e1ca7788SDave Young  *
240e1ca7788SDave Young  *	@size:		allocation size
241e1ca7788SDave Young  *
242e1ca7788SDave Young  *	Allocate enough pages to cover @size from the page level
243e1c05067SMasahiro Yamada  *	allocator and map them into contiguous kernel virtual space.
244e1ca7788SDave Young  *	The memory allocated is set to zero.
245e1ca7788SDave Young  *
246e1ca7788SDave Young  *	For tight control over page level allocator and protection flags
247e1ca7788SDave Young  *	use __vmalloc() instead.
248e1ca7788SDave Young  */
249e1ca7788SDave Young void *vzalloc(unsigned long size)
250e1ca7788SDave Young {
251e1ca7788SDave Young 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
252e1ca7788SDave Young 			PAGE_KERNEL);
253e1ca7788SDave Young }
254e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
255e1ca7788SDave Young 
256e1ca7788SDave Young /**
257e1ca7788SDave Young  * vmalloc_node - allocate memory on a specific node
258e1ca7788SDave Young  * @size:	allocation size
259e1ca7788SDave Young  * @node:	numa node
260e1ca7788SDave Young  *
261e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
262e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
263e1ca7788SDave Young  *
264e1ca7788SDave Young  * For tight control over page level allocator and protection flags
265e1ca7788SDave Young  * use __vmalloc() instead.
266e1ca7788SDave Young  */
267f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
268f6138882SAndrew Morton {
269f6138882SAndrew Morton 	return vmalloc(size);
270f6138882SAndrew Morton }
2719a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
272e1ca7788SDave Young 
273e1ca7788SDave Young /**
274e1ca7788SDave Young  * vzalloc_node - allocate memory on a specific node with zero fill
275e1ca7788SDave Young  * @size:	allocation size
276e1ca7788SDave Young  * @node:	numa node
277e1ca7788SDave Young  *
278e1ca7788SDave Young  * Allocate enough pages to cover @size from the page level
279e1ca7788SDave Young  * allocator and map them into contiguous kernel virtual space.
280e1ca7788SDave Young  * The memory allocated is set to zero.
281e1ca7788SDave Young  *
282e1ca7788SDave Young  * For tight control over page level allocator and protection flags
283e1ca7788SDave Young  * use __vmalloc() instead.
284e1ca7788SDave Young  */
285e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
286e1ca7788SDave Young {
287e1ca7788SDave Young 	return vzalloc(size);
288e1ca7788SDave Young }
289e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2901da177e4SLinus Torvalds 
2911af446edSPaul Mundt /**
2921af446edSPaul Mundt  *	vmalloc_exec  -  allocate virtually contiguous, executable memory
2931af446edSPaul Mundt  *	@size:		allocation size
2941af446edSPaul Mundt  *
2951af446edSPaul Mundt  *	Kernel-internal function to allocate enough pages to cover @size
2961af446edSPaul Mundt  *	the page level allocator and map them into contiguous and
2971af446edSPaul Mundt  *	executable kernel virtual space.
2981af446edSPaul Mundt  *
2991af446edSPaul Mundt  *	For tight control over page level allocator and protection flags
3001af446edSPaul Mundt  *	use __vmalloc() instead.
3011af446edSPaul Mundt  */
3021af446edSPaul Mundt 
3031af446edSPaul Mundt void *vmalloc_exec(unsigned long size)
3041af446edSPaul Mundt {
3051af446edSPaul Mundt 	return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
3061af446edSPaul Mundt }
3071af446edSPaul Mundt 
308b5073173SPaul Mundt /**
309b5073173SPaul Mundt  * vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
3101da177e4SLinus Torvalds  *	@size:		allocation size
3111da177e4SLinus Torvalds  *
3121da177e4SLinus Torvalds  *	Allocate enough 32bit PA addressable pages to cover @size from the
313e1c05067SMasahiro Yamada  *	page level allocator and map them into contiguous kernel virtual space.
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
3181da177e4SLinus Torvalds }
319b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
320b5073173SPaul Mundt 
321b5073173SPaul Mundt /**
322b5073173SPaul Mundt  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
323b5073173SPaul Mundt  *	@size:		allocation size
324b5073173SPaul Mundt  *
325b5073173SPaul Mundt  * The resulting memory area is 32bit addressable and zeroed so it can be
326b5073173SPaul Mundt  * mapped to userspace without leaking data.
327f905bc44SPaul Mundt  *
328f905bc44SPaul Mundt  * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
329f905bc44SPaul Mundt  * remap_vmalloc_range() are permissible.
330b5073173SPaul Mundt  */
331b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
332b5073173SPaul Mundt {
333f905bc44SPaul Mundt 	/*
334f905bc44SPaul Mundt 	 * We'll have to sort out the ZONE_DMA bits for 64-bit,
335f905bc44SPaul Mundt 	 * but for now this can simply use vmalloc_user() directly.
336f905bc44SPaul Mundt 	 */
337f905bc44SPaul Mundt 	return vmalloc_user(size);
338b5073173SPaul Mundt }
339b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	BUG();
3441da177e4SLinus Torvalds 	return NULL;
3451da177e4SLinus Torvalds }
346b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3471da177e4SLinus Torvalds 
348b3bdda02SChristoph Lameter void vunmap(const void *addr)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	BUG();
3511da177e4SLinus Torvalds }
352b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3531da177e4SLinus Torvalds 
354eb6434d9SPaul Mundt void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
355eb6434d9SPaul Mundt {
356eb6434d9SPaul Mundt 	BUG();
357eb6434d9SPaul Mundt 	return NULL;
358eb6434d9SPaul Mundt }
359eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
360eb6434d9SPaul Mundt 
361eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
362eb6434d9SPaul Mundt {
363eb6434d9SPaul Mundt 	BUG();
364eb6434d9SPaul Mundt }
365eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
366eb6434d9SPaul Mundt 
367eb6434d9SPaul Mundt void vm_unmap_aliases(void)
368eb6434d9SPaul Mundt {
369eb6434d9SPaul Mundt }
370eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
371eb6434d9SPaul Mundt 
3721da177e4SLinus Torvalds /*
3731eeb66a1SChristoph Hellwig  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
3741eeb66a1SChristoph Hellwig  * have one.
3751eeb66a1SChristoph Hellwig  */
3763b32123dSGideon Israel Dsouza void __weak vmalloc_sync_all(void)
3771eeb66a1SChristoph Hellwig {
3781eeb66a1SChristoph Hellwig }
3791eeb66a1SChristoph Hellwig 
380cd12909cSDavid Vrabel struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
38129c185e5SPaul Mundt {
38229c185e5SPaul Mundt 	BUG();
38329c185e5SPaul Mundt 	return NULL;
38429c185e5SPaul Mundt }
38529c185e5SPaul Mundt EXPORT_SYMBOL_GPL(alloc_vm_area);
38629c185e5SPaul Mundt 
38729c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
38829c185e5SPaul Mundt {
38929c185e5SPaul Mundt 	BUG();
39029c185e5SPaul Mundt }
39129c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
39229c185e5SPaul Mundt 
393b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
394b5073173SPaul Mundt 		   struct page *page)
395b5073173SPaul Mundt {
396b5073173SPaul Mundt 	return -EINVAL;
397b5073173SPaul Mundt }
398b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
399b5073173SPaul Mundt 
400a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
401a667d745SSouptick Joarder 			unsigned long num)
402a667d745SSouptick Joarder {
403a667d745SSouptick Joarder 	return -EINVAL;
404a667d745SSouptick Joarder }
405a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages);
406a667d745SSouptick Joarder 
407a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
408a667d745SSouptick Joarder 				unsigned long num)
409a667d745SSouptick Joarder {
410a667d745SSouptick Joarder 	return -EINVAL;
411a667d745SSouptick Joarder }
412a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero);
413a667d745SSouptick Joarder 
4141eeb66a1SChristoph Hellwig /*
4151da177e4SLinus Torvalds  *  sys_brk() for the most part doesn't need the global kernel
4161da177e4SLinus Torvalds  *  lock, except when an application is doing something nasty
4171da177e4SLinus Torvalds  *  like trying to un-brk an area that has already been mapped
4181da177e4SLinus Torvalds  *  to a regular file.  in this case, the unmapping will need
4191da177e4SLinus Torvalds  *  to invoke file system routines that need the global lock.
4201da177e4SLinus Torvalds  */
4216a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	if (brk < mm->start_brk || brk > mm->context.end_brk)
4261da177e4SLinus Torvalds 		return mm->brk;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	if (mm->brk == brk)
4291da177e4SLinus Torvalds 		return mm->brk;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/*
4321da177e4SLinus Torvalds 	 * Always allow shrinking brk
4331da177e4SLinus Torvalds 	 */
4341da177e4SLinus Torvalds 	if (brk <= mm->brk) {
4351da177e4SLinus Torvalds 		mm->brk = brk;
4361da177e4SLinus Torvalds 		return brk;
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	/*
4401da177e4SLinus Torvalds 	 * Ok, looks good - let it rip.
4411da177e4SLinus Torvalds 	 */
442cfe79c00SMike Frysinger 	flush_icache_range(mm->brk, brk);
4431da177e4SLinus Torvalds 	return mm->brk = brk;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4468feae131SDavid Howells /*
4473edf41d8Sseokhoon.yoon  * initialise the percpu counter for VM and region record slabs
4488feae131SDavid Howells  */
4498feae131SDavid Howells void __init mmap_init(void)
4501da177e4SLinus Torvalds {
45100a62ce9SKOSAKI Motohiro 	int ret;
45200a62ce9SKOSAKI Motohiro 
453908c7f19STejun Heo 	ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
45400a62ce9SKOSAKI Motohiro 	VM_BUG_ON(ret);
4555d097056SVladimir Davydov 	vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
4568feae131SDavid Howells }
4571da177e4SLinus Torvalds 
4588feae131SDavid Howells /*
4598feae131SDavid Howells  * validate the region tree
4608feae131SDavid Howells  * - the caller must hold the region lock
4618feae131SDavid Howells  */
4628feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
4638feae131SDavid Howells static noinline void validate_nommu_regions(void)
4648feae131SDavid Howells {
4658feae131SDavid Howells 	struct vm_region *region, *last;
4668feae131SDavid Howells 	struct rb_node *p, *lastp;
4671da177e4SLinus Torvalds 
4688feae131SDavid Howells 	lastp = rb_first(&nommu_region_tree);
4698feae131SDavid Howells 	if (!lastp)
4708feae131SDavid Howells 		return;
4718feae131SDavid Howells 
4728feae131SDavid Howells 	last = rb_entry(lastp, struct vm_region, vm_rb);
473c9427bc0SGeliang Tang 	BUG_ON(last->vm_end <= last->vm_start);
474c9427bc0SGeliang Tang 	BUG_ON(last->vm_top < last->vm_end);
4758feae131SDavid Howells 
4768feae131SDavid Howells 	while ((p = rb_next(lastp))) {
4778feae131SDavid Howells 		region = rb_entry(p, struct vm_region, vm_rb);
4788feae131SDavid Howells 		last = rb_entry(lastp, struct vm_region, vm_rb);
4798feae131SDavid Howells 
480c9427bc0SGeliang Tang 		BUG_ON(region->vm_end <= region->vm_start);
481c9427bc0SGeliang Tang 		BUG_ON(region->vm_top < region->vm_end);
482c9427bc0SGeliang Tang 		BUG_ON(region->vm_start < last->vm_top);
4838feae131SDavid Howells 
4848feae131SDavid Howells 		lastp = p;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds }
4878feae131SDavid Howells #else
48833e5d769SDavid Howells static void validate_nommu_regions(void)
48933e5d769SDavid Howells {
49033e5d769SDavid Howells }
4918feae131SDavid Howells #endif
4928feae131SDavid Howells 
4938feae131SDavid Howells /*
4948feae131SDavid Howells  * add a region into the global tree
4958feae131SDavid Howells  */
4968feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4978feae131SDavid Howells {
4988feae131SDavid Howells 	struct vm_region *pregion;
4998feae131SDavid Howells 	struct rb_node **p, *parent;
5008feae131SDavid Howells 
5018feae131SDavid Howells 	validate_nommu_regions();
5028feae131SDavid Howells 
5038feae131SDavid Howells 	parent = NULL;
5048feae131SDavid Howells 	p = &nommu_region_tree.rb_node;
5058feae131SDavid Howells 	while (*p) {
5068feae131SDavid Howells 		parent = *p;
5078feae131SDavid Howells 		pregion = rb_entry(parent, struct vm_region, vm_rb);
5088feae131SDavid Howells 		if (region->vm_start < pregion->vm_start)
5098feae131SDavid Howells 			p = &(*p)->rb_left;
5108feae131SDavid Howells 		else if (region->vm_start > pregion->vm_start)
5118feae131SDavid Howells 			p = &(*p)->rb_right;
5128feae131SDavid Howells 		else if (pregion == region)
5138feae131SDavid Howells 			return;
5148feae131SDavid Howells 		else
5158feae131SDavid Howells 			BUG();
5168feae131SDavid Howells 	}
5178feae131SDavid Howells 
5188feae131SDavid Howells 	rb_link_node(&region->vm_rb, parent, p);
5198feae131SDavid Howells 	rb_insert_color(&region->vm_rb, &nommu_region_tree);
5208feae131SDavid Howells 
5218feae131SDavid Howells 	validate_nommu_regions();
5228feae131SDavid Howells }
5238feae131SDavid Howells 
5248feae131SDavid Howells /*
5258feae131SDavid Howells  * delete a region from the global tree
5268feae131SDavid Howells  */
5278feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
5288feae131SDavid Howells {
5298feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5308feae131SDavid Howells 
5318feae131SDavid Howells 	validate_nommu_regions();
5328feae131SDavid Howells 	rb_erase(&region->vm_rb, &nommu_region_tree);
5338feae131SDavid Howells 	validate_nommu_regions();
5348feae131SDavid Howells }
5358feae131SDavid Howells 
5368feae131SDavid Howells /*
5378feae131SDavid Howells  * free a contiguous series of pages
5388feae131SDavid Howells  */
5398feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5408feae131SDavid Howells {
5418feae131SDavid Howells 	for (; from < to; from += PAGE_SIZE) {
5428feae131SDavid Howells 		struct page *page = virt_to_page(from);
5438feae131SDavid Howells 
54433e5d769SDavid Howells 		atomic_long_dec(&mmap_pages_allocated);
5458feae131SDavid Howells 		put_page(page);
5468feae131SDavid Howells 	}
5478feae131SDavid Howells }
5488feae131SDavid Howells 
5498feae131SDavid Howells /*
5508feae131SDavid Howells  * release a reference to a region
55133e5d769SDavid Howells  * - the caller must hold the region semaphore for writing, which this releases
552dd8632a1SPaul Mundt  * - the region may not have been added to the tree yet, in which case vm_top
5538feae131SDavid Howells  *   will equal vm_start
5548feae131SDavid Howells  */
5558feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5568feae131SDavid Howells 	__releases(nommu_region_sem)
5578feae131SDavid Howells {
5588feae131SDavid Howells 	BUG_ON(!nommu_region_tree.rb_node);
5598feae131SDavid Howells 
5601e2ae599SDavid Howells 	if (--region->vm_usage == 0) {
561dd8632a1SPaul Mundt 		if (region->vm_top > region->vm_start)
5628feae131SDavid Howells 			delete_nommu_region(region);
5638feae131SDavid Howells 		up_write(&nommu_region_sem);
5648feae131SDavid Howells 
5658feae131SDavid Howells 		if (region->vm_file)
5668feae131SDavid Howells 			fput(region->vm_file);
5678feae131SDavid Howells 
5688feae131SDavid Howells 		/* IO memory and memory shared directly out of the pagecache
5698feae131SDavid Howells 		 * from ramfs/tmpfs mustn't be released here */
57022cc877bSLeon Romanovsky 		if (region->vm_flags & VM_MAPPED_COPY)
571dd8632a1SPaul Mundt 			free_page_series(region->vm_start, region->vm_top);
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 /*
5883034097aSDavid Howells  * add a VMA into a process's mm_struct in the appropriate place in the list
5898feae131SDavid Howells  * and tree and add to the address space's page tree also if not an anonymous
5908feae131SDavid Howells  * page
5913034097aSDavid Howells  * - should be called with mm->mmap_sem held writelocked
5923034097aSDavid Howells  */
5938feae131SDavid Howells static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
5943034097aSDavid Howells {
5956038def0SNamhyung Kim 	struct vm_area_struct *pvma, *prev;
5968feae131SDavid Howells 	struct address_space *mapping;
5976038def0SNamhyung Kim 	struct rb_node **p, *parent, *rb_prev;
5983034097aSDavid Howells 
5998feae131SDavid Howells 	BUG_ON(!vma->vm_region);
6008feae131SDavid Howells 
6018feae131SDavid Howells 	mm->map_count++;
6028feae131SDavid Howells 	vma->vm_mm = mm;
6038feae131SDavid Howells 
6048feae131SDavid Howells 	/* add the VMA to the mapping */
6058feae131SDavid Howells 	if (vma->vm_file) {
6068feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6078feae131SDavid Howells 
60883cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6098feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6106b2dbba8SMichel Lespinasse 		vma_interval_tree_insert(vma, &mapping->i_mmap);
6118feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
61283cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6138feae131SDavid Howells 	}
6148feae131SDavid Howells 
6158feae131SDavid Howells 	/* add the VMA to the tree */
6166038def0SNamhyung Kim 	parent = rb_prev = NULL;
6178feae131SDavid Howells 	p = &mm->mm_rb.rb_node;
6188feae131SDavid Howells 	while (*p) {
6198feae131SDavid Howells 		parent = *p;
6208feae131SDavid Howells 		pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
6218feae131SDavid Howells 
6228feae131SDavid Howells 		/* sort by: start addr, end addr, VMA struct addr in that order
6238feae131SDavid Howells 		 * (the latter is necessary as we may get identical VMAs) */
6248feae131SDavid Howells 		if (vma->vm_start < pvma->vm_start)
6258feae131SDavid Howells 			p = &(*p)->rb_left;
6266038def0SNamhyung Kim 		else if (vma->vm_start > pvma->vm_start) {
6276038def0SNamhyung Kim 			rb_prev = parent;
6288feae131SDavid Howells 			p = &(*p)->rb_right;
6296038def0SNamhyung Kim 		} else if (vma->vm_end < pvma->vm_end)
6308feae131SDavid Howells 			p = &(*p)->rb_left;
6316038def0SNamhyung Kim 		else if (vma->vm_end > pvma->vm_end) {
6326038def0SNamhyung Kim 			rb_prev = parent;
6338feae131SDavid Howells 			p = &(*p)->rb_right;
6346038def0SNamhyung Kim 		} else if (vma < pvma)
6358feae131SDavid Howells 			p = &(*p)->rb_left;
6366038def0SNamhyung Kim 		else if (vma > pvma) {
6376038def0SNamhyung Kim 			rb_prev = parent;
6388feae131SDavid Howells 			p = &(*p)->rb_right;
6396038def0SNamhyung Kim 		} else
6408feae131SDavid Howells 			BUG();
6418feae131SDavid Howells 	}
6428feae131SDavid Howells 
6438feae131SDavid Howells 	rb_link_node(&vma->vm_rb, parent, p);
6448feae131SDavid Howells 	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
6458feae131SDavid Howells 
6468feae131SDavid Howells 	/* add VMA to the VMA list also */
6476038def0SNamhyung Kim 	prev = NULL;
6486038def0SNamhyung Kim 	if (rb_prev)
6496038def0SNamhyung Kim 		prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
6503034097aSDavid Howells 
6516038def0SNamhyung Kim 	__vma_link_list(mm, vma, prev, parent);
6528feae131SDavid Howells }
6538feae131SDavid Howells 
6548feae131SDavid Howells /*
6558feae131SDavid Howells  * delete a VMA from its owning mm_struct and address space
6568feae131SDavid Howells  */
6578feae131SDavid Howells static void delete_vma_from_mm(struct vm_area_struct *vma)
6588feae131SDavid Howells {
659615d6e87SDavidlohr Bueso 	int i;
6608feae131SDavid Howells 	struct address_space *mapping;
6618feae131SDavid Howells 	struct mm_struct *mm = vma->vm_mm;
662615d6e87SDavidlohr Bueso 	struct task_struct *curr = current;
6638feae131SDavid Howells 
6648feae131SDavid Howells 	mm->map_count--;
665615d6e87SDavidlohr Bueso 	for (i = 0; i < VMACACHE_SIZE; i++) {
666615d6e87SDavidlohr Bueso 		/* if the vma is cached, invalidate the entire cache */
667314ff785SIngo Molnar 		if (curr->vmacache.vmas[i] == vma) {
668e020d5bdSSteven Miao 			vmacache_invalidate(mm);
669615d6e87SDavidlohr Bueso 			break;
670615d6e87SDavidlohr Bueso 		}
671615d6e87SDavidlohr Bueso 	}
6728feae131SDavid Howells 
6738feae131SDavid Howells 	/* remove the VMA from the mapping */
6748feae131SDavid Howells 	if (vma->vm_file) {
6758feae131SDavid Howells 		mapping = vma->vm_file->f_mapping;
6768feae131SDavid Howells 
67783cde9e8SDavidlohr Bueso 		i_mmap_lock_write(mapping);
6788feae131SDavid Howells 		flush_dcache_mmap_lock(mapping);
6796b2dbba8SMichel Lespinasse 		vma_interval_tree_remove(vma, &mapping->i_mmap);
6808feae131SDavid Howells 		flush_dcache_mmap_unlock(mapping);
68183cde9e8SDavidlohr Bueso 		i_mmap_unlock_write(mapping);
6828feae131SDavid Howells 	}
6838feae131SDavid Howells 
6848feae131SDavid Howells 	/* remove from the MM's tree and list */
6858feae131SDavid Howells 	rb_erase(&vma->vm_rb, &mm->mm_rb);
686b951bf2cSNamhyung Kim 
687b951bf2cSNamhyung Kim 	if (vma->vm_prev)
688b951bf2cSNamhyung Kim 		vma->vm_prev->vm_next = vma->vm_next;
689b951bf2cSNamhyung Kim 	else
690b951bf2cSNamhyung Kim 		mm->mmap = vma->vm_next;
691b951bf2cSNamhyung Kim 
692b951bf2cSNamhyung Kim 	if (vma->vm_next)
693b951bf2cSNamhyung Kim 		vma->vm_next->vm_prev = vma->vm_prev;
6948feae131SDavid Howells }
6958feae131SDavid Howells 
6968feae131SDavid Howells /*
6978feae131SDavid Howells  * destroy a VMA record
6988feae131SDavid Howells  */
6998feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
7008feae131SDavid Howells {
7018feae131SDavid Howells 	if (vma->vm_ops && vma->vm_ops->close)
7028feae131SDavid Howells 		vma->vm_ops->close(vma);
703e9714acfSKonstantin Khlebnikov 	if (vma->vm_file)
7048feae131SDavid Howells 		fput(vma->vm_file);
7058feae131SDavid Howells 	put_nommu_region(vma->vm_region);
7063928d4f5SLinus Torvalds 	vm_area_free(vma);
7073034097aSDavid Howells }
7083034097aSDavid Howells 
7093034097aSDavid Howells /*
7103034097aSDavid Howells  * look up the first VMA in which addr resides, NULL if none
7113034097aSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7123034097aSDavid Howells  */
7133034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
7143034097aSDavid Howells {
7158feae131SDavid Howells 	struct vm_area_struct *vma;
7163034097aSDavid Howells 
7178feae131SDavid Howells 	/* check the cache first */
718615d6e87SDavidlohr Bueso 	vma = vmacache_find(mm, addr);
719615d6e87SDavidlohr Bueso 	if (likely(vma))
7208feae131SDavid Howells 		return vma;
7218feae131SDavid Howells 
722e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7238feae131SDavid Howells 	 * resides) */
724e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7258feae131SDavid Howells 		if (vma->vm_start > addr)
7268feae131SDavid Howells 			return NULL;
7278feae131SDavid Howells 		if (vma->vm_end > addr) {
728615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7298feae131SDavid Howells 			return vma;
7303034097aSDavid Howells 		}
7318feae131SDavid Howells 	}
7323034097aSDavid Howells 
7333034097aSDavid Howells 	return NULL;
7343034097aSDavid Howells }
7353034097aSDavid Howells EXPORT_SYMBOL(find_vma);
7363034097aSDavid Howells 
7373034097aSDavid Howells /*
738930e652aSDavid Howells  * find a VMA
739930e652aSDavid Howells  * - we don't extend stack VMAs under NOMMU conditions
740930e652aSDavid Howells  */
741930e652aSDavid Howells struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
742930e652aSDavid Howells {
7437561e8caSDavid Howells 	return find_vma(mm, addr);
744930e652aSDavid Howells }
745930e652aSDavid Howells 
7468feae131SDavid Howells /*
7478feae131SDavid Howells  * expand a stack to a given address
7488feae131SDavid Howells  * - not supported under NOMMU conditions
7498feae131SDavid Howells  */
75057c8f63eSGreg Ungerer int expand_stack(struct vm_area_struct *vma, unsigned long address)
75157c8f63eSGreg Ungerer {
75257c8f63eSGreg Ungerer 	return -ENOMEM;
75357c8f63eSGreg Ungerer }
75457c8f63eSGreg Ungerer 
755930e652aSDavid Howells /*
7566fa5f80bSDavid Howells  * look up the first VMA exactly that exactly matches addr
7576fa5f80bSDavid Howells  * - should be called with mm->mmap_sem at least held readlocked
7586fa5f80bSDavid Howells  */
7598feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
7608feae131SDavid Howells 					     unsigned long addr,
7618feae131SDavid Howells 					     unsigned long len)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct vm_area_struct *vma;
7648feae131SDavid Howells 	unsigned long end = addr + len;
7651da177e4SLinus Torvalds 
7668feae131SDavid Howells 	/* check the cache first */
767615d6e87SDavidlohr Bueso 	vma = vmacache_find_exact(mm, addr, end);
768615d6e87SDavidlohr Bueso 	if (vma)
7691da177e4SLinus Torvalds 		return vma;
7708feae131SDavid Howells 
771e922c4c5SNamhyung Kim 	/* trawl the list (there may be multiple mappings in which addr
7728feae131SDavid Howells 	 * resides) */
773e922c4c5SNamhyung Kim 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
7748feae131SDavid Howells 		if (vma->vm_start < addr)
7758feae131SDavid Howells 			continue;
7768feae131SDavid Howells 		if (vma->vm_start > addr)
7778feae131SDavid Howells 			return NULL;
7788feae131SDavid Howells 		if (vma->vm_end == end) {
779615d6e87SDavidlohr Bueso 			vmacache_update(addr, vma);
7808feae131SDavid Howells 			return vma;
7818feae131SDavid Howells 		}
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	return NULL;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7873034097aSDavid Howells /*
7881da177e4SLinus Torvalds  * determine whether a mapping should be permitted and, if so, what sort of
7891da177e4SLinus Torvalds  * mapping we're capable of supporting
7901da177e4SLinus Torvalds  */
7911da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
7921da177e4SLinus Torvalds 				 unsigned long addr,
7931da177e4SLinus Torvalds 				 unsigned long len,
7941da177e4SLinus Torvalds 				 unsigned long prot,
7951da177e4SLinus Torvalds 				 unsigned long flags,
7961da177e4SLinus Torvalds 				 unsigned long pgoff,
7971da177e4SLinus Torvalds 				 unsigned long *_capabilities)
7981da177e4SLinus Torvalds {
7998feae131SDavid Howells 	unsigned long capabilities, rlen;
8001da177e4SLinus Torvalds 	int ret;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* do the simple checks first */
80322cc877bSLeon Romanovsky 	if (flags & MAP_FIXED)
8041da177e4SLinus Torvalds 		return -EINVAL;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	if ((flags & MAP_TYPE) != MAP_PRIVATE &&
8071da177e4SLinus Torvalds 	    (flags & MAP_TYPE) != MAP_SHARED)
8081da177e4SLinus Torvalds 		return -EINVAL;
8091da177e4SLinus Torvalds 
810f81cff0dSMike Frysinger 	if (!len)
8111da177e4SLinus Torvalds 		return -EINVAL;
8121da177e4SLinus Torvalds 
813f81cff0dSMike Frysinger 	/* Careful about overflows.. */
8148feae131SDavid Howells 	rlen = PAGE_ALIGN(len);
8158feae131SDavid Howells 	if (!rlen || rlen > TASK_SIZE)
816f81cff0dSMike Frysinger 		return -ENOMEM;
817f81cff0dSMike Frysinger 
8181da177e4SLinus Torvalds 	/* offset overflow? */
8198feae131SDavid Howells 	if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
820f81cff0dSMike Frysinger 		return -EOVERFLOW;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	if (file) {
8231da177e4SLinus Torvalds 		/* files must support mmap */
82472c2d531SAl Viro 		if (!file->f_op->mmap)
8251da177e4SLinus Torvalds 			return -ENODEV;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		/* work out if what we've got could possibly be shared
8281da177e4SLinus Torvalds 		 * - we support chardevs that provide their own "memory"
8291da177e4SLinus Torvalds 		 * - we support files/blockdevs that are memory backed
8301da177e4SLinus Torvalds 		 */
831b4caecd4SChristoph Hellwig 		if (file->f_op->mmap_capabilities) {
832b4caecd4SChristoph Hellwig 			capabilities = file->f_op->mmap_capabilities(file);
833b4caecd4SChristoph Hellwig 		} else {
8341da177e4SLinus Torvalds 			/* no explicit capabilities set, so assume some
8351da177e4SLinus Torvalds 			 * defaults */
836496ad9aaSAl Viro 			switch (file_inode(file)->i_mode & S_IFMT) {
8371da177e4SLinus Torvalds 			case S_IFREG:
8381da177e4SLinus Torvalds 			case S_IFBLK:
839b4caecd4SChristoph Hellwig 				capabilities = NOMMU_MAP_COPY;
8401da177e4SLinus Torvalds 				break;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 			case S_IFCHR:
8431da177e4SLinus Torvalds 				capabilities =
844b4caecd4SChristoph Hellwig 					NOMMU_MAP_DIRECT |
845b4caecd4SChristoph Hellwig 					NOMMU_MAP_READ |
846b4caecd4SChristoph Hellwig 					NOMMU_MAP_WRITE;
8471da177e4SLinus Torvalds 				break;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 			default:
8501da177e4SLinus Torvalds 				return -EINVAL;
8511da177e4SLinus Torvalds 			}
8521da177e4SLinus Torvalds 		}
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		/* eliminate any capabilities that we can't support on this
8551da177e4SLinus Torvalds 		 * device */
8561da177e4SLinus Torvalds 		if (!file->f_op->get_unmapped_area)
857b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
8586e242a1cSAl Viro 		if (!(file->f_mode & FMODE_CAN_READ))
859b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
8601da177e4SLinus Torvalds 
86128d7a6aeSGraff Yang 		/* The file shall have been opened with read permission. */
86228d7a6aeSGraff Yang 		if (!(file->f_mode & FMODE_READ))
86328d7a6aeSGraff Yang 			return -EACCES;
86428d7a6aeSGraff Yang 
8651da177e4SLinus Torvalds 		if (flags & MAP_SHARED) {
8661da177e4SLinus Torvalds 			/* do checks for writing, appending and locking */
8671da177e4SLinus Torvalds 			if ((prot & PROT_WRITE) &&
8681da177e4SLinus Torvalds 			    !(file->f_mode & FMODE_WRITE))
8691da177e4SLinus Torvalds 				return -EACCES;
8701da177e4SLinus Torvalds 
871496ad9aaSAl Viro 			if (IS_APPEND(file_inode(file)) &&
8721da177e4SLinus Torvalds 			    (file->f_mode & FMODE_WRITE))
8731da177e4SLinus Torvalds 				return -EACCES;
8741da177e4SLinus Torvalds 
875d7a06983SJeff Layton 			if (locks_verify_locked(file))
8761da177e4SLinus Torvalds 				return -EAGAIN;
8771da177e4SLinus Torvalds 
878b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_DIRECT))
8791da177e4SLinus Torvalds 				return -ENODEV;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 			/* we mustn't privatise shared mappings */
882b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_COPY;
883ac714904SChoi Gi-yong 		} else {
8841da177e4SLinus Torvalds 			/* we're going to read the file into private memory we
8851da177e4SLinus Torvalds 			 * allocate */
886b4caecd4SChristoph Hellwig 			if (!(capabilities & NOMMU_MAP_COPY))
8871da177e4SLinus Torvalds 				return -ENODEV;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 			/* we don't permit a private writable mapping to be
8901da177e4SLinus Torvalds 			 * shared with the backing device */
8911da177e4SLinus Torvalds 			if (prot & PROT_WRITE)
892b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
8931da177e4SLinus Torvalds 		}
8941da177e4SLinus Torvalds 
895b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
896b4caecd4SChristoph Hellwig 			if (((prot & PROT_READ)  && !(capabilities & NOMMU_MAP_READ))  ||
897b4caecd4SChristoph Hellwig 			    ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
898b4caecd4SChristoph Hellwig 			    ((prot & PROT_EXEC)  && !(capabilities & NOMMU_MAP_EXEC))
8993c7b2045SBernd Schmidt 			    ) {
900b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
9013c7b2045SBernd Schmidt 				if (flags & MAP_SHARED) {
90222cc877bSLeon Romanovsky 					pr_warn("MAP_SHARED not completely supported on !MMU\n");
9033c7b2045SBernd Schmidt 					return -EINVAL;
9043c7b2045SBernd Schmidt 				}
9053c7b2045SBernd Schmidt 			}
9063c7b2045SBernd Schmidt 		}
9073c7b2045SBernd Schmidt 
9081da177e4SLinus Torvalds 		/* handle executable mappings and implied executable
9091da177e4SLinus Torvalds 		 * mappings */
91090f8572bSEric W. Biederman 		if (path_noexec(&file->f_path)) {
9111da177e4SLinus Torvalds 			if (prot & PROT_EXEC)
9121da177e4SLinus Torvalds 				return -EPERM;
913ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
9141da177e4SLinus Torvalds 			/* handle implication of PROT_EXEC by PROT_READ */
9151da177e4SLinus Torvalds 			if (current->personality & READ_IMPLIES_EXEC) {
916b4caecd4SChristoph Hellwig 				if (capabilities & NOMMU_MAP_EXEC)
9171da177e4SLinus Torvalds 					prot |= PROT_EXEC;
9181da177e4SLinus Torvalds 			}
919ac714904SChoi Gi-yong 		} else if ((prot & PROT_READ) &&
9201da177e4SLinus Torvalds 			 (prot & PROT_EXEC) &&
921b4caecd4SChristoph Hellwig 			 !(capabilities & NOMMU_MAP_EXEC)
9221da177e4SLinus Torvalds 			 ) {
9231da177e4SLinus Torvalds 			/* backing file is not executable, try to copy */
924b4caecd4SChristoph Hellwig 			capabilities &= ~NOMMU_MAP_DIRECT;
9251da177e4SLinus Torvalds 		}
926ac714904SChoi Gi-yong 	} else {
9271da177e4SLinus Torvalds 		/* anonymous mappings are always memory backed and can be
9281da177e4SLinus Torvalds 		 * privately mapped
9291da177e4SLinus Torvalds 		 */
930b4caecd4SChristoph Hellwig 		capabilities = NOMMU_MAP_COPY;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 		/* handle PROT_EXEC implication by PROT_READ */
9331da177e4SLinus Torvalds 		if ((prot & PROT_READ) &&
9341da177e4SLinus Torvalds 		    (current->personality & READ_IMPLIES_EXEC))
9351da177e4SLinus Torvalds 			prot |= PROT_EXEC;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/* allow the security API to have its say */
939e5467859SAl Viro 	ret = security_mmap_addr(addr);
940e5467859SAl Viro 	if (ret < 0)
941e5467859SAl Viro 		return ret;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/* looks okay */
9441da177e4SLinus Torvalds 	*_capabilities = capabilities;
9451da177e4SLinus Torvalds 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds /*
9491da177e4SLinus Torvalds  * we've determined that we can make the mapping, now translate what we
9501da177e4SLinus Torvalds  * now know into VMA flags
9511da177e4SLinus Torvalds  */
9521da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
9531da177e4SLinus Torvalds 					unsigned long prot,
9541da177e4SLinus Torvalds 					unsigned long flags,
9551da177e4SLinus Torvalds 					unsigned long capabilities)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	unsigned long vm_flags;
9581da177e4SLinus Torvalds 
959e6bfb709SDave Hansen 	vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
9601da177e4SLinus Torvalds 	/* vm_flags |= mm->def_flags; */
9611da177e4SLinus Torvalds 
962b4caecd4SChristoph Hellwig 	if (!(capabilities & NOMMU_MAP_DIRECT)) {
9631da177e4SLinus Torvalds 		/* attempt to share read-only copies of mapped file chunks */
9643c7b2045SBernd Schmidt 		vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
9651da177e4SLinus Torvalds 		if (file && !(prot & PROT_WRITE))
9661da177e4SLinus Torvalds 			vm_flags |= VM_MAYSHARE;
9673c7b2045SBernd Schmidt 	} else {
9681da177e4SLinus Torvalds 		/* overlay a shareable mapping on the backing device or inode
9691da177e4SLinus Torvalds 		 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
9701da177e4SLinus Torvalds 		 * romfs/cramfs */
971b4caecd4SChristoph Hellwig 		vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
9721da177e4SLinus Torvalds 		if (flags & MAP_SHARED)
9733c7b2045SBernd Schmidt 			vm_flags |= VM_SHARED;
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	/* refuse to let anyone share private mappings with this process if
9771da177e4SLinus Torvalds 	 * it's being traced - otherwise breakpoints set in it may interfere
9781da177e4SLinus Torvalds 	 * with another untraced process
9791da177e4SLinus Torvalds 	 */
980a288eeccSTejun Heo 	if ((flags & MAP_PRIVATE) && current->ptrace)
9811da177e4SLinus Torvalds 		vm_flags &= ~VM_MAYSHARE;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	return vm_flags;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds /*
9878feae131SDavid Howells  * set up a shared mapping on a file (the driver or filesystem provides and
9888feae131SDavid Howells  * pins the storage)
9891da177e4SLinus Torvalds  */
9908feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	int ret;
9931da177e4SLinus Torvalds 
994f74ac015SMiklos Szeredi 	ret = call_mmap(vma->vm_file, vma);
995dd8632a1SPaul Mundt 	if (ret == 0) {
996dd8632a1SPaul Mundt 		vma->vm_region->vm_top = vma->vm_region->vm_end;
997645d83c5SDavid Howells 		return 0;
998dd8632a1SPaul Mundt 	}
9991da177e4SLinus Torvalds 	if (ret != -ENOSYS)
10001da177e4SLinus Torvalds 		return ret;
10011da177e4SLinus Torvalds 
10023fa30460SDavid Howells 	/* getting -ENOSYS indicates that direct mmap isn't possible (as
10033fa30460SDavid Howells 	 * opposed to tried but failed) so we can only give a suitable error as
10043fa30460SDavid Howells 	 * it's not possible to make a private copy if MAP_SHARED was given */
10051da177e4SLinus Torvalds 	return -ENODEV;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds /*
10091da177e4SLinus Torvalds  * set up a private mapping or an anonymous shared mapping
10101da177e4SLinus Torvalds  */
10118feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
10128feae131SDavid Howells 			   struct vm_region *region,
1013645d83c5SDavid Howells 			   unsigned long len,
1014645d83c5SDavid Howells 			   unsigned long capabilities)
10151da177e4SLinus Torvalds {
1016dbc8358cSJoonsoo Kim 	unsigned long total, point;
10171da177e4SLinus Torvalds 	void *base;
10188feae131SDavid Howells 	int ret, order;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	/* invoke the file's mapping function so that it can keep track of
10211da177e4SLinus Torvalds 	 * shared mappings on devices or memory
10221da177e4SLinus Torvalds 	 * - VM_MAYSHARE will be set if it may attempt to share
10231da177e4SLinus Torvalds 	 */
1024b4caecd4SChristoph Hellwig 	if (capabilities & NOMMU_MAP_DIRECT) {
1025f74ac015SMiklos Szeredi 		ret = call_mmap(vma->vm_file, vma);
1026dd8632a1SPaul Mundt 		if (ret == 0) {
10271da177e4SLinus Torvalds 			/* shouldn't return success if we're not sharing */
1028dd8632a1SPaul Mundt 			BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1029dd8632a1SPaul Mundt 			vma->vm_region->vm_top = vma->vm_region->vm_end;
1030645d83c5SDavid Howells 			return 0;
10311da177e4SLinus Torvalds 		}
1032dd8632a1SPaul Mundt 		if (ret != -ENOSYS)
1033dd8632a1SPaul Mundt 			return ret;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		/* getting an ENOSYS error indicates that direct mmap isn't
10361da177e4SLinus Torvalds 		 * possible (as opposed to tried but failed) so we'll try to
10371da177e4SLinus Torvalds 		 * make a private copy of the data and map that instead */
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 
10408feae131SDavid Howells 
10411da177e4SLinus Torvalds 	/* allocate some memory to hold the mapping
10421da177e4SLinus Torvalds 	 * - note that this may not return a page-aligned address if the object
10431da177e4SLinus Torvalds 	 *   we're allocating is smaller than a page
10441da177e4SLinus Torvalds 	 */
1045f67d9b15SBob Liu 	order = get_order(len);
10468feae131SDavid Howells 	total = 1 << order;
1047f67d9b15SBob Liu 	point = len >> PAGE_SHIFT;
1048dd8632a1SPaul Mundt 
1049dbc8358cSJoonsoo Kim 	/* we don't want to allocate a power-of-2 sized page set */
105022cc877bSLeon Romanovsky 	if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
1051dbc8358cSJoonsoo Kim 		total = point;
10528feae131SDavid Howells 
1053da616534SJoonsoo Kim 	base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
1054dbc8358cSJoonsoo Kim 	if (!base)
1055dbc8358cSJoonsoo Kim 		goto enomem;
10568feae131SDavid Howells 
1057dbc8358cSJoonsoo Kim 	atomic_long_add(total, &mmap_pages_allocated);
1058dbc8358cSJoonsoo Kim 
10598feae131SDavid Howells 	region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
10608feae131SDavid Howells 	region->vm_start = (unsigned long) base;
1061f67d9b15SBob Liu 	region->vm_end   = region->vm_start + len;
1062dd8632a1SPaul Mundt 	region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
10638feae131SDavid Howells 
10648feae131SDavid Howells 	vma->vm_start = region->vm_start;
10658feae131SDavid Howells 	vma->vm_end   = region->vm_start + len;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	if (vma->vm_file) {
10681da177e4SLinus Torvalds 		/* read the contents of a file into the copy */
10691da177e4SLinus Torvalds 		loff_t fpos;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 		fpos = vma->vm_pgoff;
10721da177e4SLinus Torvalds 		fpos <<= PAGE_SHIFT;
10731da177e4SLinus Torvalds 
1074b4bf802aSChristoph Hellwig 		ret = kernel_read(vma->vm_file, base, len, &fpos);
10751da177e4SLinus Torvalds 		if (ret < 0)
10761da177e4SLinus Torvalds 			goto error_free;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 		/* clear the last little bit */
1079f67d9b15SBob Liu 		if (ret < len)
1080f67d9b15SBob Liu 			memset(base + ret, 0, len - ret);
10811da177e4SLinus Torvalds 
1082bfd40eafSKirill A. Shutemov 	} else {
1083bfd40eafSKirill A. Shutemov 		vma_set_anonymous(vma);
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	return 0;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds error_free:
10897223bb4aSNamhyung Kim 	free_page_series(region->vm_start, region->vm_top);
10908feae131SDavid Howells 	region->vm_start = vma->vm_start = 0;
10918feae131SDavid Howells 	region->vm_end   = vma->vm_end = 0;
1092dd8632a1SPaul Mundt 	region->vm_top   = 0;
10931da177e4SLinus Torvalds 	return ret;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds enomem:
1096b1de0d13SMitchel Humpherys 	pr_err("Allocation of length %lu from process %d (%s) failed\n",
109705ae6fa3SGreg Ungerer 	       len, current->pid, current->comm);
10989af744d7SMichal Hocko 	show_free_areas(0, NULL);
10991da177e4SLinus Torvalds 	return -ENOMEM;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds /*
11031da177e4SLinus Torvalds  * handle mapping creation for uClinux
11041da177e4SLinus Torvalds  */
11051fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
11061da177e4SLinus Torvalds 			unsigned long addr,
11071da177e4SLinus Torvalds 			unsigned long len,
11081da177e4SLinus Torvalds 			unsigned long prot,
11091da177e4SLinus Torvalds 			unsigned long flags,
11101fcfd8dbSOleg Nesterov 			vm_flags_t vm_flags,
1111bebeb3d6SMichel Lespinasse 			unsigned long pgoff,
1112897ab3e0SMike Rapoport 			unsigned long *populate,
1113897ab3e0SMike Rapoport 			struct list_head *uf)
11141da177e4SLinus Torvalds {
11158feae131SDavid Howells 	struct vm_area_struct *vma;
11168feae131SDavid Howells 	struct vm_region *region;
11171da177e4SLinus Torvalds 	struct rb_node *rb;
11181fcfd8dbSOleg Nesterov 	unsigned long capabilities, result;
11191da177e4SLinus Torvalds 	int ret;
11201da177e4SLinus Torvalds 
112141badc15SMichel Lespinasse 	*populate = 0;
1122bebeb3d6SMichel Lespinasse 
11231da177e4SLinus Torvalds 	/* decide whether we should attempt the mapping, and if so what sort of
11241da177e4SLinus Torvalds 	 * mapping */
11251da177e4SLinus Torvalds 	ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
11261da177e4SLinus Torvalds 				    &capabilities);
112722cc877bSLeon Romanovsky 	if (ret < 0)
11281da177e4SLinus Torvalds 		return ret;
11291da177e4SLinus Torvalds 
113006aab5a3SDavid Howells 	/* we ignore the address hint */
113106aab5a3SDavid Howells 	addr = 0;
1132f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
113306aab5a3SDavid Howells 
11341da177e4SLinus Torvalds 	/* we've determined that we can make the mapping, now translate what we
11351da177e4SLinus Torvalds 	 * now know into VMA flags */
11361fcfd8dbSOleg Nesterov 	vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
11371da177e4SLinus Torvalds 
11388feae131SDavid Howells 	/* we're going to need to record the mapping */
11398feae131SDavid Howells 	region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
11408feae131SDavid Howells 	if (!region)
11418feae131SDavid Howells 		goto error_getting_region;
11421da177e4SLinus Torvalds 
1143490fc053SLinus Torvalds 	vma = vm_area_alloc(current->mm);
11448feae131SDavid Howells 	if (!vma)
11458feae131SDavid Howells 		goto error_getting_vma;
11461da177e4SLinus Torvalds 
11471e2ae599SDavid Howells 	region->vm_usage = 1;
11488feae131SDavid Howells 	region->vm_flags = vm_flags;
11498feae131SDavid Howells 	region->vm_pgoff = pgoff;
11508feae131SDavid Howells 
11518feae131SDavid Howells 	vma->vm_flags = vm_flags;
11528feae131SDavid Howells 	vma->vm_pgoff = pgoff;
11538feae131SDavid Howells 
11548feae131SDavid Howells 	if (file) {
1155cb0942b8SAl Viro 		region->vm_file = get_file(file);
1156cb0942b8SAl Viro 		vma->vm_file = get_file(file);
11578feae131SDavid Howells 	}
11588feae131SDavid Howells 
11598feae131SDavid Howells 	down_write(&nommu_region_sem);
11608feae131SDavid Howells 
11618feae131SDavid Howells 	/* if we want to share, we need to check for regions created by other
11621da177e4SLinus Torvalds 	 * mmap() calls that overlap with our proposed mapping
11638feae131SDavid Howells 	 * - we can only share with a superset match on most regular files
11641da177e4SLinus Torvalds 	 * - shared mappings on character devices and memory backed files are
11651da177e4SLinus Torvalds 	 *   permitted to overlap inexactly as far as we are concerned for in
11661da177e4SLinus Torvalds 	 *   these cases, sharing is handled in the driver or filesystem rather
11671da177e4SLinus Torvalds 	 *   than here
11681da177e4SLinus Torvalds 	 */
11691da177e4SLinus Torvalds 	if (vm_flags & VM_MAYSHARE) {
11708feae131SDavid Howells 		struct vm_region *pregion;
11718feae131SDavid Howells 		unsigned long pglen, rpglen, pgend, rpgend, start;
11721da177e4SLinus Torvalds 
11738feae131SDavid Howells 		pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
11748feae131SDavid Howells 		pgend = pgoff + pglen;
1175165b2392SDavid Howells 
11768feae131SDavid Howells 		for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
11778feae131SDavid Howells 			pregion = rb_entry(rb, struct vm_region, vm_rb);
11781da177e4SLinus Torvalds 
11798feae131SDavid Howells 			if (!(pregion->vm_flags & VM_MAYSHARE))
11801da177e4SLinus Torvalds 				continue;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 			/* search for overlapping mappings on the same file */
1183496ad9aaSAl Viro 			if (file_inode(pregion->vm_file) !=
1184496ad9aaSAl Viro 			    file_inode(file))
11851da177e4SLinus Torvalds 				continue;
11861da177e4SLinus Torvalds 
11878feae131SDavid Howells 			if (pregion->vm_pgoff >= pgend)
11881da177e4SLinus Torvalds 				continue;
11891da177e4SLinus Torvalds 
11908feae131SDavid Howells 			rpglen = pregion->vm_end - pregion->vm_start;
11918feae131SDavid Howells 			rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
11928feae131SDavid Howells 			rpgend = pregion->vm_pgoff + rpglen;
11938feae131SDavid Howells 			if (pgoff >= rpgend)
11941da177e4SLinus Torvalds 				continue;
11951da177e4SLinus Torvalds 
11968feae131SDavid Howells 			/* handle inexactly overlapping matches between
11978feae131SDavid Howells 			 * mappings */
11988feae131SDavid Howells 			if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
11998feae131SDavid Howells 			    !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
12008feae131SDavid Howells 				/* new mapping is not a subset of the region */
1201b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_DIRECT))
12021da177e4SLinus Torvalds 					goto sharing_violation;
12031da177e4SLinus Torvalds 				continue;
12041da177e4SLinus Torvalds 			}
12051da177e4SLinus Torvalds 
12068feae131SDavid Howells 			/* we've found a region we can share */
12071e2ae599SDavid Howells 			pregion->vm_usage++;
12088feae131SDavid Howells 			vma->vm_region = pregion;
12098feae131SDavid Howells 			start = pregion->vm_start;
12108feae131SDavid Howells 			start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
12118feae131SDavid Howells 			vma->vm_start = start;
12128feae131SDavid Howells 			vma->vm_end = start + len;
12131da177e4SLinus Torvalds 
121422cc877bSLeon Romanovsky 			if (pregion->vm_flags & VM_MAPPED_COPY)
12158feae131SDavid Howells 				vma->vm_flags |= VM_MAPPED_COPY;
121622cc877bSLeon Romanovsky 			else {
12178feae131SDavid Howells 				ret = do_mmap_shared_file(vma);
12188feae131SDavid Howells 				if (ret < 0) {
12198feae131SDavid Howells 					vma->vm_region = NULL;
12208feae131SDavid Howells 					vma->vm_start = 0;
12218feae131SDavid Howells 					vma->vm_end = 0;
12221e2ae599SDavid Howells 					pregion->vm_usage--;
12238feae131SDavid Howells 					pregion = NULL;
12248feae131SDavid Howells 					goto error_just_free;
12251da177e4SLinus Torvalds 				}
12268feae131SDavid Howells 			}
12278feae131SDavid Howells 			fput(region->vm_file);
12288feae131SDavid Howells 			kmem_cache_free(vm_region_jar, region);
12298feae131SDavid Howells 			region = pregion;
12308feae131SDavid Howells 			result = start;
12318feae131SDavid Howells 			goto share;
12328feae131SDavid Howells 		}
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 		/* obtain the address at which to make a shared mapping
12351da177e4SLinus Torvalds 		 * - this is the hook for quasi-memory character devices to
12361da177e4SLinus Torvalds 		 *   tell us the location of a shared mapping
12371da177e4SLinus Torvalds 		 */
1238b4caecd4SChristoph Hellwig 		if (capabilities & NOMMU_MAP_DIRECT) {
12391da177e4SLinus Torvalds 			addr = file->f_op->get_unmapped_area(file, addr, len,
12401da177e4SLinus Torvalds 							     pgoff, flags);
1241bb005a59SNamhyung Kim 			if (IS_ERR_VALUE(addr)) {
12421da177e4SLinus Torvalds 				ret = addr;
1243bb005a59SNamhyung Kim 				if (ret != -ENOSYS)
12448feae131SDavid Howells 					goto error_just_free;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 				/* the driver refused to tell us where to site
12471da177e4SLinus Torvalds 				 * the mapping so we'll have to attempt to copy
12481da177e4SLinus Torvalds 				 * it */
1249bb005a59SNamhyung Kim 				ret = -ENODEV;
1250b4caecd4SChristoph Hellwig 				if (!(capabilities & NOMMU_MAP_COPY))
12518feae131SDavid Howells 					goto error_just_free;
12521da177e4SLinus Torvalds 
1253b4caecd4SChristoph Hellwig 				capabilities &= ~NOMMU_MAP_DIRECT;
12548feae131SDavid Howells 			} else {
12558feae131SDavid Howells 				vma->vm_start = region->vm_start = addr;
12568feae131SDavid Howells 				vma->vm_end = region->vm_end = addr + len;
12571da177e4SLinus Torvalds 			}
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 	}
12601da177e4SLinus Torvalds 
12618feae131SDavid Howells 	vma->vm_region = region;
12621da177e4SLinus Torvalds 
1263645d83c5SDavid Howells 	/* set up the mapping
1264b4caecd4SChristoph Hellwig 	 * - the region is filled in if NOMMU_MAP_DIRECT is still set
1265645d83c5SDavid Howells 	 */
12661da177e4SLinus Torvalds 	if (file && vma->vm_flags & VM_SHARED)
12678feae131SDavid Howells 		ret = do_mmap_shared_file(vma);
12681da177e4SLinus Torvalds 	else
1269645d83c5SDavid Howells 		ret = do_mmap_private(vma, region, len, capabilities);
12701da177e4SLinus Torvalds 	if (ret < 0)
1271645d83c5SDavid Howells 		goto error_just_free;
1272645d83c5SDavid Howells 	add_nommu_region(region);
12738feae131SDavid Howells 
1274ea637639SJie Zhang 	/* clear anonymous mappings that don't ask for uninitialized data */
12750bf5f949SChristoph Hellwig 	if (!vma->vm_file &&
12760bf5f949SChristoph Hellwig 	    (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
12770bf5f949SChristoph Hellwig 	     !(flags & MAP_UNINITIALIZED)))
1278ea637639SJie Zhang 		memset((void *)region->vm_start, 0,
1279ea637639SJie Zhang 		       region->vm_end - region->vm_start);
1280ea637639SJie Zhang 
12811da177e4SLinus Torvalds 	/* okay... we have a mapping; now we have to register it */
12828feae131SDavid Howells 	result = vma->vm_start;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	current->mm->total_vm += len >> PAGE_SHIFT;
12851da177e4SLinus Torvalds 
12868feae131SDavid Howells share:
12878feae131SDavid Howells 	add_vma_to_mm(current->mm, vma);
12881da177e4SLinus Torvalds 
1289cfe79c00SMike Frysinger 	/* we flush the region from the icache only when the first executable
1290cfe79c00SMike Frysinger 	 * mapping of it is made  */
1291cfe79c00SMike Frysinger 	if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1292cfe79c00SMike Frysinger 		flush_icache_range(region->vm_start, region->vm_end);
1293cfe79c00SMike Frysinger 		region->vm_icache_flushed = true;
1294cfe79c00SMike Frysinger 	}
12951da177e4SLinus Torvalds 
1296cfe79c00SMike Frysinger 	up_write(&nommu_region_sem);
12971da177e4SLinus Torvalds 
12988feae131SDavid Howells 	return result;
12991da177e4SLinus Torvalds 
13008feae131SDavid Howells error_just_free:
13018feae131SDavid Howells 	up_write(&nommu_region_sem);
13028feae131SDavid Howells error:
130389a86402SDavid Howells 	if (region->vm_file)
13048feae131SDavid Howells 		fput(region->vm_file);
13058feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
130689a86402SDavid Howells 	if (vma->vm_file)
13078feae131SDavid Howells 		fput(vma->vm_file);
13083928d4f5SLinus Torvalds 	vm_area_free(vma);
13091da177e4SLinus Torvalds 	return ret;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds sharing_violation:
13128feae131SDavid Howells 	up_write(&nommu_region_sem);
131322cc877bSLeon Romanovsky 	pr_warn("Attempt to share mismatched mappings\n");
13148feae131SDavid Howells 	ret = -EINVAL;
13158feae131SDavid Howells 	goto error;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds error_getting_vma:
13188feae131SDavid Howells 	kmem_cache_free(vm_region_jar, region);
131922cc877bSLeon Romanovsky 	pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
13201da177e4SLinus Torvalds 			len, current->pid);
13219af744d7SMichal Hocko 	show_free_areas(0, NULL);
13221da177e4SLinus Torvalds 	return -ENOMEM;
13231da177e4SLinus Torvalds 
13248feae131SDavid Howells error_getting_region:
132522cc877bSLeon Romanovsky 	pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
13261da177e4SLinus Torvalds 			len, current->pid);
13279af744d7SMichal Hocko 	show_free_areas(0, NULL);
13281da177e4SLinus Torvalds 	return -ENOMEM;
13291da177e4SLinus Torvalds }
13306be5ceb0SLinus Torvalds 
1331a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1332a90f590aSDominik Brodowski 			      unsigned long prot, unsigned long flags,
1333a90f590aSDominik Brodowski 			      unsigned long fd, unsigned long pgoff)
133466f0dc48SHugh Dickins {
133566f0dc48SHugh Dickins 	struct file *file = NULL;
133666f0dc48SHugh Dickins 	unsigned long retval = -EBADF;
133766f0dc48SHugh Dickins 
1338120a795dSAl Viro 	audit_mmap_fd(fd, flags);
133966f0dc48SHugh Dickins 	if (!(flags & MAP_ANONYMOUS)) {
134066f0dc48SHugh Dickins 		file = fget(fd);
134166f0dc48SHugh Dickins 		if (!file)
134266f0dc48SHugh Dickins 			goto out;
134366f0dc48SHugh Dickins 	}
134466f0dc48SHugh Dickins 
134566f0dc48SHugh Dickins 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
134666f0dc48SHugh Dickins 
1347ad1ed293SGreg Ungerer 	retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
134866f0dc48SHugh Dickins 
134966f0dc48SHugh Dickins 	if (file)
135066f0dc48SHugh Dickins 		fput(file);
135166f0dc48SHugh Dickins out:
135266f0dc48SHugh Dickins 	return retval;
135366f0dc48SHugh Dickins }
135466f0dc48SHugh Dickins 
1355a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1356a90f590aSDominik Brodowski 		unsigned long, prot, unsigned long, flags,
1357a90f590aSDominik Brodowski 		unsigned long, fd, unsigned long, pgoff)
1358a90f590aSDominik Brodowski {
1359a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1360a90f590aSDominik Brodowski }
1361a90f590aSDominik Brodowski 
1362a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1363a4679373SChristoph Hellwig struct mmap_arg_struct {
1364a4679373SChristoph Hellwig 	unsigned long addr;
1365a4679373SChristoph Hellwig 	unsigned long len;
1366a4679373SChristoph Hellwig 	unsigned long prot;
1367a4679373SChristoph Hellwig 	unsigned long flags;
1368a4679373SChristoph Hellwig 	unsigned long fd;
1369a4679373SChristoph Hellwig 	unsigned long offset;
1370a4679373SChristoph Hellwig };
1371a4679373SChristoph Hellwig 
1372a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1373a4679373SChristoph Hellwig {
1374a4679373SChristoph Hellwig 	struct mmap_arg_struct a;
1375a4679373SChristoph Hellwig 
1376a4679373SChristoph Hellwig 	if (copy_from_user(&a, arg, sizeof(a)))
1377a4679373SChristoph Hellwig 		return -EFAULT;
13781824cb75SAlexander Kuleshov 	if (offset_in_page(a.offset))
1379a4679373SChristoph Hellwig 		return -EINVAL;
1380a4679373SChristoph Hellwig 
1381a90f590aSDominik Brodowski 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1382a4679373SChristoph Hellwig 			       a.offset >> PAGE_SHIFT);
1383a4679373SChristoph Hellwig }
1384a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1385a4679373SChristoph Hellwig 
13861da177e4SLinus Torvalds /*
13878feae131SDavid Howells  * split a vma into two pieces at address 'addr', a new vma is allocated either
13888feae131SDavid Howells  * for the first part or the tail.
13891da177e4SLinus Torvalds  */
13908feae131SDavid Howells int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
13918feae131SDavid Howells 	      unsigned long addr, int new_below)
13921da177e4SLinus Torvalds {
13938feae131SDavid Howells 	struct vm_area_struct *new;
13948feae131SDavid Howells 	struct vm_region *region;
13958feae131SDavid Howells 	unsigned long npages;
13961da177e4SLinus Torvalds 
1397779c1023SDavid Howells 	/* we're only permitted to split anonymous regions (these should have
1398779c1023SDavid Howells 	 * only a single usage on the region) */
1399779c1023SDavid Howells 	if (vma->vm_file)
14008feae131SDavid Howells 		return -ENOMEM;
14011da177e4SLinus Torvalds 
14028feae131SDavid Howells 	if (mm->map_count >= sysctl_max_map_count)
14038feae131SDavid Howells 		return -ENOMEM;
14041da177e4SLinus Torvalds 
14058feae131SDavid Howells 	region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
14068feae131SDavid Howells 	if (!region)
14078feae131SDavid Howells 		return -ENOMEM;
14088feae131SDavid Howells 
14093928d4f5SLinus Torvalds 	new = vm_area_dup(vma);
14108feae131SDavid Howells 	if (!new) {
14118feae131SDavid Howells 		kmem_cache_free(vm_region_jar, region);
14128feae131SDavid Howells 		return -ENOMEM;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
14158feae131SDavid Howells 	/* most fields are the same, copy all, and then fixup */
14168feae131SDavid Howells 	*region = *vma->vm_region;
14178feae131SDavid Howells 	new->vm_region = region;
14188feae131SDavid Howells 
14198feae131SDavid Howells 	npages = (addr - vma->vm_start) >> PAGE_SHIFT;
14208feae131SDavid Howells 
14218feae131SDavid Howells 	if (new_below) {
1422dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = new->vm_end = addr;
14238feae131SDavid Howells 	} else {
14248feae131SDavid Howells 		region->vm_start = new->vm_start = addr;
14258feae131SDavid Howells 		region->vm_pgoff = new->vm_pgoff += npages;
14261da177e4SLinus Torvalds 	}
14278feae131SDavid Howells 
14288feae131SDavid Howells 	if (new->vm_ops && new->vm_ops->open)
14298feae131SDavid Howells 		new->vm_ops->open(new);
14308feae131SDavid Howells 
14318feae131SDavid Howells 	delete_vma_from_mm(vma);
14328feae131SDavid Howells 	down_write(&nommu_region_sem);
14338feae131SDavid Howells 	delete_nommu_region(vma->vm_region);
14348feae131SDavid Howells 	if (new_below) {
14358feae131SDavid Howells 		vma->vm_region->vm_start = vma->vm_start = addr;
14368feae131SDavid Howells 		vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
14378feae131SDavid Howells 	} else {
14388feae131SDavid Howells 		vma->vm_region->vm_end = vma->vm_end = addr;
1439dd8632a1SPaul Mundt 		vma->vm_region->vm_top = addr;
14408feae131SDavid Howells 	}
14418feae131SDavid Howells 	add_nommu_region(vma->vm_region);
14428feae131SDavid Howells 	add_nommu_region(new->vm_region);
14438feae131SDavid Howells 	up_write(&nommu_region_sem);
14448feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14458feae131SDavid Howells 	add_vma_to_mm(mm, new);
14468feae131SDavid Howells 	return 0;
14478feae131SDavid Howells }
14488feae131SDavid Howells 
14498feae131SDavid Howells /*
14508feae131SDavid Howells  * shrink a VMA by removing the specified chunk from either the beginning or
14518feae131SDavid Howells  * the end
14528feae131SDavid Howells  */
14538feae131SDavid Howells static int shrink_vma(struct mm_struct *mm,
14548feae131SDavid Howells 		      struct vm_area_struct *vma,
14558feae131SDavid Howells 		      unsigned long from, unsigned long to)
14568feae131SDavid Howells {
14578feae131SDavid Howells 	struct vm_region *region;
14588feae131SDavid Howells 
14598feae131SDavid Howells 	/* adjust the VMA's pointers, which may reposition it in the MM's tree
14608feae131SDavid Howells 	 * and list */
14618feae131SDavid Howells 	delete_vma_from_mm(vma);
14628feae131SDavid Howells 	if (from > vma->vm_start)
14638feae131SDavid Howells 		vma->vm_end = from;
14648feae131SDavid Howells 	else
14658feae131SDavid Howells 		vma->vm_start = to;
14668feae131SDavid Howells 	add_vma_to_mm(mm, vma);
14678feae131SDavid Howells 
14688feae131SDavid Howells 	/* cut the backing region down to size */
14698feae131SDavid Howells 	region = vma->vm_region;
14701e2ae599SDavid Howells 	BUG_ON(region->vm_usage != 1);
14718feae131SDavid Howells 
14728feae131SDavid Howells 	down_write(&nommu_region_sem);
14738feae131SDavid Howells 	delete_nommu_region(region);
1474dd8632a1SPaul Mundt 	if (from > region->vm_start) {
1475dd8632a1SPaul Mundt 		to = region->vm_top;
1476dd8632a1SPaul Mundt 		region->vm_top = region->vm_end = from;
1477dd8632a1SPaul Mundt 	} else {
14788feae131SDavid Howells 		region->vm_start = to;
1479dd8632a1SPaul Mundt 	}
14808feae131SDavid Howells 	add_nommu_region(region);
14818feae131SDavid Howells 	up_write(&nommu_region_sem);
14828feae131SDavid Howells 
14838feae131SDavid Howells 	free_page_series(from, to);
14848feae131SDavid Howells 	return 0;
14851da177e4SLinus Torvalds }
14861da177e4SLinus Torvalds 
14873034097aSDavid Howells /*
14883034097aSDavid Howells  * release a mapping
14898feae131SDavid Howells  * - under NOMMU conditions the chunk to be unmapped must be backed by a single
14908feae131SDavid Howells  *   VMA, though it need not cover the whole VMA
14913034097aSDavid Howells  */
1492897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
14931da177e4SLinus Torvalds {
14948feae131SDavid Howells 	struct vm_area_struct *vma;
1495f67d9b15SBob Liu 	unsigned long end;
14968feae131SDavid Howells 	int ret;
14971da177e4SLinus Torvalds 
1498f67d9b15SBob Liu 	len = PAGE_ALIGN(len);
14998feae131SDavid Howells 	if (len == 0)
15001da177e4SLinus Torvalds 		return -EINVAL;
15011da177e4SLinus Torvalds 
1502f67d9b15SBob Liu 	end = start + len;
1503f67d9b15SBob Liu 
15048feae131SDavid Howells 	/* find the first potentially overlapping VMA */
15058feae131SDavid Howells 	vma = find_vma(mm, start);
15068feae131SDavid Howells 	if (!vma) {
1507ac714904SChoi Gi-yong 		static int limit;
150833e5d769SDavid Howells 		if (limit < 5) {
150922cc877bSLeon Romanovsky 			pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
151033e5d769SDavid Howells 					current->pid, current->comm,
151133e5d769SDavid Howells 					start, start + len - 1);
151233e5d769SDavid Howells 			limit++;
151333e5d769SDavid Howells 		}
15148feae131SDavid Howells 		return -EINVAL;
15158feae131SDavid Howells 	}
15161da177e4SLinus Torvalds 
15178feae131SDavid Howells 	/* we're allowed to split an anonymous VMA but not a file-backed one */
15188feae131SDavid Howells 	if (vma->vm_file) {
15198feae131SDavid Howells 		do {
152022cc877bSLeon Romanovsky 			if (start > vma->vm_start)
15218feae131SDavid Howells 				return -EINVAL;
15228feae131SDavid Howells 			if (end == vma->vm_end)
15238feae131SDavid Howells 				goto erase_whole_vma;
1524d75a310cSNamhyung Kim 			vma = vma->vm_next;
1525d75a310cSNamhyung Kim 		} while (vma);
15268feae131SDavid Howells 		return -EINVAL;
15278feae131SDavid Howells 	} else {
15288feae131SDavid Howells 		/* the chunk must be a subset of the VMA found */
15298feae131SDavid Howells 		if (start == vma->vm_start && end == vma->vm_end)
15308feae131SDavid Howells 			goto erase_whole_vma;
153122cc877bSLeon Romanovsky 		if (start < vma->vm_start || end > vma->vm_end)
15328feae131SDavid Howells 			return -EINVAL;
15331824cb75SAlexander Kuleshov 		if (offset_in_page(start))
15348feae131SDavid Howells 			return -EINVAL;
15351824cb75SAlexander Kuleshov 		if (end != vma->vm_end && offset_in_page(end))
15368feae131SDavid Howells 			return -EINVAL;
15378feae131SDavid Howells 		if (start != vma->vm_start && end != vma->vm_end) {
15388feae131SDavid Howells 			ret = split_vma(mm, vma, start, 1);
153922cc877bSLeon Romanovsky 			if (ret < 0)
15408feae131SDavid Howells 				return ret;
15418feae131SDavid Howells 		}
15428feae131SDavid Howells 		return shrink_vma(mm, vma, start, end);
15438feae131SDavid Howells 	}
15441da177e4SLinus Torvalds 
15458feae131SDavid Howells erase_whole_vma:
15468feae131SDavid Howells 	delete_vma_from_mm(vma);
15478feae131SDavid Howells 	delete_vma(mm, vma);
15481da177e4SLinus Torvalds 	return 0;
15491da177e4SLinus Torvalds }
1550b5073173SPaul Mundt EXPORT_SYMBOL(do_munmap);
15511da177e4SLinus Torvalds 
1552bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
15533034097aSDavid Howells {
1554bfce281cSAl Viro 	struct mm_struct *mm = current->mm;
15553034097aSDavid Howells 	int ret;
15563034097aSDavid Howells 
15573034097aSDavid Howells 	down_write(&mm->mmap_sem);
1558897ab3e0SMike Rapoport 	ret = do_munmap(mm, addr, len, NULL);
15593034097aSDavid Howells 	up_write(&mm->mmap_sem);
15603034097aSDavid Howells 	return ret;
15613034097aSDavid Howells }
1562a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1563a46ef99dSLinus Torvalds 
1564a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1565a46ef99dSLinus Torvalds {
1566bfce281cSAl Viro 	return vm_munmap(addr, len);
1567a46ef99dSLinus Torvalds }
15683034097aSDavid Howells 
15693034097aSDavid Howells /*
15708feae131SDavid Howells  * release all the mappings made in a process's VM space
15713034097aSDavid Howells  */
15721da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
15731da177e4SLinus Torvalds {
15748feae131SDavid Howells 	struct vm_area_struct *vma;
15751da177e4SLinus Torvalds 
15768feae131SDavid Howells 	if (!mm)
15778feae131SDavid Howells 		return;
15788feae131SDavid Howells 
15791da177e4SLinus Torvalds 	mm->total_vm = 0;
15801da177e4SLinus Torvalds 
15818feae131SDavid Howells 	while ((vma = mm->mmap)) {
15828feae131SDavid Howells 		mm->mmap = vma->vm_next;
15838feae131SDavid Howells 		delete_vma_from_mm(vma);
15848feae131SDavid Howells 		delete_vma(mm, vma);
158504c34961SSteven J. Magnani 		cond_resched();
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15895d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	return -ENOMEM;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds /*
15956fa5f80bSDavid Howells  * expand (or shrink) an existing mapping, potentially moving it at the same
15966fa5f80bSDavid Howells  * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
15971da177e4SLinus Torvalds  *
15986fa5f80bSDavid Howells  * under NOMMU conditions, we only permit changing a mapping's size, and only
15998feae131SDavid Howells  * as long as it stays within the region allocated by do_mmap_private() and the
16008feae131SDavid Howells  * block is not shareable
16011da177e4SLinus Torvalds  *
16026fa5f80bSDavid Howells  * MREMAP_FIXED is not supported under NOMMU conditions
16031da177e4SLinus Torvalds  */
16044b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
16051da177e4SLinus Torvalds 			unsigned long old_len, unsigned long new_len,
16061da177e4SLinus Torvalds 			unsigned long flags, unsigned long new_addr)
16071da177e4SLinus Torvalds {
16086fa5f80bSDavid Howells 	struct vm_area_struct *vma;
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	/* insanity checks first */
1611f67d9b15SBob Liu 	old_len = PAGE_ALIGN(old_len);
1612f67d9b15SBob Liu 	new_len = PAGE_ALIGN(new_len);
16138feae131SDavid Howells 	if (old_len == 0 || new_len == 0)
16141da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16151da177e4SLinus Torvalds 
16161824cb75SAlexander Kuleshov 	if (offset_in_page(addr))
16178feae131SDavid Howells 		return -EINVAL;
16188feae131SDavid Howells 
16191da177e4SLinus Torvalds 	if (flags & MREMAP_FIXED && new_addr != addr)
16201da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16211da177e4SLinus Torvalds 
16228feae131SDavid Howells 	vma = find_vma_exact(current->mm, addr, old_len);
16236fa5f80bSDavid Howells 	if (!vma)
16241da177e4SLinus Torvalds 		return (unsigned long) -EINVAL;
16251da177e4SLinus Torvalds 
16266fa5f80bSDavid Howells 	if (vma->vm_end != vma->vm_start + old_len)
16271da177e4SLinus Torvalds 		return (unsigned long) -EFAULT;
16281da177e4SLinus Torvalds 
16296fa5f80bSDavid Howells 	if (vma->vm_flags & VM_MAYSHARE)
16301da177e4SLinus Torvalds 		return (unsigned long) -EPERM;
16311da177e4SLinus Torvalds 
16328feae131SDavid Howells 	if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
16331da177e4SLinus Torvalds 		return (unsigned long) -ENOMEM;
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	/* all checks complete - do it */
16366fa5f80bSDavid Howells 	vma->vm_end = vma->vm_start + new_len;
16376fa5f80bSDavid Howells 	return vma->vm_start;
16386fa5f80bSDavid Howells }
16396fa5f80bSDavid Howells 
16406a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
16416a6160a7SHeiko Carstens 		unsigned long, new_len, unsigned long, flags,
16426a6160a7SHeiko Carstens 		unsigned long, new_addr)
16436fa5f80bSDavid Howells {
16446fa5f80bSDavid Howells 	unsigned long ret;
16456fa5f80bSDavid Howells 
16466fa5f80bSDavid Howells 	down_write(&current->mm->mmap_sem);
16476fa5f80bSDavid Howells 	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
16486fa5f80bSDavid Howells 	up_write(&current->mm->mmap_sem);
16496fa5f80bSDavid Howells 	return ret;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
1652df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1653df06b37fSKeith Busch 			 unsigned int foll_flags)
16541da177e4SLinus Torvalds {
16551da177e4SLinus Torvalds 	return NULL;
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds 
16588f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16598f3b1327SBob Liu 		unsigned long pfn, unsigned long size, pgprot_t prot)
16601da177e4SLinus Torvalds {
16618f3b1327SBob Liu 	if (addr != (pfn << PAGE_SHIFT))
16628f3b1327SBob Liu 		return -EINVAL;
16638f3b1327SBob Liu 
1664314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
166566aa2b4bSGreg Ungerer 	return 0;
16661da177e4SLinus Torvalds }
166722c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
16681da177e4SLinus Torvalds 
16693c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
16703c0b9de6SLinus Torvalds {
16713c0b9de6SLinus Torvalds 	unsigned long pfn = start >> PAGE_SHIFT;
16723c0b9de6SLinus Torvalds 	unsigned long vm_len = vma->vm_end - vma->vm_start;
16733c0b9de6SLinus Torvalds 
16743c0b9de6SLinus Torvalds 	pfn += vma->vm_pgoff;
16753c0b9de6SLinus Torvalds 	return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
16763c0b9de6SLinus Torvalds }
16773c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
16783c0b9de6SLinus Torvalds 
1679f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1680f905bc44SPaul Mundt 			unsigned long pgoff)
1681f905bc44SPaul Mundt {
1682f905bc44SPaul Mundt 	unsigned int size = vma->vm_end - vma->vm_start;
1683f905bc44SPaul Mundt 
1684f905bc44SPaul Mundt 	if (!(vma->vm_flags & VM_USERMAP))
1685f905bc44SPaul Mundt 		return -EINVAL;
1686f905bc44SPaul Mundt 
1687f905bc44SPaul Mundt 	vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1688f905bc44SPaul Mundt 	vma->vm_end = vma->vm_start + size;
1689f905bc44SPaul Mundt 
1690f905bc44SPaul Mundt 	return 0;
1691f905bc44SPaul Mundt }
1692f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1693f905bc44SPaul Mundt 
16941da177e4SLinus Torvalds unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
16951da177e4SLinus Torvalds 	unsigned long len, unsigned long pgoff, unsigned long flags)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	return -ENOMEM;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17002bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
1701b0e15190SDavid Howells {
1702b0e15190SDavid Howells 	BUG();
1703d0217ac0SNick Piggin 	return 0;
1704b0e15190SDavid Howells }
1705b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
17060ec76a11SDavid Howells 
170782b0f8c3SJan Kara void filemap_map_pages(struct vm_fault *vmf,
1708bae473a4SKirill A. Shutemov 		pgoff_t start_pgoff, pgoff_t end_pgoff)
1709f1820361SKirill A. Shutemov {
1710f1820361SKirill A. Shutemov 	BUG();
1711f1820361SKirill A. Shutemov }
1712f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1713f1820361SKirill A. Shutemov 
171484d77d3fSEric W. Biederman int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1715442486ecSLorenzo Stoakes 		unsigned long addr, void *buf, int len, unsigned int gup_flags)
17160ec76a11SDavid Howells {
17170ec76a11SDavid Howells 	struct vm_area_struct *vma;
1718442486ecSLorenzo Stoakes 	int write = gup_flags & FOLL_WRITE;
17190ec76a11SDavid Howells 
17201e426fe2SKonstantin Khlebnikov 	if (down_read_killable(&mm->mmap_sem))
17211e426fe2SKonstantin Khlebnikov 		return 0;
17220ec76a11SDavid Howells 
17230ec76a11SDavid Howells 	/* the access must start within one of the target process's mappings */
17240159b141SDavid Howells 	vma = find_vma(mm, addr);
17250159b141SDavid Howells 	if (vma) {
17260ec76a11SDavid Howells 		/* don't overrun this mapping */
17270ec76a11SDavid Howells 		if (addr + len >= vma->vm_end)
17280ec76a11SDavid Howells 			len = vma->vm_end - addr;
17290ec76a11SDavid Howells 
17300ec76a11SDavid Howells 		/* only read or write mappings where it is permitted */
1731d00c7b99SDavid Howells 		if (write && vma->vm_flags & VM_MAYWRITE)
17327959722bSJie Zhang 			copy_to_user_page(vma, NULL, addr,
17337959722bSJie Zhang 					 (void *) addr, buf, len);
1734d00c7b99SDavid Howells 		else if (!write && vma->vm_flags & VM_MAYREAD)
17357959722bSJie Zhang 			copy_from_user_page(vma, NULL, addr,
17367959722bSJie Zhang 					    buf, (void *) addr, len);
17370ec76a11SDavid Howells 		else
17380ec76a11SDavid Howells 			len = 0;
17390ec76a11SDavid Howells 	} else {
17400ec76a11SDavid Howells 		len = 0;
17410ec76a11SDavid Howells 	}
17420ec76a11SDavid Howells 
17430ec76a11SDavid Howells 	up_read(&mm->mmap_sem);
1744f55f199bSMike Frysinger 
1745f55f199bSMike Frysinger 	return len;
1746f55f199bSMike Frysinger }
1747f55f199bSMike Frysinger 
1748f55f199bSMike Frysinger /**
1749b7701a5fSMike Rapoport  * access_remote_vm - access another process' address space
1750f55f199bSMike Frysinger  * @mm:		the mm_struct of the target address space
1751f55f199bSMike Frysinger  * @addr:	start address to access
1752f55f199bSMike Frysinger  * @buf:	source or destination buffer
1753f55f199bSMike Frysinger  * @len:	number of bytes to transfer
17546347e8d5SLorenzo Stoakes  * @gup_flags:	flags modifying lookup behaviour
1755f55f199bSMike Frysinger  *
1756f55f199bSMike Frysinger  * The caller must hold a reference on @mm.
1757f55f199bSMike Frysinger  */
1758f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
17596347e8d5SLorenzo Stoakes 		void *buf, int len, unsigned int gup_flags)
1760f55f199bSMike Frysinger {
17616347e8d5SLorenzo Stoakes 	return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
1762f55f199bSMike Frysinger }
1763f55f199bSMike Frysinger 
1764f55f199bSMike Frysinger /*
1765f55f199bSMike Frysinger  * Access another process' address space.
1766f55f199bSMike Frysinger  * - source/target buffer must be kernel space
1767f55f199bSMike Frysinger  */
1768f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1769f307ab6dSLorenzo Stoakes 		unsigned int gup_flags)
1770f55f199bSMike Frysinger {
1771f55f199bSMike Frysinger 	struct mm_struct *mm;
1772f55f199bSMike Frysinger 
1773f55f199bSMike Frysinger 	if (addr + len < addr)
1774f55f199bSMike Frysinger 		return 0;
1775f55f199bSMike Frysinger 
1776f55f199bSMike Frysinger 	mm = get_task_mm(tsk);
1777f55f199bSMike Frysinger 	if (!mm)
1778f55f199bSMike Frysinger 		return 0;
1779f55f199bSMike Frysinger 
1780f307ab6dSLorenzo Stoakes 	len = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
1781f55f199bSMike Frysinger 
17820ec76a11SDavid Howells 	mmput(mm);
17830ec76a11SDavid Howells 	return len;
17840ec76a11SDavid Howells }
1785fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
17867e660872SDavid Howells 
17877e660872SDavid Howells /**
17887e660872SDavid Howells  * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
17897e660872SDavid Howells  * @inode: The inode to check
17907e660872SDavid Howells  * @size: The current filesize of the inode
17917e660872SDavid Howells  * @newsize: The proposed filesize of the inode
17927e660872SDavid Howells  *
17937e660872SDavid Howells  * Check the shared mappings on an inode on behalf of a shrinking truncate to
17947e660872SDavid Howells  * make sure that that any outstanding VMAs aren't broken and then shrink the
17957e660872SDavid Howells  * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
17967e660872SDavid Howells  * automatically grant mappings that are too large.
17977e660872SDavid Howells  */
17987e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
17997e660872SDavid Howells 				size_t newsize)
18007e660872SDavid Howells {
18017e660872SDavid Howells 	struct vm_area_struct *vma;
18027e660872SDavid Howells 	struct vm_region *region;
18037e660872SDavid Howells 	pgoff_t low, high;
18047e660872SDavid Howells 	size_t r_size, r_top;
18057e660872SDavid Howells 
18067e660872SDavid Howells 	low = newsize >> PAGE_SHIFT;
18077e660872SDavid Howells 	high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
18087e660872SDavid Howells 
18097e660872SDavid Howells 	down_write(&nommu_region_sem);
18101acf2e04SDavidlohr Bueso 	i_mmap_lock_read(inode->i_mapping);
18117e660872SDavid Howells 
18127e660872SDavid Howells 	/* search for VMAs that fall within the dead zone */
18136b2dbba8SMichel Lespinasse 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
18147e660872SDavid Howells 		/* found one - only interested if it's shared out of the page
18157e660872SDavid Howells 		 * cache */
18167e660872SDavid Howells 		if (vma->vm_flags & VM_SHARED) {
18171acf2e04SDavidlohr Bueso 			i_mmap_unlock_read(inode->i_mapping);
18187e660872SDavid Howells 			up_write(&nommu_region_sem);
18197e660872SDavid Howells 			return -ETXTBSY; /* not quite true, but near enough */
18207e660872SDavid Howells 		}
18217e660872SDavid Howells 	}
18227e660872SDavid Howells 
18237e660872SDavid Howells 	/* reduce any regions that overlap the dead zone - if in existence,
18247e660872SDavid Howells 	 * these will be pointed to by VMAs that don't overlap the dead zone
18257e660872SDavid Howells 	 *
18267e660872SDavid Howells 	 * we don't check for any regions that start beyond the EOF as there
18277e660872SDavid Howells 	 * shouldn't be any
18287e660872SDavid Howells 	 */
18291acf2e04SDavidlohr Bueso 	vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
18307e660872SDavid Howells 		if (!(vma->vm_flags & VM_SHARED))
18317e660872SDavid Howells 			continue;
18327e660872SDavid Howells 
18337e660872SDavid Howells 		region = vma->vm_region;
18347e660872SDavid Howells 		r_size = region->vm_top - region->vm_start;
18357e660872SDavid Howells 		r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
18367e660872SDavid Howells 
18377e660872SDavid Howells 		if (r_top > newsize) {
18387e660872SDavid Howells 			region->vm_top -= r_top - newsize;
18397e660872SDavid Howells 			if (region->vm_end > region->vm_top)
18407e660872SDavid Howells 				region->vm_end = region->vm_top;
18417e660872SDavid Howells 		}
18427e660872SDavid Howells 	}
18437e660872SDavid Howells 
18441acf2e04SDavidlohr Bueso 	i_mmap_unlock_read(inode->i_mapping);
18457e660872SDavid Howells 	up_write(&nommu_region_sem);
18467e660872SDavid Howells 	return 0;
18477e660872SDavid Howells }
1848c9b1d098SAndrew Shewmaker 
1849c9b1d098SAndrew Shewmaker /*
1850c9b1d098SAndrew Shewmaker  * Initialise sysctl_user_reserve_kbytes.
1851c9b1d098SAndrew Shewmaker  *
1852c9b1d098SAndrew Shewmaker  * This is intended to prevent a user from starting a single memory hogging
1853c9b1d098SAndrew Shewmaker  * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1854c9b1d098SAndrew Shewmaker  * mode.
1855c9b1d098SAndrew Shewmaker  *
1856c9b1d098SAndrew Shewmaker  * The default value is min(3% of free memory, 128MB)
1857c9b1d098SAndrew Shewmaker  * 128MB is enough to recover with sshd/login, bash, and top/kill.
1858c9b1d098SAndrew Shewmaker  */
1859c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1860c9b1d098SAndrew Shewmaker {
1861c9b1d098SAndrew Shewmaker 	unsigned long free_kbytes;
1862c9b1d098SAndrew Shewmaker 
1863c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1864c9b1d098SAndrew Shewmaker 
1865c9b1d098SAndrew Shewmaker 	sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1866c9b1d098SAndrew Shewmaker 	return 0;
1867c9b1d098SAndrew Shewmaker }
1868a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
18694eeab4f5SAndrew Shewmaker 
18704eeab4f5SAndrew Shewmaker /*
18714eeab4f5SAndrew Shewmaker  * Initialise sysctl_admin_reserve_kbytes.
18724eeab4f5SAndrew Shewmaker  *
18734eeab4f5SAndrew Shewmaker  * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
18744eeab4f5SAndrew Shewmaker  * to log in and kill a memory hogging process.
18754eeab4f5SAndrew Shewmaker  *
18764eeab4f5SAndrew Shewmaker  * Systems with more than 256MB will reserve 8MB, enough to recover
18774eeab4f5SAndrew Shewmaker  * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
18784eeab4f5SAndrew Shewmaker  * only reserve 3% of free pages by default.
18794eeab4f5SAndrew Shewmaker  */
18804eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
18814eeab4f5SAndrew Shewmaker {
18824eeab4f5SAndrew Shewmaker 	unsigned long free_kbytes;
18834eeab4f5SAndrew Shewmaker 
1884c41f012aSMichal Hocko 	free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
18854eeab4f5SAndrew Shewmaker 
18864eeab4f5SAndrew Shewmaker 	sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
18874eeab4f5SAndrew Shewmaker 	return 0;
18884eeab4f5SAndrew Shewmaker }
1889a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1890