Skip to contentSkip to conversation

net/virtio: fix dev_unplug_pending

PATCH Superseded Jens Freimann 2 participants 10 messages Nov 14, 2019 – Nov 20, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 net/virtio: fix dev_unplug_pending

.dev_unplug_pending is set up by virtio-net code indepent of whether failover=on was set for the device or not. This gives a wrong result when we check for existing primary devices in migration code.

Fix this by actually calling dev_unplug_pending() instead of just checking if the function pointer was set. When the feature was not negotiated dev_unplug_pending() will always return false. This prevents us from going into the wait-unplug state when there's no primary device present.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 3 +++
 migration/savevm.c  | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +3 -0
index 97a5113f7e..946039c0dc 100644
@@ -3124,6 +3124,9 @@ static bool primary_unplug_pending(void *opaque)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIONet *n = VIRTIO_NET(vdev);
 
+    if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
+        return false;
+    }
     return n->primary_dev ? n->primary_dev->pending_deleted_event : false;
 }
 
migration/savevm.c +2 -1
index 966a9c3bdb..a71b930b91 100644
@@ -1119,7 +1119,8 @@ int qemu_savevm_nr_failover_devices(void)
     int n = 0;
 
     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
-        if (se->vmsd && se->vmsd->dev_unplug_pending) {
+        if (se->vmsd && se->vmsd->dev_unplug_pending &&
+            se->vmsd->dev_unplug_pending(se->opaque)) {
             n++;
         }
     }
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/4 net/virtio: return early when failover primary alread added

Bail out when primary device was already added before. This avoids printing a wrong warning message during reboot.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/net/virtio-net.c +4 -0
index 946039c0dc..ac4d19109e 100644
@@ -759,6 +759,10 @@ static void failover_add_primary(VirtIONet *n, Error **errp)
 {
     Error *err = NULL;
 
+    if (n->primary_dev) {
+        return;
+    }
+
     n->primary_device_opts = qemu_opts_find(qemu_find_opts("device"),
             n->primary_device_id);
     if (n->primary_device_opts) {
Signature · 1 line
2.21.0
Reviewed-by on v1 2/4
#2 lore raw

Re: [PATCH 2/4] net/virtio: return early when failover primary alread added

Quoted 5 lines · Jens Freimann

Bail out when primary device was already added before. This avoids printing a wrong warning message during reboot.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
Quoted 21 lines
---
 hw/net/virtio-net.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/net/virtio-net.c +4 -0
index 946039c0dc..ac4d19109e 100644
@@ -759,6 +759,10 @@ static void failover_add_primary(VirtIONet *n, Error **errp)
 {
     Error *err = NULL;
 
+    if (n->primary_dev) {
+        return;
+    }
+
     n->primary_device_opts = qemu_opts_find(qemu_find_opts("device"),
             n->primary_device_id);
     if (n->primary_device_opts) {
Signature · 1 line
2.21.0
#3 lore raw

PATCH 3/4 net/virtio: avoid passing NULL to qdev_set_parent_bus

In reply to Jens Freimann

Make sure no arguments for qdev_set_parent_bus are NULL. This fixes CID 1407224.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +10 -4
index ac4d19109e..81650d4dc0 100644
@@ -2809,8 +2809,16 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
     if (n->primary_device_opts) {
         if (n->primary_dev) {
             n->primary_bus = n->primary_dev->parent_bus;
+            if (n->primary_bus) {
+                qdev_set_parent_bus(n->primary_dev, n->primary_bus);
+            } else  {
+                error_setg(errp, "virtio_net: couldn't set bus for primary");
+                goto out;
+            }
+        } else {
+            error_setg(errp, "virtio_net: couldn't find primary device");
+            goto out;
         }
-        qdev_set_parent_bus(n->primary_dev, n->primary_bus);
         n->primary_should_be_hidden = false;
         qemu_opt_set_bool(n->primary_device_opts,
                 "partially_hotplugged", true, errp);
@@ -2819,10 +2827,8 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
             hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, errp);
             hotplug_handler_plug(hotplug_ctrl, n->primary_dev, errp);
         }
-        if (!n->primary_dev) {
-            error_setg(errp, "virtio_net: couldn't find primary device");
-        }
     }
+out:
     return *errp != NULL;
 }
 
Signature · 1 line
2.21.0
#4 lore raw

Re: [PATCH 3/4] net/virtio: avoid passing NULL to qdev_set_parent_bus

Quoted 17 lines · Jens Freimann

Make sure no arguments for qdev_set_parent_bus are NULL. This fixes CID 1407224.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +0 -0
index ac4d19109e..81650d4dc0 100644
@@ -2809,8 +2809,16 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
     if (n->primary_device_opts) {
         if (n->primary_dev) {
             n->primary_bus = n->primary_dev->parent_bus;

hw/net/virtio-net.c static bool failover_replug_primary(VirtIONet *n, Error **errp) · line 2821

Quoted 10 lines
hw/net/virtio-net.c +9 -0
+            if (n->primary_bus) {
+                qdev_set_parent_bus(n->primary_dev, n->primary_bus);
+            } else  {
+                error_setg(errp, "virtio_net: couldn't set bus for primary");
+                goto out;
+            }
+        } else {
+            error_setg(errp, "virtio_net: couldn't find primary device");
+            goto out;
         }

A bit less messy:

if (!n->primary_dev) {
            error_setg(errp, "virtio_net: couldn't find primary device");
            goto err;
}

n->primary_bus = n->primary_dev->parent_bus;

if (!n->primary_bus) {
      error_setg(errp, "virtio_net: couldn't find primary device");
          goto err;
}

qdev_set_parent_bus(n->primary_dev, n->primary_bus);

err:

	return false;

also is it valid for primary_device_opts to not be present at all? Maybe we should check that too.

Quoted 5 lines
hw/net/virtio-net.c +0 -1
-        qdev_set_parent_bus(n->primary_dev, n->primary_bus);
         n->primary_should_be_hidden = false;
         qemu_opt_set_bool(n->primary_device_opts,
                 "partially_hotplugged", true, errp);

hw/net/virtio-net.c static bool failover_replug_primary(VirtIONet *n, Error **errp) · line 2833

Quoted 10 lines
hw/net/virtio-net.c +1 -3
@@ -2819,10 +2827,8 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
             hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, errp);
             hotplug_handler_plug(hotplug_ctrl, n->primary_dev, errp);
         }
-        if (!n->primary_dev) {
-            error_setg(errp, "virtio_net: couldn't find primary device");
-        }
     }
+out:
     return *errp != NULL;
 }

I'd handle errors separately from good path.
BTW I don't understand something here:
*errp != NULL is true on error, no?
Why are we returning success?
Confused.
Just return true would be cleaner imho.

Quoted 3 lines
Signature · 2 lines
-- 
2.21.0
#5 lore raw

Re: [PATCH 3/4] net/virtio: avoid passing NULL to qdev_set_parent_bus

Quoted 67 lines · Michael S. Tsirkin
Quoted 17 lines · Jens Freimann

Make sure no arguments for qdev_set_parent_bus are NULL. This fixes CID 1407224.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +0 -0
index ac4d19109e..81650d4dc0 100644
@@ -2809,8 +2809,16 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
     if (n->primary_device_opts) {
         if (n->primary_dev) {
             n->primary_bus = n->primary_dev->parent_bus;

hw/net/virtio-net.c static bool failover_replug_primary(VirtIONet *n, Error **errp) · line 2821

Quoted 10 lines
hw/net/virtio-net.c +9 -0
+            if (n->primary_bus) {
+                qdev_set_parent_bus(n->primary_dev, n->primary_bus);
+            } else  {
+                error_setg(errp, "virtio_net: couldn't set bus for primary");
+                goto out;
+            }
+        } else {
+            error_setg(errp, "virtio_net: couldn't find primary device");
+            goto out;
         }

A bit less messy:

if (!n->primary_dev) {
           error_setg(errp, "virtio_net: couldn't find primary device");
           goto err;
}

n->primary_bus = n->primary_dev->parent_bus;

if (!n->primary_bus) {
     error_setg(errp, "virtio_net: couldn't find primary device");
         goto err;
}

qdev_set_parent_bus(n->primary_dev, n->primary_bus);

err:

	return false;

also is it valid for primary_device_opts to not be present at all? Maybe we should check that too.

Quoted 14 lines
hw/net/virtio-net.c +1 -4
-        qdev_set_parent_bus(n->primary_dev, n->primary_bus);
         n->primary_should_be_hidden = false;
         qemu_opt_set_bool(n->primary_device_opts,
                 "partially_hotplugged", true, errp);
@@ -2819,10 +2827,8 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
             hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, errp);
             hotplug_handler_plug(hotplug_ctrl, n->primary_dev, errp);
         }
-        if (!n->primary_dev) {
-            error_setg(errp, "virtio_net: couldn't find primary device");
-        }
     }
+out:
     return *errp != NULL;
Quoted 8 lines

hw/net/virtio-net.c static bool failover_replug_primary(VirtIONet *n, Error **errp) · line 2833

Quoted 1 line
hw/net/virtio-net.c +0 -0
 }

I'd handle errors separately from good path.
BTW I don't understand something here:
*errp != NULL is true on error, no?
Why are we returning success?
Confused.
Just return true would be cleaner imho.

I'll fix this and sent a new version as a single patch. As you said this is not really a series, just individual patches.

Thanks!

regards
Jens

#6 lore raw

Re: [PATCH 3/4] net/virtio: avoid passing NULL to qdev_set_parent_bus

Quoted 77 lines · Jens Freimann
Quoted 67 lines · Michael S. Tsirkin
Quoted 17 lines · Jens Freimann

Make sure no arguments for qdev_set_parent_bus are NULL. This fixes CID 1407224.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +0 -0
index ac4d19109e..81650d4dc0 100644
@@ -2809,8 +2809,16 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
     if (n->primary_device_opts) {
         if (n->primary_dev) {
             n->primary_bus = n->primary_dev->parent_bus;

hw/net/virtio-net.c static bool failover_replug_primary(VirtIONet *n, Error **errp) · line 2821

Quoted 10 lines
hw/net/virtio-net.c +9 -0
+            if (n->primary_bus) {
+                qdev_set_parent_bus(n->primary_dev, n->primary_bus);
+            } else  {
+                error_setg(errp, "virtio_net: couldn't set bus for primary");
+                goto out;
+            }
+        } else {
+            error_setg(errp, "virtio_net: couldn't find primary device");
+            goto out;
         }

A bit less messy:

if (!n->primary_dev) {
           error_setg(errp, "virtio_net: couldn't find primary device");
           goto err;
}

n->primary_bus = n->primary_dev->parent_bus;

if (!n->primary_bus) {
     error_setg(errp, "virtio_net: couldn't find primary device");
         goto err;
}

qdev_set_parent_bus(n->primary_dev, n->primary_bus);

err:

	return false;

also is it valid for primary_device_opts to not be present at all? Maybe we should check that too.

Quoted 14 lines
hw/net/virtio-net.c +1 -4
-        qdev_set_parent_bus(n->primary_dev, n->primary_bus);
         n->primary_should_be_hidden = false;
         qemu_opt_set_bool(n->primary_device_opts,
                 "partially_hotplugged", true, errp);
@@ -2819,10 +2827,8 @@ static bool failover_replug_primary(VirtIONet *n, Error **errp)
             hotplug_handler_pre_plug(hotplug_ctrl, n->primary_dev, errp);
             hotplug_handler_plug(hotplug_ctrl, n->primary_dev, errp);
         }
-        if (!n->primary_dev) {
-            error_setg(errp, "virtio_net: couldn't find primary device");
-        }
     }
+out:
     return *errp != NULL;
Quoted 8 lines

hw/net/virtio-net.c static bool failover_replug_primary(VirtIONet *n, Error **errp) · line 2833

Quoted 1 line
hw/net/virtio-net.c +0 -0
 }

I'd handle errors separately from good path.
BTW I don't understand something here:
*errp != NULL is true on error, no?
Why are we returning success?
Confused.
Just return true would be cleaner imho.

Quoted 7 lines

I'll fix this and sent a new version as a single patch. As you said this is not really a series, just individual patches.

Thanks!

regards
Jens

I'd just repost them all then. The series was also threaded weirdly (e.g. there's no cover letter instead patches 2 and on link to patch 1).

Signature · 1 line
MST
#7 lore raw

PATCH 4/4 net/virtio: return error when device_opts arg is NULL

In reply to Jens Freimann

device_opts could be NULL. Make sure we don't pass it to qemu_opts_to_dict. When we made sure it can't be NULL we can also remove it from the if condition.
This fixes CID 1407222.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
---
 hw/net/virtio-net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
hw/net/virtio-net.c +6 -3
index 81650d4dc0..d53aa5796b 100644
@@ -2878,9 +2878,12 @@ static int virtio_net_primary_should_be_hidden(DeviceListener *listener,
             QemuOpts *device_opts)
 {
     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
-    bool match_found;
-    bool hide;
+    bool match_found = false;
+    bool hide = false;
 
+    if (!device_opts) {
+        return -1;
+    }
     n->primary_device_dict = qemu_opts_to_qdict(device_opts,
             n->primary_device_dict);
     if (n->primary_device_dict) {
@@ -2888,7 +2891,7 @@ static int virtio_net_primary_should_be_hidden(DeviceListener *listener,
         n->standby_id = g_strdup(qdict_get_try_str(n->primary_device_dict,
                     "failover_pair_id"));
     }
-    if (device_opts && g_strcmp0(n->standby_id, n->netclient_name) == 0) {
+    if (g_strcmp0(n->standby_id, n->netclient_name) == 0) {
         match_found = true;
     } else {
         match_found = false;
Signature · 1 line
2.21.0
Reviewed-by on v1 4/4
#8 lore raw

Re: [PATCH 4/4] net/virtio: return error when device_opts arg is NULL

Quoted 7 lines · Jens Freimann

device_opts could be NULL. Make sure we don't pass it to qemu_opts_to_dict. When we made sure it can't be NULL we can also remove it from the if condition.
This fixes CID 1407222.

Fixes 9711cd0dfc3f ("net/virtio: add failover support")
Quoted 34 lines
---
 hw/net/virtio-net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
hw/net/virtio-net.c +6 -3
index 81650d4dc0..d53aa5796b 100644
@@ -2878,9 +2878,12 @@ static int virtio_net_primary_should_be_hidden(DeviceListener *listener,
             QemuOpts *device_opts)
 {
     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
-    bool match_found;
-    bool hide;
+    bool match_found = false;
+    bool hide = false;
 
+    if (!device_opts) {
+        return -1;
+    }
     n->primary_device_dict = qemu_opts_to_qdict(device_opts,
             n->primary_device_dict);
     if (n->primary_device_dict) {
@@ -2888,7 +2891,7 @@ static int virtio_net_primary_should_be_hidden(DeviceListener *listener,
         n->standby_id = g_strdup(qdict_get_try_str(n->primary_device_dict,
                     "failover_pair_id"));
     }
-    if (device_opts && g_strcmp0(n->standby_id, n->netclient_name) == 0) {
+    if (g_strcmp0(n->standby_id, n->netclient_name) == 0) {
         match_found = true;
     } else {
         match_found = false;
Signature · 1 line
2.21.0
Reviewed-by on v1 1/4
#9 lore raw
In reply to Jens Freimann
Quoted 10 lines · Jens Freimann

.dev_unplug_pending is set up by virtio-net code indepent of whether failover=on was set for the device or not. This gives a wrong result when we check for existing primary devices in migration code.

Fix this by actually calling dev_unplug_pending() instead of just checking if the function pointer was set. When the feature was not negotiated dev_unplug_pending() will always return false. This prevents us from going into the wait-unplug state when there's no primary device present.

Quoted 3 lines
Fixes 9711cd0dfc3f ("net/virtio: add failover support")

This isn't really a series, is it?
Just a bunch of independent patches...

anyway:

Quoted 35 lines
---
 hw/net/virtio-net.c | 3 +++
 migration/savevm.c  | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +3 -0
index 97a5113f7e..946039c0dc 100644
@@ -3124,6 +3124,9 @@ static bool primary_unplug_pending(void *opaque)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIONet *n = VIRTIO_NET(vdev);
 
+    if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
+        return false;
+    }
     return n->primary_dev ? n->primary_dev->pending_deleted_event : false;
 }
 
migration/savevm.c +2 -1
index 966a9c3bdb..a71b930b91 100644
@@ -1119,7 +1119,8 @@ int qemu_savevm_nr_failover_devices(void)
     int n = 0;
 
     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
-        if (se->vmsd && se->vmsd->dev_unplug_pending) {
+        if (se->vmsd && se->vmsd->dev_unplug_pending &&
+            se->vmsd->dev_unplug_pending(se->opaque)) {
             n++;
         }
     }
Signature · 1 line
2.21.0