Skip to contentSkip to conversation

scsi: core: clear driver private data when retry request

PATCH Superseded Ye Bin 3 participants 3 messages Feb 14, 2025 – Feb 15, 2025 SCSI

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

PATCH v2 1/1 scsi: core: clear driver private data when retry request

From: Ye Bin <[email protected]>

After commit 1bad6c4a57ef
("scsi: zero per-cmd private driver data for each MQ I/O"), xen-scsifront/virtio_scsi/snic driver remove code that zeroes driver-private command data. If request do retry will lead to driver-private command data remains. Before commit 464a00c9e0ad ("scsi: core: Kill DRIVER_SENSE") if virtio_scsi do capacity expansion, first request may return UA then request will do retry, as driver-private command data remains, request will return UA again. As a result, the request keeps retrying, and the request times out and fails.
So zeroes driver-private command data when request do retry.

Fixes f7de50da1479 ("scsi: xen-scsifront: Remove code that zeroes driver-private command data")
Fixes c2bb87318baa ("scsi: virtio_scsi: Remove code that zeroes driver-private command data")
Fixes c3006a926468 ("scsi: snic: Remove code that zeroes driver-private command data")
---
 drivers/scsi/scsi_lib.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +7 -7
index be0890e4e706..c876b1c82153 100644
@@ -1669,13 +1669,6 @@ static blk_status_t scsi_prepare_cmd(struct request *req)
 	if (in_flight)
 		__set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
 
-	/*
-	 * Only clear the driver-private command data if the LLD does not supply
-	 * a function to initialize that data.
-	 */
-	if (!shost->hostt->init_cmd_priv)
-		memset(cmd + 1, 0, shost->hostt->cmd_size);
-
 	cmd->prot_op = SCSI_PROT_NORMAL;
 	if (blk_rq_bytes(req))
 		cmd->sc_data_direction = rq_dma_dir(req);
@@ -1842,6 +1835,13 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
 	if (!scsi_host_queue_ready(q, shost, sdev, cmd))
 		goto out_dec_target_busy;
 
+	/*
+	 * Only clear the driver-private command data if the LLD does not supply
+	 * a function to initialize that data.
+	 */
+	if (!shost->hostt->init_cmd_priv && shost->hostt->cmd_size)
+		memset(cmd + 1, 0, shost->hostt->cmd_size);
+
 	if (!(req->rq_flags & RQF_DONTPREP)) {
 		ret = scsi_prepare_cmd(req);
 		if (ret != BLK_STS_OK)
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 22 lines · Ye Bin
drivers/scsi/scsi_lib.c +0 -7
index be0890e4e706..c876b1c82153 100644
@@ -1669,13 +1669,6 @@ static blk_status_t scsi_prepare_cmd(struct request *req)
  	if (in_flight)
  		__set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
 
-	/*
-	 * Only clear the driver-private command data if the LLD does not supply
-	 * a function to initialize that data.
-	 */
-	if (!shost->hostt->init_cmd_priv)
-		memset(cmd + 1, 0, shost->hostt->cmd_size);
-
  	cmd->prot_op = SCSI_PROT_NORMAL;
  	if (blk_rq_bytes(req))
  		cmd->sc_data_direction = rq_dma_dir(req);
@@ -1842,6 +1835,13 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
  	if (!scsi_host_queue_ready(q, shost, sdev, cmd))
  		goto out_dec_target_busy;
 

drivers/scsi/scsi_lib.c static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, · line 1847

Quoted 10 lines
drivers/scsi/scsi_lib.c +7 -0
+	/*
+	 * Only clear the driver-private command data if the LLD does not supply
+	 * a function to initialize that data.
+	 */
+	if (!shost->hostt->init_cmd_priv && shost->hostt->cmd_size)
+		memset(cmd + 1, 0, shost->hostt->cmd_size);
+
  	if (!(req->rq_flags & RQF_DONTPREP)) {
  		ret = scsi_prepare_cmd(req);
  		if (ret != BLK_STS_OK)
#2 lore raw
In reply to Ye Bin
Quoted 41 lines · Ye Bin

From: Ye Bin <[email protected]>

After commit 1bad6c4a57ef
("scsi: zero per-cmd private driver data for each MQ I/O"), xen-scsifront/virtio_scsi/snic driver remove code that zeroes driver-private command data. If request do retry will lead to driver-private command data remains. Before commit 464a00c9e0ad ("scsi: core: Kill DRIVER_SENSE") if virtio_scsi do capacity expansion, first request may return UA then request will do retry, as driver-private command data remains, request will return UA again. As a result, the request keeps retrying, and the request times out and fails.
So zeroes driver-private command data when request do retry.

Fixes f7de50da1479 ("scsi: xen-scsifront: Remove code that zeroes driver-private command data")
Fixes c2bb87318baa ("scsi: virtio_scsi: Remove code that zeroes driver-private command data")
Fixes c3006a926468 ("scsi: snic: Remove code that zeroes driver-private command data")
---
  drivers/scsi/scsi_lib.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +0 -7
index be0890e4e706..c876b1c82153 100644
@@ -1669,13 +1669,6 @@ static blk_status_t scsi_prepare_cmd(struct request *req)
  	if (in_flight)
  		__set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
 
-	/*
-	 * Only clear the driver-private command data if the LLD does not supply
-	 * a function to initialize that data.
-	 */
-	if (!shost->hostt->init_cmd_priv)
-		memset(cmd + 1, 0, shost->hostt->cmd_size);
-
  	cmd->prot_op = SCSI_PROT_NORMAL;
  	if (blk_rq_bytes(req))
  		cmd->sc_data_direction = rq_dma_dir(req);

drivers/scsi/scsi_lib.c static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx, · line 1842

Quoted 8 lines
drivers/scsi/scsi_lib.c +5 -0
@@ -1842,6 +1835,13 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
  	if (!scsi_host_queue_ready(q, shost, sdev, cmd))
  		goto out_dec_target_busy;
 
+	/*
+	 * Only clear the driver-private command data if the LLD does not supply
+	 * a function to initialize that data.
+	 */
+	if (!shost->hostt->init_cmd_priv && shost->hostt->cmd_size)

Some drivers do not set shost->hostt->cmd_size. And none that don't set shost->hostt->cmd_size also do set shost->hostt->init_cmd_priv, AFAICS.

So for those we are always needlessly checking both shost->hostt->init_cmd_priv and shost->hostt->cmd_size.

So how about re-order the checks:

	if (shost->hostt->cmd_size && !shost->hostt->init_cmd_priv)
Quoted 5 lines
drivers/scsi/scsi_lib.c +2 -0
+		memset(cmd + 1, 0, shost->hostt->cmd_size);
+
  	if (!(req->rq_flags & RQF_DONTPREP)) {
  		ret = scsi_prepare_cmd(req);
  		if (ret != BLK_STS_OK)