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