Skip to contentSkip to conversation

qom: improve reference counting and hotplug

RFC PATCH In review Anthony Liguori 6 participants 24 messages Aug 26, 2012 – Oct 9, 2012

lore
Share & tools
lore
#0 lore raw

cover qom: improve reference counting and hotplug

Right now, you need to pair up object_new with object_delete. This is impractical when using reference counting because we would like to ensure that object_unref() also frees memory when needed.

The first few patches fix this problem by introducing a release callback so that objects that need special release behavior (i.e. g_free) can do that.

Since link and child properties all hold references, in order to actually free an object, we need to break those links. User created devices end up as children of a container. But child properties cannot be removed which means there's no obvious way to remove the reference and ultimately free the object.

We introduce the concept of "nullable child" properties to solve this. This is a child property that can be broken by writing NULL to the child link. Today we set all /peripheral* children to be nullable so that they can be deleted by management tools.

In terms of modeling hotplug, we represent unplug by removing the object from the parent bus. We need to register a notifier for when this happens so that we can also remove the parent's child property to ultimately release the object.

Putting it all together, we have:

1) qmp_device_del will issue a callback to a device. The default callback will do a forced eject (which means writing NULL to the parent_bus link).

2) PCI hotplug is a bit more sophisticated in that it waits for the guest to do the ejection.

3) qmp_device_del will register an eject notifier such that the device gets completely removed.

There's a slightly change in behavior here. A device is not automatically destroyed based on a guest initiated eject. A management tool must explicitly break the parent's link to the child in order for the device to disappear completely. device_del behaves exactly as it does today though.

This is an RFC. I've tested the series quite a lot (it was hard to get the reference counting right) but not enough to apply. I also don't think the series is quite split right and may not bisect cleanly.

I also want to write up a document describing object life cycle since admittedly the above is probably not that easy to follow.

I wanted to share this now though because it works and I think the concepts are right.

#1 lore raw

PATCH 1/9 savevm: don't rely on paths if we can store a DeviceState object

Paths break during tear down which can result in odd behavior since we do path based lookup during unregister (which happens at tear down).

For devices, just store the DeviceState directly and use that.

---
 savevm.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
savevm.c +4 -1
index c7fe283..bb6494b 100644
@@ -1177,6 +1177,7 @@ typedef struct SaveStateEntry {
     CompatEntry *compat;
     int no_migrate;
     int is_ram;
+    DeviceState *dev;
 } SaveStateEntry;
 
 
@@ -1235,6 +1236,7 @@ int register_savevm_live(DeviceState *dev,
     se->opaque = opaque;
     se->vmsd = NULL;
     se->no_migrate = 0;
+    se->dev = dev;
     /* if this is a live_savem then set is_ram */
     if (ops->save_live_setup != NULL) {
         se->is_ram = 1;
@@ -1298,7 +1300,8 @@ void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
     pstrcat(id, sizeof(id), idstr);
 
     QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
-        if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
+        if ((dev && se->dev == dev) ||
+            (strcmp(se->idstr, id) == 0 && se->opaque == opaque)) {
             QTAILQ_REMOVE(&savevm_handlers, se, entry);
             if (se->compat) {
                 g_free(se->compat);
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 2/9 object: automatically free objects based on a release function

In reply to Anthony Liguori

Now object_delete() simply has the semantics of unref'ing an object and unparenting it.

---
 include/qemu/object.h |    5 +++++
 qom/object.c          |   16 +++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletions(-)
include/qemu/object.h +5 -0
index cc75fee..487adcd 100644
@@ -242,6 +242,8 @@ struct ObjectClass
     GSList *interfaces;
 };
 
+typedef void (ObjectReleaseFunc)(Object *obj);
+
 /**
  * Object:
  *
@@ -264,6 +266,7 @@ struct Object
     QTAILQ_HEAD(, ObjectProperty) properties;
     uint32_t ref;
     Object *parent;
+    ObjectReleaseFunc *release;
 };
 
 /**
@@ -464,6 +467,8 @@ Object *object_new_with_type(Type type);
  */
 void object_delete(Object *obj);
 
+void object_set_release_func(Object *obj, ObjectReleaseFunc *func);
+
 /**
  * object_initialize_with_type:
  * @obj: A pointer to the memory to be used for the object.
qom/object.c +15 -1
index e3e9242..44135c3 100644
@@ -384,6 +384,20 @@ void object_finalize(void *data)
     object_property_del_all(obj);
 
     g_assert(obj->ref == 0);
+
+    if (obj->release) {
+        obj->release(obj);
+    }
+}
+
+void object_set_release_func(Object *obj, ObjectReleaseFunc *func)
+{
+    obj->release = func;
+}
+
+static void object_release_func(Object *obj)
+{
+    g_free(obj);
 }
 
 Object *object_new_with_type(Type type)
@@ -395,6 +409,7 @@ Object *object_new_with_type(Type type)
 
     obj = g_malloc(type->instance_size);
     object_initialize_with_type(obj, type);
+    object_set_release_func(obj, object_release_func);
     object_ref(obj);
 
     return obj;
@@ -412,7 +427,6 @@ void object_delete(Object *obj)
     object_unparent(obj);
     g_assert(obj->ref == 1);
     object_unref(obj);
-    g_free(obj);
 }
 
 Object *object_dynamic_cast(Object *obj, const char *typename)
Signature · 1 line
1.7.5.4
Acked-by on v1 2/9
#3 lore raw

Re: [Qemu-devel] [PATCH 2/9] object: automatically free objects based on a release function

Am 26.08.2012 17:51, schrieb Anthony Liguori:

Quoted 4 lines

Now object_delete() simply has the semantics of unref'ing an object and unparenting it.

/-F

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw

PATCH 3/9 qbus: remove glib_allocated/qom_allocated and use release hook to free memory

In reply to Anthony Liguori
---
 hw/pci.c    |    7 ++++++-
 hw/qdev.c   |   15 ---------------
 hw/qdev.h   |    7 -------
 hw/sysbus.c |    7 ++++++-
 4 files changed, 12 insertions(+), 24 deletions(-)
hw/pci.c +6 -1
index 4d95984..437af70 100644
@@ -292,6 +292,11 @@ void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
     vmstate_register(NULL, -1, &vmstate_pcibus, bus);
 }
 
+static void pci_bus_release(Object *obj)
+{
+    g_free(obj);
+}
+
 PCIBus *pci_bus_new(DeviceState *parent, const char *name,
                     MemoryRegion *address_space_mem,
                     MemoryRegion *address_space_io,
@@ -300,9 +305,9 @@ PCIBus *pci_bus_new(DeviceState *parent, const char *name,
     PCIBus *bus;
 
     bus = g_malloc0(sizeof(*bus));
-    bus->qbus.glib_allocated = true;
     pci_bus_new_inplace(bus, parent, name, address_space_mem,
                         address_space_io, devfn_min);
+    object_set_release_func(OBJECT(bus), pci_bus_release);
     return bus;
 }
 
hw/qdev.c +0 -15
index b5a52ac..6b61daa 100644
@@ -459,8 +459,6 @@ BusState *qbus_create(const char *typename, DeviceState *parent, const char *nam
     BusState *bus;
 
     bus = BUS(object_new(typename));
-    bus->qom_allocated = true;
-
     bus->parent = parent;
     bus->name = name ? g_strdup(name) : NULL;
     qbus_realize(bus);
@@ -468,18 +466,6 @@ BusState *qbus_create(const char *typename, DeviceState *parent, const char *nam
     return bus;
 }
 
-void qbus_free(BusState *bus)
-{
-    if (bus->qom_allocated) {
-        object_delete(OBJECT(bus));
-    } else {
-        object_finalize(OBJECT(bus));
-        if (bus->glib_allocated) {
-            g_free(bus);
-        }
-    }
-}
-
 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
 {
     BusClass *bc = BUS_GET_CLASS(bus);
@@ -698,7 +684,6 @@ static void device_finalize(Object *obj)
     if (dev->state == DEV_STATE_INITIALIZED) {
         while (dev->num_child_bus) {
             bus = QLIST_FIRST(&dev->child_bus);
-            qbus_free(bus);
         }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
hw/qdev.h +0 -7
index d699194..3561e3a 100644
@@ -106,17 +106,12 @@ typedef struct BusChild {
 
 /**
  * BusState:
- * @qom_allocated: Indicates whether the object was allocated by QOM.
- * @glib_allocated: Indicates whether the object was initialized in-place
- * yet is expected to be freed with g_free().
  */
 struct BusState {
     Object obj;
     DeviceState *parent;
     const char *name;
     int allow_hotplug;
-    bool qom_allocated;
-    bool glib_allocated;
     int max_index;
     QTAILQ_HEAD(ChildrenHead, BusChild) children;
     QLIST_ENTRY(BusState) sibling;
@@ -201,8 +196,6 @@ int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
 void qdev_reset_all(DeviceState *dev);
 void qbus_reset_all_fn(void *opaque);
 
-void qbus_free(BusState *bus);
-
 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)
 
 /* This should go away once we get rid of the NULL bus hack */
hw/sysbus.c +6 -1
index 9d8b1ea..8305337 100644
@@ -267,6 +267,11 @@ static TypeInfo sysbus_device_type_info = {
 /* This is a nasty hack to allow passing a NULL bus to qdev_create.  */
 static BusState *main_system_bus;
 
+static void main_system_bus_release(Object *obj)
+{
+    g_free(obj);
+}
+
 static void main_system_bus_create(void)
 {
     /* assign main_system_bus before qbus_create_inplace()
@@ -274,7 +279,7 @@ static void main_system_bus_create(void)
     main_system_bus = g_malloc0(system_bus_info.instance_size);
     qbus_create_inplace(main_system_bus, TYPE_SYSTEM_BUS, NULL,
                         "main-system-bus");
-    main_system_bus->glib_allocated = true;
+    object_set_release_func(OBJECT(main_system_bus), main_system_bus_release);
     object_property_add_child(container_get(qdev_get_machine(),
                                             "/unattached"),
                               "sysbus", OBJECT(main_system_bus), NULL);
Signature · 1 line
1.7.5.4
Acked-by on v1 3/9
#5 lore raw

Re: [Qemu-devel] [PATCH 3/9] qbus: remove glib_allocated/qom_allocated and use release hook to free memory

Am 26.08.2012 17:51, schrieb Anthony Liguori:

Quoted 1 line

That's a really nice solution for cleaning this up, thanks!

However one conceptional detail...

Quoted 6 lines
---
 hw/pci.c    |    7 ++++++-
 hw/qdev.c   |   15 ---------------
 hw/qdev.h   |    7 -------
 hw/sysbus.c |    7 ++++++-
 4 files changed, 12 insertions(+), 24 deletions(-)

[...]

Quoted 4 lines
hw/qdev.c +0 -0
index b5a52ac..6b61daa 100644

[...]

Quoted 20 lines
hw/qdev.c +0 -12
@@ -468,18 +466,6 @@ BusState *qbus_create(const char *typename, DeviceState *parent, const char *nam
     return bus;
 }
 
-void qbus_free(BusState *bus)
-{
-    if (bus->qom_allocated) {
-        object_delete(OBJECT(bus));
-    } else {
-        object_finalize(OBJECT(bus));
-        if (bus->glib_allocated) {
-            g_free(bus);
-        }
-    }
-}
-
 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
 {
     BusClass *bc = BUS_GET_CLASS(bus);

hw/qdev.c static void device_finalize(Object *obj) · line 689

Quoted 7 lines
hw/qdev.c +0 -1
@@ -698,7 +684,6 @@ static void device_finalize(Object *obj)
     if (dev->state == DEV_STATE_INITIALIZED) {
         while (dev->num_child_bus) {
             bus = QLIST_FIRST(&dev->child_bus);
-            qbus_free(bus);
         }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);

I wonder how this is gonna work: The device used to be in charge of tearing down its bus children ... now it neither deletes nor finalizes nor unrefs? Is the while loop even still needed?

Wouldn't the busses still have the device as parent, referencing it, blocking device_finalize?

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#6 lore raw

Re: [Qemu-devel] [PATCH 3/9] qbus: remove glib_allocated/qom_allocated and use release hook to free memory

Quoted 53 lines · Andreas Färber

Am 26.08.2012 17:51, schrieb Anthony Liguori:

Quoted 1 line

That's a really nice solution for cleaning this up, thanks!

However one conceptional detail...

Quoted 6 lines
---
 hw/pci.c    |    7 ++++++-
 hw/qdev.c   |   15 ---------------
 hw/qdev.h   |    7 -------
 hw/sysbus.c |    7 ++++++-
 4 files changed, 12 insertions(+), 24 deletions(-)

[...]

Quoted 4 lines
hw/qdev.c +0 -0
index b5a52ac..6b61daa 100644

[...]

Quoted 20 lines
hw/qdev.c +0 -12
@@ -468,18 +466,6 @@ BusState *qbus_create(const char *typename, DeviceState *parent, const char *nam
     return bus;
 }
 
-void qbus_free(BusState *bus)
-{
-    if (bus->qom_allocated) {
-        object_delete(OBJECT(bus));
-    } else {
-        object_finalize(OBJECT(bus));
-        if (bus->glib_allocated) {
-            g_free(bus);
-        }
-    }
-}
-
 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
 {
     BusClass *bc = BUS_GET_CLASS(bus);

hw/qdev.c static void device_finalize(Object *obj) · line 689

Quoted 7 lines
hw/qdev.c +0 -1
@@ -698,7 +684,6 @@ static void device_finalize(Object *obj)
     if (dev->state == DEV_STATE_INITIALIZED) {
         while (dev->num_child_bus) {
             bus = QLIST_FIRST(&dev->child_bus);
-            qbus_free(bus);
         }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);

I wonder how this is gonna work: The device used to be in charge of tearing down its bus children ... now it neither deletes nor finalizes nor unrefs? Is the while loop even still needed?

Quoted 2 lines

Wouldn't the busses still have the device as parent, referencing it, blocking device_finalize?

This has never been right.. Just because a controller goes away, it doesn't mean that the devices ought to go away too.

There are different types of "remove" so let's consider each.

1) Guest visible eject: if a controller is ejected, then the guest will obviously see everything behind it get removed too. This is an emulation detail, not a QOM thing.

2) Final deletion: this only happens when all references go away. If you eject a controller but there are still children that reference it, the controller won't go away. You actually need to delete each individual disk (or whatever is behind it) in order to break the reference counting.

The eject notifier could walk the full bus and attempt to break the connections but honestly, I'd much prefer that we deprecate the current device_del interface and just do everything through QOM properties. That would mean manually deleting all of the devices behind the bus if that's really what you wanted to do.

Regards,

Anthony Liguori

Quoted 7 lines

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#7 lore raw

Re: [Qemu-devel] [PATCH 3/9] qbus: remove glib_allocated/qom_allocated and use release hook to free memory

Am 27.08.2012 16:22, schrieb Anthony Liguori:

Quoted 24 lines
Quoted 6 lines · Andreas Färber

I wonder how this is gonna work: The device used to be in charge of tearing down its bus children ... now it neither deletes nor finalizes nor unrefs? Is the while loop even still needed?

Wouldn't the busses still have the device as parent, referencing it, blocking device_finalize?

This has never been right.. Just because a controller goes away, it doesn't mean that the devices ought to go away too.

There are different types of "remove" so let's consider each.

1) Guest visible eject: if a controller is ejected, then the guest will obviously see everything behind it get removed too. This is an emulation detail, not a QOM thing.

2) Final deletion: this only happens when all references go away. If you eject a controller but there are still children that reference it, the controller won't go away. You actually need to delete each individual disk (or whatever is behind it) in order to break the reference counting.

Quoted 5 lines

The eject notifier could walk the full bus and attempt to break the connections but honestly, I'd much prefer that we deprecate the current device_del interface and just do everything through QOM properties. That would mean manually deleting all of the devices behind the bus if that's really what you wanted to do.

I think we're talking about different scenarios here...

I was thinking

PCIHostState has-a PCIBus

(not PCIBus has-a PCIDevice) and final deletion.

In that case I would expect that it must be guaranteed that the device that created the bus has access to the bus until it destroys it. But IIUC the PCIHostState, once unparented from its SysBus (bad example!), has a refcount of 1 (its PCIBus) thereby not being finalized?

I do understand your concept of refcounting matches what Java, .NET, etc. do for objects but combined with the new QBus I feel this is blurring the encapsulations and expected semantics of the device-centric functions we have. To me the uninitfn means "the whole object goes away" and is incompatible with "part of its children may stay behind if there are still stray references to them"... we can no longer properly access them then, only devices have canonical paths, so we'd risk piling up garbage at runtime.

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw

PATCH 4/9 object: remove object_finalize

In reply to Anthony Liguori

Callers should just use object_unref

---
 hw/qdev.c             |    4 ----
 include/qemu/object.h |    9 ---------
 qom/object.c          |    2 +-
 3 files changed, 1 insertions(+), 14 deletions(-)
hw/qdev.c +0 -4
index 6b61daa..fdee91f 100644
@@ -678,13 +678,9 @@ static void device_initfn(Object *obj)
 static void device_finalize(Object *obj)
 {
     DeviceState *dev = DEVICE(obj);
-    BusState *bus;
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
     if (dev->state == DEV_STATE_INITIALIZED) {
-        while (dev->num_child_bus) {
-            bus = QLIST_FIRST(&dev->child_bus);
-        }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
include/qemu/object.h +0 -9
index 487adcd..8bc9935 100644
@@ -490,15 +490,6 @@ void object_initialize_with_type(void *data, Type type);
 void object_initialize(void *obj, const char *typename);
 
 /**
- * object_finalize:
- * @obj: The object to finalize.
- *
- * This function destroys and object without freeing the memory associated with
- * it.
- */
-void object_finalize(void *obj);
-
-/**
  * object_dynamic_cast:
  * @obj: The object to cast.
  * @typename: The @typename to cast to.
qom/object.c +1 -1
index 44135c3..1144f79 100644
@@ -375,7 +375,7 @@ static void object_deinit(Object *obj, TypeImpl *type)
     }
 }
 
-void object_finalize(void *data)
+static void object_finalize(void *data)
 {
     Object *obj = data;
     TypeImpl *ti = obj->class->type;
Signature · 1 line
1.7.5.4
#9 lore raw

Re: [Qemu-devel] [PATCH 4/9] object: remove object_finalize

Am 26.08.2012 17:51, schrieb Anthony Liguori:

Quoted 17 lines

Callers should just use object_unref

---
 hw/qdev.c             |    4 ----
 include/qemu/object.h |    9 ---------
 qom/object.c          |    2 +-
 3 files changed, 1 insertions(+), 14 deletions(-)
hw/qdev.c +0 -0
index 6b61daa..fdee91f 100644
@@ -678,13 +678,9 @@ static void device_initfn(Object *obj)
 static void device_finalize(Object *obj)
 {
     DeviceState *dev = DEVICE(obj);

hw/qdev.c static void device_initfn(Object *obj) · line 686

Quoted 10 lines
hw/qdev.c +0 -4
-    BusState *bus;
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
     if (dev->state == DEV_STATE_INITIALIZED) {
-        while (dev->num_child_bus) {
-            bus = QLIST_FIRST(&dev->child_bus);
-        }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }

This seems to answer my remark on 3/9, should've been squashed into that one.

Quoted 25 lines
include/qemu/object.h +0 -9
index 487adcd..8bc9935 100644
@@ -490,15 +490,6 @@ void object_initialize_with_type(void *data, Type type);
 void object_initialize(void *obj, const char *typename);
 
 /**
- * object_finalize:
- * @obj: The object to finalize.
- *
- * This function destroys and object without freeing the memory associated with
- * it.
- */
-void object_finalize(void *obj);
-
-/**
  * object_dynamic_cast:
  * @obj: The object to cast.
  * @typename: The @typename to cast to.
qom/object.c +0 -0
index 44135c3..1144f79 100644

qom/object.c static void object_deinit(Object *obj, TypeImpl *type) · line 381

Quoted 8 lines
qom/object.c +1 -1
@@ -375,7 +375,7 @@ static void object_deinit(Object *obj, TypeImpl *type)
     }
 }
 
-void object_finalize(void *data)
+static void object_finalize(void *data)
 {
     Object *obj = data;
     TypeImpl *ti = obj->class->type;

This is what I was referring to with breaking the encapsulation on 3/9:
When we have a PHB with embedded PCIDevice on its PCIBus, as demonstrated with i440fx and prep_pci, then when doing object_delete() on the whole thing I expect the main object's finalizer to call object_finalize() on its embedded childs, forcing their uninit or an assert if a programming error. Not just an unref that might or might not finalize it.

If however finalize is called only at refcount 0 then who will unref the self-created children? Finalize would never be called due to pending references by its children...

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#10 lore raw

Re: [Qemu-devel] [PATCH 4/9] object: remove object_finalize

Quoted 74 lines · Andreas Färber

Am 26.08.2012 17:51, schrieb Anthony Liguori:

Quoted 17 lines

Callers should just use object_unref

---
 hw/qdev.c             |    4 ----
 include/qemu/object.h |    9 ---------
 qom/object.c          |    2 +-
 3 files changed, 1 insertions(+), 14 deletions(-)
hw/qdev.c +0 -0
index 6b61daa..fdee91f 100644
@@ -678,13 +678,9 @@ static void device_initfn(Object *obj)
 static void device_finalize(Object *obj)
 {
     DeviceState *dev = DEVICE(obj);

hw/qdev.c static void device_initfn(Object *obj) · line 686

Quoted 10 lines
hw/qdev.c +0 -4
-    BusState *bus;
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
     if (dev->state == DEV_STATE_INITIALIZED) {
-        while (dev->num_child_bus) {
-            bus = QLIST_FIRST(&dev->child_bus);
-        }
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }

This seems to answer my remark on 3/9, should've been squashed into that one.

Quoted 25 lines
include/qemu/object.h +0 -9
index 487adcd..8bc9935 100644
@@ -490,15 +490,6 @@ void object_initialize_with_type(void *data, Type type);
 void object_initialize(void *obj, const char *typename);
 
 /**
- * object_finalize:
- * @obj: The object to finalize.
- *
- * This function destroys and object without freeing the memory associated with
- * it.
- */
-void object_finalize(void *obj);
-
-/**
  * object_dynamic_cast:
  * @obj: The object to cast.
  * @typename: The @typename to cast to.
qom/object.c +0 -0
index 44135c3..1144f79 100644

qom/object.c static void object_deinit(Object *obj, TypeImpl *type) · line 381

Quoted 8 lines
qom/object.c +1 -1
@@ -375,7 +375,7 @@ static void object_deinit(Object *obj, TypeImpl *type)
     }
 }
 
-void object_finalize(void *data)
+static void object_finalize(void *data)
 {
     Object *obj = data;
     TypeImpl *ti = obj->class->type;

This is what I was referring to with breaking the encapsulation on 3/9:
When we have a PHB with embedded PCIDevice on its PCIBus, as demonstrated with i440fx and prep_pci, then when doing object_delete() on the whole thing I expect the main object's finalizer to call object_finalize() on its embedded childs, forcing their uninit or an assert if a programming error. Not just an unref that might or might not finalize it.

Quoted 3 lines

If however finalize is called only at refcount 0 then who will unref the self-created children? Finalize would never be called due to pending references by its children...

The refs are stored as properties and when properties are removed during object destruction, those references are dropped.

That is what takes the refcount to 0. We could add a g_assert(child->ref == 1) before removing the property to catch programming errors.

Regards,

Anthony Liguori

Quoted 6 lines

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#11 lore raw

PATCH 5/9 object: add support for nullable child properties

In reply to Anthony Liguori

A nullable child can be detached from its parent by setting the property to NULL. This provides a mechanism for a management tool to delete certain types of objects.

object: link<>: release reference on finalize, externalize getter/setter

Reported-by Ping Fan
---
 include/qemu/object.h |    9 +++++
 qom/object.c          |   78 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 77 insertions(+), 10 deletions(-)
include/qemu/object.h +9 -0
index 8bc9935..5bb62dd 100644
@@ -907,6 +907,9 @@ Object *object_resolve_path_component(Object *parent, gchar *part);
 void object_property_add_child(Object *obj, const char *name,
                                Object *child, struct Error **errp);
 
+void object_property_add_nullable_child(Object *obj, const char *name,
+                                        Object *child, struct Error **errp);
+
 /**
  * object_property_add_link:
  * @obj: the object to add a property to
@@ -925,6 +928,12 @@ void object_property_add_link(Object *obj, const char *name,
                               const char *type, Object **child,
                               struct Error **errp);
 
+void object_get_link_property(Object *obj, struct Visitor *v, void *opaque,
+                              const char *name, struct Error **errp);
+
+void object_set_link_property(Object *obj, struct Visitor *v, void *opaque,
+                              const char *name, struct Error **errp);
+
 /**
  * object_property_add_str:
  * @obj: the object to add a property to
qom/object.c +68 -10

126 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#12 lore raw

PATCH 6/9 qdev: make devices created with device_add nullable so they can be deleted

In reply to Anthony Liguori

A management tool can destroy these devices by writing an empty string into the child link property.

---
 hw/qdev-monitor.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
hw/qdev-monitor.c +10 -5
index 018b386..3f08575 100644
@@ -469,15 +469,20 @@ DeviceState *qdev_device_add(QemuOpts *opts)
         return NULL;
     }
     if (qdev->id) {
-        object_property_add_child(qdev_get_peripheral(), qdev->id,
-                                  OBJECT(qdev), NULL);
+        object_property_add_nullable_child(qdev_get_peripheral(), qdev->id,
+                                           OBJECT(qdev), NULL);
     } else {
         static int anon_count;
         gchar *name = g_strdup_printf("device[%d]", anon_count++);
-        object_property_add_child(qdev_get_peripheral_anon(), name,
-                                  OBJECT(qdev), NULL);
+        object_property_add_nullable_child(qdev_get_peripheral_anon(), name,
+                                           OBJECT(qdev), NULL);
         g_free(name);
-    }        
+    }
+
+    /* Drop the allocation reference -- the container link will ensure the
+       object stays alive. */
+    object_unref(OBJECT(qdev));
+
     if (qdev_init(qdev) < 0) {
         qerror_report(QERR_DEVICE_INIT_FAILED, driver);
         return NULL;
Signature · 1 line
1.7.5.4
#13 lore raw

PATCH 7/9 qdev: add notifier for when the device loses its parent bus (eject)

In reply to Anthony Liguori
---
 hw/qdev.c |    2 ++
 hw/qdev.h |    3 +++
 2 files changed, 5 insertions(+), 0 deletions(-)
hw/qdev.c +2 -0
index fdee91f..86e1337 100644
@@ -672,6 +672,8 @@ static void device_initfn(Object *obj)
 
     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
                              (Object **)&dev->parent_bus, NULL);
+
+    notifier_list_init(&dev->eject_notifier);
 }
 
 /* Unlink device from bus and free the structure.  */
hw/qdev.h +3 -0
index 3561e3a..5009072 100644
@@ -8,6 +8,7 @@
 #include "qapi/qapi-visit-core.h"
 #include "qemu/object.h"
 #include "error.h"
+#include "notify.h"
 
 typedef struct Property Property;
 
@@ -76,6 +77,8 @@ struct DeviceState {
     int num_child_bus;
     int instance_id_alias;
     int alias_required_for_version;
+
+    NotifierList eject_notifier;
 };
 
 #define TYPE_BUS "bus"
Signature · 1 line
1.7.5.4
#14 lore raw

PATCH 8/9 qdev: make qdev_set_parent_bus() just set a link property

In reply to Anthony Liguori

Also make setting the link to NULL break the bus link

---
 hw/qdev.c |   48 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 42 insertions(+), 6 deletions(-)
hw/qdev.c +42 -6
index 86e1337..525a0cb 100644
@@ -100,8 +100,7 @@ static void bus_add_child(BusState *bus, DeviceState *child)
 
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
-    dev->parent_bus = bus;
-    bus_add_child(bus, dev);
+    object_property_set_link(OBJECT(dev), OBJECT(bus), "parent_bus", NULL);
 }
 
 /* Create a new device.  This only initializes the device state structure
@@ -241,8 +240,8 @@ void qbus_reset_all_fn(void *opaque)
 /* can be used as ->unplug() callback for the simple cases */
 int qdev_simple_unplug_cb(DeviceState *dev)
 {
-    /* just zap it */
-    qdev_free(dev);
+    /* Unplug from parent bus via a forced eject */
+    qdev_set_parent_bus(dev, NULL);
     return 0;
 }
 
@@ -646,6 +645,40 @@ void qdev_property_add_static(DeviceState *dev, Property *prop,
     assert_no_error(local_err);
 }
 
+static void qdev_set_link_property(Object *obj, Visitor *v, void *opaque,
+                                   const char *name, Error **errp)
+{
+    DeviceState *dev = DEVICE(obj);
+    BusState *parent_bus = dev->parent_bus;
+
+    object_set_link_property(obj, v, opaque, name, errp);
+
+    if (parent_bus) {
+        bus_remove_child(parent_bus, dev);
+    }
+
+    if (dev->parent_bus) {
+        bus_add_child(dev->parent_bus, dev);
+    }
+
+    if (!dev->parent_bus) {
+        notifier_list_notify(&dev->eject_notifier, dev);
+    }
+}
+
+static void qdev_release_link_property(Object *obj, const char *name,
+                                       void *opaque)
+{
+    DeviceState *dev = DEVICE(obj);
+
+    if (dev->parent_bus) {
+        bus_remove_child(dev->parent_bus, dev);
+        object_unref(OBJECT(dev->parent_bus));
+    }
+
+    dev->parent_bus = NULL;
+}
+
 static void device_initfn(Object *obj)
 {
     DeviceState *dev = DEVICE(obj);
@@ -670,8 +703,11 @@ static void device_initfn(Object *obj)
     } while (class != object_class_by_name(TYPE_DEVICE));
     qdev_prop_set_globals(dev);
 
-    object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
-                             (Object **)&dev->parent_bus, NULL);
+    object_property_add(OBJECT(dev), "parent_bus", "link<" TYPE_BUS ">",
+                        object_get_link_property,
+                        qdev_set_link_property,
+                        qdev_release_link_property,
+                        &dev->parent_bus, NULL);
 
     notifier_list_init(&dev->eject_notifier);
 }
Signature · 1 line
1.7.5.4
#15 lore raw

Re: [Qemu-devel] [PATCH 8/9] qdev: make qdev_set_parent_bus() just set a link property

Quoted 22 lines · Anthony Liguori

Also make setting the link to NULL break the bus link

---
 hw/qdev.c |   48 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 42 insertions(+), 6 deletions(-)
hw/qdev.c +1 -2
index 86e1337..525a0cb 100644
@@ -100,8 +100,7 @@ static void bus_add_child(BusState *bus, DeviceState *child)
 
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
-    dev->parent_bus = bus;
-    bus_add_child(bus, dev);
+    object_property_set_link(OBJECT(dev), OBJECT(bus), "parent_bus", NULL);
 }
 
 /* Create a new device.  This only initializes the device state structure

hw/qdev.c void qbus_reset_all_fn(void *opaque) · line 244

Quoted 7 lines
hw/qdev.c +2 -2
@@ -241,8 +240,8 @@ void qbus_reset_all_fn(void *opaque)
 /* can be used as ->unplug() callback for the simple cases */
 int qdev_simple_unplug_cb(DeviceState *dev)
 {
-    /* just zap it */
-    qdev_free(dev);
+    /* Unplug from parent bus via a forced eject */
+    qdev_set_parent_bus(dev, NULL);

I think it is more reliable to remove the reference property(child, link) before object_finialize(). So when uplug-finish, we delete all the refers: bus->child, bus<-child by _del_property not using _set_property.

Quoted 62 lines
hw/qdev.c +39 -3
     return 0;
 }
 
@@ -646,6 +645,40 @@ void qdev_property_add_static(DeviceState *dev, Property *prop,
     assert_no_error(local_err);
 }
 
+static void qdev_set_link_property(Object *obj, Visitor *v, void *opaque,
+                                   const char *name, Error **errp)
+{
+    DeviceState *dev = DEVICE(obj);
+    BusState *parent_bus = dev->parent_bus;
+
+    object_set_link_property(obj, v, opaque, name, errp);
+
+    if (parent_bus) {
+        bus_remove_child(parent_bus, dev);
+    }
+
+    if (dev->parent_bus) {
+        bus_add_child(dev->parent_bus, dev);
+    }
+
+    if (!dev->parent_bus) {
+        notifier_list_notify(&dev->eject_notifier, dev);
+    }
+}
+
+static void qdev_release_link_property(Object *obj, const char *name,
+                                       void *opaque)
+{
+    DeviceState *dev = DEVICE(obj);
+
+    if (dev->parent_bus) {
+        bus_remove_child(dev->parent_bus, dev);
+        object_unref(OBJECT(dev->parent_bus));
+    }
+
+    dev->parent_bus = NULL;
+}
+
 static void device_initfn(Object *obj)
 {
     DeviceState *dev = DEVICE(obj);
@@ -670,8 +703,11 @@ static void device_initfn(Object *obj)
     } while (class != object_class_by_name(TYPE_DEVICE));
     qdev_prop_set_globals(dev);
 
-    object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
-                             (Object **)&dev->parent_bus, NULL);
+    object_property_add(OBJECT(dev), "parent_bus", "link<" TYPE_BUS ">",
+                        object_get_link_property,
+                        qdev_set_link_property,
+                        qdev_release_link_property,
+                        &dev->parent_bus, NULL);
 
     notifier_list_init(&dev->eject_notifier);
 }
--

1.7.5.4

#16 lore raw

Re: [Qemu-devel] [PATCH 8/9] qdev: make qdev_set_parent_bus() just set a link property

Quoted 21 lines · liu ping fan
Quoted 20 lines · Anthony Liguori

Also make setting the link to NULL break the bus link

---
 hw/qdev.c |   48 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 42 insertions(+), 6 deletions(-)
hw/qdev.c +1 -2
index 86e1337..525a0cb 100644
@@ -100,8 +100,7 @@ static void bus_add_child(BusState *bus, DeviceState *child)
 
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
-    dev->parent_bus = bus;
-    bus_add_child(bus, dev);
+    object_property_set_link(OBJECT(dev), OBJECT(bus), "parent_bus", NULL);
 }
 
Quoted 14 lines

hw/qdev.c void qbus_reset_all_fn(void *opaque) · line 244

Quoted 9 lines
hw/qdev.c +2 -2
 /* Create a new device.  This only initializes the device state structure
@@ -241,8 +240,8 @@ void qbus_reset_all_fn(void *opaque)
 /* can be used as ->unplug() callback for the simple cases */
 int qdev_simple_unplug_cb(DeviceState *dev)
 {
-    /* just zap it */
-    qdev_free(dev);
+    /* Unplug from parent bus via a forced eject */
+    qdev_set_parent_bus(dev, NULL);

I think it is more reliable to remove the reference property(child, link) before object_finialize(). So when uplug-finish, we delete all the refers: bus->child, bus<-child by _del_property not using _set_property.

object_finalize is called when ref=0. You cannot remove refs in finalize because by definition, ref=0.

Regards,

Anthony Liguori

#17 lore raw

PATCH 9/9 hotplug: refactor hotplug to leverage new QOM functions

In reply to Anthony Liguori

1) DeviceState::unplug requests for an eject to happen
- the default implementation is a forced eject

2) A bus can eject a device by setting the parent_bus to NULL
- this detaches the device from the bus
- this does *not* cause the device to disappear

3) The current implementation on unplug also registers an eject notifier
- the eject notifier will detach the device the parent. This will cause the device to disappear

4) A purely guest initiated unplug will not delete a device but will cause the device to appear detached from the guests PoV.

---
 hw/acpi_piix4.c   |    3 ++-
 hw/pci.c          |   10 +++++++++-
 hw/pcie.c         |    2 +-
 hw/qdev.c         |   22 ++++++++++++++++++++++
 hw/qdev.h         |    2 ++
 hw/shpc.c         |    2 +-
 hw/xen_platform.c |    2 +-
 7 files changed, 38 insertions(+), 5 deletions(-)
hw/acpi_piix4.c +2 -1
index 72d6e5c..eac53b3 100644
@@ -305,7 +305,8 @@ static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
             if (pc->no_hotplug) {
                 slot_free = false;
             } else {
-                qdev_free(qdev);
+                /* Force eject of device */
+                qdev_set_parent_bus(qdev, NULL);
             }
         }
     }
hw/pci.c +9 -1
index 437af70..cc555c2 100644
@@ -46,6 +46,14 @@ static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static int pcibus_reset(BusState *qbus);
 
+static void pcibus_remove_child(BusState *bus, DeviceState *dev)
+{
+    PCIDevice *pci_dev = PCI_DEVICE(dev);
+    PCIBus *pci_bus = PCI_BUS(bus);
+
+    pci_bus->devices[pci_dev->devfn] = NULL;
+}
+
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
@@ -65,6 +73,7 @@ static void pci_bus_class_init(ObjectClass *klass, void *data)
     k->get_dev_path = pcibus_get_dev_path;
     k->get_fw_dev_path = pcibus_get_fw_dev_path;
     k->reset = pcibus_reset;
+    k->remove_child = pcibus_remove_child;
 }
 
 static const TypeInfo pci_bus_info = {
@@ -833,7 +842,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
 static void do_pci_unregister_device(PCIDevice *pci_dev)
 {
     qemu_free_irqs(pci_dev->irq);
-    pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
 }
 
hw/pcie.c +1 -1
index 7c92f19..d10ffea 100644
@@ -235,7 +235,7 @@ static int pcie_cap_slot_hotplug(DeviceState *qdev,
                                    PCI_EXP_SLTSTA_PDS);
         pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
     } else {
-        qdev_free(&pci_dev->qdev);
+        qdev_set_parent_bus(DEVICE(pci_dev), NULL);
         pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                      PCI_EXP_SLTSTA_PDS);
         pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
hw/qdev.c +22 -0
index 525a0cb..be41f00 100644
@@ -62,6 +62,7 @@ static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
 
 static void bus_remove_child(BusState *bus, DeviceState *child)
 {
+    BusClass *bc = BUS_GET_CLASS(bus);
     BusChild *kid;
 
     QTAILQ_FOREACH(kid, &bus->children, sibling) {
@@ -71,6 +72,11 @@ static void bus_remove_child(BusState *bus, DeviceState *child)
             snprintf(name, sizeof(name), "child[%d]", kid->index);
             QTAILQ_REMOVE(&bus->children, kid, sibling);
             object_property_del(OBJECT(bus), name, NULL);
+
+            if (bc->remove_child) {
+                bc->remove_child(bus, kid->child);
+            }
+
             g_free(kid);
             return;
         }
@@ -192,9 +198,20 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+static void qdev_finish_unplug(Notifier *notifier, void *data)
+{
+    DeviceState *dev = DEVICE(data);
+
+    /* unparent the object -- this should release the last reference to the
+       child*/
+    object_unparent(OBJECT(dev));
+    g_free(notifier);
+}
+
 void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
+    Notifier *notifier;
 
     if (!dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
@@ -204,6 +221,11 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 
     qdev_hot_removed = true;
 
+    notifier = g_malloc0(sizeof(*notifier));
+    notifier->notify = qdev_finish_unplug;
+
+    notifier_list_add(&dev->eject_notifier, notifier);
+
     if (dc->unplug(dev) < 0) {
         error_set(errp, QERR_UNDEFINED_ERROR);
         return;
hw/qdev.h +2 -0
index 5009072..7ae8d5d 100644
@@ -99,6 +99,8 @@ struct BusClass {
      */
     char *(*get_fw_dev_path)(DeviceState *dev);
     int (*reset)(BusState *bus);
+
+    void (*remove_child)(BusState *bus, DeviceState *dev);
 };
 
 typedef struct BusChild {
hw/shpc.c +1 -1
index a5baf24..ce507e7 100644
@@ -253,7 +253,7 @@ static void shpc_free_devices_in_slot(SHPCDevice *shpc, int slot)
          ++devfn) {
         PCIDevice *affected_dev = shpc->sec_bus->devices[devfn];
         if (affected_dev) {
-            qdev_free(&affected_dev->qdev);
+            qdev_set_parent_bus(DEVICE(affected_dev), NULL);
         }
     }
 }
hw/xen_platform.c +1 -1
index 0d6c2ff..5c5ecf8 100644
@@ -87,7 +87,7 @@ static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
 {
     if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
             PCI_CLASS_NETWORK_ETHERNET) {
-        qdev_free(&d->qdev);
+        qdev_set_parent_bus(dev, NULL);
     }
 }
 
Signature · 1 line
1.7.5.4
#18 lore raw

Re: [Qemu-devel] [PATCH 9/9] hotplug: refactor hotplug to leverage new QOM functions

Quoted 30 lines · Anthony Liguori

1) DeviceState::unplug requests for an eject to happen
- the default implementation is a forced eject

2) A bus can eject a device by setting the parent_bus to NULL
- this detaches the device from the bus
- this does *not* cause the device to disappear

3) The current implementation on unplug also registers an eject notifier
- the eject notifier will detach the device the parent. This will cause the device to disappear

4) A purely guest initiated unplug will not delete a device but will cause the device to appear detached from the guests PoV.

---
 hw/acpi_piix4.c   |    3 ++-
 hw/pci.c          |   10 +++++++++-
 hw/pcie.c         |    2 +-
 hw/qdev.c         |   22 ++++++++++++++++++++++
 hw/qdev.h         |    2 ++
 hw/shpc.c         |    2 +-
 hw/xen_platform.c |    2 +-
 7 files changed, 38 insertions(+), 5 deletions(-)
hw/acpi_piix4.c +0 -0
index 72d6e5c..eac53b3 100644

hw/acpi_piix4.c static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots) · line 309

Quoted 6 lines
hw/acpi_piix4.c +2 -1
@@ -305,7 +305,8 @@ static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
             if (pc->no_hotplug) {
                 slot_free = false;
             } else {
-                qdev_free(qdev);
+                /* Force eject of device */
+                qdev_set_parent_bus(qdev, NULL);

Do we need to wait for guest's ACKs for all of this device's children. Then we can change this node's topology in the device tree. I think, we can color the current device as unplug_state=ACK, and then decide whether to detached it or not.
Each unplug ack from guest, will first check 1st.whether current node can be release or not. 2nd. if can released, then go bottom-up through the device tree to check whether the upper device can be released or not.
If the down device(devB) removal cause the up device(devA) becomes a leaf, then we can remove devA.
A leaf device is defined as : has no BusState kids OR all of its BusState kids are empty.

This method can avoid sudden remove.

Quoted 151 lines
hw/acpi_piix4.c +0 -0
             }
         }
     }
hw/pci.c +9 -1
index 437af70..cc555c2 100644
@@ -46,6 +46,14 @@ static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
 static int pcibus_reset(BusState *qbus);
 
+static void pcibus_remove_child(BusState *bus, DeviceState *dev)
+{
+    PCIDevice *pci_dev = PCI_DEVICE(dev);
+    PCIBus *pci_bus = PCI_BUS(bus);
+
+    pci_bus->devices[pci_dev->devfn] = NULL;
+}
+
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
@@ -65,6 +73,7 @@ static void pci_bus_class_init(ObjectClass *klass, void *data)
     k->get_dev_path = pcibus_get_dev_path;
     k->get_fw_dev_path = pcibus_get_fw_dev_path;
     k->reset = pcibus_reset;
+    k->remove_child = pcibus_remove_child;
 }
 
 static const TypeInfo pci_bus_info = {
@@ -833,7 +842,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
 static void do_pci_unregister_device(PCIDevice *pci_dev)
 {
     qemu_free_irqs(pci_dev->irq);
-    pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
 }
 
hw/pcie.c +1 -1
index 7c92f19..d10ffea 100644
@@ -235,7 +235,7 @@ static int pcie_cap_slot_hotplug(DeviceState *qdev,
                                    PCI_EXP_SLTSTA_PDS);
         pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
     } else {
-        qdev_free(&pci_dev->qdev);
+        qdev_set_parent_bus(DEVICE(pci_dev), NULL);
         pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                      PCI_EXP_SLTSTA_PDS);
         pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
hw/qdev.c +22 -0
index 525a0cb..be41f00 100644
@@ -62,6 +62,7 @@ static void qdev_property_add_legacy(DeviceState *dev, Property *prop,
 
 static void bus_remove_child(BusState *bus, DeviceState *child)
 {
+    BusClass *bc = BUS_GET_CLASS(bus);
     BusChild *kid;
 
     QTAILQ_FOREACH(kid, &bus->children, sibling) {
@@ -71,6 +72,11 @@ static void bus_remove_child(BusState *bus, DeviceState *child)
             snprintf(name, sizeof(name), "child[%d]", kid->index);
             QTAILQ_REMOVE(&bus->children, kid, sibling);
             object_property_del(OBJECT(bus), name, NULL);
+
+            if (bc->remove_child) {
+                bc->remove_child(bus, kid->child);
+            }
+
             g_free(kid);
             return;
         }
@@ -192,9 +198,20 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
     dev->alias_required_for_version = required_for_version;
 }
 
+static void qdev_finish_unplug(Notifier *notifier, void *data)
+{
+    DeviceState *dev = DEVICE(data);
+
+    /* unparent the object -- this should release the last reference to the
+       child*/
+    object_unparent(OBJECT(dev));
+    g_free(notifier);
+}
+
 void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
+    Notifier *notifier;
 
     if (!dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
@@ -204,6 +221,11 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 
     qdev_hot_removed = true;
 
+    notifier = g_malloc0(sizeof(*notifier));
+    notifier->notify = qdev_finish_unplug;
+
+    notifier_list_add(&dev->eject_notifier, notifier);
+
     if (dc->unplug(dev) < 0) {
         error_set(errp, QERR_UNDEFINED_ERROR);
         return;
hw/qdev.h +2 -0
index 5009072..7ae8d5d 100644
@@ -99,6 +99,8 @@ struct BusClass {
      */
     char *(*get_fw_dev_path)(DeviceState *dev);
     int (*reset)(BusState *bus);
+
+    void (*remove_child)(BusState *bus, DeviceState *dev);
 };
 
 typedef struct BusChild {
hw/shpc.c +1 -1
index a5baf24..ce507e7 100644
@@ -253,7 +253,7 @@ static void shpc_free_devices_in_slot(SHPCDevice *shpc, int slot)
          ++devfn) {
         PCIDevice *affected_dev = shpc->sec_bus->devices[devfn];
         if (affected_dev) {
-            qdev_free(&affected_dev->qdev);
+            qdev_set_parent_bus(DEVICE(affected_dev), NULL);
         }
     }
 }
hw/xen_platform.c +1 -2
index 0d6c2ff..5c5ecf8 100644
@@ -87,7 +87,7 @@ static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
 {
     if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
             PCI_CLASS_NETWORK_ETHERNET) {
-        qdev_free(&d->qdev);
+        qdev_set_parent_bus(dev, NULL);
     }
 }
 
--

1.7.5.4

#19 lore raw
In reply to Anthony Liguori
Quoted 4 lines · Anthony Liguori

Right now, you need to pair up object_new with object_delete. This is impractical when using reference counting because we would like to ensure that object_unref() also frees memory when needed.

Quoted 8 lines

The first few patches fix this problem by introducing a release callback so that objects that need special release behavior (i.e. g_free) can do that.

Since link and child properties all hold references, in order to actually free an object, we need to break those links. User created devices end up as children of a container. But child properties cannot be removed which means there's no obvious way to remove the reference and ultimately free the object.

Why? Since we call _add_child() in qdev_device_add(), why can not we call object_property_del_child() for qmp_device_del(). Could you explain it more detail?

Quoted 36 lines

We introduce the concept of "nullable child" properties to solve this. This is a child property that can be broken by writing NULL to the child link. Today we set all /peripheral* children to be nullable so that they can be deleted by management tools.

In terms of modeling hotplug, we represent unplug by removing the object from the parent bus. We need to register a notifier for when this happens so that we can also remove the parent's child property to ultimately release the object.

Putting it all together, we have:

1) qmp_device_del will issue a callback to a device. The default callback will do a forced eject (which means writing NULL to the parent_bus link).

2) PCI hotplug is a bit more sophisticated in that it waits for the guest to do the ejection.

3) qmp_device_del will register an eject notifier such that the device gets completely removed.

There's a slightly change in behavior here. A device is not automatically destroyed based on a guest initiated eject. A management tool must explicitly break the parent's link to the child in order for the device to disappear completely. device_del behaves exactly as it does today though.

This is an RFC. I've tested the series quite a lot (it was hard to get the reference counting right) but not enough to apply. I also don't think the series is quite split right and may not bisect cleanly.

I also want to write up a document describing object life cycle since admittedly the above is probably not that easy to follow.

I wanted to share this now though because it works and I think the concepts are right.

#20 lore raw

Am 27.08.2012 09:22, schrieb liu ping fan:

Quoted 8 lines · Anthony Liguori
Quoted 7 lines

Right now, you need to pair up object_new with object_delete. This is impractical when using reference counting because we would like to ensure that object_unref() also frees memory when needed.

The first few patches fix this problem by introducing a release callback so that objects that need special release behavior (i.e. g_free) can do that.

Quoted 8 lines
Quoted 5 lines

Since link and child properties all hold references, in order to actually free an object, we need to break those links. User created devices end up as children of a container. But child properties cannot be removed which means there's no obvious way to remove the reference and ultimately free the object.

Why? Since we call _add_child() in qdev_device_add(), why can not we call object_property_del_child() for qmp_device_del(). Could you explain it more detail?

Seconded. If we hot-unplug a device, we should surely remove its child<> property from /machine/unassigned or parent bus or whatever. Why is it that child<> properties cannot be removed?

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#21 lore raw

Il 27/08/2012 13:46, Andreas Färber ha scritto:

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

Since link and child properties all hold references, in order to actually free an object, we need to break those links. User created devices end up as children of a container. But child properties cannot be removed which means there's no obvious way to remove the reference and ultimately free the object.

Why? Since we call _add_child() in qdev_device_add(), why can not we call object_property_del_child() for qmp_device_del(). Could you explain it more detail?

Seconded. If we hot-unplug a device, we should surely remove its child<> property from /machine/unassigned or parent bus or whatever.

Sure, as soon as the device is ejected by the guest. But until that point we need to keep the device in the QOM tree so that: 1) it has a canonical path; 2) it can be examined; 3) it keeps children alive.

Quoted 1 line

Why is it that child<> properties cannot be removed?

Yeah, I didn't quite understand the difference between unparenting and setting the child property to NULL.

Paolo

#22 lore raw
Quoted 12 lines · Paolo Bonzini

Il 27/08/2012 13:46, Andreas Färber ha scritto:

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

Since link and child properties all hold references, in order to actually free an object, we need to break those links. User created devices end up as children of a container. But child properties cannot be removed which means there's no obvious way to remove the reference and ultimately free the object.

Why? Since we call _add_child() in qdev_device_add(), why can not we call object_property_del_child() for qmp_device_del(). Could you explain it more detail?

Seconded. If we hot-unplug a device, we should surely remove its child<> property from /machine/unassigned or parent bus or whatever.

That's exactly what is happening in this series. qmp_device_del adds an ejection notifier that unparents the device to remove the last reference count.

Quoted 9 lines

Sure, as soon as the device is ejected by the guest. But until that point we need to keep the device in the QOM tree so that: 1) it has a canonical path; 2) it can be examined; 3) it keeps children alive.

Quoted 1 line

Why is it that child<> properties cannot be removed?

Yeah, I didn't quite understand the difference between unparenting and setting the child property to NULL.

They are exactly the same thing. Setting the child property to NULL is unparenting.

Unparenting is essentially deleting. This series makes it such that there is a white list of devices that are capable of being deleted.

Regards,

Anthony Liguori

Quoted 2 lines

Paolo

#23 lore raw
In reply to Anthony Liguori

Hi,

Quoted 29 lines · Anthony Liguori

Right now, you need to pair up object_new with object_delete. This is impractical when using reference counting because we would like to ensure that object_unref() also frees memory when needed.

The first few patches fix this problem by introducing a release callback so that objects that need special release behavior (i.e. g_free) can do that.

Since link and child properties all hold references, in order to actually free an object, we need to break those links. User created devices end up as children of a container. But child properties cannot be removed which means there's no obvious way to remove the reference and ultimately free the object.

We introduce the concept of "nullable child" properties to solve this. This is a child property that can be broken by writing NULL to the child link. Today we set all /peripheral* children to be nullable so that they can be deleted by management tools.

In terms of modeling hotplug, we represent unplug by removing the object from the parent bus. We need to register a notifier for when this happens so that we can also remove the parent's child property to ultimately release the object.

Putting it all together, we have:

1) qmp_device_del will issue a callback to a device. The default callback will do a forced eject (which means writing NULL to the parent_bus link).

2) PCI hotplug is a bit more sophisticated in that it waits for the guest to do the ejection.

Quoted 7 lines

3) qmp_device_del will register an eject notifier such that the device gets completely removed.

There's a slightly change in behavior here. A device is not automatically destroyed based on a guest initiated eject. A management tool must explicitly break the parent's link to the child in order for the device to disappear completely. device_del behaves exactly as it does today though.

is there a plan to respin this series, or has it been dropped? Afaict it hasn't landed upstream. I am reworking an acpi memory hotplug RFC patchset, and noticed this patchset changes guest-initiated eject semantics.

Currently a successfull guest-initiated eject in my mem-hotplug patchset destroys the qdev device. But the semantics of this patchset would require a new behaviour.

thanks,

- Vasilis