• Jason Gunthorpe's avatar
    vfio: Simplify the life cycle of the group FD · b76c0eed
    Jason Gunthorpe authored
    Once userspace opens a group FD it is prevented from opening another
    instance of that same group FD until all the prior group FDs and users of
    the container are done.
    
    The first is done trivially by checking the group->opened during group FD
    open.
    
    However, things get a little weird if userspace creates a device FD and
    then closes the group FD. The group FD still cannot be re-opened, but this
    time it is because the group->container is still set and container_users
    is elevated by the device FD.
    
    Due to this mismatched lifecycle we have the
    vfio_group_try_dissolve_container() which tries to auto-free a container
    after the group FD is closed but the device FD remains open.
    
    Instead have the device FD hold onto a reference to the single group
    FD. This directly prevents vfio_group_fops_release() from being called
    when any device FD exists and makes the lifecycle model more
    understandable.
    
    vfio_group_try_dissolve_container() is removed as the only place a
    container is auto-deleted is during vfio_group_fops_release(). At this
    point the container_users is either 1 or 0 since all device FDs must be
    closed.
    
    Change group->opened to group->opened_file which points to the single
    struct file * that is open for the group. If the group->open_file is
    NULL then group->container == NULL.
    
    If all device FDs have closed then the group's notifier list must be
    empty.
    Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
    Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
    Tested-by: default avatarNicolin Chen <nicolinc@nvidia.com>
    Tested-by: default avatarMatthew Rosato <mjrosato@linux.ibm.com>
    Link: https://lore.kernel.org/r/5-v2-d035a1842d81+1bf-vfio_group_locking_jgg@nvidia.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
    b76c0eed
vfio.c 56 KB