blob: efa9a380335eed137c30d5c762c5a5cdae469b9c [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{
David Howells8feae132009-01-08 12:04:47 +0000607 struct vm_area_struct *pvma, **pp;
608 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
David Howells8feae132009-01-08 12:04:47 +0000667 vma->vm_next = *pp;
668 *pp = vma;
669}
670
671/*
672 * delete a VMA from its owning mm_struct and address space
673 */
674static void delete_vma_from_mm(struct vm_area_struct *vma)
675{
676 struct vm_area_struct **pp;
677 struct address_space *mapping;
678 struct mm_struct *mm = vma->vm_mm;
679
680 kenter("%p", vma);
681
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700682 protect_vma(vma, 0);
683
David Howells8feae132009-01-08 12:04:47 +0000684 mm->map_count--;
685 if (mm->mmap_cache == vma)
686 mm->mmap_cache = NULL;
687
688 /* remove the VMA from the mapping */
689 if (vma->vm_file) {
690 mapping = vma->vm_file->f_mapping;
691
692 flush_dcache_mmap_lock(mapping);
693 vma_prio_tree_remove(vma, &mapping->i_mmap);
694 flush_dcache_mmap_unlock(mapping);
695 }
696
697 /* remove from the MM's tree and list */
698 rb_erase(&vma->vm_rb, &mm->mm_rb);
699 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
700 if (*pp == vma) {
701 *pp = vma->vm_next;
702 break;
703 }
704 }
705
706 vma->vm_mm = NULL;
707}
708
709/*
710 * destroy a VMA record
711 */
712static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
713{
714 kenter("%p", vma);
715 if (vma->vm_ops && vma->vm_ops->close)
716 vma->vm_ops->close(vma);
717 if (vma->vm_file) {
718 fput(vma->vm_file);
719 if (vma->vm_flags & VM_EXECUTABLE)
720 removed_exe_file_vma(mm);
721 }
722 put_nommu_region(vma->vm_region);
723 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700724}
725
726/*
727 * look up the first VMA in which addr resides, NULL if none
728 * - should be called with mm->mmap_sem at least held readlocked
729 */
730struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
731{
David Howells8feae132009-01-08 12:04:47 +0000732 struct vm_area_struct *vma;
733 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700734
David Howells8feae132009-01-08 12:04:47 +0000735 /* check the cache first */
736 vma = mm->mmap_cache;
737 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
738 return vma;
739
740 /* trawl the tree (there may be multiple mappings in which addr
741 * resides) */
742 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
743 vma = rb_entry(n, struct vm_area_struct, vm_rb);
744 if (vma->vm_start > addr)
745 return NULL;
746 if (vma->vm_end > addr) {
747 mm->mmap_cache = vma;
748 return vma;
749 }
David Howells30340972006-09-27 01:50:20 -0700750 }
751
David Howells30340972006-09-27 01:50:20 -0700752 return NULL;
753}
754EXPORT_SYMBOL(find_vma);
755
756/*
David Howells930e6522006-09-27 01:50:22 -0700757 * find a VMA
758 * - we don't extend stack VMAs under NOMMU conditions
759 */
760struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
761{
David Howells7561e8c2010-03-25 16:48:38 +0000762 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700763}
764
David Howells8feae132009-01-08 12:04:47 +0000765/*
766 * expand a stack to a given address
767 * - not supported under NOMMU conditions
768 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700769int expand_stack(struct vm_area_struct *vma, unsigned long address)
770{
771 return -ENOMEM;
772}
773
David Howells930e6522006-09-27 01:50:22 -0700774/*
David Howells6fa5f802006-09-27 01:50:21 -0700775 * look up the first VMA exactly that exactly matches addr
776 * - should be called with mm->mmap_sem at least held readlocked
777 */
David Howells8feae132009-01-08 12:04:47 +0000778static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
779 unsigned long addr,
780 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000783 struct rb_node *n = mm->mm_rb.rb_node;
784 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -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 == end)
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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000795 if (vma->vm_start < addr)
796 continue;
797 if (vma->vm_start > addr)
798 return NULL;
799 if (vma->vm_end == end) {
800 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return vma;
David Howells8feae132009-01-08 12:04:47 +0000802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805 return NULL;
806}
807
David Howells30340972006-09-27 01:50:20 -0700808/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * determine whether a mapping should be permitted and, if so, what sort of
810 * mapping we're capable of supporting
811 */
812static int validate_mmap_request(struct file *file,
813 unsigned long addr,
814 unsigned long len,
815 unsigned long prot,
816 unsigned long flags,
817 unsigned long pgoff,
818 unsigned long *_capabilities)
819{
David Howells8feae132009-01-08 12:04:47 +0000820 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 unsigned long reqprot = prot;
822 int ret;
823
824 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100825 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 printk(KERN_DEBUG
827 "%d: Can't do fixed-address/overlay mmap of RAM\n",
828 current->pid);
829 return -EINVAL;
830 }
831
832 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
833 (flags & MAP_TYPE) != MAP_SHARED)
834 return -EINVAL;
835
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000836 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EINVAL;
838
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000839 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000840 rlen = PAGE_ALIGN(len);
841 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000842 return -ENOMEM;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000845 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000846 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (file) {
849 /* validate file mapping requests */
850 struct address_space *mapping;
851
852 /* files must support mmap */
853 if (!file->f_op || !file->f_op->mmap)
854 return -ENODEV;
855
856 /* work out if what we've got could possibly be shared
857 * - we support chardevs that provide their own "memory"
858 * - we support files/blockdevs that are memory backed
859 */
860 mapping = file->f_mapping;
861 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800862 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 capabilities = 0;
865 if (mapping && mapping->backing_dev_info)
866 capabilities = mapping->backing_dev_info->capabilities;
867
868 if (!capabilities) {
869 /* no explicit capabilities set, so assume some
870 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800871 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 case S_IFREG:
873 case S_IFBLK:
874 capabilities = BDI_CAP_MAP_COPY;
875 break;
876
877 case S_IFCHR:
878 capabilities =
879 BDI_CAP_MAP_DIRECT |
880 BDI_CAP_READ_MAP |
881 BDI_CAP_WRITE_MAP;
882 break;
883
884 default:
885 return -EINVAL;
886 }
887 }
888
889 /* eliminate any capabilities that we can't support on this
890 * device */
891 if (!file->f_op->get_unmapped_area)
892 capabilities &= ~BDI_CAP_MAP_DIRECT;
893 if (!file->f_op->read)
894 capabilities &= ~BDI_CAP_MAP_COPY;
895
Graff Yang28d7a6a2009-08-18 14:11:17 -0700896 /* The file shall have been opened with read permission. */
897 if (!(file->f_mode & FMODE_READ))
898 return -EACCES;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (flags & MAP_SHARED) {
901 /* do checks for writing, appending and locking */
902 if ((prot & PROT_WRITE) &&
903 !(file->f_mode & FMODE_WRITE))
904 return -EACCES;
905
Josef Sipeke9536ae2006-12-08 02:37:21 -0800906 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 (file->f_mode & FMODE_WRITE))
908 return -EACCES;
909
Josef Sipeke9536ae2006-12-08 02:37:21 -0800910 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -EAGAIN;
912
913 if (!(capabilities & BDI_CAP_MAP_DIRECT))
914 return -ENODEV;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* we mustn't privatise shared mappings */
917 capabilities &= ~BDI_CAP_MAP_COPY;
918 }
919 else {
920 /* we're going to read the file into private memory we
921 * allocate */
922 if (!(capabilities & BDI_CAP_MAP_COPY))
923 return -ENODEV;
924
925 /* we don't permit a private writable mapping to be
926 * shared with the backing device */
927 if (prot & PROT_WRITE)
928 capabilities &= ~BDI_CAP_MAP_DIRECT;
929 }
930
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700931 if (capabilities & BDI_CAP_MAP_DIRECT) {
932 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
933 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
934 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
935 ) {
936 capabilities &= ~BDI_CAP_MAP_DIRECT;
937 if (flags & MAP_SHARED) {
938 printk(KERN_WARNING
939 "MAP_SHARED not completely supported on !MMU\n");
940 return -EINVAL;
941 }
942 }
943 }
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* handle executable mappings and implied executable
946 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800947 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (prot & PROT_EXEC)
949 return -EPERM;
950 }
951 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
952 /* handle implication of PROT_EXEC by PROT_READ */
953 if (current->personality & READ_IMPLIES_EXEC) {
954 if (capabilities & BDI_CAP_EXEC_MAP)
955 prot |= PROT_EXEC;
956 }
957 }
958 else if ((prot & PROT_READ) &&
959 (prot & PROT_EXEC) &&
960 !(capabilities & BDI_CAP_EXEC_MAP)
961 ) {
962 /* backing file is not executable, try to copy */
963 capabilities &= ~BDI_CAP_MAP_DIRECT;
964 }
965 }
966 else {
967 /* anonymous mappings are always memory backed and can be
968 * privately mapped
969 */
970 capabilities = BDI_CAP_MAP_COPY;
971
972 /* handle PROT_EXEC implication by PROT_READ */
973 if ((prot & PROT_READ) &&
974 (current->personality & READ_IMPLIES_EXEC))
975 prot |= PROT_EXEC;
976 }
977
978 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -0400979 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (ret < 0)
981 return ret;
982
983 /* looks okay */
984 *_capabilities = capabilities;
985 return 0;
986}
987
988/*
989 * we've determined that we can make the mapping, now translate what we
990 * now know into VMA flags
991 */
992static unsigned long determine_vm_flags(struct file *file,
993 unsigned long prot,
994 unsigned long flags,
995 unsigned long capabilities)
996{
997 unsigned long vm_flags;
998
999 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* vm_flags |= mm->def_flags; */
1001
1002 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1003 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001004 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (file && !(prot & PROT_WRITE))
1006 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001007 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* overlay a shareable mapping on the backing device or inode
1009 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1010 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001011 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001013 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 /* refuse to let anyone share private mappings with this process if
1017 * it's being traced - otherwise breakpoints set in it may interfere
1018 * with another untraced process
1019 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001020 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 vm_flags &= ~VM_MAYSHARE;
1022
1023 return vm_flags;
1024}
1025
1026/*
David Howells8feae132009-01-08 12:04:47 +00001027 * set up a shared mapping on a file (the driver or filesystem provides and
1028 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
David Howells8feae132009-01-08 12:04:47 +00001030static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int ret;
1033
1034 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001035 if (ret == 0) {
1036 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001037 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (ret != -ENOSYS)
1040 return ret;
1041
David Howells3fa30462010-03-23 13:35:21 -07001042 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1043 * opposed to tried but failed) so we can only give a suitable error as
1044 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return -ENODEV;
1046}
1047
1048/*
1049 * set up a private mapping or an anonymous shared mapping
1050 */
David Howells8feae132009-01-08 12:04:47 +00001051static int do_mmap_private(struct vm_area_struct *vma,
1052 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001053 unsigned long len,
1054 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
David Howells8feae132009-01-08 12:04:47 +00001056 struct page *pages;
1057 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 void *base;
David Howells8feae132009-01-08 12:04:47 +00001059 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* invoke the file's mapping function so that it can keep track of
1062 * shared mappings on devices or memory
1063 * - VM_MAYSHARE will be set if it may attempt to share
1064 */
David Howells645d83c2009-09-24 15:13:10 +01001065 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001067 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001069 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1070 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001073 if (ret != -ENOSYS)
1074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /* getting an ENOSYS error indicates that direct mmap isn't
1077 * possible (as opposed to tried but failed) so we'll try to
1078 * make a private copy of the data and map that instead */
1079 }
1080
David Howells8feae132009-01-08 12:04:47 +00001081 rlen = PAGE_ALIGN(len);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* allocate some memory to hold the mapping
1084 * - note that this may not return a page-aligned address if the object
1085 * we're allocating is smaller than a page
1086 */
David Howells8feae132009-01-08 12:04:47 +00001087 order = get_order(rlen);
1088 kdebug("alloc order %d for %lx", order, len);
1089
1090 pages = alloc_pages(GFP_KERNEL, order);
1091 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto enomem;
1093
David Howells8feae132009-01-08 12:04:47 +00001094 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001095 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
David Howells8feae132009-01-08 12:04:47 +00001097 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001098
1099 /* we allocated a power-of-2 sized page set, so we may want to trim off
1100 * the excess */
1101 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1102 while (total > point) {
1103 order = ilog2(total - point);
1104 n = 1 << order;
1105 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001106 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001107 total -= n;
1108 set_page_refcounted(pages + total);
1109 __free_pages(pages + total, order);
1110 }
David Howells8feae132009-01-08 12:04:47 +00001111 }
1112
David Howells8feae132009-01-08 12:04:47 +00001113 for (point = 1; point < total; point++)
1114 set_page_refcounted(&pages[point]);
1115
1116 base = page_address(pages);
1117 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1118 region->vm_start = (unsigned long) base;
1119 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001120 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001121
1122 vma->vm_start = region->vm_start;
1123 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (vma->vm_file) {
1126 /* read the contents of a file into the copy */
1127 mm_segment_t old_fs;
1128 loff_t fpos;
1129
1130 fpos = vma->vm_pgoff;
1131 fpos <<= PAGE_SHIFT;
1132
1133 old_fs = get_fs();
1134 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001135 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 set_fs(old_fs);
1137
1138 if (ret < 0)
1139 goto error_free;
1140
1141 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001142 if (ret < rlen)
1143 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 return 0;
1148
1149error_free:
David Howells8feae132009-01-08 12:04:47 +00001150 free_page_series(region->vm_start, region->vm_end);
1151 region->vm_start = vma->vm_start = 0;
1152 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001153 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return ret;
1155
1156enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001157 printk("Allocation of length %lu from process %d (%s) failed\n",
1158 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 show_free_areas();
1160 return -ENOMEM;
1161}
1162
1163/*
1164 * handle mapping creation for uClinux
1165 */
1166unsigned long do_mmap_pgoff(struct file *file,
1167 unsigned long addr,
1168 unsigned long len,
1169 unsigned long prot,
1170 unsigned long flags,
1171 unsigned long pgoff)
1172{
David Howells8feae132009-01-08 12:04:47 +00001173 struct vm_area_struct *vma;
1174 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001176 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int ret;
1178
David Howells8feae132009-01-08 12:04:47 +00001179 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* decide whether we should attempt the mapping, and if so what sort of
1182 * mapping */
1183 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1184 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001185 if (ret < 0) {
1186 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return ret;
David Howells8feae132009-01-08 12:04:47 +00001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
David Howells06aab5a2009-09-24 12:33:48 +01001190 /* we ignore the address hint */
1191 addr = 0;
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* we've determined that we can make the mapping, now translate what we
1194 * now know into VMA flags */
1195 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1196
David Howells8feae132009-01-08 12:04:47 +00001197 /* we're going to need to record the mapping */
1198 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1199 if (!region)
1200 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
David Howells8feae132009-01-08 12:04:47 +00001202 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1203 if (!vma)
1204 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
David Howells1e2ae592010-01-15 17:01:33 -08001206 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001207 region->vm_flags = vm_flags;
1208 region->vm_pgoff = pgoff;
1209
Rik van Riel5beb4932010-03-05 13:42:07 -08001210 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001211 vma->vm_flags = vm_flags;
1212 vma->vm_pgoff = pgoff;
1213
1214 if (file) {
1215 region->vm_file = file;
1216 get_file(file);
1217 vma->vm_file = file;
1218 get_file(file);
1219 if (vm_flags & VM_EXECUTABLE) {
1220 added_exe_file_vma(current->mm);
1221 vma->vm_mm = current->mm;
1222 }
1223 }
1224
1225 down_write(&nommu_region_sem);
1226
1227 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001229 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 * - shared mappings on character devices and memory backed files are
1231 * permitted to overlap inexactly as far as we are concerned for in
1232 * these cases, sharing is handled in the driver or filesystem rather
1233 * than here
1234 */
1235 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001236 struct vm_region *pregion;
1237 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
David Howells8feae132009-01-08 12:04:47 +00001239 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1240 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001241
David Howells8feae132009-01-08 12:04:47 +00001242 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1243 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
David Howells8feae132009-01-08 12:04:47 +00001245 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 continue;
1247
1248 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001249 if (pregion->vm_file->f_path.dentry->d_inode !=
1250 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 continue;
1252
David Howells8feae132009-01-08 12:04:47 +00001253 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 continue;
1255
David Howells8feae132009-01-08 12:04:47 +00001256 rpglen = pregion->vm_end - pregion->vm_start;
1257 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1258 rpgend = pregion->vm_pgoff + rpglen;
1259 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 continue;
1261
David Howells8feae132009-01-08 12:04:47 +00001262 /* handle inexactly overlapping matches between
1263 * mappings */
1264 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1265 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1266 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1268 goto sharing_violation;
1269 continue;
1270 }
1271
David Howells8feae132009-01-08 12:04:47 +00001272 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001273 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001274 vma->vm_region = pregion;
1275 start = pregion->vm_start;
1276 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1277 vma->vm_start = start;
1278 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
David Howells8feae132009-01-08 12:04:47 +00001280 if (pregion->vm_flags & VM_MAPPED_COPY) {
1281 kdebug("share copy");
1282 vma->vm_flags |= VM_MAPPED_COPY;
1283 } else {
1284 kdebug("share mmap");
1285 ret = do_mmap_shared_file(vma);
1286 if (ret < 0) {
1287 vma->vm_region = NULL;
1288 vma->vm_start = 0;
1289 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001290 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001291 pregion = NULL;
1292 goto error_just_free;
1293 }
1294 }
1295 fput(region->vm_file);
1296 kmem_cache_free(vm_region_jar, region);
1297 region = pregion;
1298 result = start;
1299 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* obtain the address at which to make a shared mapping
1303 * - this is the hook for quasi-memory character devices to
1304 * tell us the location of a shared mapping
1305 */
David Howells645d83c2009-09-24 15:13:10 +01001306 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 addr = file->f_op->get_unmapped_area(file, addr, len,
1308 pgoff, flags);
1309 if (IS_ERR((void *) addr)) {
1310 ret = addr;
1311 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001312 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* the driver refused to tell us where to site
1315 * the mapping so we'll have to attempt to copy
1316 * it */
1317 ret = (unsigned long) -ENODEV;
1318 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001319 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001322 } else {
1323 vma->vm_start = region->vm_start = addr;
1324 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 }
1327 }
1328
David Howells8feae132009-01-08 12:04:47 +00001329 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
David Howells645d83c2009-09-24 15:13:10 +01001331 /* set up the mapping
1332 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001335 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 else
David Howells645d83c2009-09-24 15:13:10 +01001337 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001339 goto error_just_free;
1340 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001341
Jie Zhangea637632009-12-14 18:00:02 -08001342 /* clear anonymous mappings that don't ask for uninitialized data */
1343 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1344 memset((void *)region->vm_start, 0,
1345 region->vm_end - region->vm_start);
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001348 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 current->mm->total_vm += len >> PAGE_SHIFT;
1351
David Howells8feae132009-01-08 12:04:47 +00001352share:
1353 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Mike Frysingercfe79c02010-01-06 17:23:23 +00001355 /* we flush the region from the icache only when the first executable
1356 * mapping of it is made */
1357 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1358 flush_icache_range(region->vm_start, region->vm_end);
1359 region->vm_icache_flushed = true;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Mike Frysingercfe79c02010-01-06 17:23:23 +00001362 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
David Howells8feae132009-01-08 12:04:47 +00001364 kleave(" = %lx", result);
1365 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
David Howells8feae132009-01-08 12:04:47 +00001367error_just_free:
1368 up_write(&nommu_region_sem);
1369error:
David Howells89a86402009-10-30 13:13:26 +00001370 if (region->vm_file)
1371 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001372 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001373 if (vma->vm_file)
1374 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001375 if (vma->vm_flags & VM_EXECUTABLE)
1376 removed_exe_file_vma(vma->vm_mm);
1377 kmem_cache_free(vm_area_cachep, vma);
1378 kleave(" = %d", ret);
1379 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
David Howells8feae132009-01-08 12:04:47 +00001381sharing_violation:
1382 up_write(&nommu_region_sem);
1383 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1384 ret = -EINVAL;
1385 goto error;
1386
1387error_getting_vma:
1388 kmem_cache_free(vm_region_jar, region);
1389 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1390 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 len, current->pid);
1392 show_free_areas();
1393 return -ENOMEM;
1394
David Howells8feae132009-01-08 12:04:47 +00001395error_getting_region:
1396 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1397 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 len, current->pid);
1399 show_free_areas();
1400 return -ENOMEM;
1401}
Paul Mundtb5073172007-07-21 04:37:25 -07001402EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001404SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1405 unsigned long, prot, unsigned long, flags,
1406 unsigned long, fd, unsigned long, pgoff)
1407{
1408 struct file *file = NULL;
1409 unsigned long retval = -EBADF;
1410
1411 if (!(flags & MAP_ANONYMOUS)) {
1412 file = fget(fd);
1413 if (!file)
1414 goto out;
1415 }
1416
1417 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1418
1419 down_write(&current->mm->mmap_sem);
1420 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1421 up_write(&current->mm->mmap_sem);
1422
1423 if (file)
1424 fput(file);
1425out:
1426 return retval;
1427}
1428
Christoph Hellwiga4679372010-03-10 15:21:15 -08001429#ifdef __ARCH_WANT_SYS_OLD_MMAP
1430struct mmap_arg_struct {
1431 unsigned long addr;
1432 unsigned long len;
1433 unsigned long prot;
1434 unsigned long flags;
1435 unsigned long fd;
1436 unsigned long offset;
1437};
1438
1439SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1440{
1441 struct mmap_arg_struct a;
1442
1443 if (copy_from_user(&a, arg, sizeof(a)))
1444 return -EFAULT;
1445 if (a.offset & ~PAGE_MASK)
1446 return -EINVAL;
1447
1448 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1449 a.offset >> PAGE_SHIFT);
1450}
1451#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/*
David Howells8feae132009-01-08 12:04:47 +00001454 * split a vma into two pieces at address 'addr', a new vma is allocated either
1455 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
David Howells8feae132009-01-08 12:04:47 +00001457int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1458 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
David Howells8feae132009-01-08 12:04:47 +00001460 struct vm_area_struct *new;
1461 struct vm_region *region;
1462 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
David Howells8feae132009-01-08 12:04:47 +00001464 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
David Howells779c1022010-01-15 17:01:34 -08001466 /* we're only permitted to split anonymous regions (these should have
1467 * only a single usage on the region) */
1468 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001469 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
David Howells8feae132009-01-08 12:04:47 +00001471 if (mm->map_count >= sysctl_max_map_count)
1472 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
David Howells8feae132009-01-08 12:04:47 +00001474 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1475 if (!region)
1476 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
David Howells8feae132009-01-08 12:04:47 +00001478 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1479 if (!new) {
1480 kmem_cache_free(vm_region_jar, region);
1481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
David Howells8feae132009-01-08 12:04:47 +00001483
1484 /* most fields are the same, copy all, and then fixup */
1485 *new = *vma;
1486 *region = *vma->vm_region;
1487 new->vm_region = region;
1488
1489 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1490
1491 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001492 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001493 } else {
1494 region->vm_start = new->vm_start = addr;
1495 region->vm_pgoff = new->vm_pgoff += npages;
1496 }
1497
1498 if (new->vm_ops && new->vm_ops->open)
1499 new->vm_ops->open(new);
1500
1501 delete_vma_from_mm(vma);
1502 down_write(&nommu_region_sem);
1503 delete_nommu_region(vma->vm_region);
1504 if (new_below) {
1505 vma->vm_region->vm_start = vma->vm_start = addr;
1506 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1507 } else {
1508 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001509 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001510 }
1511 add_nommu_region(vma->vm_region);
1512 add_nommu_region(new->vm_region);
1513 up_write(&nommu_region_sem);
1514 add_vma_to_mm(mm, vma);
1515 add_vma_to_mm(mm, new);
1516 return 0;
1517}
1518
1519/*
1520 * shrink a VMA by removing the specified chunk from either the beginning or
1521 * the end
1522 */
1523static int shrink_vma(struct mm_struct *mm,
1524 struct vm_area_struct *vma,
1525 unsigned long from, unsigned long to)
1526{
1527 struct vm_region *region;
1528
1529 kenter("");
1530
1531 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1532 * and list */
1533 delete_vma_from_mm(vma);
1534 if (from > vma->vm_start)
1535 vma->vm_end = from;
1536 else
1537 vma->vm_start = to;
1538 add_vma_to_mm(mm, vma);
1539
1540 /* cut the backing region down to size */
1541 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001542 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001543
1544 down_write(&nommu_region_sem);
1545 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001546 if (from > region->vm_start) {
1547 to = region->vm_top;
1548 region->vm_top = region->vm_end = from;
1549 } else {
David Howells8feae132009-01-08 12:04:47 +00001550 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001551 }
David Howells8feae132009-01-08 12:04:47 +00001552 add_nommu_region(region);
1553 up_write(&nommu_region_sem);
1554
1555 free_page_series(from, to);
1556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
David Howells30340972006-09-27 01:50:20 -07001559/*
1560 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001561 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1562 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001563 */
David Howells8feae132009-01-08 12:04:47 +00001564int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
David Howells8feae132009-01-08 12:04:47 +00001566 struct vm_area_struct *vma;
1567 struct rb_node *rb;
1568 unsigned long end = start + len;
1569 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
David Howells8feae132009-01-08 12:04:47 +00001571 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
David Howells8feae132009-01-08 12:04:47 +00001573 if (len == 0)
1574 return -EINVAL;
1575
1576 /* find the first potentially overlapping VMA */
1577 vma = find_vma(mm, start);
1578 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001579 static int limit = 0;
1580 if (limit < 5) {
1581 printk(KERN_WARNING
1582 "munmap of memory not mmapped by process %d"
1583 " (%s): 0x%lx-0x%lx\n",
1584 current->pid, current->comm,
1585 start, start + len - 1);
1586 limit++;
1587 }
David Howells8feae132009-01-08 12:04:47 +00001588 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
David Howells8feae132009-01-08 12:04:47 +00001591 /* we're allowed to split an anonymous VMA but not a file-backed one */
1592 if (vma->vm_file) {
1593 do {
1594 if (start > vma->vm_start) {
1595 kleave(" = -EINVAL [miss]");
1596 return -EINVAL;
1597 }
1598 if (end == vma->vm_end)
1599 goto erase_whole_vma;
1600 rb = rb_next(&vma->vm_rb);
1601 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1602 } while (rb);
1603 kleave(" = -EINVAL [split file]");
1604 return -EINVAL;
1605 } else {
1606 /* the chunk must be a subset of the VMA found */
1607 if (start == vma->vm_start && end == vma->vm_end)
1608 goto erase_whole_vma;
1609 if (start < vma->vm_start || end > vma->vm_end) {
1610 kleave(" = -EINVAL [superset]");
1611 return -EINVAL;
1612 }
1613 if (start & ~PAGE_MASK) {
1614 kleave(" = -EINVAL [unaligned start]");
1615 return -EINVAL;
1616 }
1617 if (end != vma->vm_end && end & ~PAGE_MASK) {
1618 kleave(" = -EINVAL [unaligned split]");
1619 return -EINVAL;
1620 }
1621 if (start != vma->vm_start && end != vma->vm_end) {
1622 ret = split_vma(mm, vma, start, 1);
1623 if (ret < 0) {
1624 kleave(" = %d [split]", ret);
1625 return ret;
1626 }
1627 }
1628 return shrink_vma(mm, vma, start, end);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
David Howells8feae132009-01-08 12:04:47 +00001631erase_whole_vma:
1632 delete_vma_from_mm(vma);
1633 delete_vma(mm, vma);
1634 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
Paul Mundtb5073172007-07-21 04:37:25 -07001637EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001639SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001640{
1641 int ret;
1642 struct mm_struct *mm = current->mm;
1643
1644 down_write(&mm->mmap_sem);
1645 ret = do_munmap(mm, addr, len);
1646 up_write(&mm->mmap_sem);
1647 return ret;
1648}
1649
1650/*
David Howells8feae132009-01-08 12:04:47 +00001651 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001652 */
David Howells8feae132009-01-08 12:04:47 +00001653void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
David Howells8feae132009-01-08 12:04:47 +00001655 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
David Howells8feae132009-01-08 12:04:47 +00001657 if (!mm)
1658 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
David Howells8feae132009-01-08 12:04:47 +00001660 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
David Howells8feae132009-01-08 12:04:47 +00001662 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
David Howells8feae132009-01-08 12:04:47 +00001664 while ((vma = mm->mmap)) {
1665 mm->mmap = vma->vm_next;
1666 delete_vma_from_mm(vma);
1667 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
David Howells8feae132009-01-08 12:04:47 +00001669
1670 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673unsigned long do_brk(unsigned long addr, unsigned long len)
1674{
1675 return -ENOMEM;
1676}
1677
1678/*
David Howells6fa5f802006-09-27 01:50:21 -07001679 * expand (or shrink) an existing mapping, potentially moving it at the same
1680 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *
David Howells6fa5f802006-09-27 01:50:21 -07001682 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001683 * as long as it stays within the region allocated by do_mmap_private() and the
1684 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
David Howells6fa5f802006-09-27 01:50:21 -07001686 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
1688unsigned long do_mremap(unsigned long addr,
1689 unsigned long old_len, unsigned long new_len,
1690 unsigned long flags, unsigned long new_addr)
1691{
David Howells6fa5f802006-09-27 01:50:21 -07001692 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001695 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return (unsigned long) -EINVAL;
1697
David Howells8feae132009-01-08 12:04:47 +00001698 if (addr & ~PAGE_MASK)
1699 return -EINVAL;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (flags & MREMAP_FIXED && new_addr != addr)
1702 return (unsigned long) -EINVAL;
1703
David Howells8feae132009-01-08 12:04:47 +00001704 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001705 if (!vma)
1706 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
David Howells6fa5f802006-09-27 01:50:21 -07001708 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return (unsigned long) -EFAULT;
1710
David Howells6fa5f802006-09-27 01:50:21 -07001711 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return (unsigned long) -EPERM;
1713
David Howells8feae132009-01-08 12:04:47 +00001714 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return (unsigned long) -ENOMEM;
1716
1717 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001718 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001719 return vma->vm_start;
1720}
Paul Mundtb5073172007-07-21 04:37:25 -07001721EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001722
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001723SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1724 unsigned long, new_len, unsigned long, flags,
1725 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001726{
1727 unsigned long ret;
1728
1729 down_write(&current->mm->mmap_sem);
1730 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1731 up_write(&current->mm->mmap_sem);
1732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Linus Torvalds6aab3412005-11-28 14:34:23 -08001735struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001736 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 return NULL;
1739}
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1742 unsigned long to, unsigned long size, pgprot_t prot)
1743{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001744 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
Luke Yang22c4af42006-07-14 00:24:09 -07001747EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Paul Mundtf905bc42008-02-04 22:29:59 -08001749int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1750 unsigned long pgoff)
1751{
1752 unsigned int size = vma->vm_end - vma->vm_start;
1753
1754 if (!(vma->vm_flags & VM_USERMAP))
1755 return -EINVAL;
1756
1757 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1758 vma->vm_end = vma->vm_start + size;
1759
1760 return 0;
1761}
1762EXPORT_SYMBOL(remap_vmalloc_range);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1765{
1766}
1767
1768unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1769 unsigned long len, unsigned long pgoff, unsigned long flags)
1770{
1771 return -ENOMEM;
1772}
1773
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001774void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778void unmap_mapping_range(struct address_space *mapping,
1779 loff_t const holebegin, loff_t const holelen,
1780 int even_cows)
1781{
1782}
Luke Yang22c4af42006-07-14 00:24:09 -07001783EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785/*
1786 * Check that a process has enough memory to allocate a new virtual
1787 * mapping. 0 means there is enough memory for the allocation to
1788 * succeed and -ENOMEM implies there is not.
1789 *
1790 * We currently support three overcommit policies, which are set via the
1791 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1792 *
1793 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1794 * Additional code 2002 Jul 20 by Robert Love.
1795 *
1796 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1797 *
1798 * Note this is a helper function intended to be used by LSMs which
1799 * wish to use this logic.
1800 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001801int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 unsigned long free, allowed;
1804
1805 vm_acct_memory(pages);
1806
1807 /*
1808 * Sometimes we want to use more memory than we have
1809 */
1810 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1811 return 0;
1812
1813 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1814 unsigned long n;
1815
Christoph Lameter347ce432006-06-30 01:55:35 -07001816 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 free += nr_swap_pages;
1818
1819 /*
1820 * Any slabs which are created with the
1821 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1822 * which are reclaimable, under pressure. The dentry
1823 * cache and most inode caches should fall into this
1824 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001825 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /*
1828 * Leave the last 3% for root
1829 */
1830 if (!cap_sys_admin)
1831 free -= free / 32;
1832
1833 if (free > pages)
1834 return 0;
1835
1836 /*
1837 * nr_free_pages() is very expensive on large systems,
1838 * only call if we're about to fail.
1839 */
1840 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001841
1842 /*
1843 * Leave reserved pages. The pages are not for anonymous pages.
1844 */
1845 if (n <= totalreserve_pages)
1846 goto error;
1847 else
1848 n -= totalreserve_pages;
1849
1850 /*
1851 * Leave the last 3% for root
1852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (!cap_sys_admin)
1854 n -= n / 32;
1855 free += n;
1856
1857 if (free > pages)
1858 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001859
1860 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
1863 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1864 /*
1865 * Leave the last 3% for root
1866 */
1867 if (!cap_sys_admin)
1868 allowed -= allowed / 32;
1869 allowed += total_swap_pages;
1870
1871 /* Don't let a single process grow too big:
1872 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001873 if (mm)
1874 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001876 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001878
Hideo AOKId5ddc792006-04-10 22:53:01 -07001879error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 vm_unacct_memory(pages);
1881
1882 return -ENOMEM;
1883}
1884
1885int in_gate_area_no_task(unsigned long addr)
1886{
1887 return 0;
1888}
David Howellsb0e15192006-01-06 00:11:42 -08001889
Nick Piggind0217ac2007-07-19 01:47:03 -07001890int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001891{
1892 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001893 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001894}
Paul Mundtb5073172007-07-21 04:37:25 -07001895EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001896
1897/*
1898 * Access another process' address space.
1899 * - source/target buffer must be kernel space
1900 */
1901int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1902{
David Howells0ec76a12006-09-27 01:50:15 -07001903 struct vm_area_struct *vma;
1904 struct mm_struct *mm;
1905
1906 if (addr + len < addr)
1907 return 0;
1908
1909 mm = get_task_mm(tsk);
1910 if (!mm)
1911 return 0;
1912
1913 down_read(&mm->mmap_sem);
1914
1915 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001916 vma = find_vma(mm, addr);
1917 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001918 /* don't overrun this mapping */
1919 if (addr + len >= vma->vm_end)
1920 len = vma->vm_end - addr;
1921
1922 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001923 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001924 copy_to_user_page(vma, NULL, addr,
1925 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001926 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001927 copy_from_user_page(vma, NULL, addr,
1928 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001929 else
1930 len = 0;
1931 } else {
1932 len = 0;
1933 }
1934
1935 up_read(&mm->mmap_sem);
1936 mmput(mm);
1937 return len;
1938}
David Howells7e660872010-01-15 17:01:39 -08001939
1940/**
1941 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1942 * @inode: The inode to check
1943 * @size: The current filesize of the inode
1944 * @newsize: The proposed filesize of the inode
1945 *
1946 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1947 * make sure that that any outstanding VMAs aren't broken and then shrink the
1948 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1949 * automatically grant mappings that are too large.
1950 */
1951int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1952 size_t newsize)
1953{
1954 struct vm_area_struct *vma;
1955 struct prio_tree_iter iter;
1956 struct vm_region *region;
1957 pgoff_t low, high;
1958 size_t r_size, r_top;
1959
1960 low = newsize >> PAGE_SHIFT;
1961 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1962
1963 down_write(&nommu_region_sem);
1964
1965 /* search for VMAs that fall within the dead zone */
1966 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
1967 low, high) {
1968 /* found one - only interested if it's shared out of the page
1969 * cache */
1970 if (vma->vm_flags & VM_SHARED) {
1971 up_write(&nommu_region_sem);
1972 return -ETXTBSY; /* not quite true, but near enough */
1973 }
1974 }
1975
1976 /* reduce any regions that overlap the dead zone - if in existence,
1977 * these will be pointed to by VMAs that don't overlap the dead zone
1978 *
1979 * we don't check for any regions that start beyond the EOF as there
1980 * shouldn't be any
1981 */
1982 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
1983 0, ULONG_MAX) {
1984 if (!(vma->vm_flags & VM_SHARED))
1985 continue;
1986
1987 region = vma->vm_region;
1988 r_size = region->vm_top - region->vm_start;
1989 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1990
1991 if (r_top > newsize) {
1992 region->vm_top -= r_top - newsize;
1993 if (region->vm_end > region->vm_top)
1994 region->vm_end = region->vm_top;
1995 }
1996 }
1997
1998 up_write(&nommu_region_sem);
1999 return 0;
2000}