From patchwork Fri Jan 25 11:46:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 215663 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 6574A2C0085 for ; Sat, 26 Jan 2013 00:11:43 +1100 (EST) Received: from localhost ([::1]:38051 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tyj4H-0006IV-F1 for incoming@patchwork.ozlabs.org; Fri, 25 Jan 2013 08:11:41 -0500 Received: from eggs.gnu.org ([208.118.235.92]:48707) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tyj47-0006Ht-EK for qemu-devel@nongnu.org; Fri, 25 Jan 2013 08:11:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tyj41-0001qG-S8 for qemu-devel@nongnu.org; Fri, 25 Jan 2013 08:11:31 -0500 Received: from mail-qc0-f169.google.com ([209.85.216.169]:63074) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tyhke-0005Bd-VN for qemu-devel@nongnu.org; Fri, 25 Jan 2013 06:47:21 -0500 Received: by mail-qc0-f169.google.com with SMTP id t2so124537qcq.28 for ; Fri, 25 Jan 2013 03:47:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=NNc8O8e1qscq+LxYHEaP8YHp+DOUd3CN3/rxkk6F0K4=; b=AWKwOqkfgw7OOARBvDuOre2goP9Ab7OOGzEp8P1dxlrMg1Pg3SqtaJ2iqMYCyU2BYV AZ18zbd0RuxBEbXlDqlVjhe3MqI0ayp9tOXspgTKCHVCIaZJn8CRYyvHBPNU1otxwS4T p8l8yyw9DCEQcbzNU5ti0aIVJ4KEO9LON+s3l4qd64bTFRGkJ6Mn9MCtxs3SDiIl3xGN VEPsdpMVRlFK5xtMqK4UJHCCR3HaJWve8Mp3dtoz32/s+MGEMuFQDnUXvWhAPjR3AAuV gRc4K6FLu/ffwvKD2/Qjtpzn5X+XTbWhpSuqxMefCczEiq4tmSMT9wGDblcEzU9KrpwH jduA== X-Received: by 10.49.62.42 with SMTP id v10mr6630940qer.2.1359114440314; Fri, 25 Jan 2013 03:47:20 -0800 (PST) Received: from yakj.usersys.redhat.com (93-34-179-137.ip50.fastwebnet.it. [93.34.179.137]) by mx.google.com with ESMTPS id eg9sm419389qab.7.2013.01.25.03.47.18 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 25 Jan 2013 03:47:19 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 25 Jan 2013 12:46:54 +0100 Message-Id: <1359114420-16149-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1 In-Reply-To: <1359114420-16149-1-git-send-email-pbonzini@redhat.com> References: <1359114420-16149-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.216.169 Cc: afaerber@suse.de Subject: [Qemu-devel] [PATCH v3 06/12] qdev: move deletion of children from finalize to unparent 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 A device will never be finalized as long as it has a reference from other devices that sit on its buses. To ensure that the references go away, deassociate a bus from its children in the unparent callback for the bus. Signed-off-by: Paolo Bonzini --- hw/qdev.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 78eedf0..364386d 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -433,6 +433,25 @@ static void qbus_realize(BusState *bus, DeviceState *parent, const char *name) } } +static void bus_unparent(Object *obj) +{ + BusState *bus = BUS(obj); + BusChild *kid; + + while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { + DeviceState *dev = kid->child; + qdev_free(dev); + } + if (bus->parent) { + QLIST_REMOVE(bus, sibling); + bus->parent->num_child_bus--; + bus->parent = NULL; + } else { + assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ + qemu_unregister_reset(qbus_reset_all_fn, bus); + } +} + void qbus_create_inplace(BusState *bus, const char *typename, DeviceState *parent, const char *name) { @@ -805,22 +824,15 @@ static void qbus_initfn(Object *obj) QTAILQ_INIT(&bus->children); } +static void bus_class_init(ObjectClass *class, void *data) +{ + class->unparent = bus_unparent; +} + static void qbus_finalize(Object *obj) { BusState *bus = BUS(obj); - BusChild *kid; - while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) { - DeviceState *dev = kid->child; - qdev_free(dev); - } - if (bus->parent) { - QLIST_REMOVE(bus, sibling); - bus->parent->num_child_bus--; - } else { - assert(bus != sysbus_get_default()); /* main_system_bus is never freed */ - qemu_unregister_reset(qbus_reset_all_fn, bus); - } g_free((char *)bus->name); } @@ -832,6 +844,7 @@ static const TypeInfo bus_info = { .class_size = sizeof(BusClass), .instance_init = qbus_initfn, .instance_finalize = qbus_finalize, + .class_init = bus_class_init, }; static void qdev_register_types(void)