Skip to contentSkip to conversation

vfio-user fixes

PATCH Superseded John Levon 3 participants 11 messages Jul 15, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

John Levon

author
#0 lore raw

v2 cover vfio-user fixes

Some small Coverity and related fixes for the recently merged vfio-user series.

thanks
john

John Levon (4):
hw/vfio-user: add Cédric Le Goater as a maintainer hw/vfio: fix region fd initialization hw/vfio-user: wait for proxy close correctly hw/vfio-user: fix use of uninitialized variable

 MAINTAINERS              |  1 +
 hw/vfio-user/container.c |  6 +-----
 hw/vfio-user/proxy.c     | 10 ++++++----
 hw/vfio/device.c         |  6 +++++-
 4 files changed, 13 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/4 hw/vfio-user: add Cédric Le Goater as a maintainer

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index e88ed2c0a9..30e9b71e6e 100644
@@ -4287,6 +4287,7 @@ F: tests/functional/test_multiprocess.py
 VFIO-USER:
 M: John Levon <[email protected]>
 M: Thanos Makatos <[email protected]>
+M: Cédric Le Goater <[email protected]>
 S: Supported
 F: docs/interop/vfio-user.rst
 F: docs/system/devices/vfio-user.rst
Signature · 1 line
2.43.0
Acked-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] hw/vfio-user: add Cédric Le Goater as a maintainer

Quoted 10 lines · John Levon
---
  MAINTAINERS | 1 +
  1 file changed, 1 insertion(+)
MAINTAINERS +0 -0
index e88ed2c0a9..30e9b71e6e 100644

MAINTAINERS F: tests/functional/test_multiprocess.py · line 4293

Quoted 7 lines
MAINTAINERS +1 -0
@@ -4287,6 +4287,7 @@ F: tests/functional/test_multiprocess.py
  VFIO-USER:
  M: John Levon <[email protected]>
  M: Thanos Makatos <[email protected]>
+M: Cédric Le Goater <[email protected]>
  S: Supported
  F: docs/interop/vfio-user.rst
  F: docs/system/devices/vfio-user.rst

ATB,

Mark.

author
#3 lore raw

PATCH v2 2/4 hw/vfio: fix region fd initialization

In reply to 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 -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
author
#4 lore raw

PATCH v2 3/4 hw/vfio-user: wait for proxy close correctly

In reply to John Levon

Coverity reported:

CID 1611806: Concurrent data access violations (BAD_CHECK_OF_WAIT_COND)

A wait is performed without a loop. If there is a spurious wakeup, the condition may not be satisfied.

Fix this by checking ->state for VFIO_PROXY_CLOSED in a loop.

Resolves: Coverity CID 1611806

Fixes 0b3d881a ("vfio-user: implement message receive infrastructure")
---
 hw/vfio-user/proxy.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio-user/proxy.c +6 -4
index c418954440..2275d3fe39 100644
@@ -32,7 +32,6 @@ static void vfio_user_recycle(VFIOUserProxy *proxy, VFIOUserMsg *msg);
 
 static void vfio_user_recv(void *opaque);
 static void vfio_user_send(void *opaque);
-static void vfio_user_cb(void *opaque);
 
 static void vfio_user_request(void *opaque);
 
@@ -492,7 +491,7 @@ static void vfio_user_send(void *opaque)
     }
 }
 
-static void vfio_user_cb(void *opaque)
+static void vfio_user_close_cb(void *opaque)
 {
     VFIOUserProxy *proxy = opaque;
 
@@ -984,8 +983,11 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
      * handler to run after the proxy fd handlers were
      * deleted above.
      */
-    aio_bh_schedule_oneshot(proxy->ctx, vfio_user_cb, proxy);
-    qemu_cond_wait(&proxy->close_cv, &proxy->lock);
+    aio_bh_schedule_oneshot(proxy->ctx, vfio_user_close_cb, proxy);
+
+    while (proxy->state != VFIO_PROXY_CLOSED) {
+        qemu_cond_wait(&proxy->close_cv, &proxy->lock);
+    }
 
     /* we now hold the only ref to proxy */
     qemu_mutex_unlock(&proxy->lock);
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v2 3/4] hw/vfio-user: wait for proxy close correctly

Quoted 4 lines · John Levon

Coverity reported:

CID 1611806: Concurrent data access violations (BAD_CHECK_OF_WAIT_COND)

Quoted 8 lines

A wait is performed without a loop. If there is a spurious wakeup, the condition may not be satisfied.

Fix this by checking ->state for VFIO_PROXY_CLOSED in a loop.

Resolves: Coverity CID 1611806

Fixes 0b3d881a ("vfio-user: implement message receive infrastructure")

Is this definitely the right patch? The v2 posted at https://patchew.org/QEMU/[email protected]/ contains the updated commit message mentioning the rename of the callback, whereas this one doesn't?

Quoted 29 lines
---
  hw/vfio-user/proxy.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
hw/vfio-user/proxy.c +1 -2
index c418954440..2275d3fe39 100644
@@ -32,7 +32,6 @@ static void vfio_user_recycle(VFIOUserProxy *proxy, VFIOUserMsg *msg);
 
  static void vfio_user_recv(void *opaque);
  static void vfio_user_send(void *opaque);
-static void vfio_user_cb(void *opaque);
 
  static void vfio_user_request(void *opaque);
 
@@ -492,7 +491,7 @@ static void vfio_user_send(void *opaque)
      }
  }
 
-static void vfio_user_cb(void *opaque)
+static void vfio_user_close_cb(void *opaque)
  {
      VFIOUserProxy *proxy = opaque;
 
@@ -984,8 +983,11 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)
       * handler to run after the proxy fd handlers were
       * deleted above.
       */

hw/vfio-user/proxy.c void vfio_user_disconnect(VFIOUserProxy *proxy) · line 993

Quoted 10 lines
hw/vfio-user/proxy.c +5 -2
-    aio_bh_schedule_oneshot(proxy->ctx, vfio_user_cb, proxy);
-    qemu_cond_wait(&proxy->close_cv, &proxy->lock);
+    aio_bh_schedule_oneshot(proxy->ctx, vfio_user_close_cb, proxy);
+
+    while (proxy->state != VFIO_PROXY_CLOSED) {
+        qemu_cond_wait(&proxy->close_cv, &proxy->lock);
+    }
 
      /* we now hold the only ref to proxy */
      qemu_mutex_unlock(&proxy->lock);

ATB,

Mark.

author
#6 lore raw

Re: [PATCH v2 3/4] hw/vfio-user: wait for proxy close correctly

Quoted 11 lines · Mark Cave-Ayland
Quoted 9 lines · John Levon

Coverity reported:

CID 1611806: Concurrent data access violations (BAD_CHECK_OF_WAIT_COND)

A wait is performed without a loop. If there is a spurious wakeup, the condition may not be satisfied.

Fix this by checking ->state for VFIO_PROXY_CLOSED in a loop.

Quoted 8 lines
Quoted 3 lines

Resolves: Coverity CID 1611806

Fixes 0b3d881a ("vfio-user: implement message receive infrastructure")

Is this definitely the right patch? The v2 posted at
https://patchew.org/QEMU/[email protected]/
contains the updated commit message mentioning the rename of the callback, whereas this one doesn't?

Yep, sorry, picked the wrong commit message (but same commit contents).

Should I resend?

regards
john

#7 lore raw

Re: [PATCH v2 3/4] hw/vfio-user: wait for proxy close correctly

Quoted 17 lines · John Levon
Quoted 15 lines · Mark Cave-Ayland
Quoted 12 lines · John Levon

Coverity reported:

CID 1611806: Concurrent data access violations (BAD_CHECK_OF_WAIT_COND)

A wait is performed without a loop. If there is a spurious wakeup, the condition may not be satisfied.

Fix this by checking ->state for VFIO_PROXY_CLOSED in a loop.

Resolves: Coverity CID 1611806

Fixes 0b3d881a ("vfio-user: implement message receive infrastructure")
Quoted 8 lines
Quoted 4 lines

Is this definitely the right patch? The v2 posted at
https://patchew.org/QEMU/[email protected]/
contains the updated commit message mentioning the rename of the callback, whereas this one doesn't?

Yep, sorry, picked the wrong commit message (but same commit contents).

Should I resend?

yep. Please pick up the trailers.

Thanks,

C.

author
#8 lore raw

PATCH v2 4/4 hw/vfio-user: fix use of uninitialized variable

In reply to John Levon

Coverity reported:

CID 1611805: Uninitialized variables

in vfio_user_dma_map(). This can occur in the happy path when ->async_ops was not set; as this doesn't typically happen, it wasn't caught during testing.

Align both map and unmap implementations to initialize ret the same way to resolve this.

Resolves: Coverity CID 1611805

Fixes 18e899e6 ("vfio-user: implement VFIO_USER_DMA_MAP/UNMAP")
---
 hw/vfio-user/container.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/vfio-user/container.c +1 -5
index d318e6a339..d589dd90f5 100644
@@ -64,8 +64,6 @@ static int vfio_user_dma_unmap(const VFIOContainerBase *bcontainer,
                               0, &local_err)) {
             error_report_err(local_err);
             ret = -EFAULT;
-        } else {
-            ret = 0;
         }
     } else {
         if (!vfio_user_send_wait(container->proxy, &msgp->hdr, NULL,
@@ -92,7 +90,7 @@ static int vfio_user_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
                                                 bcontainer);
     int fd = memory_region_get_fd(mrp);
     Error *local_err = NULL;
-    int ret;
+    int ret = 0;
 
     VFIOUserFDs *fds = NULL;
     VFIOUserDMAMap *msgp = g_malloc0(sizeof(*msgp));
@@ -135,8 +133,6 @@ static int vfio_user_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
                               0, &local_err)) {
             error_report_err(local_err);
             ret = -EFAULT;
-        } else {
-            ret = 0;
         }
     } else {
         VFIOUserFDs local_fds = { 1, 0, &fd };
Signature · 1 line
2.43.0
Reviewed-by on the v2 series
#9 lore raw
In reply to John Levon
Quoted 4 lines · John Levon

Some small Coverity and related fixes for the recently merged vfio-user series.

thanks
john

Mark,

Would you please re-add your R-b ?

and applied to vfio-next.

Thanks,

C.

Quoted 12 lines

John Levon (4):
hw/vfio-user: add Cédric Le Goater as a maintainer hw/vfio: fix region fd initialization hw/vfio-user: wait for proxy close correctly hw/vfio-user: fix use of uninitialized variable

  MAINTAINERS              |  1 +
  hw/vfio-user/container.c |  6 +-----
  hw/vfio-user/proxy.c     | 10 ++++++----
  hw/vfio/device.c         |  6 +++++-
  4 files changed, 13 insertions(+), 10 deletions(-)
#10 lore raw
Quoted 9 lines · Cédric Le Goater
Quoted 4 lines · John Levon

Some small Coverity and related fixes for the recently merged vfio-user series.

thanks
john

Mark,

Would you please re-add your R-b ?

No need. b4 picked them up.

Thanks,

C.

Quoted 23 lines

and applied to vfio-next.

Thanks,

C.

Quoted 12 lines

John Levon (4):
   hw/vfio-user: add Cédric Le Goater as a maintainer hw/vfio: fix region fd initialization hw/vfio-user: wait for proxy close correctly hw/vfio-user: fix use of uninitialized variable

  MAINTAINERS              |  1 +
  hw/vfio-user/container.c |  6 +-----
  hw/vfio-user/proxy.c     | 10 ++++++----
  hw/vfio/device.c         |  6 +++++-

  4 files changed, 13 insertions(+), 10 deletions(-)