Skip to contentSkip to conversation

hw/vfio: fix region fd initialization

PATCH In review John Levon 3 participants 3 messages Jul 10, 2025 – Jul 15, 2025

lore
Share & tools
lore

John Levon

author
#0 lore raw

PATCH 1/1 hw/vfio: fix region fd initialization

We were not initializing the region fd array to -1, so we would accidentally try to close(0) on cleanup for any region that is not referenced.

Fixes 95cdb024 ("vfio: add region info cache")
---
 hw/vfio/device.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/vfio/device.c +5 -1
index 96cf21462c..52a1996dc4 100644
@@ -463,6 +463,8 @@ void vfio_device_detach(VFIODevice *vbasedev)
 void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                          struct vfio_device_info *info)
 {
+    int i;
+
     vbasedev->num_irqs = info->num_irqs;
     vbasedev->num_regions = info->num_regions;
     vbasedev->flags = info->flags;
@@ -477,6 +479,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                                vbasedev->num_regions);
     if (vbasedev->use_region_fds) {
         vbasedev->region_fds = g_new0(int, vbasedev->num_regions);
+        for (i = 0; i < vbasedev->num_regions; i++) {
+            vbasedev->region_fds[i] = -1;
+        }
     }
 }
 
@@ -489,7 +494,6 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
         if (vbasedev->region_fds != NULL && vbasedev->region_fds[i] != -1) {
             close(vbasedev->region_fds[i]);
         }
-
     }
 
     g_clear_pointer(&vbasedev->reginfo, g_free);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 34 lines · John Levon

We were not initializing the region fd array to -1, so we would accidentally try to close(0) on cleanup for any region that is not referenced.

Fixes 95cdb024 ("vfio: add region info cache")
---
  hw/vfio/device.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
hw/vfio/device.c +5 -0
index 96cf21462c..52a1996dc4 100644
@@ -463,6 +463,8 @@ void vfio_device_detach(VFIODevice *vbasedev)
  void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                           struct vfio_device_info *info)
  {
+    int i;
+
      vbasedev->num_irqs = info->num_irqs;
      vbasedev->num_regions = info->num_regions;
      vbasedev->flags = info->flags;
@@ -477,6 +479,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                                 vbasedev->num_regions);
      if (vbasedev->use_region_fds) {
          vbasedev->region_fds = g_new0(int, vbasedev->num_regions);
+        for (i = 0; i < vbasedev->num_regions; i++) {
+            vbasedev->region_fds[i] = -1;
+        }
      }
  }
 

hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 499

Quoted 7 lines
hw/vfio/device.c +0 -1
@@ -489,7 +494,6 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
          if (vbasedev->region_fds != NULL && vbasedev->region_fds[i] != -1) {
              close(vbasedev->region_fds[i]);
          }
-
      }
 
      g_clear_pointer(&vbasedev->reginfo, g_free);

ATB,

Mark.

Reviewed-by on v1 1/1
#2 lore raw
In reply to John Levon
Quoted 6 lines · John Levon

We were not initializing the region fd array to -1, so we would accidentally try to close(0) on cleanup for any region that is not referenced.

Fixes 95cdb024 ("vfio: add region info cache")

Thanks,

C.

Quoted 35 lines
---
  hw/vfio/device.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
hw/vfio/device.c +5 -1
index 96cf21462c..52a1996dc4 100644
@@ -463,6 +463,8 @@ void vfio_device_detach(VFIODevice *vbasedev)
  void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                           struct vfio_device_info *info)
  {
+    int i;
+
      vbasedev->num_irqs = info->num_irqs;
      vbasedev->num_regions = info->num_regions;
      vbasedev->flags = info->flags;
@@ -477,6 +479,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
                                 vbasedev->num_regions);
      if (vbasedev->use_region_fds) {
          vbasedev->region_fds = g_new0(int, vbasedev->num_regions);
+        for (i = 0; i < vbasedev->num_regions; i++) {
+            vbasedev->region_fds[i] = -1;
+        }
      }
  }
 
@@ -489,7 +494,6 @@ void vfio_device_unprepare(VFIODevice *vbasedev)
          if (vbasedev->region_fds != NULL && vbasedev->region_fds[i] != -1) {
              close(vbasedev->region_fds[i]);
          }
-
      }
 
      g_clear_pointer(&vbasedev->reginfo, g_free);