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