From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51091) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKVDa-0007sG-Vk for qemu-devel@nongnu.org; Mon, 03 Mar 2014 10:55:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WKVDU-0006kV-Pp for qemu-devel@nongnu.org; Mon, 03 Mar 2014 10:55:50 -0500 Received: from mx1.redhat.com ([209.132.183.28]:27151) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKVDU-0006k8-Fr for qemu-devel@nongnu.org; Mon, 03 Mar 2014 10:55:44 -0500 From: Amos Kong Date: Mon, 3 Mar 2014 23:55:24 +0800 Message-Id: <1393862124-26806-1-git-send-email-akong@redhat.com> Subject: [Qemu-devel] [PCTCH v4] qdev: set properties after device's parent is assigned List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: hutao@cn.fujitsu.com, pbonzini@redhat.com, armbru@redhat.com, aliguori@amazon.com, afaerber@suse.de Test steps: (qemu) device_add e1000,addr=adsf Property 'e1000.addr' doesn't take value 'adsf' (qemu) info qtree Then qemu crashed. Currently we set a link to the new device for qdev parent bus, but the device hasn't been added to QOM tree. When it fails to set properties, object_unparent() can't cleanup the device. Delay device property setting until device's parent is assigned. This way when property setting fails, object_unparent() can cleanup failed device properly. Signed-off-by: Amos Kong Reviewed-By: Igor Mammedov --- V2: fix bz by adjust the initialization order (Paolo) V3: fix bug without making it differs with legacy devices creation (Andreas) V4: update subject and commitlog --- qdev-monitor.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/qdev-monitor.c b/qdev-monitor.c index 6673e3c..9268c87 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -522,7 +522,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) return NULL; } - /* create device, set properties */ + /* create device */ dev = DEVICE(object_new(driver)); if (bus) { @@ -533,11 +533,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) if (id) { dev->id = id; } - if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) { - object_unparent(OBJECT(dev)); - object_unref(OBJECT(dev)); - return NULL; - } + if (dev->id) { object_property_add_child(qdev_get_peripheral(), dev->id, OBJECT(dev), NULL); @@ -549,6 +545,13 @@ DeviceState *qdev_device_add(QemuOpts *opts) g_free(name); } + /* set properties */ + if (qemu_opt_foreach(opts, set_property, dev, 1) != 0) { + object_unparent(OBJECT(dev)); + object_unref(OBJECT(dev)); + return NULL; + } + dev->opts = opts; object_property_set_bool(OBJECT(dev), true, "realized", &err); if (err != NULL) { -- 1.8.5.3