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