From patchwork Thu Jan 2 01:02:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 306006 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 B7ACC2C0090 for ; Thu, 2 Jan 2014 12:03:21 +1100 (EST) Received: from localhost ([::1]:42868 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VyWgx-0006NN-EF for incoming@patchwork.ozlabs.org; Wed, 01 Jan 2014 20:03:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49201) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VyWgC-0005g6-W6 for qemu-devel@nongnu.org; Wed, 01 Jan 2014 20:02:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VyWg7-0008Rf-16 for qemu-devel@nongnu.org; Wed, 01 Jan 2014 20:02:32 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46629) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VyWg6-0008RZ-Oc for qemu-devel@nongnu.org; Wed, 01 Jan 2014 20:02:26 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s0212H8I012913 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 1 Jan 2014 20:02:18 -0500 Received: from amosk.info.com (vpn1-112-200.nay.redhat.com [10.66.112.200]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s0212EfV031244; Wed, 1 Jan 2014 20:02:15 -0500 From: Amos Kong To: qemu-devel@nongnu.org Date: Thu, 2 Jan 2014 09:02:11 +0800 Message-Id: <1388624531-13439-1-git-send-email-akong@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: pbonzini@redhat.com, afaerber@suse.de, aliguori@amazon.com, hutao@cn.fujitsu.com Subject: [Qemu-devel] [PATCH v2] qdev: add the device to the QOM tree before using it to set a link 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. This patch moves qdev_set_parent_bus() back to object_property_add_child(), we only needs to unref the object if setting properties fails. Signed-off-by: Amos Kong Reviewed-by: Paolo Bonzini Tested-by: Markus Armbruster --- V2: fix bz by adjust the initialization order (Paolo) --- qdev-monitor.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/qdev-monitor.c b/qdev-monitor.c index dc37a43..4070b0a 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -518,16 +518,11 @@ DeviceState *qdev_device_add(QemuOpts *opts) /* create device, set properties */ dev = DEVICE(object_new(driver)); - if (bus) { - qdev_set_parent_bus(dev, bus); - } - id = qemu_opts_id(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; } @@ -541,6 +536,11 @@ DeviceState *qdev_device_add(QemuOpts *opts) OBJECT(dev), NULL); g_free(name); } + + if (bus) { + qdev_set_parent_bus(dev, bus); + } + object_property_set_bool(OBJECT(dev), true, "realized", &err); if (err != NULL) { qerror_report_err(err);