Linux 6.9-rc1
[linux-2.6-microblaze.git] / mm / nommu.c
index e819cbc..5ec8f44 100644 (file)
@@ -19,7 +19,6 @@
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/sched/mm.h>
-#include <linux/vmacache.h>
 #include <linux/mman.h>
 #include <linux/swap.h>
 #include <linux/file.h>
@@ -37,6 +36,7 @@
 #include <linux/printk.h>
 
 #include <linux/uaccess.h>
+#include <linux/uio.h>
 #include <asm/tlb.h>
 #include <asm/tlbflush.h>
 #include <asm/mmu_context.h>
@@ -131,8 +131,6 @@ int follow_pfn(struct vm_area_struct *vma, unsigned long address,
 }
 EXPORT_SYMBOL(follow_pfn);
 
-LIST_HEAD(vmap_area_list);
-
 void vfree(const void *addr)
 {
        kfree(addr);
@@ -174,7 +172,7 @@ static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
                mmap_write_lock(current->mm);
                vma = find_vma(current->mm, (unsigned long)ret);
                if (vma)
-                       vma->vm_flags |= VM_USERMAP;
+                       vm_flags_set(vma, VM_USERMAP);
                mmap_write_unlock(current->mm);
        }
 
@@ -199,14 +197,13 @@ unsigned long vmalloc_to_pfn(const void *addr)
 }
 EXPORT_SYMBOL(vmalloc_to_pfn);
 
-long vread(char *buf, char *addr, unsigned long count)
+long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
 {
        /* Don't allow overflow */
-       if ((unsigned long) buf + count < count)
-               count = -(unsigned long) buf;
+       if ((unsigned long) addr + count < count)
+               count = -(unsigned long) addr;
 
-       memcpy(buf, addr, count);
-       return count;
+       return copy_to_iter(addr, count, iter);
 }
 
 /*
@@ -545,26 +542,13 @@ static void put_nommu_region(struct vm_region *region)
        __put_nommu_region(region);
 }
 
-/*
- * add a VMA into a process's mm_struct in the appropriate place in the list
- * and tree and add to the address space's page tree also if not an anonymous
- * page
- * - should be called with mm->mmap_lock held writelocked
- */
-static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
+static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)
 {
-       struct vm_area_struct *pvma, *prev;
-       struct address_space *mapping;
-       struct rb_node **p, *parent, *rb_prev;
-
-       BUG_ON(!vma->vm_region);
-
-       mm->map_count++;
        vma->vm_mm = mm;
 
        /* add the VMA to the mapping */
        if (vma->vm_file) {
-               mapping = vma->vm_file->f_mapping;
+               struct address_space *mapping = vma->vm_file->f_mapping;
 
                i_mmap_lock_write(mapping);
                flush_dcache_mmap_lock(mapping);
@@ -572,67 +556,14 @@ static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
                flush_dcache_mmap_unlock(mapping);
                i_mmap_unlock_write(mapping);
        }
-
-       /* add the VMA to the tree */
-       parent = rb_prev = NULL;
-       p = &mm->mm_rb.rb_node;
-       while (*p) {
-               parent = *p;
-               pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
-
-               /* sort by: start addr, end addr, VMA struct addr in that order
-                * (the latter is necessary as we may get identical VMAs) */
-               if (vma->vm_start < pvma->vm_start)
-                       p = &(*p)->rb_left;
-               else if (vma->vm_start > pvma->vm_start) {
-                       rb_prev = parent;
-                       p = &(*p)->rb_right;
-               } else if (vma->vm_end < pvma->vm_end)
-                       p = &(*p)->rb_left;
-               else if (vma->vm_end > pvma->vm_end) {
-                       rb_prev = parent;
-                       p = &(*p)->rb_right;
-               } else if (vma < pvma)
-                       p = &(*p)->rb_left;
-               else if (vma > pvma) {
-                       rb_prev = parent;
-                       p = &(*p)->rb_right;
-               } else
-                       BUG();
-       }
-
-       rb_link_node(&vma->vm_rb, parent, p);
-       rb_insert_color(&vma->vm_rb, &mm->mm_rb);
-
-       /* add VMA to the VMA list also */
-       prev = NULL;
-       if (rb_prev)
-               prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
-
-       __vma_link_list(mm, vma, prev);
 }
 
-/*
- * delete a VMA from its owning mm_struct and address space
- */
-static void delete_vma_from_mm(struct vm_area_struct *vma)
-{
-       int i;
-       struct address_space *mapping;
-       struct mm_struct *mm = vma->vm_mm;
-       struct task_struct *curr = current;
-
-       mm->map_count--;
-       for (i = 0; i < VMACACHE_SIZE; i++) {
-               /* if the vma is cached, invalidate the entire cache */
-               if (curr->vmacache.vmas[i] == vma) {
-                       vmacache_invalidate(mm);
-                       break;
-               }
-       }
-
+static void cleanup_vma_from_mm(struct vm_area_struct *vma)
+{
+       vma->vm_mm->map_count--;
        /* remove the VMA from the mapping */
        if (vma->vm_file) {
+               struct address_space *mapping;
                mapping = vma->vm_file->f_mapping;
 
                i_mmap_lock_write(mapping);
@@ -641,13 +572,27 @@ static void delete_vma_from_mm(struct vm_area_struct *vma)
                flush_dcache_mmap_unlock(mapping);
                i_mmap_unlock_write(mapping);
        }
+}
 
-       /* remove from the MM's tree and list */
-       rb_erase(&vma->vm_rb, &mm->mm_rb);
+/*
+ * delete a VMA from its owning mm_struct and address space
+ */
+static int delete_vma_from_mm(struct vm_area_struct *vma)
+{
+       VMA_ITERATOR(vmi, vma->vm_mm, vma->vm_start);
 
-       __vma_unlink_list(mm, vma);
-}
+       vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
+       if (vma_iter_prealloc(&vmi, vma)) {
+               pr_warn("Allocation of vma tree for process %d failed\n",
+                      current->pid);
+               return -ENOMEM;
+       }
+       cleanup_vma_from_mm(vma);
 
+       /* remove from the MM's tree and list */
+       vma_iter_clear(&vmi);
+       return 0;
+}
 /*
  * destroy a VMA record
  */
@@ -661,52 +606,60 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
        vm_area_free(vma);
 }
 
+struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
+                                            unsigned long start_addr,
+                                            unsigned long end_addr)
+{
+       unsigned long index = start_addr;
+
+       mmap_assert_locked(mm);
+       return mt_find(&mm->mm_mt, &index, end_addr - 1);
+}
+EXPORT_SYMBOL(find_vma_intersection);
+
 /*
  * look up the first VMA in which addr resides, NULL if none
  * - should be called with mm->mmap_lock at least held readlocked
  */
 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
 {
-       struct vm_area_struct *vma;
-
-       /* check the cache first */
-       vma = vmacache_find(mm, addr);
-       if (likely(vma))
-               return vma;
-
-       /* trawl the list (there may be multiple mappings in which addr
-        * resides) */
-       for (vma = mm->mmap; vma; vma = vma->vm_next) {
-               if (vma->vm_start > addr)
-                       return NULL;
-               if (vma->vm_end > addr) {
-                       vmacache_update(addr, vma);
-                       return vma;
-               }
-       }
+       VMA_ITERATOR(vmi, mm, addr);
 
-       return NULL;
+       return vma_iter_load(&vmi);
 }
 EXPORT_SYMBOL(find_vma);
 
 /*
- * find a VMA
- * - we don't extend stack VMAs under NOMMU conditions
+ * At least xtensa ends up having protection faults even with no
+ * MMU.. No stack expansion, at least.
  */
-struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
+struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
+                       unsigned long addr, struct pt_regs *regs)
 {
-       return find_vma(mm, addr);
+       struct vm_area_struct *vma;
+
+       mmap_read_lock(mm);
+       vma = vma_lookup(mm, addr);
+       if (!vma)
+               mmap_read_unlock(mm);
+       return vma;
 }
 
 /*
  * expand a stack to a given address
  * - not supported under NOMMU conditions
  */
-int expand_stack(struct vm_area_struct *vma, unsigned long address)
+int expand_stack_locked(struct vm_area_struct *vma, unsigned long addr)
 {
        return -ENOMEM;
 }
 
+struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr)
+{
+       mmap_read_unlock(mm);
+       return NULL;
+}
+
 /*
  * look up the first VMA exactly that exactly matches addr
  * - should be called with mm->mmap_lock at least held readlocked
@@ -717,26 +670,17 @@ static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
 {
        struct vm_area_struct *vma;
        unsigned long end = addr + len;
+       VMA_ITERATOR(vmi, mm, addr);
 
-       /* check the cache first */
-       vma = vmacache_find_exact(mm, addr, end);
-       if (vma)
-               return vma;
-
-       /* trawl the list (there may be multiple mappings in which addr
-        * resides) */
-       for (vma = mm->mmap; vma; vma = vma->vm_next) {
-               if (vma->vm_start < addr)
-                       continue;
-               if (vma->vm_start > addr)
-                       return NULL;
-               if (vma->vm_end == end) {
-                       vmacache_update(addr, vma);
-                       return vma;
-               }
-       }
+       vma = vma_iter_load(&vmi);
+       if (!vma)
+               return NULL;
+       if (vma->vm_start != addr)
+               return NULL;
+       if (vma->vm_end != end)
+               return NULL;
 
-       return NULL;
+       return vma;
 }
 
 /*
@@ -909,29 +853,36 @@ static unsigned long determine_vm_flags(struct file *file,
        unsigned long vm_flags;
 
        vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
-       /* vm_flags |= mm->def_flags; */
 
-       if (!(capabilities & NOMMU_MAP_DIRECT)) {
-               /* attempt to share read-only copies of mapped file chunks */
+       if (!file) {
+               /*
+                * MAP_ANONYMOUS. MAP_SHARED is mapped to MAP_PRIVATE, because
+                * there is no fork().
+                */
                vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
-               if (file && !(prot & PROT_WRITE))
-                       vm_flags |= VM_MAYSHARE;
+       } else if (flags & MAP_PRIVATE) {
+               /* MAP_PRIVATE file mapping */
+               if (capabilities & NOMMU_MAP_DIRECT)
+                       vm_flags |= (capabilities & NOMMU_VMFLAGS);
+               else
+                       vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
+
+               if (!(prot & PROT_WRITE) && !current->ptrace)
+                       /*
+                        * R/O private file mapping which cannot be used to
+                        * modify memory, especially also not via active ptrace
+                        * (e.g., set breakpoints) or later by upgrading
+                        * permissions (no mprotect()). We can try overlaying
+                        * the file mapping, which will work e.g., on chardevs,
+                        * ramfs/tmpfs/shmfs and romfs/cramf.
+                        */
+                       vm_flags |= VM_MAYOVERLAY;
        } else {
-               /* overlay a shareable mapping on the backing device or inode
-                * if possible - used for chardevs, ramfs/tmpfs/shmfs and
-                * romfs/cramfs */
-               vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
-               if (flags & MAP_SHARED)
-                       vm_flags |= VM_SHARED;
+               /* MAP_SHARED file mapping: NOMMU_MAP_DIRECT is set. */
+               vm_flags |= VM_SHARED | VM_MAYSHARE |
+                           (capabilities & NOMMU_VMFLAGS);
        }
 
-       /* refuse to let anyone share private mappings with this process if
-        * it's being traced - otherwise breakpoints set in it may interfere
-        * with another untraced process
-        */
-       if ((flags & MAP_PRIVATE) && current->ptrace)
-               vm_flags &= ~VM_MAYSHARE;
-
        return vm_flags;
 }
 
@@ -969,15 +920,18 @@ static int do_mmap_private(struct vm_area_struct *vma,
        void *base;
        int ret, order;
 
-       /* invoke the file's mapping function so that it can keep track of
-        * shared mappings on devices or memory
-        * - VM_MAYSHARE will be set if it may attempt to share
+       /*
+        * Invoke the file's mapping function so that it can keep track of
+        * shared mappings on devices or memory. VM_MAYOVERLAY will be set if
+        * it may attempt to share, which will make is_nommu_shared_mapping()
+        * happy.
         */
        if (capabilities & NOMMU_MAP_DIRECT) {
                ret = call_mmap(vma->vm_file, vma);
+               /* shouldn't return success if we're not sharing */
+               if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags)))
+                       ret = -ENOSYS;
                if (ret == 0) {
-                       /* shouldn't return success if we're not sharing */
-                       BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
                        vma->vm_region->vm_top = vma->vm_region->vm_end;
                        return 0;
                }
@@ -1008,7 +962,8 @@ static int do_mmap_private(struct vm_area_struct *vma,
 
        atomic_long_add(total, &mmap_pages_allocated);
 
-       region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
+       vm_flags_set(vma, VM_MAPPED_COPY);
+       region->vm_flags = vma->vm_flags;
        region->vm_start = (unsigned long) base;
        region->vm_end   = region->vm_start + len;
        region->vm_top   = region->vm_start + (total << PAGE_SHIFT);
@@ -1047,7 +1002,7 @@ error_free:
 enomem:
        pr_err("Allocation of length %lu from process %d (%s) failed\n",
               len, current->pid, current->comm);
-       show_free_areas(0, NULL);
+       show_mem();
        return -ENOMEM;
 }
 
@@ -1059,6 +1014,7 @@ unsigned long do_mmap(struct file *file,
                        unsigned long len,
                        unsigned long prot,
                        unsigned long flags,
+                       vm_flags_t vm_flags,
                        unsigned long pgoff,
                        unsigned long *populate,
                        struct list_head *uf)
@@ -1066,9 +1022,9 @@ unsigned long do_mmap(struct file *file,
        struct vm_area_struct *vma;
        struct vm_region *region;
        struct rb_node *rb;
-       vm_flags_t vm_flags;
        unsigned long capabilities, result;
        int ret;
+       VMA_ITERATOR(vmi, current->mm, 0);
 
        *populate = 0;
 
@@ -1085,7 +1041,8 @@ unsigned long do_mmap(struct file *file,
 
        /* we've determined that we can make the mapping, now translate what we
         * now know into VMA flags */
-       vm_flags = determine_vm_flags(file, prot, flags, capabilities);
+       vm_flags |= determine_vm_flags(file, prot, flags, capabilities);
+
 
        /* we're going to need to record the mapping */
        region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
@@ -1100,7 +1057,7 @@ unsigned long do_mmap(struct file *file,
        region->vm_flags = vm_flags;
        region->vm_pgoff = pgoff;
 
-       vma->vm_flags = vm_flags;
+       vm_flags_init(vma, vm_flags);
        vma->vm_pgoff = pgoff;
 
        if (file) {
@@ -1118,7 +1075,7 @@ unsigned long do_mmap(struct file *file,
         *   these cases, sharing is handled in the driver or filesystem rather
         *   than here
         */
-       if (vm_flags & VM_MAYSHARE) {
+       if (is_nommu_shared_mapping(vm_flags)) {
                struct vm_region *pregion;
                unsigned long pglen, rpglen, pgend, rpgend, start;
 
@@ -1128,7 +1085,7 @@ unsigned long do_mmap(struct file *file,
                for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
                        pregion = rb_entry(rb, struct vm_region, vm_rb);
 
-                       if (!(pregion->vm_flags & VM_MAYSHARE))
+                       if (!is_nommu_shared_mapping(pregion->vm_flags))
                                continue;
 
                        /* search for overlapping mappings on the same file */
@@ -1164,7 +1121,7 @@ unsigned long do_mmap(struct file *file,
                        vma->vm_end = start + len;
 
                        if (pregion->vm_flags & VM_MAPPED_COPY)
-                               vma->vm_flags |= VM_MAPPED_COPY;
+                               vm_flags_set(vma, VM_MAPPED_COPY);
                        else {
                                ret = do_mmap_shared_file(vma);
                                if (ret < 0) {
@@ -1236,7 +1193,15 @@ unsigned long do_mmap(struct file *file,
        current->mm->total_vm += len >> PAGE_SHIFT;
 
 share:
-       add_vma_to_mm(current->mm, vma);
+       BUG_ON(!vma->vm_region);
+       vma_iter_config(&vmi, vma->vm_start, vma->vm_end);
+       if (vma_iter_prealloc(&vmi, vma))
+               goto error_just_free;
+
+       setup_vma_to_mm(vma, current->mm);
+       current->mm->map_count++;
+       /* add the VMA to the tree */
+       vma_iter_store(&vmi, vma);
 
        /* we flush the region from the icache only when the first executable
         * mapping of it is made  */
@@ -1252,6 +1217,7 @@ share:
 error_just_free:
        up_write(&nommu_region_sem);
 error:
+       vma_iter_free(&vmi);
        if (region->vm_file)
                fput(region->vm_file);
        kmem_cache_free(vm_region_jar, region);
@@ -1270,13 +1236,13 @@ error_getting_vma:
        kmem_cache_free(vm_region_jar, region);
        pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
                        len, current->pid);
-       show_free_areas(0, NULL);
+       show_mem();
        return -ENOMEM;
 
 error_getting_region:
        pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
                        len, current->pid);
-       show_free_areas(0, NULL);
+       show_mem();
        return -ENOMEM;
 }
 
@@ -1337,18 +1303,20 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  * split a vma into two pieces at address 'addr', a new vma is allocated either
  * for the first part or the tail.
  */
-int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
-             unsigned long addr, int new_below)
+static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
+                    unsigned long addr, int new_below)
 {
        struct vm_area_struct *new;
        struct vm_region *region;
        unsigned long npages;
+       struct mm_struct *mm;
 
        /* we're only permitted to split anonymous regions (these should have
         * only a single usage on the region) */
        if (vma->vm_file)
                return -ENOMEM;
 
+       mm = vma->vm_mm;
        if (mm->map_count >= sysctl_max_map_count)
                return -ENOMEM;
 
@@ -1357,10 +1325,8 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
                return -ENOMEM;
 
        new = vm_area_dup(vma);
-       if (!new) {
-               kmem_cache_free(vm_region_jar, region);
-               return -ENOMEM;
-       }
+       if (!new)
+               goto err_vma_dup;
 
        /* most fields are the same, copy all, and then fixup */
        *region = *vma->vm_region;
@@ -1375,10 +1341,16 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
                region->vm_pgoff = new->vm_pgoff += npages;
        }
 
+       vma_iter_config(vmi, new->vm_start, new->vm_end);
+       if (vma_iter_prealloc(vmi, vma)) {
+               pr_warn("Allocation of vma tree for process %d failed\n",
+                       current->pid);
+               goto err_vmi_preallocate;
+       }
+
        if (new->vm_ops && new->vm_ops->open)
                new->vm_ops->open(new);
 
-       delete_vma_from_mm(vma);
        down_write(&nommu_region_sem);
        delete_nommu_region(vma->vm_region);
        if (new_below) {
@@ -1391,16 +1363,25 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
        add_nommu_region(vma->vm_region);
        add_nommu_region(new->vm_region);
        up_write(&nommu_region_sem);
-       add_vma_to_mm(mm, vma);
-       add_vma_to_mm(mm, new);
+
+       setup_vma_to_mm(vma, mm);
+       setup_vma_to_mm(new, mm);
+       vma_iter_store(vmi, new);
+       mm->map_count++;
        return 0;
+
+err_vmi_preallocate:
+       vm_area_free(new);
+err_vma_dup:
+       kmem_cache_free(vm_region_jar, region);
+       return -ENOMEM;
 }
 
 /*
  * shrink a VMA by removing the specified chunk from either the beginning or
  * the end
  */
-static int shrink_vma(struct mm_struct *mm,
+static int vmi_shrink_vma(struct vma_iterator *vmi,
                      struct vm_area_struct *vma,
                      unsigned long from, unsigned long to)
 {
@@ -1408,12 +1389,15 @@ static int shrink_vma(struct mm_struct *mm,
 
        /* adjust the VMA's pointers, which may reposition it in the MM's tree
         * and list */
-       delete_vma_from_mm(vma);
-       if (from > vma->vm_start)
+       if (from > vma->vm_start) {
+               if (vma_iter_clear_gfp(vmi, from, vma->vm_end, GFP_KERNEL))
+                       return -ENOMEM;
                vma->vm_end = from;
-       else
+       } else {
+               if (vma_iter_clear_gfp(vmi, vma->vm_start, to, GFP_KERNEL))
+                       return -ENOMEM;
                vma->vm_start = to;
-       add_vma_to_mm(mm, vma);
+       }
 
        /* cut the backing region down to size */
        region = vma->vm_region;
@@ -1441,9 +1425,10 @@ static int shrink_vma(struct mm_struct *mm,
  */
 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
 {
+       VMA_ITERATOR(vmi, mm, start);
        struct vm_area_struct *vma;
        unsigned long end;
-       int ret;
+       int ret = 0;
 
        len = PAGE_ALIGN(len);
        if (len == 0)
@@ -1452,7 +1437,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
        end = start + len;
 
        /* find the first potentially overlapping VMA */
-       vma = find_vma(mm, start);
+       vma = vma_find(&vmi, end);
        if (!vma) {
                static int limit;
                if (limit < 5) {
@@ -1471,7 +1456,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
                                return -EINVAL;
                        if (end == vma->vm_end)
                                goto erase_whole_vma;
-                       vma = vma->vm_next;
+                       vma = vma_find(&vmi, end);
                } while (vma);
                return -EINVAL;
        } else {
@@ -1485,17 +1470,19 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
                if (end != vma->vm_end && offset_in_page(end))
                        return -EINVAL;
                if (start != vma->vm_start && end != vma->vm_end) {
-                       ret = split_vma(mm, vma, start, 1);
+                       ret = split_vma(&vmi, vma, start, 1);
                        if (ret < 0)
                                return ret;
                }
-               return shrink_vma(mm, vma, start, end);
+               return vmi_shrink_vma(&vmi, vma, start, end);
        }
 
 erase_whole_vma:
-       delete_vma_from_mm(vma);
-       delete_vma(mm, vma);
-       return 0;
+       if (delete_vma_from_mm(vma))
+               ret = -ENOMEM;
+       else
+               delete_vma(mm, vma);
+       return ret;
 }
 
 int vm_munmap(unsigned long addr, size_t len)
@@ -1520,6 +1507,7 @@ SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  */
 void exit_mmap(struct mm_struct *mm)
 {
+       VMA_ITERATOR(vmi, mm, 0);
        struct vm_area_struct *vma;
 
        if (!mm)
@@ -1527,17 +1515,18 @@ void exit_mmap(struct mm_struct *mm)
 
        mm->total_vm = 0;
 
-       while ((vma = mm->mmap)) {
-               mm->mmap = vma->vm_next;
-               delete_vma_from_mm(vma);
+       /*
+        * Lock the mm to avoid assert complaining even though this is the only
+        * user of the mm
+        */
+       mmap_write_lock(mm);
+       for_each_vma(vmi, vma) {
+               cleanup_vma_from_mm(vma);
                delete_vma(mm, vma);
                cond_resched();
        }
-}
-
-int vm_brk(unsigned long addr, unsigned long len)
-{
-       return -ENOMEM;
+       __mt_destroy(&mm->mm_mt);
+       mmap_write_unlock(mm);
 }
 
 /*
@@ -1575,7 +1564,7 @@ static unsigned long do_mremap(unsigned long addr,
        if (vma->vm_end != vma->vm_start + old_len)
                return (unsigned long) -EFAULT;
 
-       if (vma->vm_flags & VM_MAYSHARE)
+       if (is_nommu_shared_mapping(vma->vm_flags))
                return (unsigned long) -EPERM;
 
        if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
@@ -1610,7 +1599,7 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
        if (addr != (pfn << PAGE_SHIFT))
                return -EINVAL;
 
-       vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
+       vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
        return 0;
 }
 EXPORT_SYMBOL(remap_pfn_range);
@@ -1655,8 +1644,8 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 }
 EXPORT_SYMBOL(filemap_map_pages);
 
-int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
-                      int len, unsigned int gup_flags)
+static int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
+                             void *buf, int len, unsigned int gup_flags)
 {
        struct vm_area_struct *vma;
        int write = gup_flags & FOLL_WRITE;
@@ -1804,7 +1793,7 @@ static int __meminit init_user_reserve(void)
 {
        unsigned long free_kbytes;
 
-       free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+       free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
 
        sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
        return 0;
@@ -1825,7 +1814,7 @@ static int __meminit init_admin_reserve(void)
 {
        unsigned long free_kbytes;
 
-       free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+       free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
 
        sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
        return 0;