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