[PATCH] hw/vfio: fix region fd initialization

John Levon posted 1 patch 4 months, 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250710154842.1267253-1-john.levon@nutanix.com
Maintainers: Alex Williamson <alex.williamson@redhat.com>, "Cédric Le Goater" <clg@redhat.com>
hw/vfio/device.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
[PATCH] hw/vfio: fix region fd initialization
Posted by John Levon 4 months, 1 week ago
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")
Signed-off-by: John Levon <john.levon@nutanix.com>
---
 hw/vfio/device.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 96cf21462c..52a1996dc4 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -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);
-- 
2.43.0
Re: [PATCH] hw/vfio: fix region fd initialization
Posted by Cédric Le Goater 4 months ago
On 7/10/25 17:48, John Levon wrote:
> 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")
> Signed-off-by: John Levon <john.levon@nutanix.com>


Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.


> ---
>   hw/vfio/device.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/vfio/device.c b/hw/vfio/device.c
> index 96cf21462c..52a1996dc4 100644
> --- a/hw/vfio/device.c
> +++ b/hw/vfio/device.c
> @@ -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);


Re: [PATCH] hw/vfio: fix region fd initialization
Posted by Mark Cave-Ayland 4 months ago
On 10/07/2025 16:48, John Levon wrote:

> 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")
> Signed-off-by: John Levon <john.levon@nutanix.com>
> ---
>   hw/vfio/device.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/vfio/device.c b/hw/vfio/device.c
> index 96cf21462c..52a1996dc4 100644
> --- a/hw/vfio/device.c
> +++ b/hw/vfio/device.c
> @@ -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);

Reviewed-by: Mark Cave-Ayland <mark.caveayland@nutanix.com>


ATB,

Mark.