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