• Jason Gunthorpe's avatar
    vfio: Simplify the lifetime logic for vfio_device · 5e42c999
    Jason Gunthorpe authored
    The vfio_device is using a 'sleep until all refs go to zero' pattern for
    its lifetime, but it is indirectly coded by repeatedly scanning the group
    list waiting for the device to be removed on its own.
    
    Switch this around to be a direct representation, use a refcount to count
    the number of places that are blocking destruction and sleep directly on a
    completion until that counter goes to zero. kfree the device after other
    accesses have been excluded in vfio_del_group_dev(). This is a fairly
    common Linux idiom.
    
    Due to this we can now remove kref_put_mutex(), which is very rarely used
    in the kernel. Here it is being used to prevent a zero ref device from
    being seen in the group list. Instead allow the zero ref device to
    continue to exist in the device_list and use refcount_inc_not_zero() to
    exclude it once refs go to zero.
    
    This patch is organized so the next patch will be able to alter the API to
    allow drivers to provide the kfree.
    Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
    Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
    Reviewed-by: default avatarCornelia Huck <cohuck@redhat.com>
    Reviewed-by: default avatarEric Auger <eric.auger@redhat.com>
    Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
    Message-Id: <2-v3-225de1400dfc+4e074-vfio1_jgg@nvidia.com>
    Signed-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
    5e42c999
vfio.c 60.7 KB