From: Liam R. Howlett Date: Tue, 10 Nov 2020 18:37:40 +0000 (-0500) Subject: Wip: broken, vma_expand X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fmaple3_wip;p=users%2Fjedix%2Flinux-maple.git Wip: broken, vma_expand Signed-off-by: Liam R. Howlett --- diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 6b5284db50af..660e3923e314 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -776,7 +776,7 @@ static inline void mte_set_gap(const struct maple_enode *mn, static inline void mas_ascend(struct ma_state *mas) { - struct maple_enode *p_enode = mas->node; // parent enode. + struct maple_enode *p_enode= mas->node; // parent enode. struct maple_enode *a_enode = mas->node; // ancestor enode. struct maple_node *a_node = mas_mn(mas); // ancestor node. unsigned char a_slot = 0; @@ -793,7 +793,7 @@ static inline void mas_ascend(struct ma_state *mas) a_enode = p_enode; if (mte_is_root(a_enode)) { a_node = mte_to_node(a_enode); - goto no_parent; + goto parent_is_root; } mas->node = p_enode; @@ -812,6 +812,7 @@ ascend: max = mte_pivot(a_enode, a_slot); } +parent_is_root: no_parent: if (ma_is_root(a_node)) { if (!set_min) @@ -3441,6 +3442,7 @@ complete_at_root: if (ret > 2) return NULL; spanning_store: + return content; } @@ -4525,6 +4527,9 @@ void *mas_next(struct ma_state *mas, unsigned long max) { unsigned long index = 0; + if (mas_is_none(mas)) + mas->node = MAS_START; + return _mas_next(mas, max, &index); } EXPORT_SYMBOL_GPL(mas_next); @@ -4920,7 +4925,7 @@ void *mas_store(struct ma_state *mas, void *entry) if (mas_is_err(mas)) return existing; - if (!mte_is_leaf(mas->node)) // gaps were updated + if (!mte_is_leaf(mas->node)) // spanning store occurred mas->node = MAS_START; return existing; @@ -4947,7 +4952,7 @@ retry: if (mas_is_err(mas)) return xa_err(mas->node); - if (!mte_is_leaf(mas->node)) // gaps were updated + if (!mte_is_leaf(mas->node)) // spanning store occurred mas->node = MAS_START; return 0; diff --git a/mm/mmap.c b/mm/mmap.c index 43100c78ed3a..4fa5e1d179a2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -496,9 +496,11 @@ munmap_vma_range(struct mm_struct *mm, unsigned long start, unsigned long len, struct vm_area_struct **pprev, struct list_head *uf) { // Needs optimization. - while (range_has_overlap(mm, start, start + len, pprev)) + while (range_has_overlap(mm, start, start + len, pprev)) { + printk("%px %lu-%lu overlap\n", mm, start, start + len); if (do_munmap(mm, start, len, uf)) return -ENOMEM; + } return 0; } @@ -642,7 +644,97 @@ static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) __vma_link_list(mm, vma, prev); mm->map_count++; } +inline int vma_expand(struct vm_area_struct *vma, unsigned long start, + unsigned long end, pgoff_t pgoff, struct vm_area_struct *next) +{ + struct mm_struct *mm = vma->vm_mm; + struct address_space *mapping = NULL; + struct rb_root_cached *root = NULL; + struct anon_vma *anon_vma = vma->anon_vma; + struct file *file = vma->vm_file; + bool remove_next; + int error; + + if (next && (end == next->vm_end)) + remove_next = true; + + /* Expanding over next requires importing anon_vma */ + if (remove_next && next->anon_vma && !vma->anon_vma) { + vma->anon_vma = next->anon_vma; + error = anon_vma_clone(vma, next); + if (error) + return error; + } + + vma_adjust_trans_huge(vma, start, end, 0); + if (file) { + mapping = file->f_mapping; + root = &mapping->i_mmap; + uprobe_munmap(vma, vma->vm_start, vma->vm_end); + i_mmap_lock_write(mapping); + } + + if (anon_vma) { + anon_vma_lock_write(anon_vma); + anon_vma_interval_tree_pre_update_vma(vma); + } + + if (file) { + flush_dcache_mmap_lock(mapping); + vma_interval_tree_remove(vma, root); + } + + vma->vm_start = start; + vma->vm_end = end; + vma->vm_pgoff = pgoff; + vma_mt_store(mm, vma); + + if (file) { + vma_interval_tree_insert(vma, root); + flush_dcache_mmap_unlock(mapping); + } + + /* Expanding over the next vma */ + if (remove_next) { + /* Remove from mm linked list - also updates highest_vm_end */ + __vma_unlink_list(mm, next); + + /* Kill the cache */ + vmacache_invalidate(mm); + + if (file) + __remove_shared_vm_struct(next, file, mapping); + + } else if (!next) { + mm->highest_vm_end = vm_end_gap(vma); + } + + if (anon_vma) { + anon_vma_interval_tree_post_update_vma(vma); + anon_vma_unlock_write(anon_vma); + } + + if (file) { + i_mmap_unlock_write(mapping); + uprobe_mmap(vma); + } + + if (remove_next) { + if (file) { + uprobe_munmap(next, next->vm_start, next->vm_end); + fput(file); + } + if (next->anon_vma) + anon_vma_merge(vma, next); + mm->map_count--; + mpol_put(vma_policy(next)); + vm_area_free(next); + } + + validate_mm(mm); + return 0; +} /* * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that * is already present in an i_mmap tree without adjusting the tree. @@ -1633,9 +1725,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, struct list_head *uf) { struct mm_struct *mm = current->mm; - struct vm_area_struct *vma, *prev, *merge; + struct vm_area_struct *vma = NULL, *merge; + struct vm_area_struct *prev, *next; + pgoff_t pglen = len >> PAGE_SHIFT; int error; unsigned long charged = 0; + unsigned long end = addr + len; + unsigned long merge_start = addr, merge_end = end; + pgoff_t vm_pgoff; + MA_STATE(mas, &mm->mm_mt, addr, end - 1); /* Check against address space limit. */ if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { @@ -1652,9 +1750,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, return -ENOMEM; } - /* Clear old maps, set up prev and uf */ - if (munmap_vma_range(mm, addr, len, &prev, uf)) + /* Unmap any existing mapping in the area */ + if (do_munmap(mm, addr, len, uf)) return -ENOMEM; + /* * Private writable mapping: check memory availability */ @@ -1665,14 +1764,50 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vm_flags |= VM_ACCOUNT; } - /* - * Can we just expand an old mapping? - */ - vma = vma_merge(mm, prev, addr, addr + len, vm_flags, - NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX); - if (vma) - goto out; + if (vm_flags & VM_SPECIAL) + goto cannot_expand; + + /* Attempt to expand an old mapping */ + next = mas_next(&mas, ULONG_MAX); + prev = mas_prev(&mas, 0); + + if (!prev || prev->vm_end != addr || + vma_policy(prev)) + goto check_next; + + if (can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, + NULL_VM_UFFD_CTX)) { + merge_start = prev->vm_start; + vma = prev; + vm_pgoff = prev->vm_pgoff; + } + +check_next: + if (!next || next->vm_start != end || vma_policy(next)) + goto complete_merge; + + if (can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, + NULL_VM_UFFD_CTX)) { + merge_end = next->vm_end; + if (!vma) { + vma = next; + vm_pgoff = next->vm_pgoff - pglen; + } + } + +complete_merge: + if (vma) { + if (!vma_expand(vma, merge_start, merge_end, vm_pgoff, next)) { + vma = NULL; + } else { + khugepaged_enter_vma_merge(prev, vm_flags); + goto out; + } + } + + +cannot_expand: /* * Determine the object being mapped and call the appropriate * specific mapper. the address has already been validated, but @@ -1685,7 +1820,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, } vma->vm_start = addr; - vma->vm_end = addr + len; + vma->vm_end = end; vma->vm_flags = vm_flags; vma->vm_page_prot = vm_get_page_prot(vm_flags); vma->vm_pgoff = pgoff; @@ -1717,7 +1852,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, */ if (unlikely(vm_flags != vma->vm_flags && prev)) { merge = vma_merge(mm, prev, vma->vm_start, vma->vm_end, vma->vm_flags, - NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX); + NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX); if (merge) { /* ->mmap() can change vma->vm_file and fput the original file. So * fput the vma->vm_file here or we would add an extra fput for file @@ -1779,8 +1914,8 @@ out: vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); if (vm_flags & VM_LOCKED) { if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || - is_vm_hugetlb_page(vma) || - vma == get_gate_vma(current->mm)) + is_vm_hugetlb_page(vma) || + vma == get_gate_vma(current->mm)) vma->vm_flags &= VM_LOCKED_CLEAR_MASK; else mm->locked_vm += (len >> PAGE_SHIFT); @@ -2608,16 +2743,13 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, arch_unmap(mm, start, end); /* Find the first overlapping VMA */ - vma = find_vma(mm, start); + vma = find_vma_intersection(mm, start, end); if (!vma) return 0; + prev = vma->vm_prev; /* we have start < vma->vm_end */ - /* if it doesn't overlap, we have nothing.. */ - if (vma->vm_start >= end) - return 0; - /* * If we need to split any vma, do it now to save pain later. *