Skip to content

Commit

Permalink
mmap: fix error paths with dup_anon_vma()
Browse files Browse the repository at this point in the history
commit 824135c upstream.

When the calling function fails after the dup_anon_vma(), the
duplication of the anon_vma is not being undone.  Add the necessary
unlink_anon_vma() call to the error paths that are missing them.

This issue showed up during inspection of the error path in vma_merge()
for an unrelated vma iterator issue.

Users may experience increased memory usage, which may be problematic as
the failure would likely be caused by a low memory situation.

Link: https://lkml.kernel.org/r/20230929183041.2835469-3-Liam.Howlett@oracle.com
Fixes: d4af56c ("mm: start tracking VMAs with maple tree")
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Lorenzo Stoakes <lstoakes@gmail.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
howlett authored and gregkh committed Nov 8, 2023
1 parent 21ca008 commit 9411dbe
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma,
struct anon_vma *anon_vma = vma->anon_vma;
struct file *file = vma->vm_file;
bool remove_next = false;
struct vm_area_struct *anon_dup = NULL;

if (next && (vma != next) && (end == next->vm_end)) {
remove_next = true;
Expand All @@ -530,6 +531,8 @@ inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma,
error = anon_vma_clone(vma, next);
if (error)
return error;

anon_dup = vma;
}
}

Expand Down Expand Up @@ -602,6 +605,9 @@ inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma,
return 0;

nomem:
if (anon_dup)
unlink_anon_vmas(anon_dup);

return -ENOMEM;
}

Expand Down Expand Up @@ -629,6 +635,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
int remove_next = 0;
MA_STATE(mas, &mm->mm_mt, 0, 0);
struct vm_area_struct *exporter = NULL, *importer = NULL;
struct vm_area_struct *anon_dup = NULL;

if (next && !insert) {
if (end >= next->vm_end) {
Expand Down Expand Up @@ -709,11 +716,17 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
error = anon_vma_clone(importer, exporter);
if (error)
return error;

anon_dup = importer;
}
}

if (mas_preallocate(&mas, vma, GFP_KERNEL))
if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
if (anon_dup)
unlink_anon_vmas(anon_dup);

return -ENOMEM;
}

vma_adjust_trans_huge(orig_vma, start, end, adjust_next);
if (file) {
Expand Down

0 comments on commit 9411dbe

Please sign in to comment.