• Bodo Stroesser's avatar
    scsi: target: tcmu: Fix memory leak caused by wrong uio usage · 8f33bb24
    Bodo Stroesser authored
    When user deletes a tcmu device via configFS, tcmu calls
    uio_unregister_device(). During that call uio resets its pointer to struct
    uio_info provided by tcmu. That means, after uio_unregister_device() uio
    will no longer execute any of the callbacks tcmu had set in uio_info.
    
    Especially, if userspace daemon still holds the corresponding uio device
    open or mmap'ed while tcmu calls uio_unregister_device(), uio will not call
    tcmu_release() when userspace finally closes and munmaps the uio device.
    
    Since tcmu does refcounting for the tcmu device in tcmu_open() and
    tcmu_release(), in the decribed case refcount does not drop to 0 and tcmu
    does not free tcmu device's resources.  In extreme cases this can cause
    memory leaking of up to 1 GB for a single tcmu device.
    
    After uio_unregister_device(), uio will reject every open, read, write,
    mmap from userspace with -EOI. But userspace daemon can still access the
    mmap'ed command ring and data area. Therefore tcmu should wait until
    userspace munmaps the uio device before it frees the resources, as we don't
    want to cause SIGSEGV or SIGBUS to user space.
    
    That said, current refcounting during tcmu_open and tcmu_release does not
    work correctly, and refcounting better should be done in the open and close
    callouts of the vm_operations_struct, which tcmu assigns to each mmap of
    the uio device (because it wants its own page fault handler).
    
    This patch fixes the memory leak by removing refcounting from tcmu_open and
    tcmu_close, and instead adding new tcmu_vma_open() and tcmu_vma_close()
    handlers that only do refcounting.
    
    Link: https://lore.kernel.org/r/20210218175039.7829-3-bostroesser@gmail.comReviewed-by: default avatarMike Christie <michael.christie@oracle.com>
    Signed-off-by: default avatarBodo Stroesser <bostroesser@gmail.com>
    Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
    8f33bb24
target_core_user.c 76.1 KB