Skip to contentSkip to conversation

vfio: cdev: Reject duplicate bind before updating KVM file

PATCH In review Aneesh Kumar K.V (Arm) 3 participants 3 messages Oct 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio: cdev: Reject duplicate bind before updating KVM file

The cdev path only supports one bound/open device fd, but VFIO_DEVICE_BIND_IOMMUFD only checked the per-file access_granted flag before capturing the KVM file reference. A second fd for the same device could therefore replace device->kvm, fail later in vfio_df_open() because open_count is already nonzero, and then clear the active KVM association during error cleanup.

Reject the bind while holding dev_set->lock if the device is already open, matching the existing cdev single-open rule before touching the device-wide KVM state.

---
 drivers/vfio/device_cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/device_cdev.c +2 -2
index 1d9515c967b0..ab46563604ac 100644
@@ -124,8 +124,8 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
 		return ret;
 
 	mutex_lock(&device->dev_set->lock);
-	/* one device cannot be bound twice */
-	if (df->access_granted) {
+	/* The cdev path only supports one bound/open device fd. */
+	if (df->access_granted || device->open_count) {
 		ret = -EINVAL;
 		goto out_unlock;
 	}
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] vfio: UAPI breakage returning -EINVAL instead of -EBUSY on duplicate bind --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] vfio: UAPI breakage returning -EINVAL instead of -EBUSY on duplicate bind --

commit c22368f571a5e2ac9cc22bf312688fe247d99cd9
Author: Aneesh Kumar K.V (Arm) <[email protected]>

vfio: cdev: Reject duplicate bind before updating KVM file

This commit prevents a second file descriptor for the same device from overwriting the KVM reference. It does this by checking device->open_count earlier during the bind operation.

Quoted 5 lines
drivers/vfio/device_cdev.c +0 -0
index 30362936c7b59..0f23ccb780e05 100644

drivers/vfio/device_cdev.c long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, · line 131

Quoted 10 lines
drivers/vfio/device_cdev.c +2 -2
@@ -124,8 +124,8 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
 		return ret;
 
 	mutex_lock(&device->dev_set->lock);
-	/* one device cannot be bound twice */
-	if (df->access_granted) {
+	/* The cdev path only supports one bound/open device fd. */
+	if (df->access_granted || device->open_count) {
 		ret = -EINVAL;
 		goto out_unlock;
 	}

[Severity: High]
Does this change unintentionally alter the UAPI by returning -EINVAL instead of -EBUSY when device->open_count is non-zero?

Further down in drivers/vfio/device_cdev.c:vfio_df_ioctl_bind_iommufd(), there is an explicit check intended to handle transient opens:

	if (device->open_count) {
		ret = -EBUSY;
		goto out_unlock;
	}

By checking device->open_count in the first conditional block and returning -EINVAL, the second block returning -EBUSY becomes dead code. Will this cause applications relying on -EBUSY to trigger a retry to fail unexpectedly?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 24 lines · On Thu, 1 Oct 2026 15:01:52 +0530 "Aneesh Kumar K.V (Arm)"…

The cdev path only supports one bound/open device fd, but VFIO_DEVICE_BIND_IOMMUFD only checked the per-file access_granted flag before capturing the KVM file reference. A second fd for the same device could therefore replace device->kvm, fail later in vfio_df_open() because open_count is already nonzero, and then clear the active KVM association during error cleanup.

Reject the bind while holding dev_set->lock if the device is already open, matching the existing cdev single-open rule before touching the device-wide KVM state.

---
 drivers/vfio/device_cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/device_cdev.c +0 -0
index 1d9515c967b0..ab46563604ac 100644

drivers/vfio/device_cdev.c long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, · line 131

Quoted 10 lines
drivers/vfio/device_cdev.c +2 -2
@@ -124,8 +124,8 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
 		return ret;
 
 	mutex_lock(&device->dev_set->lock);
-	/* one device cannot be bound twice */
-	if (df->access_granted) {
+	/* The cdev path only supports one bound/open device fd. */
+	if (df->access_granted || device->open_count) {
 		ret = -EINVAL;
 		goto out_unlock;
 	}

This is already addressed by [1], committed as [2].

[1]https://lore.kernel.org/all/[email protected]/ [2]https://github.com/awilliam/linux-vfio/commit/258ba46543abdd36844f04bba8b4022a5731d873

Thanks,
Alex