blob: 275608cd18a3d9c112971d79f0c27112c331cee0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/nommu.c
3 *
4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
6 *
7 * See Documentation/nommu-mmap.txt
8 *
David Howells8feae132009-01-08 12:04:47 +00009 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
Paul Mundteb6434d2009-01-21 17:45:47 +090013 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
David Howellsf2b85442007-10-29 13:15:39 +000016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/swap.h>
20#include <linux/file.h>
21#include <linux/highmem.h>
22#include <linux/pagemap.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
Roland McGrathfa8e26c2008-07-25 19:45:50 -070025#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
27#include <linux/backing-dev.h>
28#include <linux/mount.h>
29#include <linux/personality.h>
30#include <linux/security.h>
31#include <linux/syscalls.h>
Al Viro120a7952010-10-30 02:54:44 -040032#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/uaccess.h>
35#include <asm/tlb.h>
36#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070037#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000038#include "internal.h"
39
David Howells8feae132009-01-08 12:04:47 +000040#if 0
41#define kenter(FMT, ...) \
42 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
43#define kleave(FMT, ...) \
44 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
45#define kdebug(FMT, ...) \
46 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47#else
48#define kenter(FMT, ...) \
49 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
50#define kleave(FMT, ...) \
51 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
52#define kdebug(FMT, ...) \
53 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
54#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56void *high_memory;
57struct page *mem_map;
58unsigned long max_mapnr;
59unsigned long num_physpages;
Hugh Dickins4266c972009-09-23 17:05:53 +010060unsigned long highest_memmap_pfn;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070061struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
63int sysctl_overcommit_ratio = 50; /* default is 50% */
64int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070065int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066int heap_stack_gap = 0;
67
David Howells33e5d7692009-04-02 16:56:32 -070068atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070071EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Howells8feae132009-01-08 12:04:47 +000073/* list of mapped, potentially shareable regions */
74static struct kmem_cache *vm_region_jar;
75struct rb_root nommu_region_tree = RB_ROOT;
76DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040078const struct vm_operations_struct generic_file_vm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Return the total memory allocated for this pointer, not
83 * just what the caller asked for.
84 *
85 * Doesn't have to be accurate, i.e. may have races.
86 */
87unsigned int kobjsize(const void *objp)
88{
89 struct page *page;
90
Michael Hennerich4016a132008-04-28 02:13:38 -070091 /*
92 * If the object we have should not have ksize performed on it,
93 * return size of 0
94 */
Paul Mundt5a1603b2008-06-12 16:29:55 +090095 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070096 return 0;
97
98 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -070099
100 /*
101 * If the allocator sets PageSlab, we know the pointer came from
102 * kmalloc().
103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (PageSlab(page))
105 return ksize(objp);
106
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700107 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000108 * If it's not a compound page, see if we have a matching VMA
109 * region. This test is intentionally done in reverse order,
110 * so if there's no VMA, we still fall through and hand back
111 * PAGE_SIZE for 0-order pages.
112 */
113 if (!PageCompound(page)) {
114 struct vm_area_struct *vma;
115
116 vma = find_vma(current->mm, (unsigned long)objp);
117 if (vma)
118 return vma->vm_end - vma->vm_start;
119 }
120
121 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700122 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900123 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700124 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900125 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Nick Pigginb291f002008-10-18 20:26:44 -0700128int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Hugh Dickins4266c972009-09-23 17:05:53 +0100129 unsigned long start, int nr_pages, unsigned int foll_flags,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200130 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700132 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700133 unsigned long vm_flags;
134 int i;
135
136 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700137 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700138 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700139 vm_flags = (foll_flags & FOLL_WRITE) ?
140 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
141 vm_flags &= (foll_flags & FOLL_FORCE) ?
142 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Peter Zijlstra9d737772009-06-25 11:58:55 +0200144 for (i = 0; i < nr_pages; i++) {
David Howells7561e8c2010-03-25 16:48:38 +0000145 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700146 if (!vma)
147 goto finish_or_fault;
148
149 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700150 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
151 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700152 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (pages) {
155 pages[i] = virt_to_page(start);
156 if (pages[i])
157 page_cache_get(pages[i]);
158 }
159 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700160 vmas[i] = vma;
David Howellse1ee65d2010-03-25 16:48:44 +0000161 start = (start + PAGE_SIZE) & PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
David Howells7b4d5b82006-09-27 01:50:18 -0700163
164 return i;
165
166finish_or_fault:
167 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Nick Pigginb291f002008-10-18 20:26:44 -0700169
Nick Pigginb291f002008-10-18 20:26:44 -0700170/*
171 * get a list of pages in an address range belonging to the specified process
172 * and indicate the VMA that covers each page
173 * - this is potentially dodgy as we may end incrementing the page count of a
174 * slab page or a secondary page from a compound page
175 * - don't permit access to VMAs that don't support it, such as I/O mappings
176 */
177int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200178 unsigned long start, int nr_pages, int write, int force,
Nick Pigginb291f002008-10-18 20:26:44 -0700179 struct page **pages, struct vm_area_struct **vmas)
180{
181 int flags = 0;
182
183 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700184 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700185 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700186 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700187
Peter Zijlstra9d737772009-06-25 11:58:55 +0200188 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
Nick Pigginb291f002008-10-18 20:26:44 -0700189}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000190EXPORT_SYMBOL(get_user_pages);
191
Paul Mundtdfc2f912009-06-26 04:31:57 +0900192/**
193 * follow_pfn - look up PFN at a user virtual address
194 * @vma: memory mapping
195 * @address: user virtual address
196 * @pfn: location to store found PFN
197 *
198 * Only IO mappings and raw PFN mappings are allowed.
199 *
200 * Returns zero and the pfn at @pfn on success, -ve otherwise.
201 */
202int follow_pfn(struct vm_area_struct *vma, unsigned long address,
203 unsigned long *pfn)
204{
205 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
206 return -EINVAL;
207
208 *pfn = address >> PAGE_SHIFT;
209 return 0;
210}
211EXPORT_SYMBOL(follow_pfn);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213DEFINE_RWLOCK(vmlist_lock);
214struct vm_struct *vmlist;
215
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800216void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 kfree(addr);
219}
Paul Mundtb5073172007-07-21 04:37:25 -0700220EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Al Virodd0fc662005-10-07 07:46:04 +0100222void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200225 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
226 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Nick Piggin84097512006-03-22 00:08:34 -0800228 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
Paul Mundtb5073172007-07-21 04:37:25 -0700230EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Paul Mundtf905bc42008-02-04 22:29:59 -0800232void *vmalloc_user(unsigned long size)
233{
234 void *ret;
235
236 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
237 PAGE_KERNEL);
238 if (ret) {
239 struct vm_area_struct *vma;
240
241 down_write(&current->mm->mmap_sem);
242 vma = find_vma(current->mm, (unsigned long)ret);
243 if (vma)
244 vma->vm_flags |= VM_USERMAP;
245 up_write(&current->mm->mmap_sem);
246 }
247
248 return ret;
249}
250EXPORT_SYMBOL(vmalloc_user);
251
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800252struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 return virt_to_page(addr);
255}
Paul Mundtb5073172007-07-21 04:37:25 -0700256EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800258unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 return page_to_pfn(virt_to_page(addr));
261}
Paul Mundtb5073172007-07-21 04:37:25 -0700262EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264long vread(char *buf, char *addr, unsigned long count)
265{
266 memcpy(buf, addr, count);
267 return count;
268}
269
270long vwrite(char *buf, char *addr, unsigned long count)
271{
272 /* Don't allow overflow */
273 if ((unsigned long) addr + count < count)
274 count = -(unsigned long) addr;
275
276 memcpy(addr, buf, count);
277 return(count);
278}
279
280/*
281 * vmalloc - allocate virtually continguos memory
282 *
283 * @size: allocation size
284 *
285 * Allocate enough pages to cover @size from the page level
286 * allocator and map them into continguos kernel virtual space.
287 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200288 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * use __vmalloc() instead.
290 */
291void *vmalloc(unsigned long size)
292{
293 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
294}
Andrew Mortonf6138882006-02-28 16:59:18 -0800295EXPORT_SYMBOL(vmalloc);
296
Dave Younge1ca7782010-10-26 14:22:06 -0700297/*
298 * vzalloc - allocate virtually continguos memory with zero fill
299 *
300 * @size: allocation size
301 *
302 * Allocate enough pages to cover @size from the page level
303 * allocator and map them into continguos kernel virtual space.
304 * The memory allocated is set to zero.
305 *
306 * For tight control over page level allocator and protection flags
307 * use __vmalloc() instead.
308 */
309void *vzalloc(unsigned long size)
310{
311 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
312 PAGE_KERNEL);
313}
314EXPORT_SYMBOL(vzalloc);
315
316/**
317 * vmalloc_node - allocate memory on a specific node
318 * @size: allocation size
319 * @node: numa node
320 *
321 * Allocate enough pages to cover @size from the page level
322 * allocator and map them into contiguous kernel virtual space.
323 *
324 * For tight control over page level allocator and protection flags
325 * use __vmalloc() instead.
326 */
Andrew Mortonf6138882006-02-28 16:59:18 -0800327void *vmalloc_node(unsigned long size, int node)
328{
329 return vmalloc(size);
330}
Paul Mundt9a14f652010-12-24 11:50:34 +0900331EXPORT_SYMBOL(vmalloc_node);
Dave Younge1ca7782010-10-26 14:22:06 -0700332
333/**
334 * vzalloc_node - allocate memory on a specific node with zero fill
335 * @size: allocation size
336 * @node: numa node
337 *
338 * Allocate enough pages to cover @size from the page level
339 * allocator and map them into contiguous kernel virtual space.
340 * The memory allocated is set to zero.
341 *
342 * For tight control over page level allocator and protection flags
343 * use __vmalloc() instead.
344 */
345void *vzalloc_node(unsigned long size, int node)
346{
347 return vzalloc(size);
348}
349EXPORT_SYMBOL(vzalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Paul Mundt1af446e2008-08-04 16:01:47 +0900351#ifndef PAGE_KERNEL_EXEC
352# define PAGE_KERNEL_EXEC PAGE_KERNEL
353#endif
354
355/**
356 * vmalloc_exec - allocate virtually contiguous, executable memory
357 * @size: allocation size
358 *
359 * Kernel-internal function to allocate enough pages to cover @size
360 * the page level allocator and map them into contiguous and
361 * executable kernel virtual space.
362 *
363 * For tight control over page level allocator and protection flags
364 * use __vmalloc() instead.
365 */
366
367void *vmalloc_exec(unsigned long size)
368{
369 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
370}
371
Paul Mundtb5073172007-07-21 04:37:25 -0700372/**
373 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * @size: allocation size
375 *
376 * Allocate enough 32bit PA addressable pages to cover @size from the
377 * page level allocator and map them into continguos kernel virtual space.
378 */
379void *vmalloc_32(unsigned long size)
380{
381 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
382}
Paul Mundtb5073172007-07-21 04:37:25 -0700383EXPORT_SYMBOL(vmalloc_32);
384
385/**
386 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
387 * @size: allocation size
388 *
389 * The resulting memory area is 32bit addressable and zeroed so it can be
390 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800391 *
392 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
393 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700394 */
395void *vmalloc_32_user(unsigned long size)
396{
Paul Mundtf905bc42008-02-04 22:29:59 -0800397 /*
398 * We'll have to sort out the ZONE_DMA bits for 64-bit,
399 * but for now this can simply use vmalloc_user() directly.
400 */
401 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700402}
403EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
406{
407 BUG();
408 return NULL;
409}
Paul Mundtb5073172007-07-21 04:37:25 -0700410EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800412void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 BUG();
415}
Paul Mundtb5073172007-07-21 04:37:25 -0700416EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Paul Mundteb6434d2009-01-21 17:45:47 +0900418void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
419{
420 BUG();
421 return NULL;
422}
423EXPORT_SYMBOL(vm_map_ram);
424
425void vm_unmap_ram(const void *mem, unsigned int count)
426{
427 BUG();
428}
429EXPORT_SYMBOL(vm_unmap_ram);
430
431void vm_unmap_aliases(void)
432{
433}
434EXPORT_SYMBOL_GPL(vm_unmap_aliases);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700437 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
438 * have one.
439 */
440void __attribute__((weak)) vmalloc_sync_all(void)
441{
442}
443
Paul Mundtb5073172007-07-21 04:37:25 -0700444int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
445 struct page *page)
446{
447 return -EINVAL;
448}
449EXPORT_SYMBOL(vm_insert_page);
450
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700451/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * sys_brk() for the most part doesn't need the global kernel
453 * lock, except when an application is doing something nasty
454 * like trying to un-brk an area that has already been mapped
455 * to a regular file. in this case, the unmapping will need
456 * to invoke file system routines that need the global lock.
457 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100458SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct mm_struct *mm = current->mm;
461
462 if (brk < mm->start_brk || brk > mm->context.end_brk)
463 return mm->brk;
464
465 if (mm->brk == brk)
466 return mm->brk;
467
468 /*
469 * Always allow shrinking brk
470 */
471 if (brk <= mm->brk) {
472 mm->brk = brk;
473 return brk;
474 }
475
476 /*
477 * Ok, looks good - let it rip.
478 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000479 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return mm->brk = brk;
481}
482
David Howells8feae132009-01-08 12:04:47 +0000483/*
484 * initialise the VMA and region record slabs
485 */
486void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700488 int ret;
489
490 ret = percpu_counter_init(&vm_committed_as, 0);
491 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700492 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells8feae132009-01-08 12:04:47 +0000495/*
496 * validate the region tree
497 * - the caller must hold the region lock
498 */
499#ifdef CONFIG_DEBUG_NOMMU_REGIONS
500static noinline void validate_nommu_regions(void)
501{
502 struct vm_region *region, *last;
503 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
David Howells8feae132009-01-08 12:04:47 +0000505 lastp = rb_first(&nommu_region_tree);
506 if (!lastp)
507 return;
508
509 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700510 BUG_ON(unlikely(last->vm_end <= last->vm_start));
511 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000512
513 while ((p = rb_next(lastp))) {
514 region = rb_entry(p, struct vm_region, vm_rb);
515 last = rb_entry(lastp, struct vm_region, vm_rb);
516
David Howells33e5d7692009-04-02 16:56:32 -0700517 BUG_ON(unlikely(region->vm_end <= region->vm_start));
518 BUG_ON(unlikely(region->vm_top < region->vm_end));
519 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000520
521 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523}
David Howells8feae132009-01-08 12:04:47 +0000524#else
David Howells33e5d7692009-04-02 16:56:32 -0700525static void validate_nommu_regions(void)
526{
527}
David Howells8feae132009-01-08 12:04:47 +0000528#endif
529
530/*
531 * add a region into the global tree
532 */
533static void add_nommu_region(struct vm_region *region)
534{
535 struct vm_region *pregion;
536 struct rb_node **p, *parent;
537
538 validate_nommu_regions();
539
David Howells8feae132009-01-08 12:04:47 +0000540 parent = NULL;
541 p = &nommu_region_tree.rb_node;
542 while (*p) {
543 parent = *p;
544 pregion = rb_entry(parent, struct vm_region, vm_rb);
545 if (region->vm_start < pregion->vm_start)
546 p = &(*p)->rb_left;
547 else if (region->vm_start > pregion->vm_start)
548 p = &(*p)->rb_right;
549 else if (pregion == region)
550 return;
551 else
552 BUG();
553 }
554
555 rb_link_node(&region->vm_rb, parent, p);
556 rb_insert_color(&region->vm_rb, &nommu_region_tree);
557
558 validate_nommu_regions();
559}
560
561/*
562 * delete a region from the global tree
563 */
564static void delete_nommu_region(struct vm_region *region)
565{
566 BUG_ON(!nommu_region_tree.rb_node);
567
568 validate_nommu_regions();
569 rb_erase(&region->vm_rb, &nommu_region_tree);
570 validate_nommu_regions();
571}
572
573/*
574 * free a contiguous series of pages
575 */
576static void free_page_series(unsigned long from, unsigned long to)
577{
578 for (; from < to; from += PAGE_SIZE) {
579 struct page *page = virt_to_page(from);
580
581 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700582 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000583 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700584 kdebug("free page %p: refcount not one: %d",
585 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000586 put_page(page);
587 }
588}
589
590/*
591 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700592 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000593 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000594 * will equal vm_start
595 */
596static void __put_nommu_region(struct vm_region *region)
597 __releases(nommu_region_sem)
598{
David Howells1e2ae592010-01-15 17:01:33 -0800599 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000600
601 BUG_ON(!nommu_region_tree.rb_node);
602
David Howells1e2ae592010-01-15 17:01:33 -0800603 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000604 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000605 delete_nommu_region(region);
606 up_write(&nommu_region_sem);
607
608 if (region->vm_file)
609 fput(region->vm_file);
610
611 /* IO memory and memory shared directly out of the pagecache
612 * from ramfs/tmpfs mustn't be released here */
613 if (region->vm_flags & VM_MAPPED_COPY) {
614 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000615 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000616 }
617 kmem_cache_free(vm_region_jar, region);
618 } else {
619 up_write(&nommu_region_sem);
620 }
621}
622
623/*
624 * release a reference to a region
625 */
626static void put_nommu_region(struct vm_region *region)
627{
628 down_write(&nommu_region_sem);
629 __put_nommu_region(region);
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
David Howells30340972006-09-27 01:50:20 -0700632/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700633 * update protection on a vma
634 */
635static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
636{
637#ifdef CONFIG_MPU
638 struct mm_struct *mm = vma->vm_mm;
639 long start = vma->vm_start & PAGE_MASK;
640 while (start < vma->vm_end) {
641 protect_page(mm, start, flags);
642 start += PAGE_SIZE;
643 }
644 update_protections(mm);
645#endif
646}
647
648/*
David Howells30340972006-09-27 01:50:20 -0700649 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000650 * and tree and add to the address space's page tree also if not an anonymous
651 * page
David Howells30340972006-09-27 01:50:20 -0700652 * - should be called with mm->mmap_sem held writelocked
653 */
David Howells8feae132009-01-08 12:04:47 +0000654static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700655{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700656 struct vm_area_struct *pvma, **pp, *next;
David Howells8feae132009-01-08 12:04:47 +0000657 struct address_space *mapping;
658 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700659
David Howells8feae132009-01-08 12:04:47 +0000660 kenter(",%p", vma);
661
662 BUG_ON(!vma->vm_region);
663
664 mm->map_count++;
665 vma->vm_mm = mm;
666
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700667 protect_vma(vma, vma->vm_flags);
668
David Howells8feae132009-01-08 12:04:47 +0000669 /* add the VMA to the mapping */
670 if (vma->vm_file) {
671 mapping = vma->vm_file->f_mapping;
672
673 flush_dcache_mmap_lock(mapping);
674 vma_prio_tree_insert(vma, &mapping->i_mmap);
675 flush_dcache_mmap_unlock(mapping);
676 }
677
678 /* add the VMA to the tree */
679 parent = NULL;
680 p = &mm->mm_rb.rb_node;
681 while (*p) {
682 parent = *p;
683 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
684
685 /* sort by: start addr, end addr, VMA struct addr in that order
686 * (the latter is necessary as we may get identical VMAs) */
687 if (vma->vm_start < pvma->vm_start)
688 p = &(*p)->rb_left;
689 else if (vma->vm_start > pvma->vm_start)
690 p = &(*p)->rb_right;
691 else if (vma->vm_end < pvma->vm_end)
692 p = &(*p)->rb_left;
693 else if (vma->vm_end > pvma->vm_end)
694 p = &(*p)->rb_right;
695 else if (vma < pvma)
696 p = &(*p)->rb_left;
697 else if (vma > pvma)
698 p = &(*p)->rb_right;
699 else
700 BUG();
701 }
702
703 rb_link_node(&vma->vm_rb, parent, p);
704 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
705
706 /* add VMA to the VMA list also */
707 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
708 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700709 break;
David Howells8feae132009-01-08 12:04:47 +0000710 if (pvma->vm_start < vma->vm_start)
711 continue;
712 if (pvma->vm_end < vma->vm_end)
713 break;
714 }
David Howells30340972006-09-27 01:50:20 -0700715
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700716 next = *pp;
David Howells8feae132009-01-08 12:04:47 +0000717 *pp = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700718 vma->vm_next = next;
719 if (next)
720 next->vm_prev = vma;
David Howells8feae132009-01-08 12:04:47 +0000721}
722
723/*
724 * delete a VMA from its owning mm_struct and address space
725 */
726static void delete_vma_from_mm(struct vm_area_struct *vma)
727{
728 struct vm_area_struct **pp;
729 struct address_space *mapping;
730 struct mm_struct *mm = vma->vm_mm;
731
732 kenter("%p", vma);
733
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700734 protect_vma(vma, 0);
735
David Howells8feae132009-01-08 12:04:47 +0000736 mm->map_count--;
737 if (mm->mmap_cache == vma)
738 mm->mmap_cache = NULL;
739
740 /* remove the VMA from the mapping */
741 if (vma->vm_file) {
742 mapping = vma->vm_file->f_mapping;
743
744 flush_dcache_mmap_lock(mapping);
745 vma_prio_tree_remove(vma, &mapping->i_mmap);
746 flush_dcache_mmap_unlock(mapping);
747 }
748
749 /* remove from the MM's tree and list */
750 rb_erase(&vma->vm_rb, &mm->mm_rb);
751 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
752 if (*pp == vma) {
753 *pp = vma->vm_next;
754 break;
755 }
756 }
757
758 vma->vm_mm = NULL;
759}
760
761/*
762 * destroy a VMA record
763 */
764static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
765{
766 kenter("%p", vma);
767 if (vma->vm_ops && vma->vm_ops->close)
768 vma->vm_ops->close(vma);
769 if (vma->vm_file) {
770 fput(vma->vm_file);
771 if (vma->vm_flags & VM_EXECUTABLE)
772 removed_exe_file_vma(mm);
773 }
774 put_nommu_region(vma->vm_region);
775 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700776}
777
778/*
779 * look up the first VMA in which addr resides, NULL if none
780 * - should be called with mm->mmap_sem at least held readlocked
781 */
782struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
783{
David Howells8feae132009-01-08 12:04:47 +0000784 struct vm_area_struct *vma;
785 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700786
David Howells8feae132009-01-08 12:04:47 +0000787 /* check the cache first */
788 vma = mm->mmap_cache;
789 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
790 return vma;
791
792 /* trawl the tree (there may be multiple mappings in which addr
793 * resides) */
794 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
795 vma = rb_entry(n, struct vm_area_struct, vm_rb);
796 if (vma->vm_start > addr)
797 return NULL;
798 if (vma->vm_end > addr) {
799 mm->mmap_cache = vma;
800 return vma;
801 }
David Howells30340972006-09-27 01:50:20 -0700802 }
803
David Howells30340972006-09-27 01:50:20 -0700804 return NULL;
805}
806EXPORT_SYMBOL(find_vma);
807
808/*
David Howells930e6522006-09-27 01:50:22 -0700809 * find a VMA
810 * - we don't extend stack VMAs under NOMMU conditions
811 */
812struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
813{
David Howells7561e8c2010-03-25 16:48:38 +0000814 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700815}
816
David Howells8feae132009-01-08 12:04:47 +0000817/*
818 * expand a stack to a given address
819 * - not supported under NOMMU conditions
820 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700821int expand_stack(struct vm_area_struct *vma, unsigned long address)
822{
823 return -ENOMEM;
824}
825
David Howells930e6522006-09-27 01:50:22 -0700826/*
David Howells6fa5f802006-09-27 01:50:21 -0700827 * look up the first VMA exactly that exactly matches addr
828 * - should be called with mm->mmap_sem at least held readlocked
829 */
David Howells8feae132009-01-08 12:04:47 +0000830static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
831 unsigned long addr,
832 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000835 struct rb_node *n = mm->mm_rb.rb_node;
836 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
David Howells8feae132009-01-08 12:04:47 +0000838 /* check the cache first */
839 vma = mm->mmap_cache;
840 if (vma && vma->vm_start == addr && vma->vm_end == end)
841 return vma;
842
843 /* trawl the tree (there may be multiple mappings in which addr
844 * resides) */
845 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000847 if (vma->vm_start < addr)
848 continue;
849 if (vma->vm_start > addr)
850 return NULL;
851 if (vma->vm_end == end) {
852 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return vma;
David Howells8feae132009-01-08 12:04:47 +0000854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 return NULL;
858}
859
David Howells30340972006-09-27 01:50:20 -0700860/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 * determine whether a mapping should be permitted and, if so, what sort of
862 * mapping we're capable of supporting
863 */
864static int validate_mmap_request(struct file *file,
865 unsigned long addr,
866 unsigned long len,
867 unsigned long prot,
868 unsigned long flags,
869 unsigned long pgoff,
870 unsigned long *_capabilities)
871{
David Howells8feae132009-01-08 12:04:47 +0000872 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 unsigned long reqprot = prot;
874 int ret;
875
876 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100877 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 printk(KERN_DEBUG
879 "%d: Can't do fixed-address/overlay mmap of RAM\n",
880 current->pid);
881 return -EINVAL;
882 }
883
884 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
885 (flags & MAP_TYPE) != MAP_SHARED)
886 return -EINVAL;
887
Mike Frysingerf81cff02006-12-06 12:02:59 +1000888 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -EINVAL;
890
Mike Frysingerf81cff02006-12-06 12:02:59 +1000891 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000892 rlen = PAGE_ALIGN(len);
893 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000894 return -ENOMEM;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000897 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000898 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (file) {
901 /* validate file mapping requests */
902 struct address_space *mapping;
903
904 /* files must support mmap */
905 if (!file->f_op || !file->f_op->mmap)
906 return -ENODEV;
907
908 /* work out if what we've got could possibly be shared
909 * - we support chardevs that provide their own "memory"
910 * - we support files/blockdevs that are memory backed
911 */
912 mapping = file->f_mapping;
913 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800914 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 capabilities = 0;
917 if (mapping && mapping->backing_dev_info)
918 capabilities = mapping->backing_dev_info->capabilities;
919
920 if (!capabilities) {
921 /* no explicit capabilities set, so assume some
922 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800923 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 case S_IFREG:
925 case S_IFBLK:
926 capabilities = BDI_CAP_MAP_COPY;
927 break;
928
929 case S_IFCHR:
930 capabilities =
931 BDI_CAP_MAP_DIRECT |
932 BDI_CAP_READ_MAP |
933 BDI_CAP_WRITE_MAP;
934 break;
935
936 default:
937 return -EINVAL;
938 }
939 }
940
941 /* eliminate any capabilities that we can't support on this
942 * device */
943 if (!file->f_op->get_unmapped_area)
944 capabilities &= ~BDI_CAP_MAP_DIRECT;
945 if (!file->f_op->read)
946 capabilities &= ~BDI_CAP_MAP_COPY;
947
Graff Yang28d7a6a2009-08-18 14:11:17 -0700948 /* The file shall have been opened with read permission. */
949 if (!(file->f_mode & FMODE_READ))
950 return -EACCES;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (flags & MAP_SHARED) {
953 /* do checks for writing, appending and locking */
954 if ((prot & PROT_WRITE) &&
955 !(file->f_mode & FMODE_WRITE))
956 return -EACCES;
957
Josef Sipeke9536ae2006-12-08 02:37:21 -0800958 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 (file->f_mode & FMODE_WRITE))
960 return -EACCES;
961
Josef Sipeke9536ae2006-12-08 02:37:21 -0800962 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -EAGAIN;
964
965 if (!(capabilities & BDI_CAP_MAP_DIRECT))
966 return -ENODEV;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* we mustn't privatise shared mappings */
969 capabilities &= ~BDI_CAP_MAP_COPY;
970 }
971 else {
972 /* we're going to read the file into private memory we
973 * allocate */
974 if (!(capabilities & BDI_CAP_MAP_COPY))
975 return -ENODEV;
976
977 /* we don't permit a private writable mapping to be
978 * shared with the backing device */
979 if (prot & PROT_WRITE)
980 capabilities &= ~BDI_CAP_MAP_DIRECT;
981 }
982
Bernd Schmidt3c7b2042010-05-25 23:43:00 -0700983 if (capabilities & BDI_CAP_MAP_DIRECT) {
984 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
985 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
986 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
987 ) {
988 capabilities &= ~BDI_CAP_MAP_DIRECT;
989 if (flags & MAP_SHARED) {
990 printk(KERN_WARNING
991 "MAP_SHARED not completely supported on !MMU\n");
992 return -EINVAL;
993 }
994 }
995 }
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* handle executable mappings and implied executable
998 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800999 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (prot & PROT_EXEC)
1001 return -EPERM;
1002 }
1003 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1004 /* handle implication of PROT_EXEC by PROT_READ */
1005 if (current->personality & READ_IMPLIES_EXEC) {
1006 if (capabilities & BDI_CAP_EXEC_MAP)
1007 prot |= PROT_EXEC;
1008 }
1009 }
1010 else if ((prot & PROT_READ) &&
1011 (prot & PROT_EXEC) &&
1012 !(capabilities & BDI_CAP_EXEC_MAP)
1013 ) {
1014 /* backing file is not executable, try to copy */
1015 capabilities &= ~BDI_CAP_MAP_DIRECT;
1016 }
1017 }
1018 else {
1019 /* anonymous mappings are always memory backed and can be
1020 * privately mapped
1021 */
1022 capabilities = BDI_CAP_MAP_COPY;
1023
1024 /* handle PROT_EXEC implication by PROT_READ */
1025 if ((prot & PROT_READ) &&
1026 (current->personality & READ_IMPLIES_EXEC))
1027 prot |= PROT_EXEC;
1028 }
1029
1030 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -04001031 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (ret < 0)
1033 return ret;
1034
1035 /* looks okay */
1036 *_capabilities = capabilities;
1037 return 0;
1038}
1039
1040/*
1041 * we've determined that we can make the mapping, now translate what we
1042 * now know into VMA flags
1043 */
1044static unsigned long determine_vm_flags(struct file *file,
1045 unsigned long prot,
1046 unsigned long flags,
1047 unsigned long capabilities)
1048{
1049 unsigned long vm_flags;
1050
1051 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* vm_flags |= mm->def_flags; */
1053
1054 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1055 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001056 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (file && !(prot & PROT_WRITE))
1058 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001059 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* overlay a shareable mapping on the backing device or inode
1061 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1062 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001063 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001065 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
1068 /* refuse to let anyone share private mappings with this process if
1069 * it's being traced - otherwise breakpoints set in it may interfere
1070 * with another untraced process
1071 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001072 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 vm_flags &= ~VM_MAYSHARE;
1074
1075 return vm_flags;
1076}
1077
1078/*
David Howells8feae132009-01-08 12:04:47 +00001079 * set up a shared mapping on a file (the driver or filesystem provides and
1080 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
David Howells8feae132009-01-08 12:04:47 +00001082static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 int ret;
1085
1086 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001087 if (ret == 0) {
1088 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001089 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (ret != -ENOSYS)
1092 return ret;
1093
David Howells3fa30462010-03-23 13:35:21 -07001094 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1095 * opposed to tried but failed) so we can only give a suitable error as
1096 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -ENODEV;
1098}
1099
1100/*
1101 * set up a private mapping or an anonymous shared mapping
1102 */
David Howells8feae132009-01-08 12:04:47 +00001103static int do_mmap_private(struct vm_area_struct *vma,
1104 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001105 unsigned long len,
1106 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
David Howells8feae132009-01-08 12:04:47 +00001108 struct page *pages;
1109 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 void *base;
David Howells8feae132009-01-08 12:04:47 +00001111 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /* invoke the file's mapping function so that it can keep track of
1114 * shared mappings on devices or memory
1115 * - VM_MAYSHARE will be set if it may attempt to share
1116 */
David Howells645d83c2009-09-24 15:13:10 +01001117 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001119 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001121 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1122 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001125 if (ret != -ENOSYS)
1126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* getting an ENOSYS error indicates that direct mmap isn't
1129 * possible (as opposed to tried but failed) so we'll try to
1130 * make a private copy of the data and map that instead */
1131 }
1132
David Howells8feae132009-01-08 12:04:47 +00001133 rlen = PAGE_ALIGN(len);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /* allocate some memory to hold the mapping
1136 * - note that this may not return a page-aligned address if the object
1137 * we're allocating is smaller than a page
1138 */
David Howells8feae132009-01-08 12:04:47 +00001139 order = get_order(rlen);
1140 kdebug("alloc order %d for %lx", order, len);
1141
1142 pages = alloc_pages(GFP_KERNEL, order);
1143 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto enomem;
1145
David Howells8feae132009-01-08 12:04:47 +00001146 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001147 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
David Howells8feae132009-01-08 12:04:47 +00001149 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001150
1151 /* we allocated a power-of-2 sized page set, so we may want to trim off
1152 * the excess */
1153 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1154 while (total > point) {
1155 order = ilog2(total - point);
1156 n = 1 << order;
1157 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001158 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001159 total -= n;
1160 set_page_refcounted(pages + total);
1161 __free_pages(pages + total, order);
1162 }
David Howells8feae132009-01-08 12:04:47 +00001163 }
1164
David Howells8feae132009-01-08 12:04:47 +00001165 for (point = 1; point < total; point++)
1166 set_page_refcounted(&pages[point]);
1167
1168 base = page_address(pages);
1169 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1170 region->vm_start = (unsigned long) base;
1171 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001172 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001173
1174 vma->vm_start = region->vm_start;
1175 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (vma->vm_file) {
1178 /* read the contents of a file into the copy */
1179 mm_segment_t old_fs;
1180 loff_t fpos;
1181
1182 fpos = vma->vm_pgoff;
1183 fpos <<= PAGE_SHIFT;
1184
1185 old_fs = get_fs();
1186 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001187 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 set_fs(old_fs);
1189
1190 if (ret < 0)
1191 goto error_free;
1192
1193 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001194 if (ret < rlen)
1195 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 return 0;
1200
1201error_free:
David Howells8feae132009-01-08 12:04:47 +00001202 free_page_series(region->vm_start, region->vm_end);
1203 region->vm_start = vma->vm_start = 0;
1204 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001205 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return ret;
1207
1208enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001209 printk("Allocation of length %lu from process %d (%s) failed\n",
1210 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 show_free_areas();
1212 return -ENOMEM;
1213}
1214
1215/*
1216 * handle mapping creation for uClinux
1217 */
1218unsigned long do_mmap_pgoff(struct file *file,
1219 unsigned long addr,
1220 unsigned long len,
1221 unsigned long prot,
1222 unsigned long flags,
1223 unsigned long pgoff)
1224{
David Howells8feae132009-01-08 12:04:47 +00001225 struct vm_area_struct *vma;
1226 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001228 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int ret;
1230
David Howells8feae132009-01-08 12:04:47 +00001231 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* decide whether we should attempt the mapping, and if so what sort of
1234 * mapping */
1235 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1236 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001237 if (ret < 0) {
1238 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return ret;
David Howells8feae132009-01-08 12:04:47 +00001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
David Howells06aab5a2009-09-24 12:33:48 +01001242 /* we ignore the address hint */
1243 addr = 0;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* we've determined that we can make the mapping, now translate what we
1246 * now know into VMA flags */
1247 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1248
David Howells8feae132009-01-08 12:04:47 +00001249 /* we're going to need to record the mapping */
1250 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1251 if (!region)
1252 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
David Howells8feae132009-01-08 12:04:47 +00001254 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1255 if (!vma)
1256 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
David Howells1e2ae592010-01-15 17:01:33 -08001258 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001259 region->vm_flags = vm_flags;
1260 region->vm_pgoff = pgoff;
1261
Rik van Riel5beb4932010-03-05 13:42:07 -08001262 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001263 vma->vm_flags = vm_flags;
1264 vma->vm_pgoff = pgoff;
1265
1266 if (file) {
1267 region->vm_file = file;
1268 get_file(file);
1269 vma->vm_file = file;
1270 get_file(file);
1271 if (vm_flags & VM_EXECUTABLE) {
1272 added_exe_file_vma(current->mm);
1273 vma->vm_mm = current->mm;
1274 }
1275 }
1276
1277 down_write(&nommu_region_sem);
1278
1279 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001281 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * - shared mappings on character devices and memory backed files are
1283 * permitted to overlap inexactly as far as we are concerned for in
1284 * these cases, sharing is handled in the driver or filesystem rather
1285 * than here
1286 */
1287 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001288 struct vm_region *pregion;
1289 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
David Howells8feae132009-01-08 12:04:47 +00001291 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1292 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001293
David Howells8feae132009-01-08 12:04:47 +00001294 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1295 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
David Howells8feae132009-01-08 12:04:47 +00001297 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 continue;
1299
1300 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001301 if (pregion->vm_file->f_path.dentry->d_inode !=
1302 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 continue;
1304
David Howells8feae132009-01-08 12:04:47 +00001305 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 continue;
1307
David Howells8feae132009-01-08 12:04:47 +00001308 rpglen = pregion->vm_end - pregion->vm_start;
1309 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1310 rpgend = pregion->vm_pgoff + rpglen;
1311 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 continue;
1313
David Howells8feae132009-01-08 12:04:47 +00001314 /* handle inexactly overlapping matches between
1315 * mappings */
1316 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1317 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1318 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1320 goto sharing_violation;
1321 continue;
1322 }
1323
David Howells8feae132009-01-08 12:04:47 +00001324 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001325 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001326 vma->vm_region = pregion;
1327 start = pregion->vm_start;
1328 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1329 vma->vm_start = start;
1330 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
David Howells8feae132009-01-08 12:04:47 +00001332 if (pregion->vm_flags & VM_MAPPED_COPY) {
1333 kdebug("share copy");
1334 vma->vm_flags |= VM_MAPPED_COPY;
1335 } else {
1336 kdebug("share mmap");
1337 ret = do_mmap_shared_file(vma);
1338 if (ret < 0) {
1339 vma->vm_region = NULL;
1340 vma->vm_start = 0;
1341 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001342 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001343 pregion = NULL;
1344 goto error_just_free;
1345 }
1346 }
1347 fput(region->vm_file);
1348 kmem_cache_free(vm_region_jar, region);
1349 region = pregion;
1350 result = start;
1351 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* obtain the address at which to make a shared mapping
1355 * - this is the hook for quasi-memory character devices to
1356 * tell us the location of a shared mapping
1357 */
David Howells645d83c2009-09-24 15:13:10 +01001358 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 addr = file->f_op->get_unmapped_area(file, addr, len,
1360 pgoff, flags);
1361 if (IS_ERR((void *) addr)) {
1362 ret = addr;
1363 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001364 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* the driver refused to tell us where to site
1367 * the mapping so we'll have to attempt to copy
1368 * it */
1369 ret = (unsigned long) -ENODEV;
1370 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001371 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001374 } else {
1375 vma->vm_start = region->vm_start = addr;
1376 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 }
1379 }
1380
David Howells8feae132009-01-08 12:04:47 +00001381 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
David Howells645d83c2009-09-24 15:13:10 +01001383 /* set up the mapping
1384 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001387 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 else
David Howells645d83c2009-09-24 15:13:10 +01001389 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001391 goto error_just_free;
1392 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001393
Jie Zhangea637632009-12-14 18:00:02 -08001394 /* clear anonymous mappings that don't ask for uninitialized data */
1395 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1396 memset((void *)region->vm_start, 0,
1397 region->vm_end - region->vm_start);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001400 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 current->mm->total_vm += len >> PAGE_SHIFT;
1403
David Howells8feae132009-01-08 12:04:47 +00001404share:
1405 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Mike Frysingercfe79c02010-01-06 17:23:23 +00001407 /* we flush the region from the icache only when the first executable
1408 * mapping of it is made */
1409 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1410 flush_icache_range(region->vm_start, region->vm_end);
1411 region->vm_icache_flushed = true;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Mike Frysingercfe79c02010-01-06 17:23:23 +00001414 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
David Howells8feae132009-01-08 12:04:47 +00001416 kleave(" = %lx", result);
1417 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
David Howells8feae132009-01-08 12:04:47 +00001419error_just_free:
1420 up_write(&nommu_region_sem);
1421error:
David Howells89a86402009-10-30 13:13:26 +00001422 if (region->vm_file)
1423 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001424 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001425 if (vma->vm_file)
1426 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001427 if (vma->vm_flags & VM_EXECUTABLE)
1428 removed_exe_file_vma(vma->vm_mm);
1429 kmem_cache_free(vm_area_cachep, vma);
1430 kleave(" = %d", ret);
1431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
David Howells8feae132009-01-08 12:04:47 +00001433sharing_violation:
1434 up_write(&nommu_region_sem);
1435 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1436 ret = -EINVAL;
1437 goto error;
1438
1439error_getting_vma:
1440 kmem_cache_free(vm_region_jar, region);
1441 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1442 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 len, current->pid);
1444 show_free_areas();
1445 return -ENOMEM;
1446
David Howells8feae132009-01-08 12:04:47 +00001447error_getting_region:
1448 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1449 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 len, current->pid);
1451 show_free_areas();
1452 return -ENOMEM;
1453}
Paul Mundtb5073172007-07-21 04:37:25 -07001454EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001456SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1457 unsigned long, prot, unsigned long, flags,
1458 unsigned long, fd, unsigned long, pgoff)
1459{
1460 struct file *file = NULL;
1461 unsigned long retval = -EBADF;
1462
Al Viro120a7952010-10-30 02:54:44 -04001463 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001464 if (!(flags & MAP_ANONYMOUS)) {
1465 file = fget(fd);
1466 if (!file)
1467 goto out;
1468 }
1469
1470 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1471
1472 down_write(&current->mm->mmap_sem);
1473 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1474 up_write(&current->mm->mmap_sem);
1475
1476 if (file)
1477 fput(file);
1478out:
1479 return retval;
1480}
1481
Christoph Hellwiga4679372010-03-10 15:21:15 -08001482#ifdef __ARCH_WANT_SYS_OLD_MMAP
1483struct mmap_arg_struct {
1484 unsigned long addr;
1485 unsigned long len;
1486 unsigned long prot;
1487 unsigned long flags;
1488 unsigned long fd;
1489 unsigned long offset;
1490};
1491
1492SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1493{
1494 struct mmap_arg_struct a;
1495
1496 if (copy_from_user(&a, arg, sizeof(a)))
1497 return -EFAULT;
1498 if (a.offset & ~PAGE_MASK)
1499 return -EINVAL;
1500
1501 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1502 a.offset >> PAGE_SHIFT);
1503}
1504#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/*
David Howells8feae132009-01-08 12:04:47 +00001507 * split a vma into two pieces at address 'addr', a new vma is allocated either
1508 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
David Howells8feae132009-01-08 12:04:47 +00001510int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1511 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
David Howells8feae132009-01-08 12:04:47 +00001513 struct vm_area_struct *new;
1514 struct vm_region *region;
1515 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
David Howells8feae132009-01-08 12:04:47 +00001517 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
David Howells779c1022010-01-15 17:01:34 -08001519 /* we're only permitted to split anonymous regions (these should have
1520 * only a single usage on the region) */
1521 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001522 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
David Howells8feae132009-01-08 12:04:47 +00001524 if (mm->map_count >= sysctl_max_map_count)
1525 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
David Howells8feae132009-01-08 12:04:47 +00001527 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1528 if (!region)
1529 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
David Howells8feae132009-01-08 12:04:47 +00001531 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1532 if (!new) {
1533 kmem_cache_free(vm_region_jar, region);
1534 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
David Howells8feae132009-01-08 12:04:47 +00001536
1537 /* most fields are the same, copy all, and then fixup */
1538 *new = *vma;
1539 *region = *vma->vm_region;
1540 new->vm_region = region;
1541
1542 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1543
1544 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001545 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001546 } else {
1547 region->vm_start = new->vm_start = addr;
1548 region->vm_pgoff = new->vm_pgoff += npages;
1549 }
1550
1551 if (new->vm_ops && new->vm_ops->open)
1552 new->vm_ops->open(new);
1553
1554 delete_vma_from_mm(vma);
1555 down_write(&nommu_region_sem);
1556 delete_nommu_region(vma->vm_region);
1557 if (new_below) {
1558 vma->vm_region->vm_start = vma->vm_start = addr;
1559 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1560 } else {
1561 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001562 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001563 }
1564 add_nommu_region(vma->vm_region);
1565 add_nommu_region(new->vm_region);
1566 up_write(&nommu_region_sem);
1567 add_vma_to_mm(mm, vma);
1568 add_vma_to_mm(mm, new);
1569 return 0;
1570}
1571
1572/*
1573 * shrink a VMA by removing the specified chunk from either the beginning or
1574 * the end
1575 */
1576static int shrink_vma(struct mm_struct *mm,
1577 struct vm_area_struct *vma,
1578 unsigned long from, unsigned long to)
1579{
1580 struct vm_region *region;
1581
1582 kenter("");
1583
1584 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1585 * and list */
1586 delete_vma_from_mm(vma);
1587 if (from > vma->vm_start)
1588 vma->vm_end = from;
1589 else
1590 vma->vm_start = to;
1591 add_vma_to_mm(mm, vma);
1592
1593 /* cut the backing region down to size */
1594 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001595 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001596
1597 down_write(&nommu_region_sem);
1598 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001599 if (from > region->vm_start) {
1600 to = region->vm_top;
1601 region->vm_top = region->vm_end = from;
1602 } else {
David Howells8feae132009-01-08 12:04:47 +00001603 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001604 }
David Howells8feae132009-01-08 12:04:47 +00001605 add_nommu_region(region);
1606 up_write(&nommu_region_sem);
1607
1608 free_page_series(from, to);
1609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
David Howells30340972006-09-27 01:50:20 -07001612/*
1613 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001614 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1615 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001616 */
David Howells8feae132009-01-08 12:04:47 +00001617int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
David Howells8feae132009-01-08 12:04:47 +00001619 struct vm_area_struct *vma;
1620 struct rb_node *rb;
1621 unsigned long end = start + len;
1622 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
David Howells8feae132009-01-08 12:04:47 +00001624 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
David Howells8feae132009-01-08 12:04:47 +00001626 if (len == 0)
1627 return -EINVAL;
1628
1629 /* find the first potentially overlapping VMA */
1630 vma = find_vma(mm, start);
1631 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001632 static int limit = 0;
1633 if (limit < 5) {
1634 printk(KERN_WARNING
1635 "munmap of memory not mmapped by process %d"
1636 " (%s): 0x%lx-0x%lx\n",
1637 current->pid, current->comm,
1638 start, start + len - 1);
1639 limit++;
1640 }
David Howells8feae132009-01-08 12:04:47 +00001641 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
David Howells8feae132009-01-08 12:04:47 +00001644 /* we're allowed to split an anonymous VMA but not a file-backed one */
1645 if (vma->vm_file) {
1646 do {
1647 if (start > vma->vm_start) {
1648 kleave(" = -EINVAL [miss]");
1649 return -EINVAL;
1650 }
1651 if (end == vma->vm_end)
1652 goto erase_whole_vma;
1653 rb = rb_next(&vma->vm_rb);
1654 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1655 } while (rb);
1656 kleave(" = -EINVAL [split file]");
1657 return -EINVAL;
1658 } else {
1659 /* the chunk must be a subset of the VMA found */
1660 if (start == vma->vm_start && end == vma->vm_end)
1661 goto erase_whole_vma;
1662 if (start < vma->vm_start || end > vma->vm_end) {
1663 kleave(" = -EINVAL [superset]");
1664 return -EINVAL;
1665 }
1666 if (start & ~PAGE_MASK) {
1667 kleave(" = -EINVAL [unaligned start]");
1668 return -EINVAL;
1669 }
1670 if (end != vma->vm_end && end & ~PAGE_MASK) {
1671 kleave(" = -EINVAL [unaligned split]");
1672 return -EINVAL;
1673 }
1674 if (start != vma->vm_start && end != vma->vm_end) {
1675 ret = split_vma(mm, vma, start, 1);
1676 if (ret < 0) {
1677 kleave(" = %d [split]", ret);
1678 return ret;
1679 }
1680 }
1681 return shrink_vma(mm, vma, start, end);
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
David Howells8feae132009-01-08 12:04:47 +00001684erase_whole_vma:
1685 delete_vma_from_mm(vma);
1686 delete_vma(mm, vma);
1687 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return 0;
1689}
Paul Mundtb5073172007-07-21 04:37:25 -07001690EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001692SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001693{
1694 int ret;
1695 struct mm_struct *mm = current->mm;
1696
1697 down_write(&mm->mmap_sem);
1698 ret = do_munmap(mm, addr, len);
1699 up_write(&mm->mmap_sem);
1700 return ret;
1701}
1702
1703/*
David Howells8feae132009-01-08 12:04:47 +00001704 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001705 */
David Howells8feae132009-01-08 12:04:47 +00001706void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
David Howells8feae132009-01-08 12:04:47 +00001708 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
David Howells8feae132009-01-08 12:04:47 +00001710 if (!mm)
1711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
David Howells8feae132009-01-08 12:04:47 +00001713 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
David Howells8feae132009-01-08 12:04:47 +00001715 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
David Howells8feae132009-01-08 12:04:47 +00001717 while ((vma = mm->mmap)) {
1718 mm->mmap = vma->vm_next;
1719 delete_vma_from_mm(vma);
1720 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001721 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
David Howells8feae132009-01-08 12:04:47 +00001723
1724 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727unsigned long do_brk(unsigned long addr, unsigned long len)
1728{
1729 return -ENOMEM;
1730}
1731
1732/*
David Howells6fa5f802006-09-27 01:50:21 -07001733 * expand (or shrink) an existing mapping, potentially moving it at the same
1734 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 *
David Howells6fa5f802006-09-27 01:50:21 -07001736 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001737 * as long as it stays within the region allocated by do_mmap_private() and the
1738 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 *
David Howells6fa5f802006-09-27 01:50:21 -07001740 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
1742unsigned long do_mremap(unsigned long addr,
1743 unsigned long old_len, unsigned long new_len,
1744 unsigned long flags, unsigned long new_addr)
1745{
David Howells6fa5f802006-09-27 01:50:21 -07001746 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001749 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return (unsigned long) -EINVAL;
1751
David Howells8feae132009-01-08 12:04:47 +00001752 if (addr & ~PAGE_MASK)
1753 return -EINVAL;
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (flags & MREMAP_FIXED && new_addr != addr)
1756 return (unsigned long) -EINVAL;
1757
David Howells8feae132009-01-08 12:04:47 +00001758 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001759 if (!vma)
1760 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
David Howells6fa5f802006-09-27 01:50:21 -07001762 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return (unsigned long) -EFAULT;
1764
David Howells6fa5f802006-09-27 01:50:21 -07001765 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return (unsigned long) -EPERM;
1767
David Howells8feae132009-01-08 12:04:47 +00001768 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return (unsigned long) -ENOMEM;
1770
1771 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001772 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001773 return vma->vm_start;
1774}
Paul Mundtb5073172007-07-21 04:37:25 -07001775EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001776
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001777SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1778 unsigned long, new_len, unsigned long, flags,
1779 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001780{
1781 unsigned long ret;
1782
1783 down_write(&current->mm->mmap_sem);
1784 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1785 up_write(&current->mm->mmap_sem);
1786 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
Linus Torvalds6aab3412005-11-28 14:34:23 -08001789struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001790 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 return NULL;
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1796 unsigned long to, unsigned long size, pgprot_t prot)
1797{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001798 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
Luke Yang22c4af42006-07-14 00:24:09 -07001801EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Paul Mundtf905bc42008-02-04 22:29:59 -08001803int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1804 unsigned long pgoff)
1805{
1806 unsigned int size = vma->vm_end - vma->vm_start;
1807
1808 if (!(vma->vm_flags & VM_USERMAP))
1809 return -EINVAL;
1810
1811 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1812 vma->vm_end = vma->vm_start + size;
1813
1814 return 0;
1815}
1816EXPORT_SYMBOL(remap_vmalloc_range);
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1819{
1820}
1821
1822unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1823 unsigned long len, unsigned long pgoff, unsigned long flags)
1824{
1825 return -ENOMEM;
1826}
1827
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001828void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832void unmap_mapping_range(struct address_space *mapping,
1833 loff_t const holebegin, loff_t const holelen,
1834 int even_cows)
1835{
1836}
Luke Yang22c4af42006-07-14 00:24:09 -07001837EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839/*
1840 * Check that a process has enough memory to allocate a new virtual
1841 * mapping. 0 means there is enough memory for the allocation to
1842 * succeed and -ENOMEM implies there is not.
1843 *
1844 * We currently support three overcommit policies, which are set via the
1845 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1846 *
1847 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1848 * Additional code 2002 Jul 20 by Robert Love.
1849 *
1850 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1851 *
1852 * Note this is a helper function intended to be used by LSMs which
1853 * wish to use this logic.
1854 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001855int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 unsigned long free, allowed;
1858
1859 vm_acct_memory(pages);
1860
1861 /*
1862 * Sometimes we want to use more memory than we have
1863 */
1864 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1865 return 0;
1866
1867 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1868 unsigned long n;
1869
Christoph Lameter347ce432006-06-30 01:55:35 -07001870 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 free += nr_swap_pages;
1872
1873 /*
1874 * Any slabs which are created with the
1875 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1876 * which are reclaimable, under pressure. The dentry
1877 * cache and most inode caches should fall into this
1878 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001879 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /*
1882 * Leave the last 3% for root
1883 */
1884 if (!cap_sys_admin)
1885 free -= free / 32;
1886
1887 if (free > pages)
1888 return 0;
1889
1890 /*
1891 * nr_free_pages() is very expensive on large systems,
1892 * only call if we're about to fail.
1893 */
1894 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001895
1896 /*
1897 * Leave reserved pages. The pages are not for anonymous pages.
1898 */
1899 if (n <= totalreserve_pages)
1900 goto error;
1901 else
1902 n -= totalreserve_pages;
1903
1904 /*
1905 * Leave the last 3% for root
1906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (!cap_sys_admin)
1908 n -= n / 32;
1909 free += n;
1910
1911 if (free > pages)
1912 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001913
1914 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
1917 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1918 /*
1919 * Leave the last 3% for root
1920 */
1921 if (!cap_sys_admin)
1922 allowed -= allowed / 32;
1923 allowed += total_swap_pages;
1924
1925 /* Don't let a single process grow too big:
1926 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001927 if (mm)
1928 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001930 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001932
Hideo AOKId5ddc792006-04-10 22:53:01 -07001933error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 vm_unacct_memory(pages);
1935
1936 return -ENOMEM;
1937}
1938
1939int in_gate_area_no_task(unsigned long addr)
1940{
1941 return 0;
1942}
David Howellsb0e15192006-01-06 00:11:42 -08001943
Nick Piggind0217ac2007-07-19 01:47:03 -07001944int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001945{
1946 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001947 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001948}
Paul Mundtb5073172007-07-21 04:37:25 -07001949EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001950
1951/*
1952 * Access another process' address space.
1953 * - source/target buffer must be kernel space
1954 */
1955int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1956{
David Howells0ec76a12006-09-27 01:50:15 -07001957 struct vm_area_struct *vma;
1958 struct mm_struct *mm;
1959
1960 if (addr + len < addr)
1961 return 0;
1962
1963 mm = get_task_mm(tsk);
1964 if (!mm)
1965 return 0;
1966
1967 down_read(&mm->mmap_sem);
1968
1969 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001970 vma = find_vma(mm, addr);
1971 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001972 /* don't overrun this mapping */
1973 if (addr + len >= vma->vm_end)
1974 len = vma->vm_end - addr;
1975
1976 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001977 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00001978 copy_to_user_page(vma, NULL, addr,
1979 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001980 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00001981 copy_from_user_page(vma, NULL, addr,
1982 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07001983 else
1984 len = 0;
1985 } else {
1986 len = 0;
1987 }
1988
1989 up_read(&mm->mmap_sem);
1990 mmput(mm);
1991 return len;
1992}
David Howells7e660872010-01-15 17:01:39 -08001993
1994/**
1995 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1996 * @inode: The inode to check
1997 * @size: The current filesize of the inode
1998 * @newsize: The proposed filesize of the inode
1999 *
2000 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2001 * make sure that that any outstanding VMAs aren't broken and then shrink the
2002 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2003 * automatically grant mappings that are too large.
2004 */
2005int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2006 size_t newsize)
2007{
2008 struct vm_area_struct *vma;
2009 struct prio_tree_iter iter;
2010 struct vm_region *region;
2011 pgoff_t low, high;
2012 size_t r_size, r_top;
2013
2014 low = newsize >> PAGE_SHIFT;
2015 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2016
2017 down_write(&nommu_region_sem);
2018
2019 /* search for VMAs that fall within the dead zone */
2020 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2021 low, high) {
2022 /* found one - only interested if it's shared out of the page
2023 * cache */
2024 if (vma->vm_flags & VM_SHARED) {
2025 up_write(&nommu_region_sem);
2026 return -ETXTBSY; /* not quite true, but near enough */
2027 }
2028 }
2029
2030 /* reduce any regions that overlap the dead zone - if in existence,
2031 * these will be pointed to by VMAs that don't overlap the dead zone
2032 *
2033 * we don't check for any regions that start beyond the EOF as there
2034 * shouldn't be any
2035 */
2036 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2037 0, ULONG_MAX) {
2038 if (!(vma->vm_flags & VM_SHARED))
2039 continue;
2040
2041 region = vma->vm_region;
2042 r_size = region->vm_top - region->vm_start;
2043 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2044
2045 if (r_top > newsize) {
2046 region->vm_top -= r_top - newsize;
2047 if (region->vm_end > region->vm_top)
2048 region->vm_end = region->vm_top;
2049 }
2050 }
2051
2052 up_write(&nommu_region_sem);
2053 return 0;
2054}