From patchwork Thu Jul 1 07:30:33 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 57483 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 BE187B6F44 for ; Thu, 1 Jul 2010 17:49:41 +1000 (EST) Received: from localhost ([127.0.0.1]:58366 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUEWf-0001ij-RW for incoming@patchwork.ozlabs.org; Thu, 01 Jul 2010 03:49:38 -0400 Received: from [140.186.70.92] (port=53152 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUEES-0001q2-OT for qemu-devel@nongnu.org; Thu, 01 Jul 2010 03:30:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OUEEP-0007av-D9 for qemu-devel@nongnu.org; Thu, 01 Jul 2010 03:30:48 -0400 Received: from oxygen.pond.sub.org ([213.239.205.148]:58074) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OUEEP-0007a6-0t for qemu-devel@nongnu.org; Thu, 01 Jul 2010 03:30:45 -0400 Received: from blackfin.pond.sub.org (pD951BCA6.dip.t-dialin.net [217.81.188.166]) by oxygen.pond.sub.org (Postfix) with ESMTPA id 1D6E8312888; Thu, 1 Jul 2010 09:30:43 +0200 (CEST) Received: by blackfin.pond.sub.org (Postfix, from userid 500) id B9EA82B8; Thu, 1 Jul 2010 09:30:41 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 1 Jul 2010 09:30:33 +0200 Message-Id: <1277969441-22879-6-git-send-email-armbru@redhat.com> X-Mailer: git-send-email 1.6.6.1 In-Reply-To: <1277969441-22879-1-git-send-email-armbru@redhat.com> References: <1277969441-22879-1-git-send-email-armbru@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: kwolf@redhat.com, kraxel@redhat.com, hch@lst.de Subject: [Qemu-devel] [PATCH v3 05/13] blockdev: Clean up automatic drive deletion 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 We automatically delete blockdev host parts on unplug of the guest device. Too much magic, but we can't change that now. The delete happens early in the guest device teardown, before the connection to the host part is severed. Thus, the guest part's pointer to the host part dangles for a brief time. No actual harm comes from this, but we'll catch such dangling pointers a few commits down the road. Clean up the dangling pointers by delaying the automatic deletion until the guest part's pointer is gone. Device usb-storage deliberately makes two qdev properties refer to the same drive, because it automatically creates a second device. Again, too much magic we can't change now. Multiple references worked okay before, but now free_drive() dies for the second one. Zap the extra reference. Signed-off-by: Markus Armbruster --- blockdev.c | 23 +++++++++++++++++++++++ blockdev.h | 4 ++++ hw/qdev-properties.c | 10 ++++++++++ hw/scsi-disk.c | 2 +- hw/scsi-generic.c | 2 +- hw/usb-msd.c | 20 ++++++++++++++++---- hw/virtio-pci.c | 2 +- 7 files changed, 56 insertions(+), 7 deletions(-) diff --git a/blockdev.c b/blockdev.c index ba4f66f..4848112 100644 --- a/blockdev.c +++ b/blockdev.c @@ -17,6 +17,29 @@ static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); +/* + * We automatically delete the drive when a device using it gets + * unplugged. Questionable feature, but we can't just drop it. + * Device models call blockdev_mark_auto_del() to schedule the + * automatic deletion, and generic qdev code calls blockdev_auto_del() + * when deletion is actually safe. + */ +void blockdev_mark_auto_del(BlockDriverState *bs) +{ + DriveInfo *dinfo = drive_get_by_blockdev(bs); + + dinfo->auto_del = 1; +} + +void blockdev_auto_del(BlockDriverState *bs) +{ + DriveInfo *dinfo = drive_get_by_blockdev(bs); + + if (dinfo->auto_del) { + drive_uninit(dinfo); + } +} + QemuOpts *drive_add(const char *file, const char *fmt, ...) { va_list ap; diff --git a/blockdev.h b/blockdev.h index 6ab592f..32e6979 100644 --- a/blockdev.h +++ b/blockdev.h @@ -13,6 +13,9 @@ #include "block.h" #include "qemu-queue.h" +void blockdev_mark_auto_del(BlockDriverState *bs); +void blockdev_auto_del(BlockDriverState *bs); + typedef enum { IF_NONE, IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO, IF_XEN, @@ -28,6 +31,7 @@ typedef struct DriveInfo { BlockInterfaceType type; int bus; int unit; + int auto_del; /* see blockdev_mark_auto_del() */ QemuOpts *opts; char serial[BLOCK_SERIAL_STRLEN + 1]; QTAILQ_ENTRY(DriveInfo) next; diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index 5a8739d..15ca6d3 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -293,6 +293,15 @@ static int parse_drive(DeviceState *dev, Property *prop, const char *str) return 0; } +static void free_drive(DeviceState *dev, Property *prop) +{ + DriveInfo **ptr = qdev_get_prop_ptr(dev, prop); + + if (*ptr) { + blockdev_auto_del((*ptr)->bdrv); + } +} + static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len) { DriveInfo **ptr = qdev_get_prop_ptr(dev, prop); @@ -305,6 +314,7 @@ PropertyInfo qdev_prop_drive = { .size = sizeof(DriveInfo*), .parse = parse_drive, .print = print_drive, + .free = free_drive, }; /* --- character device --- */ diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c index 2b38984..d76e640 100644 --- a/hw/scsi-disk.c +++ b/hw/scsi-disk.c @@ -1043,7 +1043,7 @@ static void scsi_destroy(SCSIDevice *dev) SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); scsi_disk_purge_requests(s); - drive_uninit(s->qdev.conf.dinfo); + blockdev_mark_auto_del(s->qdev.conf.dinfo->bdrv); } static int scsi_disk_initfn(SCSIDevice *dev) diff --git a/hw/scsi-generic.c b/hw/scsi-generic.c index e31060e..1859c94 100644 --- a/hw/scsi-generic.c +++ b/hw/scsi-generic.c @@ -453,7 +453,7 @@ static void scsi_destroy(SCSIDevice *d) r = DO_UPCAST(SCSIGenericReq, req, QTAILQ_FIRST(&s->qdev.requests)); scsi_remove_request(r); } - drive_uninit(s->qdev.conf.dinfo); + blockdev_mark_auto_del(s->qdev.conf.dinfo->bdrv); } static int scsi_generic_initfn(SCSIDevice *dev) diff --git a/hw/usb-msd.c b/hw/usb-msd.c index 8e9718c..3dbfcab 100644 --- a/hw/usb-msd.c +++ b/hw/usb-msd.c @@ -522,24 +522,36 @@ static void usb_msd_password_cb(void *opaque, int err) static int usb_msd_initfn(USBDevice *dev) { MSDState *s = DO_UPCAST(MSDState, dev, dev); + DriveInfo *dinfo = s->conf.dinfo; - if (!s->conf.dinfo || !s->conf.dinfo->bdrv) { + if (!dinfo || !dinfo->bdrv) { error_report("usb-msd: drive property not set"); return -1; } + /* + * Hack alert: this pretends to be a block device, but it's really + * a SCSI bus that can serve only a single device, which it + * creates automatically. Two drive properties pointing to the + * same drive is not good: free_drive() dies for the second one. + * Zap the one we're not going to use. + * + * The hack is probably a bad idea. + */ + s->conf.dinfo = NULL; + s->dev.speed = USB_SPEED_FULL; scsi_bus_new(&s->bus, &s->dev.qdev, 0, 1, usb_msd_command_complete); - s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, s->conf.dinfo, 0); + s->scsi_dev = scsi_bus_legacy_add_drive(&s->bus, dinfo, 0); if (!s->scsi_dev) { return -1; } s->bus.qbus.allow_hotplug = 0; usb_msd_handle_reset(dev); - if (bdrv_key_required(s->conf.dinfo->bdrv)) { + if (bdrv_key_required(dinfo->bdrv)) { if (cur_mon) { - monitor_read_bdrv_key_start(cur_mon, s->conf.dinfo->bdrv, + monitor_read_bdrv_key_start(cur_mon, dinfo->bdrv, usb_msd_password_cb, s); s->dev.auto_attach = 0; } else { diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c index d1303b1..31a68fe 100644 --- a/hw/virtio-pci.c +++ b/hw/virtio-pci.c @@ -571,7 +571,7 @@ static int virtio_blk_exit_pci(PCIDevice *pci_dev) { VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); - drive_uninit(proxy->block.dinfo); + blockdev_mark_auto_del(proxy->block.dinfo->bdrv); return virtio_exit_pci(pci_dev); }