Skip to content

Commit

Permalink
vfio: Fix container device registration life cycle
Browse files Browse the repository at this point in the history
In vfio_device_open(), vfio_device_container_register() is always called
when open_count == 1. On error, vfio_device_container_unregister() is
only called when open_count == 1 and close_device is set. This leaks a
registration for devices without a close_device implementation.

In vfio_device_fops_release(), vfio_device_container_unregister() is
called unconditionally. This can cause a device to be unregistered
multiple times.

Treating container device registration/unregistration uniformly (always
when open_count == 1) fixes both issues.

Fixes: ce4b465 ("vfio: Replace the DMA unmapping notifier with a callback")
Signed-off-by: Anthony DeRossi <ajderossi@gmail.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Yi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20221110014027.28780-2-ajderossi@gmail.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
  • Loading branch information
Anthony DeRossi authored and Alex Williamson committed Nov 10, 2022
1 parent f0c4d9f commit 7fdba00
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions drivers/vfio/vfio_main.c
Original file line number Diff line number Diff line change
@@ -801,8 +801,9 @@ static struct file *vfio_device_open(struct vfio_device *device)
err_close_device:
mutex_lock(&device->dev_set->lock);
mutex_lock(&device->group->group_lock);
if (device->open_count == 1 && device->ops->close_device) {
device->ops->close_device(device);
if (device->open_count == 1) {
if (device->ops->close_device)
device->ops->close_device(device);

vfio_device_container_unregister(device);
}
@@ -1017,10 +1018,12 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
mutex_lock(&device->dev_set->lock);
vfio_assert_device_open(device);
mutex_lock(&device->group->group_lock);
if (device->open_count == 1 && device->ops->close_device)
device->ops->close_device(device);
if (device->open_count == 1) {
if (device->ops->close_device)
device->ops->close_device(device);

vfio_device_container_unregister(device);
vfio_device_container_unregister(device);
}
mutex_unlock(&device->group->group_lock);
device->open_count--;
if (device->open_count == 0)

0 comments on commit 7fdba00

Please sign in to comment.