blob: 30b5c20eec15e6761bad5dd4815e69ebae1d7dc2 [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
Dave Younge1ca7782010-10-26 14:22:06 -0700296/*
297 * vzalloc - allocate virtually continguos memory with zero fill
298 *
299 * @size: allocation size
300 *
301 * Allocate enough pages to cover @size from the page level
302 * allocator and map them into continguos kernel virtual space.
303 * The memory allocated is set to zero.
304 *
305 * For tight control over page level allocator and protection flags
306 * use __vmalloc() instead.
307 */
308void *vzalloc(unsigned long size)
309{
310 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
311 PAGE_KERNEL);
312}
313EXPORT_SYMBOL(vzalloc);
314
315/**
316 * vmalloc_node - allocate memory on a specific node
317 * @size: allocation size
318 * @node: numa node
319 *
320 * Allocate enough pages to cover @size from the page level
321 * allocator and map them into contiguous kernel virtual space.
322 *
323 * For tight control over page level allocator and protection flags
324 * use __vmalloc() instead.
325 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800326void *vmalloc_node(unsigned long size, int node)
327{
328 return vmalloc(size);
329}
Dave Younge1ca7782010-10-26 14:22:06 -0700330
331/**
332 * vzalloc_node - allocate memory on a specific node with zero fill
333 * @size: allocation size
334 * @node: numa node
335 *
336 * Allocate enough pages to cover @size from the page level
337 * allocator and map them into contiguous kernel virtual space.
338 * The memory allocated is set to zero.
339 *
340 * For tight control over page level allocator and protection flags
341 * use __vmalloc() instead.
342 */
343void *vzalloc_node(unsigned long size, int node)
344{
345 return vzalloc(size);
346}
347EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Paul Mundt1af446e2008-08-04 16:01:47 +0900349#ifndef PAGE_KERNEL_EXEC
350# define PAGE_KERNEL_EXEC PAGE_KERNEL
351#endif
352
353/**
354 * vmalloc_exec - allocate virtually contiguous, executable memory
355 * @size: allocation size
356 *
357 * Kernel-internal function to allocate enough pages to cover @size
358 * the page level allocator and map them into contiguous and
359 * executable kernel virtual space.
360 *
361 * For tight control over page level allocator and protection flags
362 * use __vmalloc() instead.
363 */
364
365void *vmalloc_exec(unsigned long size)
366{
367 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
368}
369
Paul Mundtb5073172007-07-21 04:37:25 -0700370/**
371 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * @size: allocation size
373 *
374 * Allocate enough 32bit PA addressable pages to cover @size from the
375 * page level allocator and map them into continguos kernel virtual space.
376 */
377void *vmalloc_32(unsigned long size)
378{
379 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
380}
Paul Mundtb5073172007-07-21 04:37:25 -0700381EXPORT_SYMBOL(vmalloc_32);
382
383/**
384 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
385 * @size: allocation size
386 *
387 * The resulting memory area is 32bit addressable and zeroed so it can be
388 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800389 *
390 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
391 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700392 */
393void *vmalloc_32_user(unsigned long size)
394{
Paul Mundtf905bc42008-02-04 22:29:59 -0800395 /*
396 * We'll have to sort out the ZONE_DMA bits for 64-bit,
397 * but for now this can simply use vmalloc_user() directly.
398 */
399 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700400}
401EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
404{
405 BUG();
406 return NULL;
407}
Paul Mundtb5073172007-07-21 04:37:25 -0700408EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800410void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 BUG();
413}
Paul Mundtb5073172007-07-21 04:37:25 -0700414EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Paul Mundteb6434d2009-01-21 17:45:47 +0900416void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
417{
418 BUG();
419 return NULL;
420}
421EXPORT_SYMBOL(vm_map_ram);
422
423void vm_unmap_ram(const void *mem, unsigned int count)
424{
425 BUG();
426}
427EXPORT_SYMBOL(vm_unmap_ram);
428
429void vm_unmap_aliases(void)
430{
431}
432EXPORT_SYMBOL_GPL(vm_unmap_aliases);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700435 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
436 * have one.
437 */
438void __attribute__((weak)) vmalloc_sync_all(void)
439{
440}
441
Paul Mundtb5073172007-07-21 04:37:25 -0700442int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
443 struct page *page)
444{
445 return -EINVAL;
446}
447EXPORT_SYMBOL(vm_insert_page);
448
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700449/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * sys_brk() for the most part doesn't need the global kernel
451 * lock, except when an application is doing something nasty
452 * like trying to un-brk an area that has already been mapped
453 * to a regular file. in this case, the unmapping will need
454 * to invoke file system routines that need the global lock.
455 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100456SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct mm_struct *mm = current->mm;
459
460 if (brk < mm->start_brk || brk > mm->context.end_brk)
461 return mm->brk;
462
463 if (mm->brk == brk)
464 return mm->brk;
465
466 /*
467 * Always allow shrinking brk
468 */
469 if (brk <= mm->brk) {
470 mm->brk = brk;
471 return brk;
472 }
473
474 /*
475 * Ok, looks good - let it rip.
476 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000477 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return mm->brk = brk;
479}
480
David Howells8feae132009-01-08 12:04:47 +0000481/*
482 * initialise the VMA and region record slabs
483 */
484void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700486 int ret;
487
488 ret = percpu_counter_init(&vm_committed_as, 0);
489 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700490 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Howells8feae132009-01-08 12:04:47 +0000493/*
494 * validate the region tree
495 * - the caller must hold the region lock
496 */
497#ifdef CONFIG_DEBUG_NOMMU_REGIONS
498static noinline void validate_nommu_regions(void)
499{
500 struct vm_region *region, *last;
501 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
David Howells8feae132009-01-08 12:04:47 +0000503 lastp = rb_first(&nommu_region_tree);
504 if (!lastp)
505 return;
506
507 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700508 BUG_ON(unlikely(last->vm_end <= last->vm_start));
509 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000510
511 while ((p = rb_next(lastp))) {
512 region = rb_entry(p, struct vm_region, vm_rb);
513 last = rb_entry(lastp, struct vm_region, vm_rb);
514
David Howells33e5d7692009-04-02 16:56:32 -0700515 BUG_ON(unlikely(region->vm_end <= region->vm_start));
516 BUG_ON(unlikely(region->vm_top < region->vm_end));
517 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000518
519 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521}
David Howells8feae132009-01-08 12:04:47 +0000522#else
David Howells33e5d7692009-04-02 16:56:32 -0700523static void validate_nommu_regions(void)
524{
525}
David Howells8feae132009-01-08 12:04:47 +0000526#endif
527
528/*
529 * add a region into the global tree
530 */
531static void add_nommu_region(struct vm_region *region)
532{
533 struct vm_region *pregion;
534 struct rb_node **p, *parent;
535
536 validate_nommu_regions();
537
David Howells8feae132009-01-08 12:04:47 +0000538 parent = NULL;
539 p = &nommu_region_tree.rb_node;
540 while (*p) {
541 parent = *p;
542 pregion = rb_entry(parent, struct vm_region, vm_rb);
543 if (region->vm_start < pregion->vm_start)
544 p = &(*p)->rb_left;
545 else if (region->vm_start > pregion->vm_start)
546 p = &(*p)->rb_right;
547 else if (pregion == region)
548 return;
549 else
550 BUG();
551 }
552
553 rb_link_node(&region->vm_rb, parent, p);
554 rb_insert_color(&region->vm_rb, &nommu_region_tree);
555
556 validate_nommu_regions();
557}
558
559/*
560 * delete a region from the global tree
561 */
562static void delete_nommu_region(struct vm_region *region)
563{
564 BUG_ON(!nommu_region_tree.rb_node);
565
566 validate_nommu_regions();
567 rb_erase(&region->vm_rb, &nommu_region_tree);
568 validate_nommu_regions();
569}
570
571/*
572 * free a contiguous series of pages
573 */
574static void free_page_series(unsigned long from, unsigned long to)
575{
576 for (; from < to; from += PAGE_SIZE) {
577 struct page *page = virt_to_page(from);
578
579 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700580 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000581 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700582 kdebug("free page %p: refcount not one: %d",
583 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000584 put_page(page);
585 }
586}
587
588/*
589 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700590 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000591 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000592 * will equal vm_start
593 */
594static void __put_nommu_region(struct vm_region *region)
595 __releases(nommu_region_sem)
596{
David Howells1e2ae592010-01-15 17:01:33 -0800597 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000598
599 BUG_ON(!nommu_region_tree.rb_node);
600
David Howells1e2ae592010-01-15 17:01:33 -0800601 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000602 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000603 delete_nommu_region(region);
604 up_write(&nommu_region_sem);
605
606 if (region->vm_file)
607 fput(region->vm_file);
608
609 /* IO memory and memory shared directly out of the pagecache
610 * from ramfs/tmpfs mustn't be released here */
611 if (region->vm_flags & VM_MAPPED_COPY) {
612 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000613 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000614 }
615 kmem_cache_free(vm_region_jar, region);
616 } else {
617 up_write(&nommu_region_sem);
618 }
619}
620
621/*
622 * release a reference to a region
623 */
624static void put_nommu_region(struct vm_region *region)
625{
626 down_write(&nommu_region_sem);
627 __put_nommu_region(region);
628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
David Howells30340972006-09-27 01:50:20 -0700630/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700631 * update protection on a vma
632 */
633static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
634{
635#ifdef CONFIG_MPU
636 struct mm_struct *mm = vma->vm_mm;
637 long start = vma->vm_start & PAGE_MASK;
638 while (start < vma->vm_end) {
639 protect_page(mm, start, flags);
640 start += PAGE_SIZE;
641 }
642 update_protections(mm);
643#endif
644}
645
646/*
David Howells30340972006-09-27 01:50:20 -0700647 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000648 * and tree and add to the address space's page tree also if not an anonymous
649 * page
David Howells30340972006-09-27 01:50:20 -0700650 * - should be called with mm->mmap_sem held writelocked
651 */
David Howells8feae132009-01-08 12:04:47 +0000652static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700653{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700654 struct vm_area_struct *pvma, **pp, *next;
David Howells8feae132009-01-08 12:04:47 +0000655 struct address_space *mapping;
656 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700657
David Howells8feae132009-01-08 12:04:47 +0000658 kenter(",%p", vma);
659
660 BUG_ON(!vma->vm_region);
661
662 mm->map_count++;
663 vma->vm_mm = mm;
664
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700665 protect_vma(vma, vma->vm_flags);
666
David Howells8feae132009-01-08 12:04:47 +0000667 /* add the VMA to the mapping */
668 if (vma->vm_file) {
669 mapping = vma->vm_file->f_mapping;
670
671 flush_dcache_mmap_lock(mapping);
672 vma_prio_tree_insert(vma, &mapping->i_mmap);
673 flush_dcache_mmap_unlock(mapping);
674 }
675
676 /* add the VMA to the tree */
677 parent = NULL;
678 p = &mm->mm_rb.rb_node;
679 while (*p) {
680 parent = *p;
681 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
682
683 /* sort by: start addr, end addr, VMA struct addr in that order
684 * (the latter is necessary as we may get identical VMAs) */
685 if (vma->vm_start < pvma->vm_start)
686 p = &(*p)->rb_left;
687 else if (vma->vm_start > pvma->vm_start)
688 p = &(*p)->rb_right;
689 else if (vma->vm_end < pvma->vm_end)
690 p = &(*p)->rb_left;
691 else if (vma->vm_end > pvma->vm_end)
692 p = &(*p)->rb_right;
693 else if (vma < pvma)
694 p = &(*p)->rb_left;
695 else if (vma > pvma)
696 p = &(*p)->rb_right;
697 else
698 BUG();
699 }
700
701 rb_link_node(&vma->vm_rb, parent, p);
702 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
703
704 /* add VMA to the VMA list also */
705 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
706 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700707 break;
David Howells8feae132009-01-08 12:04:47 +0000708 if (pvma->vm_start < vma->vm_start)
709 continue;
710 if (pvma->vm_end < vma->vm_end)
711 break;
712 }
David Howells30340972006-09-27 01:50:20 -0700713
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700714 next = *pp;
David Howells8feae132009-01-08 12:04:47 +0000715 *pp = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700716 vma->vm_next = next;
717 if (next)
718 next->vm_prev = vma;
David Howells8feae132009-01-08 12:04:47 +0000719}
720
721/*
722 * delete a VMA from its owning mm_struct and address space
723 */
724static void delete_vma_from_mm(struct vm_area_struct *vma)
725{
726 struct vm_area_struct **pp;
727 struct address_space *mapping;
728 struct mm_struct *mm = vma->vm_mm;
729
730 kenter("%p", vma);
731
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700732 protect_vma(vma, 0);
733
David Howells8feae132009-01-08 12:04:47 +0000734 mm->map_count--;
735 if (mm->mmap_cache == vma)
736 mm->mmap_cache = NULL;
737
738 /* remove the VMA from the mapping */
739 if (vma->vm_file) {
740 mapping = vma->vm_file->f_mapping;
741
742 flush_dcache_mmap_lock(mapping);
743 vma_prio_tree_remove(vma, &mapping->i_mmap);
744 flush_dcache_mmap_unlock(mapping);
745 }
746
747 /* remove from the MM's tree and list */
748 rb_erase(&vma->vm_rb, &mm->mm_rb);
749 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
750 if (*pp == vma) {
751 *pp = vma->vm_next;
752 break;
753 }
754 }
755
756 vma->vm_mm = NULL;
757}
758
759/*
760 * destroy a VMA record
761 */
762static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
763{
764 kenter("%p", vma);
765 if (vma->vm_ops && vma->vm_ops->close)
766 vma->vm_ops->close(vma);
767 if (vma->vm_file) {
768 fput(vma->vm_file);
769 if (vma->vm_flags & VM_EXECUTABLE)
770 removed_exe_file_vma(mm);
771 }
772 put_nommu_region(vma->vm_region);
773 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700774}
775
776/*
777 * look up the first VMA in which addr resides, NULL if none
778 * - should be called with mm->mmap_sem at least held readlocked
779 */
780struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
781{
David Howells8feae132009-01-08 12:04:47 +0000782 struct vm_area_struct *vma;
783 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700784
David Howells8feae132009-01-08 12:04:47 +0000785 /* check the cache first */
786 vma = mm->mmap_cache;
787 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
788 return vma;
789
790 /* trawl the tree (there may be multiple mappings in which addr
791 * resides) */
792 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
793 vma = rb_entry(n, struct vm_area_struct, vm_rb);
794 if (vma->vm_start > addr)
795 return NULL;
796 if (vma->vm_end > addr) {
797 mm->mmap_cache = vma;
798 return vma;
799 }
David Howells30340972006-09-27 01:50:20 -0700800 }
801
David Howells30340972006-09-27 01:50:20 -0700802 return NULL;
803}
804EXPORT_SYMBOL(find_vma);
805
806/*
David Howells930e6522006-09-27 01:50:22 -0700807 * find a VMA
808 * - we don't extend stack VMAs under NOMMU conditions
809 */
810struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
811{
David Howells7561e8c2010-03-25 16:48:38 +0000812 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700813}
814
David Howells8feae132009-01-08 12:04:47 +0000815/*
816 * expand a stack to a given address
817 * - not supported under NOMMU conditions
818 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700819int expand_stack(struct vm_area_struct *vma, unsigned long address)
820{
821 return -ENOMEM;
822}
823
David Howells930e6522006-09-27 01:50:22 -0700824/*
David Howells6fa5f802006-09-27 01:50:21 -0700825 * look up the first VMA exactly that exactly matches addr
826 * - should be called with mm->mmap_sem at least held readlocked
827 */
David Howells8feae132009-01-08 12:04:47 +0000828static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
829 unsigned long addr,
830 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000833 struct rb_node *n = mm->mm_rb.rb_node;
834 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
David Howells8feae132009-01-08 12:04:47 +0000836 /* check the cache first */
837 vma = mm->mmap_cache;
838 if (vma && vma->vm_start == addr && vma->vm_end == end)
839 return vma;
840
841 /* trawl the tree (there may be multiple mappings in which addr
842 * resides) */
843 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000845 if (vma->vm_start < addr)
846 continue;
847 if (vma->vm_start > addr)
848 return NULL;
849 if (vma->vm_end == end) {
850 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return vma;
David Howells8feae132009-01-08 12:04:47 +0000852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 return NULL;
856}
857
David Howells30340972006-09-27 01:50:20 -0700858/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * determine whether a mapping should be permitted and, if so, what sort of
860 * mapping we're capable of supporting
861 */
862static int validate_mmap_request(struct file *file,
863 unsigned long addr,
864 unsigned long len,
865 unsigned long prot,
866 unsigned long flags,
867 unsigned long pgoff,
868 unsigned long *_capabilities)
869{
David Howells8feae132009-01-08 12:04:47 +0000870 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unsigned long reqprot = prot;
872 int ret;
873
874 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100875 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 printk(KERN_DEBUG
877 "%d: Can't do fixed-address/overlay mmap of RAM\n",
878 current->pid);
879 return -EINVAL;
880 }
881
882 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
883 (flags & MAP_TYPE) != MAP_SHARED)
884 return -EINVAL;
885
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000886 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EINVAL;
888
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000889 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000890 rlen = PAGE_ALIGN(len);
891 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000892 return -ENOMEM;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000895 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000896 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (file) {
899 /* validate file mapping requests */
900 struct address_space *mapping;
901
902 /* files must support mmap */
903 if (!file->f_op || !file->f_op->mmap)
904 return -ENODEV;
905
906 /* work out if what we've got could possibly be shared
907 * - we support chardevs that provide their own "memory"
908 * - we support files/blockdevs that are memory backed
909 */
910 mapping = file->f_mapping;
911 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800912 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 capabilities = 0;
915 if (mapping && mapping->backing_dev_info)
916 capabilities = mapping->backing_dev_info->capabilities;
917
918 if (!capabilities) {
919 /* no explicit capabilities set, so assume some
920 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800921 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 case S_IFREG:
923 case S_IFBLK:
924 capabilities = BDI_CAP_MAP_COPY;
925 break;
926
927 case S_IFCHR:
928 capabilities =
929 BDI_CAP_MAP_DIRECT |
930 BDI_CAP_READ_MAP |
931 BDI_CAP_WRITE_MAP;
932 break;
933
934 default:
935 return -EINVAL;
936 }
937 }
938
939 /* eliminate any capabilities that we can't support on this
940 * device */
941 if (!file->f_op->get_unmapped_area)
942 capabilities &= ~BDI_CAP_MAP_DIRECT;
943 if (!file->f_op->read)
944 capabilities &= ~BDI_CAP_MAP_COPY;
945
Graff Yang28d7a6a2009-08-18 14:11:17 -0700946 /* The file shall have been opened with read permission. */
947 if (!(file->f_mode & FMODE_READ))
948 return -EACCES;
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (flags & MAP_SHARED) {
951 /* do checks for writing, appending and locking */
952 if ((prot & PROT_WRITE) &&
953 !(file->f_mode & FMODE_WRITE))
954 return -EACCES;
955
Josef Sipeke9536ae2006-12-08 02:37:21 -0800956 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 (file->f_mode & FMODE_WRITE))
958 return -EACCES;
959
Josef Sipeke9536ae2006-12-08 02:37:21 -0800960 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EAGAIN;
962
963 if (!(capabilities & BDI_CAP_MAP_DIRECT))
964 return -ENODEV;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* we mustn't privatise shared mappings */
967 capabilities &= ~BDI_CAP_MAP_COPY;
968 }
969 else {
970 /* we're going to read the file into private memory we
971 * allocate */
972 if (!(capabilities & BDI_CAP_MAP_COPY))
973 return -ENODEV;
974
975 /* we don't permit a private writable mapping to be
976 * shared with the backing device */
977 if (prot & PROT_WRITE)
978 capabilities &= ~BDI_CAP_MAP_DIRECT;
979 }
980
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700981 if (capabilities & BDI_CAP_MAP_DIRECT) {
982 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
983 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
984 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
985 ) {
986 capabilities &= ~BDI_CAP_MAP_DIRECT;
987 if (flags & MAP_SHARED) {
988 printk(KERN_WARNING
989 "MAP_SHARED not completely supported on !MMU\n");
990 return -EINVAL;
991 }
992 }
993 }
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* handle executable mappings and implied executable
996 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800997 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (prot & PROT_EXEC)
999 return -EPERM;
1000 }
1001 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1002 /* handle implication of PROT_EXEC by PROT_READ */
1003 if (current->personality & READ_IMPLIES_EXEC) {
1004 if (capabilities & BDI_CAP_EXEC_MAP)
1005 prot |= PROT_EXEC;
1006 }
1007 }
1008 else if ((prot & PROT_READ) &&
1009 (prot & PROT_EXEC) &&
1010 !(capabilities & BDI_CAP_EXEC_MAP)
1011 ) {
1012 /* backing file is not executable, try to copy */
1013 capabilities &= ~BDI_CAP_MAP_DIRECT;
1014 }
1015 }
1016 else {
1017 /* anonymous mappings are always memory backed and can be
1018 * privately mapped
1019 */
1020 capabilities = BDI_CAP_MAP_COPY;
1021
1022 /* handle PROT_EXEC implication by PROT_READ */
1023 if ((prot & PROT_READ) &&
1024 (current->personality & READ_IMPLIES_EXEC))
1025 prot |= PROT_EXEC;
1026 }
1027
1028 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -04001029 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (ret < 0)
1031 return ret;
1032
1033 /* looks okay */
1034 *_capabilities = capabilities;
1035 return 0;
1036}
1037
1038/*
1039 * we've determined that we can make the mapping, now translate what we
1040 * now know into VMA flags
1041 */
1042static unsigned long determine_vm_flags(struct file *file,
1043 unsigned long prot,
1044 unsigned long flags,
1045 unsigned long capabilities)
1046{
1047 unsigned long vm_flags;
1048
1049 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* vm_flags |= mm->def_flags; */
1051
1052 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1053 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001054 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (file && !(prot & PROT_WRITE))
1056 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001057 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* overlay a shareable mapping on the backing device or inode
1059 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1060 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001061 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001063 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 /* refuse to let anyone share private mappings with this process if
1067 * it's being traced - otherwise breakpoints set in it may interfere
1068 * with another untraced process
1069 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001070 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 vm_flags &= ~VM_MAYSHARE;
1072
1073 return vm_flags;
1074}
1075
1076/*
David Howells8feae132009-01-08 12:04:47 +00001077 * set up a shared mapping on a file (the driver or filesystem provides and
1078 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
David Howells8feae132009-01-08 12:04:47 +00001080static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 int ret;
1083
1084 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001085 if (ret == 0) {
1086 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001087 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (ret != -ENOSYS)
1090 return ret;
1091
David Howells3fa30462010-03-23 13:35:21 -07001092 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1093 * opposed to tried but failed) so we can only give a suitable error as
1094 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -ENODEV;
1096}
1097
1098/*
1099 * set up a private mapping or an anonymous shared mapping
1100 */
David Howells8feae132009-01-08 12:04:47 +00001101static int do_mmap_private(struct vm_area_struct *vma,
1102 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001103 unsigned long len,
1104 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
David Howells8feae132009-01-08 12:04:47 +00001106 struct page *pages;
1107 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 void *base;
David Howells8feae132009-01-08 12:04:47 +00001109 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* invoke the file's mapping function so that it can keep track of
1112 * shared mappings on devices or memory
1113 * - VM_MAYSHARE will be set if it may attempt to share
1114 */
David Howells645d83c2009-09-24 15:13:10 +01001115 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001117 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001119 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1120 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001123 if (ret != -ENOSYS)
1124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /* getting an ENOSYS error indicates that direct mmap isn't
1127 * possible (as opposed to tried but failed) so we'll try to
1128 * make a private copy of the data and map that instead */
1129 }
1130
David Howells8feae132009-01-08 12:04:47 +00001131 rlen = PAGE_ALIGN(len);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* allocate some memory to hold the mapping
1134 * - note that this may not return a page-aligned address if the object
1135 * we're allocating is smaller than a page
1136 */
David Howells8feae132009-01-08 12:04:47 +00001137 order = get_order(rlen);
1138 kdebug("alloc order %d for %lx", order, len);
1139
1140 pages = alloc_pages(GFP_KERNEL, order);
1141 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto enomem;
1143
David Howells8feae132009-01-08 12:04:47 +00001144 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001145 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
David Howells8feae132009-01-08 12:04:47 +00001147 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001148
1149 /* we allocated a power-of-2 sized page set, so we may want to trim off
1150 * the excess */
1151 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1152 while (total > point) {
1153 order = ilog2(total - point);
1154 n = 1 << order;
1155 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001156 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001157 total -= n;
1158 set_page_refcounted(pages + total);
1159 __free_pages(pages + total, order);
1160 }
David Howells8feae132009-01-08 12:04:47 +00001161 }
1162
David Howells8feae132009-01-08 12:04:47 +00001163 for (point = 1; point < total; point++)
1164 set_page_refcounted(&pages[point]);
1165
1166 base = page_address(pages);
1167 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1168 region->vm_start = (unsigned long) base;
1169 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001170 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001171
1172 vma->vm_start = region->vm_start;
1173 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (vma->vm_file) {
1176 /* read the contents of a file into the copy */
1177 mm_segment_t old_fs;
1178 loff_t fpos;
1179
1180 fpos = vma->vm_pgoff;
1181 fpos <<= PAGE_SHIFT;
1182
1183 old_fs = get_fs();
1184 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001185 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 set_fs(old_fs);
1187
1188 if (ret < 0)
1189 goto error_free;
1190
1191 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001192 if (ret < rlen)
1193 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
1197 return 0;
1198
1199error_free:
David Howells8feae132009-01-08 12:04:47 +00001200 free_page_series(region->vm_start, region->vm_end);
1201 region->vm_start = vma->vm_start = 0;
1202 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001203 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return ret;
1205
1206enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001207 printk("Allocation of length %lu from process %d (%s) failed\n",
1208 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 show_free_areas();
1210 return -ENOMEM;
1211}
1212
1213/*
1214 * handle mapping creation for uClinux
1215 */
1216unsigned long do_mmap_pgoff(struct file *file,
1217 unsigned long addr,
1218 unsigned long len,
1219 unsigned long prot,
1220 unsigned long flags,
1221 unsigned long pgoff)
1222{
David Howells8feae132009-01-08 12:04:47 +00001223 struct vm_area_struct *vma;
1224 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001226 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int ret;
1228
David Howells8feae132009-01-08 12:04:47 +00001229 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* decide whether we should attempt the mapping, and if so what sort of
1232 * mapping */
1233 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1234 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001235 if (ret < 0) {
1236 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return ret;
David Howells8feae132009-01-08 12:04:47 +00001238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
David Howells06aab5a2009-09-24 12:33:48 +01001240 /* we ignore the address hint */
1241 addr = 0;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* we've determined that we can make the mapping, now translate what we
1244 * now know into VMA flags */
1245 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1246
David Howells8feae132009-01-08 12:04:47 +00001247 /* we're going to need to record the mapping */
1248 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1249 if (!region)
1250 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
David Howells8feae132009-01-08 12:04:47 +00001252 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1253 if (!vma)
1254 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
David Howells1e2ae592010-01-15 17:01:33 -08001256 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001257 region->vm_flags = vm_flags;
1258 region->vm_pgoff = pgoff;
1259
Rik van Riel5beb4932010-03-05 13:42:07 -08001260 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001261 vma->vm_flags = vm_flags;
1262 vma->vm_pgoff = pgoff;
1263
1264 if (file) {
1265 region->vm_file = file;
1266 get_file(file);
1267 vma->vm_file = file;
1268 get_file(file);
1269 if (vm_flags & VM_EXECUTABLE) {
1270 added_exe_file_vma(current->mm);
1271 vma->vm_mm = current->mm;
1272 }
1273 }
1274
1275 down_write(&nommu_region_sem);
1276
1277 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001279 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * - shared mappings on character devices and memory backed files are
1281 * permitted to overlap inexactly as far as we are concerned for in
1282 * these cases, sharing is handled in the driver or filesystem rather
1283 * than here
1284 */
1285 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001286 struct vm_region *pregion;
1287 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
David Howells8feae132009-01-08 12:04:47 +00001289 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1290 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001291
David Howells8feae132009-01-08 12:04:47 +00001292 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1293 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
David Howells8feae132009-01-08 12:04:47 +00001295 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 continue;
1297
1298 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001299 if (pregion->vm_file->f_path.dentry->d_inode !=
1300 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 continue;
1302
David Howells8feae132009-01-08 12:04:47 +00001303 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 continue;
1305
David Howells8feae132009-01-08 12:04:47 +00001306 rpglen = pregion->vm_end - pregion->vm_start;
1307 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1308 rpgend = pregion->vm_pgoff + rpglen;
1309 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 continue;
1311
David Howells8feae132009-01-08 12:04:47 +00001312 /* handle inexactly overlapping matches between
1313 * mappings */
1314 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1315 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1316 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1318 goto sharing_violation;
1319 continue;
1320 }
1321
David Howells8feae132009-01-08 12:04:47 +00001322 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001323 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001324 vma->vm_region = pregion;
1325 start = pregion->vm_start;
1326 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1327 vma->vm_start = start;
1328 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
David Howells8feae132009-01-08 12:04:47 +00001330 if (pregion->vm_flags & VM_MAPPED_COPY) {
1331 kdebug("share copy");
1332 vma->vm_flags |= VM_MAPPED_COPY;
1333 } else {
1334 kdebug("share mmap");
1335 ret = do_mmap_shared_file(vma);
1336 if (ret < 0) {
1337 vma->vm_region = NULL;
1338 vma->vm_start = 0;
1339 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001340 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001341 pregion = NULL;
1342 goto error_just_free;
1343 }
1344 }
1345 fput(region->vm_file);
1346 kmem_cache_free(vm_region_jar, region);
1347 region = pregion;
1348 result = start;
1349 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* obtain the address at which to make a shared mapping
1353 * - this is the hook for quasi-memory character devices to
1354 * tell us the location of a shared mapping
1355 */
David Howells645d83c2009-09-24 15:13:10 +01001356 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 addr = file->f_op->get_unmapped_area(file, addr, len,
1358 pgoff, flags);
1359 if (IS_ERR((void *) addr)) {
1360 ret = addr;
1361 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001362 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* the driver refused to tell us where to site
1365 * the mapping so we'll have to attempt to copy
1366 * it */
1367 ret = (unsigned long) -ENODEV;
1368 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001369 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001372 } else {
1373 vma->vm_start = region->vm_start = addr;
1374 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 }
1377 }
1378
David Howells8feae132009-01-08 12:04:47 +00001379 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
David Howells645d83c2009-09-24 15:13:10 +01001381 /* set up the mapping
1382 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001385 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 else
David Howells645d83c2009-09-24 15:13:10 +01001387 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001389 goto error_just_free;
1390 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001391
Jie Zhangea637632009-12-14 18:00:02 -08001392 /* clear anonymous mappings that don't ask for uninitialized data */
1393 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1394 memset((void *)region->vm_start, 0,
1395 region->vm_end - region->vm_start);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001398 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 current->mm->total_vm += len >> PAGE_SHIFT;
1401
David Howells8feae132009-01-08 12:04:47 +00001402share:
1403 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Mike Frysingercfe79c02010-01-06 17:23:23 +00001405 /* we flush the region from the icache only when the first executable
1406 * mapping of it is made */
1407 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1408 flush_icache_range(region->vm_start, region->vm_end);
1409 region->vm_icache_flushed = true;
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Mike Frysingercfe79c02010-01-06 17:23:23 +00001412 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
David Howells8feae132009-01-08 12:04:47 +00001414 kleave(" = %lx", result);
1415 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
David Howells8feae132009-01-08 12:04:47 +00001417error_just_free:
1418 up_write(&nommu_region_sem);
1419error:
David Howells89a86402009-10-30 13:13:26 +00001420 if (region->vm_file)
1421 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001422 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001423 if (vma->vm_file)
1424 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001425 if (vma->vm_flags & VM_EXECUTABLE)
1426 removed_exe_file_vma(vma->vm_mm);
1427 kmem_cache_free(vm_area_cachep, vma);
1428 kleave(" = %d", ret);
1429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
David Howells8feae132009-01-08 12:04:47 +00001431sharing_violation:
1432 up_write(&nommu_region_sem);
1433 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1434 ret = -EINVAL;
1435 goto error;
1436
1437error_getting_vma:
1438 kmem_cache_free(vm_region_jar, region);
1439 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1440 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 len, current->pid);
1442 show_free_areas();
1443 return -ENOMEM;
1444
David Howells8feae132009-01-08 12:04:47 +00001445error_getting_region:
1446 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1447 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 len, current->pid);
1449 show_free_areas();
1450 return -ENOMEM;
1451}
Paul Mundtb5073172007-07-21 04:37:25 -07001452EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001454SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1455 unsigned long, prot, unsigned long, flags,
1456 unsigned long, fd, unsigned long, pgoff)
1457{
1458 struct file *file = NULL;
1459 unsigned long retval = -EBADF;
1460
1461 if (!(flags & MAP_ANONYMOUS)) {
1462 file = fget(fd);
1463 if (!file)
1464 goto out;
1465 }
1466
1467 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1468
1469 down_write(&current->mm->mmap_sem);
1470 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1471 up_write(&current->mm->mmap_sem);
1472
1473 if (file)
1474 fput(file);
1475out:
1476 return retval;
1477}
1478
Christoph Hellwiga4679372010-03-10 15:21:15 -08001479#ifdef __ARCH_WANT_SYS_OLD_MMAP
1480struct mmap_arg_struct {
1481 unsigned long addr;
1482 unsigned long len;
1483 unsigned long prot;
1484 unsigned long flags;
1485 unsigned long fd;
1486 unsigned long offset;
1487};
1488
1489SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1490{
1491 struct mmap_arg_struct a;
1492
1493 if (copy_from_user(&a, arg, sizeof(a)))
1494 return -EFAULT;
1495 if (a.offset & ~PAGE_MASK)
1496 return -EINVAL;
1497
1498 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1499 a.offset >> PAGE_SHIFT);
1500}
1501#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/*
David Howells8feae132009-01-08 12:04:47 +00001504 * split a vma into two pieces at address 'addr', a new vma is allocated either
1505 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
David Howells8feae132009-01-08 12:04:47 +00001507int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1508 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
David Howells8feae132009-01-08 12:04:47 +00001510 struct vm_area_struct *new;
1511 struct vm_region *region;
1512 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
David Howells8feae132009-01-08 12:04:47 +00001514 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
David Howells779c1022010-01-15 17:01:34 -08001516 /* we're only permitted to split anonymous regions (these should have
1517 * only a single usage on the region) */
1518 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001519 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
David Howells8feae132009-01-08 12:04:47 +00001521 if (mm->map_count >= sysctl_max_map_count)
1522 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
David Howells8feae132009-01-08 12:04:47 +00001524 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1525 if (!region)
1526 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
David Howells8feae132009-01-08 12:04:47 +00001528 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1529 if (!new) {
1530 kmem_cache_free(vm_region_jar, region);
1531 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
David Howells8feae132009-01-08 12:04:47 +00001533
1534 /* most fields are the same, copy all, and then fixup */
1535 *new = *vma;
1536 *region = *vma->vm_region;
1537 new->vm_region = region;
1538
1539 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1540
1541 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001542 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001543 } else {
1544 region->vm_start = new->vm_start = addr;
1545 region->vm_pgoff = new->vm_pgoff += npages;
1546 }
1547
1548 if (new->vm_ops && new->vm_ops->open)
1549 new->vm_ops->open(new);
1550
1551 delete_vma_from_mm(vma);
1552 down_write(&nommu_region_sem);
1553 delete_nommu_region(vma->vm_region);
1554 if (new_below) {
1555 vma->vm_region->vm_start = vma->vm_start = addr;
1556 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1557 } else {
1558 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001559 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001560 }
1561 add_nommu_region(vma->vm_region);
1562 add_nommu_region(new->vm_region);
1563 up_write(&nommu_region_sem);
1564 add_vma_to_mm(mm, vma);
1565 add_vma_to_mm(mm, new);
1566 return 0;
1567}
1568
1569/*
1570 * shrink a VMA by removing the specified chunk from either the beginning or
1571 * the end
1572 */
1573static int shrink_vma(struct mm_struct *mm,
1574 struct vm_area_struct *vma,
1575 unsigned long from, unsigned long to)
1576{
1577 struct vm_region *region;
1578
1579 kenter("");
1580
1581 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1582 * and list */
1583 delete_vma_from_mm(vma);
1584 if (from > vma->vm_start)
1585 vma->vm_end = from;
1586 else
1587 vma->vm_start = to;
1588 add_vma_to_mm(mm, vma);
1589
1590 /* cut the backing region down to size */
1591 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001592 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001593
1594 down_write(&nommu_region_sem);
1595 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001596 if (from > region->vm_start) {
1597 to = region->vm_top;
1598 region->vm_top = region->vm_end = from;
1599 } else {
David Howells8feae132009-01-08 12:04:47 +00001600 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001601 }
David Howells8feae132009-01-08 12:04:47 +00001602 add_nommu_region(region);
1603 up_write(&nommu_region_sem);
1604
1605 free_page_series(from, to);
1606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
David Howells30340972006-09-27 01:50:20 -07001609/*
1610 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001611 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1612 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001613 */
David Howells8feae132009-01-08 12:04:47 +00001614int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
David Howells8feae132009-01-08 12:04:47 +00001616 struct vm_area_struct *vma;
1617 struct rb_node *rb;
1618 unsigned long end = start + len;
1619 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
David Howells8feae132009-01-08 12:04:47 +00001621 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
David Howells8feae132009-01-08 12:04:47 +00001623 if (len == 0)
1624 return -EINVAL;
1625
1626 /* find the first potentially overlapping VMA */
1627 vma = find_vma(mm, start);
1628 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001629 static int limit = 0;
1630 if (limit < 5) {
1631 printk(KERN_WARNING
1632 "munmap of memory not mmapped by process %d"
1633 " (%s): 0x%lx-0x%lx\n",
1634 current->pid, current->comm,
1635 start, start + len - 1);
1636 limit++;
1637 }
David Howells8feae132009-01-08 12:04:47 +00001638 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David Howells8feae132009-01-08 12:04:47 +00001641 /* we're allowed to split an anonymous VMA but not a file-backed one */
1642 if (vma->vm_file) {
1643 do {
1644 if (start > vma->vm_start) {
1645 kleave(" = -EINVAL [miss]");
1646 return -EINVAL;
1647 }
1648 if (end == vma->vm_end)
1649 goto erase_whole_vma;
1650 rb = rb_next(&vma->vm_rb);
1651 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1652 } while (rb);
1653 kleave(" = -EINVAL [split file]");
1654 return -EINVAL;
1655 } else {
1656 /* the chunk must be a subset of the VMA found */
1657 if (start == vma->vm_start && end == vma->vm_end)
1658 goto erase_whole_vma;
1659 if (start < vma->vm_start || end > vma->vm_end) {
1660 kleave(" = -EINVAL [superset]");
1661 return -EINVAL;
1662 }
1663 if (start & ~PAGE_MASK) {
1664 kleave(" = -EINVAL [unaligned start]");
1665 return -EINVAL;
1666 }
1667 if (end != vma->vm_end && end & ~PAGE_MASK) {
1668 kleave(" = -EINVAL [unaligned split]");
1669 return -EINVAL;
1670 }
1671 if (start != vma->vm_start && end != vma->vm_end) {
1672 ret = split_vma(mm, vma, start, 1);
1673 if (ret < 0) {
1674 kleave(" = %d [split]", ret);
1675 return ret;
1676 }
1677 }
1678 return shrink_vma(mm, vma, start, end);
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
David Howells8feae132009-01-08 12:04:47 +00001681erase_whole_vma:
1682 delete_vma_from_mm(vma);
1683 delete_vma(mm, vma);
1684 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
Paul Mundtb5073172007-07-21 04:37:25 -07001687EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001689SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001690{
1691 int ret;
1692 struct mm_struct *mm = current->mm;
1693
1694 down_write(&mm->mmap_sem);
1695 ret = do_munmap(mm, addr, len);
1696 up_write(&mm->mmap_sem);
1697 return ret;
1698}
1699
1700/*
David Howells8feae132009-01-08 12:04:47 +00001701 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001702 */
David Howells8feae132009-01-08 12:04:47 +00001703void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
David Howells8feae132009-01-08 12:04:47 +00001705 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
David Howells8feae132009-01-08 12:04:47 +00001707 if (!mm)
1708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
David Howells8feae132009-01-08 12:04:47 +00001710 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
David Howells8feae132009-01-08 12:04:47 +00001712 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
David Howells8feae132009-01-08 12:04:47 +00001714 while ((vma = mm->mmap)) {
1715 mm->mmap = vma->vm_next;
1716 delete_vma_from_mm(vma);
1717 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
David Howells8feae132009-01-08 12:04:47 +00001719
1720 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723unsigned long do_brk(unsigned long addr, unsigned long len)
1724{
1725 return -ENOMEM;
1726}
1727
1728/*
David Howells6fa5f802006-09-27 01:50:21 -07001729 * expand (or shrink) an existing mapping, potentially moving it at the same
1730 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 *
David Howells6fa5f802006-09-27 01:50:21 -07001732 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001733 * as long as it stays within the region allocated by do_mmap_private() and the
1734 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 *
David Howells6fa5f802006-09-27 01:50:21 -07001736 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
1738unsigned long do_mremap(unsigned long addr,
1739 unsigned long old_len, unsigned long new_len,
1740 unsigned long flags, unsigned long new_addr)
1741{
David Howells6fa5f802006-09-27 01:50:21 -07001742 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001745 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return (unsigned long) -EINVAL;
1747
David Howells8feae132009-01-08 12:04:47 +00001748 if (addr & ~PAGE_MASK)
1749 return -EINVAL;
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (flags & MREMAP_FIXED && new_addr != addr)
1752 return (unsigned long) -EINVAL;
1753
David Howells8feae132009-01-08 12:04:47 +00001754 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001755 if (!vma)
1756 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
David Howells6fa5f802006-09-27 01:50:21 -07001758 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return (unsigned long) -EFAULT;
1760
David Howells6fa5f802006-09-27 01:50:21 -07001761 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return (unsigned long) -EPERM;
1763
David Howells8feae132009-01-08 12:04:47 +00001764 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return (unsigned long) -ENOMEM;
1766
1767 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001768 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001769 return vma->vm_start;
1770}
Paul Mundtb5073172007-07-21 04:37:25 -07001771EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001772
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001773SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1774 unsigned long, new_len, unsigned long, flags,
1775 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001776{
1777 unsigned long ret;
1778
1779 down_write(&current->mm->mmap_sem);
1780 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1781 up_write(&current->mm->mmap_sem);
1782 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Linus Torvalds6aab3412005-11-28 14:34:23 -08001785struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001786 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 return NULL;
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1792 unsigned long to, unsigned long size, pgprot_t prot)
1793{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001794 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
Luke Yang22c4af42006-07-14 00:24:09 -07001797EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Paul Mundtf905bc42008-02-04 22:29:59 -08001799int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1800 unsigned long pgoff)
1801{
1802 unsigned int size = vma->vm_end - vma->vm_start;
1803
1804 if (!(vma->vm_flags & VM_USERMAP))
1805 return -EINVAL;
1806
1807 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1808 vma->vm_end = vma->vm_start + size;
1809
1810 return 0;
1811}
1812EXPORT_SYMBOL(remap_vmalloc_range);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1815{
1816}
1817
1818unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1819 unsigned long len, unsigned long pgoff, unsigned long flags)
1820{
1821 return -ENOMEM;
1822}
1823
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001824void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828void unmap_mapping_range(struct address_space *mapping,
1829 loff_t const holebegin, loff_t const holelen,
1830 int even_cows)
1831{
1832}
Luke Yang22c4af42006-07-14 00:24:09 -07001833EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835/*
1836 * Check that a process has enough memory to allocate a new virtual
1837 * mapping. 0 means there is enough memory for the allocation to
1838 * succeed and -ENOMEM implies there is not.
1839 *
1840 * We currently support three overcommit policies, which are set via the
1841 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1842 *
1843 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1844 * Additional code 2002 Jul 20 by Robert Love.
1845 *
1846 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1847 *
1848 * Note this is a helper function intended to be used by LSMs which
1849 * wish to use this logic.
1850 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001851int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 unsigned long free, allowed;
1854
1855 vm_acct_memory(pages);
1856
1857 /*
1858 * Sometimes we want to use more memory than we have
1859 */
1860 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1861 return 0;
1862
1863 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1864 unsigned long n;
1865
Christoph Lameter347ce432006-06-30 01:55:35 -07001866 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 free += nr_swap_pages;
1868
1869 /*
1870 * Any slabs which are created with the
1871 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1872 * which are reclaimable, under pressure. The dentry
1873 * cache and most inode caches should fall into this
1874 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001875 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /*
1878 * Leave the last 3% for root
1879 */
1880 if (!cap_sys_admin)
1881 free -= free / 32;
1882
1883 if (free > pages)
1884 return 0;
1885
1886 /*
1887 * nr_free_pages() is very expensive on large systems,
1888 * only call if we're about to fail.
1889 */
1890 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001891
1892 /*
1893 * Leave reserved pages. The pages are not for anonymous pages.
1894 */
1895 if (n <= totalreserve_pages)
1896 goto error;
1897 else
1898 n -= totalreserve_pages;
1899
1900 /*
1901 * Leave the last 3% for root
1902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (!cap_sys_admin)
1904 n -= n / 32;
1905 free += n;
1906
1907 if (free > pages)
1908 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001909
1910 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
1913 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1914 /*
1915 * Leave the last 3% for root
1916 */
1917 if (!cap_sys_admin)
1918 allowed -= allowed / 32;
1919 allowed += total_swap_pages;
1920
1921 /* Don't let a single process grow too big:
1922 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001923 if (mm)
1924 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001926 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001928
Hideo AOKId5ddc792006-04-10 22:53:01 -07001929error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 vm_unacct_memory(pages);
1931
1932 return -ENOMEM;
1933}
1934
1935int in_gate_area_no_task(unsigned long addr)
1936{
1937 return 0;
1938}
David Howellsb0e15192006-01-06 00:11:42 -08001939
Nick Piggind0217ac2007-07-19 01:47:03 -07001940int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001941{
1942 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001943 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001944}
Paul Mundtb5073172007-07-21 04:37:25 -07001945EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001946
1947/*
1948 * Access another process' address space.
1949 * - source/target buffer must be kernel space
1950 */
1951int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1952{
David Howells0ec76a12006-09-27 01:50:15 -07001953 struct vm_area_struct *vma;
1954 struct mm_struct *mm;
1955
1956 if (addr + len < addr)
1957 return 0;
1958
1959 mm = get_task_mm(tsk);
1960 if (!mm)
1961 return 0;
1962
1963 down_read(&mm->mmap_sem);
1964
1965 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001966 vma = find_vma(mm, addr);
1967 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001968 /* don't overrun this mapping */
1969 if (addr + len >= vma->vm_end)
1970 len = vma->vm_end - addr;
1971
1972 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001973 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001974 copy_to_user_page(vma, NULL, addr,
1975 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001976 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001977 copy_from_user_page(vma, NULL, addr,
1978 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001979 else
1980 len = 0;
1981 } else {
1982 len = 0;
1983 }
1984
1985 up_read(&mm->mmap_sem);
1986 mmput(mm);
1987 return len;
1988}
David Howells7e660872010-01-15 17:01:39 -08001989
1990/**
1991 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1992 * @inode: The inode to check
1993 * @size: The current filesize of the inode
1994 * @newsize: The proposed filesize of the inode
1995 *
1996 * Check the shared mappings on an inode on behalf of a shrinking truncate to
1997 * make sure that that any outstanding VMAs aren't broken and then shrink the
1998 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
1999 * automatically grant mappings that are too large.
2000 */
2001int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2002 size_t newsize)
2003{
2004 struct vm_area_struct *vma;
2005 struct prio_tree_iter iter;
2006 struct vm_region *region;
2007 pgoff_t low, high;
2008 size_t r_size, r_top;
2009
2010 low = newsize >> PAGE_SHIFT;
2011 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2012
2013 down_write(&nommu_region_sem);
2014
2015 /* search for VMAs that fall within the dead zone */
2016 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2017 low, high) {
2018 /* found one - only interested if it's shared out of the page
2019 * cache */
2020 if (vma->vm_flags & VM_SHARED) {
2021 up_write(&nommu_region_sem);
2022 return -ETXTBSY; /* not quite true, but near enough */
2023 }
2024 }
2025
2026 /* reduce any regions that overlap the dead zone - if in existence,
2027 * these will be pointed to by VMAs that don't overlap the dead zone
2028 *
2029 * we don't check for any regions that start beyond the EOF as there
2030 * shouldn't be any
2031 */
2032 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2033 0, ULONG_MAX) {
2034 if (!(vma->vm_flags & VM_SHARED))
2035 continue;
2036
2037 region = vma->vm_region;
2038 r_size = region->vm_top - region->vm_start;
2039 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2040
2041 if (r_top > newsize) {
2042 region->vm_top -= r_top - newsize;
2043 if (region->vm_end > region->vm_top)
2044 region->vm_end = region->vm_top;
2045 }
2046 }
2047
2048 up_write(&nommu_region_sem);
2049 return 0;
2050}