validate_mm(mm);
 }
 
+/*
+ * abort_munmap_vmas - Undo any munmap work and free resources
+ *
+ * Reattach any detached vmas and free up the maple tree used to track the vmas.
+ */
+static inline void abort_munmap_vmas(struct ma_state *mas_detach)
+{
+       struct vm_area_struct *vma;
+
+       mas_set(mas_detach, 0);
+       mas_for_each(mas_detach, vma, ULONG_MAX)
+               vma_mark_detached(vma, false);
+
+       __mt_destroy(mas_detach->tree);
+}
+
 /*
  * do_vmi_align_munmap() - munmap the aligned region from @start to @end.
  * @vmi: The vma iterator
 userfaultfd_error:
 munmap_gather_failed:
 end_split_failed:
-       mas_set(&mas_detach, 0);
-       mas_for_each(&mas_detach, next, end)
-               vma_mark_detached(next, false);
-
-       __mt_destroy(&mt_detach);
+       abort_munmap_vmas(&mas_detach);
 start_split_failed:
 map_count_exceeded:
        validate_mm(mm);