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