From: Yi Liu Date: Tue, 18 Jul 2023 13:55:41 +0000 (-0700) Subject: vfio: Move vfio_device_group_unregister() to be the first operation in unregister X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=291872a533a2cdea8df1ae157987cd7ab3939421;p=linux.git vfio: Move vfio_device_group_unregister() to be the first operation in unregister This avoids endless vfio_device refcount increment by userspace, which would keep blocking the vfio_unregister_group_dev(). Reviewed-by: Jason Gunthorpe Tested-by: Nicolin Chen Tested-by: Matthew Rosato Tested-by: Yanting Jiang Tested-by: Shameer Kolothum Tested-by: Terrence Xu Tested-by: Zhenzhong Duan Signed-off-by: Yi Liu Link: https://lore.kernel.org/r/20230718135551.6592-17-yi.l.liu@intel.com Signed-off-by: Alex Williamson --- diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index c71c0d1a079fd..6d45caa1f9a03 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -332,6 +332,12 @@ void vfio_unregister_group_dev(struct vfio_device *device) bool interrupted = false; long rc; + /* + * Prevent new device opened by userspace via the + * VFIO_GROUP_GET_DEVICE_FD in the group path. + */ + vfio_device_group_unregister(device); + vfio_device_put_registration(device); rc = try_wait_for_completion(&device->comp); while (rc <= 0) { @@ -355,8 +361,6 @@ void vfio_unregister_group_dev(struct vfio_device *device) } } - vfio_device_group_unregister(device); - /* Balances device_add in register path */ device_del(&device->device);