From patchwork Fri Sep 18 09:24:05 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 33825 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id F2D5BB7B73 for ; Fri, 18 Sep 2009 19:40:39 +1000 (EST) Received: from localhost ([127.0.0.1]:49785 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MoZxF-0006PI-3t for incoming@patchwork.ozlabs.org; Fri, 18 Sep 2009 05:40:37 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MoZhd-0002Bg-Ce for qemu-devel@nongnu.org; Fri, 18 Sep 2009 05:24:29 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MoZhX-00029Z-VL for qemu-devel@nongnu.org; Fri, 18 Sep 2009 05:24:28 -0400 Received: from [199.232.76.173] (port=48490 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MoZhX-00029N-IZ for qemu-devel@nongnu.org; Fri, 18 Sep 2009 05:24:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56433) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MoZhX-0007tR-1a for qemu-devel@nongnu.org; Fri, 18 Sep 2009 05:24:23 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n8I9OMQr018461 for ; Fri, 18 Sep 2009 05:24:22 -0400 Received: from zweiblum.home.kraxel.org (vpn1-4-24.ams2.redhat.com [10.36.4.24]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id n8I9OHfW014256; Fri, 18 Sep 2009 05:24:18 -0400 Received: by zweiblum.home.kraxel.org (Postfix, from userid 500) id D05D7700E2; Fri, 18 Sep 2009 11:24:12 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Fri, 18 Sep 2009 11:24:05 +0200 Message-Id: <1253265851-28919-3-git-send-email-kraxel@redhat.com> In-Reply-To: <1253265851-28919-1-git-send-email-kraxel@redhat.com> References: <1253265851-28919-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH 2/8] qdev: device free fixups. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Two bug fixes: * When freeing a device we unregister stuff unconditionally, even in case we didn't register in the first place because the ->init() callback failed. * When freeing a device with child busses attached we don't zap the child bus (and the devices attached to it). Signed-off-by: Gerd Hoffmann --- hw/qdev.c | 35 +++++++++++++++++++++++++++++++---- hw/qdev.h | 7 +++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index 43372c1..4931da1 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -102,6 +102,7 @@ DeviceState *qdev_create(BusState *bus, const char *name) qdev_prop_set_defaults(dev, dev->parent_bus->info->props); qdev_prop_set_compat(dev); QLIST_INSERT_HEAD(&bus->children, dev, sibling); + dev->state = DEV_STATE_CREATED; return dev; } @@ -216,6 +217,7 @@ int qdev_init(DeviceState *dev) { int rc; + assert(dev->state == DEV_STATE_CREATED); rc = dev->info->init(dev, dev->info); if (rc < 0) return rc; @@ -223,18 +225,27 @@ int qdev_init(DeviceState *dev) qemu_register_reset(dev->info->reset, dev); if (dev->info->vmsd) vmstate_register(-1, dev->info->vmsd, dev); + dev->state = DEV_STATE_INITIALIZED; return 0; } /* Unlink device from bus and free the structure. */ void qdev_free(DeviceState *dev) { + BusState *bus; + + if (dev->state == DEV_STATE_INITIALIZED) { + while (dev->num_child_bus) { + bus = QLIST_FIRST(&dev->child_bus); + qbus_free(bus); + } #if 0 /* FIXME: need sane vmstate_unregister function */ - if (dev->info->vmsd) - vmstate_unregister(dev->info->vmsd, dev); + if (dev->info->vmsd) + vmstate_unregister(dev->info->vmsd, dev); #endif - if (dev->info->reset) - qemu_unregister_reset(dev->info->reset, dev); + if (dev->info->reset) + qemu_unregister_reset(dev->info->reset, dev); + } QLIST_REMOVE(dev, sibling); qemu_free(dev); } @@ -549,6 +560,22 @@ BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name) return bus; } +void qbus_free(BusState *bus) +{ + DeviceState *dev; + + while ((dev = QLIST_FIRST(&bus->children)) != NULL) { + qdev_free(dev); + } + if (bus->parent) { + QLIST_REMOVE(bus, sibling); + bus->parent->num_child_bus--; + } + if (bus->qdev_allocated) { + qemu_free(bus); + } +} + #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) static void qbus_print(Monitor *mon, BusState *bus, int indent); diff --git a/hw/qdev.h b/hw/qdev.h index ccc45b9..c036aff 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -19,10 +19,16 @@ typedef struct BusState BusState; typedef struct BusInfo BusInfo; +enum DevState { + DEV_STATE_CREATED = 1, + DEV_STATE_INITIALIZED, +}; + /* This structure should not be accessed directly. We declare it here so that it can be embedded in individual device state structures. */ struct DeviceState { const char *id; + enum DevState state; DeviceInfo *info; BusState *parent_bus; int num_gpio_out; @@ -148,6 +154,7 @@ BusState *qdev_get_parent_bus(DeviceState *dev); void qbus_create_inplace(BusState *bus, BusInfo *info, DeviceState *parent, const char *name); BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name); +void qbus_free(BusState *bus); #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)