Skip to contentSkip to conversation

Refactor device_set_realized to avoid resource leak.

PATCH Superseded [email protected] 2 participants 10 messages Sep 1, 2014 – Sep 2, 2014

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Refactor device_set_realized to avoid resource leak.

From: Gonglei <[email protected]>

after committing
[PATCH v6 0/9] Refactor PCI/SHPC/PCIE hotplug to use a more generic hotplug API

If devcie hotplugging failed, will casuse resource leak. This patch series include address resouce leak and two other issuses.

v3 -> v2:
Thanks for Peter's suggestion:
- add cleanup logic for set bus/child_bus realized/unrealized failed.
- change patch 1/3 commit message, add 'Reviewed-by' tag.

v2 -> v1:
- rewrite patch 1/3, using error_abort instead of local_err.
- rewrite patch 2/3, add cleanup logic for different error embranchment.
- rewrite title of patch 3/3, and a syntax fix.

Gonglei (3):
qdev: using error_abort instead of using local_err
qdev: add cleanup logic in device_set_realized() to avoid resource
leak
pcie: don't assert when hotplug a PCIe device with 'function != 0'

 hw/core/qdev.c | 77 +++++++++++++++++++++++++++++++++++++++++-----------------
 hw/pci/pcie.c  |  6 ++++-
 2 files changed, 60 insertions(+), 23 deletions(-)
Signature · 1 line
1.7.12.4
#1 lore raw

PATCH v3 1/3 qdev: using error_abort instead of using local_err

From: Gonglei <[email protected]>

This error can not happen normally. If it happens indicates something very wrong, we should abort QEMU. moreover, The user can only refer to /machine/peripheral, not /machine/unattached.

Meanwhile remove superfluous check about local_err.

---
 hw/core/qdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/core/qdev.c +2 -2
index da1ba48..4a1ac5b 100644
@@ -820,13 +820,13 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
     }
 
     if (value && !dev->realized) {
-        if (!obj->parent && local_err == NULL) {
+        if (!obj->parent) {
             static int unattached_count;
             gchar *name = g_strdup_printf("device[%d]", unattached_count++);
 
             object_property_add_child(container_get(qdev_get_machine(),
                                                     "/unattached"),
-                                      name, obj, &local_err);
+                                      name, obj, &error_abort);
             g_free(name);
         }
 
Signature · 1 line
1.7.12.4
#2 lore raw

PATCH v3 2/3 qdev: add cleanup logic in device_set_realized() to avoid resource leak

In reply to [email protected]

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73 ++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +53 -20
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             dc->realize(dev, &local_err);
         }
 
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
             hotplug_handler_plug(dev->parent_bus->hotplug_handler,
                                  dev, &local_err);
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
+        } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
             HotplugHandler *hotplug_ctrl;
             MachineState *machine = MACHINE(qdev_get_machine());
             MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
                                            dev->instance_id_alias,
                                            dev->alias_required_for_version);
         }
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true, "realized",
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true, "realized",
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;
             }
         }
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             object_property_set_bool(OBJECT(bus), false, "realized",
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",
+                                 NULL);
+    }
+    error_propagate(errp, local_err);
+    return;
+
+set_realized_fail:
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), false, "realized",
+                                 NULL);
+    }
+
+    if (qdev_get_vmsd(dev)) {
+        vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
+    }
+
+post_realize_fail:
+    if (dc->unrealize) {
+        dc->unrealize(dev, NULL);
+    }
+
+fail:
+    error_propagate(errp, local_err);
+    return;
 }
 
 static bool device_get_hotpluggable(Object *obj, Error **errp)
Signature · 1 line
1.7.12.4
#3 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 69 lines · On Mon, Sep 1, 2014 at 3:50 PM, <[email protected]> wrote:

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73 ++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +11 -8
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             dc->realize(dev, &local_err);
         }
 
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
             hotplug_handler_plug(dev->parent_bus->hotplug_handler,
                                  dev, &local_err);
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
+        } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {
             HotplugHandler *hotplug_ctrl;
             MachineState *machine = MACHINE(qdev_get_machine());
             MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev,
                                            dev->instance_id_alias,
                                            dev->alias_required_for_version);
         }
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true, "realized",

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 871

Quoted 10 lines
hw/core/qdev.c +6 -4
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true, "realized",
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;

should this label be child_realize_fail for clarity? The object is already realized by now so this name unqualified is a little misleading.

Quoted 29 lines
hw/core/qdev.c +5 -7
             }
         }
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
             object_property_set_bool(OBJECT(bus), false, "realized",
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 903

Quoted 10 lines
hw/core/qdev.c +6 -1
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:

child_unrealize_fail

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 905

Quoted 2 lines
hw/core/qdev.c +2 -0
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",

"true" doesn't look right to me. It means if the recursive unrealize botches it will re-realize all children? The idea behind my comment last spin was to forcefully unrealize all children regardless of early-iteration errors. I think set_realized_fail and set unrealized_fail should be the same logic. You should keep going with your cleanup on error rather than undo the cleanup already done.

Regards,
Peter

Quoted 31 lines
hw/core/qdev.c +23 -1
+                                 NULL);
+    }
+    error_propagate(errp, local_err);
+    return;
+
+set_realized_fail:
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), false, "realized",
+                                 NULL);
+    }
+
+    if (qdev_get_vmsd(dev)) {
+        vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
+    }
+
+post_realize_fail:
+    if (dc->unrealize) {
+        dc->unrealize(dev, NULL);
+    }
+
+fail:
+    error_propagate(errp, local_err);
+    return;
 }
 
 static bool device_get_hotpluggable(Object *obj, Error **errp)
--

1.7.12.4

#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 157 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Peter Crosthwaite
Sent: Monday, September 01, 2014 2:08 PM
To: Gonglei (Arei)
Cc: [email protected] Developers; Huangweidong (C); Michael S. Tsirkin; Luonengjun; Huangpeng (Peter); Igor Mammedov; Paolo Bonzini; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 20 lines · On Mon, Sep 1, 2014 at 3:50 PM, <[email protected]> wrote:

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.
Quoted 7 lines

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73

++++++++++++++++++++++++++++++++++++++++++----------------

hw/core/qdev.c static void device_set_realized(Object *obj, bool

Quoted 7 lines
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +0 -0
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj, bool

value, Error **errp)

Quoted 4 lines
hw/core/qdev.c +0 -1
             dc->realize(dev, &local_err);
         }
 
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -3
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
             hotplug_handler_plug(dev->parent_bus->hotplug_handler,
                                  dev, &local_err);
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

hw/core/qdev.c static void device_set_realized(Object *obj, bool · line 844

Quoted 1 line
hw/core/qdev.c +1 -0
+        } else if (object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

hw/core/qdev.c static void device_set_realized(Object *obj, bool

Quoted 4 lines
hw/core/qdev.c +0 -0
             HotplugHandler *hotplug_ctrl;
             MachineState *machine = MACHINE(qdev_get_machine());
             MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj, bool

value, Error **errp)

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -1
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1,

qdev_get_vmsd(dev), dev,

Quoted 1 line

dev->instance_id_alias,

Quoted 1 line

dev->alias_required_for_version);

Quoted 4 lines
         }
hw/core/qdev.c +0 -3
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true, "realized",

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 871

Quoted 10 lines
hw/core/qdev.c +6 -4
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true, "realized",
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;

should this label be child_realize_fail for clarity? The object is already realized by now so this name unqualified is a little misleading.

hw/core/qdev.c static void device_set_realized(Object *obj, bool

Quoted 9 lines
             }
         }
hw/core/qdev.c +2 -1
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj, bool

value, Error **errp)

Quoted 20 lines
hw/core/qdev.c +3 -6
             object_property_set_bool(OBJECT(bus), false, "realized",
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 903

Quoted 10 lines
hw/core/qdev.c +6 -1
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:

child_unrealize_fail

hw/core/qdev.c static void device_set_realized(Object *obj, bool · line 905

Quoted 2 lines
hw/core/qdev.c +2 -0
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",
Quoted 7 lines

"true" doesn't look right to me. It means if the recursive unrealize botches it will re-realize all children? The idea behind my comment last spin was to forcefully unrealize all children regardless of early-iteration errors. I think set_realized_fail and set unrealized_fail should be the same logic. You should keep going with your cleanup on error rather than undo the cleanup already done.

I knew your mean, but forgot to explain in pervious email, sorry for this. When one child unrealized failed, the interface will return error for user, so user think the device existed yet in QEMU, but not hotplugged. If we keep going with cleanup, the state saved by QEMU is not same with user's thought.

Best regards,
-Gonglei

#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 164 lines · Gonglei (Arei)
Quoted 157 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Peter Crosthwaite
Sent: Monday, September 01, 2014 2:08 PM
To: Gonglei (Arei)
Cc: [email protected] Developers; Huangweidong (C); Michael S. Tsirkin; Luonengjun; Huangpeng (Peter); Igor Mammedov; Paolo Bonzini; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 20 lines · On Mon, Sep 1, 2014 at 3:50 PM, <[email protected]> wrote:

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.
Quoted 7 lines

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73

++++++++++++++++++++++++++++++++++++++++++----------------

hw/core/qdev.c static void device_set_realized(Object *obj, bool

Quoted 7 lines
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +0 -0
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj, bool

value, Error **errp)

Quoted 4 lines
hw/core/qdev.c +0 -1
             dc->realize(dev, &local_err);
         }
 
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -3
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
             hotplug_handler_plug(dev->parent_bus->hotplug_handler,
                                  dev, &local_err);
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

hw/core/qdev.c static void device_set_realized(Object *obj, bool · line 844

Quoted 1 line
hw/core/qdev.c +1 -0
+        } else if (object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

hw/core/qdev.c static void device_set_realized(Object *obj, bool

Quoted 4 lines
hw/core/qdev.c +0 -0
             HotplugHandler *hotplug_ctrl;
             MachineState *machine = MACHINE(qdev_get_machine());
             MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj, bool

value, Error **errp)

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -1
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1,

qdev_get_vmsd(dev), dev,

Quoted 1 line

dev->instance_id_alias,

Quoted 1 line

dev->alias_required_for_version);

Quoted 4 lines
         }
hw/core/qdev.c +0 -3
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true, "realized",

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 871

Quoted 10 lines
hw/core/qdev.c +6 -4
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true, "realized",
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;

should this label be child_realize_fail for clarity? The object is already realized by now so this name unqualified is a little misleading.

hw/core/qdev.c static void device_set_realized(Object *obj, bool

Quoted 9 lines
             }
         }
hw/core/qdev.c +2 -1
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj, bool

value, Error **errp)

Quoted 20 lines
hw/core/qdev.c +3 -6
             object_property_set_bool(OBJECT(bus), false, "realized",
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 903

Quoted 10 lines
hw/core/qdev.c +6 -1
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:

child_unrealize_fail

hw/core/qdev.c static void device_set_realized(Object *obj, bool · line 905

Quoted 2 lines
hw/core/qdev.c +2 -0
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",
Quoted 7 lines

"true" doesn't look right to me. It means if the recursive unrealize botches it will re-realize all children? The idea behind my comment last spin was to forcefully unrealize all children regardless of early-iteration errors. I think set_realized_fail and set unrealized_fail should be the same logic. You should keep going with your cleanup on error rather than undo the cleanup already done.

Quoted 5 lines

I knew your mean, but forgot to explain in pervious email, sorry for this. When one child unrealized failed, the interface will return error for user, so user think the device existed yet in QEMU, but not hotplugged. If we keep going with cleanup, the state saved by QEMU is not same with user's thought.

This is also broken in original code right?

So there's an argument to say that this case would be a fatal error, i.e. the hotplug interface should be &error_abort ing on the unrealize path. I don't think Qdev has a chance of being able to guarantee that a botched recursive unrealize has no side-effects so hotplug framework has no ability for sane recovery in these cases.

Regards,
Peter

Quoted 3 lines

Best regards,
-Gonglei

#6 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 155 lines

From: [email protected] [mailto:[email protected]] On Behalf Of Peter Crosthwaite
Sent: Tuesday, September 02, 2014 6:51 AM

Quoted 28 lines
Quoted 28 lines · On Mon, Sep 1, 2014 at 3:50 PM, <[email protected]> wrote:
Quoted 27 lines

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73
Quoted 8 lines
Quoted 8 lines

++++++++++++++++++++++++++++++++++++++++++----------------

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 7 lines
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +0 -0
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj,

bool

Quoted 4 lines
Quoted 4 lines

value, Error **errp)

Quoted 3 lines
hw/core/qdev.c +0 -0
             dc->realize(dev, &local_err);
         }
 
Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 8 lines
hw/core/qdev.c +5 -2
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
 

hotplug_handler_plug(dev->parent_bus->hotplug_handler,

Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 3 lines
hw/core/qdev.c +0 -2
                                  dev, &local_err);
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

hw/core/qdev.c static void device_set_realized(Object *obj, · line 844

Quoted 1 line
hw/core/qdev.c +1 -0
+        } else if (object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

Quoted 2 lines
hw/core/qdev.c +0 -0
             HotplugHandler *hotplug_ctrl;
             MachineState *machine =

MACHINE(qdev_get_machine());

Quoted 2 lines
Quoted 2 lines

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 2 lines
hw/core/qdev.c +0 -0
             MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj,

bool

Quoted 13 lines
Quoted 13 lines

value, Error **errp)

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -1
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1,

qdev_get_vmsd(dev), dev,

Quoted 1 line
Quoted 7 lines
Quoted 7 lines

dev->instance_id_alias,

Quoted 1 line

dev->alias_required_for_version);

Quoted 4 lines
         }
hw/core/qdev.c +0 -3
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true,

"realized",

Quoted 15 lines
Quoted 15 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 871

Quoted 10 lines
hw/core/qdev.c +6 -4
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true, "realized",
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;

should this label be child_realize_fail for clarity? The object is already realized by now so this name unqualified is a little misleading.

Quoted 9 lines
Quoted 9 lines

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 9 lines
             }
         }
hw/core/qdev.c +2 -1
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj,

bool

Quoted 44 lines
Quoted 44 lines

value, Error **errp)

Quoted 20 lines
hw/core/qdev.c +3 -6
             object_property_set_bool(OBJECT(bus), false, "realized",
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 903

Quoted 10 lines
hw/core/qdev.c +6 -1
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:

child_unrealize_fail

hw/core/qdev.c static void device_set_realized(Object *obj, · line 905

Quoted 2 lines
hw/core/qdev.c +2 -0
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",

"true" doesn't look right to me. It means if the recursive unrealize botches it will re-realize all children? The idea behind my comment last spin was to forcefully unrealize all children regardless of early-iteration errors. I think set_realized_fail and set unrealized_fail should be the same logic. You should keep going with your cleanup on error rather than undo the cleanup already done.

Quoted 9 lines
Quoted 4 lines

I knew your mean, but forgot to explain in pervious email, sorry for this. When one child unrealized failed, the interface will return error for user, so user think the device existed yet in QEMU, but not hotplugged. If we keep going with cleanup, the state saved by QEMU is not same with

user's

Quoted 1 line

thought.

This is also broken in original code right?

Yes. Any child unrealize failed, would return error immediately.

Quoted 6 lines

So there's an argument to say that this case would be a fatal error, i.e. the hotplug interface should be &error_abort ing on the unrealize path. I don't think Qdev has a chance of being able to guarantee that a botched recursive unrealize has no side-effects so hotplug framework has no ability for sane recovery in these cases.

OK. But actually I more agree with using NULL instead of
using '&error_abort', we can chose to ignore this kind of error, but not abort.

What's your opinion? Thanks. :)

Best regards,
-Gonglei

#7 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 166 lines · Gonglei (Arei)
Quoted 155 lines

From: [email protected] [mailto:[email protected]] On Behalf Of Peter Crosthwaite
Sent: Tuesday, September 02, 2014 6:51 AM

Quoted 28 lines
Quoted 28 lines · On Mon, Sep 1, 2014 at 3:50 PM, <[email protected]> wrote:
Quoted 27 lines

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73
Quoted 8 lines
Quoted 8 lines

++++++++++++++++++++++++++++++++++++++++++----------------

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 7 lines
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +0 -0
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj,

bool

Quoted 4 lines
Quoted 4 lines

value, Error **errp)

Quoted 3 lines
hw/core/qdev.c +0 -0
             dc->realize(dev, &local_err);
         }
 
Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 8 lines
hw/core/qdev.c +5 -2
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler &&
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler) {
 

hotplug_handler_plug(dev->parent_bus->hotplug_handler,

Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 3 lines
hw/core/qdev.c +0 -2
                                  dev, &local_err);
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

hw/core/qdev.c static void device_set_realized(Object *obj, · line 844

Quoted 1 line
hw/core/qdev.c +1 -0
+        } else if (object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

Quoted 2 lines
hw/core/qdev.c +0 -0
             HotplugHandler *hotplug_ctrl;
             MachineState *machine =

MACHINE(qdev_get_machine());

Quoted 2 lines
Quoted 2 lines

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 2 lines
hw/core/qdev.c +0 -0
             MachineClass *mc = MACHINE_GET_CLASS(machine);
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj,

bool

Quoted 13 lines
Quoted 13 lines

value, Error **errp)

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -1
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1,

qdev_get_vmsd(dev), dev,

Quoted 1 line
Quoted 7 lines
Quoted 7 lines

dev->instance_id_alias,

Quoted 1 line

dev->alias_required_for_version);

Quoted 4 lines
         }
hw/core/qdev.c +0 -3
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true,

"realized",

Quoted 15 lines
Quoted 15 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 871

Quoted 10 lines
hw/core/qdev.c +6 -4
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true, "realized",
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;

should this label be child_realize_fail for clarity? The object is already realized by now so this name unqualified is a little misleading.

Quoted 9 lines
Quoted 9 lines

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 9 lines
             }
         }
hw/core/qdev.c +2 -1
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj,

bool

Quoted 44 lines
Quoted 44 lines

value, Error **errp)

Quoted 20 lines
hw/core/qdev.c +3 -6
             object_property_set_bool(OBJECT(bus), false, "realized",
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 903

Quoted 10 lines
hw/core/qdev.c +6 -1
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:

child_unrealize_fail

hw/core/qdev.c static void device_set_realized(Object *obj, · line 905

Quoted 2 lines
hw/core/qdev.c +2 -0
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",

"true" doesn't look right to me. It means if the recursive unrealize botches it will re-realize all children? The idea behind my comment last spin was to forcefully unrealize all children regardless of early-iteration errors. I think set_realized_fail and set unrealized_fail should be the same logic. You should keep going with your cleanup on error rather than undo the cleanup already done.

Quoted 9 lines
Quoted 4 lines

I knew your mean, but forgot to explain in pervious email, sorry for this. When one child unrealized failed, the interface will return error for user, so user think the device existed yet in QEMU, but not hotplugged. If we keep going with cleanup, the state saved by QEMU is not same with

user's

Quoted 1 line

thought.

This is also broken in original code right?

Yes. Any child unrealize failed, would return error immediately.

Quoted 9 lines
Quoted 6 lines

So there's an argument to say that this case would be a fatal error, i.e. the hotplug interface should be &error_abort ing on the unrealize path. I don't think Qdev has a chance of being able to guarantee that a botched recursive unrealize has no side-effects so hotplug framework has no ability for sane recovery in these cases.

OK. But actually I more agree with using NULL instead of
using '&error_abort', we can chose to ignore this kind of error, but not abort.

Yes, use NULL here, but my idea is that hotplug framework may use &error_abort if unrealization fail doesn't make sense in that context. That's out of scope of this series so don't need to worry about it.

Regards,
Peter

Quoted 4 lines

What's your opinion? Thanks. :)

Best regards,
-Gonglei

#8 lore raw

Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak

Quoted 196 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Peter Crosthwaite
Sent: Tuesday, September 02, 2014 3:00 PM
To: Gonglei (Arei)
Cc: Huangweidong (C); Michael S. Tsirkin; Luonengjun; [email protected] Developers; Huangpeng (Peter); Igor Mammedov; Paolo Bonzini; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH v3 2/3] qdev: add cleanup logic in device_set_realized() to avoid resource leak
Importance: High

Quoted 40 lines · Gonglei (Arei)
Quoted 40 lines

From: [email protected] [mailto:[email protected]] On Behalf Of Peter Crosthwaite
Sent: Tuesday, September 02, 2014 6:51 AM

Quoted 28 lines
Quoted 28 lines · On Mon, Sep 1, 2014 at 3:50 PM, <[email protected]> wrote:
Quoted 27 lines

From: Gonglei <[email protected]>

At present, this function doesn't have partial cleanup implemented, which will cause resource leak in some scenarios.

Example:

1. Assuming that "dc->realize(dev, &local_err)" execute successful and local_err == NULL;
2. Executing device hotplug in hotplug_handler_plug(), but failed (It is prone to occur). Then local_err != NULL;
3. error_propagate(errp, local_err) and return. But the resources which been allocated in dc->realize() will be leaked. Simple backtrace:
dc->realize()
|->device_realize

            |->pci_qdev_init()
                |->do_pci_register_device()
                |->etc.

Adding fuller cleanup logic which assure that function can goto appropriate error label as local_err population is detected as each relevant point.

---
 hw/core/qdev.c | 73
Quoted 8 lines
Quoted 8 lines

++++++++++++++++++++++++++++++++++++++++++----------------

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 7 lines
 1 file changed, 53 insertions(+), 20 deletions(-)
hw/core/qdev.c +0 -0
index 4a1ac5b..2a82cc1 100644
@@ -834,12 +834,14 @@ static void device_set_realized(Object *obj,

bool

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

value, Error **errp)

Quoted 4 lines
hw/core/qdev.c +0 -1
             dc->realize(dev, &local_err);
         }
 
-        if (dev->parent_bus && dev->parent_bus->hotplug_handler

&&

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 6 lines
hw/core/qdev.c +5 -1
-            local_err == NULL) {
+        if (local_err != NULL) {
+            goto fail;
+        }
+
+        if (dev->parent_bus && dev->parent_bus->hotplug_handler)

{

Quoted 34 lines
Quoted 34 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

hotplug_handler_plug(dev->parent_bus->hotplug_handler,

Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c

Quoted 3 lines
                                  dev, &local_err);
hw/core/qdev.c +0 -2
-        } else if (local_err == NULL &&
-                   object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

Quoted 1 line

+ } else if (object_dynamic_cast(qdev_get_machine(),

TYPE_MACHINE)) {

Quoted 2 lines

HotplugHandler *hotplug_ctrl;
MachineState *machine =

MACHINE(qdev_get_machine());

Quoted 2 lines
Quoted 2 lines

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 2 lines
             MachineClass *mc = MACHINE_GET_CLASS(machine);
hw/core/qdev.c +0 -0
@@ -852,21 +854,25 @@ static void device_set_realized(Object *obj,

bool

Quoted 20 lines
Quoted 20 lines

value, Error **errp)

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp)

Quoted 10 lines
hw/core/qdev.c +5 -1
             }
         }
 
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (local_err != NULL) {
+            goto post_realize_fail;
+        }
+
+        if (qdev_get_vmsd(dev)) {
             vmstate_register_with_alias_id(dev, -1,

qdev_get_vmsd(dev), dev,

Quoted 1 line

dev->instance_id_alias,

Quoted 1 line

dev->alias_required_for_version);

Quoted 4 lines
         }
hw/core/qdev.c +0 -3
-        if (local_err == NULL) {
-            QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-                object_property_set_bool(OBJECT(bus), true,
Quoted 8 lines
Quoted 8 lines

"realized",

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
hw/core/qdev.c +3 -4
-                                         &local_err);
-                if (local_err != NULL) {
-                    break;
-                }
+
+        QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+            object_property_set_bool(OBJECT(bus), true,

"realized",

Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 871

Quoted 3 lines
hw/core/qdev.c +3 -0
+                                     &local_err);
+            if (local_err != NULL) {
+                goto set_realized_fail;

should this label be child_realize_fail for clarity? The object is already realized by now so this name unqualified is a little misleading.

Quoted 9 lines
Quoted 9 lines

hw/core/qdev.c static void device_set_realized(Object *obj,

Quoted 9 lines
             }
         }
hw/core/qdev.c +2 -1
-        if (dev->hotplugged && local_err == NULL) {
+
+        if (dev->hotplugged) {
             device_reset(dev);
         }
         dev->pending_deleted_event = false;
@@ -875,24 +881,51 @@ static void device_set_realized(Object *obj,

bool

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

value, Error **errp)

Quoted 1 line
hw/core/qdev.c +0 -0
             object_property_set_bool(OBJECT(bus), false,

"realized",

Quoted 59 lines
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
Quoted 19 lines
hw/core/qdev.c +3 -6
                                      &local_err);
             if (local_err != NULL) {
-                break;
+                goto set_unrealized_fail;
             }
         }
-        if (qdev_get_vmsd(dev) && local_err == NULL) {
+        if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
-        if (dc->unrealize && local_err == NULL) {
+        if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
         dev->pending_deleted_event = true;
-    }
 
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);

hw/core/qdev.c static void device_set_realized(Object *obj, bool value, Error **errp) · line 903

Quoted 10 lines
hw/core/qdev.c +6 -1
-        return;
+        if (local_err != NULL) {
+            goto fail;
+        }
     }
 
     dev->realized = value;
+    return;
+
+set_unrealized_fail:

child_unrealize_fail

hw/core/qdev.c static void device_set_realized(Object *obj, · line 905

Quoted 2 lines
hw/core/qdev.c +2 -0
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        object_property_set_bool(OBJECT(bus), true, "realized",

"true" doesn't look right to me. It means if the recursive unrealize botches it will re-realize all children? The idea behind my comment last spin was to forcefully unrealize all children regardless of early-iteration errors. I think set_realized_fail and set unrealized_fail should be the same logic. You should keep going with your cleanup on error rather than undo the cleanup already done.

Quoted 9 lines
Quoted 4 lines

I knew your mean, but forgot to explain in pervious email, sorry for this. When one child unrealized failed, the interface will return error for user, so user think the device existed yet in QEMU, but not hotplugged. If we keep going with cleanup, the state saved by QEMU is not same with

user's

Quoted 1 line

thought.

This is also broken in original code right?

Yes. Any child unrealize failed, would return error immediately.

Quoted 6 lines

So there's an argument to say that this case would be a fatal error, i.e. the hotplug interface should be &error_abort ing on the unrealize path. I don't think Qdev has a chance of being able to guarantee that a botched recursive unrealize has no side-effects so hotplug framework has no ability for sane recovery in these cases.

Quoted 8 lines
Quoted 3 lines

OK. But actually I more agree with using NULL instead of
using '&error_abort', we can chose to ignore this kind of error, but not abort.

Yes, use NULL here, but my idea is that hotplug framework may use &error_abort if unrealization fail doesn't make sense in that context. That's out of scope of this series so don't need to worry about it.

Got it. Thank you so much!

V4 will be posted.

Best regards,
-Gonglei

Quoted 7 lines

Regards,
Peter

Quoted 4 lines

What's your opinion? Thanks. :)

Best regards,
-Gonglei

#9 lore raw

PATCH v3 3/3 pcie: don't assert when hotplug a PCIe device with 'function != 0'

In reply to [email protected]

From: Gonglei <[email protected]>

It's enough to report an error. Assert() is not acceptable because the error is not a fatal error.

---
 hw/pci/pcie.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/pci/pcie.c +5 -1
index 1babddf..ab7f8a2 100644
@@ -254,7 +254,11 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
      * Right now, only a device of function = 0 is allowed to be
      * hot plugged/unplugged.
      */
-    assert(PCI_FUNC(pci_dev->devfn) == 0);
+    if (PCI_FUNC(pci_dev->devfn) != 0) {
+        error_setg(errp, "Unsupported device function %d for PCIe hotplugging, "
+                   "only supported function 0", PCI_FUNC(pci_dev->devfn));
+        return;
+    }
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
Signature · 1 line
1.7.12.4