Skip to contentSkip to conversation

scsi: core: clear driver private data when retry request

PATCH Applied Ye Bin 4 participants 8 messages Feb 17, 2025 – Feb 21, 2025 SCSI

lore
Share & tools
lore

Ye Bin

author
#0 lore raw

PATCH v3 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..f1cfe0bb89b2 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->cmd_size && !shost->hostt->init_cmd_priv)
+		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
#1 lore raw
Quoted 11 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.

So are there any drivers which expect this sort of behavior, i.e. keep private data between retries?

Quoted 10 lines

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")

---

Ps: in future, please list the changes per version here

Quoted 35 lines
  drivers/scsi/scsi_lib.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +7 -7
index be0890e4e706..f1cfe0bb89b2 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->cmd_size && !shost->hostt->init_cmd_priv)
+		memset(cmd + 1, 0, shost->hostt->cmd_size);
+
  	if (!(req->rq_flags & RQF_DONTPREP)) {
  		ret = scsi_prepare_cmd(req);
  		if (ret != BLK_STS_OK)
author
#2 lore raw
Quoted 13 lines · John Garry
Quoted 11 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.

Quoted 2 lines

So are there any drivers which expect this sort of behavior, i.e. keep private data between retries?

No driver that depends on the last state is found. If yes, the driver should provide the init_cmd_priv function to manage private data. In this way, the SCSI middle layer ignores the private data of the driver.

Quoted 13 lines
Quoted 12 lines

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")

Quoted 4 lines
Quoted 1 line

---

Ps: in future, please list the changes per version here

Thanks for the heads-up.

Quoted 38 lines
Quoted 37 lines
  drivers/scsi/scsi_lib.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +0 -2
index be0890e4e706..f1cfe0bb89b2 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

drivers/scsi/scsi_lib.c +2 -5
-     * 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

drivers/scsi/scsi_lib.c +5 -0
+     * a function to initialize that data.
+     */
+    if (shost->hostt->cmd_size && !shost->hostt->init_cmd_priv)
+        memset(cmd + 1, 0, shost->hostt->cmd_size);
+
      if (!(req->rq_flags & RQF_DONTPREP)) {
          ret = scsi_prepare_cmd(req);
          if (ret != BLK_STS_OK)
#3 lore raw
Quoted 16 lines · yebin
Quoted 13 lines · John Garry
Quoted 11 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.

Quoted 7 lines
Quoted 2 lines

So are there any drivers which expect this sort of behavior, i.e. keep private data between retries?

No driver that depends on the last state is found. If yes, the driver should provide the init_cmd_priv function to manage private data. In this way, the SCSI middle layer ignores the private data of the driver.

TBH, I am not sure on the history here. Maybe Bart or Christoph knows, but my impression is still that the priv data is only cleared once in the lifetime of the request (from 1bad6c4a) - at prep time - and some drivers may rely on that (not be cleared again). Unlikely, though.

Quoted 58 lines
Quoted 13 lines
Quoted 12 lines

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")

Quoted 4 lines
Quoted 1 line

---

Ps: in future, please list the changes per version here

Thanks for the heads-up.

Quoted 38 lines
Quoted 37 lines

  drivers/scsi/scsi_lib.c | 14 +++++++-------

  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +0 -2
index be0890e4e706..f1cfe0bb89b2 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 +2 -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->cmd_size && !shost->hostt->init_cmd_priv) +        memset(cmd + 1, 0, shost->hostt->cmd_size);
+
      if (!(req->rq_flags & RQF_DONTPREP)) {
          ret = scsi_prepare_cmd(req);
          if (ret != BLK_STS_OK)

#4 lore raw
Quoted 1 line · John Garry

TBH, I am not sure on the history here. Maybe Bart or Christoph knows, but my impression is still that the priv data is only cleared once in the lifetime of the request (from 1bad6c4a) - at prep time - and some drivers may rely on that (not be cleared again). Unlikely, though.

I'm not aware of any such drivers.

Driver-private data was introduced together with the scsi-mq code. I'm not aware of a similar concept in the legacy SCSI core.

Commit d285203cf647 ("scsi: add support for a blk-mq based I/O path") introduced the following code in kernel v3.17-rc1:

+static int scsi_mq_prep_fn(struct request *req)
+{
[ ... ]
+ memset(cmd, 0, sizeof(struct scsi_cmnd));
[ ... ]
+static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req) +{
[ ... ]
+ if (!(req->cmd_flags & REQ_DONTPREP)) {
+ ret = prep_to_mq(scsi_mq_prep_fn(req));
+ if (ret)
+ goto out_dec_host_busy;
+ req->cmd_flags |= REQ_DONTPREP;
+ }

I think the above memset() call was introduced because of the following code in the legacy SCSI core (from kernel v3.16):

struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)

{
	struct scsi_cmnd *cmd = scsi_host_alloc_command(shost, gfp_mask);

	if (unlikely(!cmd)) {
		unsigned long flags;

		spin_lock_irqsave(&shost->free_list_lock, flags);
		if (likely(!list_empty(&shost->free_list))) {
			cmd = list_entry(shost->free_list.next,
					 struct scsi_cmnd, list);
			list_del_init(&cmd->list);
		}
		spin_unlock_irqrestore(&shost->free_list_lock, flags);

		if (cmd) {
			void *buf, *prot;

			buf = cmd->sense_buffer;
			prot = cmd->prot_sdb;

			memset(cmd, 0, sizeof(*cmd));

			cmd->sense_buffer = buf;
			cmd->prot_sdb = prot;
		}
	}

	return cmd;
}

EXPORT_SYMBOL_GPL(__scsi_get_command);

If I'm reading the v3.16 block layer and SCSI code correctly, __scsi_get_command() was called not only when a command was submitted but also when it got resubmitted. See also the q->prep_rq_fn() call in blk_peek_request().

Since the historic behavior involved clearing the entire struct scsi_cmnd during requeuing, I'm fine with restoring this behavior.

Thanks,

Bart.

#5 lore raw
Quoted 7 lines · Bart Van Assche
Quoted 1 line · John Garry

TBH, I am not sure on the history here. Maybe Bart or Christoph knows, but my impression is still that the priv data is only cleared once in the lifetime of the request (from 1bad6c4a) - at prep time - and some drivers may rely on that (not be cleared again). Unlikely, though.

I'm not aware of any such drivers.

Driver-private data was introduced together with the scsi-mq code. I'm not aware of a similar concept in the legacy SCSI core.

ok, fine. Indeed, to me, it does not make much sense to keep the data in this structure persistent between retries anyway.

Thanks,
John

Reviewed-by on v3 1/1
#6 lore raw
In reply to Ye Bin
Quoted 22 lines · Ye Bin
drivers/scsi/scsi_lib.c +0 -7
index be0890e4e706..f1cfe0bb89b2 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->cmd_size && !shost->hostt->init_cmd_priv)
+		memset(cmd + 1, 0, shost->hostt->cmd_size);
+
  	if (!(req->rq_flags & RQF_DONTPREP)) {
  		ret = scsi_prepare_cmd(req);
  		if (ret != BLK_STS_OK)
#7 lore raw
In reply to Ye Bin
Quoted 12 lines · Ye Bin

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.

Quoted 1 line

[...]

Applied to 6.14/scsi-fixes, thanks!

[1/1] scsi: core: clear driver private data when retry request

      https://git.kernel.org/mkp/scsi/c/dce5c4afd035
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering