blob: 88ff091eb07a23dcaee1e8e6d0cc70927183b89b [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>
32
33#include <asm/uaccess.h>
34#include <asm/tlb.h>
35#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070036#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000037#include "internal.h"
38
David Howells8feae132009-01-08 12:04:47 +000039#if 0
40#define kenter(FMT, ...) \
41 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
42#define kleave(FMT, ...) \
43 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
44#define kdebug(FMT, ...) \
45 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
46#else
47#define kenter(FMT, ...) \
48 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
49#define kleave(FMT, ...) \
50 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
51#define kdebug(FMT, ...) \
52 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
53#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55void *high_memory;
56struct page *mem_map;
57unsigned long max_mapnr;
58unsigned long num_physpages;
Hugh Dickins4266c972009-09-23 17:05:53 +010059unsigned long highest_memmap_pfn;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070060struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
62int sysctl_overcommit_ratio = 50; /* default is 50% */
63int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070064int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int heap_stack_gap = 0;
66
David Howells33e5d7692009-04-02 16:56:32 -070067atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070070EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Howells8feae132009-01-08 12:04:47 +000072/* list of mapped, potentially shareable regions */
73static struct kmem_cache *vm_region_jar;
74struct rb_root nommu_region_tree = RB_ROOT;
75DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040077const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Return the total memory allocated for this pointer, not
82 * just what the caller asked for.
83 *
84 * Doesn't have to be accurate, i.e. may have races.
85 */
86unsigned int kobjsize(const void *objp)
87{
88 struct page *page;
89
Michael Hennerich4016a132008-04-28 02:13:38 -070090 /*
91 * If the object we have should not have ksize performed on it,
92 * return size of 0
93 */
Paul Mundt5a1603b2008-06-12 16:29:55 +090094 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070095 return 0;
96
97 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070098
99 /*
100 * If the allocator sets PageSlab, we know the pointer came from
101 * kmalloc().
102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (PageSlab(page))
104 return ksize(objp);
105
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700106 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000107 * If it's not a compound page, see if we have a matching VMA
108 * region. This test is intentionally done in reverse order,
109 * so if there's no VMA, we still fall through and hand back
110 * PAGE_SIZE for 0-order pages.
111 */
112 if (!PageCompound(page)) {
113 struct vm_area_struct *vma;
114
115 vma = find_vma(current->mm, (unsigned long)objp);
116 if (vma)
117 return vma->vm_end - vma->vm_start;
118 }
119
120 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700121 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900122 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700123 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900124 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Nick Pigginb291f002008-10-18 20:26:44 -0700127int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Hugh Dickins4266c972009-09-23 17:05:53 +0100128 unsigned long start, int nr_pages, unsigned int foll_flags,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200129 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700131 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700132 unsigned long vm_flags;
133 int i;
134
135 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700136 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700137 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700138 vm_flags = (foll_flags & FOLL_WRITE) ?
139 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
140 vm_flags &= (foll_flags & FOLL_FORCE) ?
141 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Peter Zijlstra9d737772009-06-25 11:58:55 +0200143 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000144 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700145 if (!vma)
146 goto finish_or_fault;
147
148 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700149 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
150 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700151 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (pages) {
154 pages[i] = virt_to_page(start);
155 if (pages[i])
156 page_cache_get(pages[i]);
157 }
158 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700159 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000160 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
David Howells7b4d5b82006-09-27 01:50:18 -0700162
163 return i;
164
165finish_or_fault:
166 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Nick Pigginb291f002008-10-18 20:26:44 -0700168
Nick Pigginb291f002008-10-18 20:26:44 -0700169/*
170 * get a list of pages in an address range belonging to the specified process
171 * and indicate the VMA that covers each page
172 * - this is potentially dodgy as we may end incrementing the page count of a
173 * slab page or a secondary page from a compound page
174 * - don't permit access to VMAs that don't support it, such as I/O mappings
175 */
176int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200177 unsigned long start, int nr_pages, int write, int force,
Nick Pigginb291f002008-10-18 20:26:44 -0700178 struct page **pages, struct vm_area_struct **vmas)
179{
180 int flags = 0;
181
182 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700183 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700184 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700185 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700186
Peter Zijlstra9d737772009-06-25 11:58:55 +0200187 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
Nick Pigginb291f002008-10-18 20:26:44 -0700188}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000189EXPORT_SYMBOL(get_user_pages);
190
Paul Mundtdfc2f912009-06-26 04:31:57 +0900191/**
192 * follow_pfn - look up PFN at a user virtual address
193 * @vma: memory mapping
194 * @address: user virtual address
195 * @pfn: location to store found PFN
196 *
197 * Only IO mappings and raw PFN mappings are allowed.
198 *
199 * Returns zero and the pfn at @pfn on success, -ve otherwise.
200 */
201int follow_pfn(struct vm_area_struct *vma, unsigned long address,
202 unsigned long *pfn)
203{
204 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
205 return -EINVAL;
206
207 *pfn = address >> PAGE_SHIFT;
208 return 0;
209}
210EXPORT_SYMBOL(follow_pfn);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212DEFINE_RWLOCK(vmlist_lock);
213struct vm_struct *vmlist;
214
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800215void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 kfree(addr);
218}
Paul Mundtb5073172007-07-21 04:37:25 -0700219EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Al Virodd0fc662005-10-07 07:46:04 +0100221void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200224 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
225 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Nick Piggin84097512006-03-22 00:08:34 -0800227 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
Paul Mundtb5073172007-07-21 04:37:25 -0700229EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Paul Mundtf905bc42008-02-04 22:29:59 -0800231void *vmalloc_user(unsigned long size)
232{
233 void *ret;
234
235 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
236 PAGE_KERNEL);
237 if (ret) {
238 struct vm_area_struct *vma;
239
240 down_write(&current->mm->mmap_sem);
241 vma = find_vma(current->mm, (unsigned long)ret);
242 if (vma)
243 vma->vm_flags |= VM_USERMAP;
244 up_write(&current->mm->mmap_sem);
245 }
246
247 return ret;
248}
249EXPORT_SYMBOL(vmalloc_user);
250
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800251struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 return virt_to_page(addr);
254}
Paul Mundtb5073172007-07-21 04:37:25 -0700255EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800257unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 return page_to_pfn(virt_to_page(addr));
260}
Paul Mundtb5073172007-07-21 04:37:25 -0700261EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263long vread(char *buf, char *addr, unsigned long count)
264{
265 memcpy(buf, addr, count);
266 return count;
267}
268
269long vwrite(char *buf, char *addr, unsigned long count)
270{
271 /* Don't allow overflow */
272 if ((unsigned long) addr + count < count)
273 count = -(unsigned long) addr;
274
275 memcpy(addr, buf, count);
276 return(count);
277}
278
279/*
280 * vmalloc - allocate virtually continguos memory
281 *
282 * @size: allocation size
283 *
284 * Allocate enough pages to cover @size from the page level
285 * allocator and map them into continguos kernel virtual space.
286 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200287 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * use __vmalloc() instead.
289 */
290void *vmalloc(unsigned long size)
291{
292 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
293}
Andrew Mortonf6138882006-02-28 16:59:18 -0800294EXPORT_SYMBOL(vmalloc);
295
296void *vmalloc_node(unsigned long size, int node)
297{
298 return vmalloc(size);
299}
300EXPORT_SYMBOL(vmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Paul Mundt1af446e2008-08-04 16:01:47 +0900302#ifndef PAGE_KERNEL_EXEC
303# define PAGE_KERNEL_EXEC PAGE_KERNEL
304#endif
305
306/**
307 * vmalloc_exec - allocate virtually contiguous, executable memory
308 * @size: allocation size
309 *
310 * Kernel-internal function to allocate enough pages to cover @size
311 * the page level allocator and map them into contiguous and
312 * executable kernel virtual space.
313 *
314 * For tight control over page level allocator and protection flags
315 * use __vmalloc() instead.
316 */
317
318void *vmalloc_exec(unsigned long size)
319{
320 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
321}
322
Paul Mundtb5073172007-07-21 04:37:25 -0700323/**
324 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * @size: allocation size
326 *
327 * Allocate enough 32bit PA addressable pages to cover @size from the
328 * page level allocator and map them into continguos kernel virtual space.
329 */
330void *vmalloc_32(unsigned long size)
331{
332 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
333}
Paul Mundtb5073172007-07-21 04:37:25 -0700334EXPORT_SYMBOL(vmalloc_32);
335
336/**
337 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
338 * @size: allocation size
339 *
340 * The resulting memory area is 32bit addressable and zeroed so it can be
341 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800342 *
343 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
344 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700345 */
346void *vmalloc_32_user(unsigned long size)
347{
Paul Mundtf905bc42008-02-04 22:29:59 -0800348 /*
349 * We'll have to sort out the ZONE_DMA bits for 64-bit,
350 * but for now this can simply use vmalloc_user() directly.
351 */
352 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700353}
354EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
357{
358 BUG();
359 return NULL;
360}
Paul Mundtb5073172007-07-21 04:37:25 -0700361EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800363void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 BUG();
366}
Paul Mundtb5073172007-07-21 04:37:25 -0700367EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Paul Mundteb6434d2009-01-21 17:45:47 +0900369void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
370{
371 BUG();
372 return NULL;
373}
374EXPORT_SYMBOL(vm_map_ram);
375
376void vm_unmap_ram(const void *mem, unsigned int count)
377{
378 BUG();
379}
380EXPORT_SYMBOL(vm_unmap_ram);
381
382void vm_unmap_aliases(void)
383{
384}
385EXPORT_SYMBOL_GPL(vm_unmap_aliases);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700388 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
389 * have one.
390 */
391void __attribute__((weak)) vmalloc_sync_all(void)
392{
393}
394
Paul Mundtb5073172007-07-21 04:37:25 -0700395int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
396 struct page *page)
397{
398 return -EINVAL;
399}
400EXPORT_SYMBOL(vm_insert_page);
401
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700402/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * sys_brk() for the most part doesn't need the global kernel
404 * lock, except when an application is doing something nasty
405 * like trying to un-brk an area that has already been mapped
406 * to a regular file. in this case, the unmapping will need
407 * to invoke file system routines that need the global lock.
408 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100409SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct mm_struct *mm = current->mm;
412
413 if (brk < mm->start_brk || brk > mm->context.end_brk)
414 return mm->brk;
415
416 if (mm->brk == brk)
417 return mm->brk;
418
419 /*
420 * Always allow shrinking brk
421 */
422 if (brk <= mm->brk) {
423 mm->brk = brk;
424 return brk;
425 }
426
427 /*
428 * Ok, looks good - let it rip.
429 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000430 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return mm->brk = brk;
432}
433
David Howells8feae132009-01-08 12:04:47 +0000434/*
435 * initialise the VMA and region record slabs
436 */
437void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700439 int ret;
440
441 ret = percpu_counter_init(&vm_committed_as, 0);
442 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700443 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
David Howells8feae132009-01-08 12:04:47 +0000446/*
447 * validate the region tree
448 * - the caller must hold the region lock
449 */
450#ifdef CONFIG_DEBUG_NOMMU_REGIONS
451static noinline void validate_nommu_regions(void)
452{
453 struct vm_region *region, *last;
454 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
David Howells8feae132009-01-08 12:04:47 +0000456 lastp = rb_first(&nommu_region_tree);
457 if (!lastp)
458 return;
459
460 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700461 BUG_ON(unlikely(last->vm_end <= last->vm_start));
462 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000463
464 while ((p = rb_next(lastp))) {
465 region = rb_entry(p, struct vm_region, vm_rb);
466 last = rb_entry(lastp, struct vm_region, vm_rb);
467
David Howells33e5d7692009-04-02 16:56:32 -0700468 BUG_ON(unlikely(region->vm_end <= region->vm_start));
469 BUG_ON(unlikely(region->vm_top < region->vm_end));
470 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000471
472 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474}
David Howells8feae132009-01-08 12:04:47 +0000475#else
David Howells33e5d7692009-04-02 16:56:32 -0700476static void validate_nommu_regions(void)
477{
478}
David Howells8feae132009-01-08 12:04:47 +0000479#endif
480
481/*
482 * add a region into the global tree
483 */
484static void add_nommu_region(struct vm_region *region)
485{
486 struct vm_region *pregion;
487 struct rb_node **p, *parent;
488
489 validate_nommu_regions();
490
David Howells8feae132009-01-08 12:04:47 +0000491 parent = NULL;
492 p = &nommu_region_tree.rb_node;
493 while (*p) {
494 parent = *p;
495 pregion = rb_entry(parent, struct vm_region, vm_rb);
496 if (region->vm_start < pregion->vm_start)
497 p = &(*p)->rb_left;
498 else if (region->vm_start > pregion->vm_start)
499 p = &(*p)->rb_right;
500 else if (pregion == region)
501 return;
502 else
503 BUG();
504 }
505
506 rb_link_node(&region->vm_rb, parent, p);
507 rb_insert_color(&region->vm_rb, &nommu_region_tree);
508
509 validate_nommu_regions();
510}
511
512/*
513 * delete a region from the global tree
514 */
515static void delete_nommu_region(struct vm_region *region)
516{
517 BUG_ON(!nommu_region_tree.rb_node);
518
519 validate_nommu_regions();
520 rb_erase(&region->vm_rb, &nommu_region_tree);
521 validate_nommu_regions();
522}
523
524/*
525 * free a contiguous series of pages
526 */
527static void free_page_series(unsigned long from, unsigned long to)
528{
529 for (; from < to; from += PAGE_SIZE) {
530 struct page *page = virt_to_page(from);
531
532 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700533 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000534 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700535 kdebug("free page %p: refcount not one: %d",
536 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000537 put_page(page);
538 }
539}
540
541/*
542 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700543 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000544 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000545 * will equal vm_start
546 */
547static void __put_nommu_region(struct vm_region *region)
548 __releases(nommu_region_sem)
549{
David Howells1e2ae592010-01-15 17:01:33 -0800550 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000551
552 BUG_ON(!nommu_region_tree.rb_node);
553
David Howells1e2ae592010-01-15 17:01:33 -0800554 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000555 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000556 delete_nommu_region(region);
557 up_write(&nommu_region_sem);
558
559 if (region->vm_file)
560 fput(region->vm_file);
561
562 /* IO memory and memory shared directly out of the pagecache
563 * from ramfs/tmpfs mustn't be released here */
564 if (region->vm_flags & VM_MAPPED_COPY) {
565 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000566 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000567 }
568 kmem_cache_free(vm_region_jar, region);
569 } else {
570 up_write(&nommu_region_sem);
571 }
572}
573
574/*
575 * release a reference to a region
576 */
577static void put_nommu_region(struct vm_region *region)
578{
579 down_write(&nommu_region_sem);
580 __put_nommu_region(region);
581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
David Howells30340972006-09-27 01:50:20 -0700583/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700584 * update protection on a vma
585 */
586static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
587{
588#ifdef CONFIG_MPU
589 struct mm_struct *mm = vma->vm_mm;
590 long start = vma->vm_start & PAGE_MASK;
591 while (start < vma->vm_end) {
592 protect_page(mm, start, flags);
593 start += PAGE_SIZE;
594 }
595 update_protections(mm);
596#endif
597}
598
599/*
David Howells30340972006-09-27 01:50:20 -0700600 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000601 * and tree and add to the address space's page tree also if not an anonymous
602 * page
David Howells30340972006-09-27 01:50:20 -0700603 * - should be called with mm->mmap_sem held writelocked
604 */
David Howells8feae132009-01-08 12:04:47 +0000605static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700606{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700607 struct vm_area_struct *pvma, **pp, *next;
David Howells8feae132009-01-08 12:04:47 +0000608 struct address_space *mapping;
609 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700610
David Howells8feae132009-01-08 12:04:47 +0000611 kenter(",%p", vma);
612
613 BUG_ON(!vma->vm_region);
614
615 mm->map_count++;
616 vma->vm_mm = mm;
617
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700618 protect_vma(vma, vma->vm_flags);
619
David Howells8feae132009-01-08 12:04:47 +0000620 /* add the VMA to the mapping */
621 if (vma->vm_file) {
622 mapping = vma->vm_file->f_mapping;
623
624 flush_dcache_mmap_lock(mapping);
625 vma_prio_tree_insert(vma, &mapping->i_mmap);
626 flush_dcache_mmap_unlock(mapping);
627 }
628
629 /* add the VMA to the tree */
630 parent = NULL;
631 p = &mm->mm_rb.rb_node;
632 while (*p) {
633 parent = *p;
634 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
635
636 /* sort by: start addr, end addr, VMA struct addr in that order
637 * (the latter is necessary as we may get identical VMAs) */
638 if (vma->vm_start < pvma->vm_start)
639 p = &(*p)->rb_left;
640 else if (vma->vm_start > pvma->vm_start)
641 p = &(*p)->rb_right;
642 else if (vma->vm_end < pvma->vm_end)
643 p = &(*p)->rb_left;
644 else if (vma->vm_end > pvma->vm_end)
645 p = &(*p)->rb_right;
646 else if (vma < pvma)
647 p = &(*p)->rb_left;
648 else if (vma > pvma)
649 p = &(*p)->rb_right;
650 else
651 BUG();
652 }
653
654 rb_link_node(&vma->vm_rb, parent, p);
655 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
656
657 /* add VMA to the VMA list also */
658 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
659 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700660 break;
David Howells8feae132009-01-08 12:04:47 +0000661 if (pvma->vm_start < vma->vm_start)
662 continue;
663 if (pvma->vm_end < vma->vm_end)
664 break;
665 }
David Howells30340972006-09-27 01:50:20 -0700666
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700667 next = *pp;
David Howells8feae132009-01-08 12:04:47 +0000668 *pp = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700669 vma->vm_next = next;
670 if (next)
671 next->vm_prev = vma;
David Howells8feae132009-01-08 12:04:47 +0000672}
673
674/*
675 * delete a VMA from its owning mm_struct and address space
676 */
677static void delete_vma_from_mm(struct vm_area_struct *vma)
678{
679 struct vm_area_struct **pp;
680 struct address_space *mapping;
681 struct mm_struct *mm = vma->vm_mm;
682
683 kenter("%p", vma);
684
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700685 protect_vma(vma, 0);
686
David Howells8feae132009-01-08 12:04:47 +0000687 mm->map_count--;
688 if (mm->mmap_cache == vma)
689 mm->mmap_cache = NULL;
690
691 /* remove the VMA from the mapping */
692 if (vma->vm_file) {
693 mapping = vma->vm_file->f_mapping;
694
695 flush_dcache_mmap_lock(mapping);
696 vma_prio_tree_remove(vma, &mapping->i_mmap);
697 flush_dcache_mmap_unlock(mapping);
698 }
699
700 /* remove from the MM's tree and list */
701 rb_erase(&vma->vm_rb, &mm->mm_rb);
702 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
703 if (*pp == vma) {
704 *pp = vma->vm_next;
705 break;
706 }
707 }
708
709 vma->vm_mm = NULL;
710}
711
712/*
713 * destroy a VMA record
714 */
715static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
716{
717 kenter("%p", vma);
718 if (vma->vm_ops && vma->vm_ops->close)
719 vma->vm_ops->close(vma);
720 if (vma->vm_file) {
721 fput(vma->vm_file);
722 if (vma->vm_flags & VM_EXECUTABLE)
723 removed_exe_file_vma(mm);
724 }
725 put_nommu_region(vma->vm_region);
726 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700727}
728
729/*
730 * look up the first VMA in which addr resides, NULL if none
731 * - should be called with mm->mmap_sem at least held readlocked
732 */
733struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
734{
David Howells8feae132009-01-08 12:04:47 +0000735 struct vm_area_struct *vma;
736 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700737
David Howells8feae132009-01-08 12:04:47 +0000738 /* check the cache first */
739 vma = mm->mmap_cache;
740 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
741 return vma;
742
743 /* trawl the tree (there may be multiple mappings in which addr
744 * resides) */
745 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
746 vma = rb_entry(n, struct vm_area_struct, vm_rb);
747 if (vma->vm_start > addr)
748 return NULL;
749 if (vma->vm_end > addr) {
750 mm->mmap_cache = vma;
751 return vma;
752 }
David Howells30340972006-09-27 01:50:20 -0700753 }
754
David Howells30340972006-09-27 01:50:20 -0700755 return NULL;
756}
757EXPORT_SYMBOL(find_vma);
758
759/*
David Howells930e6522006-09-27 01:50:22 -0700760 * find a VMA
761 * - we don't extend stack VMAs under NOMMU conditions
762 */
763struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
764{
David Howells7561e8c2010-03-25 16:48:38 +0000765 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700766}
767
David Howells8feae132009-01-08 12:04:47 +0000768/*
769 * expand a stack to a given address
770 * - not supported under NOMMU conditions
771 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700772int expand_stack(struct vm_area_struct *vma, unsigned long address)
773{
774 return -ENOMEM;
775}
776
David Howells930e6522006-09-27 01:50:22 -0700777/*
David Howells6fa5f802006-09-27 01:50:21 -0700778 * look up the first VMA exactly that exactly matches addr
779 * - should be called with mm->mmap_sem at least held readlocked
780 */
David Howells8feae132009-01-08 12:04:47 +0000781static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
782 unsigned long addr,
783 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000786 struct rb_node *n = mm->mm_rb.rb_node;
787 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David Howells8feae132009-01-08 12:04:47 +0000789 /* check the cache first */
790 vma = mm->mmap_cache;
791 if (vma && vma->vm_start == addr && vma->vm_end == end)
792 return vma;
793
794 /* trawl the tree (there may be multiple mappings in which addr
795 * resides) */
796 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000798 if (vma->vm_start < addr)
799 continue;
800 if (vma->vm_start > addr)
801 return NULL;
802 if (vma->vm_end == end) {
803 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return vma;
David Howells8feae132009-01-08 12:04:47 +0000805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 return NULL;
809}
810
David Howells30340972006-09-27 01:50:20 -0700811/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * determine whether a mapping should be permitted and, if so, what sort of
813 * mapping we're capable of supporting
814 */
815static int validate_mmap_request(struct file *file,
816 unsigned long addr,
817 unsigned long len,
818 unsigned long prot,
819 unsigned long flags,
820 unsigned long pgoff,
821 unsigned long *_capabilities)
822{
David Howells8feae132009-01-08 12:04:47 +0000823 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned long reqprot = prot;
825 int ret;
826
827 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100828 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 printk(KERN_DEBUG
830 "%d: Can't do fixed-address/overlay mmap of RAM\n",
831 current->pid);
832 return -EINVAL;
833 }
834
835 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
836 (flags & MAP_TYPE) != MAP_SHARED)
837 return -EINVAL;
838
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000839 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EINVAL;
841
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000842 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000843 rlen = PAGE_ALIGN(len);
844 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000845 return -ENOMEM;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000848 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000849 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (file) {
852 /* validate file mapping requests */
853 struct address_space *mapping;
854
855 /* files must support mmap */
856 if (!file->f_op || !file->f_op->mmap)
857 return -ENODEV;
858
859 /* work out if what we've got could possibly be shared
860 * - we support chardevs that provide their own "memory"
861 * - we support files/blockdevs that are memory backed
862 */
863 mapping = file->f_mapping;
864 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800865 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 capabilities = 0;
868 if (mapping && mapping->backing_dev_info)
869 capabilities = mapping->backing_dev_info->capabilities;
870
871 if (!capabilities) {
872 /* no explicit capabilities set, so assume some
873 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800874 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 case S_IFREG:
876 case S_IFBLK:
877 capabilities = BDI_CAP_MAP_COPY;
878 break;
879
880 case S_IFCHR:
881 capabilities =
882 BDI_CAP_MAP_DIRECT |
883 BDI_CAP_READ_MAP |
884 BDI_CAP_WRITE_MAP;
885 break;
886
887 default:
888 return -EINVAL;
889 }
890 }
891
892 /* eliminate any capabilities that we can't support on this
893 * device */
894 if (!file->f_op->get_unmapped_area)
895 capabilities &= ~BDI_CAP_MAP_DIRECT;
896 if (!file->f_op->read)
897 capabilities &= ~BDI_CAP_MAP_COPY;
898
Graff Yang28d7a6a2009-08-18 14:11:17 -0700899 /* The file shall have been opened with read permission. */
900 if (!(file->f_mode & FMODE_READ))
901 return -EACCES;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (flags & MAP_SHARED) {
904 /* do checks for writing, appending and locking */
905 if ((prot & PROT_WRITE) &&
906 !(file->f_mode & FMODE_WRITE))
907 return -EACCES;
908
Josef Sipeke9536ae2006-12-08 02:37:21 -0800909 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 (file->f_mode & FMODE_WRITE))
911 return -EACCES;
912
Josef Sipeke9536ae2006-12-08 02:37:21 -0800913 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -EAGAIN;
915
916 if (!(capabilities & BDI_CAP_MAP_DIRECT))
917 return -ENODEV;
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /* we mustn't privatise shared mappings */
920 capabilities &= ~BDI_CAP_MAP_COPY;
921 }
922 else {
923 /* we're going to read the file into private memory we
924 * allocate */
925 if (!(capabilities & BDI_CAP_MAP_COPY))
926 return -ENODEV;
927
928 /* we don't permit a private writable mapping to be
929 * shared with the backing device */
930 if (prot & PROT_WRITE)
931 capabilities &= ~BDI_CAP_MAP_DIRECT;
932 }
933
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700934 if (capabilities & BDI_CAP_MAP_DIRECT) {
935 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
936 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
937 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
938 ) {
939 capabilities &= ~BDI_CAP_MAP_DIRECT;
940 if (flags & MAP_SHARED) {
941 printk(KERN_WARNING
942 "MAP_SHARED not completely supported on !MMU\n");
943 return -EINVAL;
944 }
945 }
946 }
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* handle executable mappings and implied executable
949 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800950 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (prot & PROT_EXEC)
952 return -EPERM;
953 }
954 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
955 /* handle implication of PROT_EXEC by PROT_READ */
956 if (current->personality & READ_IMPLIES_EXEC) {
957 if (capabilities & BDI_CAP_EXEC_MAP)
958 prot |= PROT_EXEC;
959 }
960 }
961 else if ((prot & PROT_READ) &&
962 (prot & PROT_EXEC) &&
963 !(capabilities & BDI_CAP_EXEC_MAP)
964 ) {
965 /* backing file is not executable, try to copy */
966 capabilities &= ~BDI_CAP_MAP_DIRECT;
967 }
968 }
969 else {
970 /* anonymous mappings are always memory backed and can be
971 * privately mapped
972 */
973 capabilities = BDI_CAP_MAP_COPY;
974
975 /* handle PROT_EXEC implication by PROT_READ */
976 if ((prot & PROT_READ) &&
977 (current->personality & READ_IMPLIES_EXEC))
978 prot |= PROT_EXEC;
979 }
980
981 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -0400982 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (ret < 0)
984 return ret;
985
986 /* looks okay */
987 *_capabilities = capabilities;
988 return 0;
989}
990
991/*
992 * we've determined that we can make the mapping, now translate what we
993 * now know into VMA flags
994 */
995static unsigned long determine_vm_flags(struct file *file,
996 unsigned long prot,
997 unsigned long flags,
998 unsigned long capabilities)
999{
1000 unsigned long vm_flags;
1001
1002 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* vm_flags |= mm->def_flags; */
1004
1005 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1006 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001007 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (file && !(prot & PROT_WRITE))
1009 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* overlay a shareable mapping on the backing device or inode
1012 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1013 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001014 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001016 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
1019 /* refuse to let anyone share private mappings with this process if
1020 * it's being traced - otherwise breakpoints set in it may interfere
1021 * with another untraced process
1022 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001023 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 vm_flags &= ~VM_MAYSHARE;
1025
1026 return vm_flags;
1027}
1028
1029/*
David Howells8feae132009-01-08 12:04:47 +00001030 * set up a shared mapping on a file (the driver or filesystem provides and
1031 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
David Howells8feae132009-01-08 12:04:47 +00001033static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 int ret;
1036
1037 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001038 if (ret == 0) {
1039 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001040 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (ret != -ENOSYS)
1043 return ret;
1044
David Howells3fa30462010-03-23 13:35:21 -07001045 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1046 * opposed to tried but failed) so we can only give a suitable error as
1047 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -ENODEV;
1049}
1050
1051/*
1052 * set up a private mapping or an anonymous shared mapping
1053 */
David Howells8feae132009-01-08 12:04:47 +00001054static int do_mmap_private(struct vm_area_struct *vma,
1055 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001056 unsigned long len,
1057 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
David Howells8feae132009-01-08 12:04:47 +00001059 struct page *pages;
1060 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 void *base;
David Howells8feae132009-01-08 12:04:47 +00001062 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* invoke the file's mapping function so that it can keep track of
1065 * shared mappings on devices or memory
1066 * - VM_MAYSHARE will be set if it may attempt to share
1067 */
David Howells645d83c2009-09-24 15:13:10 +01001068 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001070 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001072 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1073 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001076 if (ret != -ENOSYS)
1077 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* getting an ENOSYS error indicates that direct mmap isn't
1080 * possible (as opposed to tried but failed) so we'll try to
1081 * make a private copy of the data and map that instead */
1082 }
1083
David Howells8feae132009-01-08 12:04:47 +00001084 rlen = PAGE_ALIGN(len);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* allocate some memory to hold the mapping
1087 * - note that this may not return a page-aligned address if the object
1088 * we're allocating is smaller than a page
1089 */
David Howells8feae132009-01-08 12:04:47 +00001090 order = get_order(rlen);
1091 kdebug("alloc order %d for %lx", order, len);
1092
1093 pages = alloc_pages(GFP_KERNEL, order);
1094 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto enomem;
1096
David Howells8feae132009-01-08 12:04:47 +00001097 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001098 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
David Howells8feae132009-01-08 12:04:47 +00001100 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001101
1102 /* we allocated a power-of-2 sized page set, so we may want to trim off
1103 * the excess */
1104 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1105 while (total > point) {
1106 order = ilog2(total - point);
1107 n = 1 << order;
1108 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001109 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001110 total -= n;
1111 set_page_refcounted(pages + total);
1112 __free_pages(pages + total, order);
1113 }
David Howells8feae132009-01-08 12:04:47 +00001114 }
1115
David Howells8feae132009-01-08 12:04:47 +00001116 for (point = 1; point < total; point++)
1117 set_page_refcounted(&pages[point]);
1118
1119 base = page_address(pages);
1120 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1121 region->vm_start = (unsigned long) base;
1122 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001123 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001124
1125 vma->vm_start = region->vm_start;
1126 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (vma->vm_file) {
1129 /* read the contents of a file into the copy */
1130 mm_segment_t old_fs;
1131 loff_t fpos;
1132
1133 fpos = vma->vm_pgoff;
1134 fpos <<= PAGE_SHIFT;
1135
1136 old_fs = get_fs();
1137 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001138 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 set_fs(old_fs);
1140
1141 if (ret < 0)
1142 goto error_free;
1143
1144 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001145 if (ret < rlen)
1146 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150 return 0;
1151
1152error_free:
David Howells8feae132009-01-08 12:04:47 +00001153 free_page_series(region->vm_start, region->vm_end);
1154 region->vm_start = vma->vm_start = 0;
1155 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001156 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return ret;
1158
1159enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001160 printk("Allocation of length %lu from process %d (%s) failed\n",
1161 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 show_free_areas();
1163 return -ENOMEM;
1164}
1165
1166/*
1167 * handle mapping creation for uClinux
1168 */
1169unsigned long do_mmap_pgoff(struct file *file,
1170 unsigned long addr,
1171 unsigned long len,
1172 unsigned long prot,
1173 unsigned long flags,
1174 unsigned long pgoff)
1175{
David Howells8feae132009-01-08 12:04:47 +00001176 struct vm_area_struct *vma;
1177 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001179 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int ret;
1181
David Howells8feae132009-01-08 12:04:47 +00001182 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* decide whether we should attempt the mapping, and if so what sort of
1185 * mapping */
1186 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1187 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001188 if (ret < 0) {
1189 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return ret;
David Howells8feae132009-01-08 12:04:47 +00001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
David Howells06aab5a2009-09-24 12:33:48 +01001193 /* we ignore the address hint */
1194 addr = 0;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* we've determined that we can make the mapping, now translate what we
1197 * now know into VMA flags */
1198 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1199
David Howells8feae132009-01-08 12:04:47 +00001200 /* we're going to need to record the mapping */
1201 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1202 if (!region)
1203 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
David Howells8feae132009-01-08 12:04:47 +00001205 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1206 if (!vma)
1207 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
David Howells1e2ae592010-01-15 17:01:33 -08001209 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001210 region->vm_flags = vm_flags;
1211 region->vm_pgoff = pgoff;
1212
Rik van Riel5beb4932010-03-05 13:42:07 -08001213 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001214 vma->vm_flags = vm_flags;
1215 vma->vm_pgoff = pgoff;
1216
1217 if (file) {
1218 region->vm_file = file;
1219 get_file(file);
1220 vma->vm_file = file;
1221 get_file(file);
1222 if (vm_flags & VM_EXECUTABLE) {
1223 added_exe_file_vma(current->mm);
1224 vma->vm_mm = current->mm;
1225 }
1226 }
1227
1228 down_write(&nommu_region_sem);
1229
1230 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001232 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 * - shared mappings on character devices and memory backed files are
1234 * permitted to overlap inexactly as far as we are concerned for in
1235 * these cases, sharing is handled in the driver or filesystem rather
1236 * than here
1237 */
1238 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001239 struct vm_region *pregion;
1240 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
David Howells8feae132009-01-08 12:04:47 +00001242 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1243 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001244
David Howells8feae132009-01-08 12:04:47 +00001245 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1246 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
David Howells8feae132009-01-08 12:04:47 +00001248 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 continue;
1250
1251 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001252 if (pregion->vm_file->f_path.dentry->d_inode !=
1253 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 continue;
1255
David Howells8feae132009-01-08 12:04:47 +00001256 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 continue;
1258
David Howells8feae132009-01-08 12:04:47 +00001259 rpglen = pregion->vm_end - pregion->vm_start;
1260 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1261 rpgend = pregion->vm_pgoff + rpglen;
1262 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 continue;
1264
David Howells8feae132009-01-08 12:04:47 +00001265 /* handle inexactly overlapping matches between
1266 * mappings */
1267 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1268 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1269 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1271 goto sharing_violation;
1272 continue;
1273 }
1274
David Howells8feae132009-01-08 12:04:47 +00001275 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001276 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001277 vma->vm_region = pregion;
1278 start = pregion->vm_start;
1279 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1280 vma->vm_start = start;
1281 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
David Howells8feae132009-01-08 12:04:47 +00001283 if (pregion->vm_flags & VM_MAPPED_COPY) {
1284 kdebug("share copy");
1285 vma->vm_flags |= VM_MAPPED_COPY;
1286 } else {
1287 kdebug("share mmap");
1288 ret = do_mmap_shared_file(vma);
1289 if (ret < 0) {
1290 vma->vm_region = NULL;
1291 vma->vm_start = 0;
1292 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001293 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001294 pregion = NULL;
1295 goto error_just_free;
1296 }
1297 }
1298 fput(region->vm_file);
1299 kmem_cache_free(vm_region_jar, region);
1300 region = pregion;
1301 result = start;
1302 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* obtain the address at which to make a shared mapping
1306 * - this is the hook for quasi-memory character devices to
1307 * tell us the location of a shared mapping
1308 */
David Howells645d83c2009-09-24 15:13:10 +01001309 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 addr = file->f_op->get_unmapped_area(file, addr, len,
1311 pgoff, flags);
1312 if (IS_ERR((void *) addr)) {
1313 ret = addr;
1314 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001315 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* the driver refused to tell us where to site
1318 * the mapping so we'll have to attempt to copy
1319 * it */
1320 ret = (unsigned long) -ENODEV;
1321 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001322 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001325 } else {
1326 vma->vm_start = region->vm_start = addr;
1327 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 }
1330 }
1331
David Howells8feae132009-01-08 12:04:47 +00001332 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
David Howells645d83c2009-09-24 15:13:10 +01001334 /* set up the mapping
1335 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001338 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 else
David Howells645d83c2009-09-24 15:13:10 +01001340 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001342 goto error_just_free;
1343 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001344
Jie Zhangea637632009-12-14 18:00:02 -08001345 /* clear anonymous mappings that don't ask for uninitialized data */
1346 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1347 memset((void *)region->vm_start, 0,
1348 region->vm_end - region->vm_start);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001351 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 current->mm->total_vm += len >> PAGE_SHIFT;
1354
David Howells8feae132009-01-08 12:04:47 +00001355share:
1356 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Mike Frysingercfe79c02010-01-06 17:23:23 +00001358 /* we flush the region from the icache only when the first executable
1359 * mapping of it is made */
1360 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1361 flush_icache_range(region->vm_start, region->vm_end);
1362 region->vm_icache_flushed = true;
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Mike Frysingercfe79c02010-01-06 17:23:23 +00001365 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
David Howells8feae132009-01-08 12:04:47 +00001367 kleave(" = %lx", result);
1368 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
David Howells8feae132009-01-08 12:04:47 +00001370error_just_free:
1371 up_write(&nommu_region_sem);
1372error:
David Howells89a86402009-10-30 13:13:26 +00001373 if (region->vm_file)
1374 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001375 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001376 if (vma->vm_file)
1377 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001378 if (vma->vm_flags & VM_EXECUTABLE)
1379 removed_exe_file_vma(vma->vm_mm);
1380 kmem_cache_free(vm_area_cachep, vma);
1381 kleave(" = %d", ret);
1382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
David Howells8feae132009-01-08 12:04:47 +00001384sharing_violation:
1385 up_write(&nommu_region_sem);
1386 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1387 ret = -EINVAL;
1388 goto error;
1389
1390error_getting_vma:
1391 kmem_cache_free(vm_region_jar, region);
1392 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1393 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 len, current->pid);
1395 show_free_areas();
1396 return -ENOMEM;
1397
David Howells8feae132009-01-08 12:04:47 +00001398error_getting_region:
1399 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1400 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 len, current->pid);
1402 show_free_areas();
1403 return -ENOMEM;
1404}
Paul Mundtb5073172007-07-21 04:37:25 -07001405EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001407SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1408 unsigned long, prot, unsigned long, flags,
1409 unsigned long, fd, unsigned long, pgoff)
1410{
1411 struct file *file = NULL;
1412 unsigned long retval = -EBADF;
1413
1414 if (!(flags & MAP_ANONYMOUS)) {
1415 file = fget(fd);
1416 if (!file)
1417 goto out;
1418 }
1419
1420 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1421
1422 down_write(&current->mm->mmap_sem);
1423 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1424 up_write(&current->mm->mmap_sem);
1425
1426 if (file)
1427 fput(file);
1428out:
1429 return retval;
1430}
1431
Christoph Hellwiga4679372010-03-10 15:21:15 -08001432#ifdef __ARCH_WANT_SYS_OLD_MMAP
1433struct mmap_arg_struct {
1434 unsigned long addr;
1435 unsigned long len;
1436 unsigned long prot;
1437 unsigned long flags;
1438 unsigned long fd;
1439 unsigned long offset;
1440};
1441
1442SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1443{
1444 struct mmap_arg_struct a;
1445
1446 if (copy_from_user(&a, arg, sizeof(a)))
1447 return -EFAULT;
1448 if (a.offset & ~PAGE_MASK)
1449 return -EINVAL;
1450
1451 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1452 a.offset >> PAGE_SHIFT);
1453}
1454#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/*
David Howells8feae132009-01-08 12:04:47 +00001457 * split a vma into two pieces at address 'addr', a new vma is allocated either
1458 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
David Howells8feae132009-01-08 12:04:47 +00001460int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1461 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
David Howells8feae132009-01-08 12:04:47 +00001463 struct vm_area_struct *new;
1464 struct vm_region *region;
1465 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
David Howells8feae132009-01-08 12:04:47 +00001467 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
David Howells779c1022010-01-15 17:01:34 -08001469 /* we're only permitted to split anonymous regions (these should have
1470 * only a single usage on the region) */
1471 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001472 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
David Howells8feae132009-01-08 12:04:47 +00001474 if (mm->map_count >= sysctl_max_map_count)
1475 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
David Howells8feae132009-01-08 12:04:47 +00001477 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1478 if (!region)
1479 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
David Howells8feae132009-01-08 12:04:47 +00001481 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1482 if (!new) {
1483 kmem_cache_free(vm_region_jar, region);
1484 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
David Howells8feae132009-01-08 12:04:47 +00001486
1487 /* most fields are the same, copy all, and then fixup */
1488 *new = *vma;
1489 *region = *vma->vm_region;
1490 new->vm_region = region;
1491
1492 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1493
1494 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001495 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001496 } else {
1497 region->vm_start = new->vm_start = addr;
1498 region->vm_pgoff = new->vm_pgoff += npages;
1499 }
1500
1501 if (new->vm_ops && new->vm_ops->open)
1502 new->vm_ops->open(new);
1503
1504 delete_vma_from_mm(vma);
1505 down_write(&nommu_region_sem);
1506 delete_nommu_region(vma->vm_region);
1507 if (new_below) {
1508 vma->vm_region->vm_start = vma->vm_start = addr;
1509 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1510 } else {
1511 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001512 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001513 }
1514 add_nommu_region(vma->vm_region);
1515 add_nommu_region(new->vm_region);
1516 up_write(&nommu_region_sem);
1517 add_vma_to_mm(mm, vma);
1518 add_vma_to_mm(mm, new);
1519 return 0;
1520}
1521
1522/*
1523 * shrink a VMA by removing the specified chunk from either the beginning or
1524 * the end
1525 */
1526static int shrink_vma(struct mm_struct *mm,
1527 struct vm_area_struct *vma,
1528 unsigned long from, unsigned long to)
1529{
1530 struct vm_region *region;
1531
1532 kenter("");
1533
1534 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1535 * and list */
1536 delete_vma_from_mm(vma);
1537 if (from > vma->vm_start)
1538 vma->vm_end = from;
1539 else
1540 vma->vm_start = to;
1541 add_vma_to_mm(mm, vma);
1542
1543 /* cut the backing region down to size */
1544 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001545 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001546
1547 down_write(&nommu_region_sem);
1548 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001549 if (from > region->vm_start) {
1550 to = region->vm_top;
1551 region->vm_top = region->vm_end = from;
1552 } else {
David Howells8feae132009-01-08 12:04:47 +00001553 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001554 }
David Howells8feae132009-01-08 12:04:47 +00001555 add_nommu_region(region);
1556 up_write(&nommu_region_sem);
1557
1558 free_page_series(from, to);
1559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
David Howells30340972006-09-27 01:50:20 -07001562/*
1563 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001564 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1565 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001566 */
David Howells8feae132009-01-08 12:04:47 +00001567int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
David Howells8feae132009-01-08 12:04:47 +00001569 struct vm_area_struct *vma;
1570 struct rb_node *rb;
1571 unsigned long end = start + len;
1572 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
David Howells8feae132009-01-08 12:04:47 +00001574 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
David Howells8feae132009-01-08 12:04:47 +00001576 if (len == 0)
1577 return -EINVAL;
1578
1579 /* find the first potentially overlapping VMA */
1580 vma = find_vma(mm, start);
1581 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001582 static int limit = 0;
1583 if (limit < 5) {
1584 printk(KERN_WARNING
1585 "munmap of memory not mmapped by process %d"
1586 " (%s): 0x%lx-0x%lx\n",
1587 current->pid, current->comm,
1588 start, start + len - 1);
1589 limit++;
1590 }
David Howells8feae132009-01-08 12:04:47 +00001591 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
David Howells8feae132009-01-08 12:04:47 +00001594 /* we're allowed to split an anonymous VMA but not a file-backed one */
1595 if (vma->vm_file) {
1596 do {
1597 if (start > vma->vm_start) {
1598 kleave(" = -EINVAL [miss]");
1599 return -EINVAL;
1600 }
1601 if (end == vma->vm_end)
1602 goto erase_whole_vma;
1603 rb = rb_next(&vma->vm_rb);
1604 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1605 } while (rb);
1606 kleave(" = -EINVAL [split file]");
1607 return -EINVAL;
1608 } else {
1609 /* the chunk must be a subset of the VMA found */
1610 if (start == vma->vm_start && end == vma->vm_end)
1611 goto erase_whole_vma;
1612 if (start < vma->vm_start || end > vma->vm_end) {
1613 kleave(" = -EINVAL [superset]");
1614 return -EINVAL;
1615 }
1616 if (start & ~PAGE_MASK) {
1617 kleave(" = -EINVAL [unaligned start]");
1618 return -EINVAL;
1619 }
1620 if (end != vma->vm_end && end & ~PAGE_MASK) {
1621 kleave(" = -EINVAL [unaligned split]");
1622 return -EINVAL;
1623 }
1624 if (start != vma->vm_start && end != vma->vm_end) {
1625 ret = split_vma(mm, vma, start, 1);
1626 if (ret < 0) {
1627 kleave(" = %d [split]", ret);
1628 return ret;
1629 }
1630 }
1631 return shrink_vma(mm, vma, start, end);
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
David Howells8feae132009-01-08 12:04:47 +00001634erase_whole_vma:
1635 delete_vma_from_mm(vma);
1636 delete_vma(mm, vma);
1637 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return 0;
1639}
Paul Mundtb5073172007-07-21 04:37:25 -07001640EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001642SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001643{
1644 int ret;
1645 struct mm_struct *mm = current->mm;
1646
1647 down_write(&mm->mmap_sem);
1648 ret = do_munmap(mm, addr, len);
1649 up_write(&mm->mmap_sem);
1650 return ret;
1651}
1652
1653/*
David Howells8feae132009-01-08 12:04:47 +00001654 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001655 */
David Howells8feae132009-01-08 12:04:47 +00001656void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
David Howells8feae132009-01-08 12:04:47 +00001658 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
David Howells8feae132009-01-08 12:04:47 +00001660 if (!mm)
1661 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
David Howells8feae132009-01-08 12:04:47 +00001663 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
David Howells8feae132009-01-08 12:04:47 +00001665 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
David Howells8feae132009-01-08 12:04:47 +00001667 while ((vma = mm->mmap)) {
1668 mm->mmap = vma->vm_next;
1669 delete_vma_from_mm(vma);
1670 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
David Howells8feae132009-01-08 12:04:47 +00001672
1673 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676unsigned long do_brk(unsigned long addr, unsigned long len)
1677{
1678 return -ENOMEM;
1679}
1680
1681/*
David Howells6fa5f802006-09-27 01:50:21 -07001682 * expand (or shrink) an existing mapping, potentially moving it at the same
1683 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 *
David Howells6fa5f802006-09-27 01:50:21 -07001685 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001686 * as long as it stays within the region allocated by do_mmap_private() and the
1687 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 *
David Howells6fa5f802006-09-27 01:50:21 -07001689 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 */
1691unsigned long do_mremap(unsigned long addr,
1692 unsigned long old_len, unsigned long new_len,
1693 unsigned long flags, unsigned long new_addr)
1694{
David Howells6fa5f802006-09-27 01:50:21 -07001695 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001698 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return (unsigned long) -EINVAL;
1700
David Howells8feae132009-01-08 12:04:47 +00001701 if (addr & ~PAGE_MASK)
1702 return -EINVAL;
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (flags & MREMAP_FIXED && new_addr != addr)
1705 return (unsigned long) -EINVAL;
1706
David Howells8feae132009-01-08 12:04:47 +00001707 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001708 if (!vma)
1709 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
David Howells6fa5f802006-09-27 01:50:21 -07001711 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return (unsigned long) -EFAULT;
1713
David Howells6fa5f802006-09-27 01:50:21 -07001714 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return (unsigned long) -EPERM;
1716
David Howells8feae132009-01-08 12:04:47 +00001717 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return (unsigned long) -ENOMEM;
1719
1720 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001721 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001722 return vma->vm_start;
1723}
Paul Mundtb5073172007-07-21 04:37:25 -07001724EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001725
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001726SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1727 unsigned long, new_len, unsigned long, flags,
1728 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001729{
1730 unsigned long ret;
1731
1732 down_write(&current->mm->mmap_sem);
1733 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1734 up_write(&current->mm->mmap_sem);
1735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
Linus Torvalds6aab3412005-11-28 14:34:23 -08001738struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001739 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 return NULL;
1742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1745 unsigned long to, unsigned long size, pgprot_t prot)
1746{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001747 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
Luke Yang22c4af42006-07-14 00:24:09 -07001750EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Paul Mundtf905bc42008-02-04 22:29:59 -08001752int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1753 unsigned long pgoff)
1754{
1755 unsigned int size = vma->vm_end - vma->vm_start;
1756
1757 if (!(vma->vm_flags & VM_USERMAP))
1758 return -EINVAL;
1759
1760 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1761 vma->vm_end = vma->vm_start + size;
1762
1763 return 0;
1764}
1765EXPORT_SYMBOL(remap_vmalloc_range);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1768{
1769}
1770
1771unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1772 unsigned long len, unsigned long pgoff, unsigned long flags)
1773{
1774 return -ENOMEM;
1775}
1776
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001777void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779}
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781void unmap_mapping_range(struct address_space *mapping,
1782 loff_t const holebegin, loff_t const holelen,
1783 int even_cows)
1784{
1785}
Luke Yang22c4af42006-07-14 00:24:09 -07001786EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788/*
1789 * Check that a process has enough memory to allocate a new virtual
1790 * mapping. 0 means there is enough memory for the allocation to
1791 * succeed and -ENOMEM implies there is not.
1792 *
1793 * We currently support three overcommit policies, which are set via the
1794 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1795 *
1796 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1797 * Additional code 2002 Jul 20 by Robert Love.
1798 *
1799 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1800 *
1801 * Note this is a helper function intended to be used by LSMs which
1802 * wish to use this logic.
1803 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001804int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 unsigned long free, allowed;
1807
1808 vm_acct_memory(pages);
1809
1810 /*
1811 * Sometimes we want to use more memory than we have
1812 */
1813 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1814 return 0;
1815
1816 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1817 unsigned long n;
1818
Christoph Lameter347ce432006-06-30 01:55:35 -07001819 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 free += nr_swap_pages;
1821
1822 /*
1823 * Any slabs which are created with the
1824 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1825 * which are reclaimable, under pressure. The dentry
1826 * cache and most inode caches should fall into this
1827 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001828 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /*
1831 * Leave the last 3% for root
1832 */
1833 if (!cap_sys_admin)
1834 free -= free / 32;
1835
1836 if (free > pages)
1837 return 0;
1838
1839 /*
1840 * nr_free_pages() is very expensive on large systems,
1841 * only call if we're about to fail.
1842 */
1843 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001844
1845 /*
1846 * Leave reserved pages. The pages are not for anonymous pages.
1847 */
1848 if (n <= totalreserve_pages)
1849 goto error;
1850 else
1851 n -= totalreserve_pages;
1852
1853 /*
1854 * Leave the last 3% for root
1855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (!cap_sys_admin)
1857 n -= n / 32;
1858 free += n;
1859
1860 if (free > pages)
1861 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001862
1863 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
1866 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1867 /*
1868 * Leave the last 3% for root
1869 */
1870 if (!cap_sys_admin)
1871 allowed -= allowed / 32;
1872 allowed += total_swap_pages;
1873
1874 /* Don't let a single process grow too big:
1875 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001876 if (mm)
1877 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001879 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001881
Hideo AOKId5ddc792006-04-10 22:53:01 -07001882error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 vm_unacct_memory(pages);
1884
1885 return -ENOMEM;
1886}
1887
1888int in_gate_area_no_task(unsigned long addr)
1889{
1890 return 0;
1891}
David Howellsb0e15192006-01-06 00:11:42 -08001892
Nick Piggind0217ac2007-07-19 01:47:03 -07001893int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001894{
1895 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001896 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001897}
Paul Mundtb5073172007-07-21 04:37:25 -07001898EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001899
1900/*
1901 * Access another process' address space.
1902 * - source/target buffer must be kernel space
1903 */
1904int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1905{
David Howells0ec76a12006-09-27 01:50:15 -07001906 struct vm_area_struct *vma;
1907 struct mm_struct *mm;
1908
1909 if (addr + len < addr)
1910 return 0;
1911
1912 mm = get_task_mm(tsk);
1913 if (!mm)
1914 return 0;
1915
1916 down_read(&mm->mmap_sem);
1917
1918 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001919 vma = find_vma(mm, addr);
1920 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001921 /* don't overrun this mapping */
1922 if (addr + len >= vma->vm_end)
1923 len = vma->vm_end - addr;
1924
1925 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001926 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001927 copy_to_user_page(vma, NULL, addr,
1928 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001929 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001930 copy_from_user_page(vma, NULL, addr,
1931 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001932 else
1933 len = 0;
1934 } else {
1935 len = 0;
1936 }
1937
1938 up_read(&mm->mmap_sem);
1939 mmput(mm);
1940 return len;
1941}
David Howells7e660872010-01-15 17:01:39 -08001942
1943/**
1944 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1945 * @inode: The inode to check
1946 * @size: The current filesize of the inode
1947 * @newsize: The proposed filesize of the inode
1948 *
1949 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1950 * make sure that that any outstanding VMAs aren't broken and then shrink the
1951 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1952 * automatically grant mappings that are too large.
1953 */
1954int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1955 size_t newsize)
1956{
1957 struct vm_area_struct *vma;
1958 struct prio_tree_iter iter;
1959 struct vm_region *region;
1960 pgoff_t low, high;
1961 size_t r_size, r_top;
1962
1963 low = newsize >> PAGE_SHIFT;
1964 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1965
1966 down_write(&nommu_region_sem);
1967
1968 /* search for VMAs that fall within the dead zone */
1969 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
1970 low, high) {
1971 /* found one - only interested if it's shared out of the page
1972 * cache */
1973 if (vma->vm_flags & VM_SHARED) {
1974 up_write(&nommu_region_sem);
1975 return -ETXTBSY; /* not quite true, but near enough */
1976 }
1977 }
1978
1979 /* reduce any regions that overlap the dead zone - if in existence,
1980 * these will be pointed to by VMAs that don't overlap the dead zone
1981 *
1982 * we don't check for any regions that start beyond the EOF as there
1983 * shouldn't be any
1984 */
1985 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
1986 0, ULONG_MAX) {
1987 if (!(vma->vm_flags & VM_SHARED))
1988 continue;
1989
1990 region = vma->vm_region;
1991 r_size = region->vm_top - region->vm_start;
1992 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1993
1994 if (r_top > newsize) {
1995 region->vm_top -= r_top - newsize;
1996 if (region->vm_end > region->vm_top)
1997 region->vm_end = region->vm_top;
1998 }
1999 }
2000
2001 up_write(&nommu_region_sem);
2002 return 0;
2003}