From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, lersek@redhat.com, kwolf@redhat.com,
mreitz@redhat.com, qemu-block@nongnu.org, pkrempa@redhat.com,
mst@redhat.com, marcel.apfelbaum@gmail.com
Subject: Re: [Qemu-devel] [RFC PATCH 6/6] pc: Support firmware configuration with -blockdev
Date: Mon, 04 Mar 2019 18:50:40 +0100 [thread overview]
Message-ID: <87bm2qzfyn.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20190225183757.27378-7-armbru@redhat.com> (Markus Armbruster's message of "Mon, 25 Feb 2019 19:37:57 +0100")
The problem at hand is how to destroy a device created with
qdev_create() without ever realizing it.
This hack passes tests:
diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
index ed608a53d3..1bd538796b 100644
--- a/hw/i386/pc_sysfw.c
+++ b/hw/i386/pc_sysfw.c
@@ -116,14 +116,9 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
prop_name = g_strdup_printf("pflash%d", i);
object_property_del(OBJECT(pcms), prop_name, &error_abort);
g_free(prop_name);
- /*
- * FIXME delete @dev_obj. Straight object_unref() is
- * wrong, since it leaves dangling references in the
- * parent bus behind. object_unparent() doesn't work,
- * either: since @dev_obj hasn't been realized,
- * dev_obj->parent is null, and object_unparent() does
- * nothing.
- */
+ object_ref(dev_obj);
+ object_get_class(dev_obj)->unparent(dev_obj);
+ object_unref(dev_obj);
pcms->flash[i] = NULL;
}
}
If you have a better idea, please let me know.
Here's how I arrived at my hack. We need to undo qdev_create().
qdev_create() is qdev_try_create() plus error handling. The latter is
uninteresting here, so let's have a look at just the former:
DeviceState *qdev_try_create(BusState *bus, const char *type)
{
DeviceState *dev;
if (object_class_by_name(type) == NULL) {
return NULL;
}
dev = DEVICE(object_new(type));
if (!dev) {
return NULL;
}
if (!bus) {
/* Assert that the device really is a SysBusDevice before
* we put it onto the sysbus. Non-sysbus devices which aren't
* being put onto a bus should be created with object_new(TYPE_FOO),
* not qdev_create(NULL, TYPE_FOO).
*/
g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE));
bus = sysbus_get_default();
}
---> qdev_set_parent_bus(dev, bus);
object_unref(OBJECT(dev));
return dev;
}
We need to undo qdev_set_parent_bus().
void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
{
bool replugging = dev->parent_bus != NULL;
if (replugging) {
/* Keep a reference to the device while it's not plugged into
* any bus, to avoid it potentially evaporating when it is
* dereffed in bus_remove_child().
*/
object_ref(OBJECT(dev));
bus_remove_child(dev->parent_bus, dev);
object_unref(OBJECT(dev->parent_bus));
}
dev->parent_bus = bus;
object_ref(OBJECT(bus));
---> bus_add_child(bus, dev);
if (replugging) {
object_unref(OBJECT(dev));
}
}
dev->parent_bus is null, since we didn't realize the device. All we
need to undo is bus_add_child(), with bus_remove_child(). Destruction
of realized devices does that here:
static void device_unparent(Object *obj)
{
DeviceState *dev = DEVICE(obj);
BusState *bus;
if (dev->realized) {
object_property_set_bool(obj, false, "realized", NULL);
}
while (dev->num_child_bus) {
bus = QLIST_FIRST(&dev->child_bus);
object_unparent(OBJECT(bus));
}
if (dev->parent_bus) {
bus_remove_child(dev->parent_bus, dev);
object_unref(OBJECT(dev->parent_bus));
dev->parent_bus = NULL;
}
}
dev->realized is false, dev->num_child_bus is zero, dev->parent_bus is
main_system_bus. Okay, this does what we need. Now how do we call it?
It's static... but it's a method:
static void device_class_init(ObjectClass *class, void *data)
{
DeviceClass *dc = DEVICE_CLASS(class);
class->unparent = device_unparent;
[...]
}
Alright, we can call object_get_class(dev_obj)->unparent(dev_obj).
Final complication: if I call just that, the device's reference counter
goes down to zero in the middle of device_unparent(), and we use after
free. So I bracket he call with object_ref() and object_unref().
next prev parent reply other threads:[~2019-03-04 17:50 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-25 18:37 [Qemu-devel] [RFC PATCH 0/6] pc: Support firmware configuration with -blockdev Markus Armbruster
2019-02-25 18:37 ` [Qemu-devel] [RFC PATCH 1/6] qdev: Fix latent bug with compat_props and onboard devices Markus Armbruster
2019-02-26 12:28 ` Marc-André Lureau
2019-02-25 18:37 ` [Qemu-devel] [RFC PATCH 2/6] qom: Move compat_props machinery from qdev to QOM Markus Armbruster
2019-02-26 12:44 ` Marc-André Lureau
2019-03-04 15:57 ` Philippe Mathieu-Daudé
2019-03-05 6:52 ` Markus Armbruster
2019-02-25 18:37 ` [Qemu-devel] [RFC PATCH 3/6] vl: Fix latent bug with -global and onboard devices Markus Armbruster
2019-02-26 12:45 ` Marc-André Lureau
2019-02-25 18:37 ` [Qemu-devel] [RFC PATCH 4/6] sysbus: Fix latent bug with " Markus Armbruster
2019-02-26 12:48 ` Marc-André Lureau
2019-03-04 16:03 ` Philippe Mathieu-Daudé
2019-03-04 18:45 ` Thomas Huth
2019-03-05 6:54 ` Markus Armbruster
2019-03-05 7:17 ` Thomas Huth
2019-02-25 18:37 ` [Qemu-devel] [RFC PATCH 5/6] vl: Create block backends before setting machine properties Markus Armbruster
2019-03-04 16:14 ` Philippe Mathieu-Daudé
2019-03-05 10:38 ` Markus Armbruster
2019-03-05 11:34 ` Philippe Mathieu-Daudé
2019-03-05 14:07 ` Markus Armbruster
2019-02-25 18:37 ` [Qemu-devel] [RFC PATCH 6/6] pc: Support firmware configuration with -blockdev Markus Armbruster
2019-02-26 9:43 ` Laszlo Ersek
2019-02-26 12:35 ` Markus Armbruster
2019-02-26 16:21 ` Laszlo Ersek
2019-03-04 17:50 ` Markus Armbruster [this message]
2019-03-05 17:08 ` Laszlo Ersek
2019-03-06 6:12 ` Markus Armbruster
2019-03-04 19:14 ` Philippe Mathieu-Daudé
2019-03-04 19:52 ` Philippe Mathieu-Daudé
2019-03-05 11:31 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87bm2qzfyn.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=kwolf@redhat.com \
--cc=lersek@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mreitz@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=pkrempa@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.