Skip to contentSkip to conversation

Reduce ATA disk resume time

PATCH Superseded Bart Van Assche 4 participants 11 messages Jun 28, 2022 – Jun 30, 2022 SCSI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Reduce ATA disk resume time

Hi Martin,

Recently it was reported that patch "scsi: core: pm: Rely on the device driver core for async power management" causes resuming to take longer if an ATA disk is present. This patch series fixes that regression. Please consider this patch series for kernel v5.20.

Thanks,

Bart.

Bart Van Assche (3):
scsi: core: Move the definition of SCSI_QUEUE_DELAY
scsi: core: Retry after a delay if the device is becoming ready
scsi: sd: Rework asynchronous resume support

 drivers/scsi/scsi_error.c |  4 +-
 drivers/scsi/scsi_lib.c   | 14 +++----
 drivers/scsi/sd.c         | 79 ++++++++++++++++++++++++++++++---------
 drivers/scsi/sd.h         |  5 +++
 4 files changed, 75 insertions(+), 27 deletions(-)
#1 lore raw

PATCH 1/3 scsi: core: Move the definition of SCSI_QUEUE_DELAY

Move the definition of SCSI_QUEUE_DELAY to just above the function that uses it.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_lib.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/scsi/scsi_lib.c +7 -7
index 6ffc9e4258a8..2aca0a838ca5 100644
@@ -75,13 +75,6 @@ int scsi_init_sense_cache(struct Scsi_Host *shost)
 	return ret;
 }
 
-/*
- * When to reinvoke queueing after a resource shortage. It's 3 msecs to
- * not change behaviour from the previous unplug mechanism, experimentation
- * may prove this needs changing.
- */
-#define SCSI_QUEUE_DELAY	3
-
 static void
 scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
 {
@@ -1648,6 +1641,13 @@ static void scsi_mq_put_budget(struct request_queue *q, int budget_token)
 	sbitmap_put(&sdev->budget_map, budget_token);
 }
 
+/*
+ * When to reinvoke queueing after a resource shortage. It's 3 msecs to
+ * not change behaviour from the previous unplug mechanism, experimentation
+ * may prove this needs changing.
+ */
+#define SCSI_QUEUE_DELAY 3
+
 static int scsi_mq_get_budget(struct request_queue *q)
 {
 	struct scsi_device *sdev = q->queuedata;
#2 lore raw

PATCH 2/3 scsi: core: Retry after a delay if the device is becoming ready

In reply to Bart Van Assche

If a logical unit reports that it is becoming ready, retry the command after a delay instead of retrying immediately.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_error.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/scsi_error.c +2 -2
index 49ef864df581..fb7e363c4c00 100644
@@ -625,10 +625,10 @@ enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
 			return NEEDS_RETRY;
 		/*
 		 * if the device is in the process of becoming ready, we
-		 * should retry.
+		 * should retry after a delay.
 		 */
 		if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
-			return NEEDS_RETRY;
+			return ADD_TO_MLQUEUE;
 		/*
 		 * if the device is not started, we need to wake
 		 * the error handler to start the motor
#3 lore raw

Re: [PATCH 2/3] scsi: core: Retry after a delay if the device is becoming ready

Quoted 16 lines · Bart Van Assche

If a logical unit reports that it is becoming ready, retry the command after a delay instead of retrying immediately.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_error.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/scsi_error.c +0 -0
index 49ef864df581..fb7e363c4c00 100644

drivers/scsi/scsi_error.c enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd) · line 631

Quoted 9 lines
drivers/scsi/scsi_error.c +2 -2
@@ -625,10 +625,10 @@ enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
 			return NEEDS_RETRY;
 		/*
 		 * if the device is in the process of becoming ready, we
-		 * should retry.
+		 * should retry after a delay.
 		 */
 		if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
-			return NEEDS_RETRY;
+			return ADD_TO_MLQUEUE;

The above code & commit log just said changing to retry after a delay, but not explains why, care to document reason why the delay is useful?

Thanks
Ming

#4 lore raw

Re: [PATCH 2/3] scsi: core: Retry after a delay if the device is becoming ready

Quoted 21 lines · Ming Lei
Quoted 20 lines · Bart Van Assche

If a logical unit reports that it is becoming ready, retry the command after a delay instead of retrying immediately.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
  drivers/scsi/scsi_error.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/scsi_error.c +0 -1
index 49ef864df581..fb7e363c4c00 100644
@@ -625,10 +625,10 @@ enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
  			return NEEDS_RETRY;
  		/*
  		 * if the device is in the process of becoming ready, we
-		 * should retry.
Quoted 8 lines

drivers/scsi/scsi_error.c enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd) · line 631

Quoted 5 lines
drivers/scsi/scsi_error.c +2 -1
+		 * should retry after a delay.
  		 */
  		if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
-			return NEEDS_RETRY;
+			return ADD_TO_MLQUEUE;

The above code & commit log just said changing to retry after a delay, but not explains why, care to document reason why the delay is useful?

I came up with this patch because I was concerned about the impact of the LOGICAL UNIT IS IN PROCESS OF BECOMING READY response on the START command. While reviewing SBC-4 I noticed that that response can be produced for any command but not for the START command with IMM=0. So I think this patch can be dropped since sd_start_stop_device() does not set the IMM flag.

Thanks,

Bart.

#5 lore raw

PATCH 3/3 scsi: sd: Rework asynchronous resume support

In reply to Bart Van Assche

For some technologies, e.g. an ATA bus, resuming can take multiple seconds. Waiting for resume to finish can cause a very noticeable delay. Hence this patch that restores the behavior from before patch "scsi:
core: pm: Rely on the device driver core for async power management" for most SCSI devices.

This patch introduces a behavior change: if the START command fails, do not consider this as a SCSI disk resume failure.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
Fixes a19a93e4c6a9 ("scsi: core: pm: Rely on the device driver core for async power management")
---
 drivers/scsi/sd.c | 79 ++++++++++++++++++++++++++++++++++++-----------
 drivers/scsi/sd.h |  5 +++
 2 files changed, 66 insertions(+), 18 deletions(-)
drivers/scsi/sd.c +61 -18
index 895b56c8f25e..06888b675e71 100644
@@ -103,6 +103,7 @@ static void sd_config_discard(struct scsi_disk *, unsigned int);
 static void sd_config_write_same(struct scsi_disk *);
 static int  sd_revalidate_disk(struct gendisk *);
 static void sd_unlock_native_capacity(struct gendisk *disk);
+static void sd_start_done_work(struct work_struct *work);
 static int  sd_probe(struct device *);
 static int  sd_remove(struct device *);
 static void sd_shutdown(struct device *);
@@ -3463,6 +3464,7 @@ static int sd_probe(struct device *dev)
 	sdkp->max_retries = SD_MAX_RETRIES;
 	atomic_set(&sdkp->openers, 0);
 	atomic_set(&sdkp->device->ioerr_cnt, 0);
+	INIT_WORK(&sdkp->start_done_work, sd_start_done_work);
 
 	if (!sdp->request_queue->rq_timeout) {
 		if (sdp->type != TYPE_MOD)
@@ -3585,12 +3587,64 @@ static void scsi_disk_release(struct device *dev)
 	kfree(sdkp);
 }
 
+/* Process sense data after a START command finished. */
+static void sd_start_done_work(struct work_struct *work)
+{
+	struct scsi_disk *sdkp = container_of(work, typeof(*sdkp),
+					      start_done_work);
+	struct scsi_sense_hdr sshdr;
+	int res = sdkp->start_result;
+
+	if (res == 0)
+		return;
+
+	sd_print_result(sdkp, "Start/Stop Unit failed", res);
+	if (res > 0 && scsi_normalize_sense(sdkp->start_sense_buffer,
+					    sdkp->start_sense_len, &sshdr))
+		sd_print_sense_hdr(sdkp, &sshdr);
+}
+
+/* A START command finished. May be called from interrupt context. */
+static void sd_start_done(struct request *req, blk_status_t status)
+{
+	const struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+	struct scsi_disk *sdkp = scsi_disk(req->q->disk);
+
+	sdkp->start_result = scmd->result;
+	WARN_ON_ONCE(scmd->sense_len > SCSI_SENSE_BUFFERSIZE);
+	sdkp->start_sense_len = scmd->sense_len;
+	memcpy(sdkp->start_sense_buffer, scmd->sense_buffer, scmd->sense_len);
+	WARN_ON_ONCE(!schedule_work(&sdkp->start_done_work));
+}
+
+/* Submit a START command asynchronously. */
+static int sd_submit_start(struct scsi_disk *sdkp, u8 cmd[], u8 cmd_len)
+{
+	struct scsi_device *sdev = sdkp->device;
+	struct request_queue *q = sdev->request_queue;
+	struct request *req;
+	struct scsi_cmnd *scmd;
+
+	req = scsi_alloc_request(q, REQ_OP_DRV_IN, BLK_MQ_REQ_PM);
+	if (IS_ERR(req))
+		return PTR_ERR(req);
+
+	scmd = blk_mq_rq_to_pdu(req);
+	scmd->cmd_len = cmd_len;
+	memcpy(scmd->cmnd, cmd, cmd_len);
+	scmd->allowed = sdkp->max_retries;
+	req->timeout = SD_TIMEOUT;
+	req->rq_flags |= RQF_PM | RQF_QUIET;
+	req->end_io = sd_start_done;
+	blk_execute_rq_nowait(req, /*at_head=*/true);
+
+	return 0;
+}
+
 static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
 {
 	unsigned char cmd[6] = { START_STOP };	/* START_VALID */
-	struct scsi_sense_hdr sshdr;
 	struct scsi_device *sdp = sdkp->device;
-	int res;
 
 	if (start)
 		cmd[4] |= 1;	/* START */
@@ -3601,23 +3655,10 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
 	if (!scsi_device_online(sdp))
 		return -ENODEV;
 
-	res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
-			SD_TIMEOUT, sdkp->max_retries, 0, RQF_PM, NULL);
-	if (res) {
-		sd_print_result(sdkp, "Start/Stop Unit failed", res);
-		if (res > 0 && scsi_sense_valid(&sshdr)) {
-			sd_print_sense_hdr(sdkp, &sshdr);
-			/* 0x3a is medium not present */
-			if (sshdr.asc == 0x3a)
-				res = 0;
-		}
-	}
+	/* Wait until processing of sense data has finished. */
+	flush_work(&sdkp->start_done_work);
 
-	/* SCSI error codes must not go to the generic layer */
-	if (res)
-		return -EIO;
-
-	return 0;
+	return sd_submit_start(sdkp, cmd, sizeof(cmd));
 }
 
 /*
@@ -3644,6 +3685,8 @@ static void sd_shutdown(struct device *dev)
 		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
 		sd_start_stop_device(sdkp, 0);
 	}
+
+	flush_work(&sdkp->start_done_work);
 }
 
 static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
drivers/scsi/sd.h +5 -0
index 5eea762f84d1..b89187761d61 100644
@@ -150,6 +150,11 @@ struct scsi_disk {
 	unsigned	urswrz : 1;
 	unsigned	security : 1;
 	unsigned	ignore_medium_access_errors : 1;
+
+	int		start_result;
+	u32		start_sense_len;
+	u8		start_sense_buffer[SCSI_SENSE_BUFFERSIZE];
+	struct work_struct start_done_work;
 };
 #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
 
#6 lore raw

Re: [PATCH 3/3] scsi: sd: Rework asynchronous resume support

Quoted 48 lines · Bart Van Assche

For some technologies, e.g. an ATA bus, resuming can take multiple seconds. Waiting for resume to finish can cause a very noticeable delay. Hence this patch that restores the behavior from before patch "scsi:
core: pm: Rely on the device driver core for async power management" for most SCSI devices.

This patch introduces a behavior change: if the START command fails, do not consider this as a SCSI disk resume failure.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
Fixes a19a93e4c6a9 ("scsi: core: pm: Rely on the device driver core for async power management")
---
  drivers/scsi/sd.c | 79 ++++++++++++++++++++++++++++++++++++-----------
  drivers/scsi/sd.h |  5 +++
  2 files changed, 66 insertions(+), 18 deletions(-)
drivers/scsi/sd.c +3 -0
index 895b56c8f25e..06888b675e71 100644
@@ -103,6 +103,7 @@ static void sd_config_discard(struct scsi_disk *, unsigned int);
  static void sd_config_write_same(struct scsi_disk *);
  static int  sd_revalidate_disk(struct gendisk *);
  static void sd_unlock_native_capacity(struct gendisk *disk);
+static void sd_start_done_work(struct work_struct *work);
  static int  sd_probe(struct device *);
  static int  sd_remove(struct device *);
  static void sd_shutdown(struct device *);
@@ -3463,6 +3464,7 @@ static int sd_probe(struct device *dev)
  	sdkp->max_retries = SD_MAX_RETRIES;
  	atomic_set(&sdkp->openers, 0);
  	atomic_set(&sdkp->device->ioerr_cnt, 0);
+	INIT_WORK(&sdkp->start_done_work, sd_start_done_work);
 
  	if (!sdp->request_queue->rq_timeout) {
  		if (sdp->type != TYPE_MOD)
@@ -3585,12 +3587,64 @@ static void scsi_disk_release(struct device *dev)
  	kfree(sdkp);
  }
 
+/* Process sense data after a START command finished. */

drivers/scsi/sd.c static void scsi_disk_release(struct device *dev) · line 3600

Quoted 10 lines
drivers/scsi/sd.c +10 -0
+static void sd_start_done_work(struct work_struct *work)
+{
+	struct scsi_disk *sdkp = container_of(work, typeof(*sdkp),
+					      start_done_work);
+	struct scsi_sense_hdr sshdr;
+	int res = sdkp->start_result;
+
+	if (res == 0)
+		return;
+ > +	sd_print_result(sdkp, "Start/Stop Unit failed", res);

Surely START/STOP unit can succeed, no?

Quoted 93 lines
drivers/scsi/sd.c +47 -18
+	if (res > 0 && scsi_normalize_sense(sdkp->start_sense_buffer,
+					    sdkp->start_sense_len, &sshdr))
+		sd_print_sense_hdr(sdkp, &sshdr);
+}
+
+/* A START command finished. May be called from interrupt context. */
+static void sd_start_done(struct request *req, blk_status_t status)
+{
+	const struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+	struct scsi_disk *sdkp = scsi_disk(req->q->disk);
+
+	sdkp->start_result = scmd->result;
+	WARN_ON_ONCE(scmd->sense_len > SCSI_SENSE_BUFFERSIZE);
+	sdkp->start_sense_len = scmd->sense_len;
+	memcpy(sdkp->start_sense_buffer, scmd->sense_buffer, scmd->sense_len);
+	WARN_ON_ONCE(!schedule_work(&sdkp->start_done_work));
+}
+
+/* Submit a START command asynchronously. */
+static int sd_submit_start(struct scsi_disk *sdkp, u8 cmd[], u8 cmd_len)
+{
+	struct scsi_device *sdev = sdkp->device;
+	struct request_queue *q = sdev->request_queue;
+	struct request *req;
+	struct scsi_cmnd *scmd;
+
+	req = scsi_alloc_request(q, REQ_OP_DRV_IN, BLK_MQ_REQ_PM);
+	if (IS_ERR(req))
+		return PTR_ERR(req);
+
+	scmd = blk_mq_rq_to_pdu(req);
+	scmd->cmd_len = cmd_len;
+	memcpy(scmd->cmnd, cmd, cmd_len);
+	scmd->allowed = sdkp->max_retries;
+	req->timeout = SD_TIMEOUT;
+	req->rq_flags |= RQF_PM | RQF_QUIET;
+	req->end_io = sd_start_done;
+	blk_execute_rq_nowait(req, /*at_head=*/true);
+
+	return 0;
+}
+
  static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  {
  	unsigned char cmd[6] = { START_STOP };	/* START_VALID */
-	struct scsi_sense_hdr sshdr;
  	struct scsi_device *sdp = sdkp->device;
-	int res;
 
  	if (start)
  		cmd[4] |= 1;	/* START */
@@ -3601,23 +3655,10 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  	if (!scsi_device_online(sdp))
  		return -ENODEV;
 
-	res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
-			SD_TIMEOUT, sdkp->max_retries, 0, RQF_PM, NULL);
-	if (res) {
-		sd_print_result(sdkp, "Start/Stop Unit failed", res);
-		if (res > 0 && scsi_sense_valid(&sshdr)) {
-			sd_print_sense_hdr(sdkp, &sshdr);
-			/* 0x3a is medium not present */
-			if (sshdr.asc == 0x3a)
-				res = 0;
-		}
-	}
+	/* Wait until processing of sense data has finished. */
+	flush_work(&sdkp->start_done_work);
 
-	/* SCSI error codes must not go to the generic layer */
-	if (res)
-		return -EIO;
-
-	return 0;
+	return sd_submit_start(sdkp, cmd, sizeof(cmd));
  }
 
  /*
@@ -3644,6 +3685,8 @@ static void sd_shutdown(struct device *dev)
  		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  		sd_start_stop_device(sdkp, 0);
  	}
+
+	flush_work(&sdkp->start_done_work);
  }
 
  static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
drivers/scsi/sd.h +0 -0
index 5eea762f84d1..b89187761d61 100644
@@ -150,6 +150,11 @@ struct scsi_disk {
  	unsigned	urswrz : 1;

drivers/scsi/sd.h struct scsi_disk { · line 160

Quoted 10 lines
drivers/scsi/sd.h +5 -0
  	unsigned	security : 1;
  	unsigned	ignore_medium_access_errors : 1;
+
+	int		start_result;
+	u32		start_sense_len;
+	u8		start_sense_buffer[SCSI_SENSE_BUFFERSIZE];
+	struct work_struct start_done_work;
  };
  #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
 

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
#7 lore raw

Re: [PATCH 3/3] scsi: sd: Rework asynchronous resume support

Quoted 15 lines · Hannes Reinecke

drivers/scsi/sd.c static void scsi_disk_release(struct device *dev) · line 3601

Quoted 12 lines · Bart Van Assche
drivers/scsi/sd.c +12 -0
+/* Process sense data after a START command finished. */
+static void sd_start_done_work(struct work_struct *work)
+{
+    struct scsi_disk *sdkp = container_of(work, typeof(*sdkp),
+                          start_done_work);
+    struct scsi_sense_hdr sshdr;
+    int res = sdkp->start_result;
+
+    if (res == 0)
+        return;
+
+    sd_print_result(sdkp, "Start/Stop Unit failed", res);

Surely START/STOP unit can succeed, no?

Yes, hence the "if (res == 0) return;" code. Did I perhaps misunderstand your question?

Bart.

#8 lore raw

Re: [PATCH 3/3] scsi: sd: Rework asynchronous resume support

In reply to Bart Van Assche
Quoted 52 lines · Bart Van Assche

For some technologies, e.g. an ATA bus, resuming can take multiple seconds. Waiting for resume to finish can cause a very noticeable delay. Hence this patch that restores the behavior from before patch "scsi:
core: pm: Rely on the device driver core for async power management" for most SCSI devices.

This patch introduces a behavior change: if the START command fails, do not consider this as a SCSI disk resume failure.

Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
Fixes a19a93e4c6a9 ("scsi: core: pm: Rely on the device driver core for async power management")
---
  drivers/scsi/sd.c | 79 ++++++++++++++++++++++++++++++++++++-----------
  drivers/scsi/sd.h |  5 +++
  2 files changed, 66 insertions(+), 18 deletions(-)
drivers/scsi/sd.c +7 -0
index 895b56c8f25e..06888b675e71 100644
@@ -103,6 +103,7 @@ static void sd_config_discard(struct scsi_disk *, unsigned int);
  static void sd_config_write_same(struct scsi_disk *);
  static int  sd_revalidate_disk(struct gendisk *);
  static void sd_unlock_native_capacity(struct gendisk *disk);
+static void sd_start_done_work(struct work_struct *work);
  static int  sd_probe(struct device *);
  static int  sd_remove(struct device *);
  static void sd_shutdown(struct device *);
@@ -3463,6 +3464,7 @@ static int sd_probe(struct device *dev)
  	sdkp->max_retries = SD_MAX_RETRIES;
  	atomic_set(&sdkp->openers, 0);
  	atomic_set(&sdkp->device->ioerr_cnt, 0);
+	INIT_WORK(&sdkp->start_done_work, sd_start_done_work);
 
  	if (!sdp->request_queue->rq_timeout) {
  		if (sdp->type != TYPE_MOD)
@@ -3585,12 +3587,64 @@ static void scsi_disk_release(struct device *dev)
  	kfree(sdkp);
  }
 
+/* Process sense data after a START command finished. */
+static void sd_start_done_work(struct work_struct *work)
+{
+	struct scsi_disk *sdkp = container_of(work, typeof(*sdkp),
+					      start_done_work);

drivers/scsi/sd.c static void scsi_disk_release(struct device *dev) · line 3604

Quoted 10 lines
drivers/scsi/sd.c +10 -0
+	struct scsi_sense_hdr sshdr;
+	int res = sdkp->start_result;
+
+	if (res == 0)
+		return;
+
+	sd_print_result(sdkp, "Start/Stop Unit failed", res);
+	if (res > 0 && scsi_normalize_sense(sdkp->start_sense_buffer,
+					    sdkp->start_sense_len, &sshdr))
+		sd_print_sense_hdr(sdkp, &sshdr);

nit: maybe you can reduce indentation, like:

	if (res < 0)
		return;

	if (scsi_normalize_sense(sdkp->start_sense_buffer,
			sdkp->start_sense_len, &sshdr)) {
		sd_print_sense_hdr(sdkp, &sshdr);
	}

drivers/scsi/sd.c static void scsi_disk_release(struct device *dev) · line 3614

Quoted 10 lines
drivers/scsi/sd.c +10 -0
+}
+
+/* A START command finished. May be called from interrupt context. */
+static void sd_start_done(struct request *req, blk_status_t status)
+{
+	const struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+	struct scsi_disk *sdkp = scsi_disk(req->q->disk);
+
+	sdkp->start_result = scmd->result;
+	WARN_ON_ONCE(scmd->sense_len > SCSI_SENSE_BUFFERSIZE);

If scmd->sense_len > SCSI_SENSE_BUFFERSIZE, do you really want to go on to copy at sdkp->start_sense_buffer (which is of size SCSI_SENSE_BUFFERSIZE)? Won't that cause a corruption?

Quoted 91 lines
drivers/scsi/sd.c +34 -18
+	sdkp->start_sense_len = scmd->sense_len;
+	memcpy(sdkp->start_sense_buffer, scmd->sense_buffer, scmd->sense_len);
+	WARN_ON_ONCE(!schedule_work(&sdkp->start_done_work));
+}
+
+/* Submit a START command asynchronously. */
+static int sd_submit_start(struct scsi_disk *sdkp, u8 cmd[], u8 cmd_len)
+{
+	struct scsi_device *sdev = sdkp->device;
+	struct request_queue *q = sdev->request_queue;
+	struct request *req;
+	struct scsi_cmnd *scmd;
+
+	req = scsi_alloc_request(q, REQ_OP_DRV_IN, BLK_MQ_REQ_PM);
+	if (IS_ERR(req))
+		return PTR_ERR(req);
+
+	scmd = blk_mq_rq_to_pdu(req);
+	scmd->cmd_len = cmd_len;
+	memcpy(scmd->cmnd, cmd, cmd_len);
+	scmd->allowed = sdkp->max_retries;
+	req->timeout = SD_TIMEOUT;
+	req->rq_flags |= RQF_PM | RQF_QUIET;
+	req->end_io = sd_start_done;
+	blk_execute_rq_nowait(req, /*at_head=*/true);
+
+	return 0;
+}
+
  static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  {
  	unsigned char cmd[6] = { START_STOP };	/* START_VALID */
-	struct scsi_sense_hdr sshdr;
  	struct scsi_device *sdp = sdkp->device;
-	int res;
 
  	if (start)
  		cmd[4] |= 1;	/* START */
@@ -3601,23 +3655,10 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  	if (!scsi_device_online(sdp))
  		return -ENODEV;
 
-	res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
-			SD_TIMEOUT, sdkp->max_retries, 0, RQF_PM, NULL);
-	if (res) {
-		sd_print_result(sdkp, "Start/Stop Unit failed", res);
-		if (res > 0 && scsi_sense_valid(&sshdr)) {
-			sd_print_sense_hdr(sdkp, &sshdr);
-			/* 0x3a is medium not present */
-			if (sshdr.asc == 0x3a)
-				res = 0;
-		}
-	}
+	/* Wait until processing of sense data has finished. */
+	flush_work(&sdkp->start_done_work);
 
-	/* SCSI error codes must not go to the generic layer */
-	if (res)
-		return -EIO;
-
-	return 0;
+	return sd_submit_start(sdkp, cmd, sizeof(cmd));
  }
 
  /*
@@ -3644,6 +3685,8 @@ static void sd_shutdown(struct device *dev)
  		sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  		sd_start_stop_device(sdkp, 0);
  	}
+
+	flush_work(&sdkp->start_done_work);
  }
 
  static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
drivers/scsi/sd.h +5 -0
index 5eea762f84d1..b89187761d61 100644
@@ -150,6 +150,11 @@ struct scsi_disk {
  	unsigned	urswrz : 1;
  	unsigned	security : 1;
  	unsigned	ignore_medium_access_errors : 1;
+
+	int		start_result;
+	u32		start_sense_len;
+	u8		start_sense_buffer[SCSI_SENSE_BUFFERSIZE];
+	struct work_struct start_done_work;
  };
  #define to_scsi_disk(obj) container_of(obj, struct scsi_disk, disk_dev)
 

.

#9 lore raw

Re: [PATCH 3/3] scsi: sd: Rework asynchronous resume support

Quoted 11 lines · John Garry

drivers/scsi/sd.c static void scsi_disk_release(struct device *dev) · line 3614

Quoted 8 lines · Bart Van Assche
drivers/scsi/sd.c +8 -0
+/* A START command finished. May be called from interrupt context. */
+static void sd_start_done(struct request *req, blk_status_t status)
+{
+    const struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+    struct scsi_disk *sdkp = scsi_disk(req->q->disk);
+
+    sdkp->start_result = scmd->result;
+    WARN_ON_ONCE(scmd->sense_len > SCSI_SENSE_BUFFERSIZE);

If scmd->sense_len > SCSI_SENSE_BUFFERSIZE, do you really want to go on to copy at sdkp->start_sense_buffer (which is of size SCSI_SENSE_BUFFERSIZE)? Won't that cause a corruption?

scsi_mq_init_request() allocates a buffer with size SCSI_SENSE_BUFFERSIZE. SCSI LLDs copy sense data into that buffer. I am not aware of any SCSI LLD that modifies the cmd->sense_buffer pointer. So if scmd->sense_len would be larger than SCSI_SENSE_BUFFERSIZE that either indicates that the LLD reported a sense length that is too large or that it wrote outside the bounds of the sense buffer. Do we really need to add a protection in the SCSI core against buggy LLDs?

Thanks,

Bart.

#10 lore raw

Re: [PATCH 3/3] scsi: sd: Rework asynchronous resume support

Quoted 13 lines · Bart Van Assche
Quoted 11 lines · John Garry

drivers/scsi/sd.c static void scsi_disk_release(struct device *dev) · line 3614

Quoted 8 lines · Bart Van Assche
drivers/scsi/sd.c +8 -0
+/* A START command finished. May be called from interrupt context. */
+static void sd_start_done(struct request *req, blk_status_t status)
+{
+    const struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+    struct scsi_disk *sdkp = scsi_disk(req->q->disk);
+
+    sdkp->start_result = scmd->result;
+    WARN_ON_ONCE(scmd->sense_len > SCSI_SENSE_BUFFERSIZE);

If scmd->sense_len > SCSI_SENSE_BUFFERSIZE, do you really want to go on to copy at sdkp->start_sense_buffer (which is of size SCSI_SENSE_BUFFERSIZE)? Won't that cause a corruption?

Quoted 1 line

scsi_mq_init_request() allocates a buffer with size SCSI_SENSE_BUFFERSIZE. SCSI LLDs copy sense data into that buffer. I am not aware of any SCSI LLD that modifies the cmd->sense_buffer pointer. So if scmd->sense_len would be larger than SCSI_SENSE_BUFFERSIZE that either indicates that the LLD reported a sense length that is too large or that it wrote outside the bounds of the sense buffer. Do we really need to add a protection in the SCSI core against buggy LLDs?

A result of the above is that SCSI_SENSE_BUFFERSIZE bytes can be copied instead of scmd->sense_len. I will make that change.

Bart.