Skip to contentSkip to conversation

vdpa: Merge all net_init_vhost_vdpa error goto

PATCH Applied Eugenio Pérez 4 participants 11 messages Aug 2, 2022 – Aug 4, 2022 Networking

lore
Share & tools
lore
#0 lore raw

cover vdpa: Merge all net_init_vhost_vdpa error goto

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

This series includes an already posted patch [1], because it must be applied on top of that.

[1] https://lists.nongnu.org/archive/html/qemu-devel/2022-08/msg00089.html

Eugenio Pérez (2):
vdpa: Fix file descriptor leak on get features error
vdpa: Merge all net_init_vhost_vdpa error goto

 net/vhost-vdpa.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 vdpa: Fix file descriptor leak on get features error

File descriptor vdpa_device_fd is not free in the case of returning error from vhost_vdpa_get_features. Fixing it by making all errors go to the same error path.

Resolves: Coverity CID 1490785

Fixes 8170ab3f43 ("vdpa: Extract get features part from vhost_vdpa_get_max_queue_pairs")
---
 net/vhost-vdpa.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/vhost-vdpa.c +2 -2
index 6abad276a6..303447a68e 100644
@@ -566,7 +566,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
     g_autofree NetClientState **ncs = NULL;
     g_autoptr(VhostIOVATree) iova_tree = NULL;
     NetClientState *nc;
-    int queue_pairs, r, i, has_cvq = 0;
+    int queue_pairs, r, i = 0, has_cvq = 0;
 
     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
     opts = &netdev->u.vhost_vdpa;
@@ -582,7 +582,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
 
     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
     if (unlikely(r < 0)) {
-        return r;
+        goto err;
     }
 
     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
Signature · 1 line
2.31.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] vdpa: Fix file descriptor leak on get features error

Quoted 26 lines · Eugenio Pérez

File descriptor vdpa_device_fd is not free in the case of returning error from vhost_vdpa_get_features. Fixing it by making all errors go to the same error path.

Resolves: Coverity CID 1490785

Fixes 8170ab3f43 ("vdpa: Extract get features part from vhost_vdpa_get_max_queue_pairs")
---
  net/vhost-vdpa.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/vhost-vdpa.c +1 -1
index 6abad276a6..303447a68e 100644
@@ -566,7 +566,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
      g_autofree NetClientState **ncs = NULL;
      g_autoptr(VhostIOVATree) iova_tree = NULL;
      NetClientState *nc;
-    int queue_pairs, r, i, has_cvq = 0;
+    int queue_pairs, r, i = 0, has_cvq = 0;
 
      assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
      opts = &netdev->u.vhost_vdpa;

net/vhost-vdpa.c int net_init_vhost_vdpa(const Netdev *netdev, const char *name, · line 588

Quoted 8 lines
net/vhost-vdpa.c +1 -1
@@ -582,7 +582,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
 
      r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
      if (unlikely(r < 0)) {
-        return r;
+        goto err;
      }
 
      queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,

I can take this one via the trivial branch.

Thanks,
Laurent

Reviewed-by
#3 lore raw

Re: [PATCH 1/2] vdpa: Fix file descriptor leak on get features error

Quoted 35 lines · Laurent Vivier
Quoted 25 lines · Eugenio Pérez

File descriptor vdpa_device_fd is not free in the case of returning error from vhost_vdpa_get_features. Fixing it by making all errors go to the same error path.

Resolves: Coverity CID 1490785

Fixes 8170ab3f43 ("vdpa: Extract get features part from vhost_vdpa_get_max_queue_pairs")
---
  net/vhost-vdpa.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/vhost-vdpa.c +1 -1
index 6abad276a6..303447a68e 100644
@@ -566,7 +566,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
      g_autofree NetClientState **ncs = NULL;
      g_autoptr(VhostIOVATree) iova_tree = NULL;
      NetClientState *nc;
-    int queue_pairs, r, i, has_cvq = 0;
+    int queue_pairs, r, i = 0, has_cvq = 0;
      assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
      opts = &netdev->u.vhost_vdpa;

net/vhost-vdpa.c int net_init_vhost_vdpa(const Netdev *netdev, const char *name, · line 586

Quoted 6 lines
net/vhost-vdpa.c +1 -1
@@ -582,7 +582,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
      r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
      if (unlikely(r < 0)) {
-        return r;
+        goto err;
      }
      queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
Quoted 4 lines

I can take this one via the trivial branch.

Thanks,
Laurent

Not sure why it's appropriate for trivial but sure if you like

#4 lore raw

Re: [PATCH 1/2] vdpa: Fix file descriptor leak on get features error

In reply to Eugenio Pérez

Le 02/08/2022 à 13:24, Eugenio Pérez a écrit :

Quoted 26 lines

File descriptor vdpa_device_fd is not free in the case of returning error from vhost_vdpa_get_features. Fixing it by making all errors go to the same error path.

Resolves: Coverity CID 1490785

Fixes 8170ab3f43 ("vdpa: Extract get features part from vhost_vdpa_get_max_queue_pairs")
---
  net/vhost-vdpa.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
net/vhost-vdpa.c +1 -1
index 6abad276a6..303447a68e 100644
@@ -566,7 +566,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
      g_autofree NetClientState **ncs = NULL;
      g_autoptr(VhostIOVATree) iova_tree = NULL;
      NetClientState *nc;
-    int queue_pairs, r, i, has_cvq = 0;
+    int queue_pairs, r, i = 0, has_cvq = 0;
 
      assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
      opts = &netdev->u.vhost_vdpa;

net/vhost-vdpa.c int net_init_vhost_vdpa(const Netdev *netdev, const char *name, · line 588

Quoted 8 lines
net/vhost-vdpa.c +1 -1
@@ -582,7 +582,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
 
      r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
      if (unlikely(r < 0)) {
-        return r;
+        goto err;
      }
 
      queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,

Applied to my trivial-patches branch.

Thanks,
Laurent

#5 lore raw

PATCH 2/2 vdpa: Merge all net_init_vhost_vdpa error goto

In reply to Eugenio Pérez

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

---
 net/vhost-vdpa.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/vhost-vdpa.c +2 -4
index 303447a68e..ac1810723c 100644
@@ -588,8 +588,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
                                                  &has_cvq, errp);
     if (queue_pairs < 0) {
-        qemu_close(vdpa_device_fd);
-        return queue_pairs;
+        goto err;
     }
 
     if (opts->x_svq) {
@@ -604,7 +603,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
         if (invalid_dev_features) {
             error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
                        invalid_dev_features);
-            goto err_svq;
+            goto err;
         }
 
         vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
@@ -640,7 +639,6 @@ err:
         }
     }
 
-err_svq:
     qemu_close(vdpa_device_fd);
 
     return -1;
Signature · 1 line
2.31.1
Reviewed-by on v1 2/2
#6 lore raw
Quoted 32 lines · Eugenio Pérez

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

---
  net/vhost-vdpa.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
net/vhost-vdpa.c +2 -3
index 303447a68e..ac1810723c 100644
@@ -588,8 +588,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
      queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
                                                   &has_cvq, errp);
      if (queue_pairs < 0) {
-        qemu_close(vdpa_device_fd);
-        return queue_pairs;
+        goto err;
      }
 
      if (opts->x_svq) {
@@ -604,7 +603,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
          if (invalid_dev_features) {
              error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
                         invalid_dev_features);
-            goto err_svq;
+            goto err;
          }
 
          vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);

net/vhost-vdpa.c err: · line 644

Quoted 7 lines
net/vhost-vdpa.c +0 -1
@@ -640,7 +639,6 @@ err:
          }
      }
 
-err_svq:
      qemu_close(vdpa_device_fd);
 
      return -1;
Reviewed-by
#7 lore raw
Quoted 5 lines · Laurent Vivier
Quoted 4 lines · Eugenio Pérez

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

qemu-trivial too I guess then?

Quoted 33 lines
Quoted 26 lines
---
  net/vhost-vdpa.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
net/vhost-vdpa.c +2 -3
index 303447a68e..ac1810723c 100644
@@ -588,8 +588,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
      queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
                                                   &has_cvq, errp);
      if (queue_pairs < 0) {
-        qemu_close(vdpa_device_fd);
-        return queue_pairs;
+        goto err;
      }
      if (opts->x_svq) {
@@ -604,7 +603,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
          if (invalid_dev_features) {
              error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
                         invalid_dev_features);
-            goto err_svq;
+            goto err;
          }
          vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);

net/vhost-vdpa.c err: · line 642

Quoted 5 lines
net/vhost-vdpa.c +0 -1
@@ -640,7 +639,6 @@ err:
          }
      }
-err_svq:
      qemu_close(vdpa_device_fd);
      return -1;
#8 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 5 lines · Laurent Vivier
Quoted 4 lines · Eugenio Pérez

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

qemu-trivial too I guess then?

For 7.1 or later?

Thanks,
Laurent

Quoted 37 lines
Quoted 33 lines
Quoted 26 lines
---
   net/vhost-vdpa.c | 6 ++----
   1 file changed, 2 insertions(+), 4 deletions(-)
net/vhost-vdpa.c +2 -3
index 303447a68e..ac1810723c 100644
@@ -588,8 +588,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
       queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
                                                    &has_cvq, errp);
       if (queue_pairs < 0) {
-        qemu_close(vdpa_device_fd);
-        return queue_pairs;
+        goto err;
       }
       if (opts->x_svq) {
@@ -604,7 +603,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
           if (invalid_dev_features) {
               error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
                          invalid_dev_features);
-            goto err_svq;
+            goto err;
           }
           vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);

net/vhost-vdpa.c err: · line 642

Quoted 5 lines
net/vhost-vdpa.c +0 -1
@@ -640,7 +639,6 @@ err:
           }
       }
-err_svq:
       qemu_close(vdpa_device_fd);
       return -1;
#9 lore raw
Quoted 9 lines · Laurent Vivier
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Laurent Vivier
Quoted 4 lines · Eugenio Pérez

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

Quoted 6 lines
Quoted 1 line

qemu-trivial too I guess then?

For 7.1 or later?

Thanks,
Laurent

I don't really care. Since you are merging this you get to decide.

Quoted 37 lines
Quoted 37 lines
Quoted 33 lines
Quoted 26 lines
---
   net/vhost-vdpa.c | 6 ++----
   1 file changed, 2 insertions(+), 4 deletions(-)
net/vhost-vdpa.c +2 -3
index 303447a68e..ac1810723c 100644
@@ -588,8 +588,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
       queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
                                                    &has_cvq, errp);
       if (queue_pairs < 0) {
-        qemu_close(vdpa_device_fd);
-        return queue_pairs;
+        goto err;
       }
       if (opts->x_svq) {
@@ -604,7 +603,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
           if (invalid_dev_features) {
               error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
                          invalid_dev_features);
-            goto err_svq;
+            goto err;
           }
           vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);

net/vhost-vdpa.c err: · line 642

Quoted 5 lines
net/vhost-vdpa.c +0 -1
@@ -640,7 +639,6 @@ err:
           }
       }
-err_svq:
       qemu_close(vdpa_device_fd);
       return -1;
#10 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 9 lines · Laurent Vivier
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Laurent Vivier
Quoted 4 lines · Eugenio Pérez

Few steps allocate new resources, and all of the allocated can be checked to avoid trying to free an invalid one.

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

qemu-trivial too I guess then?

For 7.1 or later?

Thanks,
Laurent

I don't really care. Since you are merging this you get to decide.

In my opinion it's better to leave that for a later revision.

I sent it to trivial because it only touches a few lines of code and it's easy to review even without knowledge of net or vdpa subsystem, but if it's not appropriate I can send future patches like this out of trivial.

Thanks!

Quoted 38 lines
Quoted 37 lines
Quoted 37 lines
Quoted 33 lines
Quoted 26 lines
---
   net/vhost-vdpa.c | 6 ++----
   1 file changed, 2 insertions(+), 4 deletions(-)
net/vhost-vdpa.c +2 -3
index 303447a68e..ac1810723c 100644
@@ -588,8 +588,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
       queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
                                                    &has_cvq, errp);
       if (queue_pairs < 0) {
-        qemu_close(vdpa_device_fd);
-        return queue_pairs;
+        goto err;
       }
       if (opts->x_svq) {
@@ -604,7 +603,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
           if (invalid_dev_features) {
               error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
                          invalid_dev_features);
-            goto err_svq;
+            goto err;
           }
           vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);

net/vhost-vdpa.c err: · line 642

Quoted 5 lines
net/vhost-vdpa.c +0 -1
@@ -640,7 +639,6 @@ err:
           }
       }
-err_svq:
       qemu_close(vdpa_device_fd);
       return -1;