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