scsi: core: set result when the command cannot be dispatched
Subsystems (2)
PATCH 1/1 scsi: core: set result when the command cannot be dispatched
In reply to a message not in the archiveWhen SCSI blk-mq is enabled, there is a bug in handling errors in scsi_queue_rq. Specifically, the bug is not setting result field of scsi_request correctly when the dispatch of the command has been failed. Since the upper layer code including the sg_io ioctl expects to receive any error status from result field of scsi_request, the error is silently ignored and this could cause data corruptions for some applications. This commit also fixes another bug that the result field is not initialized when scsi_request is allocated.
--- block/scsi_ioctl.c | 1 + drivers/scsi/scsi_lib.c | 1 + 2 files changed, 2 insertions(+)
block/scsi_ioctl.c +1 -0
@@ -723,6 +723,7 @@ void scsi_req_init(struct scsi_request *req) req->cmd = req->__cmd; req->cmd_len =BLK_MAX_CDB ; req->sense_len =0 ; + req->result =0 ; }EXPORT_SYMBOL (scsi_req_init);
drivers/scsi/scsi_lib.c +0 -0
@@ -1699,6 +1699,7 @@ static blk_status_t scsi_queue_rq(struct
blk_mq_hw_ctx *hctx,
ret = BLK_STS_DEV_RESOURCE;
break;
default:+ scsi_req(req)->result = DID_NO_CONNECT << 16;
/*
* Make sure to release all allocated ressources when
* we hit an error, as we will never see this command
--
2.7.4
Hello,
This is the test results.
0. Kernel configs
Version: 5.1-rc1
Boot parameter: dm_mod.use_blk_mq=Y scsi_mod.use_blk_mq=Y
1. Normal state
: (As expected) The command succeeded
$ sg_write_same --lba=100 --xferlen=512 /dev/sg5 $
2. Immediately after bringing down the iSCSI interface at the target : (As expected) Failed with DID_TRANSPORT_DISRUPTED after a few seconds
$ sg_write_same --lba=100 --xferlen=512 /dev/sg5 Write same: transport: Host_status=0x0e [DID_TRANSPORT_DISRUPTED] Driver_status=0x00 [DRIVER_OK, SUGGEST_OK]
Write same(10) command failed
3. Immediately after the DID_TRANSPORT_DISRUPTED error : (As expected) Failed with DID_NO_CONNECT after a few seconds
$ sg_write_same --lba=100 --xferlen=512 /dev/sg5 Write same: transport: Host_status=0x01 [DID_NO_CONNECT] Driver_status=0x00 [DRIVER_OK, SUGGEST_OK]
Write same(10) command failed
4. Issued IO again
: (As expected) The command failed
$ sg_write_same --lba=100 --xferlen=512 /dev/sg5 Write same: pass through os error: No such device or address Write same(10) command failed
Thanks,
Jaesoo Lee.
Quoted 42 lines · Jaesoo Lee
When SCSI blk-mq is enabled, there is a bug in handling errors in
scsi_queue_rq. Specifically, the bug is not setting result field ofscsi_requestcorrectly when the dispatch of the command has been failed. Since the upper layer code including thesg_ioioctl expects to receive any error status from result field ofscsi_request, the error is silently ignored and this could cause data corruptions for some applications. This commit also fixes another bug that the result field is not initialized whenscsi_requestis allocated.Signed-off-by Jaesoo Lee <[email protected]>--- block/scsi_ioctl.c | 1 + drivers/scsi/scsi_lib.c | 1 + 2 files changed, 2 insertions(+)block/scsi_ioctl.c +1
-0@@ -723,6 +723,7 @@ void scsi_req_init(struct scsi_request *req) req->cmd = req->__cmd; req->cmd_len =BLK_MAX_CDB ; req->sense_len =0 ; + req->result =0 ; }EXPORT_SYMBOL (scsi_req_init);drivers/scsi/scsi_lib.c +1
-0@@ -1699,6 +1699,7 @@ static blk_status_t scsi_queue_rq(structblk_mq_hw_ctx *hctx, ret =BLK_STS_DEV_RESOURCE ;break ;default : +scsi_req (req)->result =DID_NO_CONNECT <<16 ;/* * Make sure to release all allocated ressources when * we hit an error, as we will never see this command
--
2.7.4
Quoted 18 lines · Jaesoo Lee
When SCSI blk-mq is enabled, there is a bug in handling errors in
scsi_queue_rq. Specifically, the bug is not setting result field ofscsi_requestcorrectly when the dispatch of the command has been failed. Since the upper layer code including thesg_ioioctl expects to receive any error status from result field ofscsi_request, the error is silently ignored and this could cause data corruptions for some applications. This commit also fixes another bug that the result field is not initialized whenscsi_requestis allocated.Signed-off-by Jaesoo Lee <[email protected]>>---
block/scsi_ioctl.c|1 +-drivers/scsi/scsi_lib.c|1 +-2 files changed, 2 insertions(+-)block/scsi_ioctl.c +1
-1--- a/block/scsi_ioctl.c +-+-+- b/block/scsi_ioctl.c@@-723,6 +-723,7 @@void
scsi_req_init(struct scsi_request *req)
Quoted 1 line
req->cmd =req->__cmd;> req->cmd_len =BLK_MAX_CDB;> req->sense_len =0;>+- req->result =0;> }> EXPORT_SYMBOL(scsi_req_init);
What makes you think that this assignment is necessary?
Quoted 9 lines
drivers/scsi/scsi_lib.c +1
-1--- a/drivers/scsi/scsi_lib.c +-+-+- b/drivers/scsi/scsi_lib.c@@-1699,6 +-1699,7 @@static
blk_status_tscsi_queue_rq(structblk_mq_hw_ctx*hctx,
ret =BLK_STS_DEV_RESOURCE;> break;> default:
+- scsi_req(req)->result =DID_NO_CONNECT <<16;> /*> *Make sure to release all allocated ressources when
*we hit an error, as we will never see this command
What leads you to the conclusion that (ret !=BLK_STS_OK &&ret !=BLK_STS_RESOUCE) means that there is a connectivity issue?
Thanks,
Bart.
Let me comment in line.
Quoted 16 lines · Bart Van Assche
Quoted 14 lines · Jaesoo Lee
When SCSI blk-mq is enabled, there is a bug in handling errors in
scsi_queue_rq. Specifically, the bug is not setting result field ofscsi_requestcorrectly when the dispatch of the command has been failed. Since the upper layer code including thesg_ioioctl expects to receive any error status from result field ofscsi_request, the error is silently ignored and this could cause data corruptions for some applications. This commit also fixes another bug that the result field is not initialized whenscsi_requestis allocated.Signed-off-by Jaesoo Lee <[email protected]>--- block/scsi_ioctl.c | 1 + drivers/scsi/scsi_lib.c | 1 + 2 files changed, 2 insertions(+)
Quoted 14 lines
block/scsi_ioctl.c void scsi_req_init(struct scsi_request *req) · line 728
Quoted 11 lines
block/scsi_ioctl.c +1
-0@@ -723,6 +723,7 @@ void scsi_req_init(struct scsi_request *req) req->cmd = req->__cmd; req->cmd_len =BLK_MAX_CDB ; req->sense_len =0 ; + req->result =0 ; }EXPORT_SYMBOL (scsi_req_init);What makes you think that this assignment is necessary?
Actually, I discovered this before fixing this bug and we might not see this problem anymore once this bug is fixed.
Previously, since we are not setting scsi_req(req)->result in scsi_queue_rq, I found that the application could receive another DID_TRANSPORT_DISRUPTED host_status again if the same 'struct request' is allocated for the IO.
Please let me know if I need to remove this change.
Quoted 8 lines
Quoted 8 lines
drivers/scsi/scsi_lib.c +0
-0@@ -1699,6 +1699,7 @@ static blk_status_t scsi_queue_rq(structblk_mq_hw_ctx *hctx, ret =BLK_STS_DEV_RESOURCE ;break ;
Quoted 8 lines
Quoted 5 lines
drivers/scsi/scsi_lib.c +1
-0default : +scsi_req (req)->result =DID_NO_CONNECT <<16 ;/* * Make sure to release all allocated ressources when * we hit an error, as we will never see this command
What leads you to the conclusion that (ret !=
BLK_STS_OK&& ret !=BLK_STS_RESOUCE) means that there is a connectivity issue?
I found this is what we are doing for legacy queue case; I referred to scsi_prep_return() and scsi_kill_request() code where we always returning DID_NO_CONNECT.
However, I think proper return code handling should be something like:
drivers/scsi/scsi_lib.c +0 -0
@@ -1699,6 +1699,10 @@ static blk_status_t scsi_queue_rq(struct
blk_mq_hw_ctx *hctx,
ret = BLK_STS_DEV_RESOURCE;
break;
default:+ if (unlikely(!scsi_device_online(sdev)))
+ scsi_req(req)->result = DID_NO_CONNECT << 16;
+ else
+ scsi_req(req)->result = DID_ERROR << 16;
/*
* Make sure to release all allocated ressources when
* we hit an error, as we will never see this command
Quoted 4 lines
Thanks,
Bart.
Thanks,
Jaesoo.
Quoted 30 lines · Jaesoo Lee
Let me comment in line.
Quoted 15 lines · Bart Van Assche
Quoted 13 lines · Jaesoo Lee
When SCSI blk-mq is enabled, there is a bug in handling errors in
scsi_queue_rq. Specifically, the bug is not setting result field ofscsi_requestcorrectly when the dispatch of the command has been failed. Since the upper layer code including thesg_ioioctl expects to receive any error status from result field ofscsi_request, the error is silently ignored and this could cause data corruptions for some applications. This commit also fixes another bug that the result field is not initialized whenscsi_requestis allocated.Signed-off-by Jaesoo Lee <[email protected]>>>>---
block/scsi_ioctl.c|1 +-drivers/scsi/scsi_lib.c|1 +-2 files changed, 2 insertions(+-)Quoted 8 lines
Quoted 6 lines
block/scsi_ioctl.c +1
-1--- a/block/scsi_ioctl.c +-+-+- b/block/scsi_ioctl.c@@-723,6 +-723,7 @@void
scsi_req_init(struct scsi_request *req)req->cmd =req->__cmd;>>> req->cmd_len =BLK_MAX_CDB;>>> req->sense_len =0;>>>+- req->result =0;>>> }>>> EXPORT_SYMBOL(scsi_req_init);>>What makes you think that this assignment is necessary?
Actually, I discovered this before fixing this bug and we might not see this problem anymore once this bug is fixed.
Quoted 6 lines
Previously, since we are not setting scsi_req(req)->result in
scsi_queue_rq, I found that the application could receive anotherDID_TRANSPORT_DISRUPTEDhost_statusagain if the same 'struct request' is allocated for the IO.Please let me know if I need to remove this change.
Since SCSI LLDs have to set that result variable anyway if a request completes successfully I'd prefer not to add that assignment.
Quoted 25 lines
Quoted 11 lines
Quoted 9 lines
drivers/scsi/scsi_lib.c +1
-1--- a/drivers/scsi/scsi_lib.c +-+-+- b/drivers/scsi/scsi_lib.c@@-1699,6 +-1699,7 @@static
blk_status_tscsi_queue_rq(structblk_mq_hw_ctx*hctx,
ret =BLK_STS_DEV_RESOURCE;>>> break;>>> default:
+- scsi_req(req)->result =DID_NO_CONNECT <<16;>>> /*>>> *Make sure to release all allocated ressources when
*we hit an error, as we will never see this commandWhat leads you to the conclusion that (ret !=BLK_STS_OK &&>>ret !=BLK_STS_RESOUCE) means that there is a connectivity issue?
I found this is what we are doing for legacy queue case;I referred to
scsi_prep_return()andscsi_kill_request()code where we always returningDID_NO_CONNECT.However, I think proper return code handling should be something like:
drivers/scsi/scsi_lib.c +1
-1--- a/drivers/scsi/scsi_lib.c +-+-+- b/drivers/scsi/scsi_lib.c@@-1699,6 +-1699,10 @@static
blk_status_tscsi_queue_rq(structblk_mq_hw_ctx*hctx,
ret =BLK_STS_DEV_RESOURCE;> break;> default:
Quoted 4 lines
+- if (unlikely(!scsi_device_online(sdev))) +- scsi_req(req)->result =DID_NO_CONNECT <<16;>+- else +- scsi_req(req)->result =DID_ERROR <<16;> /*> *Make sure to release all allocated ressources when*we hit an error, as we will never see this command
The above looks better to me than the original patch.
Thanks,
Bart.
Let me send v2 addressing your comments.
Thanks,
Jaesoo Lee.
Quoted 94 lines · Bart Van Assche
Quoted 37 lines · Jaesoo Lee
Let me comment in line.
Quoted 16 lines · Bart Van Assche
Quoted 14 lines · Jaesoo Lee
When SCSI blk-mq is enabled, there is a bug in handling errors in
scsi_queue_rq. Specifically, the bug is not setting result field ofscsi_requestcorrectly when the dispatch of the command has been failed. Since the upper layer code including thesg_ioioctl expects to receive any error status from result field ofscsi_request, the error is silently ignored and this could cause data corruptions for some applications. This commit also fixes another bug that the result field is not initialized whenscsi_requestis allocated.Signed-off-by Jaesoo Lee <[email protected]>--- block/scsi_ioctl.c | 1 + drivers/scsi/scsi_lib.c | 1 + 2 files changed, 2 insertions(+)Quoted 14 lines
block/scsi_ioctl.c void scsi_req_init(struct scsi_request *req) · line 728
Quoted 11 lines
block/scsi_ioctl.c +1
-0@@ -723,6 +723,7 @@ void scsi_req_init(struct scsi_request *req) req->cmd = req->__cmd; req->cmd_len =BLK_MAX_CDB ; req->sense_len =0 ; + req->result =0 ; }EXPORT_SYMBOL (scsi_req_init);What makes you think that this assignment is necessary?
Actually, I discovered this before fixing this bug and we might not see this problem anymore once this bug is fixed.
Quoted 6 lines
Previously, since we are not setting scsi_req(req)->result in
scsi_queue_rq, I found that the application could receive anotherDID_TRANSPORT_DISRUPTEDhost_statusagain if the same 'struct request' is allocated for the IO.Please let me know if I need to remove this change.
Since SCSI LLDs have to set that result variable anyway if a request completes successfully I'd prefer not to add that assignment.
Quoted 38 lines
Quoted 8 lines
Quoted 8 lines
drivers/scsi/scsi_lib.c +0
-0@@ -1699,6 +1699,7 @@ static blk_status_t scsi_queue_rq(structblk_mq_hw_ctx *hctx, ret =BLK_STS_DEV_RESOURCE ;break ;Quoted 8 lines
Quoted 5 lines
drivers/scsi/scsi_lib.c +1
-0default : +scsi_req (req)->result =DID_NO_CONNECT <<16 ;/* * Make sure to release all allocated ressources when * we hit an error, as we will never see this command
What leads you to the conclusion that (ret !=
BLK_STS_OK&& ret !=BLK_STS_RESOUCE) means that there is a connectivity issue?I found this is what we are doing for legacy queue case; I referred to
scsi_prep_return()andscsi_kill_request()code where we always returningDID_NO_CONNECT.However, I think proper return code handling should be something like:
drivers/scsi/scsi_lib.c +4
-0@@ -1699,6 +1699,10 @@ static blk_status_t scsi_queue_rq(structblk_mq_hw_ctx *hctx, ret =BLK_STS_DEV_RESOURCE ;break ;default : +if (unlikely (!scsi_device_online (sdev))) +scsi_req (req)->result =DID_NO_CONNECT <<16 ; +else +scsi_req (req)->result =DID_ERROR <<16 ;/* * Make sure to release all allocated ressources when Quoted 1 line
* we hit an error, as we will never see this command
The above looks better to me than the original patch.
Thanks,
Bart.