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