From patchwork Wed Oct 14 16:59:14 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 35996 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 4DF59B7B68 for ; Thu, 15 Oct 2009 04:03:47 +1100 (EST) Received: from localhost ([127.0.0.1]:40512 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1My7GJ-00050w-97 for incoming@patchwork.ozlabs.org; Wed, 14 Oct 2009 13:03:43 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1My7CJ-0002ln-BV for qemu-devel@nongnu.org; Wed, 14 Oct 2009 12:59:35 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1My7CE-0002iL-GW for qemu-devel@nongnu.org; Wed, 14 Oct 2009 12:59:34 -0400 Received: from [199.232.76.173] (port=57095 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1My7CE-0002iB-9c for qemu-devel@nongnu.org; Wed, 14 Oct 2009 12:59:30 -0400 Received: from e8.ny.us.ibm.com ([32.97.182.138]:54903) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1My7CD-0002fp-Rj for qemu-devel@nongnu.org; Wed, 14 Oct 2009 12:59:30 -0400 Received: from d01relay02.pok.ibm.com (d01relay02.pok.ibm.com [9.56.227.234]) by e8.ny.us.ibm.com (8.14.3/8.13.1) with ESMTP id n9ECuTfK000584 for ; Wed, 14 Oct 2009 08:56:29 -0400 Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay02.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id n9EGxM3n247600 for ; Wed, 14 Oct 2009 12:59:22 -0400 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n9EGxJp2011220 for ; Wed, 14 Oct 2009 12:59:21 -0400 Received: from localhost.localdomain (sig-9-65-48-88.mts.ibm.com [9.65.48.88]) by d01av04.pok.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n9EGxExE010751; Wed, 14 Oct 2009 12:59:15 -0400 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 14 Oct 2009 11:59:14 -0500 Message-Id: <1255539554-7956-1-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.2.5 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori , Gerd Hoffman , Dustin Kirkland Subject: [Qemu-devel] [PATCH] [STABLE] Fix virtio-blk hot add after remove 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 qdev_init_bdrv() expects that each drive added is the next logical unit for the given interface type. However, when dealing with hotplug, there may be holes in the units. drive_init reclaims holes in units but qdev_init_bdrv() is not smart enough to do this. Fortunately, in master, this has all been rewritten so for stable, let's hack around this a bit. To fix this, we need to tell qdev that a device has been removed so that it can keep track of which units are allocated. The only way we can get a hook for this though is to attach to the pci uninit callback. In order for virtio-blk to get this, another uninit callback needs to be added to the virtio layer. Suggestions for a less ugly solution are appreciated. This fixes https://bugs.launchpad.net/bugs/419590 Signed-off-by: Anthony Liguori Tested-by: Dustin Kirkland --- hw/qdev.c | 33 ++++++++++++++++++++++++++++++--- hw/virtio-blk.c | 8 ++++++++ hw/virtio-pci.c | 19 ++++++++++++++++++- hw/virtio.h | 1 + sysemu.h | 2 ++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/hw/qdev.c b/hw/qdev.c index faecc76..af60e3e 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -181,21 +181,48 @@ void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr) memcpy(macaddr, dev->nd->macaddr, 6); } -static int next_block_unit[IF_COUNT]; +typedef struct BlockUnitState +{ + BlockDriverState *bs[32]; +} BlockUnitState; +static BlockUnitState next_block_unit[IF_COUNT]; /* Get a block device. This should only be used for single-drive devices (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the appropriate bus. */ BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type) { - int unit = next_block_unit[type]++; + BlockDriverState *bs; + int unit; int index; + unit = 0; + while (next_block_unit[type].bs[unit]) { + unit++; + } + index = drive_get_index(type, 0, unit); if (index == -1) { return NULL; } - return drives_table[index].bdrv; + + bs = drives_table[index].bdrv; + next_block_unit[type].bs[unit] = bs; + + return bs; +} + +void qdev_uninit_bdrv(BlockDriverState *bs, BlockInterfaceType type) +{ + int unit; + + for (unit = 0; unit < 32; unit++) { + if (next_block_unit[type].bs[unit] == bs) { + break; + } + } + + next_block_unit[type].bs[unit] = NULL; } BusState *qdev_get_child_bus(DeviceState *dev, const char *name) diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c index 5036b5b..4bc11e6 100644 --- a/hw/virtio-blk.c +++ b/hw/virtio-blk.c @@ -414,6 +414,13 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) return 0; } +static void virtio_blk_uninit(VirtIODevice *vdev) +{ + VirtIOBlock *s = to_virtio_blk(vdev); + + qdev_uninit_bdrv(s->bs, IF_VIRTIO); +} + VirtIODevice *virtio_blk_init(DeviceState *dev) { VirtIOBlock *s; @@ -430,6 +437,7 @@ VirtIODevice *virtio_blk_init(DeviceState *dev) s->vdev.get_config = virtio_blk_update_config; s->vdev.get_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; + s->vdev.uninit = virtio_blk_uninit; s->bs = bs; s->rq = NULL; if (strlen(ps = (char *)drive_get_serial(bs))) diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c index 703f4fe..00b3998 100644 --- a/hw/virtio-pci.c +++ b/hw/virtio-pci.c @@ -375,6 +375,22 @@ static const VirtIOBindings virtio_pci_bindings = { .load_queue = virtio_pci_load_queue, }; +static int virtio_pci_uninit(PCIDevice *pci_dev) +{ + VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev); + VirtIODevice *vdev = proxy->vdev; + + if (vdev->uninit) { + vdev->uninit(vdev); + } + + if (vdev->nvectors) { + return msix_uninit(pci_dev); + } + + return 0; +} + static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev, uint16_t vendor, uint16_t device, uint16_t class_code, uint8_t pif) @@ -407,10 +423,11 @@ static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev, PCI_ADDRESS_SPACE_MEM, msix_mmio_map); proxy->pci_dev.config_write = virtio_write_config; - proxy->pci_dev.unregister = msix_uninit; } else vdev->nvectors = 0; + proxy->pci_dev.unregister = virtio_pci_uninit; + size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; if (size & (size-1)) size = 1 << qemu_fls(size); diff --git a/hw/virtio.h b/hw/virtio.h index aa55677..330b317 100644 --- a/hw/virtio.h +++ b/hw/virtio.h @@ -103,6 +103,7 @@ struct VirtIODevice void (*get_config)(VirtIODevice *vdev, uint8_t *config); void (*set_config)(VirtIODevice *vdev, const uint8_t *config); void (*reset)(VirtIODevice *vdev); + void (*uninit)(VirtIODevice *vdev); VirtQueue *vq; const VirtIOBindings *binding; void *binding_opaque; diff --git a/sysemu.h b/sysemu.h index ce25109..18f610b 100644 --- a/sysemu.h +++ b/sysemu.h @@ -193,6 +193,8 @@ extern const char *drive_get_serial(BlockDriverState *bdrv); extern BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv); BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type); +void qdev_uninit_bdrv(BlockDriverState *bs, BlockInterfaceType type); + struct drive_opt { const char *file;