Skip to contentSkip to conversation

vfio: Fix unbalanced vfio_df_close call in no-iommu mode

PATCH Superseded Jacob Pan 2 participants 4 messages Jun 2, 2025 – Jun 3, 2025 VFIO driver

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jacob Pan

author
#0 lore raw

PATCH 1/2 vfio: Fix unbalanced vfio_df_close call in no-iommu mode

For no-iommu enabled devices working under IOMMUFD VFIO compat mode, the group open path does not call vfio_df_open() and the open_count is 0. So calling vfio_df_close() in the group close path will trigger warning in vfio_assert_device_open(device);

E.g. The following warning can be seen by running VFIO test.
https://github.com/awilliam/tests/blob/master/vfio-noiommu-pci-device-open.c
CONFIG_VFIO_CONTAINER = n

[   29.094781] vfio-pci 0000:02:01.0: vfio-noiommu device opened by user (vfio-noiommu-pc:164)
Failed to get device info
[   29.096540] ------------[ cut here ]------------
[   29.096616] WARNING: CPU: 1 PID: 164 at drivers/vfio/vfio_main.c:487 vfio_df_close+0xac/0xb4

This patch adds checks for no-iommu mode and open_count to skip calling vfio_df_close.

---
 drivers/vfio/group.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/vfio/group.c +4 -3
index c321d442f0da..834421149ffe 100644
@@ -238,12 +238,13 @@ void vfio_df_group_close(struct vfio_device_file *df)
 	mutex_lock(&device->group->group_lock);
 	mutex_lock(&device->dev_set->lock);
 
-	vfio_df_close(df);
-	df->iommufd = NULL;
-
 	if (device->open_count == 0)
 		vfio_device_put_kvm(device);
 
+	if (!vfio_device_is_noiommu(device))
+		vfio_df_close(df);
+
+	df->iommufd = NULL;
 	mutex_unlock(&device->dev_set->lock);
 	mutex_unlock(&device->group->group_lock);
 }
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 vfio: Prevent open_count decrement to negative

When vfio_df_close() is called with open_count=0, it triggers a warning in vfio_assert_device_open() but still decrements open_count to -1. This allows a subsequent open to incorrectly pass the open_count == 0 check, leading to unintended behavior, such as setting df->access_granted = true.

For example, running an IOMMUFD compat no-IOMMU device with VFIO tests
(https://github.com/awilliam/tests/blob/master/vfio-noiommu-pci-device-open.c)
results in a warning and a failed VFIO_GROUP_GET_DEVICE_FD ioctl on the first run, but the second run succeeds incorrectly.

Add checks to avoid decrementing open_count below zero

---
 drivers/vfio/vfio_main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vfio/vfio_main.c +2 -1
index 1fd261efc582..5046cae05222 100644
@@ -583,7 +583,8 @@ void vfio_df_close(struct vfio_device_file *df)
 
 	lockdep_assert_held(&device->dev_set->lock);
 
-	vfio_assert_device_open(device);
+	if (!vfio_assert_device_open(device))
+		return;
 	if (device->open_count == 1)
 		vfio_df_device_last_close(df);
 	device->open_count--;
Signature · 1 line
2.34.1
#2 lore raw
In reply to Jacob Pan
Quoted 13 lines · Jacob Pan

For no-iommu enabled devices working under IOMMUFD VFIO compat mode, the group open path does not call vfio_df_open() and the open_count is 0. So calling vfio_df_close() in the group close path will trigger warning in vfio_assert_device_open(device);

E.g. The following warning can be seen by running VFIO test.
https://github.com/awilliam/tests/blob/master/vfio-noiommu-pci-device-open.c
CONFIG_VFIO_CONTAINER = n

[   29.094781] vfio-pci 0000:02:01.0: vfio-noiommu device opened by user (vfio-noiommu-pc:164)
Failed to get device info
[   29.096540] ------------[ cut here ]------------
[   29.096616] WARNING: CPU: 1 PID: 164 at drivers/vfio/vfio_main.c:487 vfio_df_close+0xac/0xb4
Quoted 6 lines

This patch adds checks for no-iommu mode and open_count to skip calling vfio_df_close.

---
 drivers/vfio/group.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Did you mean to resend the original version?

Jason

author
#3 lore raw

Hi Jason,

Quoted 18 lines · On Mon, 2 Jun 2025 20:49:03 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 17 lines · Jacob Pan

For no-iommu enabled devices working under IOMMUFD VFIO compat mode, the group open path does not call vfio_df_open() and the open_count is 0. So calling vfio_df_close() in the group close path will trigger warning in vfio_assert_device_open(device);

E.g. The following warning can be seen by running VFIO test.
https://github.com/awilliam/tests/blob/master/vfio-noiommu-pci-device-open.c
CONFIG_VFIO_CONTAINER = n

[   29.094781] vfio-pci 0000:02:01.0: vfio-noiommu device opened by
user (vfio-noiommu-pc:164) Failed to get device info
[   29.096540] ------------[ cut here ]------------
[   29.096616] WARNING: CPU: 1 PID: 164 at

drivers/vfio/vfio_main.c:487 vfio_df_close+0xac/0xb4

This patch adds checks for no-iommu mode and open_count to skip calling vfio_df_close.

Quoted 6 lines
Quoted 4 lines
---
 drivers/vfio/group.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)  

Did you mean to resend the original version?

my mistake, just sent v2.

Thanks!

Jacob