• Lorenzo Stoakes's avatar
    mm/mmap: correct error handling in mmap_region() · 74874c57
    Lorenzo Stoakes authored
    Commit f8d112a4 ("mm/mmap: avoid zeroing vma tree in mmap_region()")
    changed how error handling is performed in mmap_region().
    
    The error value defaults to -ENOMEM, but then gets reassigned immediately
    to the result of vms_gather_munmap_vmas() if we are performing a MAP_FIXED
    mapping over existing VMAs (and thus unmapping them).
    
    This overwrites the error value, potentially clearing it.
    
    After this, we invoke may_expand_vm() and possibly vm_area_alloc(), and
    check to see if they failed. If they do so, then we perform error-handling
    logic, but importantly, we do NOT update the error code.
    
    This means that, if vms_gather_munmap_vmas() succeeds, but one of these
    calls does not, the function will return indicating no error, but rather an
    address value of zero, which is entirely incorrect.
    
    Correct this and avoid future confusion by strictly setting error on each
    and every occasion we jump to the error handling logic, and set the error
    code immediately prior to doing so.
    
    This way we can see at a glance that the error code is always correct.
    
    Many thanks to Vegard Nossum who spotted this issue in discussion around
    this problem.
    
    Link: https://lkml.kernel.org/r/20241002073932.13482-1-lorenzo.stoakes@oracle.com
    Fixes: f8d112a4 ("mm/mmap: avoid zeroing vma tree in mmap_region()")
    Signed-off-by: default avatarLorenzo Stoakes <lorenzo.stoakes@oracle.com>
    Suggested-by: default avatarVegard Nossum <vegard.nossum@oracle.com>
    Reviewed-by: default avatarVlastimil Babka <vbabka@suse.cz>
    Cc: "Liam R. Howlett" <Liam.Howlett@oracle.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    74874c57
mmap.c 60.3 KB