From: David Jeffery <djeffery@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: linux-nvme@lists.infradead.org, linux-scsi@vger.kernel.org,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>,
David Jeffery <djeffery@redhat.com>,
Laurence Oberman <loberman@redhat.com>
Subject: [RFC PATCH 6/6] sd: async cache flush on shutdown
Date: Wed, 7 Feb 2024 13:41:00 -0500 [thread overview]
Message-ID: <20240207184100.18066-7-djeffery@redhat.com> (raw)
In-Reply-To: <20240207184100.18066-1-djeffery@redhat.com>
Add async shutdown for the cache flush to the sd device by sending a
SYNCHRONIZE_CACHE command asynchronously. If there is any sort of error,
falls back to the synchronous sd_sync_cache() to try again and resolve
any errors.
Signed-off-by: David Jeffery <djeffery@redhat.com>
Tested-by: Laurence Oberman <loberman@redhat.com>
---
drivers/scsi/sd.c | 66 ++++++++++++++++++++++++++++++++++++++++++-----
drivers/scsi/sd.h | 2 ++
2 files changed, 62 insertions(+), 6 deletions(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 0833b3e6aa6e..f972310df76a 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3838,23 +3838,64 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
return 0;
}
-/*
- * Send a SYNCHRONIZE CACHE instruction down to the device through
- * the normal SCSI command structure. Wait for the command to
- * complete.
- */
-static void sd_shutdown(struct device *dev)
+static void sd_sync_cache_callback(struct scsi_cmnd *scmd,
+ struct scsi_exec_args *args) {
+ struct scsi_disk *sdkp;
+
+ sdkp = container_of(args, struct scsi_disk, shutdown_params);
+ complete(&sdkp->shutdown_done);
+}
+
+static void sd_async_shutdown_start(struct device *dev)
{
struct scsi_disk *sdkp = dev_get_drvdata(dev);
+ const int timeout = sdkp->device->request_queue->rq_timeout
+ * SD_FLUSH_TIMEOUT_MULTIPLIER;
+ int ret;
if (!sdkp)
return; /* this can happen */
+ init_completion(&sdkp->shutdown_done);
+ sdkp->shutdown_params.callback = sd_sync_cache_callback;
+
if (pm_runtime_suspended(dev))
return;
if (sdkp->WCE && sdkp->media_present) {
+ unsigned char cmd[16] = { 0 };
+
sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
+ if (sdkp->device->use_16_for_sync)
+ cmd[0] = SYNCHRONIZE_CACHE_16;
+ else
+ cmd[0] = SYNCHRONIZE_CACHE;
+
+ ret = scsi_execute_cmd_nowait(sdkp->device, cmd, REQ_OP_DRV_IN,
+ timeout, sdkp->max_retries,
+ &sdkp->shutdown_params);
+ if (!ret)
+ return;
+ sdkp->shutdown_params.result = ret;
+ }
+ /* no async I/O to do, go ahead and mark it complete */
+ complete(&sdkp->shutdown_done);
+}
+
+static void sd_async_shutdown_end(struct device *dev)
+{
+ struct scsi_disk *sdkp = dev_get_drvdata(dev);
+
+ if (!sdkp)
+ return;
+
+ if (pm_runtime_suspended(dev))
+ return;
+
+ wait_for_completion(&sdkp->shutdown_done);
+
+ if (sdkp->WCE && sdkp->media_present && sdkp->shutdown_params.result) {
+ /* for any error with the async flush, retry as sync */
sd_sync_cache(sdkp);
}
@@ -3867,6 +3908,17 @@ static void sd_shutdown(struct device *dev)
}
}
+/*
+ * Send a SYNCHRONIZE CACHE instruction down to the device through
+ * the normal SCSI command structure. Wait for the command to
+ * complete.
+ */
+static void sd_shutdown(struct device *dev)
+{
+ sd_async_shutdown_start(dev);
+ sd_async_shutdown_end(dev);
+}
+
static inline bool sd_do_start_stop(struct scsi_device *sdev, bool runtime)
{
return (sdev->manage_system_start_stop && !runtime) ||
@@ -4003,6 +4055,8 @@ static struct scsi_driver sd_template = {
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
.remove = sd_remove,
.shutdown = sd_shutdown,
+ .async_shutdown_start = sd_async_shutdown_start,
+ .async_shutdown_end = sd_async_shutdown_end,
.pm = &sd_pm_ops,
},
.rescan = sd_rescan,
diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h
index 409dda5350d1..7b5098211cec 100644
--- a/drivers/scsi/sd.h
+++ b/drivers/scsi/sd.h
@@ -91,6 +91,8 @@ struct scsi_disk {
struct device disk_dev;
struct gendisk *disk;
struct opal_dev *opal_dev;
+ struct scsi_exec_args shutdown_params;
+ struct completion shutdown_done;
#ifdef CONFIG_BLK_DEV_ZONED
/* Updated during revalidation before the gendisk capacity is known. */
struct zoned_disk_info early_zone_info;
--
2.43.0
prev parent reply other threads:[~2024-02-07 18:42 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-07 18:40 [RFC PATCH 0/6] async device shutdown support David Jeffery
2024-02-07 18:40 ` [RFC PATCH 1/6] minimal async shutdown infrastructure David Jeffery
2024-02-14 3:42 ` Saravana Kannan
2024-02-14 20:55 ` David Jeffery
2024-02-07 18:40 ` [RFC PATCH 2/6] Improve ability to perform async shutdown in parallel David Jeffery
2024-02-07 18:40 ` [RFC PATCH 3/6] pci bus async shutdown support David Jeffery
2024-02-07 18:40 ` [RFC PATCH 4/6] pci nvme " David Jeffery
2024-02-07 18:40 ` [RFC PATCH 5/6] scsi mid layer support for async command submit David Jeffery
2024-02-07 18:41 ` David Jeffery [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240207184100.18066-7-djeffery@redhat.com \
--to=djeffery@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=linux-scsi@vger.kernel.org \
--cc=loberman@redhat.com \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®