nommu: provide follow_pfn().
[linux-2.6-block.git] / mm / nommu.c
CommitLineData
1da177e4
LT
1/*
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 *
8feae131 9 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
1da177e4
LT
10 * 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>
eb6434d9 13 * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
1da177e4
LT
14 */
15
f2b8544f 16#include <linux/module.h>
1da177e4
LT
17#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>
fa8e26cc 25#include <linux/tracehook.h>
1da177e4
LT
26#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>
8feae131
DH
36#include "internal.h"
37
38static inline __attribute__((format(printf, 1, 2)))
39void no_printk(const char *fmt, ...)
40{
41}
42
43#if 0
44#define kenter(FMT, ...) \
45 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
46#define kleave(FMT, ...) \
47 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
48#define kdebug(FMT, ...) \
49 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
50#else
51#define kenter(FMT, ...) \
52 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
53#define kleave(FMT, ...) \
54 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
55#define kdebug(FMT, ...) \
56 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
57#endif
1da177e4 58
b291f000
NP
59#include "internal.h"
60
1da177e4
LT
61void *high_memory;
62struct page *mem_map;
63unsigned long max_mapnr;
64unsigned long num_physpages;
00a62ce9 65struct percpu_counter vm_committed_as;
1da177e4
LT
66int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
67int sysctl_overcommit_ratio = 50; /* default is 50% */
68int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
fc4d5c29 69int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
1da177e4
LT
70int heap_stack_gap = 0;
71
35f2c2f6
PM
72/* amount of vm to protect from userspace access */
73unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
74
33e5d769 75atomic_long_t mmap_pages_allocated;
8feae131 76
1da177e4 77EXPORT_SYMBOL(mem_map);
6a04de6d 78EXPORT_SYMBOL(num_physpages);
1da177e4 79
8feae131
DH
80/* list of mapped, potentially shareable regions */
81static struct kmem_cache *vm_region_jar;
82struct rb_root nommu_region_tree = RB_ROOT;
83DECLARE_RWSEM(nommu_region_sem);
1da177e4
LT
84
85struct vm_operations_struct generic_file_vm_ops = {
86};
87
88/*
89 * Handle all mappings that got truncated by a "truncate()"
90 * system call.
91 *
92 * NOTE! We have to be ready to update the memory sharing
93 * between the file and the memory map for a potential last
94 * incomplete page. Ugly, but necessary.
95 */
96int vmtruncate(struct inode *inode, loff_t offset)
97{
98 struct address_space *mapping = inode->i_mapping;
99 unsigned long limit;
100
101 if (inode->i_size < offset)
102 goto do_expand;
103 i_size_write(inode, offset);
104
105 truncate_inode_pages(mapping, offset);
106 goto out_truncate;
107
108do_expand:
109 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
110 if (limit != RLIM_INFINITY && offset > limit)
111 goto out_sig;
112 if (offset > inode->i_sb->s_maxbytes)
113 goto out;
114 i_size_write(inode, offset);
115
116out_truncate:
acfa4380 117 if (inode->i_op->truncate)
1da177e4
LT
118 inode->i_op->truncate(inode);
119 return 0;
120out_sig:
121 send_sig(SIGXFSZ, current, 0);
122out:
123 return -EFBIG;
124}
125
126EXPORT_SYMBOL(vmtruncate);
127
128/*
129 * Return the total memory allocated for this pointer, not
130 * just what the caller asked for.
131 *
132 * Doesn't have to be accurate, i.e. may have races.
133 */
134unsigned int kobjsize(const void *objp)
135{
136 struct page *page;
137
4016a139
MH
138 /*
139 * If the object we have should not have ksize performed on it,
140 * return size of 0
141 */
5a1603be 142 if (!objp || !virt_addr_valid(objp))
6cfd53fc
PM
143 return 0;
144
145 page = virt_to_head_page(objp);
6cfd53fc
PM
146
147 /*
148 * If the allocator sets PageSlab, we know the pointer came from
149 * kmalloc().
150 */
1da177e4
LT
151 if (PageSlab(page))
152 return ksize(objp);
153
ab2e83ea
PM
154 /*
155 * If it's not a compound page, see if we have a matching VMA
156 * region. This test is intentionally done in reverse order,
157 * so if there's no VMA, we still fall through and hand back
158 * PAGE_SIZE for 0-order pages.
159 */
160 if (!PageCompound(page)) {
161 struct vm_area_struct *vma;
162
163 vma = find_vma(current->mm, (unsigned long)objp);
164 if (vma)
165 return vma->vm_end - vma->vm_start;
166 }
167
6cfd53fc
PM
168 /*
169 * The ksize() function is only guaranteed to work for pointers
5a1603be 170 * returned by kmalloc(). So handle arbitrary pointers here.
6cfd53fc 171 */
5a1603be 172 return PAGE_SIZE << compound_order(page);
1da177e4
LT
173}
174
b291f000
NP
175int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
176 unsigned long start, int len, int flags,
177 struct page **pages, struct vm_area_struct **vmas)
1da177e4 178{
910e46da 179 struct vm_area_struct *vma;
7b4d5b8b
DH
180 unsigned long vm_flags;
181 int i;
b291f000
NP
182 int write = !!(flags & GUP_FLAGS_WRITE);
183 int force = !!(flags & GUP_FLAGS_FORCE);
184 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
7b4d5b8b
DH
185
186 /* calculate required read or write permissions.
187 * - if 'force' is set, we only require the "MAY" flags.
188 */
189 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
190 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1da177e4
LT
191
192 for (i = 0; i < len; i++) {
910e46da 193 vma = find_vma(mm, start);
7b4d5b8b
DH
194 if (!vma)
195 goto finish_or_fault;
196
197 /* protect what we can, including chardevs */
198 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
b291f000 199 (!ignore && !(vm_flags & vma->vm_flags)))
7b4d5b8b 200 goto finish_or_fault;
910e46da 201
1da177e4
LT
202 if (pages) {
203 pages[i] = virt_to_page(start);
204 if (pages[i])
205 page_cache_get(pages[i]);
206 }
207 if (vmas)
910e46da 208 vmas[i] = vma;
1da177e4
LT
209 start += PAGE_SIZE;
210 }
7b4d5b8b
DH
211
212 return i;
213
214finish_or_fault:
215 return i ? : -EFAULT;
1da177e4 216}
b291f000
NP
217
218
219/*
220 * get a list of pages in an address range belonging to the specified process
221 * and indicate the VMA that covers each page
222 * - this is potentially dodgy as we may end incrementing the page count of a
223 * slab page or a secondary page from a compound page
224 * - don't permit access to VMAs that don't support it, such as I/O mappings
225 */
226int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
227 unsigned long start, int len, int write, int force,
228 struct page **pages, struct vm_area_struct **vmas)
229{
230 int flags = 0;
231
232 if (write)
233 flags |= GUP_FLAGS_WRITE;
234 if (force)
235 flags |= GUP_FLAGS_FORCE;
236
237 return __get_user_pages(tsk, mm,
238 start, len, flags,
239 pages, vmas);
240}
66aa2b4b
GU
241EXPORT_SYMBOL(get_user_pages);
242
dfc2f91a
PM
243/**
244 * follow_pfn - look up PFN at a user virtual address
245 * @vma: memory mapping
246 * @address: user virtual address
247 * @pfn: location to store found PFN
248 *
249 * Only IO mappings and raw PFN mappings are allowed.
250 *
251 * Returns zero and the pfn at @pfn on success, -ve otherwise.
252 */
253int follow_pfn(struct vm_area_struct *vma, unsigned long address,
254 unsigned long *pfn)
255{
256 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
257 return -EINVAL;
258
259 *pfn = address >> PAGE_SHIFT;
260 return 0;
261}
262EXPORT_SYMBOL(follow_pfn);
263
1da177e4
LT
264DEFINE_RWLOCK(vmlist_lock);
265struct vm_struct *vmlist;
266
b3bdda02 267void vfree(const void *addr)
1da177e4
LT
268{
269 kfree(addr);
270}
b5073173 271EXPORT_SYMBOL(vfree);
1da177e4 272
dd0fc66f 273void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1da177e4
LT
274{
275 /*
8518609d
RD
276 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
277 * returns only a logical address.
1da177e4 278 */
84097518 279 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1da177e4 280}
b5073173 281EXPORT_SYMBOL(__vmalloc);
1da177e4 282
f905bc44
PM
283void *vmalloc_user(unsigned long size)
284{
285 void *ret;
286
287 ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
288 PAGE_KERNEL);
289 if (ret) {
290 struct vm_area_struct *vma;
291
292 down_write(&current->mm->mmap_sem);
293 vma = find_vma(current->mm, (unsigned long)ret);
294 if (vma)
295 vma->vm_flags |= VM_USERMAP;
296 up_write(&current->mm->mmap_sem);
297 }
298
299 return ret;
300}
301EXPORT_SYMBOL(vmalloc_user);
302
b3bdda02 303struct page *vmalloc_to_page(const void *addr)
1da177e4
LT
304{
305 return virt_to_page(addr);
306}
b5073173 307EXPORT_SYMBOL(vmalloc_to_page);
1da177e4 308
b3bdda02 309unsigned long vmalloc_to_pfn(const void *addr)
1da177e4
LT
310{
311 return page_to_pfn(virt_to_page(addr));
312}
b5073173 313EXPORT_SYMBOL(vmalloc_to_pfn);
1da177e4
LT
314
315long vread(char *buf, char *addr, unsigned long count)
316{
317 memcpy(buf, addr, count);
318 return count;
319}
320
321long vwrite(char *buf, char *addr, unsigned long count)
322{
323 /* Don't allow overflow */
324 if ((unsigned long) addr + count < count)
325 count = -(unsigned long) addr;
326
327 memcpy(addr, buf, count);
328 return(count);
329}
330
331/*
332 * vmalloc - allocate virtually continguos memory
333 *
334 * @size: allocation size
335 *
336 * Allocate enough pages to cover @size from the page level
337 * allocator and map them into continguos kernel virtual space.
338 *
c1c8897f 339 * For tight control over page level allocator and protection flags
1da177e4
LT
340 * use __vmalloc() instead.
341 */
342void *vmalloc(unsigned long size)
343{
344 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
345}
f6138882
AM
346EXPORT_SYMBOL(vmalloc);
347
348void *vmalloc_node(unsigned long size, int node)
349{
350 return vmalloc(size);
351}
352EXPORT_SYMBOL(vmalloc_node);
1da177e4 353
1af446ed
PM
354#ifndef PAGE_KERNEL_EXEC
355# define PAGE_KERNEL_EXEC PAGE_KERNEL
356#endif
357
358/**
359 * vmalloc_exec - allocate virtually contiguous, executable memory
360 * @size: allocation size
361 *
362 * Kernel-internal function to allocate enough pages to cover @size
363 * the page level allocator and map them into contiguous and
364 * executable kernel virtual space.
365 *
366 * For tight control over page level allocator and protection flags
367 * use __vmalloc() instead.
368 */
369
370void *vmalloc_exec(unsigned long size)
371{
372 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
373}
374
b5073173
PM
375/**
376 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1da177e4
LT
377 * @size: allocation size
378 *
379 * Allocate enough 32bit PA addressable pages to cover @size from the
380 * page level allocator and map them into continguos kernel virtual space.
381 */
382void *vmalloc_32(unsigned long size)
383{
384 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
385}
b5073173
PM
386EXPORT_SYMBOL(vmalloc_32);
387
388/**
389 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
390 * @size: allocation size
391 *
392 * The resulting memory area is 32bit addressable and zeroed so it can be
393 * mapped to userspace without leaking data.
f905bc44
PM
394 *
395 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
396 * remap_vmalloc_range() are permissible.
b5073173
PM
397 */
398void *vmalloc_32_user(unsigned long size)
399{
f905bc44
PM
400 /*
401 * We'll have to sort out the ZONE_DMA bits for 64-bit,
402 * but for now this can simply use vmalloc_user() directly.
403 */
404 return vmalloc_user(size);
b5073173
PM
405}
406EXPORT_SYMBOL(vmalloc_32_user);
1da177e4
LT
407
408void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
409{
410 BUG();
411 return NULL;
412}
b5073173 413EXPORT_SYMBOL(vmap);
1da177e4 414
b3bdda02 415void vunmap(const void *addr)
1da177e4
LT
416{
417 BUG();
418}
b5073173 419EXPORT_SYMBOL(vunmap);
1da177e4 420
eb6434d9
PM
421void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
422{
423 BUG();
424 return NULL;
425}
426EXPORT_SYMBOL(vm_map_ram);
427
428void vm_unmap_ram(const void *mem, unsigned int count)
429{
430 BUG();
431}
432EXPORT_SYMBOL(vm_unmap_ram);
433
434void vm_unmap_aliases(void)
435{
436}
437EXPORT_SYMBOL_GPL(vm_unmap_aliases);
438
1eeb66a1
CH
439/*
440 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
441 * have one.
442 */
443void __attribute__((weak)) vmalloc_sync_all(void)
444{
445}
446
b5073173
PM
447int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
448 struct page *page)
449{
450 return -EINVAL;
451}
452EXPORT_SYMBOL(vm_insert_page);
453
1da177e4
LT
454/*
455 * sys_brk() for the most part doesn't need the global kernel
456 * lock, except when an application is doing something nasty
457 * like trying to un-brk an area that has already been mapped
458 * to a regular file. in this case, the unmapping will need
459 * to invoke file system routines that need the global lock.
460 */
6a6160a7 461SYSCALL_DEFINE1(brk, unsigned long, brk)
1da177e4
LT
462{
463 struct mm_struct *mm = current->mm;
464
465 if (brk < mm->start_brk || brk > mm->context.end_brk)
466 return mm->brk;
467
468 if (mm->brk == brk)
469 return mm->brk;
470
471 /*
472 * Always allow shrinking brk
473 */
474 if (brk <= mm->brk) {
475 mm->brk = brk;
476 return brk;
477 }
478
479 /*
480 * Ok, looks good - let it rip.
481 */
482 return mm->brk = brk;
483}
484
8feae131
DH
485/*
486 * initialise the VMA and region record slabs
487 */
488void __init mmap_init(void)
1da177e4 489{
00a62ce9
KM
490 int ret;
491
492 ret = percpu_counter_init(&vm_committed_as, 0);
493 VM_BUG_ON(ret);
33e5d769 494 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
1da177e4 495}
1da177e4 496
3034097a 497/*
8feae131
DH
498 * validate the region tree
499 * - the caller must hold the region lock
3034097a 500 */
8feae131
DH
501#ifdef CONFIG_DEBUG_NOMMU_REGIONS
502static noinline void validate_nommu_regions(void)
3034097a 503{
8feae131
DH
504 struct vm_region *region, *last;
505 struct rb_node *p, *lastp;
3034097a 506
8feae131
DH
507 lastp = rb_first(&nommu_region_tree);
508 if (!lastp)
509 return;
510
511 last = rb_entry(lastp, struct vm_region, vm_rb);
33e5d769
DH
512 BUG_ON(unlikely(last->vm_end <= last->vm_start));
513 BUG_ON(unlikely(last->vm_top < last->vm_end));
8feae131
DH
514
515 while ((p = rb_next(lastp))) {
516 region = rb_entry(p, struct vm_region, vm_rb);
517 last = rb_entry(lastp, struct vm_region, vm_rb);
518
33e5d769
DH
519 BUG_ON(unlikely(region->vm_end <= region->vm_start));
520 BUG_ON(unlikely(region->vm_top < region->vm_end));
521 BUG_ON(unlikely(region->vm_start < last->vm_top));
3034097a 522
8feae131
DH
523 lastp = p;
524 }
3034097a 525}
8feae131 526#else
33e5d769
DH
527static void validate_nommu_regions(void)
528{
529}
8feae131 530#endif
3034097a
DH
531
532/*
8feae131 533 * add a region into the global tree
3034097a 534 */
8feae131 535static void add_nommu_region(struct vm_region *region)
3034097a 536{
8feae131
DH
537 struct vm_region *pregion;
538 struct rb_node **p, *parent;
3034097a 539
8feae131
DH
540 validate_nommu_regions();
541
8feae131
DH
542 parent = NULL;
543 p = &nommu_region_tree.rb_node;
544 while (*p) {
545 parent = *p;
546 pregion = rb_entry(parent, struct vm_region, vm_rb);
547 if (region->vm_start < pregion->vm_start)
548 p = &(*p)->rb_left;
549 else if (region->vm_start > pregion->vm_start)
550 p = &(*p)->rb_right;
551 else if (pregion == region)
552 return;
553 else
554 BUG();
3034097a
DH
555 }
556
8feae131
DH
557 rb_link_node(&region->vm_rb, parent, p);
558 rb_insert_color(&region->vm_rb, &nommu_region_tree);
3034097a 559
8feae131 560 validate_nommu_regions();
3034097a 561}
3034097a 562
930e652a 563/*
8feae131 564 * delete a region from the global tree
930e652a 565 */
8feae131 566static void delete_nommu_region(struct vm_region *region)
930e652a 567{
8feae131 568 BUG_ON(!nommu_region_tree.rb_node);
930e652a 569
8feae131
DH
570 validate_nommu_regions();
571 rb_erase(&region->vm_rb, &nommu_region_tree);
572 validate_nommu_regions();
57c8f63e
GU
573}
574
6fa5f80b 575/*
8feae131 576 * free a contiguous series of pages
6fa5f80b 577 */
8feae131 578static void free_page_series(unsigned long from, unsigned long to)
6fa5f80b 579{
8feae131
DH
580 for (; from < to; from += PAGE_SIZE) {
581 struct page *page = virt_to_page(from);
582
583 kdebug("- free %lx", from);
33e5d769 584 atomic_long_dec(&mmap_pages_allocated);
8feae131 585 if (page_count(page) != 1)
33e5d769
DH
586 kdebug("free page %p: refcount not one: %d",
587 page, page_count(page));
8feae131 588 put_page(page);
6fa5f80b 589 }
6fa5f80b
DH
590}
591
3034097a 592/*
8feae131 593 * release a reference to a region
33e5d769 594 * - the caller must hold the region semaphore for writing, which this releases
dd8632a1 595 * - the region may not have been added to the tree yet, in which case vm_top
8feae131 596 * will equal vm_start
3034097a 597 */
8feae131
DH
598static void __put_nommu_region(struct vm_region *region)
599 __releases(nommu_region_sem)
1da177e4 600{
8feae131 601 kenter("%p{%d}", region, atomic_read(&region->vm_usage));
1da177e4 602
8feae131 603 BUG_ON(!nommu_region_tree.rb_node);
1da177e4 604
8feae131 605 if (atomic_dec_and_test(&region->vm_usage)) {
dd8632a1 606 if (region->vm_top > region->vm_start)
8feae131
DH
607 delete_nommu_region(region);
608 up_write(&nommu_region_sem);
609
610 if (region->vm_file)
611 fput(region->vm_file);
612
613 /* IO memory and memory shared directly out of the pagecache
614 * from ramfs/tmpfs mustn't be released here */
615 if (region->vm_flags & VM_MAPPED_COPY) {
616 kdebug("free series");
dd8632a1 617 free_page_series(region->vm_start, region->vm_top);
8feae131
DH
618 }
619 kmem_cache_free(vm_region_jar, region);
620 } else {
621 up_write(&nommu_region_sem);
1da177e4 622 }
8feae131 623}
1da177e4 624
8feae131
DH
625/*
626 * release a reference to a region
627 */
628static void put_nommu_region(struct vm_region *region)
629{
630 down_write(&nommu_region_sem);
631 __put_nommu_region(region);
1da177e4
LT
632}
633
3034097a 634/*
8feae131
DH
635 * add a VMA into a process's mm_struct in the appropriate place in the list
636 * and tree and add to the address space's page tree also if not an anonymous
637 * page
638 * - should be called with mm->mmap_sem held writelocked
3034097a 639 */
8feae131 640static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 641{
8feae131 642 struct vm_area_struct *pvma, **pp;
1da177e4 643 struct address_space *mapping;
8feae131
DH
644 struct rb_node **p, *parent;
645
646 kenter(",%p", vma);
647
648 BUG_ON(!vma->vm_region);
649
650 mm->map_count++;
651 vma->vm_mm = mm;
1da177e4
LT
652
653 /* add the VMA to the mapping */
654 if (vma->vm_file) {
655 mapping = vma->vm_file->f_mapping;
656
657 flush_dcache_mmap_lock(mapping);
658 vma_prio_tree_insert(vma, &mapping->i_mmap);
659 flush_dcache_mmap_unlock(mapping);
660 }
661
8feae131
DH
662 /* add the VMA to the tree */
663 parent = NULL;
664 p = &mm->mm_rb.rb_node;
1da177e4
LT
665 while (*p) {
666 parent = *p;
667 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
668
8feae131
DH
669 /* sort by: start addr, end addr, VMA struct addr in that order
670 * (the latter is necessary as we may get identical VMAs) */
671 if (vma->vm_start < pvma->vm_start)
1da177e4 672 p = &(*p)->rb_left;
8feae131 673 else if (vma->vm_start > pvma->vm_start)
1da177e4 674 p = &(*p)->rb_right;
8feae131
DH
675 else if (vma->vm_end < pvma->vm_end)
676 p = &(*p)->rb_left;
677 else if (vma->vm_end > pvma->vm_end)
678 p = &(*p)->rb_right;
679 else if (vma < pvma)
680 p = &(*p)->rb_left;
681 else if (vma > pvma)
682 p = &(*p)->rb_right;
683 else
684 BUG();
1da177e4
LT
685 }
686
687 rb_link_node(&vma->vm_rb, parent, p);
8feae131
DH
688 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
689
690 /* add VMA to the VMA list also */
691 for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
692 if (pvma->vm_start > vma->vm_start)
693 break;
694 if (pvma->vm_start < vma->vm_start)
695 continue;
696 if (pvma->vm_end < vma->vm_end)
697 break;
698 }
699
700 vma->vm_next = *pp;
701 *pp = vma;
1da177e4
LT
702}
703
3034097a 704/*
8feae131 705 * delete a VMA from its owning mm_struct and address space
3034097a 706 */
8feae131 707static void delete_vma_from_mm(struct vm_area_struct *vma)
1da177e4 708{
8feae131 709 struct vm_area_struct **pp;
1da177e4 710 struct address_space *mapping;
8feae131
DH
711 struct mm_struct *mm = vma->vm_mm;
712
713 kenter("%p", vma);
714
715 mm->map_count--;
716 if (mm->mmap_cache == vma)
717 mm->mmap_cache = NULL;
1da177e4
LT
718
719 /* remove the VMA from the mapping */
720 if (vma->vm_file) {
721 mapping = vma->vm_file->f_mapping;
722
723 flush_dcache_mmap_lock(mapping);
724 vma_prio_tree_remove(vma, &mapping->i_mmap);
725 flush_dcache_mmap_unlock(mapping);
726 }
727
8feae131
DH
728 /* remove from the MM's tree and list */
729 rb_erase(&vma->vm_rb, &mm->mm_rb);
730 for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
731 if (*pp == vma) {
732 *pp = vma->vm_next;
733 break;
734 }
735 }
736
737 vma->vm_mm = NULL;
738}
739
740/*
741 * destroy a VMA record
742 */
743static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
744{
745 kenter("%p", vma);
746 if (vma->vm_ops && vma->vm_ops->close)
747 vma->vm_ops->close(vma);
748 if (vma->vm_file) {
749 fput(vma->vm_file);
750 if (vma->vm_flags & VM_EXECUTABLE)
751 removed_exe_file_vma(mm);
752 }
753 put_nommu_region(vma->vm_region);
754 kmem_cache_free(vm_area_cachep, vma);
755}
756
757/*
758 * look up the first VMA in which addr resides, NULL if none
759 * - should be called with mm->mmap_sem at least held readlocked
760 */
761struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
762{
763 struct vm_area_struct *vma;
764 struct rb_node *n = mm->mm_rb.rb_node;
765
766 /* check the cache first */
767 vma = mm->mmap_cache;
768 if (vma && vma->vm_start <= addr && vma->vm_end > addr)
769 return vma;
770
771 /* trawl the tree (there may be multiple mappings in which addr
772 * resides) */
773 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
774 vma = rb_entry(n, struct vm_area_struct, vm_rb);
775 if (vma->vm_start > addr)
776 return NULL;
777 if (vma->vm_end > addr) {
778 mm->mmap_cache = vma;
779 return vma;
780 }
781 }
782
783 return NULL;
784}
785EXPORT_SYMBOL(find_vma);
786
787/*
788 * find a VMA
789 * - we don't extend stack VMAs under NOMMU conditions
790 */
791struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
792{
793 return find_vma(mm, addr);
794}
795
796/*
797 * expand a stack to a given address
798 * - not supported under NOMMU conditions
799 */
800int expand_stack(struct vm_area_struct *vma, unsigned long address)
801{
802 return -ENOMEM;
803}
804
805/*
806 * look up the first VMA exactly that exactly matches addr
807 * - should be called with mm->mmap_sem at least held readlocked
808 */
809static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
810 unsigned long addr,
811 unsigned long len)
812{
813 struct vm_area_struct *vma;
814 struct rb_node *n = mm->mm_rb.rb_node;
815 unsigned long end = addr + len;
816
817 /* check the cache first */
818 vma = mm->mmap_cache;
819 if (vma && vma->vm_start == addr && vma->vm_end == end)
820 return vma;
821
822 /* trawl the tree (there may be multiple mappings in which addr
823 * resides) */
824 for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
825 vma = rb_entry(n, struct vm_area_struct, vm_rb);
826 if (vma->vm_start < addr)
827 continue;
828 if (vma->vm_start > addr)
829 return NULL;
830 if (vma->vm_end == end) {
831 mm->mmap_cache = vma;
832 return vma;
833 }
834 }
835
836 return NULL;
1da177e4
LT
837}
838
839/*
840 * determine whether a mapping should be permitted and, if so, what sort of
841 * mapping we're capable of supporting
842 */
843static int validate_mmap_request(struct file *file,
844 unsigned long addr,
845 unsigned long len,
846 unsigned long prot,
847 unsigned long flags,
848 unsigned long pgoff,
849 unsigned long *_capabilities)
850{
8feae131 851 unsigned long capabilities, rlen;
1da177e4
LT
852 unsigned long reqprot = prot;
853 int ret;
854
855 /* do the simple checks first */
856 if (flags & MAP_FIXED || addr) {
857 printk(KERN_DEBUG
858 "%d: Can't do fixed-address/overlay mmap of RAM\n",
859 current->pid);
860 return -EINVAL;
861 }
862
863 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
864 (flags & MAP_TYPE) != MAP_SHARED)
865 return -EINVAL;
866
f81cff0d 867 if (!len)
1da177e4
LT
868 return -EINVAL;
869
f81cff0d 870 /* Careful about overflows.. */
8feae131
DH
871 rlen = PAGE_ALIGN(len);
872 if (!rlen || rlen > TASK_SIZE)
f81cff0d
MF
873 return -ENOMEM;
874
1da177e4 875 /* offset overflow? */
8feae131 876 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
f81cff0d 877 return -EOVERFLOW;
1da177e4
LT
878
879 if (file) {
880 /* validate file mapping requests */
881 struct address_space *mapping;
882
883 /* files must support mmap */
884 if (!file->f_op || !file->f_op->mmap)
885 return -ENODEV;
886
887 /* work out if what we've got could possibly be shared
888 * - we support chardevs that provide their own "memory"
889 * - we support files/blockdevs that are memory backed
890 */
891 mapping = file->f_mapping;
892 if (!mapping)
e9536ae7 893 mapping = file->f_path.dentry->d_inode->i_mapping;
1da177e4
LT
894
895 capabilities = 0;
896 if (mapping && mapping->backing_dev_info)
897 capabilities = mapping->backing_dev_info->capabilities;
898
899 if (!capabilities) {
900 /* no explicit capabilities set, so assume some
901 * defaults */
e9536ae7 902 switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
1da177e4
LT
903 case S_IFREG:
904 case S_IFBLK:
905 capabilities = BDI_CAP_MAP_COPY;
906 break;
907
908 case S_IFCHR:
909 capabilities =
910 BDI_CAP_MAP_DIRECT |
911 BDI_CAP_READ_MAP |
912 BDI_CAP_WRITE_MAP;
913 break;
914
915 default:
916 return -EINVAL;
917 }
918 }
919
920 /* eliminate any capabilities that we can't support on this
921 * device */
922 if (!file->f_op->get_unmapped_area)
923 capabilities &= ~BDI_CAP_MAP_DIRECT;
924 if (!file->f_op->read)
925 capabilities &= ~BDI_CAP_MAP_COPY;
926
927 if (flags & MAP_SHARED) {
928 /* do checks for writing, appending and locking */
929 if ((prot & PROT_WRITE) &&
930 !(file->f_mode & FMODE_WRITE))
931 return -EACCES;
932
e9536ae7 933 if (IS_APPEND(file->f_path.dentry->d_inode) &&
1da177e4
LT
934 (file->f_mode & FMODE_WRITE))
935 return -EACCES;
936
e9536ae7 937 if (locks_verify_locked(file->f_path.dentry->d_inode))
1da177e4
LT
938 return -EAGAIN;
939
940 if (!(capabilities & BDI_CAP_MAP_DIRECT))
941 return -ENODEV;
942
943 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
944 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
945 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
946 ) {
947 printk("MAP_SHARED not completely supported on !MMU\n");
948 return -EINVAL;
949 }
950
951 /* we mustn't privatise shared mappings */
952 capabilities &= ~BDI_CAP_MAP_COPY;
953 }
954 else {
955 /* we're going to read the file into private memory we
956 * allocate */
957 if (!(capabilities & BDI_CAP_MAP_COPY))
958 return -ENODEV;
959
960 /* we don't permit a private writable mapping to be
961 * shared with the backing device */
962 if (prot & PROT_WRITE)
963 capabilities &= ~BDI_CAP_MAP_DIRECT;
964 }
965
966 /* handle executable mappings and implied executable
967 * mappings */
e9536ae7 968 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
1da177e4
LT
969 if (prot & PROT_EXEC)
970 return -EPERM;
971 }
972 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
973 /* handle implication of PROT_EXEC by PROT_READ */
974 if (current->personality & READ_IMPLIES_EXEC) {
975 if (capabilities & BDI_CAP_EXEC_MAP)
976 prot |= PROT_EXEC;
977 }
978 }
979 else if ((prot & PROT_READ) &&
980 (prot & PROT_EXEC) &&
981 !(capabilities & BDI_CAP_EXEC_MAP)
982 ) {
983 /* backing file is not executable, try to copy */
984 capabilities &= ~BDI_CAP_MAP_DIRECT;
985 }
986 }
987 else {
988 /* anonymous mappings are always memory backed and can be
989 * privately mapped
990 */
991 capabilities = BDI_CAP_MAP_COPY;
992
993 /* handle PROT_EXEC implication by PROT_READ */
994 if ((prot & PROT_READ) &&
995 (current->personality & READ_IMPLIES_EXEC))
996 prot |= PROT_EXEC;
997 }
998
999 /* allow the security API to have its say */
ed032189 1000 ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1da177e4
LT
1001 if (ret < 0)
1002 return ret;
1003
1004 /* looks okay */
1005 *_capabilities = capabilities;
1006 return 0;
1007}
1008
1009/*
1010 * we've determined that we can make the mapping, now translate what we
1011 * now know into VMA flags
1012 */
1013static unsigned long determine_vm_flags(struct file *file,
1014 unsigned long prot,
1015 unsigned long flags,
1016 unsigned long capabilities)
1017{
1018 unsigned long vm_flags;
1019
1020 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1021 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1022 /* vm_flags |= mm->def_flags; */
1023
1024 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1025 /* attempt to share read-only copies of mapped file chunks */
1026 if (file && !(prot & PROT_WRITE))
1027 vm_flags |= VM_MAYSHARE;
1028 }
1029 else {
1030 /* overlay a shareable mapping on the backing device or inode
1031 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
1032 * romfs/cramfs */
1033 if (flags & MAP_SHARED)
1034 vm_flags |= VM_MAYSHARE | VM_SHARED;
1035 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
1036 vm_flags |= VM_MAYSHARE;
1037 }
1038
1039 /* refuse to let anyone share private mappings with this process if
1040 * it's being traced - otherwise breakpoints set in it may interfere
1041 * with another untraced process
1042 */
fa8e26cc 1043 if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
1da177e4
LT
1044 vm_flags &= ~VM_MAYSHARE;
1045
1046 return vm_flags;
1047}
1048
1049/*
8feae131
DH
1050 * set up a shared mapping on a file (the driver or filesystem provides and
1051 * pins the storage)
1da177e4 1052 */
8feae131 1053static int do_mmap_shared_file(struct vm_area_struct *vma)
1da177e4
LT
1054{
1055 int ret;
1056
1057 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
dd8632a1
PM
1058 if (ret == 0) {
1059 vma->vm_region->vm_top = vma->vm_region->vm_end;
1060 return ret;
1061 }
1da177e4
LT
1062 if (ret != -ENOSYS)
1063 return ret;
1064
1065 /* getting an ENOSYS error indicates that direct mmap isn't
1066 * possible (as opposed to tried but failed) so we'll fall
1067 * through to making a private copy of the data and mapping
1068 * that if we can */
1069 return -ENODEV;
1070}
1071
1072/*
1073 * set up a private mapping or an anonymous shared mapping
1074 */
8feae131
DH
1075static int do_mmap_private(struct vm_area_struct *vma,
1076 struct vm_region *region,
1077 unsigned long len)
1da177e4 1078{
8feae131
DH
1079 struct page *pages;
1080 unsigned long total, point, n, rlen;
1da177e4 1081 void *base;
8feae131 1082 int ret, order;
1da177e4
LT
1083
1084 /* invoke the file's mapping function so that it can keep track of
1085 * shared mappings on devices or memory
1086 * - VM_MAYSHARE will be set if it may attempt to share
1087 */
1088 if (vma->vm_file) {
1089 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
dd8632a1 1090 if (ret == 0) {
1da177e4 1091 /* shouldn't return success if we're not sharing */
dd8632a1
PM
1092 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
1093 vma->vm_region->vm_top = vma->vm_region->vm_end;
1094 return ret;
1da177e4 1095 }
dd8632a1
PM
1096 if (ret != -ENOSYS)
1097 return ret;
1da177e4
LT
1098
1099 /* getting an ENOSYS error indicates that direct mmap isn't
1100 * possible (as opposed to tried but failed) so we'll try to
1101 * make a private copy of the data and map that instead */
1102 }
1103
8feae131
DH
1104 rlen = PAGE_ALIGN(len);
1105
1da177e4
LT
1106 /* allocate some memory to hold the mapping
1107 * - note that this may not return a page-aligned address if the object
1108 * we're allocating is smaller than a page
1109 */
8feae131
DH
1110 order = get_order(rlen);
1111 kdebug("alloc order %d for %lx", order, len);
1112
1113 pages = alloc_pages(GFP_KERNEL, order);
1114 if (!pages)
1da177e4
LT
1115 goto enomem;
1116
8feae131 1117 total = 1 << order;
33e5d769 1118 atomic_long_add(total, &mmap_pages_allocated);
8feae131
DH
1119
1120 point = rlen >> PAGE_SHIFT;
dd8632a1
PM
1121
1122 /* we allocated a power-of-2 sized page set, so we may want to trim off
1123 * the excess */
1124 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
1125 while (total > point) {
1126 order = ilog2(total - point);
1127 n = 1 << order;
1128 kdebug("shave %lu/%lu @%lu", n, total - point, total);
33e5d769 1129 atomic_long_sub(n, &mmap_pages_allocated);
dd8632a1
PM
1130 total -= n;
1131 set_page_refcounted(pages + total);
1132 __free_pages(pages + total, order);
1133 }
8feae131
DH
1134 }
1135
8feae131
DH
1136 for (point = 1; point < total; point++)
1137 set_page_refcounted(&pages[point]);
1da177e4 1138
8feae131
DH
1139 base = page_address(pages);
1140 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1141 region->vm_start = (unsigned long) base;
1142 region->vm_end = region->vm_start + rlen;
dd8632a1 1143 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
8feae131
DH
1144
1145 vma->vm_start = region->vm_start;
1146 vma->vm_end = region->vm_start + len;
1da177e4
LT
1147
1148 if (vma->vm_file) {
1149 /* read the contents of a file into the copy */
1150 mm_segment_t old_fs;
1151 loff_t fpos;
1152
1153 fpos = vma->vm_pgoff;
1154 fpos <<= PAGE_SHIFT;
1155
1156 old_fs = get_fs();
1157 set_fs(KERNEL_DS);
8feae131 1158 ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
1da177e4
LT
1159 set_fs(old_fs);
1160
1161 if (ret < 0)
1162 goto error_free;
1163
1164 /* clear the last little bit */
8feae131
DH
1165 if (ret < rlen)
1166 memset(base + ret, 0, rlen - ret);
1da177e4
LT
1167
1168 } else {
1169 /* if it's an anonymous mapping, then just clear it */
8feae131 1170 memset(base, 0, rlen);
1da177e4
LT
1171 }
1172
1173 return 0;
1174
1175error_free:
8feae131
DH
1176 free_page_series(region->vm_start, region->vm_end);
1177 region->vm_start = vma->vm_start = 0;
1178 region->vm_end = vma->vm_end = 0;
dd8632a1 1179 region->vm_top = 0;
1da177e4
LT
1180 return ret;
1181
1182enomem:
05ae6fa3
GU
1183 printk("Allocation of length %lu from process %d (%s) failed\n",
1184 len, current->pid, current->comm);
1da177e4
LT
1185 show_free_areas();
1186 return -ENOMEM;
1187}
1188
1189/*
1190 * handle mapping creation for uClinux
1191 */
1192unsigned long do_mmap_pgoff(struct file *file,
1193 unsigned long addr,
1194 unsigned long len,
1195 unsigned long prot,
1196 unsigned long flags,
1197 unsigned long pgoff)
1198{
8feae131
DH
1199 struct vm_area_struct *vma;
1200 struct vm_region *region;
1da177e4 1201 struct rb_node *rb;
8feae131 1202 unsigned long capabilities, vm_flags, result;
1da177e4
LT
1203 int ret;
1204
8feae131
DH
1205 kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1206
7cd94146
EP
1207 if (!(flags & MAP_FIXED))
1208 addr = round_hint_to_min(addr);
1209
1da177e4
LT
1210 /* decide whether we should attempt the mapping, and if so what sort of
1211 * mapping */
1212 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1213 &capabilities);
8feae131
DH
1214 if (ret < 0) {
1215 kleave(" = %d [val]", ret);
1da177e4 1216 return ret;
8feae131 1217 }
1da177e4
LT
1218
1219 /* we've determined that we can make the mapping, now translate what we
1220 * now know into VMA flags */
1221 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1222
8feae131
DH
1223 /* we're going to need to record the mapping */
1224 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1225 if (!region)
1226 goto error_getting_region;
1227
1228 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
1229 if (!vma)
1230 goto error_getting_vma;
1da177e4 1231
8feae131
DH
1232 atomic_set(&region->vm_usage, 1);
1233 region->vm_flags = vm_flags;
1234 region->vm_pgoff = pgoff;
1235
1236 INIT_LIST_HEAD(&vma->anon_vma_node);
1237 vma->vm_flags = vm_flags;
1238 vma->vm_pgoff = pgoff;
1da177e4 1239
8feae131
DH
1240 if (file) {
1241 region->vm_file = file;
1242 get_file(file);
1243 vma->vm_file = file;
1244 get_file(file);
1245 if (vm_flags & VM_EXECUTABLE) {
1246 added_exe_file_vma(current->mm);
1247 vma->vm_mm = current->mm;
1248 }
1249 }
1250
1251 down_write(&nommu_region_sem);
1252
1253 /* if we want to share, we need to check for regions created by other
1da177e4 1254 * mmap() calls that overlap with our proposed mapping
8feae131 1255 * - we can only share with a superset match on most regular files
1da177e4
LT
1256 * - shared mappings on character devices and memory backed files are
1257 * permitted to overlap inexactly as far as we are concerned for in
1258 * these cases, sharing is handled in the driver or filesystem rather
1259 * than here
1260 */
1261 if (vm_flags & VM_MAYSHARE) {
8feae131
DH
1262 struct vm_region *pregion;
1263 unsigned long pglen, rpglen, pgend, rpgend, start;
1da177e4 1264
8feae131
DH
1265 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1266 pgend = pgoff + pglen;
165b2392 1267
8feae131
DH
1268 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1269 pregion = rb_entry(rb, struct vm_region, vm_rb);
1da177e4 1270
8feae131 1271 if (!(pregion->vm_flags & VM_MAYSHARE))
1da177e4
LT
1272 continue;
1273
1274 /* search for overlapping mappings on the same file */
8feae131
DH
1275 if (pregion->vm_file->f_path.dentry->d_inode !=
1276 file->f_path.dentry->d_inode)
1da177e4
LT
1277 continue;
1278
8feae131 1279 if (pregion->vm_pgoff >= pgend)
1da177e4
LT
1280 continue;
1281
8feae131
DH
1282 rpglen = pregion->vm_end - pregion->vm_start;
1283 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1284 rpgend = pregion->vm_pgoff + rpglen;
1285 if (pgoff >= rpgend)
1da177e4
LT
1286 continue;
1287
8feae131
DH
1288 /* handle inexactly overlapping matches between
1289 * mappings */
1290 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1291 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1292 /* new mapping is not a subset of the region */
1da177e4
LT
1293 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1294 goto sharing_violation;
1295 continue;
1296 }
1297
8feae131
DH
1298 /* we've found a region we can share */
1299 atomic_inc(&pregion->vm_usage);
1300 vma->vm_region = pregion;
1301 start = pregion->vm_start;
1302 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1303 vma->vm_start = start;
1304 vma->vm_end = start + len;
1305
1306 if (pregion->vm_flags & VM_MAPPED_COPY) {
1307 kdebug("share copy");
1308 vma->vm_flags |= VM_MAPPED_COPY;
1309 } else {
1310 kdebug("share mmap");
1311 ret = do_mmap_shared_file(vma);
1312 if (ret < 0) {
1313 vma->vm_region = NULL;
1314 vma->vm_start = 0;
1315 vma->vm_end = 0;
1316 atomic_dec(&pregion->vm_usage);
1317 pregion = NULL;
1318 goto error_just_free;
1319 }
1320 }
1321 fput(region->vm_file);
1322 kmem_cache_free(vm_region_jar, region);
1323 region = pregion;
1324 result = start;
1325 goto share;
1da177e4
LT
1326 }
1327
1da177e4
LT
1328 /* obtain the address at which to make a shared mapping
1329 * - this is the hook for quasi-memory character devices to
1330 * tell us the location of a shared mapping
1331 */
1332 if (file && file->f_op->get_unmapped_area) {
1333 addr = file->f_op->get_unmapped_area(file, addr, len,
1334 pgoff, flags);
1335 if (IS_ERR((void *) addr)) {
1336 ret = addr;
1337 if (ret != (unsigned long) -ENOSYS)
8feae131 1338 goto error_just_free;
1da177e4
LT
1339
1340 /* the driver refused to tell us where to site
1341 * the mapping so we'll have to attempt to copy
1342 * it */
1343 ret = (unsigned long) -ENODEV;
1344 if (!(capabilities & BDI_CAP_MAP_COPY))
8feae131 1345 goto error_just_free;
1da177e4
LT
1346
1347 capabilities &= ~BDI_CAP_MAP_DIRECT;
8feae131
DH
1348 } else {
1349 vma->vm_start = region->vm_start = addr;
1350 vma->vm_end = region->vm_end = addr + len;
1da177e4
LT
1351 }
1352 }
1353 }
1354
8feae131 1355 vma->vm_region = region;
1da177e4
LT
1356
1357 /* set up the mapping */
1358 if (file && vma->vm_flags & VM_SHARED)
8feae131 1359 ret = do_mmap_shared_file(vma);
1da177e4 1360 else
8feae131 1361 ret = do_mmap_private(vma, region, len);
1da177e4 1362 if (ret < 0)
8feae131
DH
1363 goto error_put_region;
1364
1365 add_nommu_region(region);
1da177e4
LT
1366
1367 /* okay... we have a mapping; now we have to register it */
8feae131 1368 result = vma->vm_start;
1da177e4 1369
1da177e4
LT
1370 current->mm->total_vm += len >> PAGE_SHIFT;
1371
8feae131
DH
1372share:
1373 add_vma_to_mm(current->mm, vma);
1da177e4 1374
8feae131 1375 up_write(&nommu_region_sem);
1da177e4
LT
1376
1377 if (prot & PROT_EXEC)
8feae131 1378 flush_icache_range(result, result + len);
1da177e4 1379
8feae131
DH
1380 kleave(" = %lx", result);
1381 return result;
1da177e4 1382
8feae131
DH
1383error_put_region:
1384 __put_nommu_region(region);
1da177e4 1385 if (vma) {
925d1c40 1386 if (vma->vm_file) {
3fcd03e0 1387 fput(vma->vm_file);
925d1c40
MH
1388 if (vma->vm_flags & VM_EXECUTABLE)
1389 removed_exe_file_vma(vma->vm_mm);
1390 }
8feae131 1391 kmem_cache_free(vm_area_cachep, vma);
1da177e4 1392 }
8feae131 1393 kleave(" = %d [pr]", ret);
1da177e4
LT
1394 return ret;
1395
8feae131
DH
1396error_just_free:
1397 up_write(&nommu_region_sem);
1398error:
1399 fput(region->vm_file);
1400 kmem_cache_free(vm_region_jar, region);
1401 fput(vma->vm_file);
1402 if (vma->vm_flags & VM_EXECUTABLE)
1403 removed_exe_file_vma(vma->vm_mm);
1404 kmem_cache_free(vm_area_cachep, vma);
1405 kleave(" = %d", ret);
1406 return ret;
1407
1408sharing_violation:
1409 up_write(&nommu_region_sem);
1410 printk(KERN_WARNING "Attempt to share mismatched mappings\n");
1411 ret = -EINVAL;
1412 goto error;
1da177e4 1413
8feae131
DH
1414error_getting_vma:
1415 kmem_cache_free(vm_region_jar, region);
1416 printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
1417 " from process %d failed\n",
1da177e4
LT
1418 len, current->pid);
1419 show_free_areas();
1420 return -ENOMEM;
1421
8feae131
DH
1422error_getting_region:
1423 printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
1424 " from process %d failed\n",
1da177e4
LT
1425 len, current->pid);
1426 show_free_areas();
1427 return -ENOMEM;
1428}
b5073173 1429EXPORT_SYMBOL(do_mmap_pgoff);
1da177e4
LT
1430
1431/*
8feae131
DH
1432 * split a vma into two pieces at address 'addr', a new vma is allocated either
1433 * for the first part or the tail.
1da177e4 1434 */
8feae131
DH
1435int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1436 unsigned long addr, int new_below)
1da177e4 1437{
8feae131
DH
1438 struct vm_area_struct *new;
1439 struct vm_region *region;
1440 unsigned long npages;
1da177e4 1441
8feae131 1442 kenter("");
1da177e4 1443
8feae131
DH
1444 /* we're only permitted to split anonymous regions that have a single
1445 * owner */
1446 if (vma->vm_file ||
1447 atomic_read(&vma->vm_region->vm_usage) != 1)
1448 return -ENOMEM;
1da177e4 1449
8feae131
DH
1450 if (mm->map_count >= sysctl_max_map_count)
1451 return -ENOMEM;
1da177e4 1452
8feae131
DH
1453 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1454 if (!region)
1455 return -ENOMEM;
1da177e4 1456
8feae131
DH
1457 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
1458 if (!new) {
1459 kmem_cache_free(vm_region_jar, region);
1460 return -ENOMEM;
1461 }
1462
1463 /* most fields are the same, copy all, and then fixup */
1464 *new = *vma;
1465 *region = *vma->vm_region;
1466 new->vm_region = region;
1467
1468 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1469
1470 if (new_below) {
dd8632a1 1471 region->vm_top = region->vm_end = new->vm_end = addr;
8feae131
DH
1472 } else {
1473 region->vm_start = new->vm_start = addr;
1474 region->vm_pgoff = new->vm_pgoff += npages;
1da177e4 1475 }
8feae131
DH
1476
1477 if (new->vm_ops && new->vm_ops->open)
1478 new->vm_ops->open(new);
1479
1480 delete_vma_from_mm(vma);
1481 down_write(&nommu_region_sem);
1482 delete_nommu_region(vma->vm_region);
1483 if (new_below) {
1484 vma->vm_region->vm_start = vma->vm_start = addr;
1485 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1486 } else {
1487 vma->vm_region->vm_end = vma->vm_end = addr;
dd8632a1 1488 vma->vm_region->vm_top = addr;
8feae131
DH
1489 }
1490 add_nommu_region(vma->vm_region);
1491 add_nommu_region(new->vm_region);
1492 up_write(&nommu_region_sem);
1493 add_vma_to_mm(mm, vma);
1494 add_vma_to_mm(mm, new);
1495 return 0;
1da177e4
LT
1496}
1497
3034097a 1498/*
8feae131
DH
1499 * shrink a VMA by removing the specified chunk from either the beginning or
1500 * the end
3034097a 1501 */
8feae131
DH
1502static int shrink_vma(struct mm_struct *mm,
1503 struct vm_area_struct *vma,
1504 unsigned long from, unsigned long to)
1da177e4 1505{
8feae131 1506 struct vm_region *region;
1da177e4 1507
8feae131 1508 kenter("");
1da177e4 1509
8feae131
DH
1510 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1511 * and list */
1512 delete_vma_from_mm(vma);
1513 if (from > vma->vm_start)
1514 vma->vm_end = from;
1515 else
1516 vma->vm_start = to;
1517 add_vma_to_mm(mm, vma);
1da177e4 1518
8feae131
DH
1519 /* cut the backing region down to size */
1520 region = vma->vm_region;
1521 BUG_ON(atomic_read(&region->vm_usage) != 1);
1522
1523 down_write(&nommu_region_sem);
1524 delete_nommu_region(region);
dd8632a1
PM
1525 if (from > region->vm_start) {
1526 to = region->vm_top;
1527 region->vm_top = region->vm_end = from;
1528 } else {
8feae131 1529 region->vm_start = to;
dd8632a1 1530 }
8feae131
DH
1531 add_nommu_region(region);
1532 up_write(&nommu_region_sem);
1533
1534 free_page_series(from, to);
1535 return 0;
1536}
1da177e4 1537
8feae131
DH
1538/*
1539 * release a mapping
1540 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1541 * VMA, though it need not cover the whole VMA
1542 */
1543int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1544{
1545 struct vm_area_struct *vma;
1546 struct rb_node *rb;
1547 unsigned long end = start + len;
1548 int ret;
1da177e4 1549
8feae131 1550 kenter(",%lx,%zx", start, len);
1da177e4 1551
8feae131
DH
1552 if (len == 0)
1553 return -EINVAL;
365e9c87 1554
8feae131
DH
1555 /* find the first potentially overlapping VMA */
1556 vma = find_vma(mm, start);
1557 if (!vma) {
33e5d769
DH
1558 static int limit = 0;
1559 if (limit < 5) {
1560 printk(KERN_WARNING
1561 "munmap of memory not mmapped by process %d"
1562 " (%s): 0x%lx-0x%lx\n",
1563 current->pid, current->comm,
1564 start, start + len - 1);
1565 limit++;
1566 }
8feae131
DH
1567 return -EINVAL;
1568 }
1da177e4 1569
8feae131
DH
1570 /* we're allowed to split an anonymous VMA but not a file-backed one */
1571 if (vma->vm_file) {
1572 do {
1573 if (start > vma->vm_start) {
1574 kleave(" = -EINVAL [miss]");
1575 return -EINVAL;
1576 }
1577 if (end == vma->vm_end)
1578 goto erase_whole_vma;
1579 rb = rb_next(&vma->vm_rb);
1580 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
1581 } while (rb);
1582 kleave(" = -EINVAL [split file]");
1583 return -EINVAL;
1584 } else {
1585 /* the chunk must be a subset of the VMA found */
1586 if (start == vma->vm_start && end == vma->vm_end)
1587 goto erase_whole_vma;
1588 if (start < vma->vm_start || end > vma->vm_end) {
1589 kleave(" = -EINVAL [superset]");
1590 return -EINVAL;
1591 }
1592 if (start & ~PAGE_MASK) {
1593 kleave(" = -EINVAL [unaligned start]");
1594 return -EINVAL;
1595 }
1596 if (end != vma->vm_end && end & ~PAGE_MASK) {
1597 kleave(" = -EINVAL [unaligned split]");
1598 return -EINVAL;
1599 }
1600 if (start != vma->vm_start && end != vma->vm_end) {
1601 ret = split_vma(mm, vma, start, 1);
1602 if (ret < 0) {
1603 kleave(" = %d [split]", ret);
1604 return ret;
1605 }
1606 }
1607 return shrink_vma(mm, vma, start, end);
1608 }
1da177e4 1609
8feae131
DH
1610erase_whole_vma:
1611 delete_vma_from_mm(vma);
1612 delete_vma(mm, vma);
1613 kleave(" = 0");
1da177e4
LT
1614 return 0;
1615}
b5073173 1616EXPORT_SYMBOL(do_munmap);
1da177e4 1617
6a6160a7 1618SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
3034097a
DH
1619{
1620 int ret;
1621 struct mm_struct *mm = current->mm;
1622
1623 down_write(&mm->mmap_sem);
1624 ret = do_munmap(mm, addr, len);
1625 up_write(&mm->mmap_sem);
1626 return ret;
1627}
1628
1629/*
8feae131 1630 * release all the mappings made in a process's VM space
3034097a 1631 */
8feae131 1632void exit_mmap(struct mm_struct *mm)
1da177e4 1633{
8feae131 1634 struct vm_area_struct *vma;
1da177e4 1635
8feae131
DH
1636 if (!mm)
1637 return;
1da177e4 1638
8feae131 1639 kenter("");
1da177e4 1640
8feae131 1641 mm->total_vm = 0;
1da177e4 1642
8feae131
DH
1643 while ((vma = mm->mmap)) {
1644 mm->mmap = vma->vm_next;
1645 delete_vma_from_mm(vma);
1646 delete_vma(mm, vma);
1da177e4 1647 }
8feae131
DH
1648
1649 kleave("");
1da177e4
LT
1650}
1651
1da177e4
LT
1652unsigned long do_brk(unsigned long addr, unsigned long len)
1653{
1654 return -ENOMEM;
1655}
1656
1657/*
6fa5f80b
DH
1658 * expand (or shrink) an existing mapping, potentially moving it at the same
1659 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1da177e4 1660 *
6fa5f80b 1661 * under NOMMU conditions, we only permit changing a mapping's size, and only
8feae131
DH
1662 * as long as it stays within the region allocated by do_mmap_private() and the
1663 * block is not shareable
1da177e4 1664 *
6fa5f80b 1665 * MREMAP_FIXED is not supported under NOMMU conditions
1da177e4
LT
1666 */
1667unsigned long do_mremap(unsigned long addr,
1668 unsigned long old_len, unsigned long new_len,
1669 unsigned long flags, unsigned long new_addr)
1670{
6fa5f80b 1671 struct vm_area_struct *vma;
1da177e4
LT
1672
1673 /* insanity checks first */
8feae131 1674 if (old_len == 0 || new_len == 0)
1da177e4
LT
1675 return (unsigned long) -EINVAL;
1676
8feae131
DH
1677 if (addr & ~PAGE_MASK)
1678 return -EINVAL;
1679
1da177e4
LT
1680 if (flags & MREMAP_FIXED && new_addr != addr)
1681 return (unsigned long) -EINVAL;
1682
8feae131 1683 vma = find_vma_exact(current->mm, addr, old_len);
6fa5f80b
DH
1684 if (!vma)
1685 return (unsigned long) -EINVAL;
1da177e4 1686
6fa5f80b 1687 if (vma->vm_end != vma->vm_start + old_len)
1da177e4
LT
1688 return (unsigned long) -EFAULT;
1689
6fa5f80b 1690 if (vma->vm_flags & VM_MAYSHARE)
1da177e4
LT
1691 return (unsigned long) -EPERM;
1692
8feae131 1693 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1da177e4
LT
1694 return (unsigned long) -ENOMEM;
1695
1696 /* all checks complete - do it */
6fa5f80b 1697 vma->vm_end = vma->vm_start + new_len;
6fa5f80b
DH
1698 return vma->vm_start;
1699}
b5073173 1700EXPORT_SYMBOL(do_mremap);
6fa5f80b 1701
6a6160a7
HC
1702SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1703 unsigned long, new_len, unsigned long, flags,
1704 unsigned long, new_addr)
6fa5f80b
DH
1705{
1706 unsigned long ret;
1707
1708 down_write(&current->mm->mmap_sem);
1709 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1710 up_write(&current->mm->mmap_sem);
1711 return ret;
1da177e4
LT
1712}
1713
6aab341e 1714struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
deceb6cd 1715 unsigned int foll_flags)
1da177e4
LT
1716{
1717 return NULL;
1718}
1719
1da177e4
LT
1720int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1721 unsigned long to, unsigned long size, pgprot_t prot)
1722{
66aa2b4b
GU
1723 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1724 return 0;
1da177e4 1725}
22c4af40 1726EXPORT_SYMBOL(remap_pfn_range);
1da177e4 1727
f905bc44
PM
1728int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1729 unsigned long pgoff)
1730{
1731 unsigned int size = vma->vm_end - vma->vm_start;
1732
1733 if (!(vma->vm_flags & VM_USERMAP))
1734 return -EINVAL;
1735
1736 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1737 vma->vm_end = vma->vm_start + size;
1738
1739 return 0;
1740}
1741EXPORT_SYMBOL(remap_vmalloc_range);
1742
1da177e4
LT
1743void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1744{
1745}
1746
1747unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1748 unsigned long len, unsigned long pgoff, unsigned long flags)
1749{
1750 return -ENOMEM;
1751}
1752
1363c3cd 1753void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1da177e4
LT
1754{
1755}
1756
1da177e4
LT
1757void unmap_mapping_range(struct address_space *mapping,
1758 loff_t const holebegin, loff_t const holelen,
1759 int even_cows)
1760{
1761}
22c4af40 1762EXPORT_SYMBOL(unmap_mapping_range);
1da177e4 1763
d56e03cd
DH
1764/*
1765 * ask for an unmapped area at which to create a mapping on a file
1766 */
1767unsigned long get_unmapped_area(struct file *file, unsigned long addr,
1768 unsigned long len, unsigned long pgoff,
1769 unsigned long flags)
1770{
1771 unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
1772 unsigned long, unsigned long);
1773
1774 get_area = current->mm->get_unmapped_area;
1775 if (file && file->f_op && file->f_op->get_unmapped_area)
1776 get_area = file->f_op->get_unmapped_area;
1777
1778 if (!get_area)
1779 return -ENOSYS;
1780
1781 return get_area(file, addr, len, pgoff, flags);
1782}
d56e03cd
DH
1783EXPORT_SYMBOL(get_unmapped_area);
1784
1da177e4
LT
1785/*
1786 * Check that a process has enough memory to allocate a new virtual
1787 * mapping. 0 means there is enough memory for the allocation to
1788 * succeed and -ENOMEM implies there is not.
1789 *
1790 * We currently support three overcommit policies, which are set via the
1791 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1792 *
1793 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1794 * Additional code 2002 Jul 20 by Robert Love.
1795 *
1796 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1797 *
1798 * Note this is a helper function intended to be used by LSMs which
1799 * wish to use this logic.
1800 */
34b4e4aa 1801int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
1da177e4
LT
1802{
1803 unsigned long free, allowed;
1804
1805 vm_acct_memory(pages);
1806
1807 /*
1808 * Sometimes we want to use more memory than we have
1809 */
1810 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1811 return 0;
1812
1813 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1814 unsigned long n;
1815
347ce434 1816 free = global_page_state(NR_FILE_PAGES);
1da177e4
LT
1817 free += nr_swap_pages;
1818
1819 /*
1820 * Any slabs which are created with the
1821 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1822 * which are reclaimable, under pressure. The dentry
1823 * cache and most inode caches should fall into this
1824 */
972d1a7b 1825 free += global_page_state(NR_SLAB_RECLAIMABLE);
1da177e4
LT
1826
1827 /*
1828 * Leave the last 3% for root
1829 */
1830 if (!cap_sys_admin)
1831 free -= free / 32;
1832
1833 if (free > pages)
1834 return 0;
1835
1836 /*
1837 * nr_free_pages() is very expensive on large systems,
1838 * only call if we're about to fail.
1839 */
1840 n = nr_free_pages();
d5ddc79b
HA
1841
1842 /*
1843 * Leave reserved pages. The pages are not for anonymous pages.
1844 */
1845 if (n <= totalreserve_pages)
1846 goto error;
1847 else
1848 n -= totalreserve_pages;
1849
1850 /*
1851 * Leave the last 3% for root
1852 */
1da177e4
LT
1853 if (!cap_sys_admin)
1854 n -= n / 32;
1855 free += n;
1856
1857 if (free > pages)
1858 return 0;
d5ddc79b
HA
1859
1860 goto error;
1da177e4
LT
1861 }
1862
1863 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1864 /*
1865 * Leave the last 3% for root
1866 */
1867 if (!cap_sys_admin)
1868 allowed -= allowed / 32;
1869 allowed += total_swap_pages;
1870
1871 /* Don't let a single process grow too big:
1872 leave 3% of the size of this process for other processes */
731572d3
AC
1873 if (mm)
1874 allowed -= mm->total_vm / 32;
1da177e4 1875
00a62ce9 1876 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
1da177e4 1877 return 0;
00a62ce9 1878
d5ddc79b 1879error:
1da177e4
LT
1880 vm_unacct_memory(pages);
1881
1882 return -ENOMEM;
1883}
1884
1885int in_gate_area_no_task(unsigned long addr)
1886{
1887 return 0;
1888}
b0e15190 1889
d0217ac0 1890int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
b0e15190
DH
1891{
1892 BUG();
d0217ac0 1893 return 0;
b0e15190 1894}
b5073173 1895EXPORT_SYMBOL(filemap_fault);
0ec76a11
DH
1896
1897/*
1898 * Access another process' address space.
1899 * - source/target buffer must be kernel space
1900 */
1901int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1902{
0ec76a11
DH
1903 struct vm_area_struct *vma;
1904 struct mm_struct *mm;
1905
1906 if (addr + len < addr)
1907 return 0;
1908
1909 mm = get_task_mm(tsk);
1910 if (!mm)
1911 return 0;
1912
1913 down_read(&mm->mmap_sem);
1914
1915 /* the access must start within one of the target process's mappings */
0159b141
DH
1916 vma = find_vma(mm, addr);
1917 if (vma) {
0ec76a11
DH
1918 /* don't overrun this mapping */
1919 if (addr + len >= vma->vm_end)
1920 len = vma->vm_end - addr;
1921
1922 /* only read or write mappings where it is permitted */
d00c7b99 1923 if (write && vma->vm_flags & VM_MAYWRITE)
0ec76a11 1924 len -= copy_to_user((void *) addr, buf, len);
d00c7b99 1925 else if (!write && vma->vm_flags & VM_MAYREAD)
0ec76a11
DH
1926 len -= copy_from_user(buf, (void *) addr, len);
1927 else
1928 len = 0;
1929 } else {
1930 len = 0;
1931 }
1932
1933 up_read(&mm->mmap_sem);
1934 mmput(mm);
1935 return len;
1936}