blob: 1898b2fe9da5eefdc3b33823a3cd23d2a401118f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/nommu.c
3 *
4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
6 *
7 * See Documentation/nommu-mmap.txt
8 *
David Howells8feae132009-01-08 12:04:47 +00009 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
Paul Mundt29c185e2010-12-24 12:08:30 +090013 * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/swap.h>
20#include <linux/file.h>
21#include <linux/highmem.h>
22#include <linux/pagemap.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/blkdev.h>
26#include <linux/backing-dev.h>
27#include <linux/mount.h>
28#include <linux/personality.h>
29#include <linux/security.h>
30#include <linux/syscalls.h>
Al Viro120a7952010-10-30 02:54:44 -040031#include <linux/audit.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060032#include <linux/sched/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/uaccess.h>
35#include <asm/tlb.h>
36#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070037#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000038#include "internal.h"
39
David Howells8feae132009-01-08 12:04:47 +000040#if 0
41#define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43#define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45#define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47#else
48#define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50#define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52#define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56void *high_memory;
57struct page *mem_map;
58unsigned long max_mapnr;
59unsigned long num_physpages;
Hugh Dickins4266c972009-09-23 17:05:53 +010060unsigned long highest_memmap_pfn;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070061struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
63int sysctl_overcommit_ratio = 50; /* default is 50% */
64int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070065int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -070066unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -070067unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068int heap_stack_gap = 0;
69
David Howells33e5d7692009-04-02 16:56:32 -070070atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000071
K. Y. Srinivasan997071b2012-11-15 14:34:42 -080072/*
73 * The global memory commitment made in the system can be a metric
74 * that can be used to drive ballooning decisions when Linux is hosted
75 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
76 * balancing memory across competing virtual machines that are hosted.
77 * Several metrics drive this policy engine including the guest reported
78 * memory commitment.
79 */
80unsigned long vm_memory_committed(void)
81{
82 return percpu_counter_read_positive(&vm_committed_as);
83}
84
85EXPORT_SYMBOL_GPL(vm_memory_committed);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070088EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David Howells8feae132009-01-08 12:04:47 +000090/* list of mapped, potentially shareable regions */
91static struct kmem_cache *vm_region_jar;
92struct rb_root nommu_region_tree = RB_ROOT;
93DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040095const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * Return the total memory allocated for this pointer, not
100 * just what the caller asked for.
101 *
102 * Doesn't have to be accurate, i.e. may have races.
103 */
104unsigned int kobjsize(const void *objp)
105{
106 struct page *page;
107
Michael Hennerich4016a132008-04-28 02:13:38 -0700108 /*
109 * If the object we have should not have ksize performed on it,
110 * return size of 0
111 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900112 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700113 return 0;
114
115 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700116
117 /*
118 * If the allocator sets PageSlab, we know the pointer came from
119 * kmalloc().
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (PageSlab(page))
122 return ksize(objp);
123
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700124 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000125 * If it's not a compound page, see if we have a matching VMA
126 * region. This test is intentionally done in reverse order,
127 * so if there's no VMA, we still fall through and hand back
128 * PAGE_SIZE for 0-order pages.
129 */
130 if (!PageCompound(page)) {
131 struct vm_area_struct *vma;
132
133 vma = find_vma(current->mm, (unsigned long)objp);
134 if (vma)
135 return vma->vm_end - vma->vm_start;
136 }
137
138 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700139 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900140 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700141 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900142 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Michel Lespinasse28a35712013-02-22 16:35:55 -0800145long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
146 unsigned long start, unsigned long nr_pages,
147 unsigned int foll_flags, struct page **pages,
148 struct vm_area_struct **vmas, int *nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700150 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700151 unsigned long vm_flags;
152 int i;
153
154 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700155 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700156 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700157 vm_flags = (foll_flags & FOLL_WRITE) ?
158 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
159 vm_flags &= (foll_flags & FOLL_FORCE) ?
160 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Peter Zijlstra9d737772009-06-25 11:58:55 +0200162 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000163 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700164 if (!vma)
165 goto finish_or_fault;
166
167 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700168 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
169 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700170 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (pages) {
173 pages[i] = virt_to_page(start);
174 if (pages[i])
175 page_cache_get(pages[i]);
176 }
177 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700178 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000179 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
David Howells7b4d5b82006-09-27 01:50:18 -0700181
182 return i;
183
184finish_or_fault:
185 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Nick Pigginb291f002008-10-18 20:26:44 -0700187
Nick Pigginb291f002008-10-18 20:26:44 -0700188/*
189 * get a list of pages in an address range belonging to the specified process
190 * and indicate the VMA that covers each page
191 * - this is potentially dodgy as we may end incrementing the page count of a
192 * slab page or a secondary page from a compound page
193 * - don't permit access to VMAs that don't support it, such as I/O mappings
194 */
Michel Lespinasse28a35712013-02-22 16:35:55 -0800195long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
196 unsigned long start, unsigned long nr_pages,
197 int write, int force, struct page **pages,
198 struct vm_area_struct **vmas)
Nick Pigginb291f002008-10-18 20:26:44 -0700199{
200 int flags = 0;
201
202 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700203 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700204 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700205 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700206
Michel Lespinasse53a77062011-01-13 15:46:14 -0800207 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
208 NULL);
Nick Pigginb291f002008-10-18 20:26:44 -0700209}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000210EXPORT_SYMBOL(get_user_pages);
211
Paul Mundtdfc2f912009-06-26 04:31:57 +0900212/**
213 * follow_pfn - look up PFN at a user virtual address
214 * @vma: memory mapping
215 * @address: user virtual address
216 * @pfn: location to store found PFN
217 *
218 * Only IO mappings and raw PFN mappings are allowed.
219 *
220 * Returns zero and the pfn at @pfn on success, -ve otherwise.
221 */
222int follow_pfn(struct vm_area_struct *vma, unsigned long address,
223 unsigned long *pfn)
224{
225 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
226 return -EINVAL;
227
228 *pfn = address >> PAGE_SHIFT;
229 return 0;
230}
231EXPORT_SYMBOL(follow_pfn);
232
Joonsoo Kimf1c40692013-04-29 15:07:37 -0700233LIST_HEAD(vmap_area_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800235void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 kfree(addr);
238}
Paul Mundtb5073172007-07-21 04:37:25 -0700239EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Al Virodd0fc662005-10-07 07:46:04 +0100241void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200244 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
245 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Nick Piggin84097512006-03-22 00:08:34 -0800247 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Paul Mundtb5073172007-07-21 04:37:25 -0700249EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Paul Mundtf905bc42008-02-04 22:29:59 -0800251void *vmalloc_user(unsigned long size)
252{
253 void *ret;
254
255 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
256 PAGE_KERNEL);
257 if (ret) {
258 struct vm_area_struct *vma;
259
260 down_write(&current->mm->mmap_sem);
261 vma = find_vma(current->mm, (unsigned long)ret);
262 if (vma)
263 vma->vm_flags |= VM_USERMAP;
264 up_write(&current->mm->mmap_sem);
265 }
266
267 return ret;
268}
269EXPORT_SYMBOL(vmalloc_user);
270
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800271struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 return virt_to_page(addr);
274}
Paul Mundtb5073172007-07-21 04:37:25 -0700275EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800277unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 return page_to_pfn(virt_to_page(addr));
280}
Paul Mundtb5073172007-07-21 04:37:25 -0700281EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283long vread(char *buf, char *addr, unsigned long count)
284{
Chen Gang9bde9162013-07-03 15:02:36 -0700285 /* Don't allow overflow */
286 if ((unsigned long) buf + count < count)
287 count = -(unsigned long) buf;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 memcpy(buf, addr, count);
290 return count;
291}
292
293long vwrite(char *buf, char *addr, unsigned long count)
294{
295 /* Don't allow overflow */
296 if ((unsigned long) addr + count < count)
297 count = -(unsigned long) addr;
298
299 memcpy(addr, buf, count);
300 return(count);
301}
302
303/*
304 * vmalloc - allocate virtually continguos memory
305 *
306 * @size: allocation size
307 *
308 * Allocate enough pages to cover @size from the page level
309 * allocator and map them into continguos kernel virtual space.
310 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200311 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * use __vmalloc() instead.
313 */
314void *vmalloc(unsigned long size)
315{
316 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
317}
Andrew Mortonf6138882006-02-28 16:59:18 -0800318EXPORT_SYMBOL(vmalloc);
319
Dave Younge1ca7782010-10-26 14:22:06 -0700320/*
321 * vzalloc - allocate virtually continguos memory with zero fill
322 *
323 * @size: allocation size
324 *
325 * Allocate enough pages to cover @size from the page level
326 * allocator and map them into continguos kernel virtual space.
327 * The memory allocated is set to zero.
328 *
329 * For tight control over page level allocator and protection flags
330 * use __vmalloc() instead.
331 */
332void *vzalloc(unsigned long size)
333{
334 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
335 PAGE_KERNEL);
336}
337EXPORT_SYMBOL(vzalloc);
338
339/**
340 * vmalloc_node - allocate memory on a specific node
341 * @size: allocation size
342 * @node: numa node
343 *
344 * Allocate enough pages to cover @size from the page level
345 * allocator and map them into contiguous kernel virtual space.
346 *
347 * For tight control over page level allocator and protection flags
348 * use __vmalloc() instead.
349 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800350void *vmalloc_node(unsigned long size, int node)
351{
352 return vmalloc(size);
353}
Paul Mundt9a14f652010-12-24 11:50:34 +0900354EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700355
356/**
357 * vzalloc_node - allocate memory on a specific node with zero fill
358 * @size: allocation size
359 * @node: numa node
360 *
361 * Allocate enough pages to cover @size from the page level
362 * allocator and map them into contiguous kernel virtual space.
363 * The memory allocated is set to zero.
364 *
365 * For tight control over page level allocator and protection flags
366 * use __vmalloc() instead.
367 */
368void *vzalloc_node(unsigned long size, int node)
369{
370 return vzalloc(size);
371}
372EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Paul Mundt1af446e2008-08-04 16:01:47 +0900374#ifndef PAGE_KERNEL_EXEC
375# define PAGE_KERNEL_EXEC PAGE_KERNEL
376#endif
377
378/**
379 * vmalloc_exec - allocate virtually contiguous, executable memory
380 * @size: allocation size
381 *
382 * Kernel-internal function to allocate enough pages to cover @size
383 * the page level allocator and map them into contiguous and
384 * executable kernel virtual space.
385 *
386 * For tight control over page level allocator and protection flags
387 * use __vmalloc() instead.
388 */
389
390void *vmalloc_exec(unsigned long size)
391{
392 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
393}
394
Paul Mundtb5073172007-07-21 04:37:25 -0700395/**
396 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * @size: allocation size
398 *
399 * Allocate enough 32bit PA addressable pages to cover @size from the
400 * page level allocator and map them into continguos kernel virtual space.
401 */
402void *vmalloc_32(unsigned long size)
403{
404 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
405}
Paul Mundtb5073172007-07-21 04:37:25 -0700406EXPORT_SYMBOL(vmalloc_32);
407
408/**
409 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
410 * @size: allocation size
411 *
412 * The resulting memory area is 32bit addressable and zeroed so it can be
413 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800414 *
415 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
416 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700417 */
418void *vmalloc_32_user(unsigned long size)
419{
Paul Mundtf905bc42008-02-04 22:29:59 -0800420 /*
421 * We'll have to sort out the ZONE_DMA bits for 64-bit,
422 * but for now this can simply use vmalloc_user() directly.
423 */
424 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700425}
426EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
429{
430 BUG();
431 return NULL;
432}
Paul Mundtb5073172007-07-21 04:37:25 -0700433EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800435void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 BUG();
438}
Paul Mundtb5073172007-07-21 04:37:25 -0700439EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Paul Mundteb6434d2009-01-21 17:45:47 +0900441void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
442{
443 BUG();
444 return NULL;
445}
446EXPORT_SYMBOL(vm_map_ram);
447
448void vm_unmap_ram(const void *mem, unsigned int count)
449{
450 BUG();
451}
452EXPORT_SYMBOL(vm_unmap_ram);
453
454void vm_unmap_aliases(void)
455{
456}
457EXPORT_SYMBOL_GPL(vm_unmap_aliases);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700460 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
461 * have one.
462 */
463void __attribute__((weak)) vmalloc_sync_all(void)
464{
465}
466
Paul Mundt29c185e2010-12-24 12:08:30 +0900467/**
468 * alloc_vm_area - allocate a range of kernel address space
469 * @size: size of the area
470 *
471 * Returns: NULL on failure, vm_struct on success
472 *
473 * This function reserves a range of kernel address space, and
474 * allocates pagetables to map that range. No actual mappings
475 * are created. If the kernel address space is not shared
476 * between processes, it syncs the pagetable across all
477 * processes.
478 */
David Vrabelcd129092011-09-29 16:53:32 +0100479struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900480{
481 BUG();
482 return NULL;
483}
484EXPORT_SYMBOL_GPL(alloc_vm_area);
485
486void free_vm_area(struct vm_struct *area)
487{
488 BUG();
489}
490EXPORT_SYMBOL_GPL(free_vm_area);
491
Paul Mundtb5073172007-07-21 04:37:25 -0700492int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
493 struct page *page)
494{
495 return -EINVAL;
496}
497EXPORT_SYMBOL(vm_insert_page);
498
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700499/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * sys_brk() for the most part doesn't need the global kernel
501 * lock, except when an application is doing something nasty
502 * like trying to un-brk an area that has already been mapped
503 * to a regular file. in this case, the unmapping will need
504 * to invoke file system routines that need the global lock.
505 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100506SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct mm_struct *mm = current->mm;
509
510 if (brk < mm->start_brk || brk > mm->context.end_brk)
511 return mm->brk;
512
513 if (mm->brk == brk)
514 return mm->brk;
515
516 /*
517 * Always allow shrinking brk
518 */
519 if (brk <= mm->brk) {
520 mm->brk = brk;
521 return brk;
522 }
523
524 /*
525 * Ok, looks good - let it rip.
526 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000527 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return mm->brk = brk;
529}
530
David Howells8feae132009-01-08 12:04:47 +0000531/*
532 * initialise the VMA and region record slabs
533 */
534void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700536 int ret;
537
538 ret = percpu_counter_init(&vm_committed_as, 0);
539 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700540 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
David Howells8feae132009-01-08 12:04:47 +0000543/*
544 * validate the region tree
545 * - the caller must hold the region lock
546 */
547#ifdef CONFIG_DEBUG_NOMMU_REGIONS
548static noinline void validate_nommu_regions(void)
549{
550 struct vm_region *region, *last;
551 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
David Howells8feae132009-01-08 12:04:47 +0000553 lastp = rb_first(&nommu_region_tree);
554 if (!lastp)
555 return;
556
557 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700558 BUG_ON(unlikely(last->vm_end <= last->vm_start));
559 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000560
561 while ((p = rb_next(lastp))) {
562 region = rb_entry(p, struct vm_region, vm_rb);
563 last = rb_entry(lastp, struct vm_region, vm_rb);
564
David Howells33e5d7692009-04-02 16:56:32 -0700565 BUG_ON(unlikely(region->vm_end <= region->vm_start));
566 BUG_ON(unlikely(region->vm_top < region->vm_end));
567 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000568
569 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571}
David Howells8feae132009-01-08 12:04:47 +0000572#else
David Howells33e5d7692009-04-02 16:56:32 -0700573static void validate_nommu_regions(void)
574{
575}
David Howells8feae132009-01-08 12:04:47 +0000576#endif
577
578/*
579 * add a region into the global tree
580 */
581static void add_nommu_region(struct vm_region *region)
582{
583 struct vm_region *pregion;
584 struct rb_node **p, *parent;
585
586 validate_nommu_regions();
587
David Howells8feae132009-01-08 12:04:47 +0000588 parent = NULL;
589 p = &nommu_region_tree.rb_node;
590 while (*p) {
591 parent = *p;
592 pregion = rb_entry(parent, struct vm_region, vm_rb);
593 if (region->vm_start < pregion->vm_start)
594 p = &(*p)->rb_left;
595 else if (region->vm_start > pregion->vm_start)
596 p = &(*p)->rb_right;
597 else if (pregion == region)
598 return;
599 else
600 BUG();
601 }
602
603 rb_link_node(&region->vm_rb, parent, p);
604 rb_insert_color(&region->vm_rb, &nommu_region_tree);
605
606 validate_nommu_regions();
607}
608
609/*
610 * delete a region from the global tree
611 */
612static void delete_nommu_region(struct vm_region *region)
613{
614 BUG_ON(!nommu_region_tree.rb_node);
615
616 validate_nommu_regions();
617 rb_erase(&region->vm_rb, &nommu_region_tree);
618 validate_nommu_regions();
619}
620
621/*
622 * free a contiguous series of pages
623 */
624static void free_page_series(unsigned long from, unsigned long to)
625{
626 for (; from < to; from += PAGE_SIZE) {
627 struct page *page = virt_to_page(from);
628
629 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700630 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000631 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700632 kdebug("free page %p: refcount not one: %d",
633 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000634 put_page(page);
635 }
636}
637
638/*
639 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700640 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000641 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000642 * will equal vm_start
643 */
644static void __put_nommu_region(struct vm_region *region)
645 __releases(nommu_region_sem)
646{
David Howells1e2ae592010-01-15 17:01:33 -0800647 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000648
649 BUG_ON(!nommu_region_tree.rb_node);
650
David Howells1e2ae592010-01-15 17:01:33 -0800651 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000652 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000653 delete_nommu_region(region);
654 up_write(&nommu_region_sem);
655
656 if (region->vm_file)
657 fput(region->vm_file);
658
659 /* IO memory and memory shared directly out of the pagecache
660 * from ramfs/tmpfs mustn't be released here */
661 if (region->vm_flags & VM_MAPPED_COPY) {
662 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000663 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000664 }
665 kmem_cache_free(vm_region_jar, region);
666 } else {
667 up_write(&nommu_region_sem);
668 }
669}
670
671/*
672 * release a reference to a region
673 */
674static void put_nommu_region(struct vm_region *region)
675{
676 down_write(&nommu_region_sem);
677 __put_nommu_region(region);
678}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
David Howells30340972006-09-27 01:50:20 -0700680/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700681 * update protection on a vma
682 */
683static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
684{
685#ifdef CONFIG_MPU
686 struct mm_struct *mm = vma->vm_mm;
687 long start = vma->vm_start & PAGE_MASK;
688 while (start < vma->vm_end) {
689 protect_page(mm, start, flags);
690 start += PAGE_SIZE;
691 }
692 update_protections(mm);
693#endif
694}
695
696/*
David Howells30340972006-09-27 01:50:20 -0700697 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000698 * and tree and add to the address space's page tree also if not an anonymous
699 * page
David Howells30340972006-09-27 01:50:20 -0700700 * - should be called with mm->mmap_sem held writelocked
701 */
David Howells8feae132009-01-08 12:04:47 +0000702static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700703{
Namhyung Kim6038def2011-05-24 17:11:22 -0700704 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000705 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700706 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700707
David Howells8feae132009-01-08 12:04:47 +0000708 kenter(",%p", vma);
709
710 BUG_ON(!vma->vm_region);
711
712 mm->map_count++;
713 vma->vm_mm = mm;
714
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700715 protect_vma(vma, vma->vm_flags);
716
David Howells8feae132009-01-08 12:04:47 +0000717 /* add the VMA to the mapping */
718 if (vma->vm_file) {
719 mapping = vma->vm_file->f_mapping;
720
David Howells918e5562012-02-23 13:50:35 +0000721 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000722 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700723 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000724 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000725 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000726 }
727
728 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700729 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000730 p = &mm->mm_rb.rb_node;
731 while (*p) {
732 parent = *p;
733 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
734
735 /* sort by: start addr, end addr, VMA struct addr in that order
736 * (the latter is necessary as we may get identical VMAs) */
737 if (vma->vm_start < pvma->vm_start)
738 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700739 else if (vma->vm_start > pvma->vm_start) {
740 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000741 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700742 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000743 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700744 else if (vma->vm_end > pvma->vm_end) {
745 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000746 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700747 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000748 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700749 else if (vma > pvma) {
750 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000751 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700752 } else
David Howells8feae132009-01-08 12:04:47 +0000753 BUG();
754 }
755
756 rb_link_node(&vma->vm_rb, parent, p);
757 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
758
759 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700760 prev = NULL;
761 if (rb_prev)
762 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700763
Namhyung Kim6038def2011-05-24 17:11:22 -0700764 __vma_link_list(mm, vma, prev, parent);
David Howells8feae132009-01-08 12:04:47 +0000765}
766
767/*
768 * delete a VMA from its owning mm_struct and address space
769 */
770static void delete_vma_from_mm(struct vm_area_struct *vma)
771{
David Howells8feae132009-01-08 12:04:47 +0000772 struct address_space *mapping;
773 struct mm_struct *mm = vma->vm_mm;
774
775 kenter("%p", vma);
776
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700777 protect_vma(vma, 0);
778
David Howells8feae132009-01-08 12:04:47 +0000779 mm->map_count--;
780 if (mm->mmap_cache == vma)
781 mm->mmap_cache = NULL;
782
783 /* remove the VMA from the mapping */
784 if (vma->vm_file) {
785 mapping = vma->vm_file->f_mapping;
786
David Howells918e5562012-02-23 13:50:35 +0000787 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000788 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700789 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000790 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000791 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000792 }
793
794 /* remove from the MM's tree and list */
795 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700796
797 if (vma->vm_prev)
798 vma->vm_prev->vm_next = vma->vm_next;
799 else
800 mm->mmap = vma->vm_next;
801
802 if (vma->vm_next)
803 vma->vm_next->vm_prev = vma->vm_prev;
David Howells8feae132009-01-08 12:04:47 +0000804}
805
806/*
807 * destroy a VMA record
808 */
809static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
810{
811 kenter("%p", vma);
812 if (vma->vm_ops && vma->vm_ops->close)
813 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700814 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000815 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000816 put_nommu_region(vma->vm_region);
817 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700818}
819
820/*
821 * look up the first VMA in which addr resides, NULL if none
822 * - should be called with mm->mmap_sem at least held readlocked
823 */
824struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
825{
David Howells8feae132009-01-08 12:04:47 +0000826 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700827
David Howells8feae132009-01-08 12:04:47 +0000828 /* check the cache first */
Jan Stancekb6a9b7f2013-04-04 11:35:10 -0700829 vma = ACCESS_ONCE(mm->mmap_cache);
David Howells8feae132009-01-08 12:04:47 +0000830 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
831 return vma;
832
Namhyung Kime922c4c2011-05-24 17:11:24 -0700833 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000834 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700835 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000836 if (vma->vm_start > addr)
837 return NULL;
838 if (vma->vm_end > addr) {
839 mm->mmap_cache = vma;
840 return vma;
841 }
David Howells30340972006-09-27 01:50:20 -0700842 }
843
David Howells30340972006-09-27 01:50:20 -0700844 return NULL;
845}
846EXPORT_SYMBOL(find_vma);
847
848/*
David Howells930e6522006-09-27 01:50:22 -0700849 * find a VMA
850 * - we don't extend stack VMAs under NOMMU conditions
851 */
852struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
853{
David Howells7561e8c2010-03-25 16:48:38 +0000854 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700855}
856
David Howells8feae132009-01-08 12:04:47 +0000857/*
858 * expand a stack to a given address
859 * - not supported under NOMMU conditions
860 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700861int expand_stack(struct vm_area_struct *vma, unsigned long address)
862{
863 return -ENOMEM;
864}
865
David Howells930e6522006-09-27 01:50:22 -0700866/*
David Howells6fa5f802006-09-27 01:50:21 -0700867 * look up the first VMA exactly that exactly matches addr
868 * - should be called with mm->mmap_sem at least held readlocked
869 */
David Howells8feae132009-01-08 12:04:47 +0000870static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
871 unsigned long addr,
872 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000875 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
David Howells8feae132009-01-08 12:04:47 +0000877 /* check the cache first */
878 vma = mm->mmap_cache;
879 if (vma && vma->vm_start == addr && vma->vm_end == end)
880 return vma;
881
Namhyung Kime922c4c2011-05-24 17:11:24 -0700882 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000883 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700884 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000885 if (vma->vm_start < addr)
886 continue;
887 if (vma->vm_start > addr)
888 return NULL;
889 if (vma->vm_end == end) {
890 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return vma;
David Howells8feae132009-01-08 12:04:47 +0000892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
895 return NULL;
896}
897
David Howells30340972006-09-27 01:50:20 -0700898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * determine whether a mapping should be permitted and, if so, what sort of
900 * mapping we're capable of supporting
901 */
902static int validate_mmap_request(struct file *file,
903 unsigned long addr,
904 unsigned long len,
905 unsigned long prot,
906 unsigned long flags,
907 unsigned long pgoff,
908 unsigned long *_capabilities)
909{
David Howells8feae132009-01-08 12:04:47 +0000910 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 int ret;
912
913 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100914 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 printk(KERN_DEBUG
916 "%d: Can't do fixed-address/overlay mmap of RAM\n",
917 current->pid);
918 return -EINVAL;
919 }
920
921 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
922 (flags & MAP_TYPE) != MAP_SHARED)
923 return -EINVAL;
924
Mike Frysingerf81cff02006-12-06 12:02:59 +1000925 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EINVAL;
927
Mike Frysingerf81cff02006-12-06 12:02:59 +1000928 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000929 rlen = PAGE_ALIGN(len);
930 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000931 return -ENOMEM;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000934 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000935 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (file) {
938 /* validate file mapping requests */
939 struct address_space *mapping;
940
941 /* files must support mmap */
942 if (!file->f_op || !file->f_op->mmap)
943 return -ENODEV;
944
945 /* work out if what we've got could possibly be shared
946 * - we support chardevs that provide their own "memory"
947 * - we support files/blockdevs that are memory backed
948 */
949 mapping = file->f_mapping;
950 if (!mapping)
Al Viro496ad9a2013-01-23 17:07:38 -0500951 mapping = file_inode(file)->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 capabilities = 0;
954 if (mapping && mapping->backing_dev_info)
955 capabilities = mapping->backing_dev_info->capabilities;
956
957 if (!capabilities) {
958 /* no explicit capabilities set, so assume some
959 * defaults */
Al Viro496ad9a2013-01-23 17:07:38 -0500960 switch (file_inode(file)->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 case S_IFREG:
962 case S_IFBLK:
963 capabilities = BDI_CAP_MAP_COPY;
964 break;
965
966 case S_IFCHR:
967 capabilities =
968 BDI_CAP_MAP_DIRECT |
969 BDI_CAP_READ_MAP |
970 BDI_CAP_WRITE_MAP;
971 break;
972
973 default:
974 return -EINVAL;
975 }
976 }
977
978 /* eliminate any capabilities that we can't support on this
979 * device */
980 if (!file->f_op->get_unmapped_area)
981 capabilities &= ~BDI_CAP_MAP_DIRECT;
982 if (!file->f_op->read)
983 capabilities &= ~BDI_CAP_MAP_COPY;
984
Graff Yang28d7a6a2009-08-18 14:11:17 -0700985 /* The file shall have been opened with read permission. */
986 if (!(file->f_mode & FMODE_READ))
987 return -EACCES;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (flags & MAP_SHARED) {
990 /* do checks for writing, appending and locking */
991 if ((prot & PROT_WRITE) &&
992 !(file->f_mode & FMODE_WRITE))
993 return -EACCES;
994
Al Viro496ad9a2013-01-23 17:07:38 -0500995 if (IS_APPEND(file_inode(file)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 (file->f_mode & FMODE_WRITE))
997 return -EACCES;
998
Al Viro496ad9a2013-01-23 17:07:38 -0500999 if (locks_verify_locked(file_inode(file)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -EAGAIN;
1001
1002 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1003 return -ENODEV;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* we mustn't privatise shared mappings */
1006 capabilities &= ~BDI_CAP_MAP_COPY;
1007 }
1008 else {
1009 /* we're going to read the file into private memory we
1010 * allocate */
1011 if (!(capabilities & BDI_CAP_MAP_COPY))
1012 return -ENODEV;
1013
1014 /* we don't permit a private writable mapping to be
1015 * shared with the backing device */
1016 if (prot & PROT_WRITE)
1017 capabilities &= ~BDI_CAP_MAP_DIRECT;
1018 }
1019
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001020 if (capabilities & BDI_CAP_MAP_DIRECT) {
1021 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1022 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1023 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1024 ) {
1025 capabilities &= ~BDI_CAP_MAP_DIRECT;
1026 if (flags & MAP_SHARED) {
1027 printk(KERN_WARNING
1028 "MAP_SHARED not completely supported on !MMU\n");
1029 return -EINVAL;
1030 }
1031 }
1032 }
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* handle executable mappings and implied executable
1035 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -08001036 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (prot & PROT_EXEC)
1038 return -EPERM;
1039 }
1040 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1041 /* handle implication of PROT_EXEC by PROT_READ */
1042 if (current->personality & READ_IMPLIES_EXEC) {
1043 if (capabilities & BDI_CAP_EXEC_MAP)
1044 prot |= PROT_EXEC;
1045 }
1046 }
1047 else if ((prot & PROT_READ) &&
1048 (prot & PROT_EXEC) &&
1049 !(capabilities & BDI_CAP_EXEC_MAP)
1050 ) {
1051 /* backing file is not executable, try to copy */
1052 capabilities &= ~BDI_CAP_MAP_DIRECT;
1053 }
1054 }
1055 else {
1056 /* anonymous mappings are always memory backed and can be
1057 * privately mapped
1058 */
1059 capabilities = BDI_CAP_MAP_COPY;
1060
1061 /* handle PROT_EXEC implication by PROT_READ */
1062 if ((prot & PROT_READ) &&
1063 (current->personality & READ_IMPLIES_EXEC))
1064 prot |= PROT_EXEC;
1065 }
1066
1067 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -04001068 ret = security_mmap_addr(addr);
1069 if (ret < 0)
1070 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* looks okay */
1073 *_capabilities = capabilities;
1074 return 0;
1075}
1076
1077/*
1078 * we've determined that we can make the mapping, now translate what we
1079 * now know into VMA flags
1080 */
1081static unsigned long determine_vm_flags(struct file *file,
1082 unsigned long prot,
1083 unsigned long flags,
1084 unsigned long capabilities)
1085{
1086 unsigned long vm_flags;
1087
1088 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* vm_flags |= mm->def_flags; */
1090
1091 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1092 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001093 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (file && !(prot & PROT_WRITE))
1095 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001096 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* overlay a shareable mapping on the backing device or inode
1098 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1099 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001100 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001102 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
1105 /* refuse to let anyone share private mappings with this process if
1106 * it's being traced - otherwise breakpoints set in it may interfere
1107 * with another untraced process
1108 */
Tejun Heoa288eec2011-06-17 16:50:37 +02001109 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 vm_flags &= ~VM_MAYSHARE;
1111
1112 return vm_flags;
1113}
1114
1115/*
David Howells8feae132009-01-08 12:04:47 +00001116 * set up a shared mapping on a file (the driver or filesystem provides and
1117 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
David Howells8feae132009-01-08 12:04:47 +00001119static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 int ret;
1122
1123 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001124 if (ret == 0) {
1125 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001126 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (ret != -ENOSYS)
1129 return ret;
1130
David Howells3fa30462010-03-23 13:35:21 -07001131 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1132 * opposed to tried but failed) so we can only give a suitable error as
1133 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -ENODEV;
1135}
1136
1137/*
1138 * set up a private mapping or an anonymous shared mapping
1139 */
David Howells8feae132009-01-08 12:04:47 +00001140static int do_mmap_private(struct vm_area_struct *vma,
1141 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001142 unsigned long len,
1143 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
David Howells8feae132009-01-08 12:04:47 +00001145 struct page *pages;
Bob Liuf67d9b12011-05-24 17:12:56 -07001146 unsigned long total, point, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 void *base;
David Howells8feae132009-01-08 12:04:47 +00001148 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* invoke the file's mapping function so that it can keep track of
1151 * shared mappings on devices or memory
1152 * - VM_MAYSHARE will be set if it may attempt to share
1153 */
David Howells645d83c2009-09-24 15:13:10 +01001154 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001156 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001158 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1159 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001162 if (ret != -ENOSYS)
1163 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* getting an ENOSYS error indicates that direct mmap isn't
1166 * possible (as opposed to tried but failed) so we'll try to
1167 * make a private copy of the data and map that instead */
1168 }
1169
David Howells8feae132009-01-08 12:04:47 +00001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* allocate some memory to hold the mapping
1172 * - note that this may not return a page-aligned address if the object
1173 * we're allocating is smaller than a page
1174 */
Bob Liuf67d9b12011-05-24 17:12:56 -07001175 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +00001176 kdebug("alloc order %d for %lx", order, len);
1177
1178 pages = alloc_pages(GFP_KERNEL, order);
1179 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto enomem;
1181
David Howells8feae132009-01-08 12:04:47 +00001182 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001183 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Bob Liuf67d9b12011-05-24 17:12:56 -07001185 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001186
1187 /* we allocated a power-of-2 sized page set, so we may want to trim off
1188 * the excess */
1189 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1190 while (total > point) {
1191 order = ilog2(total - point);
1192 n = 1 << order;
1193 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001194 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001195 total -= n;
1196 set_page_refcounted(pages + total);
1197 __free_pages(pages + total, order);
1198 }
David Howells8feae132009-01-08 12:04:47 +00001199 }
1200
David Howells8feae132009-01-08 12:04:47 +00001201 for (point = 1; point < total; point++)
1202 set_page_refcounted(&pages[point]);
1203
1204 base = page_address(pages);
1205 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1206 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001207 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001208 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001209
1210 vma->vm_start = region->vm_start;
1211 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (vma->vm_file) {
1214 /* read the contents of a file into the copy */
1215 mm_segment_t old_fs;
1216 loff_t fpos;
1217
1218 fpos = vma->vm_pgoff;
1219 fpos <<= PAGE_SHIFT;
1220
1221 old_fs = get_fs();
1222 set_fs(KERNEL_DS);
Bob Liuf67d9b12011-05-24 17:12:56 -07001223 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 set_fs(old_fs);
1225
1226 if (ret < 0)
1227 goto error_free;
1228
1229 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001230 if (ret < len)
1231 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
1235 return 0;
1236
1237error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001238 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001239 region->vm_start = vma->vm_start = 0;
1240 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001241 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return ret;
1243
1244enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001245 printk("Allocation of length %lu from process %d (%s) failed\n",
1246 len, current->pid, current->comm);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001247 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -ENOMEM;
1249}
1250
1251/*
1252 * handle mapping creation for uClinux
1253 */
Al Viroe3fc6292012-05-30 20:08:42 -04001254unsigned long do_mmap_pgoff(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned long addr,
1256 unsigned long len,
1257 unsigned long prot,
1258 unsigned long flags,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001259 unsigned long pgoff,
Michel Lespinasse41badc12013-02-22 16:32:47 -08001260 unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
David Howells8feae132009-01-08 12:04:47 +00001262 struct vm_area_struct *vma;
1263 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001265 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int ret;
1267
David Howells8feae132009-01-08 12:04:47 +00001268 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1269
Michel Lespinasse41badc12013-02-22 16:32:47 -08001270 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /* decide whether we should attempt the mapping, and if so what sort of
1273 * mapping */
1274 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1275 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001276 if (ret < 0) {
1277 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return ret;
David Howells8feae132009-01-08 12:04:47 +00001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
David Howells06aab5a2009-09-24 12:33:48 +01001281 /* we ignore the address hint */
1282 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001283 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* we've determined that we can make the mapping, now translate what we
1286 * now know into VMA flags */
1287 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1288
David Howells8feae132009-01-08 12:04:47 +00001289 /* we're going to need to record the mapping */
1290 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1291 if (!region)
1292 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
David Howells8feae132009-01-08 12:04:47 +00001294 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1295 if (!vma)
1296 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
David Howells1e2ae592010-01-15 17:01:33 -08001298 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001299 region->vm_flags = vm_flags;
1300 region->vm_pgoff = pgoff;
1301
Rik van Riel5beb4932010-03-05 13:42:07 -08001302 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001303 vma->vm_flags = vm_flags;
1304 vma->vm_pgoff = pgoff;
1305
1306 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001307 region->vm_file = get_file(file);
1308 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001309 }
1310
1311 down_write(&nommu_region_sem);
1312
1313 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001315 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * - shared mappings on character devices and memory backed files are
1317 * permitted to overlap inexactly as far as we are concerned for in
1318 * these cases, sharing is handled in the driver or filesystem rather
1319 * than here
1320 */
1321 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001322 struct vm_region *pregion;
1323 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
David Howells8feae132009-01-08 12:04:47 +00001325 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1326 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001327
David Howells8feae132009-01-08 12:04:47 +00001328 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1329 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
David Howells8feae132009-01-08 12:04:47 +00001331 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 continue;
1333
1334 /* search for overlapping mappings on the same file */
Al Viro496ad9a2013-01-23 17:07:38 -05001335 if (file_inode(pregion->vm_file) !=
1336 file_inode(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 continue;
1338
David Howells8feae132009-01-08 12:04:47 +00001339 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 continue;
1341
David Howells8feae132009-01-08 12:04:47 +00001342 rpglen = pregion->vm_end - pregion->vm_start;
1343 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1344 rpgend = pregion->vm_pgoff + rpglen;
1345 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 continue;
1347
David Howells8feae132009-01-08 12:04:47 +00001348 /* handle inexactly overlapping matches between
1349 * mappings */
1350 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1351 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1352 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1354 goto sharing_violation;
1355 continue;
1356 }
1357
David Howells8feae132009-01-08 12:04:47 +00001358 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001359 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001360 vma->vm_region = pregion;
1361 start = pregion->vm_start;
1362 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1363 vma->vm_start = start;
1364 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
David Howells8feae132009-01-08 12:04:47 +00001366 if (pregion->vm_flags & VM_MAPPED_COPY) {
1367 kdebug("share copy");
1368 vma->vm_flags |= VM_MAPPED_COPY;
1369 } else {
1370 kdebug("share mmap");
1371 ret = do_mmap_shared_file(vma);
1372 if (ret < 0) {
1373 vma->vm_region = NULL;
1374 vma->vm_start = 0;
1375 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001376 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001377 pregion = NULL;
1378 goto error_just_free;
1379 }
1380 }
1381 fput(region->vm_file);
1382 kmem_cache_free(vm_region_jar, region);
1383 region = pregion;
1384 result = start;
1385 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* obtain the address at which to make a shared mapping
1389 * - this is the hook for quasi-memory character devices to
1390 * tell us the location of a shared mapping
1391 */
David Howells645d83c2009-09-24 15:13:10 +01001392 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 addr = file->f_op->get_unmapped_area(file, addr, len,
1394 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001395 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001397 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001398 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /* the driver refused to tell us where to site
1401 * the mapping so we'll have to attempt to copy
1402 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001403 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001405 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001408 } else {
1409 vma->vm_start = region->vm_start = addr;
1410 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 }
1413 }
1414
David Howells8feae132009-01-08 12:04:47 +00001415 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
David Howells645d83c2009-09-24 15:13:10 +01001417 /* set up the mapping
1418 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001421 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 else
David Howells645d83c2009-09-24 15:13:10 +01001423 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001425 goto error_just_free;
1426 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001427
Jie Zhangea637632009-12-14 18:00:02 -08001428 /* clear anonymous mappings that don't ask for uninitialized data */
1429 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1430 memset((void *)region->vm_start, 0,
1431 region->vm_end - region->vm_start);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001434 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 current->mm->total_vm += len >> PAGE_SHIFT;
1437
David Howells8feae132009-01-08 12:04:47 +00001438share:
1439 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Mike Frysingercfe79c02010-01-06 17:23:23 +00001441 /* we flush the region from the icache only when the first executable
1442 * mapping of it is made */
1443 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1444 flush_icache_range(region->vm_start, region->vm_end);
1445 region->vm_icache_flushed = true;
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Mike Frysingercfe79c02010-01-06 17:23:23 +00001448 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
David Howells8feae132009-01-08 12:04:47 +00001450 kleave(" = %lx", result);
1451 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
David Howells8feae132009-01-08 12:04:47 +00001453error_just_free:
1454 up_write(&nommu_region_sem);
1455error:
David Howells89a86402009-10-30 13:13:26 +00001456 if (region->vm_file)
1457 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001458 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001459 if (vma->vm_file)
1460 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001461 kmem_cache_free(vm_area_cachep, vma);
1462 kleave(" = %d", ret);
1463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
David Howells8feae132009-01-08 12:04:47 +00001465sharing_violation:
1466 up_write(&nommu_region_sem);
1467 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1468 ret = -EINVAL;
1469 goto error;
1470
1471error_getting_vma:
1472 kmem_cache_free(vm_region_jar, region);
1473 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1474 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001476 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -ENOMEM;
1478
David Howells8feae132009-01-08 12:04:47 +00001479error_getting_region:
1480 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1481 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001483 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -ENOMEM;
1485}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001486
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001487SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1488 unsigned long, prot, unsigned long, flags,
1489 unsigned long, fd, unsigned long, pgoff)
1490{
1491 struct file *file = NULL;
1492 unsigned long retval = -EBADF;
1493
Al Viro120a7952010-10-30 02:54:44 -04001494 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001495 if (!(flags & MAP_ANONYMOUS)) {
1496 file = fget(fd);
1497 if (!file)
1498 goto out;
1499 }
1500
1501 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1502
Greg Ungererad1ed292012-06-04 14:29:59 +10001503 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001504
1505 if (file)
1506 fput(file);
1507out:
1508 return retval;
1509}
1510
Christoph Hellwiga4679372010-03-10 15:21:15 -08001511#ifdef __ARCH_WANT_SYS_OLD_MMAP
1512struct mmap_arg_struct {
1513 unsigned long addr;
1514 unsigned long len;
1515 unsigned long prot;
1516 unsigned long flags;
1517 unsigned long fd;
1518 unsigned long offset;
1519};
1520
1521SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1522{
1523 struct mmap_arg_struct a;
1524
1525 if (copy_from_user(&a, arg, sizeof(a)))
1526 return -EFAULT;
1527 if (a.offset & ~PAGE_MASK)
1528 return -EINVAL;
1529
1530 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1531 a.offset >> PAGE_SHIFT);
1532}
1533#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/*
David Howells8feae132009-01-08 12:04:47 +00001536 * split a vma into two pieces at address 'addr', a new vma is allocated either
1537 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
David Howells8feae132009-01-08 12:04:47 +00001539int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1540 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
David Howells8feae132009-01-08 12:04:47 +00001542 struct vm_area_struct *new;
1543 struct vm_region *region;
1544 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
David Howells8feae132009-01-08 12:04:47 +00001546 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
David Howells779c1022010-01-15 17:01:34 -08001548 /* we're only permitted to split anonymous regions (these should have
1549 * only a single usage on the region) */
1550 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001551 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
David Howells8feae132009-01-08 12:04:47 +00001553 if (mm->map_count >= sysctl_max_map_count)
1554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
David Howells8feae132009-01-08 12:04:47 +00001556 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1557 if (!region)
1558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
David Howells8feae132009-01-08 12:04:47 +00001560 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1561 if (!new) {
1562 kmem_cache_free(vm_region_jar, region);
1563 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
David Howells8feae132009-01-08 12:04:47 +00001565
1566 /* most fields are the same, copy all, and then fixup */
1567 *new = *vma;
1568 *region = *vma->vm_region;
1569 new->vm_region = region;
1570
1571 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1572
1573 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001574 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001575 } else {
1576 region->vm_start = new->vm_start = addr;
1577 region->vm_pgoff = new->vm_pgoff += npages;
1578 }
1579
1580 if (new->vm_ops && new->vm_ops->open)
1581 new->vm_ops->open(new);
1582
1583 delete_vma_from_mm(vma);
1584 down_write(&nommu_region_sem);
1585 delete_nommu_region(vma->vm_region);
1586 if (new_below) {
1587 vma->vm_region->vm_start = vma->vm_start = addr;
1588 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1589 } else {
1590 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001591 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001592 }
1593 add_nommu_region(vma->vm_region);
1594 add_nommu_region(new->vm_region);
1595 up_write(&nommu_region_sem);
1596 add_vma_to_mm(mm, vma);
1597 add_vma_to_mm(mm, new);
1598 return 0;
1599}
1600
1601/*
1602 * shrink a VMA by removing the specified chunk from either the beginning or
1603 * the end
1604 */
1605static int shrink_vma(struct mm_struct *mm,
1606 struct vm_area_struct *vma,
1607 unsigned long from, unsigned long to)
1608{
1609 struct vm_region *region;
1610
1611 kenter("");
1612
1613 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1614 * and list */
1615 delete_vma_from_mm(vma);
1616 if (from > vma->vm_start)
1617 vma->vm_end = from;
1618 else
1619 vma->vm_start = to;
1620 add_vma_to_mm(mm, vma);
1621
1622 /* cut the backing region down to size */
1623 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001624 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001625
1626 down_write(&nommu_region_sem);
1627 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001628 if (from > region->vm_start) {
1629 to = region->vm_top;
1630 region->vm_top = region->vm_end = from;
1631 } else {
David Howells8feae132009-01-08 12:04:47 +00001632 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001633 }
David Howells8feae132009-01-08 12:04:47 +00001634 add_nommu_region(region);
1635 up_write(&nommu_region_sem);
1636
1637 free_page_series(from, to);
1638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
David Howells30340972006-09-27 01:50:20 -07001641/*
1642 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001643 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1644 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001645 */
David Howells8feae132009-01-08 12:04:47 +00001646int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
David Howells8feae132009-01-08 12:04:47 +00001648 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001649 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001650 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
David Howells8feae132009-01-08 12:04:47 +00001652 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Bob Liuf67d9b12011-05-24 17:12:56 -07001654 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001655 if (len == 0)
1656 return -EINVAL;
1657
Bob Liuf67d9b12011-05-24 17:12:56 -07001658 end = start + len;
1659
David Howells8feae132009-01-08 12:04:47 +00001660 /* find the first potentially overlapping VMA */
1661 vma = find_vma(mm, start);
1662 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001663 static int limit = 0;
1664 if (limit < 5) {
1665 printk(KERN_WARNING
1666 "munmap of memory not mmapped by process %d"
1667 " (%s): 0x%lx-0x%lx\n",
1668 current->pid, current->comm,
1669 start, start + len - 1);
1670 limit++;
1671 }
David Howells8feae132009-01-08 12:04:47 +00001672 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
David Howells8feae132009-01-08 12:04:47 +00001675 /* we're allowed to split an anonymous VMA but not a file-backed one */
1676 if (vma->vm_file) {
1677 do {
1678 if (start > vma->vm_start) {
1679 kleave(" = -EINVAL [miss]");
1680 return -EINVAL;
1681 }
1682 if (end == vma->vm_end)
1683 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001684 vma = vma->vm_next;
1685 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001686 kleave(" = -EINVAL [split file]");
1687 return -EINVAL;
1688 } else {
1689 /* the chunk must be a subset of the VMA found */
1690 if (start == vma->vm_start && end == vma->vm_end)
1691 goto erase_whole_vma;
1692 if (start < vma->vm_start || end > vma->vm_end) {
1693 kleave(" = -EINVAL [superset]");
1694 return -EINVAL;
1695 }
1696 if (start & ~PAGE_MASK) {
1697 kleave(" = -EINVAL [unaligned start]");
1698 return -EINVAL;
1699 }
1700 if (end != vma->vm_end && end & ~PAGE_MASK) {
1701 kleave(" = -EINVAL [unaligned split]");
1702 return -EINVAL;
1703 }
1704 if (start != vma->vm_start && end != vma->vm_end) {
1705 ret = split_vma(mm, vma, start, 1);
1706 if (ret < 0) {
1707 kleave(" = %d [split]", ret);
1708 return ret;
1709 }
1710 }
1711 return shrink_vma(mm, vma, start, end);
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
David Howells8feae132009-01-08 12:04:47 +00001714erase_whole_vma:
1715 delete_vma_from_mm(vma);
1716 delete_vma(mm, vma);
1717 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719}
Paul Mundtb5073172007-07-21 04:37:25 -07001720EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Al Virobfce2812012-04-20 21:57:04 -04001722int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001723{
Al Virobfce2812012-04-20 21:57:04 -04001724 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001725 int ret;
David Howells30340972006-09-27 01:50:20 -07001726
1727 down_write(&mm->mmap_sem);
1728 ret = do_munmap(mm, addr, len);
1729 up_write(&mm->mmap_sem);
1730 return ret;
1731}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001732EXPORT_SYMBOL(vm_munmap);
1733
1734SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1735{
Al Virobfce2812012-04-20 21:57:04 -04001736 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001737}
David Howells30340972006-09-27 01:50:20 -07001738
1739/*
David Howells8feae132009-01-08 12:04:47 +00001740 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001741 */
David Howells8feae132009-01-08 12:04:47 +00001742void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
David Howells8feae132009-01-08 12:04:47 +00001744 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
David Howells8feae132009-01-08 12:04:47 +00001746 if (!mm)
1747 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Howells8feae132009-01-08 12:04:47 +00001749 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
David Howells8feae132009-01-08 12:04:47 +00001751 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
David Howells8feae132009-01-08 12:04:47 +00001753 while ((vma = mm->mmap)) {
1754 mm->mmap = vma->vm_next;
1755 delete_vma_from_mm(vma);
1756 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001757 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
David Howells8feae132009-01-08 12:04:47 +00001759
1760 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07001763unsigned long vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 return -ENOMEM;
1766}
1767
1768/*
David Howells6fa5f802006-09-27 01:50:21 -07001769 * expand (or shrink) an existing mapping, potentially moving it at the same
1770 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
David Howells6fa5f802006-09-27 01:50:21 -07001772 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001773 * as long as it stays within the region allocated by do_mmap_private() and the
1774 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 *
David Howells6fa5f802006-09-27 01:50:21 -07001776 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Al Viro4b377ba2013-03-04 10:47:59 -05001778static unsigned long do_mremap(unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 unsigned long old_len, unsigned long new_len,
1780 unsigned long flags, unsigned long new_addr)
1781{
David Howells6fa5f802006-09-27 01:50:21 -07001782 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001785 old_len = PAGE_ALIGN(old_len);
1786 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001787 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return (unsigned long) -EINVAL;
1789
David Howells8feae132009-01-08 12:04:47 +00001790 if (addr & ~PAGE_MASK)
1791 return -EINVAL;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (flags & MREMAP_FIXED && new_addr != addr)
1794 return (unsigned long) -EINVAL;
1795
David Howells8feae132009-01-08 12:04:47 +00001796 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001797 if (!vma)
1798 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
David Howells6fa5f802006-09-27 01:50:21 -07001800 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return (unsigned long) -EFAULT;
1802
David Howells6fa5f802006-09-27 01:50:21 -07001803 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return (unsigned long) -EPERM;
1805
David Howells8feae132009-01-08 12:04:47 +00001806 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return (unsigned long) -ENOMEM;
1808
1809 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001810 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001811 return vma->vm_start;
1812}
1813
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001814SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1815 unsigned long, new_len, unsigned long, flags,
1816 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001817{
1818 unsigned long ret;
1819
1820 down_write(&current->mm->mmap_sem);
1821 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1822 up_write(&current->mm->mmap_sem);
1823 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
Michel Lespinasse240aade2013-02-22 16:35:56 -08001826struct page *follow_page_mask(struct vm_area_struct *vma,
1827 unsigned long address, unsigned int flags,
1828 unsigned int *page_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Michel Lespinasse240aade2013-02-22 16:35:56 -08001830 *page_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return NULL;
1832}
1833
Bob Liu8f3b1322011-07-08 15:39:46 -07001834int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1835 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Bob Liu8f3b1322011-07-08 15:39:46 -07001837 if (addr != (pfn << PAGE_SHIFT))
1838 return -EINVAL;
1839
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001840 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
Luke Yang22c4af42006-07-14 00:24:09 -07001843EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Linus Torvalds3c0b9de2013-04-27 13:25:38 -07001845int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1846{
1847 unsigned long pfn = start >> PAGE_SHIFT;
1848 unsigned long vm_len = vma->vm_end - vma->vm_start;
1849
1850 pfn += vma->vm_pgoff;
1851 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1852}
1853EXPORT_SYMBOL(vm_iomap_memory);
1854
Paul Mundtf905bc42008-02-04 22:29:59 -08001855int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1856 unsigned long pgoff)
1857{
1858 unsigned int size = vma->vm_end - vma->vm_start;
1859
1860 if (!(vma->vm_flags & VM_USERMAP))
1861 return -EINVAL;
1862
1863 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1864 vma->vm_end = vma->vm_start + size;
1865
1866 return 0;
1867}
1868EXPORT_SYMBOL(remap_vmalloc_range);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1871 unsigned long len, unsigned long pgoff, unsigned long flags)
1872{
1873 return -ENOMEM;
1874}
1875
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001876void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880void unmap_mapping_range(struct address_space *mapping,
1881 loff_t const holebegin, loff_t const holelen,
1882 int even_cows)
1883{
1884}
Luke Yang22c4af42006-07-14 00:24:09 -07001885EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887/*
1888 * Check that a process has enough memory to allocate a new virtual
1889 * mapping. 0 means there is enough memory for the allocation to
1890 * succeed and -ENOMEM implies there is not.
1891 *
1892 * We currently support three overcommit policies, which are set via the
1893 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1894 *
1895 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1896 * Additional code 2002 Jul 20 by Robert Love.
1897 *
1898 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1899 *
1900 * Note this is a helper function intended to be used by LSMs which
1901 * wish to use this logic.
1902 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001903int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001905 unsigned long free, allowed, reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 vm_acct_memory(pages);
1908
1909 /*
1910 * Sometimes we want to use more memory than we have
1911 */
1912 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1913 return 0;
1914
1915 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -07001916 free = global_page_state(NR_FREE_PAGES);
1917 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Dmitry Finkc15bef32011-07-25 17:12:19 -07001919 /*
1920 * shmem pages shouldn't be counted as free in this
1921 * case, they can't be purged, only swapped out, and
1922 * that won't affect the overall amount of available
1923 * memory in the system.
1924 */
1925 free -= global_page_state(NR_SHMEM);
1926
Shaohua Liec8acf22013-02-22 16:34:38 -08001927 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 /*
1930 * Any slabs which are created with the
1931 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1932 * which are reclaimable, under pressure. The dentry
1933 * cache and most inode caches should fall into this
1934 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001935 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -07001938 * Leave reserved pages. The pages are not for anonymous pages.
1939 */
1940 if (free <= totalreserve_pages)
1941 goto error;
1942 else
1943 free -= totalreserve_pages;
1944
1945 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001946 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
1948 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001949 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 if (free > pages)
1952 return 0;
1953
Hideo AOKId5ddc792006-04-10 22:53:01 -07001954 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
1957 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1958 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001959 * Reserve some 3% for root
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
1961 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07001962 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 allowed += total_swap_pages;
1964
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07001965 /*
1966 * Don't let a single process grow so big a user can't recover
1967 */
1968 if (mm) {
1969 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
1970 allowed -= min(mm->total_vm / 32, reserve);
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001973 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001975
Hideo AOKId5ddc792006-04-10 22:53:01 -07001976error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 vm_unacct_memory(pages);
1978
1979 return -ENOMEM;
1980}
1981
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001982int in_gate_area_no_mm(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 return 0;
1985}
David Howellsb0e15192006-01-06 00:11:42 -08001986
Nick Piggind0217ac2007-07-19 01:47:03 -07001987int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001988{
1989 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001990 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001991}
Paul Mundtb5073172007-07-21 04:37:25 -07001992EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001993
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001994int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1995 unsigned long size, pgoff_t pgoff)
1996{
1997 BUG();
1998 return 0;
1999}
2000EXPORT_SYMBOL(generic_file_remap_pages);
2001
Mike Frysingerf55f1992011-03-29 14:05:12 +01002002static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
2003 unsigned long addr, void *buf, int len, int write)
David Howells0ec76a12006-09-27 01:50:15 -07002004{
David Howells0ec76a12006-09-27 01:50:15 -07002005 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07002006
2007 down_read(&mm->mmap_sem);
2008
2009 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07002010 vma = find_vma(mm, addr);
2011 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07002012 /* don't overrun this mapping */
2013 if (addr + len >= vma->vm_end)
2014 len = vma->vm_end - addr;
2015
2016 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07002017 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00002018 copy_to_user_page(vma, NULL, addr,
2019 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07002020 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00002021 copy_from_user_page(vma, NULL, addr,
2022 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07002023 else
2024 len = 0;
2025 } else {
2026 len = 0;
2027 }
2028
2029 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01002030
2031 return len;
2032}
2033
2034/**
2035 * @access_remote_vm - access another process' address space
2036 * @mm: the mm_struct of the target address space
2037 * @addr: start address to access
2038 * @buf: source or destination buffer
2039 * @len: number of bytes to transfer
2040 * @write: whether the access is a write
2041 *
2042 * The caller must hold a reference on @mm.
2043 */
2044int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2045 void *buf, int len, int write)
2046{
2047 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2048}
2049
2050/*
2051 * Access another process' address space.
2052 * - source/target buffer must be kernel space
2053 */
2054int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2055{
2056 struct mm_struct *mm;
2057
2058 if (addr + len < addr)
2059 return 0;
2060
2061 mm = get_task_mm(tsk);
2062 if (!mm)
2063 return 0;
2064
2065 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2066
David Howells0ec76a12006-09-27 01:50:15 -07002067 mmput(mm);
2068 return len;
2069}
David Howells7e660872010-01-15 17:01:39 -08002070
2071/**
2072 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2073 * @inode: The inode to check
2074 * @size: The current filesize of the inode
2075 * @newsize: The proposed filesize of the inode
2076 *
2077 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2078 * make sure that that any outstanding VMAs aren't broken and then shrink the
2079 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2080 * automatically grant mappings that are too large.
2081 */
2082int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2083 size_t newsize)
2084{
2085 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08002086 struct vm_region *region;
2087 pgoff_t low, high;
2088 size_t r_size, r_top;
2089
2090 low = newsize >> PAGE_SHIFT;
2091 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2092
2093 down_write(&nommu_region_sem);
David Howells918e5562012-02-23 13:50:35 +00002094 mutex_lock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002095
2096 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002097 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08002098 /* found one - only interested if it's shared out of the page
2099 * cache */
2100 if (vma->vm_flags & VM_SHARED) {
David Howells918e5562012-02-23 13:50:35 +00002101 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002102 up_write(&nommu_region_sem);
2103 return -ETXTBSY; /* not quite true, but near enough */
2104 }
2105 }
2106
2107 /* reduce any regions that overlap the dead zone - if in existence,
2108 * these will be pointed to by VMAs that don't overlap the dead zone
2109 *
2110 * we don't check for any regions that start beyond the EOF as there
2111 * shouldn't be any
2112 */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002113 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2114 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08002115 if (!(vma->vm_flags & VM_SHARED))
2116 continue;
2117
2118 region = vma->vm_region;
2119 r_size = region->vm_top - region->vm_start;
2120 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2121
2122 if (r_top > newsize) {
2123 region->vm_top -= r_top - newsize;
2124 if (region->vm_end > region->vm_top)
2125 region->vm_end = region->vm_top;
2126 }
2127 }
2128
David Howells918e5562012-02-23 13:50:35 +00002129 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002130 up_write(&nommu_region_sem);
2131 return 0;
2132}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07002133
2134/*
2135 * Initialise sysctl_user_reserve_kbytes.
2136 *
2137 * This is intended to prevent a user from starting a single memory hogging
2138 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
2139 * mode.
2140 *
2141 * The default value is min(3% of free memory, 128MB)
2142 * 128MB is enough to recover with sshd/login, bash, and top/kill.
2143 */
2144static int __meminit init_user_reserve(void)
2145{
2146 unsigned long free_kbytes;
2147
2148 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2149
2150 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
2151 return 0;
2152}
2153module_init(init_user_reserve)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07002154
2155/*
2156 * Initialise sysctl_admin_reserve_kbytes.
2157 *
2158 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
2159 * to log in and kill a memory hogging process.
2160 *
2161 * Systems with more than 256MB will reserve 8MB, enough to recover
2162 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
2163 * only reserve 3% of free pages by default.
2164 */
2165static int __meminit init_admin_reserve(void)
2166{
2167 unsigned long free_kbytes;
2168
2169 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
2170
2171 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
2172 return 0;
2173}
2174module_init(init_admin_reserve)