Skip to content

Commit cc674ab

Browse files
Li Zetaoakpm00
authored andcommitted
mm/mmap: fix memory leak in mmap_region()
There is a memory leak reported by kmemleak: unreferenced object 0xffff88817231ce40 (size 224): comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff `..........B.... backtrace: [<ffffffff81936171>] __alloc_file+0x21/0x250 [<ffffffff81937051>] alloc_empty_file+0x41/0xf0 [<ffffffff81937159>] alloc_file+0x59/0x710 [<ffffffff81937964>] alloc_file_pseudo+0x154/0x210 [<ffffffff81741dbf>] __shmem_file_setup+0xff/0x2a0 [<ffffffff817502cd>] shmem_zero_setup+0x8d/0x160 [<ffffffff817cc1d5>] mmap_region+0x1075/0x19d0 [<ffffffff817cd257>] do_mmap+0x727/0x1110 [<ffffffff817518b2>] vm_mmap_pgoff+0x112/0x1e0 [<ffffffff83adf955>] do_syscall_64+0x35/0x80 [<ffffffff83c0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0 The root cause was traced to an error handing path in mmap_region() when arch_validate_flags() or mas_preallocate() fails. In the shared anonymous mapping sence, vma will be setuped and mapped with a new shared anonymous file via shmem_zero_setup(). So in this case, the file resource needs to be released. Fix it by calling fput(vma->vm_file) and unmap_region() when arch_validate_flags() or mas_preallocate() returns an error in the shared anonymous mapping sence. Link: https://lkml.kernel.org/r/[email protected] Fixes: d4af56c ("mm: start tracking VMAs with maple tree") Fixes: c462ac2 ("mm: Introduce arch_validate_flags()") Signed-off-by: Li Zetao <[email protected]> Reviewed-by: Liam R. Howlett <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 8625147 commit cc674ab

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

mm/mmap.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
26742674
error = -EINVAL;
26752675
if (file)
26762676
goto close_and_free_vma;
2677+
else if (vma->vm_file)
2678+
goto unmap_and_free_vma;
26772679
else
26782680
goto free_vma;
26792681
}
@@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
26822684
error = -ENOMEM;
26832685
if (file)
26842686
goto close_and_free_vma;
2687+
else if (vma->vm_file)
2688+
goto unmap_and_free_vma;
26852689
else
26862690
goto free_vma;
26872691
}
@@ -2751,7 +2755,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
27512755

27522756
/* Undo any partial mapping done by a device driver. */
27532757
unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
2754-
if (vm_flags & VM_SHARED)
2758+
if (file && (vm_flags & VM_SHARED))
27552759
mapping_unmap_writable(file->f_mapping);
27562760
free_vma:
27572761
vm_area_free(vma);

0 commit comments

Comments
 (0)