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 *
8dd19d293SStephen Kitt * See Documentation/admin-guide/mm/nommu-mmap.rst
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>
221da177e4SLinus Torvalds #include <linux/mman.h>
231da177e4SLinus Torvalds #include <linux/swap.h>
241da177e4SLinus Torvalds #include <linux/file.h>
251da177e4SLinus Torvalds #include <linux/highmem.h>
261da177e4SLinus Torvalds #include <linux/pagemap.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/vmalloc.h>
291da177e4SLinus Torvalds #include <linux/backing-dev.h>
303b32123dSGideon Israel Dsouza #include <linux/compiler.h>
311da177e4SLinus Torvalds #include <linux/mount.h>
321da177e4SLinus Torvalds #include <linux/personality.h>
331da177e4SLinus Torvalds #include <linux/security.h>
341da177e4SLinus Torvalds #include <linux/syscalls.h>
35120a795dSAl Viro #include <linux/audit.h>
36b1de0d13SMitchel Humpherys #include <linux/printk.h>
371da177e4SLinus Torvalds
387c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
394c91c07cSLorenzo Stoakes #include <linux/uio.h>
401da177e4SLinus Torvalds #include <asm/tlb.h>
411da177e4SLinus Torvalds #include <asm/tlbflush.h>
42eb8cdec4SBernd Schmidt #include <asm/mmu_context.h>
438feae131SDavid Howells #include "internal.h"
448feae131SDavid Howells
451da177e4SLinus Torvalds void *high_memory;
46944b6874SArnd Bergmann EXPORT_SYMBOL(high_memory);
471da177e4SLinus Torvalds struct page *mem_map;
481da177e4SLinus Torvalds unsigned long max_mapnr;
495b8bf307Sgchen gchen EXPORT_SYMBOL(max_mapnr);
504266c97aSHugh Dickins unsigned long highest_memmap_pfn;
51fc4d5c29SDavid Howells int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
521da177e4SLinus Torvalds int heap_stack_gap = 0;
531da177e4SLinus Torvalds
5433e5d769SDavid Howells atomic_long_t mmap_pages_allocated;
558feae131SDavid Howells
561da177e4SLinus Torvalds EXPORT_SYMBOL(mem_map);
571da177e4SLinus Torvalds
588feae131SDavid Howells /* list of mapped, potentially shareable regions */
598feae131SDavid Howells static struct kmem_cache *vm_region_jar;
608feae131SDavid Howells struct rb_root nommu_region_tree = RB_ROOT;
618feae131SDavid Howells DECLARE_RWSEM(nommu_region_sem);
621da177e4SLinus Torvalds
63f0f37e2fSAlexey Dobriyan const struct vm_operations_struct generic_file_vm_ops = {
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds * Return the total memory allocated for this pointer, not
681da177e4SLinus Torvalds * just what the caller asked for.
691da177e4SLinus Torvalds *
701da177e4SLinus Torvalds * Doesn't have to be accurate, i.e. may have races.
711da177e4SLinus Torvalds */
kobjsize(const void * objp)721da177e4SLinus Torvalds unsigned int kobjsize(const void *objp)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds struct page *page;
751da177e4SLinus Torvalds
764016a139SMichael Hennerich /*
774016a139SMichael Hennerich * If the object we have should not have ksize performed on it,
784016a139SMichael Hennerich * return size of 0
794016a139SMichael Hennerich */
805a1603beSPaul Mundt if (!objp || !virt_addr_valid(objp))
816cfd53fcSPaul Mundt return 0;
826cfd53fcSPaul Mundt
836cfd53fcSPaul Mundt page = virt_to_head_page(objp);
846cfd53fcSPaul Mundt
856cfd53fcSPaul Mundt /*
866cfd53fcSPaul Mundt * If the allocator sets PageSlab, we know the pointer came from
876cfd53fcSPaul Mundt * kmalloc().
886cfd53fcSPaul Mundt */
891da177e4SLinus Torvalds if (PageSlab(page))
901da177e4SLinus Torvalds return ksize(objp);
911da177e4SLinus Torvalds
926cfd53fcSPaul Mundt /*
93ab2e83eaSPaul Mundt * If it's not a compound page, see if we have a matching VMA
94ab2e83eaSPaul Mundt * region. This test is intentionally done in reverse order,
95ab2e83eaSPaul Mundt * so if there's no VMA, we still fall through and hand back
96ab2e83eaSPaul Mundt * PAGE_SIZE for 0-order pages.
97ab2e83eaSPaul Mundt */
98ab2e83eaSPaul Mundt if (!PageCompound(page)) {
99ab2e83eaSPaul Mundt struct vm_area_struct *vma;
100ab2e83eaSPaul Mundt
101ab2e83eaSPaul Mundt vma = find_vma(current->mm, (unsigned long)objp);
102ab2e83eaSPaul Mundt if (vma)
103ab2e83eaSPaul Mundt return vma->vm_end - vma->vm_start;
104ab2e83eaSPaul Mundt }
105ab2e83eaSPaul Mundt
106ab2e83eaSPaul Mundt /*
1076cfd53fcSPaul Mundt * The ksize() function is only guaranteed to work for pointers
1085a1603beSPaul Mundt * returned by kmalloc(). So handle arbitrary pointers here.
1096cfd53fcSPaul Mundt */
110a50b854eSMatthew Wilcox (Oracle) return page_size(page);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds
113dfc2f91aSPaul Mundt /**
114dfc2f91aSPaul Mundt * follow_pfn - look up PFN at a user virtual address
115dfc2f91aSPaul Mundt * @vma: memory mapping
116dfc2f91aSPaul Mundt * @address: user virtual address
117dfc2f91aSPaul Mundt * @pfn: location to store found PFN
118dfc2f91aSPaul Mundt *
119dfc2f91aSPaul Mundt * Only IO mappings and raw PFN mappings are allowed.
120dfc2f91aSPaul Mundt *
121dfc2f91aSPaul Mundt * Returns zero and the pfn at @pfn on success, -ve otherwise.
122dfc2f91aSPaul Mundt */
follow_pfn(struct vm_area_struct * vma,unsigned long address,unsigned long * pfn)123dfc2f91aSPaul Mundt int follow_pfn(struct vm_area_struct *vma, unsigned long address,
124dfc2f91aSPaul Mundt unsigned long *pfn)
125dfc2f91aSPaul Mundt {
126dfc2f91aSPaul Mundt if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
127dfc2f91aSPaul Mundt return -EINVAL;
128dfc2f91aSPaul Mundt
129dfc2f91aSPaul Mundt *pfn = address >> PAGE_SHIFT;
130dfc2f91aSPaul Mundt return 0;
131dfc2f91aSPaul Mundt }
132dfc2f91aSPaul Mundt EXPORT_SYMBOL(follow_pfn);
133dfc2f91aSPaul Mundt
134f1c4069eSJoonsoo Kim LIST_HEAD(vmap_area_list);
1351da177e4SLinus Torvalds
vfree(const void * addr)136b3bdda02SChristoph Lameter void vfree(const void *addr)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds kfree(addr);
1391da177e4SLinus Torvalds }
140b5073173SPaul Mundt EXPORT_SYMBOL(vfree);
1411da177e4SLinus Torvalds
__vmalloc(unsigned long size,gfp_t gfp_mask)14288dca4caSChristoph Hellwig void *__vmalloc(unsigned long size, gfp_t gfp_mask)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds /*
1458518609dSRobert P. J. Day * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
1468518609dSRobert P. J. Day * returns only a logical address.
1471da177e4SLinus Torvalds */
14884097518SNick Piggin return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1491da177e4SLinus Torvalds }
150b5073173SPaul Mundt EXPORT_SYMBOL(__vmalloc);
1511da177e4SLinus Torvalds
__vmalloc_node_range(unsigned long size,unsigned long align,unsigned long start,unsigned long end,gfp_t gfp_mask,pgprot_t prot,unsigned long vm_flags,int node,const void * caller)152041de93fSChristoph Hellwig void *__vmalloc_node_range(unsigned long size, unsigned long align,
153041de93fSChristoph Hellwig unsigned long start, unsigned long end, gfp_t gfp_mask,
154041de93fSChristoph Hellwig pgprot_t prot, unsigned long vm_flags, int node,
155041de93fSChristoph Hellwig const void *caller)
156041de93fSChristoph Hellwig {
157041de93fSChristoph Hellwig return __vmalloc(size, gfp_mask);
158041de93fSChristoph Hellwig }
159041de93fSChristoph Hellwig
__vmalloc_node(unsigned long size,unsigned long align,gfp_t gfp_mask,int node,const void * caller)1602b905948SChristoph Hellwig void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
1612b905948SChristoph Hellwig int node, const void *caller)
162a7c3e901SMichal Hocko {
1632b905948SChristoph Hellwig return __vmalloc(size, gfp_mask);
164a7c3e901SMichal Hocko }
165a7c3e901SMichal Hocko
__vmalloc_user_flags(unsigned long size,gfp_t flags)166ed81745aSAndrii Nakryiko static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
167f905bc44SPaul Mundt {
168f905bc44SPaul Mundt void *ret;
169f905bc44SPaul Mundt
17088dca4caSChristoph Hellwig ret = __vmalloc(size, flags);
171f905bc44SPaul Mundt if (ret) {
172f905bc44SPaul Mundt struct vm_area_struct *vma;
173f905bc44SPaul Mundt
174d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm);
175f905bc44SPaul Mundt vma = find_vma(current->mm, (unsigned long)ret);
176f905bc44SPaul Mundt if (vma)
1771c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_USERMAP);
178d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
179f905bc44SPaul Mundt }
180f905bc44SPaul Mundt
181f905bc44SPaul Mundt return ret;
182f905bc44SPaul Mundt }
183ed81745aSAndrii Nakryiko
vmalloc_user(unsigned long size)184ed81745aSAndrii Nakryiko void *vmalloc_user(unsigned long size)
185ed81745aSAndrii Nakryiko {
186ed81745aSAndrii Nakryiko return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
187ed81745aSAndrii Nakryiko }
188f905bc44SPaul Mundt EXPORT_SYMBOL(vmalloc_user);
189f905bc44SPaul Mundt
vmalloc_to_page(const void * addr)190b3bdda02SChristoph Lameter struct page *vmalloc_to_page(const void *addr)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds return virt_to_page(addr);
1931da177e4SLinus Torvalds }
194b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_page);
1951da177e4SLinus Torvalds
vmalloc_to_pfn(const void * addr)196b3bdda02SChristoph Lameter unsigned long vmalloc_to_pfn(const void *addr)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds return page_to_pfn(virt_to_page(addr));
1991da177e4SLinus Torvalds }
200b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_to_pfn);
2011da177e4SLinus Torvalds
vread_iter(struct iov_iter * iter,const char * addr,size_t count)2024c91c07cSLorenzo Stoakes long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
2031da177e4SLinus Torvalds {
2049bde916bSChen Gang /* Don't allow overflow */
2054c91c07cSLorenzo Stoakes if ((unsigned long) addr + count < count)
2064c91c07cSLorenzo Stoakes count = -(unsigned long) addr;
2079bde916bSChen Gang
2084c91c07cSLorenzo Stoakes return copy_to_iter(addr, count, iter);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds /*
212e1c05067SMasahiro Yamada * vmalloc - allocate virtually contiguous memory
2131da177e4SLinus Torvalds *
2141da177e4SLinus Torvalds * @size: allocation size
2151da177e4SLinus Torvalds *
2161da177e4SLinus Torvalds * Allocate enough pages to cover @size from the page level
217e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space.
2181da177e4SLinus Torvalds *
219c1c8897fSMichael Opdenacker * For tight control over page level allocator and protection flags
2201da177e4SLinus Torvalds * use __vmalloc() instead.
2211da177e4SLinus Torvalds */
vmalloc(unsigned long size)2221da177e4SLinus Torvalds void *vmalloc(unsigned long size)
2231da177e4SLinus Torvalds {
224176056fdSChen Li return __vmalloc(size, GFP_KERNEL);
2251da177e4SLinus Torvalds }
226f6138882SAndrew Morton EXPORT_SYMBOL(vmalloc);
227f6138882SAndrew Morton
2280fc74d82SLinus Torvalds void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __weak __alias(__vmalloc);
2290fc74d82SLinus Torvalds
230e1ca7788SDave Young /*
231e1c05067SMasahiro Yamada * vzalloc - allocate virtually contiguous memory with zero fill
232e1ca7788SDave Young *
233e1ca7788SDave Young * @size: allocation size
234e1ca7788SDave Young *
235e1ca7788SDave Young * Allocate enough pages to cover @size from the page level
236e1c05067SMasahiro Yamada * allocator and map them into contiguous kernel virtual space.
237e1ca7788SDave Young * The memory allocated is set to zero.
238e1ca7788SDave Young *
239e1ca7788SDave Young * For tight control over page level allocator and protection flags
240e1ca7788SDave Young * use __vmalloc() instead.
241e1ca7788SDave Young */
vzalloc(unsigned long size)242e1ca7788SDave Young void *vzalloc(unsigned long size)
243e1ca7788SDave Young {
244176056fdSChen Li return __vmalloc(size, GFP_KERNEL | __GFP_ZERO);
245e1ca7788SDave Young }
246e1ca7788SDave Young EXPORT_SYMBOL(vzalloc);
247e1ca7788SDave Young
248e1ca7788SDave Young /**
249e1ca7788SDave Young * vmalloc_node - allocate memory on a specific node
250e1ca7788SDave Young * @size: allocation size
251e1ca7788SDave Young * @node: numa node
252e1ca7788SDave Young *
253e1ca7788SDave Young * Allocate enough pages to cover @size from the page level
254e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space.
255e1ca7788SDave Young *
256e1ca7788SDave Young * For tight control over page level allocator and protection flags
257e1ca7788SDave Young * use __vmalloc() instead.
258e1ca7788SDave Young */
vmalloc_node(unsigned long size,int node)259f6138882SAndrew Morton void *vmalloc_node(unsigned long size, int node)
260f6138882SAndrew Morton {
261f6138882SAndrew Morton return vmalloc(size);
262f6138882SAndrew Morton }
2639a14f653SPaul Mundt EXPORT_SYMBOL(vmalloc_node);
264e1ca7788SDave Young
265e1ca7788SDave Young /**
266e1ca7788SDave Young * vzalloc_node - allocate memory on a specific node with zero fill
267e1ca7788SDave Young * @size: allocation size
268e1ca7788SDave Young * @node: numa node
269e1ca7788SDave Young *
270e1ca7788SDave Young * Allocate enough pages to cover @size from the page level
271e1ca7788SDave Young * allocator and map them into contiguous kernel virtual space.
272e1ca7788SDave Young * The memory allocated is set to zero.
273e1ca7788SDave Young *
274e1ca7788SDave Young * For tight control over page level allocator and protection flags
275e1ca7788SDave Young * use __vmalloc() instead.
276e1ca7788SDave Young */
vzalloc_node(unsigned long size,int node)277e1ca7788SDave Young void *vzalloc_node(unsigned long size, int node)
278e1ca7788SDave Young {
279e1ca7788SDave Young return vzalloc(size);
280e1ca7788SDave Young }
281e1ca7788SDave Young EXPORT_SYMBOL(vzalloc_node);
2821da177e4SLinus Torvalds
2831af446edSPaul Mundt /**
284b5073173SPaul Mundt * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
2851da177e4SLinus Torvalds * @size: allocation size
2861da177e4SLinus Torvalds *
2871da177e4SLinus Torvalds * Allocate enough 32bit PA addressable pages to cover @size from the
288e1c05067SMasahiro Yamada * page level allocator and map them into contiguous kernel virtual space.
2891da177e4SLinus Torvalds */
vmalloc_32(unsigned long size)2901da177e4SLinus Torvalds void *vmalloc_32(unsigned long size)
2911da177e4SLinus Torvalds {
29288dca4caSChristoph Hellwig return __vmalloc(size, GFP_KERNEL);
2931da177e4SLinus Torvalds }
294b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32);
295b5073173SPaul Mundt
296b5073173SPaul Mundt /**
297b5073173SPaul Mundt * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
298b5073173SPaul Mundt * @size: allocation size
299b5073173SPaul Mundt *
300b5073173SPaul Mundt * The resulting memory area is 32bit addressable and zeroed so it can be
301b5073173SPaul Mundt * mapped to userspace without leaking data.
302f905bc44SPaul Mundt *
303f905bc44SPaul Mundt * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
304f905bc44SPaul Mundt * remap_vmalloc_range() are permissible.
305b5073173SPaul Mundt */
vmalloc_32_user(unsigned long size)306b5073173SPaul Mundt void *vmalloc_32_user(unsigned long size)
307b5073173SPaul Mundt {
308f905bc44SPaul Mundt /*
309f905bc44SPaul Mundt * We'll have to sort out the ZONE_DMA bits for 64-bit,
310f905bc44SPaul Mundt * but for now this can simply use vmalloc_user() directly.
311f905bc44SPaul Mundt */
312f905bc44SPaul Mundt return vmalloc_user(size);
313b5073173SPaul Mundt }
314b5073173SPaul Mundt EXPORT_SYMBOL(vmalloc_32_user);
3151da177e4SLinus Torvalds
vmap(struct page ** pages,unsigned int count,unsigned long flags,pgprot_t prot)3161da177e4SLinus Torvalds void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds BUG();
3191da177e4SLinus Torvalds return NULL;
3201da177e4SLinus Torvalds }
321b5073173SPaul Mundt EXPORT_SYMBOL(vmap);
3221da177e4SLinus Torvalds
vunmap(const void * addr)323b3bdda02SChristoph Lameter void vunmap(const void *addr)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds BUG();
3261da177e4SLinus Torvalds }
327b5073173SPaul Mundt EXPORT_SYMBOL(vunmap);
3281da177e4SLinus Torvalds
vm_map_ram(struct page ** pages,unsigned int count,int node)329d4efd79aSChristoph Hellwig void *vm_map_ram(struct page **pages, unsigned int count, int node)
330eb6434d9SPaul Mundt {
331eb6434d9SPaul Mundt BUG();
332eb6434d9SPaul Mundt return NULL;
333eb6434d9SPaul Mundt }
334eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_map_ram);
335eb6434d9SPaul Mundt
vm_unmap_ram(const void * mem,unsigned int count)336eb6434d9SPaul Mundt void vm_unmap_ram(const void *mem, unsigned int count)
337eb6434d9SPaul Mundt {
338eb6434d9SPaul Mundt BUG();
339eb6434d9SPaul Mundt }
340eb6434d9SPaul Mundt EXPORT_SYMBOL(vm_unmap_ram);
341eb6434d9SPaul Mundt
vm_unmap_aliases(void)342eb6434d9SPaul Mundt void vm_unmap_aliases(void)
343eb6434d9SPaul Mundt {
344eb6434d9SPaul Mundt }
345eb6434d9SPaul Mundt EXPORT_SYMBOL_GPL(vm_unmap_aliases);
346eb6434d9SPaul Mundt
free_vm_area(struct vm_struct * area)34729c185e5SPaul Mundt void free_vm_area(struct vm_struct *area)
34829c185e5SPaul Mundt {
34929c185e5SPaul Mundt BUG();
35029c185e5SPaul Mundt }
35129c185e5SPaul Mundt EXPORT_SYMBOL_GPL(free_vm_area);
35229c185e5SPaul Mundt
vm_insert_page(struct vm_area_struct * vma,unsigned long addr,struct page * page)353b5073173SPaul Mundt int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
354b5073173SPaul Mundt struct page *page)
355b5073173SPaul Mundt {
356b5073173SPaul Mundt return -EINVAL;
357b5073173SPaul Mundt }
358b5073173SPaul Mundt EXPORT_SYMBOL(vm_insert_page);
359b5073173SPaul Mundt
vm_map_pages(struct vm_area_struct * vma,struct page ** pages,unsigned long num)360a667d745SSouptick Joarder int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
361a667d745SSouptick Joarder unsigned long num)
362a667d745SSouptick Joarder {
363a667d745SSouptick Joarder return -EINVAL;
364a667d745SSouptick Joarder }
365a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages);
366a667d745SSouptick Joarder
vm_map_pages_zero(struct vm_area_struct * vma,struct page ** pages,unsigned long num)367a667d745SSouptick Joarder int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
368a667d745SSouptick Joarder unsigned long num)
369a667d745SSouptick Joarder {
370a667d745SSouptick Joarder return -EINVAL;
371a667d745SSouptick Joarder }
372a667d745SSouptick Joarder EXPORT_SYMBOL(vm_map_pages_zero);
373a667d745SSouptick Joarder
3741eeb66a1SChristoph Hellwig /*
3751da177e4SLinus Torvalds * sys_brk() for the most part doesn't need the global kernel
3761da177e4SLinus Torvalds * lock, except when an application is doing something nasty
3771da177e4SLinus Torvalds * like trying to un-brk an area that has already been mapped
3781da177e4SLinus Torvalds * to a regular file. in this case, the unmapping will need
3791da177e4SLinus Torvalds * to invoke file system routines that need the global lock.
3801da177e4SLinus Torvalds */
SYSCALL_DEFINE1(brk,unsigned long,brk)3816a6160a7SHeiko Carstens SYSCALL_DEFINE1(brk, unsigned long, brk)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds struct mm_struct *mm = current->mm;
3841da177e4SLinus Torvalds
3851da177e4SLinus Torvalds if (brk < mm->start_brk || brk > mm->context.end_brk)
3861da177e4SLinus Torvalds return mm->brk;
3871da177e4SLinus Torvalds
3881da177e4SLinus Torvalds if (mm->brk == brk)
3891da177e4SLinus Torvalds return mm->brk;
3901da177e4SLinus Torvalds
3911da177e4SLinus Torvalds /*
3921da177e4SLinus Torvalds * Always allow shrinking brk
3931da177e4SLinus Torvalds */
3941da177e4SLinus Torvalds if (brk <= mm->brk) {
3951da177e4SLinus Torvalds mm->brk = brk;
3961da177e4SLinus Torvalds return brk;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds
3991da177e4SLinus Torvalds /*
4001da177e4SLinus Torvalds * Ok, looks good - let it rip.
4011da177e4SLinus Torvalds */
402a75a2df6SChristoph Hellwig flush_icache_user_range(mm->brk, brk);
4031da177e4SLinus Torvalds return mm->brk = brk;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds
4068feae131SDavid Howells /*
4073edf41d8Sseokhoon.yoon * initialise the percpu counter for VM and region record slabs
4088feae131SDavid Howells */
mmap_init(void)4098feae131SDavid Howells void __init mmap_init(void)
4101da177e4SLinus Torvalds {
41100a62ce9SKOSAKI Motohiro int ret;
41200a62ce9SKOSAKI Motohiro
413908c7f19STejun Heo ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
41400a62ce9SKOSAKI Motohiro VM_BUG_ON(ret);
4155d097056SVladimir Davydov vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
4168feae131SDavid Howells }
4171da177e4SLinus Torvalds
4188feae131SDavid Howells /*
4198feae131SDavid Howells * validate the region tree
4208feae131SDavid Howells * - the caller must hold the region lock
4218feae131SDavid Howells */
4228feae131SDavid Howells #ifdef CONFIG_DEBUG_NOMMU_REGIONS
validate_nommu_regions(void)4238feae131SDavid Howells static noinline void validate_nommu_regions(void)
4248feae131SDavid Howells {
4258feae131SDavid Howells struct vm_region *region, *last;
4268feae131SDavid Howells struct rb_node *p, *lastp;
4271da177e4SLinus Torvalds
4288feae131SDavid Howells lastp = rb_first(&nommu_region_tree);
4298feae131SDavid Howells if (!lastp)
4308feae131SDavid Howells return;
4318feae131SDavid Howells
4328feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb);
433c9427bc0SGeliang Tang BUG_ON(last->vm_end <= last->vm_start);
434c9427bc0SGeliang Tang BUG_ON(last->vm_top < last->vm_end);
4358feae131SDavid Howells
4368feae131SDavid Howells while ((p = rb_next(lastp))) {
4378feae131SDavid Howells region = rb_entry(p, struct vm_region, vm_rb);
4388feae131SDavid Howells last = rb_entry(lastp, struct vm_region, vm_rb);
4398feae131SDavid Howells
440c9427bc0SGeliang Tang BUG_ON(region->vm_end <= region->vm_start);
441c9427bc0SGeliang Tang BUG_ON(region->vm_top < region->vm_end);
442c9427bc0SGeliang Tang BUG_ON(region->vm_start < last->vm_top);
4438feae131SDavid Howells
4448feae131SDavid Howells lastp = p;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds }
4478feae131SDavid Howells #else
validate_nommu_regions(void)44833e5d769SDavid Howells static void validate_nommu_regions(void)
44933e5d769SDavid Howells {
45033e5d769SDavid Howells }
4518feae131SDavid Howells #endif
4528feae131SDavid Howells
4538feae131SDavid Howells /*
4548feae131SDavid Howells * add a region into the global tree
4558feae131SDavid Howells */
add_nommu_region(struct vm_region * region)4568feae131SDavid Howells static void add_nommu_region(struct vm_region *region)
4578feae131SDavid Howells {
4588feae131SDavid Howells struct vm_region *pregion;
4598feae131SDavid Howells struct rb_node **p, *parent;
4608feae131SDavid Howells
4618feae131SDavid Howells validate_nommu_regions();
4628feae131SDavid Howells
4638feae131SDavid Howells parent = NULL;
4648feae131SDavid Howells p = &nommu_region_tree.rb_node;
4658feae131SDavid Howells while (*p) {
4668feae131SDavid Howells parent = *p;
4678feae131SDavid Howells pregion = rb_entry(parent, struct vm_region, vm_rb);
4688feae131SDavid Howells if (region->vm_start < pregion->vm_start)
4698feae131SDavid Howells p = &(*p)->rb_left;
4708feae131SDavid Howells else if (region->vm_start > pregion->vm_start)
4718feae131SDavid Howells p = &(*p)->rb_right;
4728feae131SDavid Howells else if (pregion == region)
4738feae131SDavid Howells return;
4748feae131SDavid Howells else
4758feae131SDavid Howells BUG();
4768feae131SDavid Howells }
4778feae131SDavid Howells
4788feae131SDavid Howells rb_link_node(®ion->vm_rb, parent, p);
4798feae131SDavid Howells rb_insert_color(®ion->vm_rb, &nommu_region_tree);
4808feae131SDavid Howells
4818feae131SDavid Howells validate_nommu_regions();
4828feae131SDavid Howells }
4838feae131SDavid Howells
4848feae131SDavid Howells /*
4858feae131SDavid Howells * delete a region from the global tree
4868feae131SDavid Howells */
delete_nommu_region(struct vm_region * region)4878feae131SDavid Howells static void delete_nommu_region(struct vm_region *region)
4888feae131SDavid Howells {
4898feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node);
4908feae131SDavid Howells
4918feae131SDavid Howells validate_nommu_regions();
4928feae131SDavid Howells rb_erase(®ion->vm_rb, &nommu_region_tree);
4938feae131SDavid Howells validate_nommu_regions();
4948feae131SDavid Howells }
4958feae131SDavid Howells
4968feae131SDavid Howells /*
4978feae131SDavid Howells * free a contiguous series of pages
4988feae131SDavid Howells */
free_page_series(unsigned long from,unsigned long to)4998feae131SDavid Howells static void free_page_series(unsigned long from, unsigned long to)
5008feae131SDavid Howells {
5018feae131SDavid Howells for (; from < to; from += PAGE_SIZE) {
5029330723cSLinus Walleij struct page *page = virt_to_page((void *)from);
5038feae131SDavid Howells
50433e5d769SDavid Howells atomic_long_dec(&mmap_pages_allocated);
5058feae131SDavid Howells put_page(page);
5068feae131SDavid Howells }
5078feae131SDavid Howells }
5088feae131SDavid Howells
5098feae131SDavid Howells /*
5108feae131SDavid Howells * release a reference to a region
51133e5d769SDavid Howells * - the caller must hold the region semaphore for writing, which this releases
512dd8632a1SPaul Mundt * - the region may not have been added to the tree yet, in which case vm_top
5138feae131SDavid Howells * will equal vm_start
5148feae131SDavid Howells */
__put_nommu_region(struct vm_region * region)5158feae131SDavid Howells static void __put_nommu_region(struct vm_region *region)
5168feae131SDavid Howells __releases(nommu_region_sem)
5178feae131SDavid Howells {
5188feae131SDavid Howells BUG_ON(!nommu_region_tree.rb_node);
5198feae131SDavid Howells
5201e2ae599SDavid Howells if (--region->vm_usage == 0) {
521dd8632a1SPaul Mundt if (region->vm_top > region->vm_start)
5228feae131SDavid Howells delete_nommu_region(region);
5238feae131SDavid Howells up_write(&nommu_region_sem);
5248feae131SDavid Howells
5258feae131SDavid Howells if (region->vm_file)
5268feae131SDavid Howells fput(region->vm_file);
5278feae131SDavid Howells
5288feae131SDavid Howells /* IO memory and memory shared directly out of the pagecache
5298feae131SDavid Howells * from ramfs/tmpfs mustn't be released here */
53022cc877bSLeon Romanovsky if (region->vm_flags & VM_MAPPED_COPY)
531dd8632a1SPaul Mundt free_page_series(region->vm_start, region->vm_top);
5328feae131SDavid Howells kmem_cache_free(vm_region_jar, region);
5338feae131SDavid Howells } else {
5348feae131SDavid Howells up_write(&nommu_region_sem);
5358feae131SDavid Howells }
5368feae131SDavid Howells }
5378feae131SDavid Howells
5388feae131SDavid Howells /*
5398feae131SDavid Howells * release a reference to a region
5408feae131SDavid Howells */
put_nommu_region(struct vm_region * region)5418feae131SDavid Howells static void put_nommu_region(struct vm_region *region)
5428feae131SDavid Howells {
5438feae131SDavid Howells down_write(&nommu_region_sem);
5448feae131SDavid Howells __put_nommu_region(region);
5458feae131SDavid Howells }
5461da177e4SLinus Torvalds
setup_vma_to_mm(struct vm_area_struct * vma,struct mm_struct * mm)5478220543dSMatthew Wilcox (Oracle) static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)
5483034097aSDavid Howells {
5498feae131SDavid Howells vma->vm_mm = mm;
5508feae131SDavid Howells
5518feae131SDavid Howells /* add the VMA to the mapping */
5528feae131SDavid Howells if (vma->vm_file) {
5538220543dSMatthew Wilcox (Oracle) struct address_space *mapping = vma->vm_file->f_mapping;
5548feae131SDavid Howells
55583cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping);
5568feae131SDavid Howells flush_dcache_mmap_lock(mapping);
5576b2dbba8SMichel Lespinasse vma_interval_tree_insert(vma, &mapping->i_mmap);
5588feae131SDavid Howells flush_dcache_mmap_unlock(mapping);
55983cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping);
5608feae131SDavid Howells }
5618220543dSMatthew Wilcox (Oracle) }
5628feae131SDavid Howells
cleanup_vma_from_mm(struct vm_area_struct * vma)5638220543dSMatthew Wilcox (Oracle) static void cleanup_vma_from_mm(struct vm_area_struct *vma)
5648220543dSMatthew Wilcox (Oracle) {
5657964cf8cSLiam R. Howlett vma->vm_mm->map_count--;
5668feae131SDavid Howells /* remove the VMA from the mapping */
5678feae131SDavid Howells if (vma->vm_file) {
5687964cf8cSLiam R. Howlett struct address_space *mapping;
5698feae131SDavid Howells mapping = vma->vm_file->f_mapping;
5708feae131SDavid Howells
57183cde9e8SDavidlohr Bueso i_mmap_lock_write(mapping);
5728feae131SDavid Howells flush_dcache_mmap_lock(mapping);
5736b2dbba8SMichel Lespinasse vma_interval_tree_remove(vma, &mapping->i_mmap);
5748feae131SDavid Howells flush_dcache_mmap_unlock(mapping);
57583cde9e8SDavidlohr Bueso i_mmap_unlock_write(mapping);
5768feae131SDavid Howells }
5778220543dSMatthew Wilcox (Oracle) }
57847d9644dSLiam R. Howlett
5798220543dSMatthew Wilcox (Oracle) /*
5808220543dSMatthew Wilcox (Oracle) * delete a VMA from its owning mm_struct and address space
5818220543dSMatthew Wilcox (Oracle) */
delete_vma_from_mm(struct vm_area_struct * vma)5828220543dSMatthew Wilcox (Oracle) static int delete_vma_from_mm(struct vm_area_struct *vma)
5838220543dSMatthew Wilcox (Oracle) {
58447d9644dSLiam R. Howlett VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start);
5858220543dSMatthew Wilcox (Oracle)
586b5df0922SLiam R. Howlett vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
5878bbf0ab6SHajime Tazaki if (vma_iter_prealloc(&vmi, NULL)) {
5888220543dSMatthew Wilcox (Oracle) pr_warn("Allocation of vma tree for process %d failed\n",
5898220543dSMatthew Wilcox (Oracle) current->pid);
5908220543dSMatthew Wilcox (Oracle) return -ENOMEM;
5918220543dSMatthew Wilcox (Oracle) }
5928220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma);
5938feae131SDavid Howells
5948feae131SDavid Howells /* remove from the MM's tree and list */
595b5df0922SLiam R. Howlett vma_iter_clear(&vmi);
5968220543dSMatthew Wilcox (Oracle) return 0;
5978feae131SDavid Howells }
5988feae131SDavid Howells /*
5998feae131SDavid Howells * destroy a VMA record
6008feae131SDavid Howells */
delete_vma(struct mm_struct * mm,struct vm_area_struct * vma)6018feae131SDavid Howells static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
6028feae131SDavid Howells {
603a97fe688SLorenzo Stoakes vma_close(vma);
604e9714acfSKonstantin Khlebnikov if (vma->vm_file)
6058feae131SDavid Howells fput(vma->vm_file);
6068feae131SDavid Howells put_nommu_region(vma->vm_region);
6073928d4f5SLinus Torvalds vm_area_free(vma);
6083034097aSDavid Howells }
6093034097aSDavid Howells
find_vma_intersection(struct mm_struct * mm,unsigned long start_addr,unsigned long end_addr)610abdba2ddSLiam R. Howlett struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
611abdba2ddSLiam R. Howlett unsigned long start_addr,
612abdba2ddSLiam R. Howlett unsigned long end_addr)
613abdba2ddSLiam R. Howlett {
614abdba2ddSLiam R. Howlett unsigned long index = start_addr;
615abdba2ddSLiam R. Howlett
616abdba2ddSLiam R. Howlett mmap_assert_locked(mm);
617abdba2ddSLiam R. Howlett return mt_find(&mm->mm_mt, &index, end_addr - 1);
618abdba2ddSLiam R. Howlett }
619abdba2ddSLiam R. Howlett EXPORT_SYMBOL(find_vma_intersection);
620abdba2ddSLiam R. Howlett
6213034097aSDavid Howells /*
6223034097aSDavid Howells * look up the first VMA in which addr resides, NULL if none
623c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked
6243034097aSDavid Howells */
find_vma(struct mm_struct * mm,unsigned long addr)6253034097aSDavid Howells struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
6263034097aSDavid Howells {
62747d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr);
6283034097aSDavid Howells
62947d9644dSLiam R. Howlett return vma_iter_load(&vmi);
6303034097aSDavid Howells }
6313034097aSDavid Howells EXPORT_SYMBOL(find_vma);
6323034097aSDavid Howells
6333034097aSDavid Howells /*
634d85a143bSLinus Torvalds * At least xtensa ends up having protection faults even with no
635d85a143bSLinus Torvalds * MMU.. No stack expansion, at least.
636d85a143bSLinus Torvalds */
lock_mm_and_find_vma(struct mm_struct * mm,unsigned long addr,struct pt_regs * regs)637d85a143bSLinus Torvalds struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
638d85a143bSLinus Torvalds unsigned long addr, struct pt_regs *regs)
639d85a143bSLinus Torvalds {
64003f88937SMax Filippov struct vm_area_struct *vma;
64103f88937SMax Filippov
642d85a143bSLinus Torvalds mmap_read_lock(mm);
64303f88937SMax Filippov vma = vma_lookup(mm, addr);
64403f88937SMax Filippov if (!vma)
64503f88937SMax Filippov mmap_read_unlock(mm);
64603f88937SMax Filippov return vma;
647d85a143bSLinus Torvalds }
648d85a143bSLinus Torvalds
649d85a143bSLinus Torvalds /*
6508feae131SDavid Howells * expand a stack to a given address
6518feae131SDavid Howells * - not supported under NOMMU conditions
6528feae131SDavid Howells */
expand_stack_locked(struct vm_area_struct * vma,unsigned long addr)6538d7071afSLinus Torvalds int expand_stack_locked(struct vm_area_struct *vma, unsigned long addr)
65457c8f63eSGreg Ungerer {
65557c8f63eSGreg Ungerer return -ENOMEM;
65657c8f63eSGreg Ungerer }
65757c8f63eSGreg Ungerer
expand_stack(struct mm_struct * mm,unsigned long addr)6588d7071afSLinus Torvalds struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr)
6598d7071afSLinus Torvalds {
6608d7071afSLinus Torvalds mmap_read_unlock(mm);
6618d7071afSLinus Torvalds return NULL;
6628d7071afSLinus Torvalds }
6638d7071afSLinus Torvalds
664930e652aSDavid Howells /*
6656fa5f80bSDavid Howells * look up the first VMA exactly that exactly matches addr
666c1e8d7c6SMichel Lespinasse * - should be called with mm->mmap_lock at least held readlocked
6676fa5f80bSDavid Howells */
find_vma_exact(struct mm_struct * mm,unsigned long addr,unsigned long len)6688feae131SDavid Howells static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
6698feae131SDavid Howells unsigned long addr,
6708feae131SDavid Howells unsigned long len)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds struct vm_area_struct *vma;
6738feae131SDavid Howells unsigned long end = addr + len;
67447d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, addr);
6751da177e4SLinus Torvalds
67647d9644dSLiam R. Howlett vma = vma_iter_load(&vmi);
677524e00b3SLiam R. Howlett if (!vma)
6788feae131SDavid Howells return NULL;
679524e00b3SLiam R. Howlett if (vma->vm_start != addr)
680524e00b3SLiam R. Howlett return NULL;
681524e00b3SLiam R. Howlett if (vma->vm_end != end)
682524e00b3SLiam R. Howlett return NULL;
683524e00b3SLiam R. Howlett
6848feae131SDavid Howells return vma;
6858feae131SDavid Howells }
6861da177e4SLinus Torvalds
6873034097aSDavid Howells /*
6881da177e4SLinus Torvalds * determine whether a mapping should be permitted and, if so, what sort of
6891da177e4SLinus Torvalds * mapping we're capable of supporting
6901da177e4SLinus Torvalds */
validate_mmap_request(struct file * file,unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long pgoff,unsigned long * _capabilities)6911da177e4SLinus Torvalds static int validate_mmap_request(struct file *file,
6921da177e4SLinus Torvalds unsigned long addr,
6931da177e4SLinus Torvalds unsigned long len,
6941da177e4SLinus Torvalds unsigned long prot,
6951da177e4SLinus Torvalds unsigned long flags,
6961da177e4SLinus Torvalds unsigned long pgoff,
6971da177e4SLinus Torvalds unsigned long *_capabilities)
6981da177e4SLinus Torvalds {
6998feae131SDavid Howells unsigned long capabilities, rlen;
7001da177e4SLinus Torvalds int ret;
7011da177e4SLinus Torvalds
7021da177e4SLinus Torvalds /* do the simple checks first */
70322cc877bSLeon Romanovsky if (flags & MAP_FIXED)
7041da177e4SLinus Torvalds return -EINVAL;
7051da177e4SLinus Torvalds
7061da177e4SLinus Torvalds if ((flags & MAP_TYPE) != MAP_PRIVATE &&
7071da177e4SLinus Torvalds (flags & MAP_TYPE) != MAP_SHARED)
7081da177e4SLinus Torvalds return -EINVAL;
7091da177e4SLinus Torvalds
710f81cff0dSMike Frysinger if (!len)
7111da177e4SLinus Torvalds return -EINVAL;
7121da177e4SLinus Torvalds
713f81cff0dSMike Frysinger /* Careful about overflows.. */
7148feae131SDavid Howells rlen = PAGE_ALIGN(len);
7158feae131SDavid Howells if (!rlen || rlen > TASK_SIZE)
716f81cff0dSMike Frysinger return -ENOMEM;
717f81cff0dSMike Frysinger
7181da177e4SLinus Torvalds /* offset overflow? */
7198feae131SDavid Howells if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
720f81cff0dSMike Frysinger return -EOVERFLOW;
7211da177e4SLinus Torvalds
7221da177e4SLinus Torvalds if (file) {
7231da177e4SLinus Torvalds /* files must support mmap */
72472c2d531SAl Viro if (!file->f_op->mmap)
7251da177e4SLinus Torvalds return -ENODEV;
7261da177e4SLinus Torvalds
7271da177e4SLinus Torvalds /* work out if what we've got could possibly be shared
7281da177e4SLinus Torvalds * - we support chardevs that provide their own "memory"
7291da177e4SLinus Torvalds * - we support files/blockdevs that are memory backed
7301da177e4SLinus Torvalds */
731b4caecd4SChristoph Hellwig if (file->f_op->mmap_capabilities) {
732b4caecd4SChristoph Hellwig capabilities = file->f_op->mmap_capabilities(file);
733b4caecd4SChristoph Hellwig } else {
7341da177e4SLinus Torvalds /* no explicit capabilities set, so assume some
7351da177e4SLinus Torvalds * defaults */
736496ad9aaSAl Viro switch (file_inode(file)->i_mode & S_IFMT) {
7371da177e4SLinus Torvalds case S_IFREG:
7381da177e4SLinus Torvalds case S_IFBLK:
739b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY;
7401da177e4SLinus Torvalds break;
7411da177e4SLinus Torvalds
7421da177e4SLinus Torvalds case S_IFCHR:
7431da177e4SLinus Torvalds capabilities =
744b4caecd4SChristoph Hellwig NOMMU_MAP_DIRECT |
745b4caecd4SChristoph Hellwig NOMMU_MAP_READ |
746b4caecd4SChristoph Hellwig NOMMU_MAP_WRITE;
7471da177e4SLinus Torvalds break;
7481da177e4SLinus Torvalds
7491da177e4SLinus Torvalds default:
7501da177e4SLinus Torvalds return -EINVAL;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds
7541da177e4SLinus Torvalds /* eliminate any capabilities that we can't support on this
7551da177e4SLinus Torvalds * device */
7561da177e4SLinus Torvalds if (!file->f_op->get_unmapped_area)
757b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT;
7586e242a1cSAl Viro if (!(file->f_mode & FMODE_CAN_READ))
759b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY;
7601da177e4SLinus Torvalds
76128d7a6aeSGraff Yang /* The file shall have been opened with read permission. */
76228d7a6aeSGraff Yang if (!(file->f_mode & FMODE_READ))
76328d7a6aeSGraff Yang return -EACCES;
76428d7a6aeSGraff Yang
7651da177e4SLinus Torvalds if (flags & MAP_SHARED) {
7661da177e4SLinus Torvalds /* do checks for writing, appending and locking */
7671da177e4SLinus Torvalds if ((prot & PROT_WRITE) &&
7681da177e4SLinus Torvalds !(file->f_mode & FMODE_WRITE))
7691da177e4SLinus Torvalds return -EACCES;
7701da177e4SLinus Torvalds
771496ad9aaSAl Viro if (IS_APPEND(file_inode(file)) &&
7721da177e4SLinus Torvalds (file->f_mode & FMODE_WRITE))
7731da177e4SLinus Torvalds return -EACCES;
7741da177e4SLinus Torvalds
775b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT))
7761da177e4SLinus Torvalds return -ENODEV;
7771da177e4SLinus Torvalds
7781da177e4SLinus Torvalds /* we mustn't privatise shared mappings */
779b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_COPY;
780ac714904SChoi Gi-yong } else {
7811da177e4SLinus Torvalds /* we're going to read the file into private memory we
7821da177e4SLinus Torvalds * allocate */
783b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY))
7841da177e4SLinus Torvalds return -ENODEV;
7851da177e4SLinus Torvalds
7861da177e4SLinus Torvalds /* we don't permit a private writable mapping to be
7871da177e4SLinus Torvalds * shared with the backing device */
7881da177e4SLinus Torvalds if (prot & PROT_WRITE)
789b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds
792b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) {
793b4caecd4SChristoph Hellwig if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
794b4caecd4SChristoph Hellwig ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
795b4caecd4SChristoph Hellwig ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
7963c7b2045SBernd Schmidt ) {
797b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT;
7983c7b2045SBernd Schmidt if (flags & MAP_SHARED) {
79922cc877bSLeon Romanovsky pr_warn("MAP_SHARED not completely supported on !MMU\n");
8003c7b2045SBernd Schmidt return -EINVAL;
8013c7b2045SBernd Schmidt }
8023c7b2045SBernd Schmidt }
8033c7b2045SBernd Schmidt }
8043c7b2045SBernd Schmidt
8051da177e4SLinus Torvalds /* handle executable mappings and implied executable
8061da177e4SLinus Torvalds * mappings */
80790f8572bSEric W. Biederman if (path_noexec(&file->f_path)) {
8081da177e4SLinus Torvalds if (prot & PROT_EXEC)
8091da177e4SLinus Torvalds return -EPERM;
810ac714904SChoi Gi-yong } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
8111da177e4SLinus Torvalds /* handle implication of PROT_EXEC by PROT_READ */
8121da177e4SLinus Torvalds if (current->personality & READ_IMPLIES_EXEC) {
813b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_EXEC)
8141da177e4SLinus Torvalds prot |= PROT_EXEC;
8151da177e4SLinus Torvalds }
816ac714904SChoi Gi-yong } else if ((prot & PROT_READ) &&
8171da177e4SLinus Torvalds (prot & PROT_EXEC) &&
818b4caecd4SChristoph Hellwig !(capabilities & NOMMU_MAP_EXEC)
8191da177e4SLinus Torvalds ) {
8201da177e4SLinus Torvalds /* backing file is not executable, try to copy */
821b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT;
8221da177e4SLinus Torvalds }
823ac714904SChoi Gi-yong } else {
8241da177e4SLinus Torvalds /* anonymous mappings are always memory backed and can be
8251da177e4SLinus Torvalds * privately mapped
8261da177e4SLinus Torvalds */
827b4caecd4SChristoph Hellwig capabilities = NOMMU_MAP_COPY;
8281da177e4SLinus Torvalds
8291da177e4SLinus Torvalds /* handle PROT_EXEC implication by PROT_READ */
8301da177e4SLinus Torvalds if ((prot & PROT_READ) &&
8311da177e4SLinus Torvalds (current->personality & READ_IMPLIES_EXEC))
8321da177e4SLinus Torvalds prot |= PROT_EXEC;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds
8351da177e4SLinus Torvalds /* allow the security API to have its say */
836e5467859SAl Viro ret = security_mmap_addr(addr);
837e5467859SAl Viro if (ret < 0)
838e5467859SAl Viro return ret;
8391da177e4SLinus Torvalds
8401da177e4SLinus Torvalds /* looks okay */
8411da177e4SLinus Torvalds *_capabilities = capabilities;
8421da177e4SLinus Torvalds return 0;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds
8451da177e4SLinus Torvalds /*
8461da177e4SLinus Torvalds * we've determined that we can make the mapping, now translate what we
8471da177e4SLinus Torvalds * now know into VMA flags
8481da177e4SLinus Torvalds */
determine_vm_flags(struct file * file,unsigned long prot,unsigned long flags,unsigned long capabilities)8491da177e4SLinus Torvalds static unsigned long determine_vm_flags(struct file *file,
8501da177e4SLinus Torvalds unsigned long prot,
8511da177e4SLinus Torvalds unsigned long flags,
8521da177e4SLinus Torvalds unsigned long capabilities)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds unsigned long vm_flags;
8551da177e4SLinus Torvalds
856*04b7efa4SLorenzo Stoakes vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags);
8571da177e4SLinus Torvalds
858b6b7a8faSDavid Hildenbrand if (!file) {
859b6b7a8faSDavid Hildenbrand /*
860b6b7a8faSDavid Hildenbrand * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because
861b6b7a8faSDavid Hildenbrand * there is no fork().
8621da177e4SLinus Torvalds */
863b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
864b6b7a8faSDavid Hildenbrand } else if (flags & MAP_PRIVATE) {
865b6b7a8faSDavid Hildenbrand /* MAP_PRIVATE file mapping */
866b6b7a8faSDavid Hildenbrand if (capabilities & NOMMU_MAP_DIRECT)
867b6b7a8faSDavid Hildenbrand vm_flags |= (capabilities & NOMMU_VMFLAGS);
868b6b7a8faSDavid Hildenbrand else
869b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
870b6b7a8faSDavid Hildenbrand
871b6b7a8faSDavid Hildenbrand if (!(prot & PROT_WRITE) && !current->ptrace)
872b6b7a8faSDavid Hildenbrand /*
873b6b7a8faSDavid Hildenbrand * R/O private file mapping which cannot be used to
874b6b7a8faSDavid Hildenbrand * modify memory, especially also not via active ptrace
875b6b7a8faSDavid Hildenbrand * (e.g., set breakpoints) or later by upgrading
876b6b7a8faSDavid Hildenbrand * permissions (no mprotect()). We can try overlaying
877b6b7a8faSDavid Hildenbrand * the file mapping, which will work e.g., on chardevs,
878b6b7a8faSDavid Hildenbrand * ramfs/tmpfs/shmfs and romfs/cramf.
879b6b7a8faSDavid Hildenbrand */
880b6b7a8faSDavid Hildenbrand vm_flags |= VM_MAYOVERLAY;
881b6b7a8faSDavid Hildenbrand } else {
882b6b7a8faSDavid Hildenbrand /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */
883b6b7a8faSDavid Hildenbrand vm_flags |= VM_SHARED | VM_MAYSHARE |
884b6b7a8faSDavid Hildenbrand (capabilities & NOMMU_VMFLAGS);
885b6b7a8faSDavid Hildenbrand }
8861da177e4SLinus Torvalds
8871da177e4SLinus Torvalds return vm_flags;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds
8901da177e4SLinus Torvalds /*
8918feae131SDavid Howells * set up a shared mapping on a file (the driver or filesystem provides and
8928feae131SDavid Howells * pins the storage)
8931da177e4SLinus Torvalds */
do_mmap_shared_file(struct vm_area_struct * vma)8948feae131SDavid Howells static int do_mmap_shared_file(struct vm_area_struct *vma)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds int ret;
8971da177e4SLinus Torvalds
898cd3ed99fSLorenzo Stoakes ret = mmap_file(vma->vm_file, vma);
899dd8632a1SPaul Mundt if (ret == 0) {
900dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end;
901645d83c5SDavid Howells return 0;
902dd8632a1SPaul Mundt }
9031da177e4SLinus Torvalds if (ret != -ENOSYS)
9041da177e4SLinus Torvalds return ret;
9051da177e4SLinus Torvalds
9063fa30460SDavid Howells /* getting -ENOSYS indicates that direct mmap isn't possible (as
9073fa30460SDavid Howells * opposed to tried but failed) so we can only give a suitable error as
9083fa30460SDavid Howells * it's not possible to make a private copy if MAP_SHARED was given */
9091da177e4SLinus Torvalds return -ENODEV;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds
9121da177e4SLinus Torvalds /*
9131da177e4SLinus Torvalds * set up a private mapping or an anonymous shared mapping
9141da177e4SLinus Torvalds */
do_mmap_private(struct vm_area_struct * vma,struct vm_region * region,unsigned long len,unsigned long capabilities)9158feae131SDavid Howells static int do_mmap_private(struct vm_area_struct *vma,
9168feae131SDavid Howells struct vm_region *region,
917645d83c5SDavid Howells unsigned long len,
918645d83c5SDavid Howells unsigned long capabilities)
9191da177e4SLinus Torvalds {
920dbc8358cSJoonsoo Kim unsigned long total, point;
9211da177e4SLinus Torvalds void *base;
9228feae131SDavid Howells int ret, order;
9231da177e4SLinus Torvalds
924b6b7a8faSDavid Hildenbrand /*
925b6b7a8faSDavid Hildenbrand * Invoke the file's mapping function so that it can keep track of
926b6b7a8faSDavid Hildenbrand * shared mappings on devices or memory. VM_MAYOVERLAY will be set if
927b6b7a8faSDavid Hildenbrand * it may attempt to share, which will make is_nommu_shared_mapping()
928b6b7a8faSDavid Hildenbrand * happy.
9291da177e4SLinus Torvalds */
930b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) {
931cd3ed99fSLorenzo Stoakes ret = mmap_file(vma->vm_file, vma);
9321da177e4SLinus Torvalds /* shouldn't return success if we're not sharing */
933fc4f4be9SDavid Hildenbrand if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags)))
934fc4f4be9SDavid Hildenbrand ret = -ENOSYS;
935fc4f4be9SDavid Hildenbrand if (ret == 0) {
936dd8632a1SPaul Mundt vma->vm_region->vm_top = vma->vm_region->vm_end;
937645d83c5SDavid Howells return 0;
9381da177e4SLinus Torvalds }
939dd8632a1SPaul Mundt if (ret != -ENOSYS)
940dd8632a1SPaul Mundt return ret;
9411da177e4SLinus Torvalds
9421da177e4SLinus Torvalds /* getting an ENOSYS error indicates that direct mmap isn't
9431da177e4SLinus Torvalds * possible (as opposed to tried but failed) so we'll try to
9441da177e4SLinus Torvalds * make a private copy of the data and map that instead */
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds
9478feae131SDavid Howells
9481da177e4SLinus Torvalds /* allocate some memory to hold the mapping
9491da177e4SLinus Torvalds * - note that this may not return a page-aligned address if the object
9501da177e4SLinus Torvalds * we're allocating is smaller than a page
9511da177e4SLinus Torvalds */
952f67d9b15SBob Liu order = get_order(len);
9538feae131SDavid Howells total = 1 << order;
954f67d9b15SBob Liu point = len >> PAGE_SHIFT;
955dd8632a1SPaul Mundt
956dbc8358cSJoonsoo Kim /* we don't want to allocate a power-of-2 sized page set */
95722cc877bSLeon Romanovsky if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
958dbc8358cSJoonsoo Kim total = point;
9598feae131SDavid Howells
960da616534SJoonsoo Kim base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
961dbc8358cSJoonsoo Kim if (!base)
962dbc8358cSJoonsoo Kim goto enomem;
9638feae131SDavid Howells
964dbc8358cSJoonsoo Kim atomic_long_add(total, &mmap_pages_allocated);
965dbc8358cSJoonsoo Kim
9661c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY);
9671c71222eSSuren Baghdasaryan region->vm_flags = vma->vm_flags;
9688feae131SDavid Howells region->vm_start = (unsigned long) base;
969f67d9b15SBob Liu region->vm_end = region->vm_start + len;
970dd8632a1SPaul Mundt region->vm_top = region->vm_start + (total << PAGE_SHIFT);
9718feae131SDavid Howells
9728feae131SDavid Howells vma->vm_start = region->vm_start;
9738feae131SDavid Howells vma->vm_end = region->vm_start + len;
9741da177e4SLinus Torvalds
9751da177e4SLinus Torvalds if (vma->vm_file) {
9761da177e4SLinus Torvalds /* read the contents of a file into the copy */
9771da177e4SLinus Torvalds loff_t fpos;
9781da177e4SLinus Torvalds
9791da177e4SLinus Torvalds fpos = vma->vm_pgoff;
9801da177e4SLinus Torvalds fpos <<= PAGE_SHIFT;
9811da177e4SLinus Torvalds
982b4bf802aSChristoph Hellwig ret = kernel_read(vma->vm_file, base, len, &fpos);
9831da177e4SLinus Torvalds if (ret < 0)
9841da177e4SLinus Torvalds goto error_free;
9851da177e4SLinus Torvalds
9861da177e4SLinus Torvalds /* clear the last little bit */
987f67d9b15SBob Liu if (ret < len)
988f67d9b15SBob Liu memset(base + ret, 0, len - ret);
9891da177e4SLinus Torvalds
990bfd40eafSKirill A. Shutemov } else {
991bfd40eafSKirill A. Shutemov vma_set_anonymous(vma);
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds
9941da177e4SLinus Torvalds return 0;
9951da177e4SLinus Torvalds
9961da177e4SLinus Torvalds error_free:
9977223bb4aSNamhyung Kim free_page_series(region->vm_start, region->vm_top);
9988feae131SDavid Howells region->vm_start = vma->vm_start = 0;
9998feae131SDavid Howells region->vm_end = vma->vm_end = 0;
1000dd8632a1SPaul Mundt region->vm_top = 0;
10011da177e4SLinus Torvalds return ret;
10021da177e4SLinus Torvalds
10031da177e4SLinus Torvalds enomem:
1004b1de0d13SMitchel Humpherys pr_err("Allocation of length %lu from process %d (%s) failed\n",
100505ae6fa3SGreg Ungerer len, current->pid, current->comm);
10061279aa06SKefeng Wang show_mem();
10071da177e4SLinus Torvalds return -ENOMEM;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds
10101da177e4SLinus Torvalds /*
10111da177e4SLinus Torvalds * handle mapping creation for uClinux
10121da177e4SLinus Torvalds */
do_mmap(struct file * file,unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,vm_flags_t vm_flags,unsigned long pgoff,unsigned long * populate,struct list_head * uf)10131fcfd8dbSOleg Nesterov unsigned long do_mmap(struct file *file,
10141da177e4SLinus Torvalds unsigned long addr,
10151da177e4SLinus Torvalds unsigned long len,
10161da177e4SLinus Torvalds unsigned long prot,
10171da177e4SLinus Torvalds unsigned long flags,
1018592b5fadSYu-cheng Yu vm_flags_t vm_flags,
1019bebeb3d6SMichel Lespinasse unsigned long pgoff,
1020897ab3e0SMike Rapoport unsigned long *populate,
1021897ab3e0SMike Rapoport struct list_head *uf)
10221da177e4SLinus Torvalds {
10238feae131SDavid Howells struct vm_area_struct *vma;
10248feae131SDavid Howells struct vm_region *region;
10251da177e4SLinus Torvalds struct rb_node *rb;
10261fcfd8dbSOleg Nesterov unsigned long capabilities, result;
10271da177e4SLinus Torvalds int ret;
102847d9644dSLiam R. Howlett VMA_ITERATOR(vmi, current->mm, 0);
10291da177e4SLinus Torvalds
103041badc15SMichel Lespinasse *populate = 0;
1031bebeb3d6SMichel Lespinasse
10321da177e4SLinus Torvalds /* decide whether we should attempt the mapping, and if so what sort of
10331da177e4SLinus Torvalds * mapping */
10341da177e4SLinus Torvalds ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
10351da177e4SLinus Torvalds &capabilities);
103622cc877bSLeon Romanovsky if (ret < 0)
10371da177e4SLinus Torvalds return ret;
10381da177e4SLinus Torvalds
103906aab5a3SDavid Howells /* we ignore the address hint */
104006aab5a3SDavid Howells addr = 0;
1041f67d9b15SBob Liu len = PAGE_ALIGN(len);
104206aab5a3SDavid Howells
10431da177e4SLinus Torvalds /* we've determined that we can make the mapping, now translate what we
10441da177e4SLinus Torvalds * now know into VMA flags */
1045592b5fadSYu-cheng Yu vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
10461da177e4SLinus Torvalds
10478220543dSMatthew Wilcox (Oracle)
10488feae131SDavid Howells /* we're going to need to record the mapping */
10498feae131SDavid Howells region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
10508feae131SDavid Howells if (!region)
10518feae131SDavid Howells goto error_getting_region;
10521da177e4SLinus Torvalds
1053490fc053SLinus Torvalds vma = vm_area_alloc(current->mm);
10548feae131SDavid Howells if (!vma)
10558feae131SDavid Howells goto error_getting_vma;
10561da177e4SLinus Torvalds
10571e2ae599SDavid Howells region->vm_usage = 1;
10588feae131SDavid Howells region->vm_flags = vm_flags;
10598feae131SDavid Howells region->vm_pgoff = pgoff;
10608feae131SDavid Howells
10611c71222eSSuren Baghdasaryan vm_flags_init(vma, vm_flags);
10628feae131SDavid Howells vma->vm_pgoff = pgoff;
10638feae131SDavid Howells
10648feae131SDavid Howells if (file) {
1065cb0942b8SAl Viro region->vm_file = get_file(file);
1066cb0942b8SAl Viro vma->vm_file = get_file(file);
10678feae131SDavid Howells }
10688feae131SDavid Howells
10698feae131SDavid Howells down_write(&nommu_region_sem);
10708feae131SDavid Howells
10718feae131SDavid Howells /* if we want to share, we need to check for regions created by other
10721da177e4SLinus Torvalds * mmap() calls that overlap with our proposed mapping
10738feae131SDavid Howells * - we can only share with a superset match on most regular files
10741da177e4SLinus Torvalds * - shared mappings on character devices and memory backed files are
10751da177e4SLinus Torvalds * permitted to overlap inexactly as far as we are concerned for in
10761da177e4SLinus Torvalds * these cases, sharing is handled in the driver or filesystem rather
10771da177e4SLinus Torvalds * than here
10781da177e4SLinus Torvalds */
1079fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vm_flags)) {
10808feae131SDavid Howells struct vm_region *pregion;
10818feae131SDavid Howells unsigned long pglen, rpglen, pgend, rpgend, start;
10821da177e4SLinus Torvalds
10838feae131SDavid Howells pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
10848feae131SDavid Howells pgend = pgoff + pglen;
1085165b2392SDavid Howells
10868feae131SDavid Howells for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
10878feae131SDavid Howells pregion = rb_entry(rb, struct vm_region, vm_rb);
10881da177e4SLinus Torvalds
1089fc4f4be9SDavid Hildenbrand if (!is_nommu_shared_mapping(pregion->vm_flags))
10901da177e4SLinus Torvalds continue;
10911da177e4SLinus Torvalds
10921da177e4SLinus Torvalds /* search for overlapping mappings on the same file */
1093496ad9aaSAl Viro if (file_inode(pregion->vm_file) !=
1094496ad9aaSAl Viro file_inode(file))
10951da177e4SLinus Torvalds continue;
10961da177e4SLinus Torvalds
10978feae131SDavid Howells if (pregion->vm_pgoff >= pgend)
10981da177e4SLinus Torvalds continue;
10991da177e4SLinus Torvalds
11008feae131SDavid Howells rpglen = pregion->vm_end - pregion->vm_start;
11018feae131SDavid Howells rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
11028feae131SDavid Howells rpgend = pregion->vm_pgoff + rpglen;
11038feae131SDavid Howells if (pgoff >= rpgend)
11041da177e4SLinus Torvalds continue;
11051da177e4SLinus Torvalds
11068feae131SDavid Howells /* handle inexactly overlapping matches between
11078feae131SDavid Howells * mappings */
11088feae131SDavid Howells if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
11098feae131SDavid Howells !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
11108feae131SDavid Howells /* new mapping is not a subset of the region */
1111b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_DIRECT))
11121da177e4SLinus Torvalds goto sharing_violation;
11131da177e4SLinus Torvalds continue;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds
11168feae131SDavid Howells /* we've found a region we can share */
11171e2ae599SDavid Howells pregion->vm_usage++;
11188feae131SDavid Howells vma->vm_region = pregion;
11198feae131SDavid Howells start = pregion->vm_start;
11208feae131SDavid Howells start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
11218feae131SDavid Howells vma->vm_start = start;
11228feae131SDavid Howells vma->vm_end = start + len;
11231da177e4SLinus Torvalds
112422cc877bSLeon Romanovsky if (pregion->vm_flags & VM_MAPPED_COPY)
11251c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_MAPPED_COPY);
112622cc877bSLeon Romanovsky else {
11278feae131SDavid Howells ret = do_mmap_shared_file(vma);
11288feae131SDavid Howells if (ret < 0) {
11298feae131SDavid Howells vma->vm_region = NULL;
11308feae131SDavid Howells vma->vm_start = 0;
11318feae131SDavid Howells vma->vm_end = 0;
11321e2ae599SDavid Howells pregion->vm_usage--;
11338feae131SDavid Howells pregion = NULL;
11348feae131SDavid Howells goto error_just_free;
11351da177e4SLinus Torvalds }
11368feae131SDavid Howells }
11378feae131SDavid Howells fput(region->vm_file);
11388feae131SDavid Howells kmem_cache_free(vm_region_jar, region);
11398feae131SDavid Howells region = pregion;
11408feae131SDavid Howells result = start;
11418feae131SDavid Howells goto share;
11428feae131SDavid Howells }
11431da177e4SLinus Torvalds
11441da177e4SLinus Torvalds /* obtain the address at which to make a shared mapping
11451da177e4SLinus Torvalds * - this is the hook for quasi-memory character devices to
11461da177e4SLinus Torvalds * tell us the location of a shared mapping
11471da177e4SLinus Torvalds */
1148b4caecd4SChristoph Hellwig if (capabilities & NOMMU_MAP_DIRECT) {
11491da177e4SLinus Torvalds addr = file->f_op->get_unmapped_area(file, addr, len,
11501da177e4SLinus Torvalds pgoff, flags);
1151bb005a59SNamhyung Kim if (IS_ERR_VALUE(addr)) {
11521da177e4SLinus Torvalds ret = addr;
1153bb005a59SNamhyung Kim if (ret != -ENOSYS)
11548feae131SDavid Howells goto error_just_free;
11551da177e4SLinus Torvalds
11561da177e4SLinus Torvalds /* the driver refused to tell us where to site
11571da177e4SLinus Torvalds * the mapping so we'll have to attempt to copy
11581da177e4SLinus Torvalds * it */
1159bb005a59SNamhyung Kim ret = -ENODEV;
1160b4caecd4SChristoph Hellwig if (!(capabilities & NOMMU_MAP_COPY))
11618feae131SDavid Howells goto error_just_free;
11621da177e4SLinus Torvalds
1163b4caecd4SChristoph Hellwig capabilities &= ~NOMMU_MAP_DIRECT;
11648feae131SDavid Howells } else {
11658feae131SDavid Howells vma->vm_start = region->vm_start = addr;
11668feae131SDavid Howells vma->vm_end = region->vm_end = addr + len;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds
11718feae131SDavid Howells vma->vm_region = region;
11721da177e4SLinus Torvalds
1173645d83c5SDavid Howells /* set up the mapping
1174b4caecd4SChristoph Hellwig * - the region is filled in if NOMMU_MAP_DIRECT is still set
1175645d83c5SDavid Howells */
11761da177e4SLinus Torvalds if (file && vma->vm_flags & VM_SHARED)
11778feae131SDavid Howells ret = do_mmap_shared_file(vma);
11781da177e4SLinus Torvalds else
1179645d83c5SDavid Howells ret = do_mmap_private(vma, region, len, capabilities);
11801da177e4SLinus Torvalds if (ret < 0)
1181645d83c5SDavid Howells goto error_just_free;
1182645d83c5SDavid Howells add_nommu_region(region);
11838feae131SDavid Howells
1184ea637639SJie Zhang /* clear anonymous mappings that don't ask for uninitialized data */
11850bf5f949SChristoph Hellwig if (!vma->vm_file &&
11860bf5f949SChristoph Hellwig (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
11870bf5f949SChristoph Hellwig !(flags & MAP_UNINITIALIZED)))
1188ea637639SJie Zhang memset((void *)region->vm_start, 0,
1189ea637639SJie Zhang region->vm_end - region->vm_start);
1190ea637639SJie Zhang
11911da177e4SLinus Torvalds /* okay... we have a mapping; now we have to register it */
11928feae131SDavid Howells result = vma->vm_start;
11931da177e4SLinus Torvalds
11941da177e4SLinus Torvalds current->mm->total_vm += len >> PAGE_SHIFT;
11951da177e4SLinus Torvalds
11968feae131SDavid Howells share:
119707f1bc5aSLiam R. Howlett BUG_ON(!vma->vm_region);
1198b5df0922SLiam R. Howlett vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
1199b5df0922SLiam R. Howlett if (vma_iter_prealloc(&vmi, vma))
1200b5df0922SLiam R. Howlett goto error_just_free;
1201b5df0922SLiam R. Howlett
120207f1bc5aSLiam R. Howlett setup_vma_to_mm(vma, current->mm);
120307f1bc5aSLiam R. Howlett current->mm->map_count++;
120407f1bc5aSLiam R. Howlett /* add the VMA to the tree */
120507f1bc5aSLiam R. Howlett vma_iter_store(&vmi, vma);
12061da177e4SLinus Torvalds
1207cfe79c00SMike Frysinger /* we flush the region from the icache only when the first executable
1208cfe79c00SMike Frysinger * mapping of it is made */
1209cfe79c00SMike Frysinger if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1210a75a2df6SChristoph Hellwig flush_icache_user_range(region->vm_start, region->vm_end);
1211cfe79c00SMike Frysinger region->vm_icache_flushed = true;
1212cfe79c00SMike Frysinger }
12131da177e4SLinus Torvalds
1214cfe79c00SMike Frysinger up_write(&nommu_region_sem);
12151da177e4SLinus Torvalds
12168feae131SDavid Howells return result;
12171da177e4SLinus Torvalds
12188feae131SDavid Howells error_just_free:
12198feae131SDavid Howells up_write(&nommu_region_sem);
12208feae131SDavid Howells error:
122147d9644dSLiam R. Howlett vma_iter_free(&vmi);
122289a86402SDavid Howells if (region->vm_file)
12238feae131SDavid Howells fput(region->vm_file);
12248feae131SDavid Howells kmem_cache_free(vm_region_jar, region);
122589a86402SDavid Howells if (vma->vm_file)
12268feae131SDavid Howells fput(vma->vm_file);
12273928d4f5SLinus Torvalds vm_area_free(vma);
12281da177e4SLinus Torvalds return ret;
12291da177e4SLinus Torvalds
12301da177e4SLinus Torvalds sharing_violation:
12318feae131SDavid Howells up_write(&nommu_region_sem);
123222cc877bSLeon Romanovsky pr_warn("Attempt to share mismatched mappings\n");
12338feae131SDavid Howells ret = -EINVAL;
12348feae131SDavid Howells goto error;
12351da177e4SLinus Torvalds
12361da177e4SLinus Torvalds error_getting_vma:
12378feae131SDavid Howells kmem_cache_free(vm_region_jar, region);
123822cc877bSLeon Romanovsky pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
12391da177e4SLinus Torvalds len, current->pid);
12401279aa06SKefeng Wang show_mem();
12411da177e4SLinus Torvalds return -ENOMEM;
12421da177e4SLinus Torvalds
12438feae131SDavid Howells error_getting_region:
124422cc877bSLeon Romanovsky pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
12451da177e4SLinus Torvalds len, current->pid);
12461279aa06SKefeng Wang show_mem();
12471da177e4SLinus Torvalds return -ENOMEM;
12481da177e4SLinus Torvalds }
12496be5ceb0SLinus Torvalds
ksys_mmap_pgoff(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long pgoff)1250a90f590aSDominik Brodowski unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1251a90f590aSDominik Brodowski unsigned long prot, unsigned long flags,
1252a90f590aSDominik Brodowski unsigned long fd, unsigned long pgoff)
125366f0dc48SHugh Dickins {
125466f0dc48SHugh Dickins struct file *file = NULL;
125566f0dc48SHugh Dickins unsigned long retval = -EBADF;
125666f0dc48SHugh Dickins
1257120a795dSAl Viro audit_mmap_fd(fd, flags);
125866f0dc48SHugh Dickins if (!(flags & MAP_ANONYMOUS)) {
125966f0dc48SHugh Dickins file = fget(fd);
126066f0dc48SHugh Dickins if (!file)
126166f0dc48SHugh Dickins goto out;
126266f0dc48SHugh Dickins }
126366f0dc48SHugh Dickins
1264ad1ed293SGreg Ungerer retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
126566f0dc48SHugh Dickins
126666f0dc48SHugh Dickins if (file)
126766f0dc48SHugh Dickins fput(file);
126866f0dc48SHugh Dickins out:
126966f0dc48SHugh Dickins return retval;
127066f0dc48SHugh Dickins }
127166f0dc48SHugh Dickins
SYSCALL_DEFINE6(mmap_pgoff,unsigned long,addr,unsigned long,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,unsigned long,pgoff)1272a90f590aSDominik Brodowski SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1273a90f590aSDominik Brodowski unsigned long, prot, unsigned long, flags,
1274a90f590aSDominik Brodowski unsigned long, fd, unsigned long, pgoff)
1275a90f590aSDominik Brodowski {
1276a90f590aSDominik Brodowski return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1277a90f590aSDominik Brodowski }
1278a90f590aSDominik Brodowski
1279a4679373SChristoph Hellwig #ifdef __ARCH_WANT_SYS_OLD_MMAP
1280a4679373SChristoph Hellwig struct mmap_arg_struct {
1281a4679373SChristoph Hellwig unsigned long addr;
1282a4679373SChristoph Hellwig unsigned long len;
1283a4679373SChristoph Hellwig unsigned long prot;
1284a4679373SChristoph Hellwig unsigned long flags;
1285a4679373SChristoph Hellwig unsigned long fd;
1286a4679373SChristoph Hellwig unsigned long offset;
1287a4679373SChristoph Hellwig };
1288a4679373SChristoph Hellwig
SYSCALL_DEFINE1(old_mmap,struct mmap_arg_struct __user *,arg)1289a4679373SChristoph Hellwig SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1290a4679373SChristoph Hellwig {
1291a4679373SChristoph Hellwig struct mmap_arg_struct a;
1292a4679373SChristoph Hellwig
1293a4679373SChristoph Hellwig if (copy_from_user(&a, arg, sizeof(a)))
1294a4679373SChristoph Hellwig return -EFAULT;
12951824cb75SAlexander Kuleshov if (offset_in_page(a.offset))
1296a4679373SChristoph Hellwig return -EINVAL;
1297a4679373SChristoph Hellwig
1298a90f590aSDominik Brodowski return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1299a4679373SChristoph Hellwig a.offset >> PAGE_SHIFT);
1300a4679373SChristoph Hellwig }
1301a4679373SChristoph Hellwig #endif /* __ARCH_WANT_SYS_OLD_MMAP */
1302a4679373SChristoph Hellwig
13031da177e4SLinus Torvalds /*
13048feae131SDavid Howells * split a vma into two pieces at address 'addr', a new vma is allocated either
13058feae131SDavid Howells * for the first part or the tail.
13061da177e4SLinus Torvalds */
split_vma(struct vma_iterator * vmi,struct vm_area_struct * vma,unsigned long addr,int new_below)13079760ebffSLiam R. Howlett int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
13089760ebffSLiam R. Howlett unsigned long addr, int new_below)
13091da177e4SLinus Torvalds {
13108feae131SDavid Howells struct vm_area_struct *new;
13118feae131SDavid Howells struct vm_region *region;
13128feae131SDavid Howells unsigned long npages;
13139760ebffSLiam R. Howlett struct mm_struct *mm;
13141da177e4SLinus Torvalds
1315779c1023SDavid Howells /* we're only permitted to split anonymous regions (these should have
1316779c1023SDavid Howells * only a single usage on the region) */
1317779c1023SDavid Howells if (vma->vm_file)
13188feae131SDavid Howells return -ENOMEM;
13191da177e4SLinus Torvalds
13209760ebffSLiam R. Howlett mm = vma->vm_mm;
13218feae131SDavid Howells if (mm->map_count >= sysctl_max_map_count)
13228feae131SDavid Howells return -ENOMEM;
13231da177e4SLinus Torvalds
13248feae131SDavid Howells region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
13258feae131SDavid Howells if (!region)
13268feae131SDavid Howells return -ENOMEM;
13278feae131SDavid Howells
13283928d4f5SLinus Torvalds new = vm_area_dup(vma);
13298220543dSMatthew Wilcox (Oracle) if (!new)
13308220543dSMatthew Wilcox (Oracle) goto err_vma_dup;
13318220543dSMatthew Wilcox (Oracle)
13328feae131SDavid Howells /* most fields are the same, copy all, and then fixup */
13338feae131SDavid Howells *region = *vma->vm_region;
13348feae131SDavid Howells new->vm_region = region;
13358feae131SDavid Howells
13368feae131SDavid Howells npages = (addr - vma->vm_start) >> PAGE_SHIFT;
13378feae131SDavid Howells
13388feae131SDavid Howells if (new_below) {
1339dd8632a1SPaul Mundt region->vm_top = region->vm_end = new->vm_end = addr;
13408feae131SDavid Howells } else {
13418feae131SDavid Howells region->vm_start = new->vm_start = addr;
13428feae131SDavid Howells region->vm_pgoff = new->vm_pgoff += npages;
13431da177e4SLinus Torvalds }
13448feae131SDavid Howells
1345b5df0922SLiam R. Howlett vma_iter_config(vmi, new->vm_start, new->vm_end);
1346b5df0922SLiam R. Howlett if (vma_iter_prealloc(vmi, vma)) {
1347b5df0922SLiam R. Howlett pr_warn("Allocation of vma tree for process %d failed\n",
1348b5df0922SLiam R. Howlett current->pid);
1349b5df0922SLiam R. Howlett goto err_vmi_preallocate;
1350b5df0922SLiam R. Howlett }
1351b5df0922SLiam R. Howlett
13528feae131SDavid Howells if (new->vm_ops && new->vm_ops->open)
13538feae131SDavid Howells new->vm_ops->open(new);
13548feae131SDavid Howells
13558feae131SDavid Howells down_write(&nommu_region_sem);
13568feae131SDavid Howells delete_nommu_region(vma->vm_region);
13578feae131SDavid Howells if (new_below) {
13588feae131SDavid Howells vma->vm_region->vm_start = vma->vm_start = addr;
13598feae131SDavid Howells vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
13608feae131SDavid Howells } else {
13618feae131SDavid Howells vma->vm_region->vm_end = vma->vm_end = addr;
1362dd8632a1SPaul Mundt vma->vm_region->vm_top = addr;
13638feae131SDavid Howells }
13648feae131SDavid Howells add_nommu_region(vma->vm_region);
13658feae131SDavid Howells add_nommu_region(new->vm_region);
13668feae131SDavid Howells up_write(&nommu_region_sem);
13678220543dSMatthew Wilcox (Oracle)
13688220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(vma, mm);
13698220543dSMatthew Wilcox (Oracle) setup_vma_to_mm(new, mm);
137047d9644dSLiam R. Howlett vma_iter_store(vmi, new);
1371fd9edbdbSLiam Howlett mm->map_count++;
13728feae131SDavid Howells return 0;
13738220543dSMatthew Wilcox (Oracle)
137447d9644dSLiam R. Howlett err_vmi_preallocate:
13758220543dSMatthew Wilcox (Oracle) vm_area_free(new);
13768220543dSMatthew Wilcox (Oracle) err_vma_dup:
13778220543dSMatthew Wilcox (Oracle) kmem_cache_free(vm_region_jar, region);
13788220543dSMatthew Wilcox (Oracle) return -ENOMEM;
13798feae131SDavid Howells }
13808feae131SDavid Howells
13818feae131SDavid Howells /*
13828feae131SDavid Howells * shrink a VMA by removing the specified chunk from either the beginning or
13838feae131SDavid Howells * the end
13848feae131SDavid Howells */
vmi_shrink_vma(struct vma_iterator * vmi,struct vm_area_struct * vma,unsigned long from,unsigned long to)138507f1bc5aSLiam R. Howlett static int vmi_shrink_vma(struct vma_iterator *vmi,
13868feae131SDavid Howells struct vm_area_struct *vma,
13878feae131SDavid Howells unsigned long from, unsigned long to)
13888feae131SDavid Howells {
13898feae131SDavid Howells struct vm_region *region;
13908feae131SDavid Howells
13918feae131SDavid Howells /* adjust the VMA's pointers, which may reposition it in the MM's tree
13928feae131SDavid Howells * and list */
139307f1bc5aSLiam R. Howlett if (from > vma->vm_start) {
1394f72cf24aSLiam R. Howlett if (vma_iter_clear_gfp(vmi, from, vma->vm_end, GFP_KERNEL))
1395f72cf24aSLiam R. Howlett return -ENOMEM;
13968feae131SDavid Howells vma->vm_end = from;
139707f1bc5aSLiam R. Howlett } else {
1398f72cf24aSLiam R. Howlett if (vma_iter_clear_gfp(vmi, vma->vm_start, to, GFP_KERNEL))
1399f72cf24aSLiam R. Howlett return -ENOMEM;
14008feae131SDavid Howells vma->vm_start = to;
140107f1bc5aSLiam R. Howlett }
14028feae131SDavid Howells
14038feae131SDavid Howells /* cut the backing region down to size */
14048feae131SDavid Howells region = vma->vm_region;
14051e2ae599SDavid Howells BUG_ON(region->vm_usage != 1);
14068feae131SDavid Howells
14078feae131SDavid Howells down_write(&nommu_region_sem);
14088feae131SDavid Howells delete_nommu_region(region);
1409dd8632a1SPaul Mundt if (from > region->vm_start) {
1410dd8632a1SPaul Mundt to = region->vm_top;
1411dd8632a1SPaul Mundt region->vm_top = region->vm_end = from;
1412dd8632a1SPaul Mundt } else {
14138feae131SDavid Howells region->vm_start = to;
1414dd8632a1SPaul Mundt }
14158feae131SDavid Howells add_nommu_region(region);
14168feae131SDavid Howells up_write(&nommu_region_sem);
14178feae131SDavid Howells
14188feae131SDavid Howells free_page_series(from, to);
14198feae131SDavid Howells return 0;
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds
14223034097aSDavid Howells /*
14233034097aSDavid Howells * release a mapping
14248feae131SDavid Howells * - under NOMMU conditions the chunk to be unmapped must be backed by a single
14258feae131SDavid Howells * VMA, though it need not cover the whole VMA
14263034097aSDavid Howells */
do_munmap(struct mm_struct * mm,unsigned long start,size_t len,struct list_head * uf)1427897ab3e0SMike Rapoport int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
14281da177e4SLinus Torvalds {
142947d9644dSLiam R. Howlett VMA_ITERATOR(vmi, mm, start);
14308feae131SDavid Howells struct vm_area_struct *vma;
1431f67d9b15SBob Liu unsigned long end;
14328220543dSMatthew Wilcox (Oracle) int ret = 0;
14331da177e4SLinus Torvalds
1434f67d9b15SBob Liu len = PAGE_ALIGN(len);
14358feae131SDavid Howells if (len == 0)
14361da177e4SLinus Torvalds return -EINVAL;
14371da177e4SLinus Torvalds
1438f67d9b15SBob Liu end = start + len;
1439f67d9b15SBob Liu
14408feae131SDavid Howells /* find the first potentially overlapping VMA */
144147d9644dSLiam R. Howlett vma = vma_find(&vmi, end);
14428feae131SDavid Howells if (!vma) {
1443ac714904SChoi Gi-yong static int limit;
144433e5d769SDavid Howells if (limit < 5) {
144522cc877bSLeon Romanovsky pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
144633e5d769SDavid Howells current->pid, current->comm,
144733e5d769SDavid Howells start, start + len - 1);
144833e5d769SDavid Howells limit++;
144933e5d769SDavid Howells }
14508feae131SDavid Howells return -EINVAL;
14518feae131SDavid Howells }
14521da177e4SLinus Torvalds
14538feae131SDavid Howells /* we're allowed to split an anonymous VMA but not a file-backed one */
14548feae131SDavid Howells if (vma->vm_file) {
14558feae131SDavid Howells do {
145622cc877bSLeon Romanovsky if (start > vma->vm_start)
14578feae131SDavid Howells return -EINVAL;
14588feae131SDavid Howells if (end == vma->vm_end)
14598feae131SDavid Howells goto erase_whole_vma;
146047d9644dSLiam R. Howlett vma = vma_find(&vmi, end);
1461d75a310cSNamhyung Kim } while (vma);
14628feae131SDavid Howells return -EINVAL;
14638feae131SDavid Howells } else {
14648feae131SDavid Howells /* the chunk must be a subset of the VMA found */
14658feae131SDavid Howells if (start == vma->vm_start && end == vma->vm_end)
14668feae131SDavid Howells goto erase_whole_vma;
146722cc877bSLeon Romanovsky if (start < vma->vm_start || end > vma->vm_end)
14688feae131SDavid Howells return -EINVAL;
14691824cb75SAlexander Kuleshov if (offset_in_page(start))
14708feae131SDavid Howells return -EINVAL;
14711824cb75SAlexander Kuleshov if (end != vma->vm_end && offset_in_page(end))
14728feae131SDavid Howells return -EINVAL;
14738feae131SDavid Howells if (start != vma->vm_start && end != vma->vm_end) {
14749760ebffSLiam R. Howlett ret = split_vma(&vmi, vma, start, 1);
147522cc877bSLeon Romanovsky if (ret < 0)
14768feae131SDavid Howells return ret;
14778feae131SDavid Howells }
147807f1bc5aSLiam R. Howlett return vmi_shrink_vma(&vmi, vma, start, end);
14798feae131SDavid Howells }
14801da177e4SLinus Torvalds
14818feae131SDavid Howells erase_whole_vma:
14828220543dSMatthew Wilcox (Oracle) if (delete_vma_from_mm(vma))
14838220543dSMatthew Wilcox (Oracle) ret = -ENOMEM;
148480be727eSLiam Howlett else
14858feae131SDavid Howells delete_vma(mm, vma);
14868220543dSMatthew Wilcox (Oracle) return ret;
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds
vm_munmap(unsigned long addr,size_t len)1489bfce281cSAl Viro int vm_munmap(unsigned long addr, size_t len)
14903034097aSDavid Howells {
1491bfce281cSAl Viro struct mm_struct *mm = current->mm;
14923034097aSDavid Howells int ret;
14933034097aSDavid Howells
1494d8ed45c5SMichel Lespinasse mmap_write_lock(mm);
1495897ab3e0SMike Rapoport ret = do_munmap(mm, addr, len, NULL);
1496d8ed45c5SMichel Lespinasse mmap_write_unlock(mm);
14973034097aSDavid Howells return ret;
14983034097aSDavid Howells }
1499a46ef99dSLinus Torvalds EXPORT_SYMBOL(vm_munmap);
1500a46ef99dSLinus Torvalds
SYSCALL_DEFINE2(munmap,unsigned long,addr,size_t,len)1501a46ef99dSLinus Torvalds SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1502a46ef99dSLinus Torvalds {
1503bfce281cSAl Viro return vm_munmap(addr, len);
1504a46ef99dSLinus Torvalds }
15053034097aSDavid Howells
15063034097aSDavid Howells /*
15078feae131SDavid Howells * release all the mappings made in a process's VM space
15083034097aSDavid Howells */
exit_mmap(struct mm_struct * mm)15091da177e4SLinus Torvalds void exit_mmap(struct mm_struct *mm)
15101da177e4SLinus Torvalds {
15118220543dSMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, 0);
15128feae131SDavid Howells struct vm_area_struct *vma;
15131da177e4SLinus Torvalds
15148feae131SDavid Howells if (!mm)
15158feae131SDavid Howells return;
15168feae131SDavid Howells
15171da177e4SLinus Torvalds mm->total_vm = 0;
15181da177e4SLinus Torvalds
15198220543dSMatthew Wilcox (Oracle) /*
15208220543dSMatthew Wilcox (Oracle) * Lock the mm to avoid assert complaining even though this is the only
15218220543dSMatthew Wilcox (Oracle) * user of the mm
15228220543dSMatthew Wilcox (Oracle) */
15238220543dSMatthew Wilcox (Oracle) mmap_write_lock(mm);
15248220543dSMatthew Wilcox (Oracle) for_each_vma(vmi, vma) {
15258220543dSMatthew Wilcox (Oracle) cleanup_vma_from_mm(vma);
15268feae131SDavid Howells delete_vma(mm, vma);
152704c34961SSteven J. Magnani cond_resched();
15281da177e4SLinus Torvalds }
1529524e00b3SLiam R. Howlett __mt_destroy(&mm->mm_mt);
15308220543dSMatthew Wilcox (Oracle) mmap_write_unlock(mm);
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds
vm_brk(unsigned long addr,unsigned long len)15335d22fc25SLinus Torvalds int vm_brk(unsigned long addr, unsigned long len)
15341da177e4SLinus Torvalds {
15351da177e4SLinus Torvalds return -ENOMEM;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds
15381da177e4SLinus Torvalds /*
15396fa5f80bSDavid Howells * expand (or shrink) an existing mapping, potentially moving it at the same
15406fa5f80bSDavid Howells * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
15411da177e4SLinus Torvalds *
15426fa5f80bSDavid Howells * under NOMMU conditions, we only permit changing a mapping's size, and only
15438feae131SDavid Howells * as long as it stays within the region allocated by do_mmap_private() and the
15448feae131SDavid Howells * block is not shareable
15451da177e4SLinus Torvalds *
15466fa5f80bSDavid Howells * MREMAP_FIXED is not supported under NOMMU conditions
15471da177e4SLinus Torvalds */
do_mremap(unsigned long addr,unsigned long old_len,unsigned long new_len,unsigned long flags,unsigned long new_addr)15484b377babSAl Viro static unsigned long do_mremap(unsigned long addr,
15491da177e4SLinus Torvalds unsigned long old_len, unsigned long new_len,
15501da177e4SLinus Torvalds unsigned long flags, unsigned long new_addr)
15511da177e4SLinus Torvalds {
15526fa5f80bSDavid Howells struct vm_area_struct *vma;
15531da177e4SLinus Torvalds
15541da177e4SLinus Torvalds /* insanity checks first */
1555f67d9b15SBob Liu old_len = PAGE_ALIGN(old_len);
1556f67d9b15SBob Liu new_len = PAGE_ALIGN(new_len);
15578feae131SDavid Howells if (old_len == 0 || new_len == 0)
15581da177e4SLinus Torvalds return (unsigned long) -EINVAL;
15591da177e4SLinus Torvalds
15601824cb75SAlexander Kuleshov if (offset_in_page(addr))
15618feae131SDavid Howells return -EINVAL;
15628feae131SDavid Howells
15631da177e4SLinus Torvalds if (flags & MREMAP_FIXED && new_addr != addr)
15641da177e4SLinus Torvalds return (unsigned long) -EINVAL;
15651da177e4SLinus Torvalds
15668feae131SDavid Howells vma = find_vma_exact(current->mm, addr, old_len);
15676fa5f80bSDavid Howells if (!vma)
15681da177e4SLinus Torvalds return (unsigned long) -EINVAL;
15691da177e4SLinus Torvalds
15706fa5f80bSDavid Howells if (vma->vm_end != vma->vm_start + old_len)
15711da177e4SLinus Torvalds return (unsigned long) -EFAULT;
15721da177e4SLinus Torvalds
1573fc4f4be9SDavid Hildenbrand if (is_nommu_shared_mapping(vma->vm_flags))
15741da177e4SLinus Torvalds return (unsigned long) -EPERM;
15751da177e4SLinus Torvalds
15768feae131SDavid Howells if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
15771da177e4SLinus Torvalds return (unsigned long) -ENOMEM;
15781da177e4SLinus Torvalds
15791da177e4SLinus Torvalds /* all checks complete - do it */
15806fa5f80bSDavid Howells vma->vm_end = vma->vm_start + new_len;
15816fa5f80bSDavid Howells return vma->vm_start;
15826fa5f80bSDavid Howells }
15836fa5f80bSDavid Howells
SYSCALL_DEFINE5(mremap,unsigned long,addr,unsigned long,old_len,unsigned long,new_len,unsigned long,flags,unsigned long,new_addr)15846a6160a7SHeiko Carstens SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
15856a6160a7SHeiko Carstens unsigned long, new_len, unsigned long, flags,
15866a6160a7SHeiko Carstens unsigned long, new_addr)
15876fa5f80bSDavid Howells {
15886fa5f80bSDavid Howells unsigned long ret;
15896fa5f80bSDavid Howells
1590d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm);
15916fa5f80bSDavid Howells ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1592d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
15936fa5f80bSDavid Howells return ret;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds
follow_page(struct vm_area_struct * vma,unsigned long address,unsigned int foll_flags)1596df06b37fSKeith Busch struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1597df06b37fSKeith Busch unsigned int foll_flags)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds return NULL;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds
remap_pfn_range(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot)16028f3b1327SBob Liu int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
16038f3b1327SBob Liu unsigned long pfn, unsigned long size, pgprot_t prot)
16041da177e4SLinus Torvalds {
16058f3b1327SBob Liu if (addr != (pfn << PAGE_SHIFT))
16068f3b1327SBob Liu return -EINVAL;
16078f3b1327SBob Liu
16081c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
160966aa2b4bSGreg Ungerer return 0;
16101da177e4SLinus Torvalds }
161122c4af40SLuke Yang EXPORT_SYMBOL(remap_pfn_range);
16121da177e4SLinus Torvalds
vm_iomap_memory(struct vm_area_struct * vma,phys_addr_t start,unsigned long len)16133c0b9de6SLinus Torvalds int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
16143c0b9de6SLinus Torvalds {
16153c0b9de6SLinus Torvalds unsigned long pfn = start >> PAGE_SHIFT;
16163c0b9de6SLinus Torvalds unsigned long vm_len = vma->vm_end - vma->vm_start;
16173c0b9de6SLinus Torvalds
16183c0b9de6SLinus Torvalds pfn += vma->vm_pgoff;
16193c0b9de6SLinus Torvalds return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
16203c0b9de6SLinus Torvalds }
16213c0b9de6SLinus Torvalds EXPORT_SYMBOL(vm_iomap_memory);
16223c0b9de6SLinus Torvalds
remap_vmalloc_range(struct vm_area_struct * vma,void * addr,unsigned long pgoff)1623f905bc44SPaul Mundt int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1624f905bc44SPaul Mundt unsigned long pgoff)
1625f905bc44SPaul Mundt {
1626f905bc44SPaul Mundt unsigned int size = vma->vm_end - vma->vm_start;
1627f905bc44SPaul Mundt
1628f905bc44SPaul Mundt if (!(vma->vm_flags & VM_USERMAP))
1629f905bc44SPaul Mundt return -EINVAL;
1630f905bc44SPaul Mundt
1631f905bc44SPaul Mundt vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1632f905bc44SPaul Mundt vma->vm_end = vma->vm_start + size;
1633f905bc44SPaul Mundt
1634f905bc44SPaul Mundt return 0;
1635f905bc44SPaul Mundt }
1636f905bc44SPaul Mundt EXPORT_SYMBOL(remap_vmalloc_range);
1637f905bc44SPaul Mundt
filemap_fault(struct vm_fault * vmf)16382bcd6454SSouptick Joarder vm_fault_t filemap_fault(struct vm_fault *vmf)
1639b0e15190SDavid Howells {
1640b0e15190SDavid Howells BUG();
1641d0217ac0SNick Piggin return 0;
1642b0e15190SDavid Howells }
1643b5073173SPaul Mundt EXPORT_SYMBOL(filemap_fault);
16440ec76a11SDavid Howells
filemap_map_pages(struct vm_fault * vmf,pgoff_t start_pgoff,pgoff_t end_pgoff)16453f98a28cSGeert Uytterhoeven vm_fault_t filemap_map_pages(struct vm_fault *vmf,
1646bae473a4SKirill A. Shutemov pgoff_t start_pgoff, pgoff_t end_pgoff)
1647f1820361SKirill A. Shutemov {
1648f1820361SKirill A. Shutemov BUG();
16493f98a28cSGeert Uytterhoeven return 0;
1650f1820361SKirill A. Shutemov }
1651f1820361SKirill A. Shutemov EXPORT_SYMBOL(filemap_map_pages);
1652f1820361SKirill A. Shutemov
__access_remote_vm(struct mm_struct * mm,unsigned long addr,void * buf,int len,unsigned int gup_flags)1653d3f5ffcaSJohn Hubbard int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
1654d3f5ffcaSJohn Hubbard int len, unsigned int gup_flags)
16550ec76a11SDavid Howells {
16560ec76a11SDavid Howells struct vm_area_struct *vma;
1657442486ecSLorenzo Stoakes int write = gup_flags & FOLL_WRITE;
16580ec76a11SDavid Howells
1659d8ed45c5SMichel Lespinasse if (mmap_read_lock_killable(mm))
16601e426fe2SKonstantin Khlebnikov return 0;
16610ec76a11SDavid Howells
16620ec76a11SDavid Howells /* the access must start within one of the target process's mappings */
16630159b141SDavid Howells vma = find_vma(mm, addr);
16640159b141SDavid Howells if (vma) {
16650ec76a11SDavid Howells /* don't overrun this mapping */
16660ec76a11SDavid Howells if (addr + len >= vma->vm_end)
16670ec76a11SDavid Howells len = vma->vm_end - addr;
16680ec76a11SDavid Howells
16690ec76a11SDavid Howells /* only read or write mappings where it is permitted */
1670d00c7b99SDavid Howells if (write && vma->vm_flags & VM_MAYWRITE)
16717959722bSJie Zhang copy_to_user_page(vma, NULL, addr,
16727959722bSJie Zhang (void *) addr, buf, len);
1673d00c7b99SDavid Howells else if (!write && vma->vm_flags & VM_MAYREAD)
16747959722bSJie Zhang copy_from_user_page(vma, NULL, addr,
16757959722bSJie Zhang buf, (void *) addr, len);
16760ec76a11SDavid Howells else
16770ec76a11SDavid Howells len = 0;
16780ec76a11SDavid Howells } else {
16790ec76a11SDavid Howells len = 0;
16800ec76a11SDavid Howells }
16810ec76a11SDavid Howells
1682d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
1683f55f199bSMike Frysinger
1684f55f199bSMike Frysinger return len;
1685f55f199bSMike Frysinger }
1686f55f199bSMike Frysinger
1687f55f199bSMike Frysinger /**
1688b7701a5fSMike Rapoport * access_remote_vm - access another process' address space
1689f55f199bSMike Frysinger * @mm: the mm_struct of the target address space
1690f55f199bSMike Frysinger * @addr: start address to access
1691f55f199bSMike Frysinger * @buf: source or destination buffer
1692f55f199bSMike Frysinger * @len: number of bytes to transfer
16936347e8d5SLorenzo Stoakes * @gup_flags: flags modifying lookup behaviour
1694f55f199bSMike Frysinger *
1695f55f199bSMike Frysinger * The caller must hold a reference on @mm.
1696f55f199bSMike Frysinger */
access_remote_vm(struct mm_struct * mm,unsigned long addr,void * buf,int len,unsigned int gup_flags)1697f55f199bSMike Frysinger int access_remote_vm(struct mm_struct *mm, unsigned long addr,
16986347e8d5SLorenzo Stoakes void *buf, int len, unsigned int gup_flags)
1699f55f199bSMike Frysinger {
1700d3f5ffcaSJohn Hubbard return __access_remote_vm(mm, addr, buf, len, gup_flags);
1701f55f199bSMike Frysinger }
1702f55f199bSMike Frysinger
1703f55f199bSMike Frysinger /*
1704f55f199bSMike Frysinger * Access another process' address space.
1705f55f199bSMike Frysinger * - source/target buffer must be kernel space
1706f55f199bSMike Frysinger */
access_process_vm(struct task_struct * tsk,unsigned long addr,void * buf,int len,unsigned int gup_flags)1707f307ab6dSLorenzo Stoakes int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1708f307ab6dSLorenzo Stoakes unsigned int gup_flags)
1709f55f199bSMike Frysinger {
1710f55f199bSMike Frysinger struct mm_struct *mm;
1711f55f199bSMike Frysinger
1712f55f199bSMike Frysinger if (addr + len < addr)
1713f55f199bSMike Frysinger return 0;
1714f55f199bSMike Frysinger
1715f55f199bSMike Frysinger mm = get_task_mm(tsk);
1716f55f199bSMike Frysinger if (!mm)
1717f55f199bSMike Frysinger return 0;
1718f55f199bSMike Frysinger
1719d3f5ffcaSJohn Hubbard len = __access_remote_vm(mm, addr, buf, len, gup_flags);
1720f55f199bSMike Frysinger
17210ec76a11SDavid Howells mmput(mm);
17220ec76a11SDavid Howells return len;
17230ec76a11SDavid Howells }
1724fcd35857SCatalin Marinas EXPORT_SYMBOL_GPL(access_process_vm);
17257e660872SDavid Howells
17267e660872SDavid Howells /**
17277e660872SDavid Howells * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
17287e660872SDavid Howells * @inode: The inode to check
17297e660872SDavid Howells * @size: The current filesize of the inode
17307e660872SDavid Howells * @newsize: The proposed filesize of the inode
17317e660872SDavid Howells *
17327e660872SDavid Howells * Check the shared mappings on an inode on behalf of a shrinking truncate to
1733c08b342cSRandy Dunlap * make sure that any outstanding VMAs aren't broken and then shrink the
1734c08b342cSRandy Dunlap * vm_regions that extend beyond so that do_mmap() doesn't
17357e660872SDavid Howells * automatically grant mappings that are too large.
17367e660872SDavid Howells */
nommu_shrink_inode_mappings(struct inode * inode,size_t size,size_t newsize)17377e660872SDavid Howells int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
17387e660872SDavid Howells size_t newsize)
17397e660872SDavid Howells {
17407e660872SDavid Howells struct vm_area_struct *vma;
17417e660872SDavid Howells struct vm_region *region;
17427e660872SDavid Howells pgoff_t low, high;
17437e660872SDavid Howells size_t r_size, r_top;
17447e660872SDavid Howells
17457e660872SDavid Howells low = newsize >> PAGE_SHIFT;
17467e660872SDavid Howells high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
17477e660872SDavid Howells
17487e660872SDavid Howells down_write(&nommu_region_sem);
17491acf2e04SDavidlohr Bueso i_mmap_lock_read(inode->i_mapping);
17507e660872SDavid Howells
17517e660872SDavid Howells /* search for VMAs that fall within the dead zone */
17526b2dbba8SMichel Lespinasse vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
17537e660872SDavid Howells /* found one - only interested if it's shared out of the page
17547e660872SDavid Howells * cache */
17557e660872SDavid Howells if (vma->vm_flags & VM_SHARED) {
17561acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping);
17577e660872SDavid Howells up_write(&nommu_region_sem);
17587e660872SDavid Howells return -ETXTBSY; /* not quite true, but near enough */
17597e660872SDavid Howells }
17607e660872SDavid Howells }
17617e660872SDavid Howells
17627e660872SDavid Howells /* reduce any regions that overlap the dead zone - if in existence,
17637e660872SDavid Howells * these will be pointed to by VMAs that don't overlap the dead zone
17647e660872SDavid Howells *
17657e660872SDavid Howells * we don't check for any regions that start beyond the EOF as there
17667e660872SDavid Howells * shouldn't be any
17677e660872SDavid Howells */
17681acf2e04SDavidlohr Bueso vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
17697e660872SDavid Howells if (!(vma->vm_flags & VM_SHARED))
17707e660872SDavid Howells continue;
17717e660872SDavid Howells
17727e660872SDavid Howells region = vma->vm_region;
17737e660872SDavid Howells r_size = region->vm_top - region->vm_start;
17747e660872SDavid Howells r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
17757e660872SDavid Howells
17767e660872SDavid Howells if (r_top > newsize) {
17777e660872SDavid Howells region->vm_top -= r_top - newsize;
17787e660872SDavid Howells if (region->vm_end > region->vm_top)
17797e660872SDavid Howells region->vm_end = region->vm_top;
17807e660872SDavid Howells }
17817e660872SDavid Howells }
17827e660872SDavid Howells
17831acf2e04SDavidlohr Bueso i_mmap_unlock_read(inode->i_mapping);
17847e660872SDavid Howells up_write(&nommu_region_sem);
17857e660872SDavid Howells return 0;
17867e660872SDavid Howells }
1787c9b1d098SAndrew Shewmaker
1788c9b1d098SAndrew Shewmaker /*
1789c9b1d098SAndrew Shewmaker * Initialise sysctl_user_reserve_kbytes.
1790c9b1d098SAndrew Shewmaker *
1791c9b1d098SAndrew Shewmaker * This is intended to prevent a user from starting a single memory hogging
1792c9b1d098SAndrew Shewmaker * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1793c9b1d098SAndrew Shewmaker * mode.
1794c9b1d098SAndrew Shewmaker *
1795c9b1d098SAndrew Shewmaker * The default value is min(3% of free memory, 128MB)
1796c9b1d098SAndrew Shewmaker * 128MB is enough to recover with sshd/login, bash, and top/kill.
1797c9b1d098SAndrew Shewmaker */
init_user_reserve(void)1798c9b1d098SAndrew Shewmaker static int __meminit init_user_reserve(void)
1799c9b1d098SAndrew Shewmaker {
1800c9b1d098SAndrew Shewmaker unsigned long free_kbytes;
1801c9b1d098SAndrew Shewmaker
1802d5a6474dSZhangPeng free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
1803c9b1d098SAndrew Shewmaker
1804c9b1d098SAndrew Shewmaker sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1805c9b1d098SAndrew Shewmaker return 0;
1806c9b1d098SAndrew Shewmaker }
1807a4bc6fc7SPaul Gortmaker subsys_initcall(init_user_reserve);
18084eeab4f5SAndrew Shewmaker
18094eeab4f5SAndrew Shewmaker /*
18104eeab4f5SAndrew Shewmaker * Initialise sysctl_admin_reserve_kbytes.
18114eeab4f5SAndrew Shewmaker *
18124eeab4f5SAndrew Shewmaker * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
18134eeab4f5SAndrew Shewmaker * to log in and kill a memory hogging process.
18144eeab4f5SAndrew Shewmaker *
18154eeab4f5SAndrew Shewmaker * Systems with more than 256MB will reserve 8MB, enough to recover
18164eeab4f5SAndrew Shewmaker * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
18174eeab4f5SAndrew Shewmaker * only reserve 3% of free pages by default.
18184eeab4f5SAndrew Shewmaker */
init_admin_reserve(void)18194eeab4f5SAndrew Shewmaker static int __meminit init_admin_reserve(void)
18204eeab4f5SAndrew Shewmaker {
18214eeab4f5SAndrew Shewmaker unsigned long free_kbytes;
18224eeab4f5SAndrew Shewmaker
1823d5a6474dSZhangPeng free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
18244eeab4f5SAndrew Shewmaker
18254eeab4f5SAndrew Shewmaker sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
18264eeab4f5SAndrew Shewmaker return 0;
18274eeab4f5SAndrew Shewmaker }
1828a4bc6fc7SPaul Gortmaker subsys_initcall(init_admin_reserve);
1829