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