async device shutdown support
Subsystems (4)
cover async device shutdown support
This is another attempt to implement an acceptable implementation of async device shutdown, inspired by a previous attempt by Tanjore Suresh. For systems with many disks, async shutdown can greatly reduce shutdown times from having slow operations run in parallel. The older patches were rejected, with this new implementation attempting to fix my understanding of the flaws in the older patches.
Using similar interfaces and building off the ideas of the older patches, this patchset creates an async shutdown implementation which follows the basic ordering of the shutdown list, ensuring the shutdown of any children devices complete whether synchronous or asynchronous before performing shutdown on a parent device.
In addition to an implementation for asynchronous pci nvme shutdown, this patchset also adds support for async shutdown of sd devices for cache flush. As an example of the effects of the patch, one system with a large amount of disks went from over 30 seconds to shut down to less than 5.
The specific driver changes are the roughest part of this patchset. But the acceptability of the core async functionality is critical. Any feedback on flaws or improvements is appreciated.
David Jeffery (6):
minimal async shutdown infrastructure Improve ability to perform async shutdown in parallel pci bus async shutdown support
pci nvme async shutdown support
scsi mid layer support for async command submit
sd: async cache flush on shutdown
drivers/base/base.h | 1 + drivers/base/core.c | 149 +++++++++++++++++++++++++++++++++- drivers/nvme/host/core.c | 26 ++++-- drivers/nvme/host/nvme.h | 2 + drivers/nvme/host/pci.c | 53 +++++++++++- drivers/pci/pci-driver.c | 24 +++++- drivers/scsi/scsi_lib.c | 138 ++++++++++++++++++++++++------- drivers/scsi/sd.c | 66 +++++++++++++-- drivers/scsi/sd.h | 2 + include/linux/device/bus.h | 8 +- include/linux/device/driver.h | 7 ++ include/linux/pci.h | 4 + include/scsi/scsi_device.h | 8 ++ 13 files changed, 439 insertions(+), 49 deletions(-)
Signature · 1 line
2.43.0
RFC PATCH 1/6 minimal async shutdown infrastructure
Adds the async_shutdown_start and async_shutdown_end calls to perform async shutdown. Implements a very minimalist method of async shutdown support within device_shutdown(). The device at the head of the shutdown list is checked against a list of devices under async shutdown. If the head is a parent of a device on the async list, all active async shutdown operations are completed before the parent's shutdown call is performed.
The number of async operations also has a max limit to prevent the list being checked for a child from getting overly large.
--- drivers/base/core.c | 116 +++++++++++++++++++++++++++++++++- include/linux/device/bus.h | 8 ++- include/linux/device/driver.h | 7 ++ 3 files changed, 127 insertions(+), 4 deletions(-)
drivers/base/core.c +113 -3
@@ -4719,12 +4719,92 @@ int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) }EXPORT_SYMBOL_GPL (device_change_owner); + +#define MAX_ASYNC_SHUTDOWNS 32 +static int async_shutdown_count; +static LIST_HEAD (async_shutdown_list); + +/** +* If a device has a child busy with an async shutdown or there are too many +* async shutdowns active, the device may not be shut down at this time. +*/ +static bool may_shutdown_device (struct device *dev) +{ +struct device *tmp; + +if (async_shutdown_count >=MAX_ASYNC_SHUTDOWNS ) +return false; + +list_for_each_entry (tmp, &async_shutdown_list, kobj.entry) { +if (tmp->parent == dev) +return false; + } +return true; +} + +/** +* Call and track each async shutdown call +*/ +static void async_shutdown_start (struct device *dev,void (*callback) (struct device *)) +{ +if (initcall_debug) +dev_info (dev,"async_shutdown_start\n" ); + + (*callback)(dev); +list_add_tail (&dev->kobj.entry, &async_shutdown_list); + async_shutdown_count++; +} + +/** +* Wait for all async shutdown operations currently active to complete +*/ +static void wait_for_active_async_shutdown (void ) +{ +struct device *dev, *parent; + +while (!list_empty (&async_shutdown_list)) { + dev = list_entry(async_shutdown_list.next, struct device, + kobj.entry); + + parent = dev->parent; + +/* +* Make sure the device is off the list +*/ +list_del_init (&dev->kobj.entry); +if (parent) +device_lock (parent); +device_lock (dev); +if (dev->bus && dev->bus->async_shutdown_end) { +if (initcall_debug) +dev_info (dev, +"async_shutdown_end called\n" ); + dev->bus->async_shutdown_end (dev); + }else if (dev->driver && dev->driver->async_shutdown_end) { +if (initcall_debug) +dev_info (dev, +"async_shutdown_end called\n" ); + dev->driver->async_shutdown_end (dev); + } +device_unlock (dev); +if (parent) +device_unlock (parent); + +put_device (dev); +put_device (parent); + } +if (initcall_debug) +printk (KERN_INFO "device shutdown: waited for %d async shutdown callbacks\n" , async_shutdown_count); + async_shutdown_count =0 ; +} +/** * device_shutdown - call ->shutdown() on each device to shutdown. */ void device_shutdown (void ) {struct device *dev, *parent; +bool async_busy;wait_for_device_probe ();device_block_probing ();
@@ -4741,6 +4821,8 @@ void device_shutdown(void) dev = list_entry(devices_kset->list.prev, struct device, kobj.entry); + async_busy = false; +/* * hold reference count of device's parent to * prevent it from being freed because parent's
@@ -4748,6 +4830,17 @@ void device_shutdown(void)*/ parent =get_device (dev->parent);get_device (dev); + +if (!may_shutdown_device (dev)) { +put_device (dev); +put_device (parent); + +spin_unlock (&devices_kset->list_lock); +wait_for_active_async_shutdown (); +spin_lock (&devices_kset->list_lock); +continue ; + } +/* * Make sure the device is off the kset list, in the * event that dev->*->shutdown() doesn't remove it.
@@ -4769,26 +4862,43 @@ void device_shutdown(void)dev_info (dev,"shutdown_pre\n" ); dev->class->shutdown_pre (dev); } -if (dev->bus && dev->bus->shutdown) { +if (dev->bus && dev->bus->async_shutdown_start) { +async_shutdown_start (dev, dev->bus->async_shutdown_start); + async_busy = true; + }else if (dev->bus && dev->bus->shutdown) {if (initcall_debug)dev_info (dev,"shutdown\n" ); dev->bus->shutdown (dev); + }else if (dev->driver && dev->driver->async_shutdown_start) { +async_shutdown_start (dev, dev->driver->async_shutdown_start); + async_busy = true; }else if (dev->driver && dev->driver->shutdown) {if (initcall_debug)dev_info (dev,"shutdown\n" ); dev->driver->shutdown (dev); + }else { +if (initcall_debug) +dev_info (dev,"no shutdown callback\n" ); }device_unlock (dev);if (parent)device_unlock (parent); -put_device (dev); -put_device (parent); +/* if device has an async shutdown, drop the ref when done */ +if (!async_busy) { +put_device (dev); +put_device (parent); + }spin_lock (&devices_kset->list_lock); }spin_unlock (&devices_kset->list_lock); +/* +* Wait for any async shutdown still running. +*/ +if (!list_empty (&async_shutdown_list)) +wait_for_active_async_shutdown (); }/*
include/linux/device/bus.h +7 -1
@@ -48,7 +48,11 @@ struct fwnode_handle;* will never get called until they do. * @remove: Called when a device removed from this bus. * @shutdown: Called at shut-down time to quiesce the device. -* +* @async_shutdown_start: Optional call to support and begin the shutdown +* process on the device in an asynchronous manner. +* @async_shutdown_end: Optional call to complete an asynchronous +* shutdown of the device. Must be provided if a +* sync_shutdown_start call is provided. * @online: Called to put the device back online (after offlining it). * @offline: Called to put the device offline for hot-removal. May fail. *
@@ -87,6 +91,8 @@ struct bus_type {void (*sync_state)(struct device *dev);void (*remove)(struct device *dev);void (*shutdown)(struct device *dev); +void (*async_shutdown_start)(struct device *dev); +void (*async_shutdown_end)(struct device *dev);int (*online)(struct device *dev);int (*offline)(struct device *dev);
include/linux/device/driver.h +7 -0
@@ -71,6 +71,11 @@ enum probe_type {* @remove: Called when the device is removed from the system to * unbind a device from this driver. * @shutdown: Called at shut-down time to quiesce the device. +* @async_shutdown_start: Optional call to support and begin the shutdown +* process on the device in an asynchronous manner. +* @async_shutdown_end: Optional call to complete an asynchronous +* shutdown of the device. Must be provided if a +* sync_shutdown_start call is provided. * @suspend: Called to put the device to sleep mode. Usually to a * low power state. * @resume: Called to bring a device from sleep mode.
@@ -110,6 +115,8 @@ struct device_driver {void (*sync_state)(struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev); +void (*async_shutdown_start) (struct device *dev); +void (*async_shutdown_end) (struct device *dev);int (*suspend) (struct device *dev,pm_message_t state);int (*resume) (struct device *dev);const struct attribute_group **groups;
Signature · 1 line
2.43.0
Re: [RFC PATCH 1/6] minimal async shutdown infrastructure
Quoted 66 lines · David Jeffery
Adds the
async_shutdown_startandasync_shutdown_endcalls to perform async shutdown. Implements a very minimalist method of async shutdown support withindevice_shutdown(). The device at the head of the shutdown list is checked against a list of devices under async shutdown. If the head is a parent of a device on the async list, all active async shutdown operations are completed before the parent's shutdown call is performed.The number of async operations also has a max limit to prevent the list being checked for a child from getting overly large.
Signed-off-by David Jeffery <[email protected]>Tested-by Laurence Oberman <[email protected]>--- drivers/base/core.c | 116 +++++++++++++++++++++++++++++++++- include/linux/device/bus.h | 8 ++- include/linux/device/driver.h | 7 ++ 3 files changed, 127 insertions(+), 4 deletions(-)drivers/base/core.c +38
-0@@ -4719,12 +4719,92 @@ int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) }EXPORT_SYMBOL_GPL (device_change_owner); + +#define MAX_ASYNC_SHUTDOWNS 32 +static int async_shutdown_count; +static LIST_HEAD (async_shutdown_list); + +/** +* If a device has a child busy with an async shutdown or there are too many +* async shutdowns active, the device may not be shut down at this time. +*/ +static bool may_shutdown_device (struct device *dev) +{ +struct device *tmp; + +if (async_shutdown_count >=MAX_ASYNC_SHUTDOWNS ) +return false; + +list_for_each_entry (tmp, &async_shutdown_list, kobj.entry) { +if (tmp->parent == dev) +return false; + } +return true; +} + +/** +* Call and track each async shutdown call +*/ +static void async_shutdown_start (struct device *dev,void (*callback) (struct device *)) +{ +if (initcall_debug) +dev_info (dev,"async_shutdown_start\n" ); + + (*callback)(dev); +list_add_tail (&dev->kobj.entry, &async_shutdown_list); + async_shutdown_count++; +} + +/** +* Wait for all async shutdown operations currently active to complete
drivers/base/core.c int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) · line 4769
Quoted 10 lines
drivers/base/core.c +10
-0+*/ +static void wait_for_active_async_shutdown (void ) +{ +struct device *dev, *parent; + +while (!list_empty (&async_shutdown_list)) { + dev = list_entry(async_shutdown_list.next, struct device, + kobj.entry); + + parent = dev->parent;
I didn't check the code thoroughly, but so there might be other big issues. But you definitely need to take device links into account. Shutdown all your consumers first similar to how you shutdown the children devices first. Look at the async suspend/resume code for some guidance.
Quoted 158 lines
drivers/base/core.c +65
-3+ +/* +* Make sure the device is off the list +*/ +list_del_init (&dev->kobj.entry); +if (parent) +device_lock (parent); +device_lock (dev); +if (dev->bus && dev->bus->async_shutdown_end) { +if (initcall_debug) +dev_info (dev, +"async_shutdown_end called\n" ); + dev->bus->async_shutdown_end (dev); + }else if (dev->driver && dev->driver->async_shutdown_end) { +if (initcall_debug) +dev_info (dev, +"async_shutdown_end called\n" ); + dev->driver->async_shutdown_end (dev); + } +device_unlock (dev); +if (parent) +device_unlock (parent); + +put_device (dev); +put_device (parent); + } +if (initcall_debug) +printk (KERN_INFO "device shutdown: waited for %d async shutdown callbacks\n" , async_shutdown_count); + async_shutdown_count =0 ; +} +/** * device_shutdown - call ->shutdown() on each device to shutdown. */ void device_shutdown (void ) {struct device *dev, *parent; +bool async_busy;wait_for_device_probe ();device_block_probing ();@@ -4741,6 +4821,8 @@ void device_shutdown(void) dev = list_entry(devices_kset->list.prev, struct device, kobj.entry); + async_busy = false; +/* * hold reference count of device's parent to * prevent it from being freed because parent's @@ -4748,6 +4830,17 @@ void device_shutdown(void)*/ parent =get_device (dev->parent);get_device (dev); + +if (!may_shutdown_device (dev)) { +put_device (dev); +put_device (parent); + +spin_unlock (&devices_kset->list_lock); +wait_for_active_async_shutdown (); +spin_lock (&devices_kset->list_lock); +continue ; + } +/* * Make sure the device is off the kset list, in the * event that dev->*->shutdown() doesn't remove it. @@ -4769,26 +4862,43 @@ void device_shutdown(void)dev_info (dev,"shutdown_pre\n" ); dev->class->shutdown_pre (dev); } -if (dev->bus && dev->bus->shutdown) { +if (dev->bus && dev->bus->async_shutdown_start) { +async_shutdown_start (dev, dev->bus->async_shutdown_start); + async_busy = true; + }else if (dev->bus && dev->bus->shutdown) {if (initcall_debug)dev_info (dev,"shutdown\n" ); dev->bus->shutdown (dev); + }else if (dev->driver && dev->driver->async_shutdown_start) { +async_shutdown_start (dev, dev->driver->async_shutdown_start); + async_busy = true; }else if (dev->driver && dev->driver->shutdown) {if (initcall_debug)dev_info (dev,"shutdown\n" ); dev->driver->shutdown (dev); + }else { +if (initcall_debug) +dev_info (dev,"no shutdown callback\n" ); }device_unlock (dev);if (parent)device_unlock (parent); -put_device (dev); -put_device (parent); +/* if device has an async shutdown, drop the ref when done */ +if (!async_busy) { +put_device (dev); +put_device (parent); + }spin_lock (&devices_kset->list_lock); }spin_unlock (&devices_kset->list_lock); +/* +* Wait for any async shutdown still running. +*/ +if (!list_empty (&async_shutdown_list)) +wait_for_active_async_shutdown (); }/* include/linux/device/bus.h +7
-1@@ -48,7 +48,11 @@ struct fwnode_handle;* will never get called until they do. * @remove: Called when a device removed from this bus. * @shutdown: Called at shut-down time to quiesce the device. -* +* @async_shutdown_start: Optional call to support and begin the shutdown +* process on the device in an asynchronous manner. +* @async_shutdown_end: Optional call to complete an asynchronous +* shutdown of the device. Must be provided if a +* sync_shutdown_start call is provided. * @online: Called to put the device back online (after offlining it). * @offline: Called to put the device offline for hot-removal. May fail. * @@ -87,6 +91,8 @@ struct bus_type {void (*sync_state)(struct device *dev);void (*remove)(struct device *dev);void (*shutdown)(struct device *dev); +void (*async_shutdown_start)(struct device *dev); +void (*async_shutdown_end)(struct device *dev);int (*online)(struct device *dev);int (*offline)(struct device *dev);include/linux/device/driver.h +5
-0@@ -71,6 +71,11 @@ enum probe_type {* @remove: Called when the device is removed from the system to * unbind a device from this driver. * @shutdown: Called at shut-down time to quiesce the device. +* @async_shutdown_start: Optional call to support and begin the shutdown +* process on the device in an asynchronous manner. +* @async_shutdown_end: Optional call to complete an asynchronous +* shutdown of the device. Must be provided if a +* sync_shutdown_start call is provided. * @suspend: Called to put the device to sleep mode. Usually to a * low power state. * @resume: Called to bring a device from sleep mode.
include/linux/device/driver.h struct device_driver { · line 119
Quoted 5 lines
include/linux/device/driver.h +2
-0@@ -110,6 +115,8 @@ struct device_driver {void (*sync_state)(struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev); +void (*async_shutdown_start) (struct device *dev); +void (*async_shutdown_end) (struct device *dev);
Why not use the existing shutdown and call it from an async thread and wait for it to finish? Similar to how async probes are handled. Also, adding separate ops for this feels clunky and a very narrow fix. Just use a flag to indicate the driver can support async shutdown using the existing shutdown() op.
-Saravana
Re: [RFC PATCH 1/6] minimal async shutdown infrastructure
Quoted 12 lines · Saravana Kannan
Quoted 10 lines · David Jeffery
+ * Wait for all async shutdown operations currently active to complete
+ */drivers/base/core.c +7
-0+static void wait_for_active_async_shutdown (void ) +{ +struct device *dev, *parent; + +while (!list_empty (&async_shutdown_list)) { + dev = list_entry(async_shutdown_list.next, struct device, + kobj.entry);
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + parent = dev->parent;I didn't check the code thoroughly, but so there might be other big issues. But you definitely need to take device links into account. Shutdown all your consumers first similar to how you shutdown the children devices first. Look at the async suspend/resume code for some guidance.
Sure, I'll work on adding that into the order rules.
Quoted 4 lines
Quoted 4 lines
include/linux/device/driver.h +0
-0@@ -110,6 +115,8 @@ struct device_driver {void (*sync_state)(struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev);
Quoted 9 lines
include/linux/device/driver.h struct device_driver { · line 119
Quoted 2 lines
include/linux/device/driver.h +2
-0+void (*async_shutdown_start) (struct device *dev); +void (*async_shutdown_end) (struct device *dev);Why not use the existing shutdown and call it from an async thread and wait for it to finish? Similar to how async probes are handled. Also, adding separate ops for this feels clunky and a very narrow fix. Just use a flag to indicate the driver can support async shutdown using the existing
shutdown()op.
It is rather clunky. It was carried from older patches where I mistakenly thought people wanted this separate interface. And adding threads seemed like overkill. Others have been working on similar patches on linux-nvme that I was unaware of. They add an optional shutdown_wait call instead of this interface. I had planned on adapting to work with their interface design.
David Jeffery
RFC PATCH 2/6 Improve ability to perform async shutdown in parallel
In reply to David JefferyExpands the async shutdown implementation to allow more cases of parallel async shutdown. A field is added so that a device under async shutdown can mark its parent as busy due to the async shutdown. A busy parent on reaching the head of the shutdown list gets stored and flags its own parent as busy.
Once the async shutdown operations are completed, the stored parents are returned to the shutdown list and shut down in an order maintaining their parent-child ordering. Unlike the minimal implementation, this allows more end nodes of the device tree to be under async shutdown in parallel.
A cap on the number of async shutdown devices is still enforced, though not required and could be removed if desired.
--- drivers/base/base.h | 1 + drivers/base/core.c | 71 ++++++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 20 deletions(-)
drivers/base/base.h +1 -0
@@ -116,6 +116,7 @@ struct device_private {char *deferred_probe_reason;struct device *device; u8 dead:1; + u8 child_shutdown:1; };#define to_device_private_parent (obj) \ container_of(obj, struct device_private, knode_parent)
drivers/base/core.c +51 -20
150 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 3/6 pci bus async shutdown support
In reply to David JefferyAdd async shutdown shutdown fields and Convert pci's shutdown logic into async shutdown calls so that individual pci device drivers can implement async shutdown.
--- drivers/pci/pci-driver.c | 24 ++++++++++++++++++++++-- include/linux/pci.h | 4 ++++ 2 files changed, 26 insertions(+), 2 deletions(-)
drivers/pci/pci-driver.c +22 -2
@@ -502,16 +502,28 @@ static void pci_device_remove(struct device *dev)pci_dev_put (pci_dev); } -static void pci_device_shutdown (struct device *dev) +static void pci_device_async_shutdown_start (struct device *dev) {struct pci_dev *pci_dev =to_pci_dev (dev);struct pci_driver *drv = pci_dev->driver;pm_runtime_resume (dev); -if (drv && drv->shutdown) +if (drv && drv->async_shutdown_start) + drv->async_shutdown_start (pci_dev); +else if (drv && drv->shutdown) drv->shutdown (pci_dev); +} + +static void pci_device_async_shutdown_end (struct device *dev) +{ +struct pci_dev *pci_dev =to_pci_dev (dev); +struct pci_driver *drv = pci_dev->driver; + +if (drv && drv->async_shutdown_end) + drv->async_shutdown_end (pci_dev); +/* * If this is a kexec reboot, turn off Bus Master bit on the * device to tell it to not continue to do DMA. Don't touch
@@ -523,6 +535,12 @@ static void pci_device_shutdown(struct device *dev)pci_clear_master (pci_dev); } +static void pci_device_shutdown (struct device *dev) +{ +pci_device_async_shutdown_start (dev); +pci_device_async_shutdown_end (dev); +} +#ifdef CONFIG_PM_SLEEP /* Auxiliary functions used for system resume */
@@ -1682,6 +1700,8 @@ struct bus_type pci_bus_type = { .probe = pci_device_probe, .remove = pci_device_remove, .shutdown = pci_device_shutdown, + .async_shutdown_start = pci_device_async_shutdown_start, + .async_shutdown_end = pci_device_async_shutdown_end, .dev_groups = pci_dev_groups, .bus_groups = pci_bus_groups, .drv_groups = pci_drv_groups,
include/linux/pci.h +4 -0
@@ -917,6 +917,8 @@ struct module;* Useful for enabling wake-on-lan (NIC) or changing * the power state of a device before reboot. * e.g. drivers/net/e100.c. +* @async_shutdown_start: This starts the asynchronous shutdown +* @async_shutdown_end: This completes the started asynchronous shutdown * @sriov_configure: Optional driver callback to allow configuration of * number of VFs to enable via sysfs "sriov_numvfs" file. * @sriov_set_msix_vec_count: PF Driver callback to change number of MSI-X
@@ -947,6 +949,8 @@ struct pci_driver {int (*suspend)(struct pci_dev *dev,pm_message_t state);/* Device suspended */ int (*resume)(struct pci_dev *dev);/* Device woken up */ void (*shutdown)(struct pci_dev *dev); +void (*async_shutdown_start)(struct pci_dev *dev); +void (*async_shutdown_end)(struct pci_dev *dev);int (*sriov_configure)(struct pci_dev *dev,int num_vfs);/* On PF */ int (*sriov_set_msix_vec_count)(struct pci_dev *vf,int msix_vec_count);/* On PF */ u32 (*sriov_get_vf_total_msix)(struct pci_dev *pf);
Signature · 1 line
2.43.0
RFC PATCH 4/6 pci nvme async shutdown support
In reply to David JefferyAlter nvme so that the pci nvme driver may shut down the hardware asynchronously, providing a large benefit to shutdown time with large numbers of nvme devices in some configurations.
The functionality of nvme_disable_ctrl is split in separate functions for starting disabling the hardware and for waiting for the hardware to report success. A wrapper is created which provides the same interface for the other nvme types so they remain unchanged.
And new field is added to nvme-pci's nvme_dev to track the nvme shutdown state so that the nvme async_shutdown_end call knows what if anything it still needs to do.
--- drivers/nvme/host/core.c | 26 +++++++++++++++----- drivers/nvme/host/nvme.h | 2 ++ drivers/nvme/host/pci.c | 53 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 8 deletions(-)
drivers/nvme/host/core.c +20 -6
@@ -2242,20 +2242,21 @@ static int nvme_wait_ready(struct nvme_ctrl *ctrl, u32 mask, u32 val,return ret; } -int nvme_disable_ctrl (struct nvme_ctrl *ctrl,bool shutdown) +int nvme_disable_ctrl_send (struct nvme_ctrl *ctrl,bool shutdown) { -int ret; - ctrl->ctrl_config &= ~NVME_CC_SHN_MASK ;if (shutdown) ctrl->ctrl_config |=NVME_CC_SHN_NORMAL ;else ctrl->ctrl_config &= ~NVME_CC_ENABLE ; - ret = ctrl->ops->reg_write32 (ctrl,NVME_REG_CC , ctrl->ctrl_config); -if (ret) -return ret; +return ctrl->ops->reg_write32 (ctrl,NVME_REG_CC , ctrl->ctrl_config); +} +EXPORT_SYMBOL_GPL (nvme_disable_ctrl_send); + +int nvme_disable_ctrl_wait (struct nvme_ctrl *ctrl,bool shutdown) +{if (shutdown) { return nvme_wait_ready(ctrl, NVME_CSTS_SHST_MASK, NVME_CSTS_SHST_CMPLT,
@@ -2266,6 +2267,19 @@ int nvme_disable_ctrl(struct nvme_ctrl *ctrl, bool shutdown)return nvme_wait_ready (ctrl,NVME_CSTS_RDY ,0 , (NVME_CAP_TIMEOUT (ctrl->cap) +1 ) /2 ,"reset" ); } +EXPORT_SYMBOL_GPL (nvme_disable_ctrl_wait); + + +int nvme_disable_ctrl (struct nvme_ctrl *ctrl,bool shutdown) +{ +int ret; + + ret =nvme_disable_ctrl_send (ctrl, shutdown); +if (!ret) + ret =nvme_disable_ctrl_wait (ctrl, shutdown); + +return ret; +}EXPORT_SYMBOL_GPL (nvme_disable_ctrl);int nvme_enable_ctrl (struct nvme_ctrl *ctrl)
drivers/nvme/host/nvme.h +2 -0
@@ -761,6 +761,8 @@ void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl);bool nvme_change_ctrl_state (struct nvme_ctrl *ctrl,enum nvme_ctrl_state new_state);int nvme_disable_ctrl (struct nvme_ctrl *ctrl,bool shutdown); +int nvme_disable_ctrl_send (struct nvme_ctrl *ctrl,bool shutdown); +int nvme_disable_ctrl_wait (struct nvme_ctrl *ctrl,bool shutdown);int nvme_enable_ctrl (struct nvme_ctrl *ctrl);int nvme_init_ctrl (struct nvme_ctrl *ctrl,struct device *dev,const struct nvme_ctrl_ops *ops,unsigned long quirks);
drivers/nvme/host/pci.c +51 -2
100 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 5/6 scsi mid layer support for async command submit
In reply to David JefferyCreate scsi_execute_cmd_nowait to allow asynchronous scsi command submit. Parts of the code originally in scsi_execute_cmd are shifted into helper functions used by both scsi_execute_cmd and the new scsi_execute_cmd_nowait.
The scsi_exec_args struct is expanded to contain the fields needed for the completion and callback for the async path.
--- drivers/scsi/scsi_lib.c | 138 +++++++++++++++++++++++++++++-------- include/scsi/scsi_device.h | 8 +++ 2 files changed, 118 insertions(+), 28 deletions(-)
drivers/scsi/scsi_lib.c +110 -28
@@ -185,42 +185,37 @@ void scsi_queue_insert(struct scsi_cmnd *cmd, int reason) }/** -* scsi_execute_cmd - insert request and wait for the result +* scsi_execute_init - helper for setting up a scsi_cmnd in a request * @sdev: scsi_device * @cmd: scsi command * @opf: block layer request cmd_flags -* @buffer: data buffer -* @bufflen: len of buffer * @timeout: request timeout in HZ * @retries: number of times to retry request -* @args: Optional args. See struct definition for field descriptions +* @args: scsi command args * -* Returns the scsi_cmnd result field if a command was executed, or a negative -* Linux error code if we didn't get that far. +* Returns a request if successful, or an error pointer if there was a failure. */ -int scsi_execute_cmd (struct scsi_device *sdev,const unsigned char *cmd, -blk_opf_t opf,void *buffer,unsigned int bufflen, -int timeout,int retries, -const struct scsi_exec_args *args) +static struct request *scsi_execute_init (struct scsi_device *sdev, +const unsigned char *cmd, +blk_opf_t opf, +int timeout,int retries, +struct scsi_exec_args *args) { -static const struct scsi_exec_args default_args;struct request *req;struct scsi_cmnd *scmd;int ret; -if (!args) - args = &default_args; -else if (WARN_ON_ONCE (args->sense && - args->sense_len !=SCSI_SENSE_BUFFERSIZE )) -return -EINVAL ; +if (WARN_ON_ONCE (args->sense && + args->sense_len !=SCSI_SENSE_BUFFERSIZE )) +return ERR_PTR (-EINVAL ); req =scsi_alloc_request (sdev->request_queue, opf, args->req_flags);if (IS_ERR (req)) -return PTR_ERR (req); +return req; -if (bufflen) { - ret =blk_rq_map_kern (sdev->request_queue, req, - buffer, bufflen,GFP_NOIO ); +if (args->bufflen) { + ret =blk_rq_map_kern (sdev->request_queue, req, args->buffer, + args->bufflen,GFP_NOIO );if (ret) goto out; }
@@ -232,19 +227,27 @@ int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd, req->timeout = timeout; req->rq_flags |=RQF_QUIET ; -/* -* head injection *required* here otherwise quiesce won't work -*/ -blk_execute_rq (req, true); +return req; +out: +blk_mq_free_request (req); +return ERR_PTR (ret); +} + +static int scsi_execute_uninit (struct request *req, +struct scsi_exec_args *args) +{ +struct scsi_cmnd *scmd; + scmd =blk_mq_rq_to_pdu (req);/* * Some devices (USB mass-storage in particular) may transfer * garbage data together with a residue indicating that the data * is invalid. Prevent the garbage from being misinterpreted * and prevent security leaks by zeroing out the excess data. */ -if (unlikely (scmd->resid_len >0 && scmd->resid_len <= bufflen)) -memset (buffer + bufflen - scmd->resid_len,0 , scmd->resid_len); +if (unlikely (scmd->resid_len >0 && scmd->resid_len <= args->bufflen)) +memset (args->buffer + args->bufflen - scmd->resid_len,0 , + scmd->resid_len);if (args->resid) *args->resid = scmd->resid_len;
@@ -254,14 +257,93 @@ int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,scsi_normalize_sense (scmd->sense_buffer, scmd->sense_len, args->sshdr); - ret = scmd->result; - out: + args->result = scmd->result; + +if (args->callback) + args->callback (scmd, args); + +return scmd->result; +} + +/** +* scsi_execute_cmd - insert request and wait for the result +* @sdev: scsi_device +* @cmd: scsi command +* @opf: block layer request cmd_flags +* @buffer: data buffer +* @bufflen: len of buffer +* @timeout: request timeout in HZ +* @retries: number of times to retry request +* @args: Optional args. See struct definition for field descriptions +* +* Returns the scsi_cmnd result field if a command was executed, or a negative +* Linux error code if we didn't get that far. +*/ +int scsi_execute_cmd (struct scsi_device *sdev,const unsigned char *cmd, +blk_opf_t opf,void *buffer,unsigned int bufflen, +int timeout,int retries, +const struct scsi_exec_args *const_args) +{ +struct scsi_exec_args args; +int ret; +struct request *req; + +if (!const_args) +memset (&args,0 ,sizeof (struct scsi_exec_args )); +else + args = *const_args; + + args.buffer = buffer; + args.bufflen = bufflen; + + req =scsi_execute_init (sdev, cmd, opf, timeout, retries, &args); + +if (IS_ERR (req)) +return PTR_ERR (req); + +/* +* head injection *required* here otherwise quiesce won't work +*/ +blk_execute_rq (req, true); + + ret =scsi_execute_uninit (req, &args); +blk_mq_free_request (req);return ret; }EXPORT_SYMBOL (scsi_execute_cmd); + +static enum rq_end_io_ret scsi_execute_cmd_complete (struct request *req, +blk_status_t ret) +{ +struct scsi_exec_args *args = req->end_io_data; + +scsi_execute_uninit (req, args); +return RQ_END_IO_FREE ; +} + +int scsi_execute_cmd_nowait (struct scsi_device *sdev,const unsigned char *cmd, +blk_opf_t opf,int timeout,int retries, +struct scsi_exec_args *args) +{ +struct request *req; + + req =scsi_execute_init (sdev, cmd, opf, timeout, retries, args); + +if (IS_ERR (req)) +return PTR_ERR (req); + + req->end_io = scsi_execute_cmd_complete; + req->end_io_data = args; + +blk_execute_rq_nowait (req, true); + +return 0 ; +} +EXPORT_SYMBOL (scsi_execute_cmd_nowait); +/* * Wake up the error handler if necessary. Avoid as follows that the error * handler is not woken up if host in-flight requests number ==
include/scsi/scsi_device.h +8 -0
@@ -497,6 +497,10 @@ struct scsi_exec_args {blk_mq_req_flags_t req_flags;/* BLK_MQ_REQ flags */ int scmd_flags;/* SCMD flags */ int *resid;/* residual length */ +void *buffer;/* data buffer */ +unsigned int bufflen;/* buffer length */ +int result;/* scsi layer result */ +void (*callback)(struct scsi_cmnd *scmd,struct scsi_exec_args *args); }; int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,
@@ -504,6 +508,10 @@ int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd, int timeout, int retries, const struct scsi_exec_args *args); +int scsi_execute_cmd_nowait (struct scsi_device *sdev,const unsigned char *cmd, +blk_opf_t opf,int timeout,int retries, +struct scsi_exec_args *args); +extern void sdev_disable_disk_events (struct scsi_device *sdev);extern void sdev_enable_disk_events (struct scsi_device *sdev);extern int scsi_vpd_lun_id (struct scsi_device *,char *,size_t );
Signature · 1 line
2.43.0
kernel test robot
build botRe: [RFC PATCH 5/6] scsi mid layer support for async command submit
kernel test robot noticed the following build warnings:
Hi David,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on driver-core/driver-core-testing]
[also build test WARNING on driver-core/driver-core-next driver-core/driver-core-linus linus/master v6.8-rc3] [cannot apply to mkp-scsi/for-next jejb-scsi/for-next next-20240209]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/David-Jeffery/minimal-async-shutdown-infrastructure/20240208-024535 base: driver-core/driver-core-testing patch link: https://lore.kernel.org/r/20240207184100.18066-6-djeffery%40redhat.com patch subject: [RFC PATCH 5/6] scsi mid layer support for async command submit config: alpha-defconfig (https://download.01.org/0day-ci/archive/20240209/[email protected]/config) compiler: alpha-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240209/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/scsi/scsi_lib.c:286: warning: Function parameter or struct member 'const_args' not described in 'scsi_execute_cmd' >> drivers/scsi/scsi_lib.c:286: warning: Excess function parameter 'args' description in 'scsi_execute_cmd'
vim +286 drivers/scsi/scsi_lib.c
267
268 /**
269 * scsi_execute_cmd - insert request and wait for the result
270 * @sdev: scsi_device
271 * @cmd: scsi command
272 * @opf: block layer request cmd_flags
273 * @buffer: data buffer
274 * @bufflen: len of buffer
275 * @timeout: request timeout in HZ
276 * @retries: number of times to retry request
277 * @args: Optional args. See struct definition for field descriptions
278 *
279 * Returns the scsi_cmnd result field if a command was executed, or a negative
280 * Linux error code if we didn't get that far.
281 */
282 int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,
283 blk_opf_t opf, void *buffer, unsigned int bufflen,
284 int timeout, int retries,
285 const struct scsi_exec_args *const_args)
> 286 { 287 struct scsi_exec_args args;
288 int ret;
289 struct request *req;
290
291 if (!const_args)
292 memset(&args, 0, sizeof(struct scsi_exec_args));
293 else
294 args = *const_args;
295
296 args.buffer = buffer;
297 args.bufflen = bufflen;
298
299 req = scsi_execute_init(sdev, cmd, opf, timeout, retries, &args);
300
301 if (IS_ERR(req))
302 return PTR_ERR(req);
303
304 /*
305 * head injection *required* here otherwise quiesce won't work
306 */
307 blk_execute_rq(req, true);
308
309 ret = scsi_execute_uninit(req, &args);
310
311 blk_mq_free_request(req);
312
313 return ret;
314 }
315 EXPORT_SYMBOL(scsi_execute_cmd);
316
317
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH 6/6 sd: async cache flush on shutdown
In reply to David JefferyAdd 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.
--- drivers/scsi/sd.c | 66 ++++++++++++++++++++++++++++++++++++++++++----- drivers/scsi/sd.h | 2 ++ 2 files changed, 62 insertions(+), 6 deletions(-)
drivers/scsi/sd.c +60 -6
@@ -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,
drivers/scsi/sd.h +2 -0
@@ -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;
Signature · 1 line
2.43.0