Skip to contentSkip to conversation

v2 Decouple block device removal from device removal

PATCH In review Ryan Harper 5 participants 9 messages Oct 19, 2010 – Oct 29, 2010

lore
Share & tools
lore
#0 lore raw

cover v2 Decouple block device removal from device removal

This patch series decouples the detachment of a block device from the removal of the backing pci-device. Removal of a hotplugged pci device requires the guest to respond before qemu tears down the block device. In some cases, the guest may not respond leaving the guest with continued access to the block device.

The new monitor command, drive_unplug, will revoke a guests access to the block device independently of the removal of the pci device.

The first patch adds a new drive find method, the second patch implements the monitor command and block layer changes.

Changes since v1:
- CodingStyle fixes
- Added qemu_aio_flush() to bdrv_unplug()

#2 lore raw

PATCH 2/2 v2 Fix Block Hotplug race with drive_unplug()

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device. We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close(). Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

Changes since v1:
- Added qemu_aio_flush() before bdrv_flush() to wait on pending io

---
 block.c         |    7 +++++++
 block.h         |    1 +
 blockdev.c      |   26 ++++++++++++++++++++++++++
 blockdev.h      |    1 +
 hmp-commands.hx |   15 +++++++++++++++
 5 files changed, 50 insertions(+), 0 deletions(-)
block.c +7 -0
index a19374d..be47655 100644
@@ -1328,6 +1328,13 @@ void bdrv_set_removable(BlockDriverState *bs, int removable)
     }
 }
 
+void bdrv_unplug(BlockDriverState *bs)
+{
+    qemu_aio_flush();
+    bdrv_flush(bs);
+    bdrv_close(bs);
+}
+
 int bdrv_is_removable(BlockDriverState *bs)
 {
     return bs->removable;
block.h +1 -0
index 5f64380..732f63e 100644
@@ -171,6 +171,7 @@ void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
                        BlockErrorAction on_write_error);
 BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
 void bdrv_set_removable(BlockDriverState *bs, int removable);
+void bdrv_unplug(BlockDriverState *bs);
 int bdrv_is_removable(BlockDriverState *bs);
 int bdrv_is_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
blockdev.c +26 -0
index 5fc3b9b..68eb329 100644
@@ -610,3 +610,29 @@ int do_change_block(Monitor *mon, const char *device,
     }
     return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
 }
+
+int do_drive_unplug(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    DriveInfo *dinfo;
+    BlockDriverState *bs;
+    const char *id;
+
+    if (!qdict_haskey(qdict, "id")) {
+        qerror_report(QERR_MISSING_PARAMETER, "id");
+        return -1;
+    }
+
+    id = qdict_get_str(qdict, "id");
+    dinfo = drive_get_by_id(id);
+    if (!dinfo) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, id);
+        return -1;
+    }
+
+    /* mark block device unplugged */
+    bs = dinfo->bdrv;
+    bdrv_unplug(bs);
+
+    return 0;
+}
+ 
blockdev.h +1 -0
index 19c6915..ecb9ac8 100644
@@ -52,5 +52,6 @@ int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_block_set_passwd(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_change_block(Monitor *mon, const char *device,
                     const char *filename, const char *fmt);
+int do_drive_unplug(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 #endif
hmp-commands.hx +15 -0
index 81999aa..7a32a2e 100644
@@ -68,6 +68,21 @@ Eject a removable medium (use -f to force it).
 ETEXI
 
     {
+        .name       = "drive_unplug",
+        .args_type  = "id:s",
+        .params     = "device",
+        .help       = "unplug block device",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_drive_unplug,
+    },
+
+STEXI
+@item unplug @var{device}
+@findex unplug
+Unplug block device.
+ETEXI
+
+    {
         .name       = "change",
         .args_type  = "device:B,target:F,arg:s?",
         .params     = "device filename [format]",
Signature · 1 line
1.6.3.3
#1 lore raw

PATCH 1/2 v2 Add drive_get_by_id

In reply to Ryan Harper

Add a function to find a drive by id string.

Changes since v1:
-Coding Style fix

---
 blockdev.c |   13 +++++++++++++
 blockdev.h |    1 +
 2 files changed, 14 insertions(+), 0 deletions(-)
blockdev.c +13 -0
index ff7602b..5fc3b9b 100644
@@ -75,6 +75,19 @@ DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
     return NULL;
 }
 
+DriveInfo *drive_get_by_id(const char *id)
+{
+    DriveInfo *dinfo;
+
+    QTAILQ_FOREACH(dinfo, &drives, next) {
+        if (strcmp(id, dinfo->id)) {
+            continue;
+        }
+        return dinfo;
+    }
+    return NULL;
+}
+
 int drive_get_max_bus(BlockInterfaceType type)
 {
     int max_bus;
blockdev.h +1 -0
index 653affc..19c6915 100644
@@ -38,6 +38,7 @@ DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
 int drive_get_max_bus(BlockInterfaceType type);
 void drive_uninit(DriveInfo *dinfo);
 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs);
+DriveInfo *drive_get_by_id(const char *id);
 
 QemuOpts *drive_add(const char *file, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi, int *fatal_error);
Signature · 1 line
1.6.3.3
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] v2 Fix Block Hotplug race with drive_unplug()

In reply to Ryan Harper
Quoted 22 lines · Ryan Harper

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device.  We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close().  Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

Changes since v1:
- Added qemu_aio_flush() before bdrv_flush() to wait on pending io

Quoted 8 lines

---
 block.c         |    7 +++++++
 block.h         |    1 +
 blockdev.c      |   26 ++++++++++++++++++++++++++ blockdev.h      |    1 +
 hmp-commands.hx |   15 +++++++++++++++

 5 files changed, 50 insertions(+), 0 deletions(-)

Looks good to me.

Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] v2 Fix Block Hotplug race with drive_unplug()

In reply to Ryan Harper
Quoted 15 lines · Ryan Harper

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device. We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close(). Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

Quoted 3 lines

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

The name 'drive_unplug' suggests to me that the drive object is not being deleted/free()d ? Is that correct understanding, and if so, what is responsible for finally free()ing the drive backend ?

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London    -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :|
|: http://autobuild.org        -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] v2 Fix Block Hotplug race with drive_unplug()

Quoted 16 lines · Daniel P. Berrange
Quoted 15 lines · Ryan Harper

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device. We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close(). Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

Quoted 7 lines
Quoted 3 lines

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

The name 'drive_unplug' suggests to me that the drive object is not being deleted/free()d ? Is that correct understanding, and if so, what is responsible for finally free()ing the drive backend ?

It's technically the BlockDriverState Driver that we're closing. To fully release the remaining resources, a device_del is required (which of course requires guest participation with the current interface).

Once QEMU issues the removal request, the guest responds and the piix4 acpi handler for pciej_write writes invokes qdev_free() on the target device. qdev_free() on the pci device will make it's way to the qdev exit handler registered for virtio-blk devices, virtio_blk_exit_pci(). virtio_blk_exit_pci() marks the drive structure for deletion. When qdev calls the properties handler, it invokes free_drive() on the disk and that calls blockdev_auto_del() which will do a bdrv_delete() which nukes the remaining objects (the acutal BlockDriverState).

I think I got the whole path in there.

Signature · 4 lines
Ryan Harper
Software Engineer; Linux Technology Center
IBM Corp., Austin, Tx
[email protected]
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] v2 Fix Block Hotplug race with drive_unplug()

Quoted 39 lines · Ryan Harper
Quoted 16 lines · Daniel P. Berrange
Quoted 15 lines · Ryan Harper

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device. We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close(). Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

Quoted 7 lines
Quoted 3 lines

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

The name 'drive_unplug' suggests to me that the drive object is not being deleted/free()d ? Is that correct understanding, and if so, what is responsible for finally free()ing the drive backend ?

It's technically the BlockDriverState Driver that we're closing. To fully release the remaining resources, a device_del is required (which of course requires guest participation with the current interface).

Once QEMU issues the removal request, the guest responds and the piix4 acpi handler for pciej_write writes invokes qdev_free() on the target device. qdev_free() on the pci device will make it's way to the qdev exit handler registered for virtio-blk devices, virtio_blk_exit_pci(). virtio_blk_exit_pci() marks the drive structure for deletion. When qdev calls the properties handler, it invokes free_drive() on the disk and that calls blockdev_auto_del() which will do a bdrv_delete() which nukes the remaining objects (the acutal BlockDriverState).

Quoted 1 line

I think I got the whole path in there.

Ok, thanks, that makes sense to me.

Sounds like we do still need a separate drive_del in the future to handle the different case of, drive_add, followed by a device_add attempt which fails.

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London    -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :|
|: http://autobuild.org        -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] v2 Fix Block Hotplug race with drive_unplug()

In reply to Ryan Harper

Am 21.10.2010 23:37, schrieb Ryan Harper:

Quoted 21 lines · Daniel P. Berrange
Quoted 20 lines · Ryan Harper
Quoted 18 lines

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device. We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close(). Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

Quoted 8 lines
Quoted 3 lines

The name 'drive_unplug' suggests to me that the drive object is not being deleted/free()d ? Is that correct understanding, and if so, what is responsible for finally free()ing the drive backend ?

It's technically the BlockDriverState Driver that we're closing. To fully release the remaining resources, a device_del is required (which of course requires guest participation with the current interface).

So is this basically what blockdev_del is supposed to become one day?

Copying Markus to have a look at this. I'm sure he has some thoughts on it as he was planning to implement blockdev_add/del.

Kevin

Quoted 11 lines

Once QEMU issues the removal request, the guest responds and the piix4 acpi handler for pciej_write writes invokes qdev_free() on the target device. qdev_free() on the pci device will make it's way to the qdev exit handler registered for virtio-blk devices, virtio_blk_exit_pci(). virtio_blk_exit_pci() marks the drive structure for deletion. When qdev calls the properties handler, it invokes free_drive() on the disk and that calls blockdev_auto_del() which will do a bdrv_delete() which nukes the remaining objects (the acutal BlockDriverState).

I think I got the whole path in there.

#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] v2 Fix Block Hotplug race with drive_unplug()

Quoted 31 lines · Kevin Wolf

Am 21.10.2010 23:37, schrieb Ryan Harper:

Quoted 21 lines · Daniel P. Berrange
Quoted 20 lines · Ryan Harper
Quoted 18 lines

Block hot unplug is racy since the guest is required to acknowlege the ACPI unplug event; this may not happen synchronously with the device removal command

This series aims to close a gap where by mgmt applications that assume the block resource has been removed without confirming that the guest has acknowledged the removal may re-assign the underlying device to a second guest leading to data leakage.

This series introduces a new montor command to decouple asynchornous device removal from restricting guest access to a block device. We do this by creating a new monitor command drive_unplug which maps to a bdrv_unplug() command which does a qemu_aio_flush; bdrv_flush() and bdrv_close(). Once complete, subsequent IO is rejected from the device and the guest will get IO errors but continue to function.

A subsequent device removal command can be issued to remove the device, to which the guest may or maynot respond, but as long as the unplugged bit is set, no IO will be sumbitted.

Quoted 8 lines
Quoted 3 lines

The name 'drive_unplug' suggests to me that the drive object is not being deleted/free()d ? Is that correct understanding, and if so, what is responsible for finally free()ing the drive backend ?

It's technically the BlockDriverState Driver that we're closing. To fully release the remaining resources, a device_del is required (which of course requires guest participation with the current interface).

Quoted 4 lines

So is this basically what blockdev_del is supposed to become one day?

Copying Markus to have a look at this. I'm sure he has some thoughts on it as he was planning to implement blockdev_add/del.

Yes, Ryan's drive_unplug is quite close to my blockdev_del. However, my blockdev_del is part of a more ambitious job, namely to cleanly separate host and guest part of block devices. A whole lot of preliminary cleanups have made it in so far, but not the actual commands.

I'll reply in more detail to the latest version of the patch series.

[...]