16 #include <linux/export.h>
18 #include <linux/mman.h>
23 #include <linux/slab.h>
28 #include <linux/personality.h>
31 #include <linux/audit.h>
33 #include <asm/uaccess.h>
35 #include <asm/tlbflush.h>
36 #include <asm/mmu_context.h>
40 #define kenter(FMT, ...) \
41 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
42 #define kleave(FMT, ...) \
43 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
44 #define kdebug(FMT, ...) \
45 printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
47 #define kenter(FMT, ...) \
48 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
49 #define kleave(FMT, ...) \
50 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
51 #define kdebug(FMT, ...) \
52 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
97 page = virt_to_head_page(objp);
112 if (!PageCompound(page)) {
124 return PAGE_SIZE << compound_order(page);
128 unsigned long start,
int nr_pages,
unsigned int foll_flags,
139 vm_flags = (foll_flags & FOLL_WRITE) ?
140 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
141 vm_flags &= (foll_flags & FOLL_FORCE) ?
142 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
144 for (i = 0; i < nr_pages; i++) {
147 goto finish_or_fault;
150 if ((vma->
vm_flags & (VM_IO | VM_PFNMAP)) ||
152 goto finish_or_fault;
206 if (!(vma->
vm_flags & (VM_IO | VM_PFNMAP)))
274 if ((
unsigned long) addr + count < count)
275 count = -(
unsigned long) addr;
352 #ifndef PAGE_KERNEL_EXEC
353 # define PAGE_KERNEL_EXEC PAGE_KERNEL
497 if (brk <= mm->
brk) {
525 #ifdef CONFIG_DEBUG_NOMMU_REGIONS
526 static noinline void validate_nommu_regions(
void)
531 lastp =
rb_first(&nommu_region_tree);
551 static void validate_nommu_regions(
void)
559 static void add_nommu_region(
struct vm_region *region)
564 validate_nommu_regions();
567 p = &nommu_region_tree.
rb_node;
575 else if (pregion == region)
581 rb_link_node(®ion->
vm_rb, parent, p);
584 validate_nommu_regions();
590 static void delete_nommu_region(
struct vm_region *region)
594 validate_nommu_regions();
596 validate_nommu_regions();
602 static void free_page_series(
unsigned long from,
unsigned long to)
607 kdebug(
"- free %lx", from);
608 atomic_long_dec(&mmap_pages_allocated);
609 if (page_count(page) != 1)
610 kdebug(
"free page %p: refcount not one: %d",
611 page, page_count(page));
622 static void __put_nommu_region(
struct vm_region *region)
631 delete_nommu_region(region);
639 if (region->
vm_flags & VM_MAPPED_COPY) {
652 static void put_nommu_region(
struct vm_region *region)
655 __put_nommu_region(region);
666 while (start < vma->vm_end) {
667 protect_page(mm, start, flags);
670 update_protections(mm);
697 mapping = vma->
vm_file->f_mapping;
701 vma_interval_tree_insert(vma, &mapping->
i_mmap);
707 parent = rb_prev =
NULL;
708 p = &mm->
mm_rb.rb_node;
725 }
else if (vma < pvma)
727 else if (vma > pvma) {
734 rb_link_node(&vma->
vm_rb, parent, p);
763 mapping = vma->
vm_file->f_mapping;
767 vma_interval_tree_remove(vma, &mapping->
i_mmap);
853 unsigned long end = addr + len;
880 static int validate_mmap_request(
struct file *
file,
886 unsigned long *_capabilities)
894 "%d: Can't do fixed-address/overlay mmap of RAM\n",
920 if (!file->
f_op || !file->
f_op->mmap)
929 mapping = file->
f_path.dentry->d_inode->i_mapping;
938 switch (file->
f_path.dentry->d_inode->i_mode &
S_IFMT) {
958 if (!file->
f_op->get_unmapped_area)
960 if (!file->
f_op->read)
977 if (locks_verify_locked(file->
f_path.dentry->d_inode))
994 if (prot & PROT_WRITE)
1003 capabilities &= ~BDI_CAP_MAP_DIRECT;
1004 if (flags & MAP_SHARED) {
1006 "MAP_SHARED not completely supported on !MMU\n");
1015 if (prot & PROT_EXEC)
1018 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1021 if (capabilities & BDI_CAP_EXEC_MAP)
1025 else if ((prot & PROT_READ) &&
1026 (prot & PROT_EXEC) &&
1027 !(capabilities & BDI_CAP_EXEC_MAP)
1030 capabilities &= ~BDI_CAP_MAP_DIRECT;
1040 if ((prot & PROT_READ) &&
1059 static unsigned long determine_vm_flags(
struct file *file,
1061 unsigned long flags,
1062 unsigned long capabilities)
1066 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
1069 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
1071 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1072 if (file && !(prot & PROT_WRITE))
1073 vm_flags |= VM_MAYSHARE;
1079 if (flags & MAP_SHARED)
1080 vm_flags |= VM_SHARED;
1088 vm_flags &= ~VM_MAYSHARE;
1121 unsigned long capabilities)
1124 unsigned long total,
point,
n;
1132 if (capabilities & BDI_CAP_MAP_DIRECT) {
1154 kdebug(
"alloc order %d for %lx", order, len);
1161 atomic_long_add(total, &mmap_pages_allocated);
1168 while (total > point) {
1169 order =
ilog2(total - point);
1171 kdebug(
"shave %lu/%lu @%lu", n, total - point, total);
1172 atomic_long_sub(n, &mmap_pages_allocated);
1174 set_page_refcounted(pages + total);
1179 for (point = 1; point < total; point++)
1180 set_page_refcounted(&pages[point]);
1209 memset(base + ret, 0, len - ret);
1223 printk(
"Allocation of length %lu from process %d (%s) failed\n",
1236 unsigned long flags,
1237 unsigned long pgoff)
1245 kenter(
",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
1249 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1252 kleave(
" = %d [val]", ret);
1262 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1265 region = kmem_cache_zalloc(vm_region_jar,
GFP_KERNEL);
1267 goto error_getting_region;
1271 goto error_getting_vma;
1282 region->
vm_file = get_file(file);
1283 vma->
vm_file = get_file(file);
1296 if (vm_flags & VM_MAYSHARE) {
1298 unsigned long pglen, rpglen, pgend, rpgend,
start;
1300 pglen = (len +
PAGE_SIZE - 1) >> PAGE_SHIFT;
1301 pgend = pgoff + pglen;
1306 if (!(pregion->
vm_flags & VM_MAYSHARE))
1310 if (pregion->
vm_file->f_path.dentry->d_inode !=
1311 file->
f_path.dentry->d_inode)
1318 rpglen = (rpglen +
PAGE_SIZE - 1) >> PAGE_SHIFT;
1319 rpgend = pregion->
vm_pgoff + rpglen;
1320 if (pgoff >= rpgend)
1325 if ((pregion->
vm_pgoff != pgoff || rpglen != pglen) &&
1326 !(pgoff >= pregion->
vm_pgoff && pgend <= rpgend)) {
1328 if (!(capabilities & BDI_CAP_MAP_DIRECT))
1329 goto sharing_violation;
1337 start += (pgoff - pregion->
vm_pgoff) << PAGE_SHIFT;
1339 vma->
vm_end = start + len;
1341 if (pregion->
vm_flags & VM_MAPPED_COPY) {
1346 ret = do_mmap_shared_file(vma);
1353 goto error_just_free;
1367 if (capabilities & BDI_CAP_MAP_DIRECT) {
1368 addr = file->
f_op->get_unmapped_area(file, addr, len,
1373 goto error_just_free;
1379 if (!(capabilities & BDI_CAP_MAP_COPY))
1380 goto error_just_free;
1382 capabilities &= ~BDI_CAP_MAP_DIRECT;
1395 if (file && vma->
vm_flags & VM_SHARED)
1396 ret = do_mmap_shared_file(vma);
1398 ret = do_mmap_private(vma, region, len, capabilities);
1400 goto error_just_free;
1401 add_nommu_region(region);
1414 add_vma_to_mm(
current->mm, vma);
1425 kleave(
" = %lx", result);
1449 " from process %d failed\n",
1454 error_getting_region:
1456 " from process %d failed\n",
1463 unsigned long, prot,
unsigned long, flags,
1464 unsigned long,
fd,
unsigned long, pgoff)
1466 struct file *file =
NULL;
1469 audit_mmap_fd(
fd, flags);
1478 retval =
vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1486 #ifdef __ARCH_WANT_SYS_OLD_MMAP
1487 struct mmap_arg_struct {
1491 unsigned long flags;
1498 struct mmap_arg_struct
a;
1506 a.offset >> PAGE_SHIFT);
1515 unsigned long addr,
int new_below)
1519 unsigned long npages;
1544 new->vm_region = region;
1546 npages = (addr - vma->
vm_start) >> PAGE_SHIFT;
1552 region->
vm_pgoff =
new->vm_pgoff += npages;
1555 if (new->vm_ops && new->vm_ops->open)
1556 new->vm_ops->open(
new);
1558 delete_vma_from_mm(vma);
1569 add_nommu_region(new->vm_region);
1571 add_vma_to_mm(mm, vma);
1572 add_vma_to_mm(mm,
new);
1580 static int shrink_vma(
struct mm_struct *mm,
1582 unsigned long from,
unsigned long to)
1590 delete_vma_from_mm(vma);
1595 add_vma_to_mm(mm, vma);
1602 delete_nommu_region(region);
1609 add_nommu_region(region);
1612 free_page_series(from, to);
1627 kenter(
",%lx,%zx", start, len);
1638 static int limit = 0;
1641 "munmap of memory not mmapped by process %d"
1642 " (%s): 0x%lx-0x%lx\n",
1644 start, start + len - 1);
1654 kleave(
" = -EINVAL [miss]");
1658 goto erase_whole_vma;
1661 kleave(
" = -EINVAL [split file]");
1666 goto erase_whole_vma;
1668 kleave(
" = -EINVAL [superset]");
1672 kleave(
" = -EINVAL [unaligned start]");
1676 kleave(
" = -EINVAL [unaligned split]");
1682 kleave(
" = %d [split]", ret);
1686 return shrink_vma(mm, vma, start, end);
1690 delete_vma_from_mm(vma);
1691 delete_vma(mm, vma);
1728 while ((vma = mm->
mmap)) {
1730 delete_vma_from_mm(vma);
1731 delete_vma(mm, vma);
1738 unsigned long vm_brk(
unsigned long addr,
unsigned long len)
1754 unsigned long old_len,
unsigned long new_len,
1755 unsigned long flags,
unsigned long new_addr)
1762 if (old_len == 0 || new_len == 0)
1763 return (
unsigned long) -
EINVAL;
1769 return (
unsigned long) -
EINVAL;
1771 vma = find_vma_exact(
current->mm, addr, old_len);
1773 return (
unsigned long) -
EINVAL;
1776 return (
unsigned long) -
EFAULT;
1779 return (
unsigned long) -
EPERM;
1782 return (
unsigned long) -
ENOMEM;
1791 unsigned long, new_len,
unsigned long, flags,
1792 unsigned long, new_addr)
1797 ret =
do_mremap(addr, old_len, new_len, flags, new_addr);
1803 unsigned int foll_flags)
1811 if (addr != (pfn << PAGE_SHIFT))
1814 vma->
vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1820 unsigned long pgoff)
1827 vma->
vm_start = (
unsigned long)(addr + (pgoff << PAGE_SHIFT));
1835 unsigned long len,
unsigned long pgoff,
unsigned long flags)
1845 loff_t
const holebegin, loff_t
const holelen,
1869 unsigned long free, allowed;
1871 vm_acct_memory(pages);
1889 free -= global_page_state(
NR_SHMEM);
1904 if (free <= totalreserve_pages)
1907 free -= totalreserve_pages;
1926 allowed -= allowed / 32;
1938 vm_unacct_memory(pages);
1964 unsigned long addr,
void *
buf,
int len,
int write)
1974 if (addr + len >= vma->
vm_end)
1978 if (write && vma->
vm_flags & VM_MAYWRITE)
1980 (
void *) addr, buf, len);
1981 else if (!write && vma->
vm_flags & VM_MAYREAD)
1983 buf, (
void *) addr, len);
2006 void *buf,
int len,
int write)
2008 return __access_remote_vm(
NULL, mm, addr, buf, len, write);
2019 if (addr + len < addr)
2026 len = __access_remote_vm(tsk, mm, addr, buf, len, write);
2049 size_t r_size, r_top;
2052 high = (size +
PAGE_SIZE - 1) >> PAGE_SHIFT;
2058 vma_interval_tree_foreach(vma, &inode->
i_mapping->i_mmap, low, high) {
2074 vma_interval_tree_foreach(vma, &inode->
i_mapping->i_mmap,
2083 if (r_top > newsize) {
2084 region->
vm_top -= r_top - newsize;