blob: 1a4473faac484067859b852ffbcb9d6bf0dcfb3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/nommu.c
3 *
4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
6 *
7 * See Documentation/nommu-mmap.txt
8 *
David Howells8feae132009-01-08 12:04:47 +00009 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
Paul Mundteb6434d2009-01-21 17:45:47 +090013 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
David Howellsf2b85442007-10-29 13:15:39 +000016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/swap.h>
20#include <linux/file.h>
21#include <linux/highmem.h>
22#include <linux/pagemap.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
Roland McGrathfa8e26c2008-07-25 19:45:50 -070025#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/blkdev.h>
27#include <linux/backing-dev.h>
28#include <linux/mount.h>
29#include <linux/personality.h>
30#include <linux/security.h>
31#include <linux/syscalls.h>
32
33#include <asm/uaccess.h>
34#include <asm/tlb.h>
35#include <asm/tlbflush.h>
Bernd Schmidteb8cdec2009-09-21 17:03:57 -070036#include <asm/mmu_context.h>
David Howells8feae132009-01-08 12:04:47 +000037#include "internal.h"
38
39static inline __attribute__((format(printf, 1, 2)))
40void no_printk(const char *fmt, ...)
41{
42}
43
44#if 0
45#define kenter(FMT, ...) \
46 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
47#define kleave(FMT, ...) \
48 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
49#define kdebug(FMT, ...) \
50 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
51#else
52#define kenter(FMT, ...) \
53 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
54#define kleave(FMT, ...) \
55 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
56#define kdebug(FMT, ...) \
57 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60void *high_memory;
61struct page *mem_map;
62unsigned long max_mapnr;
63unsigned long num_physpages;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -070064struct percpu_counter vm_committed_as;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
66int sysctl_overcommit_ratio = 50; /* default is 50% */
67int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
David Howellsfc4d5c22009-05-06 16:03:05 -070068int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069int heap_stack_gap = 0;
70
David Howells33e5d7692009-04-02 16:56:32 -070071atomic_long_t mmap_pages_allocated;
David Howells8feae132009-01-08 12:04:47 +000072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073EXPORT_SYMBOL(mem_map);
Wu, Bryan6a04de62007-04-11 23:28:47 -070074EXPORT_SYMBOL(num_physpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David Howells8feae132009-01-08 12:04:47 +000076/* list of mapped, potentially shareable regions */
77static struct kmem_cache *vm_region_jar;
78struct rb_root nommu_region_tree = RB_ROOT;
79DECLARE_RWSEM(nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81struct vm_operations_struct generic_file_vm_ops = {
82};
83
84/*
85 * Handle all mappings that got truncated by a "truncate()"
86 * system call.
87 *
88 * NOTE! We have to be ready to update the memory sharing
89 * between the file and the memory map for a potential last
90 * incomplete page. Ugly, but necessary.
91 */
92int vmtruncate(struct inode *inode, loff_t offset)
93{
94 struct address_space *mapping = inode->i_mapping;
95 unsigned long limit;
96
97 if (inode->i_size < offset)
98 goto do_expand;
99 i_size_write(inode, offset);
100
101 truncate_inode_pages(mapping, offset);
102 goto out_truncate;
103
104do_expand:
105 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
106 if (limit != RLIM_INFINITY && offset > limit)
107 goto out_sig;
108 if (offset > inode->i_sb->s_maxbytes)
109 goto out;
110 i_size_write(inode, offset);
111
112out_truncate:
Al Viroacfa4382008-12-04 10:06:33 -0500113 if (inode->i_op->truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 inode->i_op->truncate(inode);
115 return 0;
116out_sig:
117 send_sig(SIGXFSZ, current, 0);
118out:
119 return -EFBIG;
120}
121
122EXPORT_SYMBOL(vmtruncate);
123
124/*
125 * Return the total memory allocated for this pointer, not
126 * just what the caller asked for.
127 *
128 * Doesn't have to be accurate, i.e. may have races.
129 */
130unsigned int kobjsize(const void *objp)
131{
132 struct page *page;
133
Michael Hennerich4016a132008-04-28 02:13:38 -0700134 /*
135 * If the object we have should not have ksize performed on it,
136 * return size of 0
137 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900138 if (!objp || !virt_addr_valid(objp))
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700139 return 0;
140
141 page = virt_to_head_page(objp);
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700142
143 /*
144 * If the allocator sets PageSlab, we know the pointer came from
145 * kmalloc().
146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (PageSlab(page))
148 return ksize(objp);
149
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700150 /*
Paul Mundtab2e83e2009-01-08 12:04:48 +0000151 * If it's not a compound page, see if we have a matching VMA
152 * region. This test is intentionally done in reverse order,
153 * so if there's no VMA, we still fall through and hand back
154 * PAGE_SIZE for 0-order pages.
155 */
156 if (!PageCompound(page)) {
157 struct vm_area_struct *vma;
158
159 vma = find_vma(current->mm, (unsigned long)objp);
160 if (vma)
161 return vma->vm_end - vma->vm_start;
162 }
163
164 /*
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700165 * The ksize() function is only guaranteed to work for pointers
Paul Mundt5a1603b2008-06-12 16:29:55 +0900166 * returned by kmalloc(). So handle arbitrary pointers here.
Paul Mundt6cfd53fc2008-06-05 22:46:08 -0700167 */
Paul Mundt5a1603b2008-06-12 16:29:55 +0900168 return PAGE_SIZE << compound_order(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Nick Pigginb291f002008-10-18 20:26:44 -0700171int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Hugh Dickins58fa8792009-09-21 17:03:31 -0700172 unsigned long start, int nr_pages, int foll_flags,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200173 struct page **pages, struct vm_area_struct **vmas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Sonic Zhang910e46d2006-09-27 01:50:17 -0700175 struct vm_area_struct *vma;
David Howells7b4d5b82006-09-27 01:50:18 -0700176 unsigned long vm_flags;
177 int i;
178
179 /* calculate required read or write permissions.
Hugh Dickins58fa8792009-09-21 17:03:31 -0700180 * If FOLL_FORCE is set, we only require the "MAY" flags.
David Howells7b4d5b82006-09-27 01:50:18 -0700181 */
Hugh Dickins58fa8792009-09-21 17:03:31 -0700182 vm_flags = (foll_flags & FOLL_WRITE) ?
183 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
184 vm_flags &= (foll_flags & FOLL_FORCE) ?
185 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Peter Zijlstra9d737772009-06-25 11:58:55 +0200187 for (i = 0; i < nr_pages; i++) {
Sonic Zhang910e46d2006-09-27 01:50:17 -0700188 vma = find_vma(mm, start);
David Howells7b4d5b82006-09-27 01:50:18 -0700189 if (!vma)
190 goto finish_or_fault;
191
192 /* protect what we can, including chardevs */
Hugh Dickins1c3aff12009-09-21 17:03:24 -0700193 if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
194 !(vm_flags & vma->vm_flags))
David Howells7b4d5b82006-09-27 01:50:18 -0700195 goto finish_or_fault;
Sonic Zhang910e46d2006-09-27 01:50:17 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (pages) {
198 pages[i] = virt_to_page(start);
199 if (pages[i])
200 page_cache_get(pages[i]);
201 }
202 if (vmas)
Sonic Zhang910e46d2006-09-27 01:50:17 -0700203 vmas[i] = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 start += PAGE_SIZE;
205 }
David Howells7b4d5b82006-09-27 01:50:18 -0700206
207 return i;
208
209finish_or_fault:
210 return i ? : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Nick Pigginb291f002008-10-18 20:26:44 -0700212
Nick Pigginb291f002008-10-18 20:26:44 -0700213/*
214 * get a list of pages in an address range belonging to the specified process
215 * and indicate the VMA that covers each page
216 * - this is potentially dodgy as we may end incrementing the page count of a
217 * slab page or a secondary page from a compound page
218 * - don't permit access to VMAs that don't support it, such as I/O mappings
219 */
220int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
Peter Zijlstra9d737772009-06-25 11:58:55 +0200221 unsigned long start, int nr_pages, int write, int force,
Nick Pigginb291f002008-10-18 20:26:44 -0700222 struct page **pages, struct vm_area_struct **vmas)
223{
224 int flags = 0;
225
226 if (write)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700227 flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700228 if (force)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700229 flags |= FOLL_FORCE;
Nick Pigginb291f002008-10-18 20:26:44 -0700230
Peter Zijlstra9d737772009-06-25 11:58:55 +0200231 return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
Nick Pigginb291f002008-10-18 20:26:44 -0700232}
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000233EXPORT_SYMBOL(get_user_pages);
234
Paul Mundtdfc2f912009-06-26 04:31:57 +0900235/**
236 * follow_pfn - look up PFN at a user virtual address
237 * @vma: memory mapping
238 * @address: user virtual address
239 * @pfn: location to store found PFN
240 *
241 * Only IO mappings and raw PFN mappings are allowed.
242 *
243 * Returns zero and the pfn at @pfn on success, -ve otherwise.
244 */
245int follow_pfn(struct vm_area_struct *vma, unsigned long address,
246 unsigned long *pfn)
247{
248 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
249 return -EINVAL;
250
251 *pfn = address >> PAGE_SHIFT;
252 return 0;
253}
254EXPORT_SYMBOL(follow_pfn);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256DEFINE_RWLOCK(vmlist_lock);
257struct vm_struct *vmlist;
258
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800259void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 kfree(addr);
262}
Paul Mundtb5073172007-07-21 04:37:25 -0700263EXPORT_SYMBOL(vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Al Virodd0fc662005-10-07 07:46:04 +0100265void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 /*
Robert P. J. Day85186092007-10-19 23:11:38 +0200268 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
269 * returns only a logical address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
Nick Piggin84097512006-03-22 00:08:34 -0800271 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Paul Mundtb5073172007-07-21 04:37:25 -0700273EXPORT_SYMBOL(__vmalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Paul Mundtf905bc42008-02-04 22:29:59 -0800275void *vmalloc_user(unsigned long size)
276{
277 void *ret;
278
279 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
280 PAGE_KERNEL);
281 if (ret) {
282 struct vm_area_struct *vma;
283
284 down_write(&current->mm->mmap_sem);
285 vma = find_vma(current->mm, (unsigned long)ret);
286 if (vma)
287 vma->vm_flags |= VM_USERMAP;
288 up_write(&current->mm->mmap_sem);
289 }
290
291 return ret;
292}
293EXPORT_SYMBOL(vmalloc_user);
294
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800295struct page *vmalloc_to_page(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 return virt_to_page(addr);
298}
Paul Mundtb5073172007-07-21 04:37:25 -0700299EXPORT_SYMBOL(vmalloc_to_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800301unsigned long vmalloc_to_pfn(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 return page_to_pfn(virt_to_page(addr));
304}
Paul Mundtb5073172007-07-21 04:37:25 -0700305EXPORT_SYMBOL(vmalloc_to_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307long vread(char *buf, char *addr, unsigned long count)
308{
309 memcpy(buf, addr, count);
310 return count;
311}
312
313long vwrite(char *buf, char *addr, unsigned long count)
314{
315 /* Don't allow overflow */
316 if ((unsigned long) addr + count < count)
317 count = -(unsigned long) addr;
318
319 memcpy(addr, buf, count);
320 return(count);
321}
322
323/*
324 * vmalloc - allocate virtually continguos memory
325 *
326 * @size: allocation size
327 *
328 * Allocate enough pages to cover @size from the page level
329 * allocator and map them into continguos kernel virtual space.
330 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +0200331 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * use __vmalloc() instead.
333 */
334void *vmalloc(unsigned long size)
335{
336 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
337}
Andrew Mortonf6138882006-02-28 16:59:18 -0800338EXPORT_SYMBOL(vmalloc);
339
340void *vmalloc_node(unsigned long size, int node)
341{
342 return vmalloc(size);
343}
344EXPORT_SYMBOL(vmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Paul Mundt1af446e2008-08-04 16:01:47 +0900346#ifndef PAGE_KERNEL_EXEC
347# define PAGE_KERNEL_EXEC PAGE_KERNEL
348#endif
349
350/**
351 * vmalloc_exec - allocate virtually contiguous, executable memory
352 * @size: allocation size
353 *
354 * Kernel-internal function to allocate enough pages to cover @size
355 * the page level allocator and map them into contiguous and
356 * executable kernel virtual space.
357 *
358 * For tight control over page level allocator and protection flags
359 * use __vmalloc() instead.
360 */
361
362void *vmalloc_exec(unsigned long size)
363{
364 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
365}
366
Paul Mundtb5073172007-07-21 04:37:25 -0700367/**
368 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * @size: allocation size
370 *
371 * Allocate enough 32bit PA addressable pages to cover @size from the
372 * page level allocator and map them into continguos kernel virtual space.
373 */
374void *vmalloc_32(unsigned long size)
375{
376 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
377}
Paul Mundtb5073172007-07-21 04:37:25 -0700378EXPORT_SYMBOL(vmalloc_32);
379
380/**
381 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
382 * @size: allocation size
383 *
384 * The resulting memory area is 32bit addressable and zeroed so it can be
385 * mapped to userspace without leaking data.
Paul Mundtf905bc42008-02-04 22:29:59 -0800386 *
387 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
388 * remap_vmalloc_range() are permissible.
Paul Mundtb5073172007-07-21 04:37:25 -0700389 */
390void *vmalloc_32_user(unsigned long size)
391{
Paul Mundtf905bc42008-02-04 22:29:59 -0800392 /*
393 * We'll have to sort out the ZONE_DMA bits for 64-bit,
394 * but for now this can simply use vmalloc_user() directly.
395 */
396 return vmalloc_user(size);
Paul Mundtb5073172007-07-21 04:37:25 -0700397}
398EXPORT_SYMBOL(vmalloc_32_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
401{
402 BUG();
403 return NULL;
404}
Paul Mundtb5073172007-07-21 04:37:25 -0700405EXPORT_SYMBOL(vmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800407void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 BUG();
410}
Paul Mundtb5073172007-07-21 04:37:25 -0700411EXPORT_SYMBOL(vunmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Paul Mundteb6434d2009-01-21 17:45:47 +0900413void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
414{
415 BUG();
416 return NULL;
417}
418EXPORT_SYMBOL(vm_map_ram);
419
420void vm_unmap_ram(const void *mem, unsigned int count)
421{
422 BUG();
423}
424EXPORT_SYMBOL(vm_unmap_ram);
425
426void vm_unmap_aliases(void)
427{
428}
429EXPORT_SYMBOL_GPL(vm_unmap_aliases);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700432 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
433 * have one.
434 */
435void __attribute__((weak)) vmalloc_sync_all(void)
436{
437}
438
Paul Mundtb5073172007-07-21 04:37:25 -0700439int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
440 struct page *page)
441{
442 return -EINVAL;
443}
444EXPORT_SYMBOL(vm_insert_page);
445
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * sys_brk() for the most part doesn't need the global kernel
448 * lock, except when an application is doing something nasty
449 * like trying to un-brk an area that has already been mapped
450 * to a regular file. in this case, the unmapping will need
451 * to invoke file system routines that need the global lock.
452 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100453SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct mm_struct *mm = current->mm;
456
457 if (brk < mm->start_brk || brk > mm->context.end_brk)
458 return mm->brk;
459
460 if (mm->brk == brk)
461 return mm->brk;
462
463 /*
464 * Always allow shrinking brk
465 */
466 if (brk <= mm->brk) {
467 mm->brk = brk;
468 return brk;
469 }
470
471 /*
472 * Ok, looks good - let it rip.
473 */
474 return mm->brk = brk;
475}
476
David Howells8feae132009-01-08 12:04:47 +0000477/*
478 * initialise the VMA and region record slabs
479 */
480void __init mmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700482 int ret;
483
484 ret = percpu_counter_init(&vm_committed_as, 0);
485 VM_BUG_ON(ret);
David Howells33e5d7692009-04-02 16:56:32 -0700486 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
David Howells8feae132009-01-08 12:04:47 +0000487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David Howells8feae132009-01-08 12:04:47 +0000489/*
490 * validate the region tree
491 * - the caller must hold the region lock
492 */
493#ifdef CONFIG_DEBUG_NOMMU_REGIONS
494static noinline void validate_nommu_regions(void)
495{
496 struct vm_region *region, *last;
497 struct rb_node *p, *lastp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Howells8feae132009-01-08 12:04:47 +0000499 lastp = rb_first(&nommu_region_tree);
500 if (!lastp)
501 return;
502
503 last = rb_entry(lastp, struct vm_region, vm_rb);
David Howells33e5d7692009-04-02 16:56:32 -0700504 BUG_ON(unlikely(last->vm_end <= last->vm_start));
505 BUG_ON(unlikely(last->vm_top < last->vm_end));
David Howells8feae132009-01-08 12:04:47 +0000506
507 while ((p = rb_next(lastp))) {
508 region = rb_entry(p, struct vm_region, vm_rb);
509 last = rb_entry(lastp, struct vm_region, vm_rb);
510
David Howells33e5d7692009-04-02 16:56:32 -0700511 BUG_ON(unlikely(region->vm_end <= region->vm_start));
512 BUG_ON(unlikely(region->vm_top < region->vm_end));
513 BUG_ON(unlikely(region->vm_start < last->vm_top));
David Howells8feae132009-01-08 12:04:47 +0000514
515 lastp = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517}
David Howells8feae132009-01-08 12:04:47 +0000518#else
David Howells33e5d7692009-04-02 16:56:32 -0700519static void validate_nommu_regions(void)
520{
521}
David Howells8feae132009-01-08 12:04:47 +0000522#endif
523
524/*
525 * add a region into the global tree
526 */
527static void add_nommu_region(struct vm_region *region)
528{
529 struct vm_region *pregion;
530 struct rb_node **p, *parent;
531
532 validate_nommu_regions();
533
David Howells8feae132009-01-08 12:04:47 +0000534 parent = NULL;
535 p = &nommu_region_tree.rb_node;
536 while (*p) {
537 parent = *p;
538 pregion = rb_entry(parent, struct vm_region, vm_rb);
539 if (region->vm_start < pregion->vm_start)
540 p = &(*p)->rb_left;
541 else if (region->vm_start > pregion->vm_start)
542 p = &(*p)->rb_right;
543 else if (pregion == region)
544 return;
545 else
546 BUG();
547 }
548
549 rb_link_node(&region->vm_rb, parent, p);
550 rb_insert_color(&region->vm_rb, &nommu_region_tree);
551
552 validate_nommu_regions();
553}
554
555/*
556 * delete a region from the global tree
557 */
558static void delete_nommu_region(struct vm_region *region)
559{
560 BUG_ON(!nommu_region_tree.rb_node);
561
562 validate_nommu_regions();
563 rb_erase(&region->vm_rb, &nommu_region_tree);
564 validate_nommu_regions();
565}
566
567/*
568 * free a contiguous series of pages
569 */
570static void free_page_series(unsigned long from, unsigned long to)
571{
572 for (; from < to; from += PAGE_SIZE) {
573 struct page *page = virt_to_page(from);
574
575 kdebug("- free %lx", from);
David Howells33e5d7692009-04-02 16:56:32 -0700576 atomic_long_dec(&mmap_pages_allocated);
David Howells8feae132009-01-08 12:04:47 +0000577 if (page_count(page) != 1)
David Howells33e5d7692009-04-02 16:56:32 -0700578 kdebug("free page %p: refcount not one: %d",
579 page, page_count(page));
David Howells8feae132009-01-08 12:04:47 +0000580 put_page(page);
581 }
582}
583
584/*
585 * release a reference to a region
David Howells33e5d7692009-04-02 16:56:32 -0700586 * - the caller must hold the region semaphore for writing, which this releases
Paul Mundtdd8632a2009-01-08 12:04:47 +0000587 * - the region may not have been added to the tree yet, in which case vm_top
David Howells8feae132009-01-08 12:04:47 +0000588 * will equal vm_start
589 */
590static void __put_nommu_region(struct vm_region *region)
591 __releases(nommu_region_sem)
592{
593 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
594
595 BUG_ON(!nommu_region_tree.rb_node);
596
597 if (atomic_dec_and_test(&region->vm_usage)) {
Paul Mundtdd8632a2009-01-08 12:04:47 +0000598 if (region->vm_top > region->vm_start)
David Howells8feae132009-01-08 12:04:47 +0000599 delete_nommu_region(region);
600 up_write(&nommu_region_sem);
601
602 if (region->vm_file)
603 fput(region->vm_file);
604
605 /* IO memory and memory shared directly out of the pagecache
606 * from ramfs/tmpfs mustn't be released here */
607 if (region->vm_flags & VM_MAPPED_COPY) {
608 kdebug("free series");
Paul Mundtdd8632a2009-01-08 12:04:47 +0000609 free_page_series(region->vm_start, region->vm_top);
David Howells8feae132009-01-08 12:04:47 +0000610 }
611 kmem_cache_free(vm_region_jar, region);
612 } else {
613 up_write(&nommu_region_sem);
614 }
615}
616
617/*
618 * release a reference to a region
619 */
620static void put_nommu_region(struct vm_region *region)
621{
622 down_write(&nommu_region_sem);
623 __put_nommu_region(region);
624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David Howells30340972006-09-27 01:50:20 -0700626/*
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700627 * update protection on a vma
628 */
629static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
630{
631#ifdef CONFIG_MPU
632 struct mm_struct *mm = vma->vm_mm;
633 long start = vma->vm_start & PAGE_MASK;
634 while (start < vma->vm_end) {
635 protect_page(mm, start, flags);
636 start += PAGE_SIZE;
637 }
638 update_protections(mm);
639#endif
640}
641
642/*
David Howells30340972006-09-27 01:50:20 -0700643 * add a VMA into a process's mm_struct in the appropriate place in the list
David Howells8feae132009-01-08 12:04:47 +0000644 * and tree and add to the address space's page tree also if not an anonymous
645 * page
David Howells30340972006-09-27 01:50:20 -0700646 * - should be called with mm->mmap_sem held writelocked
647 */
David Howells8feae132009-01-08 12:04:47 +0000648static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
David Howells30340972006-09-27 01:50:20 -0700649{
David Howells8feae132009-01-08 12:04:47 +0000650 struct vm_area_struct *pvma, **pp;
651 struct address_space *mapping;
652 struct rb_node **p, *parent;
David Howells30340972006-09-27 01:50:20 -0700653
David Howells8feae132009-01-08 12:04:47 +0000654 kenter(",%p", vma);
655
656 BUG_ON(!vma->vm_region);
657
658 mm->map_count++;
659 vma->vm_mm = mm;
660
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700661 protect_vma(vma, vma->vm_flags);
662
David Howells8feae132009-01-08 12:04:47 +0000663 /* add the VMA to the mapping */
664 if (vma->vm_file) {
665 mapping = vma->vm_file->f_mapping;
666
667 flush_dcache_mmap_lock(mapping);
668 vma_prio_tree_insert(vma, &mapping->i_mmap);
669 flush_dcache_mmap_unlock(mapping);
670 }
671
672 /* add the VMA to the tree */
673 parent = NULL;
674 p = &mm->mm_rb.rb_node;
675 while (*p) {
676 parent = *p;
677 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
678
679 /* sort by: start addr, end addr, VMA struct addr in that order
680 * (the latter is necessary as we may get identical VMAs) */
681 if (vma->vm_start < pvma->vm_start)
682 p = &(*p)->rb_left;
683 else if (vma->vm_start > pvma->vm_start)
684 p = &(*p)->rb_right;
685 else if (vma->vm_end < pvma->vm_end)
686 p = &(*p)->rb_left;
687 else if (vma->vm_end > pvma->vm_end)
688 p = &(*p)->rb_right;
689 else if (vma < pvma)
690 p = &(*p)->rb_left;
691 else if (vma > pvma)
692 p = &(*p)->rb_right;
693 else
694 BUG();
695 }
696
697 rb_link_node(&vma->vm_rb, parent, p);
698 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
699
700 /* add VMA to the VMA list also */
701 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
702 if (pvma->vm_start > vma->vm_start)
David Howells30340972006-09-27 01:50:20 -0700703 break;
David Howells8feae132009-01-08 12:04:47 +0000704 if (pvma->vm_start < vma->vm_start)
705 continue;
706 if (pvma->vm_end < vma->vm_end)
707 break;
708 }
David Howells30340972006-09-27 01:50:20 -0700709
David Howells8feae132009-01-08 12:04:47 +0000710 vma->vm_next = *pp;
711 *pp = vma;
712}
713
714/*
715 * delete a VMA from its owning mm_struct and address space
716 */
717static void delete_vma_from_mm(struct vm_area_struct *vma)
718{
719 struct vm_area_struct **pp;
720 struct address_space *mapping;
721 struct mm_struct *mm = vma->vm_mm;
722
723 kenter("%p", vma);
724
Bernd Schmidteb8cdec2009-09-21 17:03:57 -0700725 protect_vma(vma, 0);
726
David Howells8feae132009-01-08 12:04:47 +0000727 mm->map_count--;
728 if (mm->mmap_cache == vma)
729 mm->mmap_cache = NULL;
730
731 /* remove the VMA from the mapping */
732 if (vma->vm_file) {
733 mapping = vma->vm_file->f_mapping;
734
735 flush_dcache_mmap_lock(mapping);
736 vma_prio_tree_remove(vma, &mapping->i_mmap);
737 flush_dcache_mmap_unlock(mapping);
738 }
739
740 /* remove from the MM's tree and list */
741 rb_erase(&vma->vm_rb, &mm->mm_rb);
742 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
743 if (*pp == vma) {
744 *pp = vma->vm_next;
745 break;
746 }
747 }
748
749 vma->vm_mm = NULL;
750}
751
752/*
753 * destroy a VMA record
754 */
755static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
756{
757 kenter("%p", vma);
758 if (vma->vm_ops && vma->vm_ops->close)
759 vma->vm_ops->close(vma);
760 if (vma->vm_file) {
761 fput(vma->vm_file);
762 if (vma->vm_flags & VM_EXECUTABLE)
763 removed_exe_file_vma(mm);
764 }
765 put_nommu_region(vma->vm_region);
766 kmem_cache_free(vm_area_cachep, vma);
David Howells30340972006-09-27 01:50:20 -0700767}
768
769/*
770 * look up the first VMA in which addr resides, NULL if none
771 * - should be called with mm->mmap_sem at least held readlocked
772 */
773struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
774{
David Howells8feae132009-01-08 12:04:47 +0000775 struct vm_area_struct *vma;
776 struct rb_node *n = mm->mm_rb.rb_node;
David Howells30340972006-09-27 01:50:20 -0700777
David Howells8feae132009-01-08 12:04:47 +0000778 /* check the cache first */
779 vma = mm->mmap_cache;
780 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
781 return vma;
782
783 /* trawl the tree (there may be multiple mappings in which addr
784 * resides) */
785 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
786 vma = rb_entry(n, struct vm_area_struct, vm_rb);
787 if (vma->vm_start > addr)
788 return NULL;
789 if (vma->vm_end > addr) {
790 mm->mmap_cache = vma;
791 return vma;
792 }
David Howells30340972006-09-27 01:50:20 -0700793 }
794
David Howells30340972006-09-27 01:50:20 -0700795 return NULL;
796}
797EXPORT_SYMBOL(find_vma);
798
799/*
David Howells930e6522006-09-27 01:50:22 -0700800 * find a VMA
801 * - we don't extend stack VMAs under NOMMU conditions
802 */
803struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
804{
805 return find_vma(mm, addr);
806}
807
David Howells8feae132009-01-08 12:04:47 +0000808/*
809 * expand a stack to a given address
810 * - not supported under NOMMU conditions
811 */
Greg Ungerer57c8f632007-07-15 23:38:28 -0700812int expand_stack(struct vm_area_struct *vma, unsigned long address)
813{
814 return -ENOMEM;
815}
816
David Howells930e6522006-09-27 01:50:22 -0700817/*
David Howells6fa5f802006-09-27 01:50:21 -0700818 * look up the first VMA exactly that exactly matches addr
819 * - should be called with mm->mmap_sem at least held readlocked
820 */
David Howells8feae132009-01-08 12:04:47 +0000821static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
822 unsigned long addr,
823 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct vm_area_struct *vma;
David Howells8feae132009-01-08 12:04:47 +0000826 struct rb_node *n = mm->mm_rb.rb_node;
827 unsigned long end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
David Howells8feae132009-01-08 12:04:47 +0000829 /* check the cache first */
830 vma = mm->mmap_cache;
831 if (vma && vma->vm_start == addr && vma->vm_end == end)
832 return vma;
833
834 /* trawl the tree (there may be multiple mappings in which addr
835 * resides) */
836 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 vma = rb_entry(n, struct vm_area_struct, vm_rb);
David Howells8feae132009-01-08 12:04:47 +0000838 if (vma->vm_start < addr)
839 continue;
840 if (vma->vm_start > addr)
841 return NULL;
842 if (vma->vm_end == end) {
843 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return vma;
David Howells8feae132009-01-08 12:04:47 +0000845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
848 return NULL;
849}
850
David Howells30340972006-09-27 01:50:20 -0700851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * determine whether a mapping should be permitted and, if so, what sort of
853 * mapping we're capable of supporting
854 */
855static int validate_mmap_request(struct file *file,
856 unsigned long addr,
857 unsigned long len,
858 unsigned long prot,
859 unsigned long flags,
860 unsigned long pgoff,
861 unsigned long *_capabilities)
862{
David Howells8feae132009-01-08 12:04:47 +0000863 unsigned long capabilities, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned long reqprot = prot;
865 int ret;
866
867 /* do the simple checks first */
868 if (flags & MAP_FIXED || addr) {
869 printk(KERN_DEBUG
870 "%d: Can't do fixed-address/overlay mmap of RAM\n",
871 current->pid);
872 return -EINVAL;
873 }
874
875 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
876 (flags & MAP_TYPE) != MAP_SHARED)
877 return -EINVAL;
878
Mike Frysingerf81cff02006-12-06 12:02:59 +1000879 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -EINVAL;
881
Mike Frysingerf81cff02006-12-06 12:02:59 +1000882 /* Careful about overflows.. */
David Howells8feae132009-01-08 12:04:47 +0000883 rlen = PAGE_ALIGN(len);
884 if (!rlen || rlen > TASK_SIZE)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000885 return -ENOMEM;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* offset overflow? */
David Howells8feae132009-01-08 12:04:47 +0000888 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
Mike Frysingerf81cff02006-12-06 12:02:59 +1000889 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (file) {
892 /* validate file mapping requests */
893 struct address_space *mapping;
894
895 /* files must support mmap */
896 if (!file->f_op || !file->f_op->mmap)
897 return -ENODEV;
898
899 /* work out if what we've got could possibly be shared
900 * - we support chardevs that provide their own "memory"
901 * - we support files/blockdevs that are memory backed
902 */
903 mapping = file->f_mapping;
904 if (!mapping)
Josef Sipeke9536ae2006-12-08 02:37:21 -0800905 mapping = file->f_path.dentry->d_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 capabilities = 0;
908 if (mapping && mapping->backing_dev_info)
909 capabilities = mapping->backing_dev_info->capabilities;
910
911 if (!capabilities) {
912 /* no explicit capabilities set, so assume some
913 * defaults */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800914 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 case S_IFREG:
916 case S_IFBLK:
917 capabilities = BDI_CAP_MAP_COPY;
918 break;
919
920 case S_IFCHR:
921 capabilities =
922 BDI_CAP_MAP_DIRECT |
923 BDI_CAP_READ_MAP |
924 BDI_CAP_WRITE_MAP;
925 break;
926
927 default:
928 return -EINVAL;
929 }
930 }
931
932 /* eliminate any capabilities that we can't support on this
933 * device */
934 if (!file->f_op->get_unmapped_area)
935 capabilities &= ~BDI_CAP_MAP_DIRECT;
936 if (!file->f_op->read)
937 capabilities &= ~BDI_CAP_MAP_COPY;
938
Graff Yang28d7a6a2009-08-18 14:11:17 -0700939 /* The file shall have been opened with read permission. */
940 if (!(file->f_mode & FMODE_READ))
941 return -EACCES;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (flags & MAP_SHARED) {
944 /* do checks for writing, appending and locking */
945 if ((prot & PROT_WRITE) &&
946 !(file->f_mode & FMODE_WRITE))
947 return -EACCES;
948
Josef Sipeke9536ae2006-12-08 02:37:21 -0800949 if (IS_APPEND(file->f_path.dentry->d_inode) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 (file->f_mode & FMODE_WRITE))
951 return -EACCES;
952
Josef Sipeke9536ae2006-12-08 02:37:21 -0800953 if (locks_verify_locked(file->f_path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return -EAGAIN;
955
956 if (!(capabilities & BDI_CAP_MAP_DIRECT))
957 return -ENODEV;
958
959 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
960 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
961 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
962 ) {
963 printk("MAP_SHARED not completely supported on !MMU\n");
964 return -EINVAL;
965 }
966
967 /* we mustn't privatise shared mappings */
968 capabilities &= ~BDI_CAP_MAP_COPY;
969 }
970 else {
971 /* we're going to read the file into private memory we
972 * allocate */
973 if (!(capabilities & BDI_CAP_MAP_COPY))
974 return -ENODEV;
975
976 /* we don't permit a private writable mapping to be
977 * shared with the backing device */
978 if (prot & PROT_WRITE)
979 capabilities &= ~BDI_CAP_MAP_DIRECT;
980 }
981
982 /* handle executable mappings and implied executable
983 * mappings */
Josef Sipeke9536ae2006-12-08 02:37:21 -0800984 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (prot & PROT_EXEC)
986 return -EPERM;
987 }
988 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
989 /* handle implication of PROT_EXEC by PROT_READ */
990 if (current->personality & READ_IMPLIES_EXEC) {
991 if (capabilities & BDI_CAP_EXEC_MAP)
992 prot |= PROT_EXEC;
993 }
994 }
995 else if ((prot & PROT_READ) &&
996 (prot & PROT_EXEC) &&
997 !(capabilities & BDI_CAP_EXEC_MAP)
998 ) {
999 /* backing file is not executable, try to copy */
1000 capabilities &= ~BDI_CAP_MAP_DIRECT;
1001 }
1002 }
1003 else {
1004 /* anonymous mappings are always memory backed and can be
1005 * privately mapped
1006 */
1007 capabilities = BDI_CAP_MAP_COPY;
1008
1009 /* handle PROT_EXEC implication by PROT_READ */
1010 if ((prot & PROT_READ) &&
1011 (current->personality & READ_IMPLIES_EXEC))
1012 prot |= PROT_EXEC;
1013 }
1014
1015 /* allow the security API to have its say */
Eric Parised032182007-06-28 15:55:21 -04001016 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (ret < 0)
1018 return ret;
1019
1020 /* looks okay */
1021 *_capabilities = capabilities;
1022 return 0;
1023}
1024
1025/*
1026 * we've determined that we can make the mapping, now translate what we
1027 * now know into VMA flags
1028 */
1029static unsigned long determine_vm_flags(struct file *file,
1030 unsigned long prot,
1031 unsigned long flags,
1032 unsigned long capabilities)
1033{
1034 unsigned long vm_flags;
1035
1036 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1037 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1038 /* vm_flags |= mm->def_flags; */
1039
1040 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1041 /* attempt to share read-only copies of mapped file chunks */
1042 if (file && !(prot & PROT_WRITE))
1043 vm_flags |= VM_MAYSHARE;
1044 }
1045 else {
1046 /* overlay a shareable mapping on the backing device or inode
1047 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1048 * romfs/cramfs */
1049 if (flags & MAP_SHARED)
1050 vm_flags |= VM_MAYSHARE | VM_SHARED;
1051 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
1052 vm_flags |= VM_MAYSHARE;
1053 }
1054
1055 /* refuse to let anyone share private mappings with this process if
1056 * it's being traced - otherwise breakpoints set in it may interfere
1057 * with another untraced process
1058 */
Roland McGrathfa8e26c2008-07-25 19:45:50 -07001059 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 vm_flags &= ~VM_MAYSHARE;
1061
1062 return vm_flags;
1063}
1064
1065/*
David Howells8feae132009-01-08 12:04:47 +00001066 * set up a shared mapping on a file (the driver or filesystem provides and
1067 * pins the storage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
David Howells8feae132009-01-08 12:04:47 +00001069static int do_mmap_shared_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int ret;
1072
1073 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001074 if (ret == 0) {
1075 vma->vm_region->vm_top = vma->vm_region->vm_end;
1076 return ret;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (ret != -ENOSYS)
1079 return ret;
1080
1081 /* getting an ENOSYS error indicates that direct mmap isn't
1082 * possible (as opposed to tried but failed) so we'll fall
1083 * through to making a private copy of the data and mapping
1084 * that if we can */
1085 return -ENODEV;
1086}
1087
1088/*
1089 * set up a private mapping or an anonymous shared mapping
1090 */
David Howells8feae132009-01-08 12:04:47 +00001091static int do_mmap_private(struct vm_area_struct *vma,
1092 struct vm_region *region,
1093 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
David Howells8feae132009-01-08 12:04:47 +00001095 struct page *pages;
1096 unsigned long total, point, n, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 void *base;
David Howells8feae132009-01-08 12:04:47 +00001098 int ret, order;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* invoke the file's mapping function so that it can keep track of
1101 * shared mappings on devices or memory
1102 * - VM_MAYSHARE will be set if it may attempt to share
1103 */
1104 if (vma->vm_file) {
1105 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001106 if (ret == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* shouldn't return success if we're not sharing */
Paul Mundtdd8632a2009-01-08 12:04:47 +00001108 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1109 vma->vm_region->vm_top = vma->vm_region->vm_end;
1110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Paul Mundtdd8632a2009-01-08 12:04:47 +00001112 if (ret != -ENOSYS)
1113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /* getting an ENOSYS error indicates that direct mmap isn't
1116 * possible (as opposed to tried but failed) so we'll try to
1117 * make a private copy of the data and map that instead */
1118 }
1119
David Howells8feae132009-01-08 12:04:47 +00001120 rlen = PAGE_ALIGN(len);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* allocate some memory to hold the mapping
1123 * - note that this may not return a page-aligned address if the object
1124 * we're allocating is smaller than a page
1125 */
David Howells8feae132009-01-08 12:04:47 +00001126 order = get_order(rlen);
1127 kdebug("alloc order %d for %lx", order, len);
1128
1129 pages = alloc_pages(GFP_KERNEL, order);
1130 if (!pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto enomem;
1132
David Howells8feae132009-01-08 12:04:47 +00001133 total = 1 << order;
David Howells33e5d7692009-04-02 16:56:32 -07001134 atomic_long_add(total, &mmap_pages_allocated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
David Howells8feae132009-01-08 12:04:47 +00001136 point = rlen >> PAGE_SHIFT;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001137
1138 /* we allocated a power-of-2 sized page set, so we may want to trim off
1139 * the excess */
1140 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1141 while (total > point) {
1142 order = ilog2(total - point);
1143 n = 1 << order;
1144 kdebug("shave %lu/%lu @%lu", n, total - point, total);
David Howells33e5d7692009-04-02 16:56:32 -07001145 atomic_long_sub(n, &mmap_pages_allocated);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001146 total -= n;
1147 set_page_refcounted(pages + total);
1148 __free_pages(pages + total, order);
1149 }
David Howells8feae132009-01-08 12:04:47 +00001150 }
1151
David Howells8feae132009-01-08 12:04:47 +00001152 for (point = 1; point < total; point++)
1153 set_page_refcounted(&pages[point]);
1154
1155 base = page_address(pages);
1156 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1157 region->vm_start = (unsigned long) base;
1158 region->vm_end = region->vm_start + rlen;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001159 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
David Howells8feae132009-01-08 12:04:47 +00001160
1161 vma->vm_start = region->vm_start;
1162 vma->vm_end = region->vm_start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (vma->vm_file) {
1165 /* read the contents of a file into the copy */
1166 mm_segment_t old_fs;
1167 loff_t fpos;
1168
1169 fpos = vma->vm_pgoff;
1170 fpos <<= PAGE_SHIFT;
1171
1172 old_fs = get_fs();
1173 set_fs(KERNEL_DS);
David Howells8feae132009-01-08 12:04:47 +00001174 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 set_fs(old_fs);
1176
1177 if (ret < 0)
1178 goto error_free;
1179
1180 /* clear the last little bit */
David Howells8feae132009-01-08 12:04:47 +00001181 if (ret < rlen)
1182 memset(base + ret, 0, rlen - ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 } else {
1185 /* if it's an anonymous mapping, then just clear it */
David Howells8feae132009-01-08 12:04:47 +00001186 memset(base, 0, rlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
1189 return 0;
1190
1191error_free:
David Howells8feae132009-01-08 12:04:47 +00001192 free_page_series(region->vm_start, region->vm_end);
1193 region->vm_start = vma->vm_start = 0;
1194 region->vm_end = vma->vm_end = 0;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001195 region->vm_top = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return ret;
1197
1198enomem:
Greg Ungerer05ae6fa2009-01-13 17:30:22 +10001199 printk("Allocation of length %lu from process %d (%s) failed\n",
1200 len, current->pid, current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 show_free_areas();
1202 return -ENOMEM;
1203}
1204
1205/*
1206 * handle mapping creation for uClinux
1207 */
1208unsigned long do_mmap_pgoff(struct file *file,
1209 unsigned long addr,
1210 unsigned long len,
1211 unsigned long prot,
1212 unsigned long flags,
1213 unsigned long pgoff)
1214{
David Howells8feae132009-01-08 12:04:47 +00001215 struct vm_area_struct *vma;
1216 struct vm_region *region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct rb_node *rb;
David Howells8feae132009-01-08 12:04:47 +00001218 unsigned long capabilities, vm_flags, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int ret;
1220
David Howells8feae132009-01-08 12:04:47 +00001221 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1222
Eric Paris7cd94142007-11-26 18:47:40 -05001223 if (!(flags & MAP_FIXED))
1224 addr = round_hint_to_min(addr);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* decide whether we should attempt the mapping, and if so what sort of
1227 * mapping */
1228 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1229 &capabilities);
David Howells8feae132009-01-08 12:04:47 +00001230 if (ret < 0) {
1231 kleave(" = %d [val]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return ret;
David Howells8feae132009-01-08 12:04:47 +00001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* we've determined that we can make the mapping, now translate what we
1236 * now know into VMA flags */
1237 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1238
David Howells8feae132009-01-08 12:04:47 +00001239 /* we're going to need to record the mapping */
1240 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1241 if (!region)
1242 goto error_getting_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
David Howells8feae132009-01-08 12:04:47 +00001244 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1245 if (!vma)
1246 goto error_getting_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
David Howells8feae132009-01-08 12:04:47 +00001248 atomic_set(&region->vm_usage, 1);
1249 region->vm_flags = vm_flags;
1250 region->vm_pgoff = pgoff;
1251
1252 INIT_LIST_HEAD(&vma->anon_vma_node);
1253 vma->vm_flags = vm_flags;
1254 vma->vm_pgoff = pgoff;
1255
1256 if (file) {
1257 region->vm_file = file;
1258 get_file(file);
1259 vma->vm_file = file;
1260 get_file(file);
1261 if (vm_flags & VM_EXECUTABLE) {
1262 added_exe_file_vma(current->mm);
1263 vma->vm_mm = current->mm;
1264 }
1265 }
1266
1267 down_write(&nommu_region_sem);
1268
1269 /* if we want to share, we need to check for regions created by other
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * mmap() calls that overlap with our proposed mapping
David Howells8feae132009-01-08 12:04:47 +00001271 * - we can only share with a superset match on most regular files
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * - shared mappings on character devices and memory backed files are
1273 * permitted to overlap inexactly as far as we are concerned for in
1274 * these cases, sharing is handled in the driver or filesystem rather
1275 * than here
1276 */
1277 if (vm_flags & VM_MAYSHARE) {
David Howells8feae132009-01-08 12:04:47 +00001278 struct vm_region *pregion;
1279 unsigned long pglen, rpglen, pgend, rpgend, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
David Howells8feae132009-01-08 12:04:47 +00001281 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1282 pgend = pgoff + pglen;
David Howells165b2392007-03-22 00:11:24 -08001283
David Howells8feae132009-01-08 12:04:47 +00001284 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1285 pregion = rb_entry(rb, struct vm_region, vm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
David Howells8feae132009-01-08 12:04:47 +00001287 if (!(pregion->vm_flags & VM_MAYSHARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 continue;
1289
1290 /* search for overlapping mappings on the same file */
David Howells8feae132009-01-08 12:04:47 +00001291 if (pregion->vm_file->f_path.dentry->d_inode !=
1292 file->f_path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 continue;
1294
David Howells8feae132009-01-08 12:04:47 +00001295 if (pregion->vm_pgoff >= pgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 continue;
1297
David Howells8feae132009-01-08 12:04:47 +00001298 rpglen = pregion->vm_end - pregion->vm_start;
1299 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1300 rpgend = pregion->vm_pgoff + rpglen;
1301 if (pgoff >= rpgend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 continue;
1303
David Howells8feae132009-01-08 12:04:47 +00001304 /* handle inexactly overlapping matches between
1305 * mappings */
1306 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1307 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1308 /* new mapping is not a subset of the region */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1310 goto sharing_violation;
1311 continue;
1312 }
1313
David Howells8feae132009-01-08 12:04:47 +00001314 /* we've found a region we can share */
1315 atomic_inc(&pregion->vm_usage);
1316 vma->vm_region = pregion;
1317 start = pregion->vm_start;
1318 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1319 vma->vm_start = start;
1320 vma->vm_end = start + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
David Howells8feae132009-01-08 12:04:47 +00001322 if (pregion->vm_flags & VM_MAPPED_COPY) {
1323 kdebug("share copy");
1324 vma->vm_flags |= VM_MAPPED_COPY;
1325 } else {
1326 kdebug("share mmap");
1327 ret = do_mmap_shared_file(vma);
1328 if (ret < 0) {
1329 vma->vm_region = NULL;
1330 vma->vm_start = 0;
1331 vma->vm_end = 0;
1332 atomic_dec(&pregion->vm_usage);
1333 pregion = NULL;
1334 goto error_just_free;
1335 }
1336 }
1337 fput(region->vm_file);
1338 kmem_cache_free(vm_region_jar, region);
1339 region = pregion;
1340 result = start;
1341 goto share;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /* obtain the address at which to make a shared mapping
1345 * - this is the hook for quasi-memory character devices to
1346 * tell us the location of a shared mapping
1347 */
1348 if (file && file->f_op->get_unmapped_area) {
1349 addr = file->f_op->get_unmapped_area(file, addr, len,
1350 pgoff, flags);
1351 if (IS_ERR((void *) addr)) {
1352 ret = addr;
1353 if (ret != (unsigned long) -ENOSYS)
David Howells8feae132009-01-08 12:04:47 +00001354 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* the driver refused to tell us where to site
1357 * the mapping so we'll have to attempt to copy
1358 * it */
1359 ret = (unsigned long) -ENODEV;
1360 if (!(capabilities & BDI_CAP_MAP_COPY))
David Howells8feae132009-01-08 12:04:47 +00001361 goto error_just_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 capabilities &= ~BDI_CAP_MAP_DIRECT;
David Howells8feae132009-01-08 12:04:47 +00001364 } else {
1365 vma->vm_start = region->vm_start = addr;
1366 vma->vm_end = region->vm_end = addr + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
1369 }
1370
David Howells8feae132009-01-08 12:04:47 +00001371 vma->vm_region = region;
David Howellsa1908872009-09-05 11:17:07 -07001372 add_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /* set up the mapping */
1375 if (file && vma->vm_flags & VM_SHARED)
David Howells8feae132009-01-08 12:04:47 +00001376 ret = do_mmap_shared_file(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 else
David Howells8feae132009-01-08 12:04:47 +00001378 ret = do_mmap_private(vma, region, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (ret < 0)
David Howells8feae132009-01-08 12:04:47 +00001380 goto error_put_region;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* okay... we have a mapping; now we have to register it */
David Howells8feae132009-01-08 12:04:47 +00001383 result = vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 current->mm->total_vm += len >> PAGE_SHIFT;
1386
David Howells8feae132009-01-08 12:04:47 +00001387share:
1388 add_vma_to_mm(current->mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
David Howells8feae132009-01-08 12:04:47 +00001390 up_write(&nommu_region_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (prot & PROT_EXEC)
David Howells8feae132009-01-08 12:04:47 +00001393 flush_icache_range(result, result + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
David Howells8feae132009-01-08 12:04:47 +00001395 kleave(" = %lx", result);
1396 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
David Howells8feae132009-01-08 12:04:47 +00001398error_put_region:
1399 __put_nommu_region(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (vma) {
Matt Helsley925d1c42008-04-29 01:01:36 -07001401 if (vma->vm_file) {
Gavin Lambert3fcd03e2006-09-30 23:27:01 -07001402 fput(vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07001403 if (vma->vm_flags & VM_EXECUTABLE)
1404 removed_exe_file_vma(vma->vm_mm);
1405 }
David Howells8feae132009-01-08 12:04:47 +00001406 kmem_cache_free(vm_area_cachep, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
David Howells8feae132009-01-08 12:04:47 +00001408 kleave(" = %d [pr]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return ret;
1410
David Howells8feae132009-01-08 12:04:47 +00001411error_just_free:
1412 up_write(&nommu_region_sem);
1413error:
1414 fput(region->vm_file);
1415 kmem_cache_free(vm_region_jar, region);
1416 fput(vma->vm_file);
1417 if (vma->vm_flags & VM_EXECUTABLE)
1418 removed_exe_file_vma(vma->vm_mm);
1419 kmem_cache_free(vm_area_cachep, vma);
1420 kleave(" = %d", ret);
1421 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
David Howells8feae132009-01-08 12:04:47 +00001423sharing_violation:
1424 up_write(&nommu_region_sem);
1425 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1426 ret = -EINVAL;
1427 goto error;
1428
1429error_getting_vma:
1430 kmem_cache_free(vm_region_jar, region);
1431 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1432 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 len, current->pid);
1434 show_free_areas();
1435 return -ENOMEM;
1436
David Howells8feae132009-01-08 12:04:47 +00001437error_getting_region:
1438 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1439 " from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 len, current->pid);
1441 show_free_areas();
1442 return -ENOMEM;
1443}
Paul Mundtb5073172007-07-21 04:37:25 -07001444EXPORT_SYMBOL(do_mmap_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446/*
David Howells8feae132009-01-08 12:04:47 +00001447 * split a vma into two pieces at address 'addr', a new vma is allocated either
1448 * for the first part or the tail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
David Howells8feae132009-01-08 12:04:47 +00001450int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1451 unsigned long addr, int new_below)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
David Howells8feae132009-01-08 12:04:47 +00001453 struct vm_area_struct *new;
1454 struct vm_region *region;
1455 unsigned long npages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
David Howells8feae132009-01-08 12:04:47 +00001457 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David Howells8feae132009-01-08 12:04:47 +00001459 /* we're only permitted to split anonymous regions that have a single
1460 * owner */
1461 if (vma->vm_file ||
1462 atomic_read(&vma->vm_region->vm_usage) != 1)
1463 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
David Howells8feae132009-01-08 12:04:47 +00001465 if (mm->map_count >= sysctl_max_map_count)
1466 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
David Howells8feae132009-01-08 12:04:47 +00001468 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1469 if (!region)
1470 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
David Howells8feae132009-01-08 12:04:47 +00001472 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1473 if (!new) {
1474 kmem_cache_free(vm_region_jar, region);
1475 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
David Howells8feae132009-01-08 12:04:47 +00001477
1478 /* most fields are the same, copy all, and then fixup */
1479 *new = *vma;
1480 *region = *vma->vm_region;
1481 new->vm_region = region;
1482
1483 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1484
1485 if (new_below) {
Paul Mundtdd8632a2009-01-08 12:04:47 +00001486 region->vm_top = region->vm_end = new->vm_end = addr;
David Howells8feae132009-01-08 12:04:47 +00001487 } else {
1488 region->vm_start = new->vm_start = addr;
1489 region->vm_pgoff = new->vm_pgoff += npages;
1490 }
1491
1492 if (new->vm_ops && new->vm_ops->open)
1493 new->vm_ops->open(new);
1494
1495 delete_vma_from_mm(vma);
1496 down_write(&nommu_region_sem);
1497 delete_nommu_region(vma->vm_region);
1498 if (new_below) {
1499 vma->vm_region->vm_start = vma->vm_start = addr;
1500 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1501 } else {
1502 vma->vm_region->vm_end = vma->vm_end = addr;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001503 vma->vm_region->vm_top = addr;
David Howells8feae132009-01-08 12:04:47 +00001504 }
1505 add_nommu_region(vma->vm_region);
1506 add_nommu_region(new->vm_region);
1507 up_write(&nommu_region_sem);
1508 add_vma_to_mm(mm, vma);
1509 add_vma_to_mm(mm, new);
1510 return 0;
1511}
1512
1513/*
1514 * shrink a VMA by removing the specified chunk from either the beginning or
1515 * the end
1516 */
1517static int shrink_vma(struct mm_struct *mm,
1518 struct vm_area_struct *vma,
1519 unsigned long from, unsigned long to)
1520{
1521 struct vm_region *region;
1522
1523 kenter("");
1524
1525 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1526 * and list */
1527 delete_vma_from_mm(vma);
1528 if (from > vma->vm_start)
1529 vma->vm_end = from;
1530 else
1531 vma->vm_start = to;
1532 add_vma_to_mm(mm, vma);
1533
1534 /* cut the backing region down to size */
1535 region = vma->vm_region;
1536 BUG_ON(atomic_read(&region->vm_usage) != 1);
1537
1538 down_write(&nommu_region_sem);
1539 delete_nommu_region(region);
Paul Mundtdd8632a2009-01-08 12:04:47 +00001540 if (from > region->vm_start) {
1541 to = region->vm_top;
1542 region->vm_top = region->vm_end = from;
1543 } else {
David Howells8feae132009-01-08 12:04:47 +00001544 region->vm_start = to;
Paul Mundtdd8632a2009-01-08 12:04:47 +00001545 }
David Howells8feae132009-01-08 12:04:47 +00001546 add_nommu_region(region);
1547 up_write(&nommu_region_sem);
1548
1549 free_page_series(from, to);
1550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
David Howells30340972006-09-27 01:50:20 -07001553/*
1554 * release a mapping
David Howells8feae132009-01-08 12:04:47 +00001555 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1556 * VMA, though it need not cover the whole VMA
David Howells30340972006-09-27 01:50:20 -07001557 */
David Howells8feae132009-01-08 12:04:47 +00001558int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
David Howells8feae132009-01-08 12:04:47 +00001560 struct vm_area_struct *vma;
1561 struct rb_node *rb;
1562 unsigned long end = start + len;
1563 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
David Howells8feae132009-01-08 12:04:47 +00001565 kenter(",%lx,%zx", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
David Howells8feae132009-01-08 12:04:47 +00001567 if (len == 0)
1568 return -EINVAL;
1569
1570 /* find the first potentially overlapping VMA */
1571 vma = find_vma(mm, start);
1572 if (!vma) {
David Howells33e5d7692009-04-02 16:56:32 -07001573 static int limit = 0;
1574 if (limit < 5) {
1575 printk(KERN_WARNING
1576 "munmap of memory not mmapped by process %d"
1577 " (%s): 0x%lx-0x%lx\n",
1578 current->pid, current->comm,
1579 start, start + len - 1);
1580 limit++;
1581 }
David Howells8feae132009-01-08 12:04:47 +00001582 return -EINVAL;
David Howells30340972006-09-27 01:50:20 -07001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
David Howells8feae132009-01-08 12:04:47 +00001585 /* we're allowed to split an anonymous VMA but not a file-backed one */
1586 if (vma->vm_file) {
1587 do {
1588 if (start > vma->vm_start) {
1589 kleave(" = -EINVAL [miss]");
1590 return -EINVAL;
1591 }
1592 if (end == vma->vm_end)
1593 goto erase_whole_vma;
1594 rb = rb_next(&vma->vm_rb);
1595 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1596 } while (rb);
1597 kleave(" = -EINVAL [split file]");
1598 return -EINVAL;
1599 } else {
1600 /* the chunk must be a subset of the VMA found */
1601 if (start == vma->vm_start && end == vma->vm_end)
1602 goto erase_whole_vma;
1603 if (start < vma->vm_start || end > vma->vm_end) {
1604 kleave(" = -EINVAL [superset]");
1605 return -EINVAL;
1606 }
1607 if (start & ~PAGE_MASK) {
1608 kleave(" = -EINVAL [unaligned start]");
1609 return -EINVAL;
1610 }
1611 if (end != vma->vm_end && end & ~PAGE_MASK) {
1612 kleave(" = -EINVAL [unaligned split]");
1613 return -EINVAL;
1614 }
1615 if (start != vma->vm_start && end != vma->vm_end) {
1616 ret = split_vma(mm, vma, start, 1);
1617 if (ret < 0) {
1618 kleave(" = %d [split]", ret);
1619 return ret;
1620 }
1621 }
1622 return shrink_vma(mm, vma, start, end);
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
David Howells8feae132009-01-08 12:04:47 +00001625erase_whole_vma:
1626 delete_vma_from_mm(vma);
1627 delete_vma(mm, vma);
1628 kleave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return 0;
1630}
Paul Mundtb5073172007-07-21 04:37:25 -07001631EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001633SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
David Howells30340972006-09-27 01:50:20 -07001634{
1635 int ret;
1636 struct mm_struct *mm = current->mm;
1637
1638 down_write(&mm->mmap_sem);
1639 ret = do_munmap(mm, addr, len);
1640 up_write(&mm->mmap_sem);
1641 return ret;
1642}
1643
1644/*
David Howells8feae132009-01-08 12:04:47 +00001645 * release all the mappings made in a process's VM space
David Howells30340972006-09-27 01:50:20 -07001646 */
David Howells8feae132009-01-08 12:04:47 +00001647void exit_mmap(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
David Howells8feae132009-01-08 12:04:47 +00001649 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
David Howells8feae132009-01-08 12:04:47 +00001651 if (!mm)
1652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
David Howells8feae132009-01-08 12:04:47 +00001654 kenter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
David Howells8feae132009-01-08 12:04:47 +00001656 mm->total_vm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
David Howells8feae132009-01-08 12:04:47 +00001658 while ((vma = mm->mmap)) {
1659 mm->mmap = vma->vm_next;
1660 delete_vma_from_mm(vma);
1661 delete_vma(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
David Howells8feae132009-01-08 12:04:47 +00001663
1664 kleave("");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667unsigned long do_brk(unsigned long addr, unsigned long len)
1668{
1669 return -ENOMEM;
1670}
1671
1672/*
David Howells6fa5f802006-09-27 01:50:21 -07001673 * expand (or shrink) an existing mapping, potentially moving it at the same
1674 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *
David Howells6fa5f802006-09-27 01:50:21 -07001676 * under NOMMU conditions, we only permit changing a mapping's size, and only
David Howells8feae132009-01-08 12:04:47 +00001677 * as long as it stays within the region allocated by do_mmap_private() and the
1678 * block is not shareable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 *
David Howells6fa5f802006-09-27 01:50:21 -07001680 * MREMAP_FIXED is not supported under NOMMU conditions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
1682unsigned long do_mremap(unsigned long addr,
1683 unsigned long old_len, unsigned long new_len,
1684 unsigned long flags, unsigned long new_addr)
1685{
David Howells6fa5f802006-09-27 01:50:21 -07001686 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /* insanity checks first */
David Howells8feae132009-01-08 12:04:47 +00001689 if (old_len == 0 || new_len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return (unsigned long) -EINVAL;
1691
David Howells8feae132009-01-08 12:04:47 +00001692 if (addr & ~PAGE_MASK)
1693 return -EINVAL;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (flags & MREMAP_FIXED && new_addr != addr)
1696 return (unsigned long) -EINVAL;
1697
David Howells8feae132009-01-08 12:04:47 +00001698 vma = find_vma_exact(current->mm, addr, old_len);
David Howells6fa5f802006-09-27 01:50:21 -07001699 if (!vma)
1700 return (unsigned long) -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
David Howells6fa5f802006-09-27 01:50:21 -07001702 if (vma->vm_end != vma->vm_start + old_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return (unsigned long) -EFAULT;
1704
David Howells6fa5f802006-09-27 01:50:21 -07001705 if (vma->vm_flags & VM_MAYSHARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return (unsigned long) -EPERM;
1707
David Howells8feae132009-01-08 12:04:47 +00001708 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return (unsigned long) -ENOMEM;
1710
1711 /* all checks complete - do it */
David Howells6fa5f802006-09-27 01:50:21 -07001712 vma->vm_end = vma->vm_start + new_len;
David Howells6fa5f802006-09-27 01:50:21 -07001713 return vma->vm_start;
1714}
Paul Mundtb5073172007-07-21 04:37:25 -07001715EXPORT_SYMBOL(do_mremap);
David Howells6fa5f802006-09-27 01:50:21 -07001716
Heiko Carstens6a6160a2009-01-14 14:14:15 +01001717SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1718 unsigned long, new_len, unsigned long, flags,
1719 unsigned long, new_addr)
David Howells6fa5f802006-09-27 01:50:21 -07001720{
1721 unsigned long ret;
1722
1723 down_write(&current->mm->mmap_sem);
1724 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1725 up_write(&current->mm->mmap_sem);
1726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Linus Torvalds6aab3412005-11-28 14:34:23 -08001729struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001730 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 return NULL;
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1736 unsigned long to, unsigned long size, pgprot_t prot)
1737{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001738 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
Luke Yang22c4af42006-07-14 00:24:09 -07001741EXPORT_SYMBOL(remap_pfn_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Paul Mundtf905bc42008-02-04 22:29:59 -08001743int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1744 unsigned long pgoff)
1745{
1746 unsigned int size = vma->vm_end - vma->vm_start;
1747
1748 if (!(vma->vm_flags & VM_USERMAP))
1749 return -EINVAL;
1750
1751 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1752 vma->vm_end = vma->vm_start + size;
1753
1754 return 0;
1755}
1756EXPORT_SYMBOL(remap_vmalloc_range);
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1759{
1760}
1761
1762unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1763 unsigned long len, unsigned long pgoff, unsigned long flags)
1764{
1765 return -ENOMEM;
1766}
1767
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001768void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772void unmap_mapping_range(struct address_space *mapping,
1773 loff_t const holebegin, loff_t const holelen,
1774 int even_cows)
1775{
1776}
Luke Yang22c4af42006-07-14 00:24:09 -07001777EXPORT_SYMBOL(unmap_mapping_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779/*
David Howellsd56e03c2007-03-22 00:11:23 -08001780 * ask for an unmapped area at which to create a mapping on a file
1781 */
1782unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1783 unsigned long len, unsigned long pgoff,
1784 unsigned long flags)
1785{
1786 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1787 unsigned long, unsigned long);
1788
1789 get_area = current->mm->get_unmapped_area;
1790 if (file && file->f_op && file->f_op->get_unmapped_area)
1791 get_area = file->f_op->get_unmapped_area;
1792
1793 if (!get_area)
1794 return -ENOSYS;
1795
1796 return get_area(file, addr, len, pgoff, flags);
1797}
David Howellsd56e03c2007-03-22 00:11:23 -08001798EXPORT_SYMBOL(get_unmapped_area);
1799
1800/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 * Check that a process has enough memory to allocate a new virtual
1802 * mapping. 0 means there is enough memory for the allocation to
1803 * succeed and -ENOMEM implies there is not.
1804 *
1805 * We currently support three overcommit policies, which are set via the
1806 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1807 *
1808 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1809 * Additional code 2002 Jul 20 by Robert Love.
1810 *
1811 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1812 *
1813 * Note this is a helper function intended to be used by LSMs which
1814 * wish to use this logic.
1815 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001816int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 unsigned long free, allowed;
1819
1820 vm_acct_memory(pages);
1821
1822 /*
1823 * Sometimes we want to use more memory than we have
1824 */
1825 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1826 return 0;
1827
1828 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1829 unsigned long n;
1830
Christoph Lameter347ce432006-06-30 01:55:35 -07001831 free = global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 free += nr_swap_pages;
1833
1834 /*
1835 * Any slabs which are created with the
1836 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1837 * which are reclaimable, under pressure. The dentry
1838 * cache and most inode caches should fall into this
1839 */
Christoph Lameter972d1a72006-09-25 23:31:51 -07001840 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /*
1843 * Leave the last 3% for root
1844 */
1845 if (!cap_sys_admin)
1846 free -= free / 32;
1847
1848 if (free > pages)
1849 return 0;
1850
1851 /*
1852 * nr_free_pages() is very expensive on large systems,
1853 * only call if we're about to fail.
1854 */
1855 n = nr_free_pages();
Hideo AOKId5ddc792006-04-10 22:53:01 -07001856
1857 /*
1858 * Leave reserved pages. The pages are not for anonymous pages.
1859 */
1860 if (n <= totalreserve_pages)
1861 goto error;
1862 else
1863 n -= totalreserve_pages;
1864
1865 /*
1866 * Leave the last 3% for root
1867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (!cap_sys_admin)
1869 n -= n / 32;
1870 free += n;
1871
1872 if (free > pages)
1873 return 0;
Hideo AOKId5ddc792006-04-10 22:53:01 -07001874
1875 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877
1878 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1879 /*
1880 * Leave the last 3% for root
1881 */
1882 if (!cap_sys_admin)
1883 allowed -= allowed / 32;
1884 allowed += total_swap_pages;
1885
1886 /* Don't let a single process grow too big:
1887 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -07001888 if (mm)
1889 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001891 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07001893
Hideo AOKId5ddc792006-04-10 22:53:01 -07001894error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 vm_unacct_memory(pages);
1896
1897 return -ENOMEM;
1898}
1899
1900int in_gate_area_no_task(unsigned long addr)
1901{
1902 return 0;
1903}
David Howellsb0e15192006-01-06 00:11:42 -08001904
Nick Piggind0217ac2007-07-19 01:47:03 -07001905int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
David Howellsb0e15192006-01-06 00:11:42 -08001906{
1907 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07001908 return 0;
David Howellsb0e15192006-01-06 00:11:42 -08001909}
Paul Mundtb5073172007-07-21 04:37:25 -07001910EXPORT_SYMBOL(filemap_fault);
David Howells0ec76a12006-09-27 01:50:15 -07001911
1912/*
1913 * Access another process' address space.
1914 * - source/target buffer must be kernel space
1915 */
1916int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1917{
David Howells0ec76a12006-09-27 01:50:15 -07001918 struct vm_area_struct *vma;
1919 struct mm_struct *mm;
1920
1921 if (addr + len < addr)
1922 return 0;
1923
1924 mm = get_task_mm(tsk);
1925 if (!mm)
1926 return 0;
1927
1928 down_read(&mm->mmap_sem);
1929
1930 /* the access must start within one of the target process's mappings */
David Howells0159b142006-09-27 01:50:16 -07001931 vma = find_vma(mm, addr);
1932 if (vma) {
David Howells0ec76a12006-09-27 01:50:15 -07001933 /* don't overrun this mapping */
1934 if (addr + len >= vma->vm_end)
1935 len = vma->vm_end - addr;
1936
1937 /* only read or write mappings where it is permitted */
David Howellsd00c7b992006-09-27 01:50:19 -07001938 if (write && vma->vm_flags & VM_MAYWRITE)
David Howells0ec76a12006-09-27 01:50:15 -07001939 len -= copy_to_user((void *) addr, buf, len);
David Howellsd00c7b992006-09-27 01:50:19 -07001940 else if (!write && vma->vm_flags & VM_MAYREAD)
David Howells0ec76a12006-09-27 01:50:15 -07001941 len -= copy_from_user(buf, (void *) addr, len);
1942 else
1943 len = 0;
1944 } else {
1945 len = 0;
1946 }
1947
1948 up_read(&mm->mmap_sem);
1949 mmput(mm);
1950 return len;
1951}