shut down devices asynchronously
Subsystems (5)
v15 cover shut down devices asynchronously
This patchset allows the kernel to shutdown devices asynchronously and unrelated async devices to be shut down in parallel to each other.
Only devices which explicitly enable it are shut down asynchronously. The default is for a device to be shut down from the synchronous shutdown loop.
This can dramatically reduce system shutdown/reboot time on systems that have multiple devices that take many seconds to shut down (like certain NVMe drives). On one system tested, the shutdown time went from 11 minutes without this patch to 55 seconds with the patch. And on another system from 80 seconds to 11.
And thank you to everyone who has spent some of their valuable time providing reviews, suggestions, criticisms, or tests on the various iterations of this patchset.
Changes from V14:
Remove unneeded use of '!!' with boolean type
Changes from V13:
Remove duplicate flagging of async shutdown on scsi hosts/targets/devices
Changes from V12:
Only acquire a parent reference if acquiring the parent's lock device_enable_async_shutdown should return void Minor comment and description cleanups
Changes from V11:
* Swap the order of the first two patches
* Rework conditional parent locking so that lock and unlock no longer use separate conditional checks
* Remove an used variable
* Comment and description text cleanups
Changes from V10:
Reworked to more closely match the design used for async suspend
* No longer uses async subsystem cookies for synchronization
* Minimized changes to struct device
* Enable async shutdown for pci and scsi devices which support async suspend
Changes from V9:
Address resource and timing issues when spawning a unique async thread for every device during shutdown:
* Make the asynchronous threads able to shut down multiple devices, instead of spawning a unique thread for every device.
* Modify core kernel async code with a custom wake function so it doesn't wake up a thread waiting to synchronize on a cookie until the cookie has reached the desired value, instead of waking up every waiting thread to check the cookie every time an async thread ends.
Changes from V8:
Deal with shutdown hangs resulting when a parent/supplier device is
later in the devices_kset list than its children/consumers:
* Ignore sync_state_only devlinks for shutdown dependencies
* Ignore shutdown_after for devices that don't want async shutdown
* Add a sanity check to revert to sync shutdown for any device that would otherwise wait for a child/consumer shutdown that hasn't already been scheduled
Changes from V7:
Do not expose driver async_shutdown_enable in sysfs. Wrapped a long line.
Changes from V6:
Removed a sysfs attribute that allowed the async device shutdown to be "on" (with driver opt-out), "safe" (driver opt-in), or "off"... what was previously "safe" is now the only behavior, so drivers now only need to have the option to enable or disable async shutdown.
Changes from V5:
Separated into multiple patches to make review easier. Reworked some code to make it more readable
Made devices wait for consumers to shut down, not just children
(suggested by David Jeffery)
Changes from V4:
Change code to use cookies for synchronization rather than async domains Allow async shutdown to be disabled via sysfs, and allow driver opt-in or
opt-out of async shutdown (when not disabled), with ability to control driver opt-in/opt-out via sysfs
Changes from V3:
Bug fix (used "parent" not "dev->parent" in device_shutdown)
Changes from V2:
Removed recursive functions to schedule children to be shutdown before
parents, since existing device_shutdown loop will already do this
Changes from V1:
Rewritten using kernel async code (suggested by Lukas Wunner)
Stuart Hayes (2):
driver core: separate function to shutdown one device driver core: do not always lock parent in shutdown
David Jeffery (3):
driver core: async device shutdown infrastructure
PCI: Enable async shutdown support
scsi: Enable async shutdown support
drivers/base/base.h | 2 + drivers/base/core.c | 180 ++++++++++++++++++++++++++++++-------- drivers/pci/probe.c | 2 + drivers/scsi/hosts.c | 2 + drivers/scsi/scsi_sysfs.c | 3 + include/linux/device.h | 14 ++- 6 files changed, 166 insertions(+), 37 deletions(-)
Signature · 1 line
2.53.0
PATCH v15 1/5 driver core: separate function to shutdown one device
Make a separate function for the part of device_shutdown() that does the shutown for a single device. This is in preparation for making device shutdown asynchronous.
--- drivers/base/core.c | 71 +++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 32 deletions(-)
drivers/base/core.c +39 -32
@@ -4799,12 +4799,48 @@ int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)return error; } +static void shutdown_one_device (struct device *dev) +{ +struct device *parent = dev->parent; + +/* hold lock to avoid race with probe/release */ +if (parent) +device_lock (parent); +device_lock (dev); + +/* Don't allow any more runtime suspends */ +pm_runtime_get_noresume (dev); +pm_runtime_barrier (dev); + +if (dev->class && dev->class->shutdown_pre) { +if (initcall_debug) +dev_info (dev,"shutdown_pre\n" ); + dev->class->shutdown_pre (dev); + } +if (dev->bus && dev->bus->shutdown) { +if (initcall_debug) +dev_info (dev,"shutdown\n" ); + dev->bus->shutdown (dev); + }else if (dev->driver && dev->driver->shutdown) { +if (initcall_debug) +dev_info (dev,"shutdown\n" ); + dev->driver->shutdown (dev); + } + +device_unlock (dev); +if (parent) +device_unlock (parent); + +put_device (parent); +put_device (dev); +} +/** * device_shutdown - call ->shutdown() on each device to shutdown. */ void device_shutdown (void ) { -struct device *dev, *parent; +struct device *dev;wait_for_device_probe ();device_block_probing ();
@@ -4826,7 +4862,7 @@ void device_shutdown(void)* prevent it from being freed because parent's * lock is to be held */ - parent =get_device (dev->parent); +get_device (dev->parent);get_device (dev);/* * Make sure the device is off the kset list, in the
@@ -4835,36 +4871,7 @@ void device_shutdown(void)list_del_init (&dev->kobj.entry);spin_unlock (&devices_kset->list_lock); -/* hold lock to avoid race with probe/release */ -if (parent) -device_lock (parent); -device_lock (dev); - -/* Don't allow any more runtime suspends */ -pm_runtime_get_noresume (dev); -pm_runtime_barrier (dev); - -if (dev->class && dev->class->shutdown_pre) { -if (initcall_debug) -dev_info (dev,"shutdown_pre\n" ); - dev->class->shutdown_pre (dev); - } -if (dev->bus && dev->bus->shutdown) { -if (initcall_debug) -dev_info (dev,"shutdown\n" ); - dev->bus->shutdown (dev); - }else if (dev->driver && dev->driver->shutdown) { -if (initcall_debug) -dev_info (dev,"shutdown\n" ); - dev->driver->shutdown (dev); - } - -device_unlock (dev); -if (parent) -device_unlock (parent); - -put_device (dev); -put_device (parent); +shutdown_one_device (dev);spin_lock (&devices_kset->list_lock); }
Signature · 1 line
2.53.0
PATCH v15 2/5 driver core: do not always lock parent in shutdown
In reply to David JefferyDon't lock a parent device unless it is needed in device_shutdown. This is in preparation for making device shutdown asynchronous, when it will be needed to allow children of a common parent to shut down simultaneously.
And only acquire a reference to the parent device if the parent is to be locked.
--- drivers/base/core.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
drivers/base/core.c +16 -14
@@ -4799,13 +4799,8 @@ int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)return error; } -static void shutdown_one_device (struct device *dev) +static void __shutdown_one_device (struct device *dev) { -struct device *parent = dev->parent; - -/* hold lock to avoid race with probe/release */ -if (parent) -device_lock (parent);device_lock (dev);/* Don't allow any more runtime suspends */
@@ -4828,10 +4823,23 @@ static void shutdown_one_device(struct device *dev) }device_unlock (dev); -if (parent) +} + +static void shutdown_one_device (struct device *dev) +{ +struct device *parent; + +/* hold lock if needed to avoid race with probe/release */ +if (dev->bus && dev->bus->need_parent_lock && + (parent =get_device (dev->parent))) { +device_lock (parent); +__shutdown_one_device (dev);device_unlock (parent); +put_device (parent); + }else { +__shutdown_one_device (dev); + } -put_device (parent);put_device (dev); }
@@ -4857,12 +4865,6 @@ void device_shutdown(void) dev = list_entry(devices_kset->list.prev, struct device, kobj.entry); -/* -* hold reference count of device's parent to -* prevent it from being freed because parent's -* lock is to be held -*/ -get_device (dev->parent);get_device (dev);/* * Make sure the device is off the kset list, in the
Signature · 1 line
2.53.0
PATCH v15 3/5 driver core: async device shutdown infrastructure
In reply to David JefferyPatterned after async suspend, allow devices to mark themselves as wanting to perform async shutdown. Devices using async shutdown wait only for their dependencies to shutdown before executing their shutdown routine.
Sync shutdown devices are shut down one at a time and will only wait for an async shutdown device if the async device is a dependency.
--- drivers/base/base.h | 2 + drivers/base/core.c | 101 ++++++++++++++++++++++++++++++++++++++++- include/linux/device.h | 14 +++++- 3 files changed, 115 insertions(+), 2 deletions(-)
drivers/base/base.h +2 -0
@@ -116,6 +116,7 @@ struct driver_type {* @device: pointer back to the struct device that this structure is * associated with. * @driver_type: The type of the bound Rust driver. +* @complete: completion for device shutdown ordering * @dead: This device is currently either in the process of or has been * removed from the system. Any asynchronous events scheduled for this * device should exit without taking any action.
@@ -135,6 +136,7 @@ struct device_private {#ifdef CONFIG_RUST struct driver_type driver_type;#endif +struct completion complete; u8 dead:1; };#define to_device_private_parent (obj) \
drivers/base/core.c +100 -1
153 lines not on this page · raw message
include/linux/device.h +13 -1
@@ -615,6 +615,8 @@ enum struct_device_flags {* @dma_skip_sync: DMA sync operations can be skipped for coherent buffers. * @dma_iommu: Device is using default IOMMU implementation for DMA and * doesn't rely on dma_ops structure. +* @async_shutdown: Device shutdown may be run asynchronously and in parallel +* to the shutdown of unrelated devices * @flags: DEV_FLAG_XXX flags. Use atomic bitfield operations to modify. * * At the lowest level, every device in a Linux system is represented by an
@@ -738,7 +740,7 @@ struct device {#ifdef CONFIG_IOMMU_DMA bool dma_iommu:1;#endif - + bool async_shutdown:1;DECLARE_BITMAP (flags,DEV_FLAG_COUNT ); };
@@ -969,6 +971,16 @@ static inline bool device_async_suspend_enabled(struct device *dev)return !!dev->power.async_suspend; } +static inline void device_enable_async_shutdown (struct device *dev) +{ + dev->async_shutdown = true; +} + +static inline bool device_async_shutdown_enabled (struct device *dev) +{ +return dev->async_shutdown; +} +static inline bool device_pm_not_required (struct device *dev) {return dev->power.no_pm;
Signature · 1 line
2.53.0
PATCH v15 4/5 PCI: Enable async shutdown support
In reply to David JefferyLike its async suspend support, allow PCI device shutdown to be performed asynchronously to reduce shutdown time.
--- drivers/pci/probe.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/pci/probe.c +2 -0
@@ -1045,6 +1045,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) bus->bridge =get_device (&bridge->dev);device_enable_async_suspend (bus->bridge); +device_enable_async_shutdown (bus->bridge);pci_set_bus_of_node (bus);pci_set_bus_msi_domain (bus); if (bridge->msi_domain && !dev_get_msi_domain(&bus->dev) &&
@@ -2753,6 +2754,7 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)pci_reassigndev_resource_alignment (dev);pci_init_capabilities (dev); +device_enable_async_shutdown (&dev->dev);/* * Add the device to our list of discovered devices
Signature · 1 line
2.53.0
Re: [PATCH 4/5] PCI: Enable async shutdown support
Quoted 8 lines · David Jeffery
Like its async suspend support, allow PCI device shutdown to be performed asynchronously to reduce shutdown time.
Signed-off-by David Jeffery <[email protected]>Signed-off-by Stuart Hayes <[email protected]>Tested-by Laurence Oberman <[email protected]>Reviewed-by Martin K. Petersen <[email protected]>Reviewed-by Pasha Tatashin <[email protected]>
I'm concerned about tripping over driver issues, but it's a pretty big benefit. I think it's worth mentioning the "async_shutdown" module parameter somewhere in the commit logs and putting an example in Documentation/admin-guide/kernel-parameters.txt.
Might even consider keeping in -next for a cycle+ and targeting v7.3.
Quoted 27 lines
--- drivers/pci/probe.c | 2 ++ 1 file changed, 2 insertions(+)drivers/pci/probe.c +2
-0@@ -1045,6 +1045,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) bus->bridge =get_device (&bridge->dev);device_enable_async_suspend (bus->bridge); +device_enable_async_shutdown (bus->bridge);pci_set_bus_of_node (bus);pci_set_bus_msi_domain (bus); if (bridge->msi_domain && !dev_get_msi_domain(&bus->dev) &&@@ -2753,6 +2754,7 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)pci_reassigndev_resource_alignment (dev);pci_init_capabilities (dev); +device_enable_async_shutdown (&dev->dev);/* * Add the device to our list of discovered devices Signature · 1 line
2.53.0
Re: [PATCH 4/5] PCI: Enable async shutdown support
Quoted 11 lines · Bjorn Helgaas
Quoted 8 lines · David Jeffery
Like its async suspend support, allow PCI device shutdown to be performed asynchronously to reduce shutdown time.
Signed-off-by David Jeffery <[email protected]>Signed-off-by Stuart Hayes <[email protected]>Tested-by Laurence Oberman <[email protected]>Reviewed-by Martin K. Petersen <[email protected]>Reviewed-by Pasha Tatashin <[email protected]>
Quoted 6 lines
Reviewed-by Bjorn Helgaas <[email protected]>I'm concerned about tripping over driver issues, but it's a pretty big benefit. I think it's worth mentioning the "
async_shutdown" module parameter somewhere in the commit logs and putting an example inDocumentation/admin-guide/kernel-parameters.txt.
Sure, I can see about adding a document patch to it.
Quoted 1 line
Might even consider keeping in -next for a cycle+ and targeting v7.3.
I would be happy for it to spend as much time in linux-next as needed for people to be comfortable with the risk of odd interactions with all the pci drivers it will indirectly impact.
But I must admit my ignorance with some of the details of linux-next. Looking at the main code changes, the patchset would seem to want to go through driver-core to get to linux-next. But as far as indirect impact, it is the diverse collection of devices and drivers under PCI which are most affected by the changes. Is there a consensus on how to steward a patchset like this one into linux-next?
David Jeffery
Re: [PATCH 4/5] PCI: Enable async shutdown support
Quoted 26 lines · David Jeffery
Quoted 10 lines · Bjorn Helgaas
Quoted 8 lines · David Jeffery
Like its async suspend support, allow PCI device shutdown to be performed asynchronously to reduce shutdown time.
Signed-off-by David Jeffery <[email protected]>Signed-off-by Stuart Hayes <[email protected]>Tested-by Laurence Oberman <[email protected]>Reviewed-by Martin K. Petersen <[email protected]>Reviewed-by Pasha Tatashin <[email protected]>Quoted 6 lines
Reviewed-by Bjorn Helgaas <[email protected]>I'm concerned about tripping over driver issues, but it's a pretty big benefit. I think it's worth mentioning the "
async_shutdown" module parameter somewhere in the commit logs and putting an example inDocumentation/admin-guide/kernel-parameters.txt.Sure, I can see about adding a document patch to it.
Quoted 1 line
Might even consider keeping in -next for a cycle+ and targeting v7.3.
I would be happy for it to spend as much time in linux-next as needed for people to be comfortable with the risk of odd interactions with all the pci drivers it will indirectly impact.
Quoted 6 lines
But I must admit my ignorance with some of the details of linux-next. Looking at the main code changes, the patchset would seem to want to go through driver-core to get to linux-next. But as far as indirect impact, it is the diverse collection of devices and drivers under PCI which are most affected by the changes. Is there a consensus on how to steward a patchset like this one into linux-next?
My guess is it should all go via the driver-core tree, given acks or reviewed-by from the other subsystems it touches.
PATCH v15 5/5 scsi: Enable async shutdown support
In reply to David JefferyLike scsi's async suspend support, allow scsi devices to be shut down asynchronously to reduce system shutdown time.
--- drivers/scsi/hosts.c | 2 ++ drivers/scsi/scsi_sysfs.c | 3 +++ 2 files changed, 5 insertions(+)
drivers/scsi/hosts.c +2 -0
@@ -273,6 +273,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,pm_runtime_set_active (&shost->shost_gendev);pm_runtime_enable (&shost->shost_gendev);device_enable_async_suspend (&shost->shost_gendev); +device_enable_async_shutdown (&shost->shost_gendev); error =device_add (&shost->shost_gendev);if (error)
@@ -282,6 +283,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,get_device (shost->shost_gendev.parent);device_enable_async_suspend (&shost->shost_dev); +device_enable_async_shutdown (&shost->shost_dev);get_device (&shost->shost_gendev); error =device_add (&shost->shost_dev);
drivers/scsi/scsi_sysfs.c +3 -0
@@ -1386,6 +1386,7 @@ static int scsi_target_add(struct scsi_target *starget)pm_runtime_set_active (&starget->dev);pm_runtime_enable (&starget->dev);device_enable_async_suspend (&starget->dev); +device_enable_async_shutdown (&starget->dev);return 0 ; }
@@ -1412,6 +1413,7 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)transport_configure_device (&starget->dev);device_enable_async_suspend (&sdev->sdev_gendev); +device_enable_async_shutdown (&sdev->sdev_gendev);scsi_autopm_get_target (starget);pm_runtime_set_active (&sdev->sdev_gendev);if (!sdev->rpm_autosuspend)
@@ -1431,6 +1433,7 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev) }device_enable_async_suspend (&sdev->sdev_dev); +device_enable_async_shutdown (&sdev->sdev_dev); error =device_add (&sdev->sdev_dev);if (error) { sdev_printk(KERN_INFO, sdev,
Signature · 1 line
2.53.0
David Jeffery <[email protected]> writes:
FWIW,
I have tested this series by opting it for NVMe devices and observed significant performance benefit (from 9 sec to 2 sec).
Please feel free to use
Quoted 127 lines
This patchset allows the kernel to shutdown devices asynchronously and unrelated async devices to be shut down in parallel to each other.
Only devices which explicitly enable it are shut down asynchronously. The default is for a device to be shut down from the synchronous shutdown loop.
This can dramatically reduce system shutdown/reboot time on systems that have multiple devices that take many seconds to shut down (like certain NVMe drives). On one system tested, the shutdown time went from 11 minutes without this patch to 55 seconds with the patch. And on another system from 80 seconds to 11.
And thank you to everyone who has spent some of their valuable time providing reviews, suggestions, criticisms, or tests on the various iterations of this patchset.
Changes from V14:
Remove unneeded use of '!!' with boolean type
Changes from V13:
Remove duplicate flagging of async shutdown on scsi hosts/targets/devices
Changes from V12:
Only acquire a parent reference if acquiring the parent's lock
device_enable_async_shutdownshould return void Minor comment and description cleanupsChanges from V11:
* Swap the order of the first two patches
* Rework conditional parent locking so that lock and unlock no longer use separate conditional checks
* Remove an used variable
* Comment and description text cleanupsChanges from V10:
Reworked to more closely match the design used for async suspend
* No longer uses async subsystem cookies for synchronization
* Minimized changes to struct device
* Enable async shutdown for pci and scsi devices which support async suspendChanges from V9:
Address resource and timing issues when spawning a unique async thread for every device during shutdown:
* Make the asynchronous threads able to shut down multiple devices, instead of spawning a unique thread for every device.
* Modify core kernel async code with a custom wake function so it doesn't wake up a thread waiting to synchronize on a cookie until the cookie has reached the desired value, instead of waking up every waiting thread to check the cookie every time an async thread ends.Changes from V8:
Deal with shutdown hangs resulting when a parent/supplier device is
later in thedevices_ksetlist than its children/consumers:
* Ignoresync_state_onlydevlinks for shutdown dependencies
* Ignoreshutdown_afterfor devices that don't want async shutdown
* Add a sanity check to revert to sync shutdown for any device that would otherwise wait for a child/consumer shutdown that hasn't already been scheduledChanges from V7:
Do not expose driver
async_shutdown_enablein sysfs. Wrapped a long line.Changes from V6:
Removed a sysfs attribute that allowed the async device shutdown to be "on" (with driver opt-out), "safe" (driver opt-in), or "off"... what was previously "safe" is now the only behavior, so drivers now only need to have the option to enable or disable async shutdown.
Changes from V5:
Separated into multiple patches to make review easier. Reworked some code to make it more readable
Made devices wait for consumers to shut down, not just children
(suggested by David Jeffery)Changes from V4:
Change code to use cookies for synchronization rather than async domains Allow async shutdown to be disabled via sysfs, and allow driver opt-in or
opt-out of async shutdown (when not disabled), with ability to control driver opt-in/opt-out via sysfsChanges from V3:
Bug fix (used "parent" not "
dev->parent" indevice_shutdown)Changes from V2:
Removed recursive functions to schedule children to be shutdown before
parents, since existingdevice_shutdownloop will already do thisChanges from V1:
Rewritten using kernel async code (suggested by Lukas Wunner)
Stuart Hayes (2):
driver core: separate function to shutdown one device driver core: do not always lock parent in shutdownDavid Jeffery (3):
driver core: async device shutdown infrastructure
PCI: Enable async shutdown support
scsi: Enable async shutdown supportdrivers/base/base.h | 2 + drivers/base/core.c | 180 ++++++++++++++++++++++++++++++-------- drivers/pci/probe.c | 2 + drivers/scsi/hosts.c | 2 + drivers/scsi/scsi_sysfs.c | 3 + include/linux/device.h | 14 ++- 6 files changed, 166 insertions(+), 37 deletions(-)Signature · 1 line
2.53.0