From patchwork Mon Mar 3 15:55:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 325890 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 311472C00AE for ; Tue, 4 Mar 2014 02:56:18 +1100 (EST) Received: from localhost ([::1]:40338 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKVDz-0007x3-3W for incoming@patchwork.ozlabs.org; Mon, 03 Mar 2014 10:56:15 -0500 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 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s23FtUNV003402 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 3 Mar 2014 10:55:31 -0500 Received: from amosk.info.com (vpn1-115-164.nay.redhat.com [10.66.115.164]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s23FtRNu024691; Mon, 3 Mar 2014 10:55:28 -0500 From: Amos Kong To: qemu-devel@nongnu.org Date: Mon, 3 Mar 2014 23:55:24 +0800 Message-Id: <1393862124-26806-1-git-send-email-akong@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: hutao@cn.fujitsu.com, pbonzini@redhat.com, armbru@redhat.com, aliguori@amazon.com, afaerber@suse.de Subject: [Qemu-devel] [PCTCH v4] qdev: set properties after device's parent is assigned X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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) {