blob: ef4045d010d5f37631c3ad2f9de0e824e1de65a7 [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 Mundt29c185e2010-12-24 12:08:30 +090013 * Copyright (c) 2007-2010 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 Mundt29c185e2010-12-24 12:08:30 +0900444/**
445 * alloc_vm_area - allocate a range of kernel address space
446 * @size: size of the area
447 *
448 * Returns: NULL on failure, vm_struct on success
449 *
450 * This function reserves a range of kernel address space, and
451 * allocates pagetables to map that range. No actual mappings
452 * are created. If the kernel address space is not shared
453 * between processes, it syncs the pagetable across all
454 * processes.
455 */
456struct vm_struct *alloc_vm_area(size_t size)
457{
458 BUG();
459 return NULL;
460}
461EXPORT_SYMBOL_GPL(alloc_vm_area);
462
463void free_vm_area(struct vm_struct *area)
464{
465 BUG();
466}
467EXPORT_SYMBOL_GPL(free_vm_area);
468
Paul Mundtb5073172007-07-21 04:37:25 -0700469int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
470 struct page *page)
471{
472 return -EINVAL;
473}
474EXPORT_SYMBOL(vm_insert_page);
475
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * sys_brk() for the most part doesn't need the global kernel
478 * lock, except when an application is doing something nasty
479 * like trying to un-brk an area that has already been mapped
480 * to a regular file. in this case, the unmapping will need
481 * to invoke file system routines that need the global lock.
482 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100483SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct mm_struct *mm = current->mm;
486
487 if (brk < mm->start_brk || brk > mm->context.end_brk)
488 return mm->brk;
489
490 if (mm->brk == brk)
491 return mm->brk;
492
493 /*
494 * Always allow shrinking brk
495 */
496 if (brk <= mm->brk) {
497 mm->brk = brk;
498 return brk;
499 }
500
501 /*
502 * Ok, looks good - let it rip.
503 */
Mike Frysingercfe79c02010-01-06 17:23:23 +0000504 flush_icache_range(mm->brk, brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return mm->brk = brk;
506}
507
David Howells8feae132009-01-08 12:04:47 +0000508/*
509 * initialise the VMA and region record slabs
510 */
511void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700513 int ret;
514
515 ret = percpu_counter_init(&vm_committed_as, 0);
516 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700517 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David Howells8feae132009-01-08 12:04:47 +0000520/*
521 * validate the region tree
522 * - the caller must hold the region lock
523 */
524#ifdef CONFIG_DEBUG_NOMMU_REGIONS
525static noinline void validate_nommu_regions(void)
526{
527 struct vm_region *region, *last;
528 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
David Howells8feae132009-01-08 12:04:47 +0000530 lastp = rb_first(&nommu_region_tree);
531 if (!lastp)
532 return;
533
534 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700535 BUG_ON(unlikely(last->vm_end <= last->vm_start));
536 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000537
538 while ((p = rb_next(lastp))) {
539 region = rb_entry(p, struct vm_region, vm_rb);
540 last = rb_entry(lastp, struct vm_region, vm_rb);
541
David Howells33e5d7692009-04-02 16:56:32 -0700542 BUG_ON(unlikely(region->vm_end <= region->vm_start));
543 BUG_ON(unlikely(region->vm_top < region->vm_end));
544 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000545
546 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548}
David Howells8feae132009-01-08 12:04:47 +0000549#else
David Howells33e5d7692009-04-02 16:56:32 -0700550static void validate_nommu_regions(void)
551{
552}
David Howells8feae132009-01-08 12:04:47 +0000553#endif
554
555/*
556 * add a region into the global tree
557 */
558static void add_nommu_region(struct vm_region *region)
559{
560 struct vm_region *pregion;
561 struct rb_node **p, *parent;
562
563 validate_nommu_regions();
564
David Howells8feae132009-01-08 12:04:47 +0000565 parent = NULL;
566 p = &nommu_region_tree.rb_node;
567 while (*p) {
568 parent = *p;
569 pregion = rb_entry(parent, struct vm_region, vm_rb);
570 if (region->vm_start < pregion->vm_start)
571 p = &(*p)->rb_left;
572 else if (region->vm_start > pregion->vm_start)
573 p = &(*p)->rb_right;
574 else if (pregion == region)
575 return;
576 else
577 BUG();
578 }
579
580 rb_link_node(&region->vm_rb, parent, p);
581 rb_insert_color(&region->vm_rb, &nommu_region_tree);
582
583 validate_nommu_regions();
584}
585
586/*
587 * delete a region from the global tree
588 */
589static void delete_nommu_region(struct vm_region *region)
590{
591 BUG_ON(!nommu_region_tree.rb_node);
592
593 validate_nommu_regions();
594 rb_erase(&region->vm_rb, &nommu_region_tree);
595 validate_nommu_regions();
596}
597
598/*
599 * free a contiguous series of pages
600 */
601static void free_page_series(unsigned long from, unsigned long to)
602{
603 for (; from < to; from += PAGE_SIZE) {
604 struct page *page = virt_to_page(from);
605
606 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700607 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000608 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700609 kdebug("free page %p: refcount not one: %d",
610 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000611 put_page(page);
612 }
613}
614
615/*
616 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700617 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000618 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000619 * will equal vm_start
620 */
621static void __put_nommu_region(struct vm_region *region)
622 __releases(nommu_region_sem)
623{
David Howells1e2ae592010-01-15 17:01:33 -0800624 kenter("%p{%d}", region, region->vm_usage);
David Howells8feae132009-01-08 12:04:47 +0000625
626 BUG_ON(!nommu_region_tree.rb_node);
627
David Howells1e2ae592010-01-15 17:01:33 -0800628 if (--region->vm_usage == 0) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000629 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000630 delete_nommu_region(region);
631 up_write(&nommu_region_sem);
632
633 if (region->vm_file)
634 fput(region->vm_file);
635
636 /* IO memory and memory shared directly out of the pagecache
637 * from ramfs/tmpfs mustn't be released here */
638 if (region->vm_flags & VM_MAPPED_COPY) {
639 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000640 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000641 }
642 kmem_cache_free(vm_region_jar, region);
643 } else {
644 up_write(&nommu_region_sem);
645 }
646}
647
648/*
649 * release a reference to a region
650 */
651static void put_nommu_region(struct vm_region *region)
652{
653 down_write(&nommu_region_sem);
654 __put_nommu_region(region);
655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
David Howells30340972006-09-27 01:50:20 -0700657/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700658 * update protection on a vma
659 */
660static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
661{
662#ifdef CONFIG_MPU
663 struct mm_struct *mm = vma->vm_mm;
664 long start = vma->vm_start & PAGE_MASK;
665 while (start < vma->vm_end) {
666 protect_page(mm, start, flags);
667 start += PAGE_SIZE;
668 }
669 update_protections(mm);
670#endif
671}
672
673/*
David Howells30340972006-09-27 01:50:20 -0700674 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000675 * and tree and add to the address space's page tree also if not an anonymous
676 * page
David Howells30340972006-09-27 01:50:20 -0700677 * - should be called with mm->mmap_sem held writelocked
678 */
David Howells8feae132009-01-08 12:04:47 +0000679static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700680{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700681 struct vm_area_struct *pvma, **pp, *next;
David Howells8feae132009-01-08 12:04:47 +0000682 struct address_space *mapping;
683 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700684
David Howells8feae132009-01-08 12:04:47 +0000685 kenter(",%p", vma);
686
687 BUG_ON(!vma->vm_region);
688
689 mm->map_count++;
690 vma->vm_mm = mm;
691
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700692 protect_vma(vma, vma->vm_flags);
693
David Howells8feae132009-01-08 12:04:47 +0000694 /* add the VMA to the mapping */
695 if (vma->vm_file) {
696 mapping = vma->vm_file->f_mapping;
697
698 flush_dcache_mmap_lock(mapping);
699 vma_prio_tree_insert(vma, &mapping->i_mmap);
700 flush_dcache_mmap_unlock(mapping);
701 }
702
703 /* add the VMA to the tree */
704 parent = NULL;
705 p = &mm->mm_rb.rb_node;
706 while (*p) {
707 parent = *p;
708 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
709
710 /* sort by: start addr, end addr, VMA struct addr in that order
711 * (the latter is necessary as we may get identical VMAs) */
712 if (vma->vm_start < pvma->vm_start)
713 p = &(*p)->rb_left;
714 else if (vma->vm_start > pvma->vm_start)
715 p = &(*p)->rb_right;
716 else if (vma->vm_end < pvma->vm_end)
717 p = &(*p)->rb_left;
718 else if (vma->vm_end > pvma->vm_end)
719 p = &(*p)->rb_right;
720 else if (vma < pvma)
721 p = &(*p)->rb_left;
722 else if (vma > pvma)
723 p = &(*p)->rb_right;
724 else
725 BUG();
726 }
727
728 rb_link_node(&vma->vm_rb, parent, p);
729 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
730
731 /* add VMA to the VMA list also */
732 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
733 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700734 break;
David Howells8feae132009-01-08 12:04:47 +0000735 if (pvma->vm_start < vma->vm_start)
736 continue;
737 if (pvma->vm_end < vma->vm_end)
738 break;
739 }
David Howells30340972006-09-27 01:50:20 -0700740
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700741 next = *pp;
David Howells8feae132009-01-08 12:04:47 +0000742 *pp = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700743 vma->vm_next = next;
744 if (next)
745 next->vm_prev = vma;
David Howells8feae132009-01-08 12:04:47 +0000746}
747
748/*
749 * delete a VMA from its owning mm_struct and address space
750 */
751static void delete_vma_from_mm(struct vm_area_struct *vma)
752{
753 struct vm_area_struct **pp;
754 struct address_space *mapping;
755 struct mm_struct *mm = vma->vm_mm;
756
757 kenter("%p", vma);
758
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700759 protect_vma(vma, 0);
760
David Howells8feae132009-01-08 12:04:47 +0000761 mm->map_count--;
762 if (mm->mmap_cache == vma)
763 mm->mmap_cache = NULL;
764
765 /* remove the VMA from the mapping */
766 if (vma->vm_file) {
767 mapping = vma->vm_file->f_mapping;
768
769 flush_dcache_mmap_lock(mapping);
770 vma_prio_tree_remove(vma, &mapping->i_mmap);
771 flush_dcache_mmap_unlock(mapping);
772 }
773
774 /* remove from the MM's tree and list */
775 rb_erase(&vma->vm_rb, &mm->mm_rb);
776 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
777 if (*pp == vma) {
778 *pp = vma->vm_next;
779 break;
780 }
781 }
782
783 vma->vm_mm = NULL;
784}
785
786/*
787 * destroy a VMA record
788 */
789static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
790{
791 kenter("%p", vma);
792 if (vma->vm_ops && vma->vm_ops->close)
793 vma->vm_ops->close(vma);
794 if (vma->vm_file) {
795 fput(vma->vm_file);
796 if (vma->vm_flags & VM_EXECUTABLE)
797 removed_exe_file_vma(mm);
798 }
799 put_nommu_region(vma->vm_region);
800 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700801}
802
803/*
804 * look up the first VMA in which addr resides, NULL if none
805 * - should be called with mm->mmap_sem at least held readlocked
806 */
807struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
808{
David Howells8feae132009-01-08 12:04:47 +0000809 struct vm_area_struct *vma;
810 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700811
David Howells8feae132009-01-08 12:04:47 +0000812 /* check the cache first */
813 vma = mm->mmap_cache;
814 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
815 return vma;
816
817 /* trawl the tree (there may be multiple mappings in which addr
818 * resides) */
819 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
820 vma = rb_entry(n, struct vm_area_struct, vm_rb);
821 if (vma->vm_start > addr)
822 return NULL;
823 if (vma->vm_end > addr) {
824 mm->mmap_cache = vma;
825 return vma;
826 }
David Howells30340972006-09-27 01:50:20 -0700827 }
828
David Howells30340972006-09-27 01:50:20 -0700829 return NULL;
830}
831EXPORT_SYMBOL(find_vma);
832
833/*
David Howells930e6522006-09-27 01:50:22 -0700834 * find a VMA
835 * - we don't extend stack VMAs under NOMMU conditions
836 */
837struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
838{
David Howells7561e8c2010-03-25 16:48:38 +0000839 return find_vma(mm, addr);
David Howells930e6522006-09-27 01:50:22 -0700840}
841
David Howells8feae132009-01-08 12:04:47 +0000842/*
843 * expand a stack to a given address
844 * - not supported under NOMMU conditions
845 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700846int expand_stack(struct vm_area_struct *vma, unsigned long address)
847{
848 return -ENOMEM;
849}
850
David Howells930e6522006-09-27 01:50:22 -0700851/*
David Howells6fa5f802006-09-27 01:50:21 -0700852 * look up the first VMA exactly that exactly matches addr
853 * - should be called with mm->mmap_sem at least held readlocked
854 */
David Howells8feae132009-01-08 12:04:47 +0000855static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
856 unsigned long addr,
857 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000860 struct rb_node *n = mm->mm_rb.rb_node;
861 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
David Howells8feae132009-01-08 12:04:47 +0000863 /* check the cache first */
864 vma = mm->mmap_cache;
865 if (vma && vma->vm_start == addr && vma->vm_end == end)
866 return vma;
867
868 /* trawl the tree (there may be multiple mappings in which addr
869 * resides) */
870 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000872 if (vma->vm_start < addr)
873 continue;
874 if (vma->vm_start > addr)
875 return NULL;
876 if (vma->vm_end == end) {
877 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return vma;
David Howells8feae132009-01-08 12:04:47 +0000879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
882 return NULL;
883}
884
David Howells30340972006-09-27 01:50:20 -0700885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * determine whether a mapping should be permitted and, if so, what sort of
887 * mapping we're capable of supporting
888 */
889static int validate_mmap_request(struct file *file,
890 unsigned long addr,
891 unsigned long len,
892 unsigned long prot,
893 unsigned long flags,
894 unsigned long pgoff,
895 unsigned long *_capabilities)
896{
David Howells8feae132009-01-08 12:04:47 +0000897 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 unsigned long reqprot = prot;
899 int ret;
900
901 /* do the simple checks first */
David Howells06aab5a2009-09-24 12:33:48 +0100902 if (flags & MAP_FIXED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 printk(KERN_DEBUG
904 "%d: Can't do fixed-address/overlay mmap of RAM\n",
905 current->pid);
906 return -EINVAL;
907 }
908
909 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
910 (flags & MAP_TYPE) != MAP_SHARED)
911 return -EINVAL;
912
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000913 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -EINVAL;
915
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000916 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000917 rlen = PAGE_ALIGN(len);
918 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000919 return -ENOMEM;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000922 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff0d2006-12-06 12:02:59 +1000923 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (file) {
926 /* validate file mapping requests */
927 struct address_space *mapping;
928
929 /* files must support mmap */
930 if (!file->f_op || !file->f_op->mmap)
931 return -ENODEV;
932
933 /* work out if what we've got could possibly be shared
934 * - we support chardevs that provide their own "memory"
935 * - we support files/blockdevs that are memory backed
936 */
937 mapping = file->f_mapping;
938 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800939 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 capabilities = 0;
942 if (mapping && mapping->backing_dev_info)
943 capabilities = mapping->backing_dev_info->capabilities;
944
945 if (!capabilities) {
946 /* no explicit capabilities set, so assume some
947 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800948 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case S_IFREG:
950 case S_IFBLK:
951 capabilities = BDI_CAP_MAP_COPY;
952 break;
953
954 case S_IFCHR:
955 capabilities =
956 BDI_CAP_MAP_DIRECT |
957 BDI_CAP_READ_MAP |
958 BDI_CAP_WRITE_MAP;
959 break;
960
961 default:
962 return -EINVAL;
963 }
964 }
965
966 /* eliminate any capabilities that we can't support on this
967 * device */
968 if (!file->f_op->get_unmapped_area)
969 capabilities &= ~BDI_CAP_MAP_DIRECT;
970 if (!file->f_op->read)
971 capabilities &= ~BDI_CAP_MAP_COPY;
972
Graff Yang28d7a6a2009-08-18 14:11:17 -0700973 /* The file shall have been opened with read permission. */
974 if (!(file->f_mode & FMODE_READ))
975 return -EACCES;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (flags & MAP_SHARED) {
978 /* do checks for writing, appending and locking */
979 if ((prot & PROT_WRITE) &&
980 !(file->f_mode & FMODE_WRITE))
981 return -EACCES;
982
Josef Sipeke9536ae2006-12-08 02:37:21 -0800983 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 (file->f_mode & FMODE_WRITE))
985 return -EACCES;
986
Josef Sipeke9536ae2006-12-08 02:37:21 -0800987 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return -EAGAIN;
989
990 if (!(capabilities & BDI_CAP_MAP_DIRECT))
991 return -ENODEV;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* we mustn't privatise shared mappings */
994 capabilities &= ~BDI_CAP_MAP_COPY;
995 }
996 else {
997 /* we're going to read the file into private memory we
998 * allocate */
999 if (!(capabilities & BDI_CAP_MAP_COPY))
1000 return -ENODEV;
1001
1002 /* we don't permit a private writable mapping to be
1003 * shared with the backing device */
1004 if (prot & PROT_WRITE)
1005 capabilities &= ~BDI_CAP_MAP_DIRECT;
1006 }
1007
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001008 if (capabilities & BDI_CAP_MAP_DIRECT) {
1009 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
1010 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
1011 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
1012 ) {
1013 capabilities &= ~BDI_CAP_MAP_DIRECT;
1014 if (flags & MAP_SHARED) {
1015 printk(KERN_WARNING
1016 "MAP_SHARED not completely supported on !MMU\n");
1017 return -EINVAL;
1018 }
1019 }
1020 }
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /* handle executable mappings and implied executable
1023 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -08001024 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (prot & PROT_EXEC)
1026 return -EPERM;
1027 }
1028 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1029 /* handle implication of PROT_EXEC by PROT_READ */
1030 if (current->personality & READ_IMPLIES_EXEC) {
1031 if (capabilities & BDI_CAP_EXEC_MAP)
1032 prot |= PROT_EXEC;
1033 }
1034 }
1035 else if ((prot & PROT_READ) &&
1036 (prot & PROT_EXEC) &&
1037 !(capabilities & BDI_CAP_EXEC_MAP)
1038 ) {
1039 /* backing file is not executable, try to copy */
1040 capabilities &= ~BDI_CAP_MAP_DIRECT;
1041 }
1042 }
1043 else {
1044 /* anonymous mappings are always memory backed and can be
1045 * privately mapped
1046 */
1047 capabilities = BDI_CAP_MAP_COPY;
1048
1049 /* handle PROT_EXEC implication by PROT_READ */
1050 if ((prot & PROT_READ) &&
1051 (current->personality & READ_IMPLIES_EXEC))
1052 prot |= PROT_EXEC;
1053 }
1054
1055 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -04001056 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (ret < 0)
1058 return ret;
1059
1060 /* looks okay */
1061 *_capabilities = capabilities;
1062 return 0;
1063}
1064
1065/*
1066 * we've determined that we can make the mapping, now translate what we
1067 * now know into VMA flags
1068 */
1069static unsigned long determine_vm_flags(struct file *file,
1070 unsigned long prot,
1071 unsigned long flags,
1072 unsigned long capabilities)
1073{
1074 unsigned long vm_flags;
1075
1076 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* vm_flags |= mm->def_flags; */
1078
1079 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1080 /* attempt to share read-only copies of mapped file chunks */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001081 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (file && !(prot & PROT_WRITE))
1083 vm_flags |= VM_MAYSHARE;
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001084 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* overlay a shareable mapping on the backing device or inode
1086 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1087 * romfs/cramfs */
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001088 vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (flags & MAP_SHARED)
Bernd Schmidt3c7b2042010-05-25 23:43:00 -07001090 vm_flags |= VM_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 /* refuse to let anyone share private mappings with this process if
1094 * it's being traced - otherwise breakpoints set in it may interfere
1095 * with another untraced process
1096 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001097 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 vm_flags &= ~VM_MAYSHARE;
1099
1100 return vm_flags;
1101}
1102
1103/*
David Howells8feae132009-01-08 12:04:47 +00001104 * set up a shared mapping on a file (the driver or filesystem provides and
1105 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
David Howells8feae132009-01-08 12:04:47 +00001107static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 int ret;
1110
1111 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001112 if (ret == 0) {
1113 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001114 return 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (ret != -ENOSYS)
1117 return ret;
1118
David Howells3fa30462010-03-23 13:35:21 -07001119 /* getting -ENOSYS indicates that direct mmap isn't possible (as
1120 * opposed to tried but failed) so we can only give a suitable error as
1121 * it's not possible to make a private copy if MAP_SHARED was given */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return -ENODEV;
1123}
1124
1125/*
1126 * set up a private mapping or an anonymous shared mapping
1127 */
David Howells8feae132009-01-08 12:04:47 +00001128static int do_mmap_private(struct vm_area_struct *vma,
1129 struct vm_region *region,
David Howells645d83c2009-09-24 15:13:10 +01001130 unsigned long len,
1131 unsigned long capabilities)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
David Howells8feae132009-01-08 12:04:47 +00001133 struct page *pages;
1134 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 void *base;
David Howells8feae132009-01-08 12:04:47 +00001136 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* invoke the file's mapping function so that it can keep track of
1139 * shared mappings on devices or memory
1140 * - VM_MAYSHARE will be set if it may attempt to share
1141 */
David Howells645d83c2009-09-24 15:13:10 +01001142 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001144 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001146 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1147 vma->vm_region->vm_top = vma->vm_region->vm_end;
David Howells645d83c2009-09-24 15:13:10 +01001148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001150 if (ret != -ENOSYS)
1151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /* getting an ENOSYS error indicates that direct mmap isn't
1154 * possible (as opposed to tried but failed) so we'll try to
1155 * make a private copy of the data and map that instead */
1156 }
1157
David Howells8feae132009-01-08 12:04:47 +00001158 rlen = PAGE_ALIGN(len);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* allocate some memory to hold the mapping
1161 * - note that this may not return a page-aligned address if the object
1162 * we're allocating is smaller than a page
1163 */
David Howells8feae132009-01-08 12:04:47 +00001164 order = get_order(rlen);
1165 kdebug("alloc order %d for %lx", order, len);
1166
1167 pages = alloc_pages(GFP_KERNEL, order);
1168 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto enomem;
1170
David Howells8feae132009-01-08 12:04:47 +00001171 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001172 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
David Howells8feae132009-01-08 12:04:47 +00001174 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001175
1176 /* we allocated a power-of-2 sized page set, so we may want to trim off
1177 * the excess */
1178 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1179 while (total > point) {
1180 order = ilog2(total - point);
1181 n = 1 << order;
1182 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001183 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001184 total -= n;
1185 set_page_refcounted(pages + total);
1186 __free_pages(pages + total, order);
1187 }
David Howells8feae132009-01-08 12:04:47 +00001188 }
1189
David Howells8feae132009-01-08 12:04:47 +00001190 for (point = 1; point < total; point++)
1191 set_page_refcounted(&pages[point]);
1192
1193 base = page_address(pages);
1194 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1195 region->vm_start = (unsigned long) base;
1196 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001197 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001198
1199 vma->vm_start = region->vm_start;
1200 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 if (vma->vm_file) {
1203 /* read the contents of a file into the copy */
1204 mm_segment_t old_fs;
1205 loff_t fpos;
1206
1207 fpos = vma->vm_pgoff;
1208 fpos <<= PAGE_SHIFT;
1209
1210 old_fs = get_fs();
1211 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001212 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 set_fs(old_fs);
1214
1215 if (ret < 0)
1216 goto error_free;
1217
1218 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001219 if (ret < rlen)
1220 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
1224 return 0;
1225
1226error_free:
David Howells8feae132009-01-08 12:04:47 +00001227 free_page_series(region->vm_start, region->vm_end);
1228 region->vm_start = vma->vm_start = 0;
1229 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001230 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return ret;
1232
1233enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001234 printk("Allocation of length %lu from process %d (%s) failed\n",
1235 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 show_free_areas();
1237 return -ENOMEM;
1238}
1239
1240/*
1241 * handle mapping creation for uClinux
1242 */
1243unsigned long do_mmap_pgoff(struct file *file,
1244 unsigned long addr,
1245 unsigned long len,
1246 unsigned long prot,
1247 unsigned long flags,
1248 unsigned long pgoff)
1249{
David Howells8feae132009-01-08 12:04:47 +00001250 struct vm_area_struct *vma;
1251 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001253 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int ret;
1255
David Howells8feae132009-01-08 12:04:47 +00001256 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* decide whether we should attempt the mapping, and if so what sort of
1259 * mapping */
1260 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1261 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001262 if (ret < 0) {
1263 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return ret;
David Howells8feae132009-01-08 12:04:47 +00001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
David Howells06aab5a2009-09-24 12:33:48 +01001267 /* we ignore the address hint */
1268 addr = 0;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* we've determined that we can make the mapping, now translate what we
1271 * now know into VMA flags */
1272 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1273
David Howells8feae132009-01-08 12:04:47 +00001274 /* we're going to need to record the mapping */
1275 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1276 if (!region)
1277 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
David Howells8feae132009-01-08 12:04:47 +00001279 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1280 if (!vma)
1281 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
David Howells1e2ae592010-01-15 17:01:33 -08001283 region->vm_usage = 1;
David Howells8feae132009-01-08 12:04:47 +00001284 region->vm_flags = vm_flags;
1285 region->vm_pgoff = pgoff;
1286
Rik van Riel5beb4932010-03-05 13:42:07 -08001287 INIT_LIST_HEAD(&vma->anon_vma_chain);
David Howells8feae132009-01-08 12:04:47 +00001288 vma->vm_flags = vm_flags;
1289 vma->vm_pgoff = pgoff;
1290
1291 if (file) {
1292 region->vm_file = file;
1293 get_file(file);
1294 vma->vm_file = file;
1295 get_file(file);
1296 if (vm_flags & VM_EXECUTABLE) {
1297 added_exe_file_vma(current->mm);
1298 vma->vm_mm = current->mm;
1299 }
1300 }
1301
1302 down_write(&nommu_region_sem);
1303
1304 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001306 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * - shared mappings on character devices and memory backed files are
1308 * permitted to overlap inexactly as far as we are concerned for in
1309 * these cases, sharing is handled in the driver or filesystem rather
1310 * than here
1311 */
1312 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001313 struct vm_region *pregion;
1314 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
David Howells8feae132009-01-08 12:04:47 +00001316 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1317 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001318
David Howells8feae132009-01-08 12:04:47 +00001319 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1320 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
David Howells8feae132009-01-08 12:04:47 +00001322 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 continue;
1324
1325 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001326 if (pregion->vm_file->f_path.dentry->d_inode !=
1327 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 continue;
1329
David Howells8feae132009-01-08 12:04:47 +00001330 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 continue;
1332
David Howells8feae132009-01-08 12:04:47 +00001333 rpglen = pregion->vm_end - pregion->vm_start;
1334 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1335 rpgend = pregion->vm_pgoff + rpglen;
1336 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 continue;
1338
David Howells8feae132009-01-08 12:04:47 +00001339 /* handle inexactly overlapping matches between
1340 * mappings */
1341 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1342 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1343 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1345 goto sharing_violation;
1346 continue;
1347 }
1348
David Howells8feae132009-01-08 12:04:47 +00001349 /* we've found a region we can share */
David Howells1e2ae592010-01-15 17:01:33 -08001350 pregion->vm_usage++;
David Howells8feae132009-01-08 12:04:47 +00001351 vma->vm_region = pregion;
1352 start = pregion->vm_start;
1353 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1354 vma->vm_start = start;
1355 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
David Howells8feae132009-01-08 12:04:47 +00001357 if (pregion->vm_flags & VM_MAPPED_COPY) {
1358 kdebug("share copy");
1359 vma->vm_flags |= VM_MAPPED_COPY;
1360 } else {
1361 kdebug("share mmap");
1362 ret = do_mmap_shared_file(vma);
1363 if (ret < 0) {
1364 vma->vm_region = NULL;
1365 vma->vm_start = 0;
1366 vma->vm_end = 0;
David Howells1e2ae592010-01-15 17:01:33 -08001367 pregion->vm_usage--;
David Howells8feae132009-01-08 12:04:47 +00001368 pregion = NULL;
1369 goto error_just_free;
1370 }
1371 }
1372 fput(region->vm_file);
1373 kmem_cache_free(vm_region_jar, region);
1374 region = pregion;
1375 result = start;
1376 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* obtain the address at which to make a shared mapping
1380 * - this is the hook for quasi-memory character devices to
1381 * tell us the location of a shared mapping
1382 */
David Howells645d83c2009-09-24 15:13:10 +01001383 if (capabilities & BDI_CAP_MAP_DIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 addr = file->f_op->get_unmapped_area(file, addr, len,
1385 pgoff, flags);
1386 if (IS_ERR((void *) addr)) {
1387 ret = addr;
1388 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001389 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* the driver refused to tell us where to site
1392 * the mapping so we'll have to attempt to copy
1393 * it */
1394 ret = (unsigned long) -ENODEV;
1395 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001396 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001399 } else {
1400 vma->vm_start = region->vm_start = addr;
1401 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
1404 }
1405
David Howells8feae132009-01-08 12:04:47 +00001406 vma->vm_region = region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
David Howells645d83c2009-09-24 15:13:10 +01001408 /* set up the mapping
1409 * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
1410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001412 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 else
David Howells645d83c2009-09-24 15:13:10 +01001414 ret = do_mmap_private(vma, region, len, capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (ret < 0)
David Howells645d83c2009-09-24 15:13:10 +01001416 goto error_just_free;
1417 add_nommu_region(region);
David Howells8feae132009-01-08 12:04:47 +00001418
Jie Zhangea637632009-12-14 18:00:02 -08001419 /* clear anonymous mappings that don't ask for uninitialized data */
1420 if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
1421 memset((void *)region->vm_start, 0,
1422 region->vm_end - region->vm_start);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001425 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 current->mm->total_vm += len >> PAGE_SHIFT;
1428
David Howells8feae132009-01-08 12:04:47 +00001429share:
1430 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Mike Frysingercfe79c02010-01-06 17:23:23 +00001432 /* we flush the region from the icache only when the first executable
1433 * mapping of it is made */
1434 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
1435 flush_icache_range(region->vm_start, region->vm_end);
1436 region->vm_icache_flushed = true;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Mike Frysingercfe79c02010-01-06 17:23:23 +00001439 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
David Howells8feae132009-01-08 12:04:47 +00001441 kleave(" = %lx", result);
1442 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
David Howells8feae132009-01-08 12:04:47 +00001444error_just_free:
1445 up_write(&nommu_region_sem);
1446error:
David Howells89a86402009-10-30 13:13:26 +00001447 if (region->vm_file)
1448 fput(region->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001449 kmem_cache_free(vm_region_jar, region);
David Howells89a86402009-10-30 13:13:26 +00001450 if (vma->vm_file)
1451 fput(vma->vm_file);
David Howells8feae132009-01-08 12:04:47 +00001452 if (vma->vm_flags & VM_EXECUTABLE)
1453 removed_exe_file_vma(vma->vm_mm);
1454 kmem_cache_free(vm_area_cachep, vma);
1455 kleave(" = %d", ret);
1456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
David Howells8feae132009-01-08 12:04:47 +00001458sharing_violation:
1459 up_write(&nommu_region_sem);
1460 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1461 ret = -EINVAL;
1462 goto error;
1463
1464error_getting_vma:
1465 kmem_cache_free(vm_region_jar, region);
1466 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1467 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 len, current->pid);
1469 show_free_areas();
1470 return -ENOMEM;
1471
David Howells8feae132009-01-08 12:04:47 +00001472error_getting_region:
1473 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1474 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 len, current->pid);
1476 show_free_areas();
1477 return -ENOMEM;
1478}
Paul Mundtb5073172007-07-21 04:37:25 -07001479EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001481SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1482 unsigned long, prot, unsigned long, flags,
1483 unsigned long, fd, unsigned long, pgoff)
1484{
1485 struct file *file = NULL;
1486 unsigned long retval = -EBADF;
1487
Al Viro120a7952010-10-30 02:54:44 -04001488 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001489 if (!(flags & MAP_ANONYMOUS)) {
1490 file = fget(fd);
1491 if (!file)
1492 goto out;
1493 }
1494
1495 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1496
1497 down_write(&current->mm->mmap_sem);
1498 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1499 up_write(&current->mm->mmap_sem);
1500
1501 if (file)
1502 fput(file);
1503out:
1504 return retval;
1505}
1506
Christoph Hellwiga4679372010-03-10 15:21:15 -08001507#ifdef __ARCH_WANT_SYS_OLD_MMAP
1508struct mmap_arg_struct {
1509 unsigned long addr;
1510 unsigned long len;
1511 unsigned long prot;
1512 unsigned long flags;
1513 unsigned long fd;
1514 unsigned long offset;
1515};
1516
1517SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1518{
1519 struct mmap_arg_struct a;
1520
1521 if (copy_from_user(&a, arg, sizeof(a)))
1522 return -EFAULT;
1523 if (a.offset & ~PAGE_MASK)
1524 return -EINVAL;
1525
1526 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1527 a.offset >> PAGE_SHIFT);
1528}
1529#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/*
David Howells8feae132009-01-08 12:04:47 +00001532 * split a vma into two pieces at address 'addr', a new vma is allocated either
1533 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
David Howells8feae132009-01-08 12:04:47 +00001535int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1536 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
David Howells8feae132009-01-08 12:04:47 +00001538 struct vm_area_struct *new;
1539 struct vm_region *region;
1540 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
David Howells8feae132009-01-08 12:04:47 +00001542 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
David Howells779c1022010-01-15 17:01:34 -08001544 /* we're only permitted to split anonymous regions (these should have
1545 * only a single usage on the region) */
1546 if (vma->vm_file)
David Howells8feae132009-01-08 12:04:47 +00001547 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
David Howells8feae132009-01-08 12:04:47 +00001549 if (mm->map_count >= sysctl_max_map_count)
1550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David Howells8feae132009-01-08 12:04:47 +00001552 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1553 if (!region)
1554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
David Howells8feae132009-01-08 12:04:47 +00001556 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1557 if (!new) {
1558 kmem_cache_free(vm_region_jar, region);
1559 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
David Howells8feae132009-01-08 12:04:47 +00001561
1562 /* most fields are the same, copy all, and then fixup */
1563 *new = *vma;
1564 *region = *vma->vm_region;
1565 new->vm_region = region;
1566
1567 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1568
1569 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001570 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001571 } else {
1572 region->vm_start = new->vm_start = addr;
1573 region->vm_pgoff = new->vm_pgoff += npages;
1574 }
1575
1576 if (new->vm_ops && new->vm_ops->open)
1577 new->vm_ops->open(new);
1578
1579 delete_vma_from_mm(vma);
1580 down_write(&nommu_region_sem);
1581 delete_nommu_region(vma->vm_region);
1582 if (new_below) {
1583 vma->vm_region->vm_start = vma->vm_start = addr;
1584 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1585 } else {
1586 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001587 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001588 }
1589 add_nommu_region(vma->vm_region);
1590 add_nommu_region(new->vm_region);
1591 up_write(&nommu_region_sem);
1592 add_vma_to_mm(mm, vma);
1593 add_vma_to_mm(mm, new);
1594 return 0;
1595}
1596
1597/*
1598 * shrink a VMA by removing the specified chunk from either the beginning or
1599 * the end
1600 */
1601static int shrink_vma(struct mm_struct *mm,
1602 struct vm_area_struct *vma,
1603 unsigned long from, unsigned long to)
1604{
1605 struct vm_region *region;
1606
1607 kenter("");
1608
1609 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1610 * and list */
1611 delete_vma_from_mm(vma);
1612 if (from > vma->vm_start)
1613 vma->vm_end = from;
1614 else
1615 vma->vm_start = to;
1616 add_vma_to_mm(mm, vma);
1617
1618 /* cut the backing region down to size */
1619 region = vma->vm_region;
David Howells1e2ae592010-01-15 17:01:33 -08001620 BUG_ON(region->vm_usage != 1);
David Howells8feae132009-01-08 12:04:47 +00001621
1622 down_write(&nommu_region_sem);
1623 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001624 if (from > region->vm_start) {
1625 to = region->vm_top;
1626 region->vm_top = region->vm_end = from;
1627 } else {
David Howells8feae132009-01-08 12:04:47 +00001628 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001629 }
David Howells8feae132009-01-08 12:04:47 +00001630 add_nommu_region(region);
1631 up_write(&nommu_region_sem);
1632
1633 free_page_series(from, to);
1634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
David Howells30340972006-09-27 01:50:20 -07001637/*
1638 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001639 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1640 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001641 */
David Howells8feae132009-01-08 12:04:47 +00001642int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
David Howells8feae132009-01-08 12:04:47 +00001644 struct vm_area_struct *vma;
1645 struct rb_node *rb;
1646 unsigned long end = start + len;
1647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
David Howells8feae132009-01-08 12:04:47 +00001649 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
David Howells8feae132009-01-08 12:04:47 +00001651 if (len == 0)
1652 return -EINVAL;
1653
1654 /* find the first potentially overlapping VMA */
1655 vma = find_vma(mm, start);
1656 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001657 static int limit = 0;
1658 if (limit < 5) {
1659 printk(KERN_WARNING
1660 "munmap of memory not mmapped by process %d"
1661 " (%s): 0x%lx-0x%lx\n",
1662 current->pid, current->comm,
1663 start, start + len - 1);
1664 limit++;
1665 }
David Howells8feae132009-01-08 12:04:47 +00001666 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
David Howells8feae132009-01-08 12:04:47 +00001669 /* we're allowed to split an anonymous VMA but not a file-backed one */
1670 if (vma->vm_file) {
1671 do {
1672 if (start > vma->vm_start) {
1673 kleave(" = -EINVAL [miss]");
1674 return -EINVAL;
1675 }
1676 if (end == vma->vm_end)
1677 goto erase_whole_vma;
1678 rb = rb_next(&vma->vm_rb);
1679 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1680 } while (rb);
1681 kleave(" = -EINVAL [split file]");
1682 return -EINVAL;
1683 } else {
1684 /* the chunk must be a subset of the VMA found */
1685 if (start == vma->vm_start && end == vma->vm_end)
1686 goto erase_whole_vma;
1687 if (start < vma->vm_start || end > vma->vm_end) {
1688 kleave(" = -EINVAL [superset]");
1689 return -EINVAL;
1690 }
1691 if (start & ~PAGE_MASK) {
1692 kleave(" = -EINVAL [unaligned start]");
1693 return -EINVAL;
1694 }
1695 if (end != vma->vm_end && end & ~PAGE_MASK) {
1696 kleave(" = -EINVAL [unaligned split]");
1697 return -EINVAL;
1698 }
1699 if (start != vma->vm_start && end != vma->vm_end) {
1700 ret = split_vma(mm, vma, start, 1);
1701 if (ret < 0) {
1702 kleave(" = %d [split]", ret);
1703 return ret;
1704 }
1705 }
1706 return shrink_vma(mm, vma, start, end);
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
David Howells8feae132009-01-08 12:04:47 +00001709erase_whole_vma:
1710 delete_vma_from_mm(vma);
1711 delete_vma(mm, vma);
1712 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return 0;
1714}
Paul Mundtb5073172007-07-21 04:37:25 -07001715EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001717SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001718{
1719 int ret;
1720 struct mm_struct *mm = current->mm;
1721
1722 down_write(&mm->mmap_sem);
1723 ret = do_munmap(mm, addr, len);
1724 up_write(&mm->mmap_sem);
1725 return ret;
1726}
1727
1728/*
David Howells8feae132009-01-08 12:04:47 +00001729 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001730 */
David Howells8feae132009-01-08 12:04:47 +00001731void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
David Howells8feae132009-01-08 12:04:47 +00001733 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
David Howells8feae132009-01-08 12:04:47 +00001735 if (!mm)
1736 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
David Howells8feae132009-01-08 12:04:47 +00001738 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
David Howells8feae132009-01-08 12:04:47 +00001740 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
David Howells8feae132009-01-08 12:04:47 +00001742 while ((vma = mm->mmap)) {
1743 mm->mmap = vma->vm_next;
1744 delete_vma_from_mm(vma);
1745 delete_vma(mm, vma);
Steven J. Magnani04c34962010-11-24 12:56:54 -08001746 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
David Howells8feae132009-01-08 12:04:47 +00001748
1749 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752unsigned long do_brk(unsigned long addr, unsigned long len)
1753{
1754 return -ENOMEM;
1755}
1756
1757/*
David Howells6fa5f802006-09-27 01:50:21 -07001758 * expand (or shrink) an existing mapping, potentially moving it at the same
1759 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *
David Howells6fa5f802006-09-27 01:50:21 -07001761 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001762 * as long as it stays within the region allocated by do_mmap_private() and the
1763 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 *
David Howells6fa5f802006-09-27 01:50:21 -07001765 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
1767unsigned long do_mremap(unsigned long addr,
1768 unsigned long old_len, unsigned long new_len,
1769 unsigned long flags, unsigned long new_addr)
1770{
David Howells6fa5f802006-09-27 01:50:21 -07001771 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001774 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return (unsigned long) -EINVAL;
1776
David Howells8feae132009-01-08 12:04:47 +00001777 if (addr & ~PAGE_MASK)
1778 return -EINVAL;
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (flags & MREMAP_FIXED && new_addr != addr)
1781 return (unsigned long) -EINVAL;
1782
David Howells8feae132009-01-08 12:04:47 +00001783 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001784 if (!vma)
1785 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
David Howells6fa5f802006-09-27 01:50:21 -07001787 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return (unsigned long) -EFAULT;
1789
David Howells6fa5f802006-09-27 01:50:21 -07001790 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return (unsigned long) -EPERM;
1792
David Howells8feae132009-01-08 12:04:47 +00001793 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return (unsigned long) -ENOMEM;
1795
1796 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001797 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001798 return vma->vm_start;
1799}
Paul Mundtb5073172007-07-21 04:37:25 -07001800EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001801
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001802SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1803 unsigned long, new_len, unsigned long, flags,
1804 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001805{
1806 unsigned long ret;
1807
1808 down_write(&current->mm->mmap_sem);
1809 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1810 up_write(&current->mm->mmap_sem);
1811 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Linus Torvalds6aab3412005-11-28 14:34:23 -08001814struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001815 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 return NULL;
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1821 unsigned long to, unsigned long size, pgprot_t prot)
1822{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001823 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
Luke Yang22c4af42006-07-14 00:24:09 -07001826EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Paul Mundtf905bc42008-02-04 22:29:59 -08001828int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1829 unsigned long pgoff)
1830{
1831 unsigned int size = vma->vm_end - vma->vm_start;
1832
1833 if (!(vma->vm_flags & VM_USERMAP))
1834 return -EINVAL;
1835
1836 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1837 vma->vm_end = vma->vm_start + size;
1838
1839 return 0;
1840}
1841EXPORT_SYMBOL(remap_vmalloc_range);
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1844{
1845}
1846
1847unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1848 unsigned long len, unsigned long pgoff, unsigned long flags)
1849{
1850 return -ENOMEM;
1851}
1852
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001853void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857void unmap_mapping_range(struct address_space *mapping,
1858 loff_t const holebegin, loff_t const holelen,
1859 int even_cows)
1860{
1861}
Luke Yang22c4af42006-07-14 00:24:09 -07001862EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864/*
1865 * Check that a process has enough memory to allocate a new virtual
1866 * mapping. 0 means there is enough memory for the allocation to
1867 * succeed and -ENOMEM implies there is not.
1868 *
1869 * We currently support three overcommit policies, which are set via the
1870 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1871 *
1872 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1873 * Additional code 2002 Jul 20 by Robert Love.
1874 *
1875 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1876 *
1877 * Note this is a helper function intended to be used by LSMs which
1878 * wish to use this logic.
1879 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001880int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 unsigned long free, allowed;
1883
1884 vm_acct_memory(pages);
1885
1886 /*
1887 * Sometimes we want to use more memory than we have
1888 */
1889 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1890 return 0;
1891
1892 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1893 unsigned long n;
1894
Christoph Lameter347ce432006-06-30 01:55:35 -07001895 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 free += nr_swap_pages;
1897
1898 /*
1899 * Any slabs which are created with the
1900 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1901 * which are reclaimable, under pressure. The dentry
1902 * cache and most inode caches should fall into this
1903 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001904 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /*
1907 * Leave the last 3% for root
1908 */
1909 if (!cap_sys_admin)
1910 free -= free / 32;
1911
1912 if (free > pages)
1913 return 0;
1914
1915 /*
1916 * nr_free_pages() is very expensive on large systems,
1917 * only call if we're about to fail.
1918 */
1919 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001920
1921 /*
1922 * Leave reserved pages. The pages are not for anonymous pages.
1923 */
1924 if (n <= totalreserve_pages)
1925 goto error;
1926 else
1927 n -= totalreserve_pages;
1928
1929 /*
1930 * Leave the last 3% for root
1931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (!cap_sys_admin)
1933 n -= n / 32;
1934 free += n;
1935
1936 if (free > pages)
1937 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001938
1939 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
1942 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1943 /*
1944 * Leave the last 3% for root
1945 */
1946 if (!cap_sys_admin)
1947 allowed -= allowed / 32;
1948 allowed += total_swap_pages;
1949
1950 /* Don't let a single process grow too big:
1951 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001952 if (mm)
1953 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001955 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001957
Hideo AOKId5ddc792006-04-10 22:53:01 -07001958error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 vm_unacct_memory(pages);
1960
1961 return -ENOMEM;
1962}
1963
1964int in_gate_area_no_task(unsigned long addr)
1965{
1966 return 0;
1967}
David Howellsb0e15192006-01-06 00:11:42 -08001968
Nick Piggind0217ac2007-07-19 01:47:03 -07001969int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001970{
1971 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001972 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001973}
Paul Mundtb5073172007-07-21 04:37:25 -07001974EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001975
1976/*
1977 * Access another process' address space.
1978 * - source/target buffer must be kernel space
1979 */
1980int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1981{
David Howells0ec76a12006-09-27 01:50:15 -07001982 struct vm_area_struct *vma;
1983 struct mm_struct *mm;
1984
1985 if (addr + len < addr)
1986 return 0;
1987
1988 mm = get_task_mm(tsk);
1989 if (!mm)
1990 return 0;
1991
1992 down_read(&mm->mmap_sem);
1993
1994 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001995 vma = find_vma(mm, addr);
1996 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001997 /* don't overrun this mapping */
1998 if (addr + len >= vma->vm_end)
1999 len = vma->vm_end - addr;
2000
2001 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07002002 if (write && vma->vm_flags & VM_MAYWRITE)
Jie Zhang79597222010-01-06 17:23:28 +00002003 copy_to_user_page(vma, NULL, addr,
2004 (void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07002005 else if (!write && vma->vm_flags & VM_MAYREAD)
Jie Zhang79597222010-01-06 17:23:28 +00002006 copy_from_user_page(vma, NULL, addr,
2007 buf, (void *) addr, len);
David Howells0ec76a12006-09-27 01:50:15 -07002008 else
2009 len = 0;
2010 } else {
2011 len = 0;
2012 }
2013
2014 up_read(&mm->mmap_sem);
2015 mmput(mm);
2016 return len;
2017}
David Howells7e660872010-01-15 17:01:39 -08002018
2019/**
2020 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
2021 * @inode: The inode to check
2022 * @size: The current filesize of the inode
2023 * @newsize: The proposed filesize of the inode
2024 *
2025 * Check the shared mappings on an inode on behalf of a shrinking truncate to
2026 * make sure that that any outstanding VMAs aren't broken and then shrink the
2027 * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
2028 * automatically grant mappings that are too large.
2029 */
2030int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
2031 size_t newsize)
2032{
2033 struct vm_area_struct *vma;
2034 struct prio_tree_iter iter;
2035 struct vm_region *region;
2036 pgoff_t low, high;
2037 size_t r_size, r_top;
2038
2039 low = newsize >> PAGE_SHIFT;
2040 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2041
2042 down_write(&nommu_region_sem);
2043
2044 /* search for VMAs that fall within the dead zone */
2045 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2046 low, high) {
2047 /* found one - only interested if it's shared out of the page
2048 * cache */
2049 if (vma->vm_flags & VM_SHARED) {
2050 up_write(&nommu_region_sem);
2051 return -ETXTBSY; /* not quite true, but near enough */
2052 }
2053 }
2054
2055 /* reduce any regions that overlap the dead zone - if in existence,
2056 * these will be pointed to by VMAs that don't overlap the dead zone
2057 *
2058 * we don't check for any regions that start beyond the EOF as there
2059 * shouldn't be any
2060 */
2061 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
2062 0, ULONG_MAX) {
2063 if (!(vma->vm_flags & VM_SHARED))
2064 continue;
2065
2066 region = vma->vm_region;
2067 r_size = region->vm_top - region->vm_start;
2068 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
2069
2070 if (r_top > newsize) {
2071 region->vm_top -= r_top - newsize;
2072 if (region->vm_end > region->vm_top)
2073 region->vm_end = region->vm_top;
2074 }
2075 }
2076
2077 up_write(&nommu_region_sem);
2078 return 0;
2079}