[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250625201853.84062-5-stuart.w.hayes@gmail.com>
Date: Wed, 25 Jun 2025 15:18:52 -0500
From: Stuart Hayes <stuart.w.hayes@...il.com>
To: linux-kernel@...r.kernel.org,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J . Wysocki" <rafael@...nel.org>,
Martin Belanger <Martin.Belanger@...l.com>,
Oliver O'Halloran <oohall@...il.com>,
Daniel Wagner <dwagner@...e.de>,
Keith Busch <kbusch@...nel.org>,
Lukas Wunner <lukas@...ner.de>,
David Jeffery <djeffery@...hat.com>,
Jeremy Allison <jallison@....com>,
Jens Axboe <axboe@...com>,
Christoph Hellwig <hch@....de>,
Sagi Grimberg <sagi@...mberg.me>,
linux-nvme@...ts.infradead.org,
Nathan Chancellor <nathan@...nel.org>,
Jan Kiszka <jan.kiszka@...mens.com>,
Bert Karwatzki <spasswolf@....de>
Cc: Stuart Hayes <stuart.w.hayes@...il.com>
Subject: [PATCH v10 4/5] driver core: shut down devices asynchronously
Add code to allow asynchronous shutdown of devices.
Devices allowed to do asynchronous shutdown can start to shut down in their
own thread as soon as all of the devices dependent on them have finished
shutting down. All other devices will wait until the previous device in
the devices_kset list have finished shutting down.
Only devices with drivers that have async_shutdown_enable set will be shut
down asynchronously.
Devices shutting down asynchronously will be shut down in their own thread,
but synchronous devices in the devices_kset list between asynch devices
will be put in a list, and that list will be shut down in a single thread.
This avoids burdening the system with hundreds of threads on shutdown.
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 when this patch.
Signed-off-by: David Jeffery <djeffery@...hat.com>
Signed-off-by: Stuart Hayes <stuart.w.hayes@...il.com>
---
drivers/base/base.h | 8 ++
drivers/base/core.c | 157 +++++++++++++++++++++++++++++++++-
include/linux/device/driver.h | 2 +
3 files changed, 165 insertions(+), 2 deletions(-)
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 123031a757d9..214207ca5392 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -10,6 +10,7 @@
* shared outside of the drivers/base/ directory.
*
*/
+#include <linux/async.h>
#include <linux/notifier.h>
/**
@@ -85,6 +86,11 @@ struct driver_private {
};
#define to_driver(obj) container_of(obj, struct driver_private, kobj)
+union shutdown_private {
+ struct device *next;
+ async_cookie_t after;
+};
+
/**
* struct device_private - structure to hold the private to the driver core portions of the device structure.
*
@@ -98,6 +104,7 @@ struct driver_private {
* the device; typically because it depends on another driver getting
* probed first.
* @async_driver - pointer to device driver awaiting probe via async_probe
+ * @shutdown - used during device shutdown to ensure correct shutdown ordering.
* @device - pointer back to the struct device that this structure is
* associated with.
* @dead - This device is currently either in the process of or has been
@@ -115,6 +122,7 @@ struct device_private {
struct list_head deferred_probe;
const struct device_driver *async_driver;
char *deferred_probe_reason;
+ union shutdown_private shutdown;
struct device *device;
u8 dead:1;
};
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 39502621e88e..f0484ceefc52 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -9,6 +9,7 @@
*/
#include <linux/acpi.h>
+#include <linux/async.h>
#include <linux/blkdev.h>
#include <linux/cleanup.h>
#include <linux/cpufreq.h>
@@ -4786,6 +4787,8 @@ int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
}
EXPORT_SYMBOL_GPL(device_change_owner);
+static ASYNC_DOMAIN(sd_domain);
+
static void shutdown_one_device(struct device *dev)
{
/* hold lock to avoid race with probe/release */
@@ -4821,12 +4824,116 @@ static void shutdown_one_device(struct device *dev)
put_device(dev);
}
+static bool device_wants_async_shutdown(struct device *dev)
+{
+ if (dev->driver && dev->driver->async_shutdown_enable)
+ return true;
+
+ return false;
+}
+
+/**
+ * set_wait_cookies
+ * @dev: device to find parents and suppliers for
+ * @cookie: shutdown cookie for dev
+ *
+ * Look for parent and suppliers of dev that want async shutdown, and
+ * set shutdown.after to cookie on those devices to ensure they
+ * don't shut down before dev.
+ *
+ * Passing a cookie of zero will return whether any such devices are found
+ * without setting shutdown.after.
+ *
+ * Return true if any async supplier/parent devices are found.
+ */
+static bool device_set_async_cookie(struct device *dev, async_cookie_t cookie)
+{
+ int idx;
+ struct device_link *link;
+ bool ret = false;
+ struct device *parent = dev->parent;
+
+ if (parent && device_wants_async_shutdown(parent)) {
+ ret = true;
+ if (cookie)
+ parent->p->shutdown.after = cookie;
+ else
+ goto done;
+ }
+
+ idx = device_links_read_lock();
+ list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
+ device_links_read_lock_held()) {
+ if (!device_link_flag_is_sync_state_only(link->flags)
+ && device_wants_async_shutdown(link->supplier)) {
+ ret = true;
+ if (cookie)
+ link->supplier->p->shutdown.after = cookie;
+ else
+ break;
+ }
+ }
+ device_links_read_unlock(idx);
+done:
+ return ret;
+}
+
+#define is_async_shutdown_dependency(dev) device_set_async_cookie(dev, 0)
+
+/**
+ * shutdown_devices_async
+ * @data: list of devices to be shutdown
+ * @cookie: not used
+ *
+ * Shuts down devices after waiting for previous devices to shut down (for
+ * synchronous shutdown) or waiting for device's last child or consumer to
+ * be shutdown (for async shutdown).
+ *
+ * shutdown.after is set to the shutdown cookie of the last child or consumer
+ * of this device (if any).
+ */
+static void shutdown_devices_async(void *data, async_cookie_t cookie)
+{
+ struct device *next, *dev = data;
+ async_cookie_t wait = cookie;
+ bool async = device_wants_async_shutdown(dev);
+
+ if (async) {
+ wait = dev->p->shutdown.after + 1;
+ /*
+ * To prevent system hang, revert to sync shutdown in the event
+ * that shutdown.after would make this shutdown wait for a
+ * shutdown that hasn't been scheduled yet.
+ *
+ * This can happen if a parent or supplier is not ordered in the
+ * devices_kset list before a child or consumer, which is not
+ * expected.
+ */
+ if (wait > cookie) {
+ wait = cookie;
+ dev_warn(dev, "Unsafe shutdown ordering, forcing sync order\n");
+ }
+ }
+
+ async_synchronize_cookie_domain(wait, &sd_domain);
+
+ /*
+ * Shut down the async device or list of sync devices
+ */
+ do {
+ next = dev->p->shutdown.next;
+ shutdown_one_device(dev);
+ dev = next;
+ } while (!async && dev);
+}
+
/**
* device_shutdown - call ->shutdown() on each device to shutdown.
*/
void device_shutdown(void)
{
- struct device *dev, *parent;
+ struct device *dev, *parent, *synclist = NULL, *syncend = NULL;
+ async_cookie_t cookie = 0;
wait_for_device_probe();
device_block_probing();
@@ -4857,11 +4964,57 @@ void device_shutdown(void)
list_del_init(&dev->kobj.entry);
spin_unlock(&devices_kset->list_lock);
- shutdown_one_device(dev);
+ get_device(dev);
+ get_device(parent);
+
+ if (device_wants_async_shutdown(dev)) {
+ /*
+ * async devices run alone in their own async task,
+ * push out any waiting sync devices to maintain
+ * ordering.
+ */
+ if (synclist) {
+ async_schedule_domain(shutdown_devices_async,
+ synclist, &sd_domain);
+ synclist = syncend = NULL;
+ }
+
+ cookie = async_schedule_domain(shutdown_devices_async,
+ dev, &sd_domain);
+ device_set_async_cookie(dev, cookie);
+ } else {
+ if (!synclist) {
+ synclist = syncend = dev;
+ } else {
+ syncend->p->shutdown.next = dev;
+ syncend = dev;
+ }
+ if (is_async_shutdown_dependency(dev)) {
+ /*
+ * dev is a dependency for an async device,
+ * kick off a new thread so it can complete
+ * and allow the async device to run its
+ * shutdown.
+ */
+ cookie = async_schedule_domain(
+ shutdown_devices_async,
+ synclist, &sd_domain);
+ device_set_async_cookie(dev, cookie);
+ synclist = syncend = NULL;
+ }
+ }
+
+ put_device(parent);
+ put_device(dev);
spin_lock(&devices_kset->list_lock);
}
spin_unlock(&devices_kset->list_lock);
+
+ if (synclist)
+ async_schedule_domain(shutdown_devices_async, synclist,
+ &sd_domain);
+ async_synchronize_full_domain(&sd_domain);
}
/*
diff --git a/include/linux/device/driver.h b/include/linux/device/driver.h
index cd8e0f0a634b..c63bc0050c84 100644
--- a/include/linux/device/driver.h
+++ b/include/linux/device/driver.h
@@ -56,6 +56,7 @@ enum probe_type {
* @mod_name: Used for built-in modules.
* @suppress_bind_attrs: Disables bind/unbind via sysfs.
* @probe_type: Type of the probe (synchronous or asynchronous) to use.
+ * @async_shutdown_enable: Enables devices to be shutdown asynchronously.
* @of_match_table: The open firmware table.
* @acpi_match_table: The ACPI match table.
* @probe: Called to query the existence of a specific device,
@@ -102,6 +103,7 @@ struct device_driver {
bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
enum probe_type probe_type;
+ bool async_shutdown_enable;
const struct of_device_id *of_match_table;
const struct acpi_device_id *acpi_match_table;
--
2.39.3
Powered by blists - more mailing lists