Re: [PATCH v3] vfio: fix potential deadlock on vfio group lock

From: Jason Gunthorpe
Date: Fri Jan 13 2023 - 13:53:06 EST


On Fri, Jan 13, 2023 at 12:11:32PM -0500, Matthew Rosato wrote:
> @@ -462,9 +520,19 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device)
> static int vfio_device_fops_release(struct inode *inode, struct file *filep)
> {
> struct vfio_device *device = filep->private_data;
> + struct kvm *kvm = NULL;
>
> vfio_device_group_close(device);
>
> + mutex_lock(&device->dev_set->lock);
> + if (device->open_count == 0 && device->kvm) {
> + kvm = device->kvm;
> + device->kvm = NULL;
> + }
> + mutex_unlock(&device->dev_set->lock);

This still doesn't seem right, another thread could have incr'd the
open_count already

This has to be done at the moment open_count is decremented to zero,
while still under the original lock.

Jason