blob: 7296a5a280e7d798b10718bf1cd9998fa0f640a3 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066int heap_stack_gap = 0;
67
David Howells33e5d7692009-04-02 16:56:32 -070068atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000069
K. Y. Srinivasan997071b2012-11-15 14:34:42 -080070/*
71 * The global memory commitment made in the system can be a metric
72 * that can be used to drive ballooning decisions when Linux is hosted
73 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
74 * balancing memory across competing virtual machines that are hosted.
75 * Several metrics drive this policy engine including the guest reported
76 * memory commitment.
77 */
78unsigned long vm_memory_committed(void)
79{
80 return percpu_counter_read_positive(&vm_committed_as);
81}
82
83EXPORT_SYMBOL_GPL(vm_memory_committed);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070086EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
David Howells8feae132009-01-08 12:04:47 +000088/* list of mapped, potentially shareable regions */
89static struct kmem_cache *vm_region_jar;
90struct rb_root nommu_region_tree = RB_ROOT;
91DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040093const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * Return the total memory allocated for this pointer, not
98 * just what the caller asked for.
99 *
100 * Doesn't have to be accurate, i.e. may have races.
101 */
102unsigned int kobjsize(const void *objp)
103{
104 struct page *page;
105
Michael Hennerich4016a132008-04-28 02:13:38 -0700106 /*
107 * If the object we have should not have ksize performed on it,
108 * return size of 0
109 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900110 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700111 return 0;
112
113 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700114
115 /*
116 * If the allocator sets PageSlab, we know the pointer came from
117 * kmalloc().
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (PageSlab(page))
120 return ksize(objp);
121
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700122 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000123 * If it's not a compound page, see if we have a matching VMA
124 * region. This test is intentionally done in reverse order,
125 * so if there's no VMA, we still fall through and hand back
126 * PAGE_SIZE for 0-order pages.
127 */
128 if (!PageCompound(page)) {
129 struct vm_area_struct *vma;
130
131 vma = find_vma(current->mm, (unsigned long)objp);
132 if (vma)
133 return vma->vm_end - vma->vm_start;
134 }
135
136 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700137 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900138 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700139 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900140 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Nick Pigginb291f002008-10-18 20:26:44 -0700143int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Hugh Dickins4266c972009-09-23 17:05:53 +0100144 unsigned long start, int nr_pages, unsigned int foll_flags,
Michel Lespinasse53a77062011-01-13 15:46:14 -0800145 struct page **pages, struct vm_area_struct **vmas,
146 int *retry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700148 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700149 unsigned long vm_flags;
150 int i;
151
152 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700153 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700154 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700155 vm_flags = (foll_flags & FOLL_WRITE) ?
156 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
157 vm_flags &= (foll_flags & FOLL_FORCE) ?
158 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Peter Zijlstra9d737772009-06-25 11:58:55 +0200160 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000161 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700162 if (!vma)
163 goto finish_or_fault;
164
165 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700166 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
167 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700168 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (pages) {
171 pages[i] = virt_to_page(start);
172 if (pages[i])
173 page_cache_get(pages[i]);
174 }
175 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700176 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000177 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
David Howells7b4d5b82006-09-27 01:50:18 -0700179
180 return i;
181
182finish_or_fault:
183 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
Nick Pigginb291f002008-10-18 20:26:44 -0700185
Nick Pigginb291f002008-10-18 20:26:44 -0700186/*
187 * get a list of pages in an address range belonging to the specified process
188 * and indicate the VMA that covers each page
189 * - this is potentially dodgy as we may end incrementing the page count of a
190 * slab page or a secondary page from a compound page
191 * - don't permit access to VMAs that don't support it, such as I/O mappings
192 */
193int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200194 unsigned long start, int nr_pages, int write, int force,
Nick Pigginb291f002008-10-18 20:26:44 -0700195 struct page **pages, struct vm_area_struct **vmas)
196{
197 int flags = 0;
198
199 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700200 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700201 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700202 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700203
Michel Lespinasse53a77062011-01-13 15:46:14 -0800204 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
205 NULL);
Nick Pigginb291f002008-10-18 20:26:44 -0700206}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000207EXPORT_SYMBOL(get_user_pages);
208
Paul Mundtdfc2f912009-06-26 04:31:57 +0900209/**
210 * follow_pfn - look up PFN at a user virtual address
211 * @vma: memory mapping
212 * @address: user virtual address
213 * @pfn: location to store found PFN
214 *
215 * Only IO mappings and raw PFN mappings are allowed.
216 *
217 * Returns zero and the pfn at @pfn on success, -ve otherwise.
218 */
219int follow_pfn(struct vm_area_struct *vma, unsigned long address,
220 unsigned long *pfn)
221{
222 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
223 return -EINVAL;
224
225 *pfn = address >> PAGE_SHIFT;
226 return 0;
227}
228EXPORT_SYMBOL(follow_pfn);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230DEFINE_RWLOCK(vmlist_lock);
231struct vm_struct *vmlist;
232
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800233void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 kfree(addr);
236}
Paul Mundtb5073172007-07-21 04:37:25 -0700237EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Al Virodd0fc662005-10-07 07:46:04 +0100239void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200242 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
243 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Nick Piggin84097512006-03-22 00:08:34 -0800245 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Paul Mundtb5073172007-07-21 04:37:25 -0700247EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Paul Mundtf905bc42008-02-04 22:29:59 -0800249void *vmalloc_user(unsigned long size)
250{
251 void *ret;
252
253 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
254 PAGE_KERNEL);
255 if (ret) {
256 struct vm_area_struct *vma;
257
258 down_write(&current->mm->mmap_sem);
259 vma = find_vma(current->mm, (unsigned long)ret);
260 if (vma)
261 vma->vm_flags |= VM_USERMAP;
262 up_write(&current->mm->mmap_sem);
263 }
264
265 return ret;
266}
267EXPORT_SYMBOL(vmalloc_user);
268
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800269struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 return virt_to_page(addr);
272}
Paul Mundtb5073172007-07-21 04:37:25 -0700273EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800275unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 return page_to_pfn(virt_to_page(addr));
278}
Paul Mundtb5073172007-07-21 04:37:25 -0700279EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281long vread(char *buf, char *addr, unsigned long count)
282{
283 memcpy(buf, addr, count);
284 return count;
285}
286
287long vwrite(char *buf, char *addr, unsigned long count)
288{
289 /* Don't allow overflow */
290 if ((unsigned long) addr + count < count)
291 count = -(unsigned long) addr;
292
293 memcpy(addr, buf, count);
294 return(count);
295}
296
297/*
298 * vmalloc - allocate virtually continguos memory
299 *
300 * @size: allocation size
301 *
302 * Allocate enough pages to cover @size from the page level
303 * allocator and map them into continguos kernel virtual space.
304 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200305 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * use __vmalloc() instead.
307 */
308void *vmalloc(unsigned long size)
309{
310 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
311}
Andrew Mortonf6138882006-02-28 16:59:18 -0800312EXPORT_SYMBOL(vmalloc);
313
Dave Younge1ca7782010-10-26 14:22:06 -0700314/*
315 * vzalloc - allocate virtually continguos memory with zero fill
316 *
317 * @size: allocation size
318 *
319 * Allocate enough pages to cover @size from the page level
320 * allocator and map them into continguos kernel virtual space.
321 * The memory allocated is set to zero.
322 *
323 * For tight control over page level allocator and protection flags
324 * use __vmalloc() instead.
325 */
326void *vzalloc(unsigned long size)
327{
328 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
329 PAGE_KERNEL);
330}
331EXPORT_SYMBOL(vzalloc);
332
333/**
334 * vmalloc_node - allocate memory on a specific node
335 * @size: allocation size
336 * @node: numa node
337 *
338 * Allocate enough pages to cover @size from the page level
339 * allocator and map them into contiguous kernel virtual space.
340 *
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
343 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800344void *vmalloc_node(unsigned long size, int node)
345{
346 return vmalloc(size);
347}
Paul Mundt9a14f652010-12-24 11:50:34 +0900348EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700349
350/**
351 * vzalloc_node - allocate memory on a specific node with zero fill
352 * @size: allocation size
353 * @node: numa node
354 *
355 * Allocate enough pages to cover @size from the page level
356 * allocator and map them into contiguous kernel virtual space.
357 * The memory allocated is set to zero.
358 *
359 * For tight control over page level allocator and protection flags
360 * use __vmalloc() instead.
361 */
362void *vzalloc_node(unsigned long size, int node)
363{
364 return vzalloc(size);
365}
366EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Paul Mundt1af446e2008-08-04 16:01:47 +0900368#ifndef PAGE_KERNEL_EXEC
369# define PAGE_KERNEL_EXEC PAGE_KERNEL
370#endif
371
372/**
373 * vmalloc_exec - allocate virtually contiguous, executable memory
374 * @size: allocation size
375 *
376 * Kernel-internal function to allocate enough pages to cover @size
377 * the page level allocator and map them into contiguous and
378 * executable kernel virtual space.
379 *
380 * For tight control over page level allocator and protection flags
381 * use __vmalloc() instead.
382 */
383
384void *vmalloc_exec(unsigned long size)
385{
386 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
387}
388
Paul Mundtb5073172007-07-21 04:37:25 -0700389/**
390 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * @size: allocation size
392 *
393 * Allocate enough 32bit PA addressable pages to cover @size from the
394 * page level allocator and map them into continguos kernel virtual space.
395 */
396void *vmalloc_32(unsigned long size)
397{
398 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
399}
Paul Mundtb5073172007-07-21 04:37:25 -0700400EXPORT_SYMBOL(vmalloc_32);
401
402/**
403 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
404 * @size: allocation size
405 *
406 * The resulting memory area is 32bit addressable and zeroed so it can be
407 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800408 *
409 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
410 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700411 */
412void *vmalloc_32_user(unsigned long size)
413{
Paul Mundtf905bc42008-02-04 22:29:59 -0800414 /*
415 * We'll have to sort out the ZONE_DMA bits for 64-bit,
416 * but for now this can simply use vmalloc_user() directly.
417 */
418 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700419}
420EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
423{
424 BUG();
425 return NULL;
426}
Paul Mundtb5073172007-07-21 04:37:25 -0700427EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800429void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 BUG();
432}
Paul Mundtb5073172007-07-21 04:37:25 -0700433EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Paul Mundteb6434d2009-01-21 17:45:47 +0900435void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
436{
437 BUG();
438 return NULL;
439}
440EXPORT_SYMBOL(vm_map_ram);
441
442void vm_unmap_ram(const void *mem, unsigned int count)
443{
444 BUG();
445}
446EXPORT_SYMBOL(vm_unmap_ram);
447
448void vm_unmap_aliases(void)
449{
450}
451EXPORT_SYMBOL_GPL(vm_unmap_aliases);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700454 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
455 * have one.
456 */
457void __attribute__((weak)) vmalloc_sync_all(void)
458{
459}
460
Paul Mundt29c185e2010-12-24 12:08:30 +0900461/**
462 * alloc_vm_area - allocate a range of kernel address space
463 * @size: size of the area
464 *
465 * Returns: NULL on failure, vm_struct on success
466 *
467 * This function reserves a range of kernel address space, and
468 * allocates pagetables to map that range. No actual mappings
469 * are created. If the kernel address space is not shared
470 * between processes, it syncs the pagetable across all
471 * processes.
472 */
David Vrabelcd129092011-09-29 16:53:32 +0100473struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Paul Mundt29c185e2010-12-24 12:08:30 +0900474{
475 BUG();
476 return NULL;
477}
478EXPORT_SYMBOL_GPL(alloc_vm_area);
479
480void free_vm_area(struct vm_struct *area)
481{
482 BUG();
483}
484EXPORT_SYMBOL_GPL(free_vm_area);
485
Paul Mundtb5073172007-07-21 04:37:25 -0700486int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
487 struct page *page)
488{
489 return -EINVAL;
490}
491EXPORT_SYMBOL(vm_insert_page);
492
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700493/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * sys_brk() for the most part doesn't need the global kernel
495 * lock, except when an application is doing something nasty
496 * like trying to un-brk an area that has already been mapped
497 * to a regular file. in this case, the unmapping will need
498 * to invoke file system routines that need the global lock.
499 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100500SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct mm_struct *mm = current->mm;
503
504 if (brk < mm->start_brk || brk > mm->context.end_brk)
505 return mm->brk;
506
507 if (mm->brk == brk)
508 return mm->brk;
509
510 /*
511 * Always allow shrinking brk
512 */
513 if (brk <= mm->brk) {
514 mm->brk = brk;
515 return brk;
516 }
517
518 /*
519 * Ok, looks good - let it rip.
520 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000521 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return mm->brk = brk;
523}
524
David Howells8feae132009-01-08 12:04:47 +0000525/*
526 * initialise the VMA and region record slabs
527 */
528void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700530 int ret;
531
532 ret = percpu_counter_init(&vm_committed_as, 0);
533 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700534 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David Howells8feae132009-01-08 12:04:47 +0000537/*
538 * validate the region tree
539 * - the caller must hold the region lock
540 */
541#ifdef CONFIG_DEBUG_NOMMU_REGIONS
542static noinline void validate_nommu_regions(void)
543{
544 struct vm_region *region, *last;
545 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
David Howells8feae132009-01-08 12:04:47 +0000547 lastp = rb_first(&nommu_region_tree);
548 if (!lastp)
549 return;
550
551 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700552 BUG_ON(unlikely(last->vm_end <= last->vm_start));
553 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000554
555 while ((p = rb_next(lastp))) {
556 region = rb_entry(p, struct vm_region, vm_rb);
557 last = rb_entry(lastp, struct vm_region, vm_rb);
558
David Howells33e5d7692009-04-02 16:56:32 -0700559 BUG_ON(unlikely(region->vm_end <= region->vm_start));
560 BUG_ON(unlikely(region->vm_top < region->vm_end));
561 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000562
563 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565}
David Howells8feae132009-01-08 12:04:47 +0000566#else
David Howells33e5d7692009-04-02 16:56:32 -0700567static void validate_nommu_regions(void)
568{
569}
David Howells8feae132009-01-08 12:04:47 +0000570#endif
571
572/*
573 * add a region into the global tree
574 */
575static void add_nommu_region(struct vm_region *region)
576{
577 struct vm_region *pregion;
578 struct rb_node **p, *parent;
579
580 validate_nommu_regions();
581
David Howells8feae132009-01-08 12:04:47 +0000582 parent = NULL;
583 p = &nommu_region_tree.rb_node;
584 while (*p) {
585 parent = *p;
586 pregion = rb_entry(parent, struct vm_region, vm_rb);
587 if (region->vm_start < pregion->vm_start)
588 p = &(*p)->rb_left;
589 else if (region->vm_start > pregion->vm_start)
590 p = &(*p)->rb_right;
591 else if (pregion == region)
592 return;
593 else
594 BUG();
595 }
596
597 rb_link_node(&region->vm_rb, parent, p);
598 rb_insert_color(&region->vm_rb, &nommu_region_tree);
599
600 validate_nommu_regions();
601}
602
603/*
604 * delete a region from the global tree
605 */
606static void delete_nommu_region(struct vm_region *region)
607{
608 BUG_ON(!nommu_region_tree.rb_node);
609
610 validate_nommu_regions();
611 rb_erase(&region->vm_rb, &nommu_region_tree);
612 validate_nommu_regions();
613}
614
615/*
616 * free a contiguous series of pages
617 */
618static void free_page_series(unsigned long from, unsigned long to)
619{
620 for (; from < to; from += PAGE_SIZE) {
621 struct page *page = virt_to_page(from);
622
623 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700624 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000625 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700626 kdebug("free page %p: refcount not one: %d",
627 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000628 put_page(page);
629 }
630}
631
632/*
633 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700634 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000635 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000636 * will equal vm_start
637 */
638static void __put_nommu_region(struct vm_region *region)
639 __releases(nommu_region_sem)
640{
David Howells1e2ae592010-01-15 17:01:33 -0800641 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000642
643 BUG_ON(!nommu_region_tree.rb_node);
644
David Howells1e2ae592010-01-15 17:01:33 -0800645 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000646 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000647 delete_nommu_region(region);
648 up_write(&nommu_region_sem);
649
650 if (region->vm_file)
651 fput(region->vm_file);
652
653 /* IO memory and memory shared directly out of the pagecache
654 * from ramfs/tmpfs mustn't be released here */
655 if (region->vm_flags & VM_MAPPED_COPY) {
656 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000657 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000658 }
659 kmem_cache_free(vm_region_jar, region);
660 } else {
661 up_write(&nommu_region_sem);
662 }
663}
664
665/*
666 * release a reference to a region
667 */
668static void put_nommu_region(struct vm_region *region)
669{
670 down_write(&nommu_region_sem);
671 __put_nommu_region(region);
672}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David Howells30340972006-09-27 01:50:20 -0700674/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700675 * update protection on a vma
676 */
677static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
678{
679#ifdef CONFIG_MPU
680 struct mm_struct *mm = vma->vm_mm;
681 long start = vma->vm_start & PAGE_MASK;
682 while (start < vma->vm_end) {
683 protect_page(mm, start, flags);
684 start += PAGE_SIZE;
685 }
686 update_protections(mm);
687#endif
688}
689
690/*
David Howells30340972006-09-27 01:50:20 -0700691 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000692 * and tree and add to the address space's page tree also if not an anonymous
693 * page
David Howells30340972006-09-27 01:50:20 -0700694 * - should be called with mm->mmap_sem held writelocked
695 */
David Howells8feae132009-01-08 12:04:47 +0000696static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700697{
Namhyung Kim6038def2011-05-24 17:11:22 -0700698 struct vm_area_struct *pvma, *prev;
David Howells8feae132009-01-08 12:04:47 +0000699 struct address_space *mapping;
Namhyung Kim6038def2011-05-24 17:11:22 -0700700 struct rb_node **p, *parent, *rb_prev;
David Howells30340972006-09-27 01:50:20 -0700701
David Howells8feae132009-01-08 12:04:47 +0000702 kenter(",%p", vma);
703
704 BUG_ON(!vma->vm_region);
705
706 mm->map_count++;
707 vma->vm_mm = mm;
708
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700709 protect_vma(vma, vma->vm_flags);
710
David Howells8feae132009-01-08 12:04:47 +0000711 /* add the VMA to the mapping */
712 if (vma->vm_file) {
713 mapping = vma->vm_file->f_mapping;
714
David Howells918e5562012-02-23 13:50:35 +0000715 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000716 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700717 vma_interval_tree_insert(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000718 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000719 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000720 }
721
722 /* add the VMA to the tree */
Namhyung Kim6038def2011-05-24 17:11:22 -0700723 parent = rb_prev = NULL;
David Howells8feae132009-01-08 12:04:47 +0000724 p = &mm->mm_rb.rb_node;
725 while (*p) {
726 parent = *p;
727 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
728
729 /* sort by: start addr, end addr, VMA struct addr in that order
730 * (the latter is necessary as we may get identical VMAs) */
731 if (vma->vm_start < pvma->vm_start)
732 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700733 else if (vma->vm_start > pvma->vm_start) {
734 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000735 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700736 } else if (vma->vm_end < pvma->vm_end)
David Howells8feae132009-01-08 12:04:47 +0000737 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700738 else if (vma->vm_end > pvma->vm_end) {
739 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000740 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700741 } else if (vma < pvma)
David Howells8feae132009-01-08 12:04:47 +0000742 p = &(*p)->rb_left;
Namhyung Kim6038def2011-05-24 17:11:22 -0700743 else if (vma > pvma) {
744 rb_prev = parent;
David Howells8feae132009-01-08 12:04:47 +0000745 p = &(*p)->rb_right;
Namhyung Kim6038def2011-05-24 17:11:22 -0700746 } else
David Howells8feae132009-01-08 12:04:47 +0000747 BUG();
748 }
749
750 rb_link_node(&vma->vm_rb, parent, p);
751 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
752
753 /* add VMA to the VMA list also */
Namhyung Kim6038def2011-05-24 17:11:22 -0700754 prev = NULL;
755 if (rb_prev)
756 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
David Howells30340972006-09-27 01:50:20 -0700757
Namhyung Kim6038def2011-05-24 17:11:22 -0700758 __vma_link_list(mm, vma, prev, parent);
David Howells8feae132009-01-08 12:04:47 +0000759}
760
761/*
762 * delete a VMA from its owning mm_struct and address space
763 */
764static void delete_vma_from_mm(struct vm_area_struct *vma)
765{
David Howells8feae132009-01-08 12:04:47 +0000766 struct address_space *mapping;
767 struct mm_struct *mm = vma->vm_mm;
768
769 kenter("%p", vma);
770
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700771 protect_vma(vma, 0);
772
David Howells8feae132009-01-08 12:04:47 +0000773 mm->map_count--;
774 if (mm->mmap_cache == vma)
775 mm->mmap_cache = NULL;
776
777 /* remove the VMA from the mapping */
778 if (vma->vm_file) {
779 mapping = vma->vm_file->f_mapping;
780
David Howells918e5562012-02-23 13:50:35 +0000781 mutex_lock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000782 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700783 vma_interval_tree_remove(vma, &mapping->i_mmap);
David Howells8feae132009-01-08 12:04:47 +0000784 flush_dcache_mmap_unlock(mapping);
David Howells918e5562012-02-23 13:50:35 +0000785 mutex_unlock(&mapping->i_mmap_mutex);
David Howells8feae132009-01-08 12:04:47 +0000786 }
787
788 /* remove from the MM's tree and list */
789 rb_erase(&vma->vm_rb, &mm->mm_rb);
Namhyung Kimb951bf22011-05-24 17:11:23 -0700790
791 if (vma->vm_prev)
792 vma->vm_prev->vm_next = vma->vm_next;
793 else
794 mm->mmap = vma->vm_next;
795
796 if (vma->vm_next)
797 vma->vm_next->vm_prev = vma->vm_prev;
David Howells8feae132009-01-08 12:04:47 +0000798}
799
800/*
801 * destroy a VMA record
802 */
803static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
804{
805 kenter("%p", vma);
806 if (vma->vm_ops && vma->vm_ops->close)
807 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700808 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +0000809 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +0000810 put_nommu_region(vma->vm_region);
811 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700812}
813
814/*
815 * look up the first VMA in which addr resides, NULL if none
816 * - should be called with mm->mmap_sem at least held readlocked
817 */
818struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
819{
David Howells8feae132009-01-08 12:04:47 +0000820 struct vm_area_struct *vma;
David Howells30340972006-09-27 01:50:20 -0700821
David Howells8feae132009-01-08 12:04:47 +0000822 /* check the cache first */
823 vma = mm->mmap_cache;
824 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
825 return vma;
826
Namhyung Kime922c4c2011-05-24 17:11:24 -0700827 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000828 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700829 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000830 if (vma->vm_start > addr)
831 return NULL;
832 if (vma->vm_end > addr) {
833 mm->mmap_cache = vma;
834 return vma;
835 }
David Howells30340972006-09-27 01:50:20 -0700836 }
837
David Howells30340972006-09-27 01:50:20 -0700838 return NULL;
839}
840EXPORT_SYMBOL(find_vma);
841
842/*
David Howells930e6522006-09-27 01:50:22 -0700843 * find a VMA
844 * - we don't extend stack VMAs under NOMMU conditions
845 */
846struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
847{
David Howells7561e8c2010-03-25 16:48:38 +0000848 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700849}
850
David Howells8feae132009-01-08 12:04:47 +0000851/*
852 * expand a stack to a given address
853 * - not supported under NOMMU conditions
854 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700855int expand_stack(struct vm_area_struct *vma, unsigned long address)
856{
857 return -ENOMEM;
858}
859
David Howells930e6522006-09-27 01:50:22 -0700860/*
David Howells6fa5f802006-09-27 01:50:21 -0700861 * look up the first VMA exactly that exactly matches addr
862 * - should be called with mm->mmap_sem at least held readlocked
863 */
David Howells8feae132009-01-08 12:04:47 +0000864static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
865 unsigned long addr,
866 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000869 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
David Howells8feae132009-01-08 12:04:47 +0000871 /* check the cache first */
872 vma = mm->mmap_cache;
873 if (vma && vma->vm_start == addr && vma->vm_end == end)
874 return vma;
875
Namhyung Kime922c4c2011-05-24 17:11:24 -0700876 /* trawl the list (there may be multiple mappings in which addr
David Howells8feae132009-01-08 12:04:47 +0000877 * resides) */
Namhyung Kime922c4c2011-05-24 17:11:24 -0700878 for (vma = mm->mmap; vma; vma = vma->vm_next) {
David Howells8feae132009-01-08 12:04:47 +0000879 if (vma->vm_start < addr)
880 continue;
881 if (vma->vm_start > addr)
882 return NULL;
883 if (vma->vm_end == end) {
884 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return vma;
David Howells8feae132009-01-08 12:04:47 +0000886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
889 return NULL;
890}
891
David Howells30340972006-09-27 01:50:20 -0700892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * determine whether a mapping should be permitted and, if so, what sort of
894 * mapping we're capable of supporting
895 */
896static int validate_mmap_request(struct file *file,
897 unsigned long addr,
898 unsigned long len,
899 unsigned long prot,
900 unsigned long flags,
901 unsigned long pgoff,
902 unsigned long *_capabilities)
903{
David Howells8feae132009-01-08 12:04:47 +0000904 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int ret;
906
907 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100908 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 printk(KERN_DEBUG
910 "%d: Can't do fixed-address/overlay mmap of RAM\n",
911 current->pid);
912 return -EINVAL;
913 }
914
915 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
916 (flags & MAP_TYPE) != MAP_SHARED)
917 return -EINVAL;
918
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000919 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EINVAL;
921
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000922 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000923 rlen = PAGE_ALIGN(len);
924 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000925 return -ENOMEM;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000928 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000929 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (file) {
932 /* validate file mapping requests */
933 struct address_space *mapping;
934
935 /* files must support mmap */
936 if (!file->f_op || !file->f_op->mmap)
937 return -ENODEV;
938
939 /* work out if what we've got could possibly be shared
940 * - we support chardevs that provide their own "memory"
941 * - we support files/blockdevs that are memory backed
942 */
943 mapping = file->f_mapping;
944 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800945 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 capabilities = 0;
948 if (mapping && mapping->backing_dev_info)
949 capabilities = mapping->backing_dev_info->capabilities;
950
951 if (!capabilities) {
952 /* no explicit capabilities set, so assume some
953 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800954 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 case S_IFREG:
956 case S_IFBLK:
957 capabilities = BDI_CAP_MAP_COPY;
958 break;
959
960 case S_IFCHR:
961 capabilities =
962 BDI_CAP_MAP_DIRECT |
963 BDI_CAP_READ_MAP |
964 BDI_CAP_WRITE_MAP;
965 break;
966
967 default:
968 return -EINVAL;
969 }
970 }
971
972 /* eliminate any capabilities that we can't support on this
973 * device */
974 if (!file->f_op->get_unmapped_area)
975 capabilities &= ~BDI_CAP_MAP_DIRECT;
976 if (!file->f_op->read)
977 capabilities &= ~BDI_CAP_MAP_COPY;
978
Graff Yang28d7a6a2009-08-18 14:11:17 -0700979 /* The file shall have been opened with read permission. */
980 if (!(file->f_mode & FMODE_READ))
981 return -EACCES;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (flags & MAP_SHARED) {
984 /* do checks for writing, appending and locking */
985 if ((prot & PROT_WRITE) &&
986 !(file->f_mode & FMODE_WRITE))
987 return -EACCES;
988
Josef Sipeke9536ae2006-12-08 02:37:21 -0800989 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 (file->f_mode & FMODE_WRITE))
991 return -EACCES;
992
Josef Sipeke9536ae2006-12-08 02:37:21 -0800993 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -EAGAIN;
995
996 if (!(capabilities & BDI_CAP_MAP_DIRECT))
997 return -ENODEV;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* we mustn't privatise shared mappings */
1000 capabilities &= ~BDI_CAP_MAP_COPY;
1001 }
1002 else {
1003 /* we're going to read the file into private memory we
1004 * allocate */
1005 if (!(capabilities & BDI_CAP_MAP_COPY))
1006 return -ENODEV;
1007
1008 /* we don't permit a private writable mapping to be
1009 * shared with the backing device */
1010 if (prot & PROT_WRITE)
1011 capabilities &= ~BDI_CAP_MAP_DIRECT;
1012 }
1013
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001014 if (capabilities & BDI_CAP_MAP_DIRECT) {
1015 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1016 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1017 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1018 ) {
1019 capabilities &= ~BDI_CAP_MAP_DIRECT;
1020 if (flags & MAP_SHARED) {
1021 printk(KERN_WARNING
1022 "MAP_SHARED not completely supported on !MMU\n");
1023 return -EINVAL;
1024 }
1025 }
1026 }
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* handle executable mappings and implied executable
1029 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -08001030 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (prot & PROT_EXEC)
1032 return -EPERM;
1033 }
1034 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1035 /* handle implication of PROT_EXEC by PROT_READ */
1036 if (current->personality & READ_IMPLIES_EXEC) {
1037 if (capabilities & BDI_CAP_EXEC_MAP)
1038 prot |= PROT_EXEC;
1039 }
1040 }
1041 else if ((prot & PROT_READ) &&
1042 (prot & PROT_EXEC) &&
1043 !(capabilities & BDI_CAP_EXEC_MAP)
1044 ) {
1045 /* backing file is not executable, try to copy */
1046 capabilities &= ~BDI_CAP_MAP_DIRECT;
1047 }
1048 }
1049 else {
1050 /* anonymous mappings are always memory backed and can be
1051 * privately mapped
1052 */
1053 capabilities = BDI_CAP_MAP_COPY;
1054
1055 /* handle PROT_EXEC implication by PROT_READ */
1056 if ((prot & PROT_READ) &&
1057 (current->personality & READ_IMPLIES_EXEC))
1058 prot |= PROT_EXEC;
1059 }
1060
1061 /* allow the security API to have its say */
Al Viroe5467852012-05-30 13:30:51 -04001062 ret = security_mmap_addr(addr);
1063 if (ret < 0)
1064 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* looks okay */
1067 *_capabilities = capabilities;
1068 return 0;
1069}
1070
1071/*
1072 * we've determined that we can make the mapping, now translate what we
1073 * now know into VMA flags
1074 */
1075static unsigned long determine_vm_flags(struct file *file,
1076 unsigned long prot,
1077 unsigned long flags,
1078 unsigned long capabilities)
1079{
1080 unsigned long vm_flags;
1081
1082 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* vm_flags |= mm->def_flags; */
1084
1085 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1086 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001087 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (file && !(prot & PROT_WRITE))
1089 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001090 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* overlay a shareable mapping on the backing device or inode
1092 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1093 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001094 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001096 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 /* refuse to let anyone share private mappings with this process if
1100 * it's being traced - otherwise breakpoints set in it may interfere
1101 * with another untraced process
1102 */
Tejun Heoa288eec2011-06-17 16:50:37 +02001103 if ((flags & MAP_PRIVATE) && current->ptrace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 vm_flags &= ~VM_MAYSHARE;
1105
1106 return vm_flags;
1107}
1108
1109/*
David Howells8feae132009-01-08 12:04:47 +00001110 * set up a shared mapping on a file (the driver or filesystem provides and
1111 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 */
David Howells8feae132009-01-08 12:04:47 +00001113static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 int ret;
1116
1117 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001118 if (ret == 0) {
1119 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001120 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (ret != -ENOSYS)
1123 return ret;
1124
David Howells3fa30462010-03-23 13:35:21 -07001125 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1126 * opposed to tried but failed) so we can only give a suitable error as
1127 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -ENODEV;
1129}
1130
1131/*
1132 * set up a private mapping or an anonymous shared mapping
1133 */
David Howells8feae132009-01-08 12:04:47 +00001134static int do_mmap_private(struct vm_area_struct *vma,
1135 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001136 unsigned long len,
1137 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
David Howells8feae132009-01-08 12:04:47 +00001139 struct page *pages;
Bob Liuf67d9b12011-05-24 17:12:56 -07001140 unsigned long total, point, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 void *base;
David Howells8feae132009-01-08 12:04:47 +00001142 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /* invoke the file's mapping function so that it can keep track of
1145 * shared mappings on devices or memory
1146 * - VM_MAYSHARE will be set if it may attempt to share
1147 */
David Howells645d83c2009-09-24 15:13:10 +01001148 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001150 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001152 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1153 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001156 if (ret != -ENOSYS)
1157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* getting an ENOSYS error indicates that direct mmap isn't
1160 * possible (as opposed to tried but failed) so we'll try to
1161 * make a private copy of the data and map that instead */
1162 }
1163
David Howells8feae132009-01-08 12:04:47 +00001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* allocate some memory to hold the mapping
1166 * - note that this may not return a page-aligned address if the object
1167 * we're allocating is smaller than a page
1168 */
Bob Liuf67d9b12011-05-24 17:12:56 -07001169 order = get_order(len);
David Howells8feae132009-01-08 12:04:47 +00001170 kdebug("alloc order %d for %lx", order, len);
1171
1172 pages = alloc_pages(GFP_KERNEL, order);
1173 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto enomem;
1175
David Howells8feae132009-01-08 12:04:47 +00001176 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001177 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Bob Liuf67d9b12011-05-24 17:12:56 -07001179 point = len >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001180
1181 /* we allocated a power-of-2 sized page set, so we may want to trim off
1182 * the excess */
1183 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1184 while (total > point) {
1185 order = ilog2(total - point);
1186 n = 1 << order;
1187 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001188 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001189 total -= n;
1190 set_page_refcounted(pages + total);
1191 __free_pages(pages + total, order);
1192 }
David Howells8feae132009-01-08 12:04:47 +00001193 }
1194
David Howells8feae132009-01-08 12:04:47 +00001195 for (point = 1; point < total; point++)
1196 set_page_refcounted(&pages[point]);
1197
1198 base = page_address(pages);
1199 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1200 region->vm_start = (unsigned long) base;
Bob Liuf67d9b12011-05-24 17:12:56 -07001201 region->vm_end = region->vm_start + len;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001202 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001203
1204 vma->vm_start = region->vm_start;
1205 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (vma->vm_file) {
1208 /* read the contents of a file into the copy */
1209 mm_segment_t old_fs;
1210 loff_t fpos;
1211
1212 fpos = vma->vm_pgoff;
1213 fpos <<= PAGE_SHIFT;
1214
1215 old_fs = get_fs();
1216 set_fs(KERNEL_DS);
Bob Liuf67d9b12011-05-24 17:12:56 -07001217 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 set_fs(old_fs);
1219
1220 if (ret < 0)
1221 goto error_free;
1222
1223 /* clear the last little bit */
Bob Liuf67d9b12011-05-24 17:12:56 -07001224 if (ret < len)
1225 memset(base + ret, 0, len - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
1229 return 0;
1230
1231error_free:
Namhyung Kim7223bb42011-05-24 17:11:26 -07001232 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +00001233 region->vm_start = vma->vm_start = 0;
1234 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001235 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return ret;
1237
1238enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001239 printk("Allocation of length %lu from process %d (%s) failed\n",
1240 len, current->pid, current->comm);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001241 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -ENOMEM;
1243}
1244
1245/*
1246 * handle mapping creation for uClinux
1247 */
Al Viroe3fc6292012-05-30 20:08:42 -04001248unsigned long do_mmap_pgoff(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 unsigned long addr,
1250 unsigned long len,
1251 unsigned long prot,
1252 unsigned long flags,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001253 unsigned long pgoff,
1254 bool *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
David Howells8feae132009-01-08 12:04:47 +00001256 struct vm_area_struct *vma;
1257 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001259 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int ret;
1261
David Howells8feae132009-01-08 12:04:47 +00001262 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1263
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001264 *populate = false;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* decide whether we should attempt the mapping, and if so what sort of
1267 * mapping */
1268 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1269 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001270 if (ret < 0) {
1271 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return ret;
David Howells8feae132009-01-08 12:04:47 +00001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
David Howells06aab5a2009-09-24 12:33:48 +01001275 /* we ignore the address hint */
1276 addr = 0;
Bob Liuf67d9b12011-05-24 17:12:56 -07001277 len = PAGE_ALIGN(len);
David Howells06aab5a2009-09-24 12:33:48 +01001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* we've determined that we can make the mapping, now translate what we
1280 * now know into VMA flags */
1281 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1282
David Howells8feae132009-01-08 12:04:47 +00001283 /* we're going to need to record the mapping */
1284 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1285 if (!region)
1286 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
David Howells8feae132009-01-08 12:04:47 +00001288 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1289 if (!vma)
1290 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
David Howells1e2ae592010-01-15 17:01:33 -08001292 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001293 region->vm_flags = vm_flags;
1294 region->vm_pgoff = pgoff;
1295
Rik van Riel5beb4932010-03-05 13:42:07 -08001296 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001297 vma->vm_flags = vm_flags;
1298 vma->vm_pgoff = pgoff;
1299
1300 if (file) {
Al Virocb0942b2012-08-27 14:48:26 -04001301 region->vm_file = get_file(file);
1302 vma->vm_file = get_file(file);
David Howells8feae132009-01-08 12:04:47 +00001303 }
1304
1305 down_write(&nommu_region_sem);
1306
1307 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001309 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * - shared mappings on character devices and memory backed files are
1311 * permitted to overlap inexactly as far as we are concerned for in
1312 * these cases, sharing is handled in the driver or filesystem rather
1313 * than here
1314 */
1315 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001316 struct vm_region *pregion;
1317 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
David Howells8feae132009-01-08 12:04:47 +00001319 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1320 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001321
David Howells8feae132009-01-08 12:04:47 +00001322 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1323 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
David Howells8feae132009-01-08 12:04:47 +00001325 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 continue;
1327
1328 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001329 if (pregion->vm_file->f_path.dentry->d_inode !=
1330 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 continue;
1332
David Howells8feae132009-01-08 12:04:47 +00001333 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 continue;
1335
David Howells8feae132009-01-08 12:04:47 +00001336 rpglen = pregion->vm_end - pregion->vm_start;
1337 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1338 rpgend = pregion->vm_pgoff + rpglen;
1339 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 continue;
1341
David Howells8feae132009-01-08 12:04:47 +00001342 /* handle inexactly overlapping matches between
1343 * mappings */
1344 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1345 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1346 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1348 goto sharing_violation;
1349 continue;
1350 }
1351
David Howells8feae132009-01-08 12:04:47 +00001352 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001353 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001354 vma->vm_region = pregion;
1355 start = pregion->vm_start;
1356 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1357 vma->vm_start = start;
1358 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
David Howells8feae132009-01-08 12:04:47 +00001360 if (pregion->vm_flags & VM_MAPPED_COPY) {
1361 kdebug("share copy");
1362 vma->vm_flags |= VM_MAPPED_COPY;
1363 } else {
1364 kdebug("share mmap");
1365 ret = do_mmap_shared_file(vma);
1366 if (ret < 0) {
1367 vma->vm_region = NULL;
1368 vma->vm_start = 0;
1369 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001370 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001371 pregion = NULL;
1372 goto error_just_free;
1373 }
1374 }
1375 fput(region->vm_file);
1376 kmem_cache_free(vm_region_jar, region);
1377 region = pregion;
1378 result = start;
1379 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* obtain the address at which to make a shared mapping
1383 * - this is the hook for quasi-memory character devices to
1384 * tell us the location of a shared mapping
1385 */
David Howells645d83c2009-09-24 15:13:10 +01001386 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 addr = file->f_op->get_unmapped_area(file, addr, len,
1388 pgoff, flags);
Namhyung Kimbb005a52011-05-24 17:11:27 -07001389 if (IS_ERR_VALUE(addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ret = addr;
Namhyung Kimbb005a52011-05-24 17:11:27 -07001391 if (ret != -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001392 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* the driver refused to tell us where to site
1395 * the mapping so we'll have to attempt to copy
1396 * it */
Namhyung Kimbb005a52011-05-24 17:11:27 -07001397 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001399 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001402 } else {
1403 vma->vm_start = region->vm_start = addr;
1404 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 }
1407 }
1408
David Howells8feae132009-01-08 12:04:47 +00001409 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
David Howells645d83c2009-09-24 15:13:10 +01001411 /* set up the mapping
1412 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001415 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 else
David Howells645d83c2009-09-24 15:13:10 +01001417 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001419 goto error_just_free;
1420 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001421
Jie Zhangea637632009-12-14 18:00:02 -08001422 /* clear anonymous mappings that don't ask for uninitialized data */
1423 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1424 memset((void *)region->vm_start, 0,
1425 region->vm_end - region->vm_start);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001428 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 current->mm->total_vm += len >> PAGE_SHIFT;
1431
David Howells8feae132009-01-08 12:04:47 +00001432share:
1433 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Mike Frysingercfe79c02010-01-06 17:23:23 +00001435 /* we flush the region from the icache only when the first executable
1436 * mapping of it is made */
1437 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1438 flush_icache_range(region->vm_start, region->vm_end);
1439 region->vm_icache_flushed = true;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Mike Frysingercfe79c02010-01-06 17:23:23 +00001442 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
David Howells8feae132009-01-08 12:04:47 +00001444 kleave(" = %lx", result);
1445 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
David Howells8feae132009-01-08 12:04:47 +00001447error_just_free:
1448 up_write(&nommu_region_sem);
1449error:
David Howells89a86402009-10-30 13:13:26 +00001450 if (region->vm_file)
1451 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001452 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001453 if (vma->vm_file)
1454 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001455 kmem_cache_free(vm_area_cachep, vma);
1456 kleave(" = %d", ret);
1457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David Howells8feae132009-01-08 12:04:47 +00001459sharing_violation:
1460 up_write(&nommu_region_sem);
1461 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1462 ret = -EINVAL;
1463 goto error;
1464
1465error_getting_vma:
1466 kmem_cache_free(vm_region_jar, region);
1467 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1468 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001470 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return -ENOMEM;
1472
David Howells8feae132009-01-08 12:04:47 +00001473error_getting_region:
1474 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1475 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 len, current->pid);
David Rientjes7bf02ea2011-05-24 17:11:16 -07001477 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return -ENOMEM;
1479}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001480
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001481SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1482 unsigned long, prot, unsigned long, flags,
1483 unsigned long, fd, unsigned long, pgoff)
1484{
1485 struct file *file = NULL;
1486 unsigned long retval = -EBADF;
1487
Al Viro120a7952010-10-30 02:54:44 -04001488 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001489 if (!(flags & MAP_ANONYMOUS)) {
1490 file = fget(fd);
1491 if (!file)
1492 goto out;
1493 }
1494
1495 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1496
Greg Ungererad1ed292012-06-04 14:29:59 +10001497 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001498
1499 if (file)
1500 fput(file);
1501out:
1502 return retval;
1503}
1504
Christoph Hellwiga4679372010-03-10 15:21:15 -08001505#ifdef __ARCH_WANT_SYS_OLD_MMAP
1506struct mmap_arg_struct {
1507 unsigned long addr;
1508 unsigned long len;
1509 unsigned long prot;
1510 unsigned long flags;
1511 unsigned long fd;
1512 unsigned long offset;
1513};
1514
1515SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1516{
1517 struct mmap_arg_struct a;
1518
1519 if (copy_from_user(&a, arg, sizeof(a)))
1520 return -EFAULT;
1521 if (a.offset & ~PAGE_MASK)
1522 return -EINVAL;
1523
1524 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1525 a.offset >> PAGE_SHIFT);
1526}
1527#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/*
David Howells8feae132009-01-08 12:04:47 +00001530 * split a vma into two pieces at address 'addr', a new vma is allocated either
1531 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
David Howells8feae132009-01-08 12:04:47 +00001533int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1534 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
David Howells8feae132009-01-08 12:04:47 +00001536 struct vm_area_struct *new;
1537 struct vm_region *region;
1538 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
David Howells8feae132009-01-08 12:04:47 +00001540 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
David Howells779c1022010-01-15 17:01:34 -08001542 /* we're only permitted to split anonymous regions (these should have
1543 * only a single usage on the region) */
1544 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001545 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
David Howells8feae132009-01-08 12:04:47 +00001547 if (mm->map_count >= sysctl_max_map_count)
1548 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
David Howells8feae132009-01-08 12:04:47 +00001550 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1551 if (!region)
1552 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
David Howells8feae132009-01-08 12:04:47 +00001554 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1555 if (!new) {
1556 kmem_cache_free(vm_region_jar, region);
1557 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
David Howells8feae132009-01-08 12:04:47 +00001559
1560 /* most fields are the same, copy all, and then fixup */
1561 *new = *vma;
1562 *region = *vma->vm_region;
1563 new->vm_region = region;
1564
1565 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1566
1567 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001568 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001569 } else {
1570 region->vm_start = new->vm_start = addr;
1571 region->vm_pgoff = new->vm_pgoff += npages;
1572 }
1573
1574 if (new->vm_ops && new->vm_ops->open)
1575 new->vm_ops->open(new);
1576
1577 delete_vma_from_mm(vma);
1578 down_write(&nommu_region_sem);
1579 delete_nommu_region(vma->vm_region);
1580 if (new_below) {
1581 vma->vm_region->vm_start = vma->vm_start = addr;
1582 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1583 } else {
1584 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001585 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001586 }
1587 add_nommu_region(vma->vm_region);
1588 add_nommu_region(new->vm_region);
1589 up_write(&nommu_region_sem);
1590 add_vma_to_mm(mm, vma);
1591 add_vma_to_mm(mm, new);
1592 return 0;
1593}
1594
1595/*
1596 * shrink a VMA by removing the specified chunk from either the beginning or
1597 * the end
1598 */
1599static int shrink_vma(struct mm_struct *mm,
1600 struct vm_area_struct *vma,
1601 unsigned long from, unsigned long to)
1602{
1603 struct vm_region *region;
1604
1605 kenter("");
1606
1607 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1608 * and list */
1609 delete_vma_from_mm(vma);
1610 if (from > vma->vm_start)
1611 vma->vm_end = from;
1612 else
1613 vma->vm_start = to;
1614 add_vma_to_mm(mm, vma);
1615
1616 /* cut the backing region down to size */
1617 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001618 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001619
1620 down_write(&nommu_region_sem);
1621 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001622 if (from > region->vm_start) {
1623 to = region->vm_top;
1624 region->vm_top = region->vm_end = from;
1625 } else {
David Howells8feae132009-01-08 12:04:47 +00001626 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001627 }
David Howells8feae132009-01-08 12:04:47 +00001628 add_nommu_region(region);
1629 up_write(&nommu_region_sem);
1630
1631 free_page_series(from, to);
1632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
David Howells30340972006-09-27 01:50:20 -07001635/*
1636 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001637 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1638 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001639 */
David Howells8feae132009-01-08 12:04:47 +00001640int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
David Howells8feae132009-01-08 12:04:47 +00001642 struct vm_area_struct *vma;
Bob Liuf67d9b12011-05-24 17:12:56 -07001643 unsigned long end;
David Howells8feae132009-01-08 12:04:47 +00001644 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
David Howells8feae132009-01-08 12:04:47 +00001646 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Bob Liuf67d9b12011-05-24 17:12:56 -07001648 len = PAGE_ALIGN(len);
David Howells8feae132009-01-08 12:04:47 +00001649 if (len == 0)
1650 return -EINVAL;
1651
Bob Liuf67d9b12011-05-24 17:12:56 -07001652 end = start + len;
1653
David Howells8feae132009-01-08 12:04:47 +00001654 /* find the first potentially overlapping VMA */
1655 vma = find_vma(mm, start);
1656 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001657 static int limit = 0;
1658 if (limit < 5) {
1659 printk(KERN_WARNING
1660 "munmap of memory not mmapped by process %d"
1661 " (%s): 0x%lx-0x%lx\n",
1662 current->pid, current->comm,
1663 start, start + len - 1);
1664 limit++;
1665 }
David Howells8feae132009-01-08 12:04:47 +00001666 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
David Howells8feae132009-01-08 12:04:47 +00001669 /* we're allowed to split an anonymous VMA but not a file-backed one */
1670 if (vma->vm_file) {
1671 do {
1672 if (start > vma->vm_start) {
1673 kleave(" = -EINVAL [miss]");
1674 return -EINVAL;
1675 }
1676 if (end == vma->vm_end)
1677 goto erase_whole_vma;
Namhyung Kimd75a3102011-05-24 17:11:25 -07001678 vma = vma->vm_next;
1679 } while (vma);
David Howells8feae132009-01-08 12:04:47 +00001680 kleave(" = -EINVAL [split file]");
1681 return -EINVAL;
1682 } else {
1683 /* the chunk must be a subset of the VMA found */
1684 if (start == vma->vm_start && end == vma->vm_end)
1685 goto erase_whole_vma;
1686 if (start < vma->vm_start || end > vma->vm_end) {
1687 kleave(" = -EINVAL [superset]");
1688 return -EINVAL;
1689 }
1690 if (start & ~PAGE_MASK) {
1691 kleave(" = -EINVAL [unaligned start]");
1692 return -EINVAL;
1693 }
1694 if (end != vma->vm_end && end & ~PAGE_MASK) {
1695 kleave(" = -EINVAL [unaligned split]");
1696 return -EINVAL;
1697 }
1698 if (start != vma->vm_start && end != vma->vm_end) {
1699 ret = split_vma(mm, vma, start, 1);
1700 if (ret < 0) {
1701 kleave(" = %d [split]", ret);
1702 return ret;
1703 }
1704 }
1705 return shrink_vma(mm, vma, start, end);
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
David Howells8feae132009-01-08 12:04:47 +00001708erase_whole_vma:
1709 delete_vma_from_mm(vma);
1710 delete_vma(mm, vma);
1711 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
1713}
Paul Mundtb5073172007-07-21 04:37:25 -07001714EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Al Virobfce2812012-04-20 21:57:04 -04001716int vm_munmap(unsigned long addr, size_t len)
David Howells30340972006-09-27 01:50:20 -07001717{
Al Virobfce2812012-04-20 21:57:04 -04001718 struct mm_struct *mm = current->mm;
David Howells30340972006-09-27 01:50:20 -07001719 int ret;
David Howells30340972006-09-27 01:50:20 -07001720
1721 down_write(&mm->mmap_sem);
1722 ret = do_munmap(mm, addr, len);
1723 up_write(&mm->mmap_sem);
1724 return ret;
1725}
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001726EXPORT_SYMBOL(vm_munmap);
1727
1728SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1729{
Al Virobfce2812012-04-20 21:57:04 -04001730 return vm_munmap(addr, len);
Linus Torvaldsa46ef992012-04-20 16:20:01 -07001731}
David Howells30340972006-09-27 01:50:20 -07001732
1733/*
David Howells8feae132009-01-08 12:04:47 +00001734 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001735 */
David Howells8feae132009-01-08 12:04:47 +00001736void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
David Howells8feae132009-01-08 12:04:47 +00001738 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
David Howells8feae132009-01-08 12:04:47 +00001740 if (!mm)
1741 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
David Howells8feae132009-01-08 12:04:47 +00001743 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
David Howells8feae132009-01-08 12:04:47 +00001745 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
David Howells8feae132009-01-08 12:04:47 +00001747 while ((vma = mm->mmap)) {
1748 mm->mmap = vma->vm_next;
1749 delete_vma_from_mm(vma);
1750 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001751 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
David Howells8feae132009-01-08 12:04:47 +00001753
1754 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07001757unsigned long vm_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 return -ENOMEM;
1760}
1761
1762/*
David Howells6fa5f802006-09-27 01:50:21 -07001763 * expand (or shrink) an existing mapping, potentially moving it at the same
1764 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 *
David Howells6fa5f802006-09-27 01:50:21 -07001766 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001767 * as long as it stays within the region allocated by do_mmap_private() and the
1768 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 *
David Howells6fa5f802006-09-27 01:50:21 -07001770 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 */
1772unsigned long do_mremap(unsigned long addr,
1773 unsigned long old_len, unsigned long new_len,
1774 unsigned long flags, unsigned long new_addr)
1775{
David Howells6fa5f802006-09-27 01:50:21 -07001776 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 /* insanity checks first */
Bob Liuf67d9b12011-05-24 17:12:56 -07001779 old_len = PAGE_ALIGN(old_len);
1780 new_len = PAGE_ALIGN(new_len);
David Howells8feae132009-01-08 12:04:47 +00001781 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return (unsigned long) -EINVAL;
1783
David Howells8feae132009-01-08 12:04:47 +00001784 if (addr & ~PAGE_MASK)
1785 return -EINVAL;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (flags & MREMAP_FIXED && new_addr != addr)
1788 return (unsigned long) -EINVAL;
1789
David Howells8feae132009-01-08 12:04:47 +00001790 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001791 if (!vma)
1792 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
David Howells6fa5f802006-09-27 01:50:21 -07001794 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return (unsigned long) -EFAULT;
1796
David Howells6fa5f802006-09-27 01:50:21 -07001797 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return (unsigned long) -EPERM;
1799
David Howells8feae132009-01-08 12:04:47 +00001800 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return (unsigned long) -ENOMEM;
1802
1803 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001804 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001805 return vma->vm_start;
1806}
Paul Mundtb5073172007-07-21 04:37:25 -07001807EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001808
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001809SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1810 unsigned long, new_len, unsigned long, flags,
1811 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001812{
1813 unsigned long ret;
1814
1815 down_write(&current->mm->mmap_sem);
1816 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1817 up_write(&current->mm->mmap_sem);
1818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Linus Torvalds6aab3412005-11-28 14:34:23 -08001821struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001822 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 return NULL;
1825}
1826
Bob Liu8f3b1322011-07-08 15:39:46 -07001827int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1828 unsigned long pfn, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Bob Liu8f3b1322011-07-08 15:39:46 -07001830 if (addr != (pfn << PAGE_SHIFT))
1831 return -EINVAL;
1832
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001833 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
Luke Yang22c4af42006-07-14 00:24:09 -07001836EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Paul Mundtf905bc42008-02-04 22:29:59 -08001838int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1839 unsigned long pgoff)
1840{
1841 unsigned int size = vma->vm_end - vma->vm_start;
1842
1843 if (!(vma->vm_flags & VM_USERMAP))
1844 return -EINVAL;
1845
1846 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1847 vma->vm_end = vma->vm_start + size;
1848
1849 return 0;
1850}
1851EXPORT_SYMBOL(remap_vmalloc_range);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1854 unsigned long len, unsigned long pgoff, unsigned long flags)
1855{
1856 return -ENOMEM;
1857}
1858
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001859void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861}
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863void unmap_mapping_range(struct address_space *mapping,
1864 loff_t const holebegin, loff_t const holelen,
1865 int even_cows)
1866{
1867}
Luke Yang22c4af42006-07-14 00:24:09 -07001868EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870/*
1871 * Check that a process has enough memory to allocate a new virtual
1872 * mapping. 0 means there is enough memory for the allocation to
1873 * succeed and -ENOMEM implies there is not.
1874 *
1875 * We currently support three overcommit policies, which are set via the
1876 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1877 *
1878 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1879 * Additional code 2002 Jul 20 by Robert Love.
1880 *
1881 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1882 *
1883 * Note this is a helper function intended to be used by LSMs which
1884 * wish to use this logic.
1885 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001886int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 unsigned long free, allowed;
1889
1890 vm_acct_memory(pages);
1891
1892 /*
1893 * Sometimes we want to use more memory than we have
1894 */
1895 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1896 return 0;
1897
1898 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -07001899 free = global_page_state(NR_FREE_PAGES);
1900 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Dmitry Finkc15bef32011-07-25 17:12:19 -07001902 /*
1903 * shmem pages shouldn't be counted as free in this
1904 * case, they can't be purged, only swapped out, and
1905 * that won't affect the overall amount of available
1906 * memory in the system.
1907 */
1908 free -= global_page_state(NR_SHMEM);
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 free += nr_swap_pages;
1911
1912 /*
1913 * Any slabs which are created with the
1914 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1915 * which are reclaimable, under pressure. The dentry
1916 * cache and most inode caches should fall into this
1917 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001918 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -07001921 * Leave reserved pages. The pages are not for anonymous pages.
1922 */
1923 if (free <= totalreserve_pages)
1924 goto error;
1925 else
1926 free -= totalreserve_pages;
1927
1928 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * Leave the last 3% for root
1930 */
1931 if (!cap_sys_admin)
1932 free -= free / 32;
1933
1934 if (free > pages)
1935 return 0;
1936
Hideo AOKId5ddc792006-04-10 22:53:01 -07001937 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939
1940 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1941 /*
1942 * Leave the last 3% for root
1943 */
1944 if (!cap_sys_admin)
1945 allowed -= allowed / 32;
1946 allowed += total_swap_pages;
1947
1948 /* Don't let a single process grow too big:
1949 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001950 if (mm)
1951 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001953 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001955
Hideo AOKId5ddc792006-04-10 22:53:01 -07001956error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 vm_unacct_memory(pages);
1958
1959 return -ENOMEM;
1960}
1961
Stephen Wilsoncae5d392011-03-13 15:49:17 -04001962int in_gate_area_no_mm(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 return 0;
1965}
David Howellsb0e15192006-01-06 00:11:42 -08001966
Nick Piggind0217ac2007-07-19 01:47:03 -07001967int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001968{
1969 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001970 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001971}
Paul Mundtb5073172007-07-21 04:37:25 -07001972EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001973
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001974int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
1975 unsigned long size, pgoff_t pgoff)
1976{
1977 BUG();
1978 return 0;
1979}
1980EXPORT_SYMBOL(generic_file_remap_pages);
1981
Mike Frysingerf55f1992011-03-29 14:05:12 +01001982static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
1983 unsigned long addr, void *buf, int len, int write)
David Howells0ec76a12006-09-27 01:50:15 -07001984{
David Howells0ec76a12006-09-27 01:50:15 -07001985 struct vm_area_struct *vma;
David Howells0ec76a12006-09-27 01:50:15 -07001986
1987 down_read(&mm->mmap_sem);
1988
1989 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001990 vma = find_vma(mm, addr);
1991 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001992 /* don't overrun this mapping */
1993 if (addr + len >= vma->vm_end)
1994 len = vma->vm_end - addr;
1995
1996 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001997 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001998 copy_to_user_page(vma, NULL, addr,
1999 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07002000 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00002001 copy_from_user_page(vma, NULL, addr,
2002 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07002003 else
2004 len = 0;
2005 } else {
2006 len = 0;
2007 }
2008
2009 up_read(&mm->mmap_sem);
Mike Frysingerf55f1992011-03-29 14:05:12 +01002010
2011 return len;
2012}
2013
2014/**
2015 * @access_remote_vm - access another process' address space
2016 * @mm: the mm_struct of the target address space
2017 * @addr: start address to access
2018 * @buf: source or destination buffer
2019 * @len: number of bytes to transfer
2020 * @write: whether the access is a write
2021 *
2022 * The caller must hold a reference on @mm.
2023 */
2024int access_remote_vm(struct mm_struct *mm, unsigned long addr,
2025 void *buf, int len, int write)
2026{
2027 return __access_remote_vm(NULL, mm, addr, buf, len, write);
2028}
2029
2030/*
2031 * Access another process' address space.
2032 * - source/target buffer must be kernel space
2033 */
2034int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
2035{
2036 struct mm_struct *mm;
2037
2038 if (addr + len < addr)
2039 return 0;
2040
2041 mm = get_task_mm(tsk);
2042 if (!mm)
2043 return 0;
2044
2045 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2046
David Howells0ec76a12006-09-27 01:50:15 -07002047 mmput(mm);
2048 return len;
2049}
David Howells7e660872010-01-15 17:01:39 -08002050
2051/**
2052 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2053 * @inode: The inode to check
2054 * @size: The current filesize of the inode
2055 * @newsize: The proposed filesize of the inode
2056 *
2057 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2058 * make sure that that any outstanding VMAs aren't broken and then shrink the
2059 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2060 * automatically grant mappings that are too large.
2061 */
2062int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2063 size_t newsize)
2064{
2065 struct vm_area_struct *vma;
David Howells7e660872010-01-15 17:01:39 -08002066 struct vm_region *region;
2067 pgoff_t low, high;
2068 size_t r_size, r_top;
2069
2070 low = newsize >> PAGE_SHIFT;
2071 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2072
2073 down_write(&nommu_region_sem);
David Howells918e5562012-02-23 13:50:35 +00002074 mutex_lock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002075
2076 /* search for VMAs that fall within the dead zone */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002077 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
David Howells7e660872010-01-15 17:01:39 -08002078 /* found one - only interested if it's shared out of the page
2079 * cache */
2080 if (vma->vm_flags & VM_SHARED) {
David Howells918e5562012-02-23 13:50:35 +00002081 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002082 up_write(&nommu_region_sem);
2083 return -ETXTBSY; /* not quite true, but near enough */
2084 }
2085 }
2086
2087 /* reduce any regions that overlap the dead zone - if in existence,
2088 * these will be pointed to by VMAs that don't overlap the dead zone
2089 *
2090 * we don't check for any regions that start beyond the EOF as there
2091 * shouldn't be any
2092 */
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002093 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
2094 0, ULONG_MAX) {
David Howells7e660872010-01-15 17:01:39 -08002095 if (!(vma->vm_flags & VM_SHARED))
2096 continue;
2097
2098 region = vma->vm_region;
2099 r_size = region->vm_top - region->vm_start;
2100 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2101
2102 if (r_top > newsize) {
2103 region->vm_top -= r_top - newsize;
2104 if (region->vm_end > region->vm_top)
2105 region->vm_end = region->vm_top;
2106 }
2107 }
2108
David Howells918e5562012-02-23 13:50:35 +00002109 mutex_unlock(&inode->i_mapping->i_mmap_mutex);
David Howells7e660872010-01-15 17:01:39 -08002110 up_write(&nommu_region_sem);
2111 return 0;
2112}