blob: 3613517c75929b17e1b3d93a650e5d9957c2315a [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 Mundteb6434d2009-01-21 17:45:47 +090013 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
David Howellsf2b85442007-10-29 13:15:39 +000016#include <linux/module.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>
Roland McGrathfa8e26c2008-07-25 19:45:50 -070025#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
27#include <linux/backing-dev.h>
28#include <linux/mount.h>
29#include <linux/personality.h>
30#include <linux/security.h>
31#include <linux/syscalls.h>
Al Viro120a7952010-10-30 02:54:44 -040032#include <linux/audit.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070070EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070071EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Howells8feae132009-01-08 12:04:47 +000073/* list of mapped, potentially shareable regions */
74static struct kmem_cache *vm_region_jar;
75struct rb_root nommu_region_tree = RB_ROOT;
76DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040078const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Return the total memory allocated for this pointer, not
83 * just what the caller asked for.
84 *
85 * Doesn't have to be accurate, i.e. may have races.
86 */
87unsigned int kobjsize(const void *objp)
88{
89 struct page *page;
90
Michael Hennerich4016a132008-04-28 02:13:38 -070091 /*
92 * If the object we have should not have ksize performed on it,
93 * return size of 0
94 */
Paul Mundt5a1603b2008-06-12 16:29:55 +090095 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070096 return 0;
97
98 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070099
100 /*
101 * If the allocator sets PageSlab, we know the pointer came from
102 * kmalloc().
103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (PageSlab(page))
105 return ksize(objp);
106
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700107 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000108 * If it's not a compound page, see if we have a matching VMA
109 * region. This test is intentionally done in reverse order,
110 * so if there's no VMA, we still fall through and hand back
111 * PAGE_SIZE for 0-order pages.
112 */
113 if (!PageCompound(page)) {
114 struct vm_area_struct *vma;
115
116 vma = find_vma(current->mm, (unsigned long)objp);
117 if (vma)
118 return vma->vm_end - vma->vm_start;
119 }
120
121 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700122 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900123 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700124 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900125 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Nick Pigginb291f002008-10-18 20:26:44 -0700128int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Hugh Dickins4266c972009-09-23 17:05:53 +0100129 unsigned long start, int nr_pages, unsigned int foll_flags,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200130 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700132 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700133 unsigned long vm_flags;
134 int i;
135
136 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700137 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700138 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700139 vm_flags = (foll_flags & FOLL_WRITE) ?
140 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
141 vm_flags &= (foll_flags & FOLL_FORCE) ?
142 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Peter Zijlstra9d737772009-06-25 11:58:55 +0200144 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000145 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700146 if (!vma)
147 goto finish_or_fault;
148
149 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700150 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
151 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700152 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (pages) {
155 pages[i] = virt_to_page(start);
156 if (pages[i])
157 page_cache_get(pages[i]);
158 }
159 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700160 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000161 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
David Howells7b4d5b82006-09-27 01:50:18 -0700163
164 return i;
165
166finish_or_fault:
167 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Nick Pigginb291f002008-10-18 20:26:44 -0700169
Nick Pigginb291f002008-10-18 20:26:44 -0700170/*
171 * get a list of pages in an address range belonging to the specified process
172 * and indicate the VMA that covers each page
173 * - this is potentially dodgy as we may end incrementing the page count of a
174 * slab page or a secondary page from a compound page
175 * - don't permit access to VMAs that don't support it, such as I/O mappings
176 */
177int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200178 unsigned long start, int nr_pages, int write, int force,
Nick Pigginb291f002008-10-18 20:26:44 -0700179 struct page **pages, struct vm_area_struct **vmas)
180{
181 int flags = 0;
182
183 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700184 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700185 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700186 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700187
Peter Zijlstra9d737772009-06-25 11:58:55 +0200188 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
Nick Pigginb291f002008-10-18 20:26:44 -0700189}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000190EXPORT_SYMBOL(get_user_pages);
191
Paul Mundtdfc2f912009-06-26 04:31:57 +0900192/**
193 * follow_pfn - look up PFN at a user virtual address
194 * @vma: memory mapping
195 * @address: user virtual address
196 * @pfn: location to store found PFN
197 *
198 * Only IO mappings and raw PFN mappings are allowed.
199 *
200 * Returns zero and the pfn at @pfn on success, -ve otherwise.
201 */
202int follow_pfn(struct vm_area_struct *vma, unsigned long address,
203 unsigned long *pfn)
204{
205 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
206 return -EINVAL;
207
208 *pfn = address >> PAGE_SHIFT;
209 return 0;
210}
211EXPORT_SYMBOL(follow_pfn);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213DEFINE_RWLOCK(vmlist_lock);
214struct vm_struct *vmlist;
215
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800216void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 kfree(addr);
219}
Paul Mundtb5073172007-07-21 04:37:25 -0700220EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Al Virodd0fc662005-10-07 07:46:04 +0100222void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200225 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
226 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Nick Piggin84097512006-03-22 00:08:34 -0800228 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
Paul Mundtb5073172007-07-21 04:37:25 -0700230EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Paul Mundtf905bc42008-02-04 22:29:59 -0800232void *vmalloc_user(unsigned long size)
233{
234 void *ret;
235
236 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
237 PAGE_KERNEL);
238 if (ret) {
239 struct vm_area_struct *vma;
240
241 down_write(&current->mm->mmap_sem);
242 vma = find_vma(current->mm, (unsigned long)ret);
243 if (vma)
244 vma->vm_flags |= VM_USERMAP;
245 up_write(&current->mm->mmap_sem);
246 }
247
248 return ret;
249}
250EXPORT_SYMBOL(vmalloc_user);
251
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800252struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 return virt_to_page(addr);
255}
Paul Mundtb5073172007-07-21 04:37:25 -0700256EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800258unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 return page_to_pfn(virt_to_page(addr));
261}
Paul Mundtb5073172007-07-21 04:37:25 -0700262EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264long vread(char *buf, char *addr, unsigned long count)
265{
266 memcpy(buf, addr, count);
267 return count;
268}
269
270long vwrite(char *buf, char *addr, unsigned long count)
271{
272 /* Don't allow overflow */
273 if ((unsigned long) addr + count < count)
274 count = -(unsigned long) addr;
275
276 memcpy(addr, buf, count);
277 return(count);
278}
279
280/*
281 * vmalloc - allocate virtually continguos memory
282 *
283 * @size: allocation size
284 *
285 * Allocate enough pages to cover @size from the page level
286 * allocator and map them into continguos kernel virtual space.
287 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200288 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * use __vmalloc() instead.
290 */
291void *vmalloc(unsigned long size)
292{
293 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
294}
Andrew Mortonf6138882006-02-28 16:59:18 -0800295EXPORT_SYMBOL(vmalloc);
296
Dave Younge1ca7782010-10-26 14:22:06 -0700297/*
298 * vzalloc - allocate virtually continguos memory with zero fill
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 * The memory allocated is set to zero.
305 *
306 * For tight control over page level allocator and protection flags
307 * use __vmalloc() instead.
308 */
309void *vzalloc(unsigned long size)
310{
311 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
312 PAGE_KERNEL);
313}
314EXPORT_SYMBOL(vzalloc);
315
316/**
317 * vmalloc_node - allocate memory on a specific node
318 * @size: allocation size
319 * @node: numa node
320 *
321 * Allocate enough pages to cover @size from the page level
322 * allocator and map them into contiguous kernel virtual space.
323 *
324 * For tight control over page level allocator and protection flags
325 * use __vmalloc() instead.
326 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800327void *vmalloc_node(unsigned long size, int node)
328{
329 return vmalloc(size);
330}
Dave Younge1ca7782010-10-26 14:22:06 -0700331
332/**
333 * vzalloc_node - allocate memory on a specific node with zero fill
334 * @size: allocation size
335 * @node: numa node
336 *
337 * Allocate enough pages to cover @size from the page level
338 * allocator and map them into contiguous kernel virtual space.
339 * The memory allocated is set to zero.
340 *
341 * For tight control over page level allocator and protection flags
342 * use __vmalloc() instead.
343 */
344void *vzalloc_node(unsigned long size, int node)
345{
346 return vzalloc(size);
347}
348EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Paul Mundt1af446e2008-08-04 16:01:47 +0900350#ifndef PAGE_KERNEL_EXEC
351# define PAGE_KERNEL_EXEC PAGE_KERNEL
352#endif
353
354/**
355 * vmalloc_exec - allocate virtually contiguous, executable memory
356 * @size: allocation size
357 *
358 * Kernel-internal function to allocate enough pages to cover @size
359 * the page level allocator and map them into contiguous and
360 * executable kernel virtual space.
361 *
362 * For tight control over page level allocator and protection flags
363 * use __vmalloc() instead.
364 */
365
366void *vmalloc_exec(unsigned long size)
367{
368 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
369}
370
Paul Mundtb5073172007-07-21 04:37:25 -0700371/**
372 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * @size: allocation size
374 *
375 * Allocate enough 32bit PA addressable pages to cover @size from the
376 * page level allocator and map them into continguos kernel virtual space.
377 */
378void *vmalloc_32(unsigned long size)
379{
380 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
381}
Paul Mundtb5073172007-07-21 04:37:25 -0700382EXPORT_SYMBOL(vmalloc_32);
383
384/**
385 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
386 * @size: allocation size
387 *
388 * The resulting memory area is 32bit addressable and zeroed so it can be
389 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800390 *
391 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
392 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700393 */
394void *vmalloc_32_user(unsigned long size)
395{
Paul Mundtf905bc42008-02-04 22:29:59 -0800396 /*
397 * We'll have to sort out the ZONE_DMA bits for 64-bit,
398 * but for now this can simply use vmalloc_user() directly.
399 */
400 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700401}
402EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
405{
406 BUG();
407 return NULL;
408}
Paul Mundtb5073172007-07-21 04:37:25 -0700409EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800411void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 BUG();
414}
Paul Mundtb5073172007-07-21 04:37:25 -0700415EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Paul Mundteb6434d2009-01-21 17:45:47 +0900417void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
418{
419 BUG();
420 return NULL;
421}
422EXPORT_SYMBOL(vm_map_ram);
423
424void vm_unmap_ram(const void *mem, unsigned int count)
425{
426 BUG();
427}
428EXPORT_SYMBOL(vm_unmap_ram);
429
430void vm_unmap_aliases(void)
431{
432}
433EXPORT_SYMBOL_GPL(vm_unmap_aliases);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700436 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
437 * have one.
438 */
439void __attribute__((weak)) vmalloc_sync_all(void)
440{
441}
442
Paul Mundtb5073172007-07-21 04:37:25 -0700443int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
444 struct page *page)
445{
446 return -EINVAL;
447}
448EXPORT_SYMBOL(vm_insert_page);
449
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700450/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * sys_brk() for the most part doesn't need the global kernel
452 * lock, except when an application is doing something nasty
453 * like trying to un-brk an area that has already been mapped
454 * to a regular file. in this case, the unmapping will need
455 * to invoke file system routines that need the global lock.
456 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100457SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct mm_struct *mm = current->mm;
460
461 if (brk < mm->start_brk || brk > mm->context.end_brk)
462 return mm->brk;
463
464 if (mm->brk == brk)
465 return mm->brk;
466
467 /*
468 * Always allow shrinking brk
469 */
470 if (brk <= mm->brk) {
471 mm->brk = brk;
472 return brk;
473 }
474
475 /*
476 * Ok, looks good - let it rip.
477 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000478 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return mm->brk = brk;
480}
481
David Howells8feae132009-01-08 12:04:47 +0000482/*
483 * initialise the VMA and region record slabs
484 */
485void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700487 int ret;
488
489 ret = percpu_counter_init(&vm_committed_as, 0);
490 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700491 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David Howells8feae132009-01-08 12:04:47 +0000494/*
495 * validate the region tree
496 * - the caller must hold the region lock
497 */
498#ifdef CONFIG_DEBUG_NOMMU_REGIONS
499static noinline void validate_nommu_regions(void)
500{
501 struct vm_region *region, *last;
502 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
David Howells8feae132009-01-08 12:04:47 +0000504 lastp = rb_first(&nommu_region_tree);
505 if (!lastp)
506 return;
507
508 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700509 BUG_ON(unlikely(last->vm_end <= last->vm_start));
510 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000511
512 while ((p = rb_next(lastp))) {
513 region = rb_entry(p, struct vm_region, vm_rb);
514 last = rb_entry(lastp, struct vm_region, vm_rb);
515
David Howells33e5d7692009-04-02 16:56:32 -0700516 BUG_ON(unlikely(region->vm_end <= region->vm_start));
517 BUG_ON(unlikely(region->vm_top < region->vm_end));
518 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000519
520 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522}
David Howells8feae132009-01-08 12:04:47 +0000523#else
David Howells33e5d7692009-04-02 16:56:32 -0700524static void validate_nommu_regions(void)
525{
526}
David Howells8feae132009-01-08 12:04:47 +0000527#endif
528
529/*
530 * add a region into the global tree
531 */
532static void add_nommu_region(struct vm_region *region)
533{
534 struct vm_region *pregion;
535 struct rb_node **p, *parent;
536
537 validate_nommu_regions();
538
David Howells8feae132009-01-08 12:04:47 +0000539 parent = NULL;
540 p = &nommu_region_tree.rb_node;
541 while (*p) {
542 parent = *p;
543 pregion = rb_entry(parent, struct vm_region, vm_rb);
544 if (region->vm_start < pregion->vm_start)
545 p = &(*p)->rb_left;
546 else if (region->vm_start > pregion->vm_start)
547 p = &(*p)->rb_right;
548 else if (pregion == region)
549 return;
550 else
551 BUG();
552 }
553
554 rb_link_node(&region->vm_rb, parent, p);
555 rb_insert_color(&region->vm_rb, &nommu_region_tree);
556
557 validate_nommu_regions();
558}
559
560/*
561 * delete a region from the global tree
562 */
563static void delete_nommu_region(struct vm_region *region)
564{
565 BUG_ON(!nommu_region_tree.rb_node);
566
567 validate_nommu_regions();
568 rb_erase(&region->vm_rb, &nommu_region_tree);
569 validate_nommu_regions();
570}
571
572/*
573 * free a contiguous series of pages
574 */
575static void free_page_series(unsigned long from, unsigned long to)
576{
577 for (; from < to; from += PAGE_SIZE) {
578 struct page *page = virt_to_page(from);
579
580 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700581 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000582 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700583 kdebug("free page %p: refcount not one: %d",
584 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000585 put_page(page);
586 }
587}
588
589/*
590 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700591 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000592 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000593 * will equal vm_start
594 */
595static void __put_nommu_region(struct vm_region *region)
596 __releases(nommu_region_sem)
597{
David Howells1e2ae592010-01-15 17:01:33 -0800598 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000599
600 BUG_ON(!nommu_region_tree.rb_node);
601
David Howells1e2ae592010-01-15 17:01:33 -0800602 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000603 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000604 delete_nommu_region(region);
605 up_write(&nommu_region_sem);
606
607 if (region->vm_file)
608 fput(region->vm_file);
609
610 /* IO memory and memory shared directly out of the pagecache
611 * from ramfs/tmpfs mustn't be released here */
612 if (region->vm_flags & VM_MAPPED_COPY) {
613 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000614 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000615 }
616 kmem_cache_free(vm_region_jar, region);
617 } else {
618 up_write(&nommu_region_sem);
619 }
620}
621
622/*
623 * release a reference to a region
624 */
625static void put_nommu_region(struct vm_region *region)
626{
627 down_write(&nommu_region_sem);
628 __put_nommu_region(region);
629}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
David Howells30340972006-09-27 01:50:20 -0700631/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700632 * update protection on a vma
633 */
634static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
635{
636#ifdef CONFIG_MPU
637 struct mm_struct *mm = vma->vm_mm;
638 long start = vma->vm_start & PAGE_MASK;
639 while (start < vma->vm_end) {
640 protect_page(mm, start, flags);
641 start += PAGE_SIZE;
642 }
643 update_protections(mm);
644#endif
645}
646
647/*
David Howells30340972006-09-27 01:50:20 -0700648 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000649 * and tree and add to the address space's page tree also if not an anonymous
650 * page
David Howells30340972006-09-27 01:50:20 -0700651 * - should be called with mm->mmap_sem held writelocked
652 */
David Howells8feae132009-01-08 12:04:47 +0000653static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700654{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700655 struct vm_area_struct *pvma, **pp, *next;
David Howells8feae132009-01-08 12:04:47 +0000656 struct address_space *mapping;
657 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700658
David Howells8feae132009-01-08 12:04:47 +0000659 kenter(",%p", vma);
660
661 BUG_ON(!vma->vm_region);
662
663 mm->map_count++;
664 vma->vm_mm = mm;
665
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700666 protect_vma(vma, vma->vm_flags);
667
David Howells8feae132009-01-08 12:04:47 +0000668 /* add the VMA to the mapping */
669 if (vma->vm_file) {
670 mapping = vma->vm_file->f_mapping;
671
672 flush_dcache_mmap_lock(mapping);
673 vma_prio_tree_insert(vma, &mapping->i_mmap);
674 flush_dcache_mmap_unlock(mapping);
675 }
676
677 /* add the VMA to the tree */
678 parent = NULL;
679 p = &mm->mm_rb.rb_node;
680 while (*p) {
681 parent = *p;
682 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
683
684 /* sort by: start addr, end addr, VMA struct addr in that order
685 * (the latter is necessary as we may get identical VMAs) */
686 if (vma->vm_start < pvma->vm_start)
687 p = &(*p)->rb_left;
688 else if (vma->vm_start > pvma->vm_start)
689 p = &(*p)->rb_right;
690 else if (vma->vm_end < pvma->vm_end)
691 p = &(*p)->rb_left;
692 else if (vma->vm_end > pvma->vm_end)
693 p = &(*p)->rb_right;
694 else if (vma < pvma)
695 p = &(*p)->rb_left;
696 else if (vma > pvma)
697 p = &(*p)->rb_right;
698 else
699 BUG();
700 }
701
702 rb_link_node(&vma->vm_rb, parent, p);
703 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
704
705 /* add VMA to the VMA list also */
706 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
707 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700708 break;
David Howells8feae132009-01-08 12:04:47 +0000709 if (pvma->vm_start < vma->vm_start)
710 continue;
711 if (pvma->vm_end < vma->vm_end)
712 break;
713 }
David Howells30340972006-09-27 01:50:20 -0700714
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700715 next = *pp;
David Howells8feae132009-01-08 12:04:47 +0000716 *pp = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700717 vma->vm_next = next;
718 if (next)
719 next->vm_prev = vma;
David Howells8feae132009-01-08 12:04:47 +0000720}
721
722/*
723 * delete a VMA from its owning mm_struct and address space
724 */
725static void delete_vma_from_mm(struct vm_area_struct *vma)
726{
727 struct vm_area_struct **pp;
728 struct address_space *mapping;
729 struct mm_struct *mm = vma->vm_mm;
730
731 kenter("%p", vma);
732
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700733 protect_vma(vma, 0);
734
David Howells8feae132009-01-08 12:04:47 +0000735 mm->map_count--;
736 if (mm->mmap_cache == vma)
737 mm->mmap_cache = NULL;
738
739 /* remove the VMA from the mapping */
740 if (vma->vm_file) {
741 mapping = vma->vm_file->f_mapping;
742
743 flush_dcache_mmap_lock(mapping);
744 vma_prio_tree_remove(vma, &mapping->i_mmap);
745 flush_dcache_mmap_unlock(mapping);
746 }
747
748 /* remove from the MM's tree and list */
749 rb_erase(&vma->vm_rb, &mm->mm_rb);
750 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
751 if (*pp == vma) {
752 *pp = vma->vm_next;
753 break;
754 }
755 }
756
757 vma->vm_mm = NULL;
758}
759
760/*
761 * destroy a VMA record
762 */
763static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
764{
765 kenter("%p", vma);
766 if (vma->vm_ops && vma->vm_ops->close)
767 vma->vm_ops->close(vma);
768 if (vma->vm_file) {
769 fput(vma->vm_file);
770 if (vma->vm_flags & VM_EXECUTABLE)
771 removed_exe_file_vma(mm);
772 }
773 put_nommu_region(vma->vm_region);
774 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700775}
776
777/*
778 * look up the first VMA in which addr resides, NULL if none
779 * - should be called with mm->mmap_sem at least held readlocked
780 */
781struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
782{
David Howells8feae132009-01-08 12:04:47 +0000783 struct vm_area_struct *vma;
784 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700785
David Howells8feae132009-01-08 12:04:47 +0000786 /* check the cache first */
787 vma = mm->mmap_cache;
788 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
789 return vma;
790
791 /* trawl the tree (there may be multiple mappings in which addr
792 * resides) */
793 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
794 vma = rb_entry(n, struct vm_area_struct, vm_rb);
795 if (vma->vm_start > addr)
796 return NULL;
797 if (vma->vm_end > addr) {
798 mm->mmap_cache = vma;
799 return vma;
800 }
David Howells30340972006-09-27 01:50:20 -0700801 }
802
David Howells30340972006-09-27 01:50:20 -0700803 return NULL;
804}
805EXPORT_SYMBOL(find_vma);
806
807/*
David Howells930e6522006-09-27 01:50:22 -0700808 * find a VMA
809 * - we don't extend stack VMAs under NOMMU conditions
810 */
811struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
812{
David Howells7561e8c2010-03-25 16:48:38 +0000813 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700814}
815
David Howells8feae132009-01-08 12:04:47 +0000816/*
817 * expand a stack to a given address
818 * - not supported under NOMMU conditions
819 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700820int expand_stack(struct vm_area_struct *vma, unsigned long address)
821{
822 return -ENOMEM;
823}
824
David Howells930e6522006-09-27 01:50:22 -0700825/*
David Howells6fa5f802006-09-27 01:50:21 -0700826 * look up the first VMA exactly that exactly matches addr
827 * - should be called with mm->mmap_sem at least held readlocked
828 */
David Howells8feae132009-01-08 12:04:47 +0000829static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
830 unsigned long addr,
831 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000834 struct rb_node *n = mm->mm_rb.rb_node;
835 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
David Howells8feae132009-01-08 12:04:47 +0000837 /* check the cache first */
838 vma = mm->mmap_cache;
839 if (vma && vma->vm_start == addr && vma->vm_end == end)
840 return vma;
841
842 /* trawl the tree (there may be multiple mappings in which addr
843 * resides) */
844 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000846 if (vma->vm_start < addr)
847 continue;
848 if (vma->vm_start > addr)
849 return NULL;
850 if (vma->vm_end == end) {
851 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return vma;
David Howells8feae132009-01-08 12:04:47 +0000853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856 return NULL;
857}
858
David Howells30340972006-09-27 01:50:20 -0700859/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * determine whether a mapping should be permitted and, if so, what sort of
861 * mapping we're capable of supporting
862 */
863static int validate_mmap_request(struct file *file,
864 unsigned long addr,
865 unsigned long len,
866 unsigned long prot,
867 unsigned long flags,
868 unsigned long pgoff,
869 unsigned long *_capabilities)
870{
David Howells8feae132009-01-08 12:04:47 +0000871 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 unsigned long reqprot = prot;
873 int ret;
874
875 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100876 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 printk(KERN_DEBUG
878 "%d: Can't do fixed-address/overlay mmap of RAM\n",
879 current->pid);
880 return -EINVAL;
881 }
882
883 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
884 (flags & MAP_TYPE) != MAP_SHARED)
885 return -EINVAL;
886
Mike Frysingerf81cff02006-12-06 12:02:59 +1000887 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EINVAL;
889
Mike Frysingerf81cff02006-12-06 12:02:59 +1000890 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000891 rlen = PAGE_ALIGN(len);
892 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000893 return -ENOMEM;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000896 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000897 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (file) {
900 /* validate file mapping requests */
901 struct address_space *mapping;
902
903 /* files must support mmap */
904 if (!file->f_op || !file->f_op->mmap)
905 return -ENODEV;
906
907 /* work out if what we've got could possibly be shared
908 * - we support chardevs that provide their own "memory"
909 * - we support files/blockdevs that are memory backed
910 */
911 mapping = file->f_mapping;
912 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800913 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 capabilities = 0;
916 if (mapping && mapping->backing_dev_info)
917 capabilities = mapping->backing_dev_info->capabilities;
918
919 if (!capabilities) {
920 /* no explicit capabilities set, so assume some
921 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800922 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 case S_IFREG:
924 case S_IFBLK:
925 capabilities = BDI_CAP_MAP_COPY;
926 break;
927
928 case S_IFCHR:
929 capabilities =
930 BDI_CAP_MAP_DIRECT |
931 BDI_CAP_READ_MAP |
932 BDI_CAP_WRITE_MAP;
933 break;
934
935 default:
936 return -EINVAL;
937 }
938 }
939
940 /* eliminate any capabilities that we can't support on this
941 * device */
942 if (!file->f_op->get_unmapped_area)
943 capabilities &= ~BDI_CAP_MAP_DIRECT;
944 if (!file->f_op->read)
945 capabilities &= ~BDI_CAP_MAP_COPY;
946
Graff Yang28d7a6a2009-08-18 14:11:17 -0700947 /* The file shall have been opened with read permission. */
948 if (!(file->f_mode & FMODE_READ))
949 return -EACCES;
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (flags & MAP_SHARED) {
952 /* do checks for writing, appending and locking */
953 if ((prot & PROT_WRITE) &&
954 !(file->f_mode & FMODE_WRITE))
955 return -EACCES;
956
Josef Sipeke9536ae2006-12-08 02:37:21 -0800957 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 (file->f_mode & FMODE_WRITE))
959 return -EACCES;
960
Josef Sipeke9536ae2006-12-08 02:37:21 -0800961 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EAGAIN;
963
964 if (!(capabilities & BDI_CAP_MAP_DIRECT))
965 return -ENODEV;
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* we mustn't privatise shared mappings */
968 capabilities &= ~BDI_CAP_MAP_COPY;
969 }
970 else {
971 /* we're going to read the file into private memory we
972 * allocate */
973 if (!(capabilities & BDI_CAP_MAP_COPY))
974 return -ENODEV;
975
976 /* we don't permit a private writable mapping to be
977 * shared with the backing device */
978 if (prot & PROT_WRITE)
979 capabilities &= ~BDI_CAP_MAP_DIRECT;
980 }
981
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700982 if (capabilities & BDI_CAP_MAP_DIRECT) {
983 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
984 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
985 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
986 ) {
987 capabilities &= ~BDI_CAP_MAP_DIRECT;
988 if (flags & MAP_SHARED) {
989 printk(KERN_WARNING
990 "MAP_SHARED not completely supported on !MMU\n");
991 return -EINVAL;
992 }
993 }
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* handle executable mappings and implied executable
997 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800998 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (prot & PROT_EXEC)
1000 return -EPERM;
1001 }
1002 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1003 /* handle implication of PROT_EXEC by PROT_READ */
1004 if (current->personality & READ_IMPLIES_EXEC) {
1005 if (capabilities & BDI_CAP_EXEC_MAP)
1006 prot |= PROT_EXEC;
1007 }
1008 }
1009 else if ((prot & PROT_READ) &&
1010 (prot & PROT_EXEC) &&
1011 !(capabilities & BDI_CAP_EXEC_MAP)
1012 ) {
1013 /* backing file is not executable, try to copy */
1014 capabilities &= ~BDI_CAP_MAP_DIRECT;
1015 }
1016 }
1017 else {
1018 /* anonymous mappings are always memory backed and can be
1019 * privately mapped
1020 */
1021 capabilities = BDI_CAP_MAP_COPY;
1022
1023 /* handle PROT_EXEC implication by PROT_READ */
1024 if ((prot & PROT_READ) &&
1025 (current->personality & READ_IMPLIES_EXEC))
1026 prot |= PROT_EXEC;
1027 }
1028
1029 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -04001030 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (ret < 0)
1032 return ret;
1033
1034 /* looks okay */
1035 *_capabilities = capabilities;
1036 return 0;
1037}
1038
1039/*
1040 * we've determined that we can make the mapping, now translate what we
1041 * now know into VMA flags
1042 */
1043static unsigned long determine_vm_flags(struct file *file,
1044 unsigned long prot,
1045 unsigned long flags,
1046 unsigned long capabilities)
1047{
1048 unsigned long vm_flags;
1049
1050 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* vm_flags |= mm->def_flags; */
1052
1053 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1054 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001055 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (file && !(prot & PROT_WRITE))
1057 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001058 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* overlay a shareable mapping on the backing device or inode
1060 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1061 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001062 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001064 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
1067 /* refuse to let anyone share private mappings with this process if
1068 * it's being traced - otherwise breakpoints set in it may interfere
1069 * with another untraced process
1070 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001071 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 vm_flags &= ~VM_MAYSHARE;
1073
1074 return vm_flags;
1075}
1076
1077/*
David Howells8feae132009-01-08 12:04:47 +00001078 * set up a shared mapping on a file (the driver or filesystem provides and
1079 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
David Howells8feae132009-01-08 12:04:47 +00001081static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 int ret;
1084
1085 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001086 if (ret == 0) {
1087 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001088 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (ret != -ENOSYS)
1091 return ret;
1092
David Howells3fa30462010-03-23 13:35:21 -07001093 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1094 * opposed to tried but failed) so we can only give a suitable error as
1095 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return -ENODEV;
1097}
1098
1099/*
1100 * set up a private mapping or an anonymous shared mapping
1101 */
David Howells8feae132009-01-08 12:04:47 +00001102static int do_mmap_private(struct vm_area_struct *vma,
1103 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001104 unsigned long len,
1105 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
David Howells8feae132009-01-08 12:04:47 +00001107 struct page *pages;
1108 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 void *base;
David Howells8feae132009-01-08 12:04:47 +00001110 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* invoke the file's mapping function so that it can keep track of
1113 * shared mappings on devices or memory
1114 * - VM_MAYSHARE will be set if it may attempt to share
1115 */
David Howells645d83c2009-09-24 15:13:10 +01001116 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001118 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001120 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1121 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001124 if (ret != -ENOSYS)
1125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* getting an ENOSYS error indicates that direct mmap isn't
1128 * possible (as opposed to tried but failed) so we'll try to
1129 * make a private copy of the data and map that instead */
1130 }
1131
David Howells8feae132009-01-08 12:04:47 +00001132 rlen = PAGE_ALIGN(len);
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* allocate some memory to hold the mapping
1135 * - note that this may not return a page-aligned address if the object
1136 * we're allocating is smaller than a page
1137 */
David Howells8feae132009-01-08 12:04:47 +00001138 order = get_order(rlen);
1139 kdebug("alloc order %d for %lx", order, len);
1140
1141 pages = alloc_pages(GFP_KERNEL, order);
1142 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto enomem;
1144
David Howells8feae132009-01-08 12:04:47 +00001145 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001146 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
David Howells8feae132009-01-08 12:04:47 +00001148 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001149
1150 /* we allocated a power-of-2 sized page set, so we may want to trim off
1151 * the excess */
1152 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1153 while (total > point) {
1154 order = ilog2(total - point);
1155 n = 1 << order;
1156 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001157 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001158 total -= n;
1159 set_page_refcounted(pages + total);
1160 __free_pages(pages + total, order);
1161 }
David Howells8feae132009-01-08 12:04:47 +00001162 }
1163
David Howells8feae132009-01-08 12:04:47 +00001164 for (point = 1; point < total; point++)
1165 set_page_refcounted(&pages[point]);
1166
1167 base = page_address(pages);
1168 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1169 region->vm_start = (unsigned long) base;
1170 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001171 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001172
1173 vma->vm_start = region->vm_start;
1174 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if (vma->vm_file) {
1177 /* read the contents of a file into the copy */
1178 mm_segment_t old_fs;
1179 loff_t fpos;
1180
1181 fpos = vma->vm_pgoff;
1182 fpos <<= PAGE_SHIFT;
1183
1184 old_fs = get_fs();
1185 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001186 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 set_fs(old_fs);
1188
1189 if (ret < 0)
1190 goto error_free;
1191
1192 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001193 if (ret < rlen)
1194 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
1198 return 0;
1199
1200error_free:
David Howells8feae132009-01-08 12:04:47 +00001201 free_page_series(region->vm_start, region->vm_end);
1202 region->vm_start = vma->vm_start = 0;
1203 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001204 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return ret;
1206
1207enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001208 printk("Allocation of length %lu from process %d (%s) failed\n",
1209 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 show_free_areas();
1211 return -ENOMEM;
1212}
1213
1214/*
1215 * handle mapping creation for uClinux
1216 */
1217unsigned long do_mmap_pgoff(struct file *file,
1218 unsigned long addr,
1219 unsigned long len,
1220 unsigned long prot,
1221 unsigned long flags,
1222 unsigned long pgoff)
1223{
David Howells8feae132009-01-08 12:04:47 +00001224 struct vm_area_struct *vma;
1225 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001227 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int ret;
1229
David Howells8feae132009-01-08 12:04:47 +00001230 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* decide whether we should attempt the mapping, and if so what sort of
1233 * mapping */
1234 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1235 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001236 if (ret < 0) {
1237 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return ret;
David Howells8feae132009-01-08 12:04:47 +00001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
David Howells06aab5a2009-09-24 12:33:48 +01001241 /* we ignore the address hint */
1242 addr = 0;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* we've determined that we can make the mapping, now translate what we
1245 * now know into VMA flags */
1246 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1247
David Howells8feae132009-01-08 12:04:47 +00001248 /* we're going to need to record the mapping */
1249 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1250 if (!region)
1251 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
David Howells8feae132009-01-08 12:04:47 +00001253 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1254 if (!vma)
1255 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
David Howells1e2ae592010-01-15 17:01:33 -08001257 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001258 region->vm_flags = vm_flags;
1259 region->vm_pgoff = pgoff;
1260
Rik van Riel5beb4932010-03-05 13:42:07 -08001261 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001262 vma->vm_flags = vm_flags;
1263 vma->vm_pgoff = pgoff;
1264
1265 if (file) {
1266 region->vm_file = file;
1267 get_file(file);
1268 vma->vm_file = file;
1269 get_file(file);
1270 if (vm_flags & VM_EXECUTABLE) {
1271 added_exe_file_vma(current->mm);
1272 vma->vm_mm = current->mm;
1273 }
1274 }
1275
1276 down_write(&nommu_region_sem);
1277
1278 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001280 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * - shared mappings on character devices and memory backed files are
1282 * permitted to overlap inexactly as far as we are concerned for in
1283 * these cases, sharing is handled in the driver or filesystem rather
1284 * than here
1285 */
1286 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001287 struct vm_region *pregion;
1288 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David Howells8feae132009-01-08 12:04:47 +00001290 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1291 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001292
David Howells8feae132009-01-08 12:04:47 +00001293 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1294 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
David Howells8feae132009-01-08 12:04:47 +00001296 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 continue;
1298
1299 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001300 if (pregion->vm_file->f_path.dentry->d_inode !=
1301 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 continue;
1303
David Howells8feae132009-01-08 12:04:47 +00001304 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 continue;
1306
David Howells8feae132009-01-08 12:04:47 +00001307 rpglen = pregion->vm_end - pregion->vm_start;
1308 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1309 rpgend = pregion->vm_pgoff + rpglen;
1310 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 continue;
1312
David Howells8feae132009-01-08 12:04:47 +00001313 /* handle inexactly overlapping matches between
1314 * mappings */
1315 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1316 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1317 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1319 goto sharing_violation;
1320 continue;
1321 }
1322
David Howells8feae132009-01-08 12:04:47 +00001323 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001324 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001325 vma->vm_region = pregion;
1326 start = pregion->vm_start;
1327 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1328 vma->vm_start = start;
1329 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
David Howells8feae132009-01-08 12:04:47 +00001331 if (pregion->vm_flags & VM_MAPPED_COPY) {
1332 kdebug("share copy");
1333 vma->vm_flags |= VM_MAPPED_COPY;
1334 } else {
1335 kdebug("share mmap");
1336 ret = do_mmap_shared_file(vma);
1337 if (ret < 0) {
1338 vma->vm_region = NULL;
1339 vma->vm_start = 0;
1340 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001341 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001342 pregion = NULL;
1343 goto error_just_free;
1344 }
1345 }
1346 fput(region->vm_file);
1347 kmem_cache_free(vm_region_jar, region);
1348 region = pregion;
1349 result = start;
1350 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* obtain the address at which to make a shared mapping
1354 * - this is the hook for quasi-memory character devices to
1355 * tell us the location of a shared mapping
1356 */
David Howells645d83c2009-09-24 15:13:10 +01001357 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 addr = file->f_op->get_unmapped_area(file, addr, len,
1359 pgoff, flags);
1360 if (IS_ERR((void *) addr)) {
1361 ret = addr;
1362 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001363 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /* the driver refused to tell us where to site
1366 * the mapping so we'll have to attempt to copy
1367 * it */
1368 ret = (unsigned long) -ENODEV;
1369 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001370 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001373 } else {
1374 vma->vm_start = region->vm_start = addr;
1375 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 }
1378 }
1379
David Howells8feae132009-01-08 12:04:47 +00001380 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
David Howells645d83c2009-09-24 15:13:10 +01001382 /* set up the mapping
1383 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001386 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 else
David Howells645d83c2009-09-24 15:13:10 +01001388 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001390 goto error_just_free;
1391 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001392
Jie Zhangea637632009-12-14 18:00:02 -08001393 /* clear anonymous mappings that don't ask for uninitialized data */
1394 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1395 memset((void *)region->vm_start, 0,
1396 region->vm_end - region->vm_start);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001399 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 current->mm->total_vm += len >> PAGE_SHIFT;
1402
David Howells8feae132009-01-08 12:04:47 +00001403share:
1404 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Mike Frysingercfe79c02010-01-06 17:23:23 +00001406 /* we flush the region from the icache only when the first executable
1407 * mapping of it is made */
1408 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1409 flush_icache_range(region->vm_start, region->vm_end);
1410 region->vm_icache_flushed = true;
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Mike Frysingercfe79c02010-01-06 17:23:23 +00001413 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
David Howells8feae132009-01-08 12:04:47 +00001415 kleave(" = %lx", result);
1416 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
David Howells8feae132009-01-08 12:04:47 +00001418error_just_free:
1419 up_write(&nommu_region_sem);
1420error:
David Howells89a86402009-10-30 13:13:26 +00001421 if (region->vm_file)
1422 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001423 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001424 if (vma->vm_file)
1425 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001426 if (vma->vm_flags & VM_EXECUTABLE)
1427 removed_exe_file_vma(vma->vm_mm);
1428 kmem_cache_free(vm_area_cachep, vma);
1429 kleave(" = %d", ret);
1430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
David Howells8feae132009-01-08 12:04:47 +00001432sharing_violation:
1433 up_write(&nommu_region_sem);
1434 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1435 ret = -EINVAL;
1436 goto error;
1437
1438error_getting_vma:
1439 kmem_cache_free(vm_region_jar, region);
1440 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1441 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 len, current->pid);
1443 show_free_areas();
1444 return -ENOMEM;
1445
David Howells8feae132009-01-08 12:04:47 +00001446error_getting_region:
1447 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1448 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 len, current->pid);
1450 show_free_areas();
1451 return -ENOMEM;
1452}
Paul Mundtb5073172007-07-21 04:37:25 -07001453EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001455SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1456 unsigned long, prot, unsigned long, flags,
1457 unsigned long, fd, unsigned long, pgoff)
1458{
1459 struct file *file = NULL;
1460 unsigned long retval = -EBADF;
1461
Al Viro120a7952010-10-30 02:54:44 -04001462 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001463 if (!(flags & MAP_ANONYMOUS)) {
1464 file = fget(fd);
1465 if (!file)
1466 goto out;
1467 }
1468
1469 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1470
1471 down_write(&current->mm->mmap_sem);
1472 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1473 up_write(&current->mm->mmap_sem);
1474
1475 if (file)
1476 fput(file);
1477out:
1478 return retval;
1479}
1480
Christoph Hellwiga4679372010-03-10 15:21:15 -08001481#ifdef __ARCH_WANT_SYS_OLD_MMAP
1482struct mmap_arg_struct {
1483 unsigned long addr;
1484 unsigned long len;
1485 unsigned long prot;
1486 unsigned long flags;
1487 unsigned long fd;
1488 unsigned long offset;
1489};
1490
1491SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1492{
1493 struct mmap_arg_struct a;
1494
1495 if (copy_from_user(&a, arg, sizeof(a)))
1496 return -EFAULT;
1497 if (a.offset & ~PAGE_MASK)
1498 return -EINVAL;
1499
1500 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1501 a.offset >> PAGE_SHIFT);
1502}
1503#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/*
David Howells8feae132009-01-08 12:04:47 +00001506 * split a vma into two pieces at address 'addr', a new vma is allocated either
1507 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 */
David Howells8feae132009-01-08 12:04:47 +00001509int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1510 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
David Howells8feae132009-01-08 12:04:47 +00001512 struct vm_area_struct *new;
1513 struct vm_region *region;
1514 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
David Howells8feae132009-01-08 12:04:47 +00001516 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
David Howells779c1022010-01-15 17:01:34 -08001518 /* we're only permitted to split anonymous regions (these should have
1519 * only a single usage on the region) */
1520 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001521 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
David Howells8feae132009-01-08 12:04:47 +00001523 if (mm->map_count >= sysctl_max_map_count)
1524 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
David Howells8feae132009-01-08 12:04:47 +00001526 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1527 if (!region)
1528 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
David Howells8feae132009-01-08 12:04:47 +00001530 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1531 if (!new) {
1532 kmem_cache_free(vm_region_jar, region);
1533 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
David Howells8feae132009-01-08 12:04:47 +00001535
1536 /* most fields are the same, copy all, and then fixup */
1537 *new = *vma;
1538 *region = *vma->vm_region;
1539 new->vm_region = region;
1540
1541 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1542
1543 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001544 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001545 } else {
1546 region->vm_start = new->vm_start = addr;
1547 region->vm_pgoff = new->vm_pgoff += npages;
1548 }
1549
1550 if (new->vm_ops && new->vm_ops->open)
1551 new->vm_ops->open(new);
1552
1553 delete_vma_from_mm(vma);
1554 down_write(&nommu_region_sem);
1555 delete_nommu_region(vma->vm_region);
1556 if (new_below) {
1557 vma->vm_region->vm_start = vma->vm_start = addr;
1558 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1559 } else {
1560 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001561 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001562 }
1563 add_nommu_region(vma->vm_region);
1564 add_nommu_region(new->vm_region);
1565 up_write(&nommu_region_sem);
1566 add_vma_to_mm(mm, vma);
1567 add_vma_to_mm(mm, new);
1568 return 0;
1569}
1570
1571/*
1572 * shrink a VMA by removing the specified chunk from either the beginning or
1573 * the end
1574 */
1575static int shrink_vma(struct mm_struct *mm,
1576 struct vm_area_struct *vma,
1577 unsigned long from, unsigned long to)
1578{
1579 struct vm_region *region;
1580
1581 kenter("");
1582
1583 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1584 * and list */
1585 delete_vma_from_mm(vma);
1586 if (from > vma->vm_start)
1587 vma->vm_end = from;
1588 else
1589 vma->vm_start = to;
1590 add_vma_to_mm(mm, vma);
1591
1592 /* cut the backing region down to size */
1593 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001594 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001595
1596 down_write(&nommu_region_sem);
1597 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001598 if (from > region->vm_start) {
1599 to = region->vm_top;
1600 region->vm_top = region->vm_end = from;
1601 } else {
David Howells8feae132009-01-08 12:04:47 +00001602 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001603 }
David Howells8feae132009-01-08 12:04:47 +00001604 add_nommu_region(region);
1605 up_write(&nommu_region_sem);
1606
1607 free_page_series(from, to);
1608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
David Howells30340972006-09-27 01:50:20 -07001611/*
1612 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001613 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1614 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001615 */
David Howells8feae132009-01-08 12:04:47 +00001616int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
David Howells8feae132009-01-08 12:04:47 +00001618 struct vm_area_struct *vma;
1619 struct rb_node *rb;
1620 unsigned long end = start + len;
1621 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
David Howells8feae132009-01-08 12:04:47 +00001623 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
David Howells8feae132009-01-08 12:04:47 +00001625 if (len == 0)
1626 return -EINVAL;
1627
1628 /* find the first potentially overlapping VMA */
1629 vma = find_vma(mm, start);
1630 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001631 static int limit = 0;
1632 if (limit < 5) {
1633 printk(KERN_WARNING
1634 "munmap of memory not mmapped by process %d"
1635 " (%s): 0x%lx-0x%lx\n",
1636 current->pid, current->comm,
1637 start, start + len - 1);
1638 limit++;
1639 }
David Howells8feae132009-01-08 12:04:47 +00001640 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
David Howells8feae132009-01-08 12:04:47 +00001643 /* we're allowed to split an anonymous VMA but not a file-backed one */
1644 if (vma->vm_file) {
1645 do {
1646 if (start > vma->vm_start) {
1647 kleave(" = -EINVAL [miss]");
1648 return -EINVAL;
1649 }
1650 if (end == vma->vm_end)
1651 goto erase_whole_vma;
1652 rb = rb_next(&vma->vm_rb);
1653 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1654 } while (rb);
1655 kleave(" = -EINVAL [split file]");
1656 return -EINVAL;
1657 } else {
1658 /* the chunk must be a subset of the VMA found */
1659 if (start == vma->vm_start && end == vma->vm_end)
1660 goto erase_whole_vma;
1661 if (start < vma->vm_start || end > vma->vm_end) {
1662 kleave(" = -EINVAL [superset]");
1663 return -EINVAL;
1664 }
1665 if (start & ~PAGE_MASK) {
1666 kleave(" = -EINVAL [unaligned start]");
1667 return -EINVAL;
1668 }
1669 if (end != vma->vm_end && end & ~PAGE_MASK) {
1670 kleave(" = -EINVAL [unaligned split]");
1671 return -EINVAL;
1672 }
1673 if (start != vma->vm_start && end != vma->vm_end) {
1674 ret = split_vma(mm, vma, start, 1);
1675 if (ret < 0) {
1676 kleave(" = %d [split]", ret);
1677 return ret;
1678 }
1679 }
1680 return shrink_vma(mm, vma, start, end);
1681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
David Howells8feae132009-01-08 12:04:47 +00001683erase_whole_vma:
1684 delete_vma_from_mm(vma);
1685 delete_vma(mm, vma);
1686 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688}
Paul Mundtb5073172007-07-21 04:37:25 -07001689EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001691SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001692{
1693 int ret;
1694 struct mm_struct *mm = current->mm;
1695
1696 down_write(&mm->mmap_sem);
1697 ret = do_munmap(mm, addr, len);
1698 up_write(&mm->mmap_sem);
1699 return ret;
1700}
1701
1702/*
David Howells8feae132009-01-08 12:04:47 +00001703 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001704 */
David Howells8feae132009-01-08 12:04:47 +00001705void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
David Howells8feae132009-01-08 12:04:47 +00001707 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
David Howells8feae132009-01-08 12:04:47 +00001709 if (!mm)
1710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
David Howells8feae132009-01-08 12:04:47 +00001712 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
David Howells8feae132009-01-08 12:04:47 +00001714 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
David Howells8feae132009-01-08 12:04:47 +00001716 while ((vma = mm->mmap)) {
1717 mm->mmap = vma->vm_next;
1718 delete_vma_from_mm(vma);
1719 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
David Howells8feae132009-01-08 12:04:47 +00001721
1722 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725unsigned long do_brk(unsigned long addr, unsigned long len)
1726{
1727 return -ENOMEM;
1728}
1729
1730/*
David Howells6fa5f802006-09-27 01:50:21 -07001731 * expand (or shrink) an existing mapping, potentially moving it at the same
1732 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 *
David Howells6fa5f802006-09-27 01:50:21 -07001734 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001735 * as long as it stays within the region allocated by do_mmap_private() and the
1736 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 *
David Howells6fa5f802006-09-27 01:50:21 -07001738 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
1740unsigned long do_mremap(unsigned long addr,
1741 unsigned long old_len, unsigned long new_len,
1742 unsigned long flags, unsigned long new_addr)
1743{
David Howells6fa5f802006-09-27 01:50:21 -07001744 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001747 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return (unsigned long) -EINVAL;
1749
David Howells8feae132009-01-08 12:04:47 +00001750 if (addr & ~PAGE_MASK)
1751 return -EINVAL;
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (flags & MREMAP_FIXED && new_addr != addr)
1754 return (unsigned long) -EINVAL;
1755
David Howells8feae132009-01-08 12:04:47 +00001756 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001757 if (!vma)
1758 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
David Howells6fa5f802006-09-27 01:50:21 -07001760 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return (unsigned long) -EFAULT;
1762
David Howells6fa5f802006-09-27 01:50:21 -07001763 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return (unsigned long) -EPERM;
1765
David Howells8feae132009-01-08 12:04:47 +00001766 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return (unsigned long) -ENOMEM;
1768
1769 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001770 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001771 return vma->vm_start;
1772}
Paul Mundtb5073172007-07-21 04:37:25 -07001773EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001774
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001775SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1776 unsigned long, new_len, unsigned long, flags,
1777 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001778{
1779 unsigned long ret;
1780
1781 down_write(&current->mm->mmap_sem);
1782 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1783 up_write(&current->mm->mmap_sem);
1784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Linus Torvalds6aab3412005-11-28 14:34:23 -08001787struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001788 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 return NULL;
1791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1794 unsigned long to, unsigned long size, pgprot_t prot)
1795{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001796 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
Luke Yang22c4af42006-07-14 00:24:09 -07001799EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Paul Mundtf905bc42008-02-04 22:29:59 -08001801int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1802 unsigned long pgoff)
1803{
1804 unsigned int size = vma->vm_end - vma->vm_start;
1805
1806 if (!(vma->vm_flags & VM_USERMAP))
1807 return -EINVAL;
1808
1809 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1810 vma->vm_end = vma->vm_start + size;
1811
1812 return 0;
1813}
1814EXPORT_SYMBOL(remap_vmalloc_range);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1817{
1818}
1819
1820unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1821 unsigned long len, unsigned long pgoff, unsigned long flags)
1822{
1823 return -ENOMEM;
1824}
1825
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001826void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828}
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830void unmap_mapping_range(struct address_space *mapping,
1831 loff_t const holebegin, loff_t const holelen,
1832 int even_cows)
1833{
1834}
Luke Yang22c4af42006-07-14 00:24:09 -07001835EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837/*
1838 * Check that a process has enough memory to allocate a new virtual
1839 * mapping. 0 means there is enough memory for the allocation to
1840 * succeed and -ENOMEM implies there is not.
1841 *
1842 * We currently support three overcommit policies, which are set via the
1843 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1844 *
1845 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1846 * Additional code 2002 Jul 20 by Robert Love.
1847 *
1848 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1849 *
1850 * Note this is a helper function intended to be used by LSMs which
1851 * wish to use this logic.
1852 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001853int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 unsigned long free, allowed;
1856
1857 vm_acct_memory(pages);
1858
1859 /*
1860 * Sometimes we want to use more memory than we have
1861 */
1862 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1863 return 0;
1864
1865 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1866 unsigned long n;
1867
Christoph Lameter347ce432006-06-30 01:55:35 -07001868 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 free += nr_swap_pages;
1870
1871 /*
1872 * Any slabs which are created with the
1873 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1874 * which are reclaimable, under pressure. The dentry
1875 * cache and most inode caches should fall into this
1876 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001877 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 /*
1880 * Leave the last 3% for root
1881 */
1882 if (!cap_sys_admin)
1883 free -= free / 32;
1884
1885 if (free > pages)
1886 return 0;
1887
1888 /*
1889 * nr_free_pages() is very expensive on large systems,
1890 * only call if we're about to fail.
1891 */
1892 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001893
1894 /*
1895 * Leave reserved pages. The pages are not for anonymous pages.
1896 */
1897 if (n <= totalreserve_pages)
1898 goto error;
1899 else
1900 n -= totalreserve_pages;
1901
1902 /*
1903 * Leave the last 3% for root
1904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!cap_sys_admin)
1906 n -= n / 32;
1907 free += n;
1908
1909 if (free > pages)
1910 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001911
1912 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914
1915 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1916 /*
1917 * Leave the last 3% for root
1918 */
1919 if (!cap_sys_admin)
1920 allowed -= allowed / 32;
1921 allowed += total_swap_pages;
1922
1923 /* Don't let a single process grow too big:
1924 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001925 if (mm)
1926 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001928 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001930
Hideo AOKId5ddc792006-04-10 22:53:01 -07001931error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 vm_unacct_memory(pages);
1933
1934 return -ENOMEM;
1935}
1936
1937int in_gate_area_no_task(unsigned long addr)
1938{
1939 return 0;
1940}
David Howellsb0e15192006-01-06 00:11:42 -08001941
Nick Piggind0217ac2007-07-19 01:47:03 -07001942int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001943{
1944 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001945 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001946}
Paul Mundtb5073172007-07-21 04:37:25 -07001947EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001948
1949/*
1950 * Access another process' address space.
1951 * - source/target buffer must be kernel space
1952 */
1953int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1954{
David Howells0ec76a12006-09-27 01:50:15 -07001955 struct vm_area_struct *vma;
1956 struct mm_struct *mm;
1957
1958 if (addr + len < addr)
1959 return 0;
1960
1961 mm = get_task_mm(tsk);
1962 if (!mm)
1963 return 0;
1964
1965 down_read(&mm->mmap_sem);
1966
1967 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001968 vma = find_vma(mm, addr);
1969 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001970 /* don't overrun this mapping */
1971 if (addr + len >= vma->vm_end)
1972 len = vma->vm_end - addr;
1973
1974 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001975 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001976 copy_to_user_page(vma, NULL, addr,
1977 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001978 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001979 copy_from_user_page(vma, NULL, addr,
1980 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001981 else
1982 len = 0;
1983 } else {
1984 len = 0;
1985 }
1986
1987 up_read(&mm->mmap_sem);
1988 mmput(mm);
1989 return len;
1990}
David Howells7e660872010-01-15 17:01:39 -08001991
1992/**
1993 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1994 * @inode: The inode to check
1995 * @size: The current filesize of the inode
1996 * @newsize: The proposed filesize of the inode
1997 *
1998 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1999 * make sure that that any outstanding VMAs aren't broken and then shrink the
2000 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2001 * automatically grant mappings that are too large.
2002 */
2003int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2004 size_t newsize)
2005{
2006 struct vm_area_struct *vma;
2007 struct prio_tree_iter iter;
2008 struct vm_region *region;
2009 pgoff_t low, high;
2010 size_t r_size, r_top;
2011
2012 low = newsize >> PAGE_SHIFT;
2013 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2014
2015 down_write(&nommu_region_sem);
2016
2017 /* search for VMAs that fall within the dead zone */
2018 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2019 low, high) {
2020 /* found one - only interested if it's shared out of the page
2021 * cache */
2022 if (vma->vm_flags & VM_SHARED) {
2023 up_write(&nommu_region_sem);
2024 return -ETXTBSY; /* not quite true, but near enough */
2025 }
2026 }
2027
2028 /* reduce any regions that overlap the dead zone - if in existence,
2029 * these will be pointed to by VMAs that don't overlap the dead zone
2030 *
2031 * we don't check for any regions that start beyond the EOF as there
2032 * shouldn't be any
2033 */
2034 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2035 0, ULONG_MAX) {
2036 if (!(vma->vm_flags & VM_SHARED))
2037 continue;
2038
2039 region = vma->vm_region;
2040 r_size = region->vm_top - region->vm_start;
2041 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2042
2043 if (r_top > newsize) {
2044 region->vm_top -= r_top - newsize;
2045 if (region->vm_end > region->vm_top)
2046 region->vm_end = region->vm_top;
2047 }
2048 }
2049
2050 up_write(&nommu_region_sem);
2051 return 0;
2052}