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