scsi: clean up persistent reservation state after migration
Subsystems (3)
cover scsi: clean up persistent reservation state after migration
When a guest using SCSI persistent reservations is migrated on a Linux host using multipathd, some state is left behind and this can cause multipathd to register the old key again in the future. One way this can be observed is when migrating a guest back-and-forth between two hosts: the REGISTER command will fail when migrating back to the first host if the LUN is detached and re-attached between migrations (i.e. multipathd sees that a path has come up).
This patch series modifies qemu-pr-helper to send multipathd commands that clear the state once migration has completed successfully. This is implemented as a vendor-specific SCSI command called PR_HELPER_CLEANUP that QEMU sends to qemu-pr-helper just like PERSISTENT RESERVE IN and PERSISTENT RESERVE OUT commands.
I considered adding a separate interface or more complex qemu-pr-helper protocol extension that does not reuse SCSI commands because that would be cleaner. Since the command does not have security implications (it's a variant of REGISTER AND IGNORE EXISTING KEYS), I went with this approach for simplicity. Let me know if you prefer a different interface and what that it should look like.
Stefan Hajnoczi (8):
pr-manager-helper: fix sense buffer overflow
scsi: increase sense buffer size in scsi_SG_IO() hw/scsi-disk: move MIG_EVENT_FAILED into a new function
qemu-pr-helper: add PR_HELPER_CLEANUP for live migration
pr-manager-helper: add PR_HELPER_CLEANUP command support
file-posix: forward PR_HELPER_CLEANUP commands to pr-manager
scsi: clean up persistent reservation state after migration
scsi: use REGISTER AND IGNORE EXISTING KEY for robustness
meson.build | 2 + include/hw/scsi/scsi.h | 1 + include/scsi/constants.h | 1 + scsi/pr-helper.h | 3 + block/file-posix.c | 3 +- hw/scsi/scsi-disk.c | 45 +++++++++--- hw/scsi/scsi-generic.c | 64 +++++++++++++---- scsi/pr-manager-helper.c | 23 ++++-- scsi/qemu-pr-helper.c | 146 ++++++++++++++++++++++++++++++++++++--- scsi/utils.c | 3 + 10 files changed, 251 insertions(+), 40 deletions(-)
Signature · 1 line
2.56.0
PATCH 1/8 pr-manager-helper: fix sense buffer overflow
scsi_build_sense() assumes the sense buffer is SCSI_SENSE_LEN (18) bytes long, so the caller's buffer with an arbitrary size cannot be used. Use our own sense buffer (resp.sense) as an intermediate buffer and honor the caller's sense buffer size (io_hdr->mx_sb_len).
This is triggered when scsi-generic.c:scsi_SG_IO() calls blk_ioctl() with an 8-byte sense buffer. There does not appear to be a way for the guest to trigger this buffer overflow directly since SCSIRequest->sense[] is 252 bytes long.
--- scsi/pr-manager-helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
scsi/pr-manager-helper.c +2 -1
@@ -234,10 +234,11 @@ static int pr_manager_helper_run(PRManager *p, out:if (ret <0 ) { -int sense_len =scsi_build_sense (io_hdr->sbp, +int sense_len =scsi_build_sense (resp.sense,SENSE_CODE (LUN_COMM_FAILURE )); io_hdr->driver_status =SG_ERR_DRIVER_SENSE ; io_hdr->sb_len_wr =MIN (io_hdr->mx_sb_len, sense_len); +memcpy (io_hdr->sbp, resp.sense, io_hdr->sb_len_wr); io_hdr->status =CHECK_CONDITION ; }qemu_mutex_unlock (&pr_mgr->lock);
Signature · 1 line
2.56.0
Re: [PATCH 1/8] pr-manager-helper: fix sense buffer overflow
Quoted 10 lines · Stefan Hajnoczi
scsi_build_sense()assumes the sense buffer isSCSI_SENSE_LEN(18) bytes long, so the caller's buffer with an arbitrary size cannot be used. Use our own sense buffer (resp.sense) as an intermediate buffer and honor the caller's sense buffer size (io_hdr->mx_sb_len).This is triggered when scsi-generic.c:scsi_SG_IO() calls
blk_ioctl()with an 8-byte sense buffer. There does not appear to be a way for the guest to trigger this buffer overflow directly since SCSIRequest->sense[] is 252 bytes long.
Quoted 5 lines
Fixes 9bad2a6b9d0a ("scsi: add persistent reservation manager using qemu-pr-helper")Signed-off-by Stefan Hajnoczi <[email protected]>--- scsi/pr-manager-helper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
PATCH 2/8 scsi: increase sense buffer size in scsi_SG_IO()
In reply to Stefan HajnocziThere are different SCSI sense formats. Increase the size so we can get more detailed sense data.
--- hw/scsi/scsi-generic.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
hw/scsi/scsi-generic.c +13 -10
@@ -923,7 +923,7 @@ int scsi_SG_IO(BlockBackend *blk, int direction, uint8_t *cmd, uint32_t timeout, Error **errp) { sg_io_hdr_t io_header; -uint8_t sensebuf[8 ] = {}; +uint8_t sensebuf[SCSI_SENSE_LEN ] = {};int ret;memset (&io_header,0 ,sizeof (io_header));
@@ -947,15 +947,18 @@ int scsi_SG_IO(BlockBackend *blk, int direction, uint8_t *cmd,error_setg_errno (errp, -ret,"SG_IO ioctl failed" ); }else { g_autofree char *sensebuf_hex = -g_strdup_printf ("%02x%02x%02x%02x%02x%02x%02x%02x" , - sensebuf[0 ], - sensebuf[1 ], - sensebuf[2 ], - sensebuf[3 ], - sensebuf[4 ], - sensebuf[5 ], - sensebuf[6 ], - sensebuf[7 ]); +g_strdup_printf ("%02x%02x%02x%02x%02x%02x%02x%02x" +"%02x%02x%02x%02x%02x%02x%02x%02x" +"%02x%02x" , + sensebuf[0 ], sensebuf[1 ], + sensebuf[2 ], sensebuf[3 ], + sensebuf[4 ], sensebuf[5 ], + sensebuf[6 ], sensebuf[7 ], + sensebuf[8 ], sensebuf[9 ], + sensebuf[10 ], sensebuf[11 ], + sensebuf[12 ], sensebuf[13 ], + sensebuf[14 ], sensebuf[15 ], + sensebuf[16 ], sensebuf[17 ]);error_setg (errp,"SG_IO SCSI command failed with status=0x%x " "driver_status=0x%x host_status=0x%x sensebuf=%s "
Signature · 1 line
2.56.0
Re: [PATCH 2/8] scsi: increase sense buffer size in scsi_SG_IO()
Quoted 7 lines · Stefan Hajnoczi
There are different SCSI sense formats. Increase the size so we can get more detailed sense data.
Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/scsi/scsi-generic.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
PATCH 3/8 hw/scsi-disk: move MIG_EVENT_FAILED into a new function
In reply to Stefan HajnocziMove the MIG_EVENT_FAILED logic from scsi_block_migration_notifier() into its own new scsi_block_migration_failed() function. The next commit adds logic for handling another event, so doing that in dedicated functions makes the code easier to read.
--- hw/scsi/scsi-disk.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
hw/scsi/scsi-disk.c +16 -10
@@ -2761,20 +2761,26 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,* fails because the destination may have already preempted and we need to get * the reservation back. */ +static void scsi_block_migration_failed (SCSIDiskState *s) +{ +SCSIDevice *d = &s->qdev; +Error *local_err =NULL ; + +if (!scsi_generic_pr_state_preempt (d, &local_err)) { +/* MIG_EVENT_FAILED cannot fail, so just warn */ +error_prepend (&local_err,"scsi-block migration rollback: " ); +warn_report_err (local_err); + } +} +static int scsi_block_migration_notifier (NotifierWithReturn *notifier,MigrationEvent *e,Error **errp) { -if (e->type ==MIG_EVENT_FAILED ) { -SCSIDiskState *s = -container_of (notifier, SCSIDiskState, migration_notifier); -SCSIDevice *d = &s->qdev; -Error *local_err =NULL ; +SCSIDiskState *s = +container_of (notifier, SCSIDiskState, migration_notifier); -if (!scsi_generic_pr_state_preempt (d, &local_err)) { -/* MIG_EVENT_FAILED cannot fail, so just warn */ -error_prepend (&local_err,"scsi-block migration rollback: " ); -warn_report_err (local_err); - } +if (e->type ==MIG_EVENT_FAILED ) { +scsi_block_migration_failed (s); }return 0 ; }
Signature · 1 line
2.56.0
Re: [PATCH 3/8] hw/scsi-disk: move MIG_EVENT_FAILED into a new function
Quoted 16 lines · Stefan Hajnoczi
Move the
MIG_EVENT_FAILEDlogic fromscsi_block_migration_notifier()into its own newscsi_block_migration_failed()function. The next commit adds logic for handling another event, so doing that in dedicated functions makes the code easier to read.Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/scsi/scsi-disk.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)hw/scsi/scsi-disk.c +0
-0
hw/scsi/scsi-disk.c static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, · line 2763
Quoted 3 lines
hw/scsi/scsi-disk.c +0
-0@@ -2761,20 +2761,26 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,* fails because the destination may have already preempted and we need to get * the reservation back. */
I think this comment...
hw/scsi/scsi-disk.c static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, · line 2776
Quoted 13 lines
hw/scsi/scsi-disk.c +12
-0+static void scsi_block_migration_failed (SCSIDiskState *s) +{ +SCSIDevice *d = &s->qdev; +Error *local_err =NULL ; + +if (!scsi_generic_pr_state_preempt (d, &local_err)) { +/* MIG_EVENT_FAILED cannot fail, so just warn */ +error_prepend (&local_err,"scsi-block migration rollback: " ); +warn_report_err (local_err); + } +} + static int scsi_block_migration_notifier(NotifierWithReturn *notifier,
...belongs better with the function it was originally introducing.
Fix that, and you can add
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH 3/8] hw/scsi-disk: move MIG_EVENT_FAILED into a new function
Quoted 23 lines · Eric Blake
Quoted 16 lines · Stefan Hajnoczi
Move the
MIG_EVENT_FAILEDlogic fromscsi_block_migration_notifier()into its own newscsi_block_migration_failed()function. The next commit adds logic for handling another event, so doing that in dedicated functions makes the code easier to read.Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/scsi/scsi-disk.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)hw/scsi/scsi-disk.c +0
-0hw/scsi/scsi-disk.c static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, · line 2763
Quoted 3 lines
hw/scsi/scsi-disk.c +0
-0@@ -2761,20 +2761,26 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,* fails because the destination may have already preempted and we need to get * the reservation back. */ I think this comment...
Quoted 15 lines
hw/scsi/scsi-disk.c static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, · line 2776
Quoted 13 lines
hw/scsi/scsi-disk.c +12
-0+static void scsi_block_migration_failed (SCSIDiskState *s) +{ +SCSIDevice *d = &s->qdev; +Error *local_err =NULL ; + +if (!scsi_generic_pr_state_preempt (d, &local_err)) { +/* MIG_EVENT_FAILED cannot fail, so just warn */ +error_prepend (&local_err,"scsi-block migration rollback: " ); +warn_report_err (local_err); + } +} + static int scsi_block_migration_notifier(NotifierWithReturn *notifier,...belongs better with the function it was originally introducing.
I think it's clearer to explain scsi_block_migration_failed()'s purpose in its doc comment rather than in scsi_block_migration_notifier(), which this patch series turns into just a dispatch function that doesn't do anything itself.
scsi_block_migration_notifier() will dispatch MIG_EVENT_DONE in a later commit, so it's more general and no longer does the MIG_EVENT_FAILED work itself.
What do you think?
Quoted 10 lines
Fix that, and you can add
Reviewed-by Eric Blake <[email protected]>Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH 3/8] hw/scsi-disk: move MIG_EVENT_FAILED into a new function
Quoted 45 lines · Stefan Hajnoczi
Quoted 23 lines · Eric Blake
Quoted 16 lines · Stefan Hajnoczi
Move the
MIG_EVENT_FAILEDlogic fromscsi_block_migration_notifier()into its own newscsi_block_migration_failed()function. The next commit adds logic for handling another event, so doing that in dedicated functions makes the code easier to read.Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/scsi/scsi-disk.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)hw/scsi/scsi-disk.c +0
-0hw/scsi/scsi-disk.c static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, · line 2763
Quoted 3 lines
hw/scsi/scsi-disk.c +0
-0@@ -2761,20 +2761,26 @@ static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun,* fails because the destination may have already preempted and we need to get * the reservation back. */ I think this comment...
Quoted 15 lines
hw/scsi/scsi-disk.c static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, · line 2776
Quoted 13 lines
hw/scsi/scsi-disk.c +12
-0+static void scsi_block_migration_failed (SCSIDiskState *s) +{ +SCSIDevice *d = &s->qdev; +Error *local_err =NULL ; + +if (!scsi_generic_pr_state_preempt (d, &local_err)) { +/* MIG_EVENT_FAILED cannot fail, so just warn */ +error_prepend (&local_err,"scsi-block migration rollback: " ); +warn_report_err (local_err); + } +} + static int scsi_block_migration_notifier(NotifierWithReturn *notifier,...belongs better with the function it was originally introducing.
I think it's clearer to explain scsi_block_migration_failed()'s purpose in its doc comment rather than in
scsi_block_migration_notifier(), which this patch series turns into just a dispatch function that doesn't do anything itself.
Quoted 5 lines
scsi_block_migration_notifier()will dispatchMIG_EVENT_DONEin a later commit, so it's more general and no longer does theMIG_EVENT_FAILEDwork itself.What do you think?
Okay, that explanation makes sense. I'll take a look at your v2 now.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
PATCH 4/8 qemu-pr-helper: add PR_HELPER_CLEANUP for live migration
In reply to Stefan HajnocziIntroduce a new vendor-specific SCSI command that acts similar to REGISTER AND IGNORE EXISTING KEY but also discards multipathd's state (including /etc/multipath/prkeys). This command will be used on the source after live migration completes successfully.
This cleanup step is necessary because multipathd will register the key it has stored when the path comes up again. This is undesirable when the guest has migrated to another host and can interfere with the migration process or the application running inside the guest.
Add a qemu-pr-manager protocol feature bit indicating that the new PR_HELPER_CLEANUP command is available. The command has no CDB fields other than OPCODE and the response contains no data aside from the usual result and sense data.
Note that the linker needs an explicit libmpathcmd dependency for mpath_disconnect() even though qemu-pr-helper.c already #included the mpath_cmd.h header before.
--- meson.build | 2 + include/scsi/constants.h | 1 + scsi/pr-helper.h | 3 + scsi/qemu-pr-helper.c | 146 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 143 insertions(+), 9 deletions(-)
meson.build +2 -0
@@ -1486,6 +1486,8 @@ if host_os == 'linux' and have_tools and get_option('mpath').allowed() endif mpathlibs += cc.find_library('multipath', required: get_option('mpath')) + mpathlibs += cc.find_library('mpathcmd', + required: get_option('mpath')) foreach lib: mpathlibs if not lib.found() mpathlibs = []
include/scsi/constants.h +1 -0
@@ -149,6 +149,7 @@#define MECHANISM_STATUS 0xbd#define READ_CD 0xbe#define SEND_DVD_STRUCTURE 0xbf +#define PR_HELPER_CLEANUP 0xff/* vendor-specific QEMU command */ /* * SERVICE ACTION IN subcodes
scsi/pr-helper.h +3 -0
@@ -31,6 +31,9 @@#define PR_HELPER_SENSE_SIZE 96#define PR_HELPER_DATA_SIZE 8192 +/* Protocol features */ +#define PR_HELPER_FEATURE_CLEANUP (1u << 0) + typedef struct PRHelperResponse { int32_t result; int32_t sz;
scsi/qemu-pr-helper.c +137 -9
195 lines not on this page · raw message
Signature · 1 line
2.56.0
Re: [PATCH 4/8] qemu-pr-helper: add PR_HELPER_CLEANUP for live migration
Quoted 31 lines · Stefan Hajnoczi
Introduce a new vendor-specific SCSI command that acts similar to REGISTER AND IGNORE EXISTING KEY but also discards multipathd's state (including
/etc/multipath/prkeys). This command will be used on the source after live migration completes successfully.This cleanup step is necessary because multipathd will register the key it has stored when the path comes up again. This is undesirable when the guest has migrated to another host and can interfere with the migration process or the application running inside the guest.
Add a qemu-pr-manager protocol feature bit indicating that the new
PR_HELPER_CLEANUPcommand is available. The command has no CDB fields other than OPCODE and the response contains no data aside from the usual result and sense data.Note that the linker needs an explicit libmpathcmd dependency for
mpath_disconnect()even thoughqemu-pr-helper.calready #included thempath_cmd.hheader before.Signed-off-by Stefan Hajnoczi <[email protected]>--- meson.build | 2 + include/scsi/constants.h | 1 + scsi/pr-helper.h | 3 + scsi/qemu-pr-helper.c | 146 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 143 insertions(+), 9 deletions(-)+++
b/scsi/qemu-pr-helper.c
scsi/qemu-pr-helper.c static int coroutine_fn prh_write_response(PRHelperClient *client, · line 828
Quoted 7 lines
scsi/qemu-pr-helper.c +2
-1@@ -703,7 +823,8 @@ static int coroutine_fn prh_write_response(PRHelperClient *client,ssize_t r;size_t sz; -if (req->cdb[0 ] ==PERSISTENT_RESERVE_IN && resp->result ==GOOD ) { +if (req->cdb[0 ] ==PERSISTENT_RESERVE_IN && + resp->result ==GOOD ) {assert (resp->sz <= req->sz && resp->sz <=sizeof (client->data));
Spurious hunk?
Otherwise,
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
PATCH 5/8 pr-manager-helper: add PR_HELPER_CLEANUP command support
In reply to Stefan Hajnocziqemu-pr-helper now supports the vendor-specific PR_HELPER_CLEANUP SCSI command. Add support to the client side.
--- scsi/pr-manager-helper.c | 20 ++++++++++++++++---- scsi/utils.c | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-)
scsi/pr-manager-helper.c +16 -4
@@ -38,6 +38,8 @@ struct PRManagerHelper {QemuMutex lock;QIOChannel *ioc; + +uint32_t proto_flags;/* flags negotiated with pr-helper */ };static void pr_manager_send_status_changed_event (PRManagerHelper *pr_mgr)
@@ -129,8 +131,9 @@ static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr, goto out_close; } - flags =0 ; - r =pr_manager_helper_write (pr_mgr,-1 , &flags,sizeof (flags), errp); + pr_mgr->proto_flags = flags &PR_HELPER_FEATURE_CLEANUP ; + r =pr_manager_helper_write (pr_mgr,-1 , &pr_mgr->proto_flags, +sizeof (pr_mgr->proto_flags), errp);if (r <0 ) { goto out_close; }
@@ -160,10 +163,19 @@ static int pr_manager_helper_run(PRManager *p,return -EINVAL ; } +/* The command is only available when pr-helper supports it */ +if (cdb[0 ] ==PR_HELPER_CLEANUP && + !(pr_mgr->proto_flags &PR_HELPER_FEATURE_CLEANUP )) { +return -ENOTSUP ; + } +memcpy (cdb, io_hdr->cmdp, io_hdr->cmd_len); -assert (cdb[0 ] ==PERSISTENT_RESERVE_OUT || cdb[0 ] ==PERSISTENT_RESERVE_IN ); +assert (cdb[0 ] ==PERSISTENT_RESERVE_OUT || + cdb[0 ] ==PERSISTENT_RESERVE_IN || + cdb[0 ] ==PR_HELPER_CLEANUP ); expected_dir = - (cdb[0 ] ==PERSISTENT_RESERVE_OUT ?SG_DXFER_TO_DEV :SG_DXFER_FROM_DEV ); + (cdb[0 ] ==PERSISTENT_RESERVE_IN ? +SG_DXFER_FROM_DEV :SG_DXFER_TO_DEV );if (io_hdr->dxfer_direction != expected_dir) {return -EINVAL ; }
scsi/utils.c +3 -0
@@ -29,6 +29,9 @@ uint32_t scsi_data_cdb_xfer(uint8_t *buf)uint32_t scsi_cdb_xfer (uint8_t *buf) { +if (buf[0 ] ==PR_HELPER_CLEANUP ) { +return 0 ; + }switch (buf[0 ] >>5 ) {case 0 :return buf[4 ];
Signature · 1 line
2.56.0
Re: [PATCH 5/8] pr-manager-helper: add PR_HELPER_CLEANUP command support
Quoted 2 lines · Stefan Hajnoczi
qemu-pr-helper now supports the vendor-specific
PR_HELPER_CLEANUPSCSI command. Add support to the client side.
Hi Stefan, up to this point it's all good; qemu-pr-helper works in SCSI commands so a vendor-specific command is the obvious way to add the functionality---and I'm glad the feature negotiation worked out.
For the next steps, however, I'm not sure about using blk_ioctl to send the migration done command from SCSIDevice to BlockBackend, which (if I understand correctly) implicitly exposes the command to the guest as well.
In particular, perhaps it makes sense to add a migration_done callback to BlockDriverState and BlockBackend? They can be called either only from SCSIGeneric, or from a new block layer migration notifier that walks the BlockBackend list.
If instead the command is not exposed to the guest, please just add a comment explaining why.
Thanks,
Paolo
Quoted 67 lines
Signed-off-by Stefan Hajnoczi <[email protected]>--- scsi/pr-manager-helper.c | 20 ++++++++++++++++---- scsi/utils.c | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-)scsi/pr-manager-helper.c +16
-4@@ -38,6 +38,8 @@ struct PRManagerHelper {QemuMutex lock;QIOChannel *ioc; + +uint32_t proto_flags;/* flags negotiated with pr-helper */ };static void pr_manager_send_status_changed_event (PRManagerHelper *pr_mgr)@@ -129,8 +131,9 @@ static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr, goto out_close; } - flags =0 ; - r =pr_manager_helper_write (pr_mgr,-1 , &flags,sizeof (flags), errp); + pr_mgr->proto_flags = flags &PR_HELPER_FEATURE_CLEANUP ; + r =pr_manager_helper_write (pr_mgr,-1 , &pr_mgr->proto_flags, +sizeof (pr_mgr->proto_flags), errp);if (r <0 ) { goto out_close; }@@ -160,10 +163,19 @@ static int pr_manager_helper_run(PRManager *p,return -EINVAL ; } +/* The command is only available when pr-helper supports it */ +if (cdb[0 ] ==PR_HELPER_CLEANUP && + !(pr_mgr->proto_flags &PR_HELPER_FEATURE_CLEANUP )) { +return -ENOTSUP ; + } +memcpy (cdb, io_hdr->cmdp, io_hdr->cmd_len); -assert (cdb[0 ] ==PERSISTENT_RESERVE_OUT || cdb[0 ] ==PERSISTENT_RESERVE_IN ); +assert (cdb[0 ] ==PERSISTENT_RESERVE_OUT || + cdb[0 ] ==PERSISTENT_RESERVE_IN || + cdb[0 ] ==PR_HELPER_CLEANUP ); expected_dir = - (cdb[0 ] ==PERSISTENT_RESERVE_OUT ?SG_DXFER_TO_DEV :SG_DXFER_FROM_DEV ); + (cdb[0 ] ==PERSISTENT_RESERVE_IN ? +SG_DXFER_FROM_DEV :SG_DXFER_TO_DEV );if (io_hdr->dxfer_direction != expected_dir) {return -EINVAL ; }scsi/utils.c +3
-0@@ -29,6 +29,9 @@ uint32_t scsi_data_cdb_xfer(uint8_t *buf)uint32_t scsi_cdb_xfer (uint8_t *buf) { +if (buf[0 ] ==PR_HELPER_CLEANUP ) { +return 0 ; + }switch (buf[0 ] >>5 ) {case 0 :return buf[4 ];
Re: [PATCH 5/8] pr-manager-helper: add PR_HELPER_CLEANUP command support
Quoted 12 lines · Paolo Bonzini
Quoted 2 lines · Stefan Hajnoczi
qemu-pr-helper now supports the vendor-specific
PR_HELPER_CLEANUPSCSI command. Add support to the client side.Hi Stefan, up to this point it's all good; qemu-pr-helper works in SCSI commands so a vendor-specific command is the obvious way to add the functionality---and I'm glad the feature negotiation worked out.
For the next steps, however, I'm not sure about using
blk_ioctlto send the migration done command from SCSIDevice to BlockBackend, which (if I understand correctly) implicitly exposes the command to the guest as well.
Quoted 7 lines
In particular, perhaps it makes sense to add a
migration_donecallback to BlockDriverState and BlockBackend? They can be called either only from SCSIGeneric, or from a new block layer migration notifier that walks the BlockBackend list.If instead the command is not exposed to the guest, please just add a comment explaining why.
I looked into this: hw/scsi/scsi-bus.c:scsi_req_new() does not support vendor-specific commands since scsi_cdb_length() returns -1. So PR_HELPER_CLEANUP (0xff) from the guest will be rejected immediately by any emulated SCSI HBA.
However, I will send a v2 patch series that goes further: let's explicitly reject PR_HELPER_CLEANUP in scsi_req_new() so that even if scsi_cdb_length() or related code changes in the future, it will never be possible to submit the command from a guest.
Quoted 68 lines
Thanks,
Paolo
Quoted 62 lines
Signed-off-by Stefan Hajnoczi <[email protected]>--- scsi/pr-manager-helper.c | 20 ++++++++++++++++---- scsi/utils.c | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-)scsi/pr-manager-helper.c +16
-4@@ -38,6 +38,8 @@ struct PRManagerHelper {QemuMutex lock;QIOChannel *ioc; + +uint32_t proto_flags;/* flags negotiated with pr-helper */ };static void pr_manager_send_status_changed_event (PRManagerHelper *pr_mgr)@@ -129,8 +131,9 @@ static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr, goto out_close; } - flags =0 ; - r =pr_manager_helper_write (pr_mgr,-1 , &flags,sizeof (flags), errp); + pr_mgr->proto_flags = flags &PR_HELPER_FEATURE_CLEANUP ; + r =pr_manager_helper_write (pr_mgr,-1 , &pr_mgr->proto_flags, +sizeof (pr_mgr->proto_flags), errp);if (r <0 ) { goto out_close; }@@ -160,10 +163,19 @@ static int pr_manager_helper_run(PRManager *p,return -EINVAL ; } +/* The command is only available when pr-helper supports it */ +if (cdb[0 ] ==PR_HELPER_CLEANUP && + !(pr_mgr->proto_flags &PR_HELPER_FEATURE_CLEANUP )) { +return -ENOTSUP ; + } +memcpy (cdb, io_hdr->cmdp, io_hdr->cmd_len); -assert (cdb[0 ] ==PERSISTENT_RESERVE_OUT || cdb[0 ] ==PERSISTENT_RESERVE_IN ); +assert (cdb[0 ] ==PERSISTENT_RESERVE_OUT || + cdb[0 ] ==PERSISTENT_RESERVE_IN || + cdb[0 ] ==PR_HELPER_CLEANUP ); expected_dir = - (cdb[0 ] ==PERSISTENT_RESERVE_OUT ?SG_DXFER_TO_DEV :SG_DXFER_FROM_DEV ); + (cdb[0 ] ==PERSISTENT_RESERVE_IN ? +SG_DXFER_FROM_DEV :SG_DXFER_TO_DEV );if (io_hdr->dxfer_direction != expected_dir) {return -EINVAL ; }scsi/utils.c +3
-0@@ -29,6 +29,9 @@ uint32_t scsi_data_cdb_xfer(uint8_t *buf)uint32_t scsi_cdb_xfer (uint8_t *buf) { +if (buf[0 ] ==PR_HELPER_CLEANUP ) { +return 0 ; + }switch (buf[0 ] >>5 ) {case 0 :return buf[4 ];
PATCH 6/8 file-posix: forward PR_HELPER_CLEANUP commands to pr-manager
In reply to Stefan Hajnoczi--- block/file-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
block/file-posix.c +2 -1
@@ -4346,7 +4346,8 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)if (req ==SG_IO && s->pr_mgr) {struct sg_io_hdr *io_hdr = buf;if (io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_OUT || - io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_IN ) { + io_hdr->cmdp[0 ] ==PERSISTENT_RESERVE_IN || + io_hdr->cmdp[0 ] ==PR_HELPER_CLEANUP ) {return pr_manager_execute (s->pr_mgr,qemu_get_current_aio_context (), s->fd, io_hdr); }
Signature · 1 line
2.56.0
PATCH 7/8 scsi: clean up persistent reservation state after migration
In reply to Stefan HajnocziLive migration of persistent reservations is designed to work without coordination between the source and destination. The destination uses the PREEMPT command to atomically take over the reservation or unregister keys belonging to the source. This approach avoids race conditions where other nodes in the cluster might acquire the reservation and it minimizes communication during the downtime window.
While this works at the SCSI level, this approach is incomplete when using multipathd. Multipathd tracks registered keys and automatically registers them again when paths come up. It is unaware that the destination has preempted the reservation and therefore it will still attempt to register the key on the source host.
Use the new pr-helper CLEANUP command to communicate with multipathd on the source after migration completes successfully. The effect is similar to REGISTER AND IGNORE EXISTING KEY, except it also discards multipathd's state so that the source will no longer attempt to register the key.
--- include/hw/scsi/scsi.h | 1 + hw/scsi/scsi-disk.c | 21 ++++++++++++++++++++- hw/scsi/scsi-generic.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-)
include/hw/scsi/scsi.h +1 -0
@@ -256,6 +256,7 @@ SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun);/* scsi-generic.c. */ extern const SCSIReqOps scsi_generic_req_ops;bool scsi_generic_pr_state_preempt (SCSIDevice *s,Error **errp); +bool scsi_generic_pr_state_cleanup (SCSIDevice *s,Error **errp);/* scsi-disk.c */ #define SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR 0
hw/scsi/scsi-disk.c +20 -1
@@ -2773,13 +2773,32 @@ static void scsi_block_migration_failed(SCSIDiskState *s) } } +/* +* Unregister keys on the source after migration since the destination has +* preempted and taken over. This cleanup is not necessary at the SCSI level +* but multipathd tracks registered keys and must be informed that this host no +* longer wishes to be registered. +*/ +static void scsi_block_migration_done (SCSIDiskState *s) +{ +SCSIDevice *d = &s->qdev; +Error *local_err =NULL ; + +if (!scsi_generic_pr_state_cleanup (d, &local_err)) { +error_prepend (&local_err,"scsi-block migration done: " ); +warn_report_err (local_err); + } +} +static int scsi_block_migration_notifier (NotifierWithReturn *notifier,MigrationEvent *e,Error **errp) {SCSIDiskState *s =container_of (notifier, SCSIDiskState, migration_notifier); -if (e->type ==MIG_EVENT_FAILED ) { +if (e->type ==MIG_EVENT_DONE ) { +scsi_block_migration_done (s); + }else if (e->type ==MIG_EVENT_FAILED ) {scsi_block_migration_failed (s); }return 0 ;
hw/scsi/scsi-generic.c +29 -0
@@ -652,6 +652,35 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp)return true; } +/* Clean up keys on source after migration */ +bool scsi_generic_pr_state_cleanup (SCSIDevice *s,Error **errp) +{ +SCSIPRState *pr_state = &s->pr_state; +uint8_t cmd[10 ] = { +PR_HELPER_CLEANUP ,/* OPCODE */ + }; +int ret; + + WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { +if (pr_state->key ==0 ) { +return true;/* nothing to do */ + } + } + + ret =scsi_SG_IO (s->conf.blk,SG_DXFER_TO_DEV , cmd,sizeof (cmd),NULL ,0 , + s->io_timeout, errp); +if (ret <0 ) { +error_prepend (errp,"pr-helper CLEANUP failed: " ); +return false; + } + + WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { + pr_state->key =0 ; + pr_state->resv_type =0 ; + } +return true; +} +static void scsi_read_complete (void * opaque,int ret) {SCSIGenericReq *r = (SCSIGenericReq *)opaque;
Signature · 1 line
2.56.0
PATCH 8/8 scsi: use REGISTER AND IGNORE EXISTING KEY for robustness
In reply to Stefan HajnocziThe regular REGISTER service action fails with RESERVATION CONFLICT when the I_T nexus already has a key registered. While we don't expect a key to be registered, it is fragile to rely on this. If there is a key registered for some reason, live migration will fail when the destination hits the RESERVATION CONFLICT error.
Use REGISTER AND IGNORE EXISTING KEY so that the command is likely to succeed.
--- hw/scsi/scsi-generic.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
hw/scsi/scsi-generic.c +7 -5
@@ -424,7 +424,8 @@ static void scsi_handle_persistent_reserve_out_reply( } } -static bool scsi_generic_pr_register (SCSIDevice *s,uint64_t key,Error **errp) +static bool scsi_generic_pr_register_ignore (SCSIDevice *s,uint64_t key, +Error **errp) {uint8_t cmd[10 ] = {};uint8_t buf[24 ] = {};
@@ -432,14 +433,15 @@ static bool scsi_generic_pr_register(SCSIDevice *s, uint64_t key, Error **errp)int ret; cmd[0 ] =PERSISTENT_RESERVE_OUT ; - cmd[1 ] =PRO_REGISTER ; + cmd[1 ] =PRO_REGISTER_AND_IGNORE_EXISTING_KEY ; cmd[8 ] =sizeof (buf);memcpy (&buf[8 ], &key_be,sizeof (key_be)); ret =scsi_SG_IO (s->conf.blk,SG_DXFER_TO_DEV , cmd,sizeof (cmd), buf,sizeof (buf), s->io_timeout, errp);if (ret <0 ) { -error_prepend (errp,"PERSISTENT RESERVE OUT with REGISTER: " ); +error_prepend (errp,"PERSISTENT RESERVE OUT with " +"REGISTER AND IGNORE EXISTING KEY: " );return false; }return true;
@@ -620,7 +622,7 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp)return true;/* no PR state, do nothing */ } -if (!scsi_generic_pr_register (s, key, errp)) { +if (!scsi_generic_pr_register_ignore (s, key, errp)) {return false; }
@@ -647,7 +649,7 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp)* CONFLICT is expected if our registration remained in place. */ if (resv_type ==0 ) { -scsi_generic_pr_register (s, key,NULL ); +scsi_generic_pr_register_ignore (s, key,NULL ); }return true; }
Signature · 1 line
2.56.0
Re: [PATCH 8/8] scsi: use REGISTER AND IGNORE EXISTING KEY for robustness
Quoted 6 lines · Stefan Hajnoczi
The regular REGISTER service action fails with RESERVATION CONFLICT when the
I_Tnexus already has a key registered. While we don't expect a key to be registered, it is fragile to rely on this. If there is a key registered for some reason, live migration will fail when the destination hits the RESERVATION CONFLICT error.
Quoted 7 lines
Use REGISTER AND IGNORE EXISTING KEY so that the command is likely to succeed.
Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/scsi/scsi-generic.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org