Skip to content

Commit

Permalink
mm/mmap: undo ->mmap() when mas_preallocate() fails
Browse files Browse the repository at this point in the history
A memory leak in hugetlb_reserve_pages was reported in [1].  The root
cause was traced to an error path in mmap_region when mas_preallocate()
fails.  In this case, the vma is freed after a successful call to
filesystem specific mmap.  The hugetlbfs mmap routine may allocate data
structures pointed to by m_private_data.  These need to be cleaned up by
the hugetlb vm_ops->close() routine.

The same issue was addressed by commit deb0f65 ("mm/mmap: undo
->mmap() when arch_validate_flags() fails") for the arch_validate_flags()
test.  Go to the same close_and_free_vma label if mas_preallocate() fails.

[1] https://lore.kernel.org/linux-mm/CAKXUXMxf7OiCwbxib7MwfR4M1b5+b3cNTU7n5NV9Zm4967=FPQ@mail.gmail.com/

Link: https://lkml.kernel.org/r/[email protected]
Fixes: d4af56c ("mm: start tracking VMAs with maple tree")
Signed-off-by: Mike Kravetz <[email protected]>
Reported-by: Lukas Bulwahn <[email protected]>
Reviewed-by: Liam R. Howlett <[email protected]>
Cc: Andrii Nakryiko <[email protected]>
Cc: Carlos Llamas <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Muchun Song <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
mjkravetz authored and akpm00 committed Oct 21, 2022
1 parent eacf96d commit 5789151
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
if (mas_preallocate(&mas, vma, GFP_KERNEL)) {
error = -ENOMEM;
if (file)
goto unmap_and_free_vma;
goto close_and_free_vma;
else
goto free_vma;
}
Expand Down

0 comments on commit 5789151

Please sign in to comment.