Skip to contentSkip to conversation

shut down devices asynchronously

PATCH Superseded David Jeffery 3 participants 13 messages Jul 1 – Jul 14

Subsystems (6)
Documentation SCSI PCI Driver core +2 more subsystems

v18 of 22 Superseded Latest is v22 →

lore
Share & tools
lore
#0 lore raw

v18 cover shut down devices asynchronously

These patches are rebased against the driver-core tree's driver-core-next branch and should also apply against recent linux-next. Changes for v18 are contained in patch 3.

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 V17:

Fix mangled text in kernel parameter description
Re-protect the list removal with the spinlock
* Hold a device reference to ensure the device cannot be freed before attempting list removal

Changes from V16:

Drop spinlock before async subsystem call which uses GFP_KERNEL Handle that async shutdown can widen races between device shutdown and deletion
* __shutdown_one_device will immediately return if a device is dead
* Set shutdown device completion to complete when marking a device dead to prevent waiting on a dead device
* Only late-access a parent pointer if device is in a non-dead state to ensure the pointer is still valid

Changes from V15:

The async_shutdown bit field is converted to a device flags bit Convert all patches to use the flag bit accessor macros to set or check if
async shutdown should be used
Added documentation on the kernel parameter to control use of async shutdown

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

 .../admin-guide/kernel-parameters.txt         |  10 +
 drivers/base/base.h                           |   2 +
 drivers/base/core.c                           | 221 +++++++++++++++---
 drivers/pci/probe.c                           |   2 +
 drivers/scsi/hosts.c                          |   2 +
 drivers/scsi/scsi_sysfs.c                     |   3 +
 include/linux/device.h                        |   2 +
 7 files changed, 206 insertions(+), 36 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v18 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
index 4d026682944f..74c693cd19cf 100644
@@ -4865,12 +4865,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();
@@ -4892,7 +4928,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
@@ -4901,36 +4937,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.54.0
#2 lore raw

Re: [PATCH 1/5] driver core: separate function to shutdown one device

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v18 2/5 driver core: do not always lock parent in shutdown

In reply to David Jeffery

Don'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 | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)
drivers/base/core.c +26 -16
index 74c693cd19cf..3b3d983b1747 100644
@@ -4865,14 +4865,10 @@ 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);
+	if (dev->p->dead)
+		return;
 
 	/* Don't allow any more runtime suspends */
 	pm_runtime_get_noresume(dev);
@@ -4892,12 +4888,32 @@ static void shutdown_one_device(struct device *dev)
 			dev_info(dev, "shutdown\n");
 		dev->driver->shutdown(dev);
 	}
+}
 
-	device_unlock(dev);
-	if (parent)
+static void shutdown_one_device(struct device *dev)
+{
+	struct device *parent;
+
+	device_lock(dev);
+
+	/* use parent lock if needed to avoid race with probe/release */
+	if (dev->bus && dev->bus->need_parent_lock && !dev->p->dead &&
+	    (parent = get_device(dev->parent))) {
+		/* the parent lock needs to be acquired first, so re-lock */
+		device_unlock(dev);
+
+		device_lock(parent);
+		device_lock(dev);
+
+		__shutdown_one_device(dev);
+		device_unlock(dev);
 		device_unlock(parent);
+		put_device(parent);
+	} else {
+		__shutdown_one_device(dev);
+		device_unlock(dev);
+	}
 
-	put_device(parent);
 	put_device(dev);
 }
 
@@ -4923,12 +4939,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.54.0
#4 lore raw

Re: [PATCH 2/5] driver core: do not always lock parent in shutdown

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

Don'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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v18 3/5 driver core: async device shutdown infrastructure

In reply to David Jeffery

Patterned 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.

Enabled by default, async shutdown can be explicitly enabled or disabled by using the kernel parameter "core.async_shutdown=<bool>"

---
 .../admin-guide/kernel-parameters.txt         |  10 ++
 drivers/base/base.h                           |   2 +
 drivers/base/core.c                           | 134 +++++++++++++++++-
 include/linux/device.h                        |   2 +
 4 files changed, 147 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +10 -0
index b5a51a36a048..7a428479089e 100644
@@ -1019,6 +1019,16 @@ Kernel parameters
 			seconds. A value of 0 disables the blank timer.
 			Defaults to 0.
 
+	core.async_shutdown=
+			[KNL]
+			Format: <bool>
+			Enable or disable asynchronous shutdown support. When
+			enabled, on system shutdown unrelated devices flagged
+			as async shutdown compatible may be shut down in
+			parallel and asynchronously. When disabled, device
+			shutdown is performed serially and synchronously.
+			Enabled by default.
+
 	coredump_filter=
 			[KNL] Change the default value for
 			/proc/<pid>/coredump_filter.
drivers/base/base.h +2 -0
index a5b7abc10ff0..40dbf588a5d6 100644
@@ -103,6 +103,7 @@ struct driver_private {
  *			   dev_err_probe() for later retrieval via debugfs
  * @device: pointer back to the struct device that this structure is
  *	    associated with.
+ * @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.
@@ -119,6 +120,7 @@ struct device_private {
 	const struct device_driver *async_driver;
 	char *deferred_probe_reason;
 	struct device *device;
+	struct completion complete;
 	u8 dead:1;
 };
 #define to_device_private_parent(obj)	\
drivers/base/core.c +133 -1

192 lines not on this page · raw message

include/linux/device.h +2 -0
index 7b2baffdd2f5..f913d72218f8 100644
@@ -610,6 +610,7 @@ enum struct_device_flags {
 	DEV_FLAG_OF_NODE_REUSED = 7,
 	DEV_FLAG_OFFLINE_DISABLED = 8,
 	DEV_FLAG_OFFLINE = 9,
+	DEV_FLAG_ASYNC_SHUTDOWN = 10,
 
 	DEV_FLAG_COUNT
 };
@@ -827,6 +828,7 @@ __create_dev_flag_accessors(dma_coherent, DEV_FLAG_DMA_COHERENT);
 __create_dev_flag_accessors(of_node_reused, DEV_FLAG_OF_NODE_REUSED);
 __create_dev_flag_accessors(offline_disabled, DEV_FLAG_OFFLINE_DISABLED);
 __create_dev_flag_accessors(offline, DEV_FLAG_OFFLINE);
+__create_dev_flag_accessors(async_shutdown, DEV_FLAG_ASYNC_SHUTDOWN);
 
 #undef __create_dev_flag_accessors
 
Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH 3/5] driver core: async device shutdown infrastructure

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Modifying the core.async_shutdown module parameter during shutdown causes synchronous suppliers to…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Modifying the core.async_shutdown module parameter during shutdown causes synchronous suppliers to skip waiting for asynchronous consumers.
- [High] Unregistered consumer devices will cause a NULL pointer dereference during async shutdown dependency waiting.
- [High] Adding a new device with async shutdown enabled while its synchronous supplier is being processed leads to an unresolvable deadlock.
- [High] Dereferencing dev->p->dead without NULL checks causes a crash if a concurrent device_add() fails.
--

commit cf98cdf76df805443cc885868cb3d9e13da5501c
Author: David Jeffery <[email protected]>

driver core: async device shutdown infrastructure

This commit allows devices to opt into asynchronous shutdown, executing in parallel with unrelated devices after their dependencies shut down. Synchronous shutdown devices will wait for their asynchronous dependencies. The feature is enabled by default and can be toggled via the core.async_shutdown parameter.

Quoted 3 lines
drivers/base/core.c +0 -0

[ ... ]

Quoted 4 lines
drivers/base/core.c +4 -0
+static bool wants_async_shutdown(struct device *dev)
+{
+	return async_shutdown && dev_async_shutdown(dev);
+}

drivers/base/core.c int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) · line 4888

Quoted 10 lines
drivers/base/core.c +10 -0
+
+static int wait_for_device_shutdown(struct device *dev, void *data)
+{
+	bool async = *(bool *)data;
+
+	if (async || wants_async_shutdown(dev))
+		wait_for_completion(&dev->p->complete);
+
+	return 0;
+}

[Severity: High]
If a user-space process toggles the 0644 core.async_shutdown sysfs parameter from 1 to 0 during shutdown, could this cause synchronous suppliers to skip waiting for their asynchronous consumers?

If start_async_shutdown_devices() schedules an async consumer, and then the parameter is toggled before the sync loop processes its supplier, the call to wants_async_shutdown(dev) here would evaluate to false.

This would skip the wait_for_completion(), potentially causing the supplier (e.g., a PCI bus) to shut down while its consumer is still active.

[Severity: High]
Could this result in a NULL pointer dereference if the consumer is not yet registered?

The device_link_add() function allows creating links to consumer devices that haven't been registered yet, meaning their dev->p is NULL because device_private_init() hasn't run.

If wait_for_shutdown_dependencies() is called from the async pool, where the async argument is forced to true, the wait_for_completion() will unconditionally evaluate &dev->p->complete, crashing on the NULL pointer.

[ ... ]

drivers/base/core.c int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)

Quoted 4 lines
drivers/base/core.c +1 -0
+static void __shutdown_one_device(struct device *dev)
 {
 	if (dev->p->dead)
 		return;

[Severity: High]
Could this cause a NULL pointer dereference if a concurrent device_add() fails?

If a concurrent device_add() fails after calling kobject_add(), its error path calls kobject_del() and then frees and nullifies dev->p. Evaluating dev->p->dead without checking if dev->p is valid could crash here.

[ ... ]

Quoted 3 lines
drivers/base/core.c +3 -0
+static void start_async_shutdown_devices(void)
+{
+	struct device *dev, *next, *ndev, *needs_put = NULL;

[ ... ]

drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 5031

Quoted 9 lines
drivers/base/core.c +9 -0
+			needs_put = ndev;
+			spin_lock(&devices_kset->list_lock);
+			/*
+			 * If the next device has been marked dead while the
+			 * spinlock was released, it may no longer be on the
+			 * devices_kset list. Restart the list walk to be safe
+			 */
+			if (ndev && ndev->p->dead)
+				goto restart;

[Severity: High]
Could ndev->p be NULL if a concurrent device_add() fails?

If start_async_shutdown_devices() obtains a reference to next via get_device(), and concurrently device_add() fails and clears dev->p, reacquiring the lock and evaluating ndev->p->dead here could result in a NULL pointer dereference.

[ ... ]

Quoted 2 lines

void device_shutdown(void)
{

[ ... ]

drivers/base/core.c void device_shutdown(void) · line 5084

Quoted 9 lines
drivers/base/core.c +4 -1
-		shutdown_one_device(dev);
+		if (!wants_async_shutdown(dev) || !shutdown_device_async(dev)) {
+			wait_for_shutdown_dependencies(dev, false);
+			shutdown_one_device(dev);
+		}
 
 		spin_lock(&devices_kset->list_lock);
 	}
 	spin_unlock(&devices_kset->list_lock);

[Severity: High]
Can this sequence lead to an unresolvable deadlock if a new device is added concurrently?

If a new device 'C' with DEV_FLAG_ASYNC_SHUTDOWN is added via hotplug after its synchronous supplier 'S' has already been popped from devices_kset by this loop, 'C' is appended to devices_kset.

'S' will call wait_for_shutdown_dependencies() and find 'C', blocking on wait_for_completion(&C->p->complete) because wants_async_shutdown(C) is true.

Since 'C' was added after start_async_shutdown_devices() ran, it is not running in the async pool. Because the sync loop is blocked on 'S', it will never reach 'C' to start it, deadlocking the shutdown process permanently.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [PATCH 3/5] driver core: async device shutdown infrastructure

Hello,

These most of the errors are due to the device_add concurrently can add the device and device_shutdown can access it. One of the reason I think a device_add will be triggered while being shutdown is inserting the physical PCI device or USB stick etc.

But During shutdown, addition of a new device is not valid. So we can add a check something like

drivers/base/core.c +7 -0
index 76ba02c26aa5..c3795fa1cc26 100644
@@ -3650,6 +3650,13 @@ int device_add(struct device *dev)
        if (!dev)
                goto done;
 
+       if (unlikely(system_state == SYSTEM_HALT ||
+                    system_state == SYSTEM_POWER_OFF ||
+                    system_state == SYSTEM_RESTART)) {
+               error = -ESHUTDOWN;
+               goto done;
+       }
+
        if (!dev->p) {
                error = device_private_init(dev);
                if (error)

We can also partially resolve the races partially by removing the manual kfree(dev->p) from device_add() error path. As put_device free it anyway when reference to devices reaches to 0. So if device_shutdown path takes reference, dev->p will not be released. And few races will be taken care of. But this will not resolve the deadlock and other races like the above change.

removing the kfree(dev->p) from device_add can be a independent patch. like:

drivers/base/core.c +1 -2
index 4d026682944f..76ba02c26aa5 100644
@@ -2614,6 +2614,7 @@ static void device_release(struct kobject *kobj)
        struct device *dev = kobj_to_dev(kobj);
        struct device_private *p = dev->p;
 
+       dev->p = NULL;
        /*
         * Some platform devices are driven without driver attached
         * and managed resources may have been acquired.  Make sure
@@ -3824,8 +3825,6 @@ int device_add(struct device *dev)
 parent_error:
        put_device(parent);
 name_error:
-       kfree(dev->p);
-       dev->p = NULL;
        goto done;
 }
 EXPORT_SYMBOL_GPL(device_add);

~Tarun

Quoted 127 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Modifying the core.async_shutdown module parameter during shutdown causes synchronous suppliers to skip waiting for asynchronous consumers.
- [High] Unregistered consumer devices will cause a NULL pointer dereference during async shutdown dependency waiting.
- [High] Adding a new device with async shutdown enabled while its synchronous supplier is being processed leads to an unresolvable deadlock.
- [High] Dereferencing dev->p->dead without NULL checks causes a crash if a concurrent device_add() fails.
--

commit cf98cdf76df805443cc885868cb3d9e13da5501c
Author: David Jeffery <[email protected]>

driver core: async device shutdown infrastructure

This commit allows devices to opt into asynchronous shutdown, executing in parallel with unrelated devices after their dependencies shut down. Synchronous shutdown devices will wait for their asynchronous dependencies. The feature is enabled by default and can be toggled via the core.async_shutdown parameter.

Quoted 3 lines
drivers/base/core.c +0 -0

[ ... ]

Quoted 4 lines
drivers/base/core.c +4 -0
+static bool wants_async_shutdown(struct device *dev)
+{
+	return async_shutdown && dev_async_shutdown(dev);
+}

drivers/base/core.c

Quoted 10 lines
drivers/base/core.c +10 -0
+
+static int wait_for_device_shutdown(struct device *dev, void *data)
+{
+	bool async = *(bool *)data;
+
+	if (async || wants_async_shutdown(dev))
+		wait_for_completion(&dev->p->complete);
+
+	return 0;
+}

[Severity: High]
If a user-space process toggles the 0644 core.async_shutdown sysfs parameter from 1 to 0 during shutdown, could this cause synchronous suppliers to skip waiting for their asynchronous consumers?

If start_async_shutdown_devices() schedules an async consumer, and then the parameter is toggled before the sync loop processes its supplier, the call to wants_async_shutdown(dev) here would evaluate to false.

This would skip the wait_for_completion(), potentially causing the supplier (e.g., a PCI bus) to shut down while its consumer is still active.

[Severity: High]
Could this result in a NULL pointer dereference if the consumer is not yet registered?

The device_link_add() function allows creating links to consumer devices that haven't been registered yet, meaning their dev->p is NULL because device_private_init() hasn't run.

If wait_for_shutdown_dependencies() is called from the async pool, where the async argument is forced to true, the wait_for_completion() will unconditionally evaluate &dev->p->complete, crashing on the NULL pointer.

[ ... ]

drivers/base/core.c

Quoted 4 lines
drivers/base/core.c +1 -0
+static void __shutdown_one_device(struct device *dev)
 {
 	if (dev->p->dead)
 		return;

[Severity: High]
Could this cause a NULL pointer dereference if a concurrent device_add() fails?

If a concurrent device_add() fails after calling kobject_add(), its error path calls kobject_del() and then frees and nullifies dev->p. Evaluating dev->p->dead without checking if dev->p is valid could crash here.

[ ... ]

Quoted 3 lines
drivers/base/core.c +3 -0
+static void start_async_shutdown_devices(void)
+{
+	struct device *dev, *next, *ndev, *needs_put = NULL;

[ ... ]

drivers/base/core.c

Quoted 9 lines
drivers/base/core.c +9 -0
+			needs_put = ndev;
+			spin_lock(&devices_kset->list_lock);
+			/*
+			 * If the next device has been marked dead while the
+			 * spinlock was released, it may no longer be on the
+			 * devices_kset list. Restart the list walk to be safe
+			 */
+			if (ndev && ndev->p->dead)
+				goto restart;

[Severity: High]
Could ndev->p be NULL if a concurrent device_add() fails?

If start_async_shutdown_devices() obtains a reference to next via get_device(), and concurrently device_add() fails and clears dev->p, reacquiring the lock and evaluating ndev->p->dead here could result in a NULL pointer dereference.

[ ... ]

Quoted 2 lines

void device_shutdown(void)
{

[ ... ]

drivers/base/core.c

Quoted 9 lines
drivers/base/core.c +4 -1
-		shutdown_one_device(dev);
+		if (!wants_async_shutdown(dev) || !shutdown_device_async(dev)) {
+			wait_for_shutdown_dependencies(dev, false);
+			shutdown_one_device(dev);
+		}
 
 		spin_lock(&devices_kset->list_lock);
 	}
 	spin_unlock(&devices_kset->list_lock);

[Severity: High]
Can this sequence lead to an unresolvable deadlock if a new device is added concurrently?

If a new device 'C' with DEV_FLAG_ASYNC_SHUTDOWN is added via hotplug after its synchronous supplier 'S' has already been popped from devices_kset by this loop, 'C' is appended to devices_kset.

'S' will call wait_for_shutdown_dependencies() and find 'C', blocking on wait_for_completion(&C->p->complete) because wants_async_shutdown(C) is true.

Since 'C' was added after start_async_shutdown_devices() ran, it is not running in the async pool. Because the sync loop is blocked on 'S', it will never reach 'C' to start it, deadlocking the shutdown process permanently.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#8 lore raw

Re: [PATCH 3/5] driver core: async device shutdown infrastructure

+Adding folks from the original message. Unfortunately, Sashiko drops the people from the original message.

Quoted 194 lines · Tarun Sahu

Hello,

These most of the errors are due to the device_add concurrently can add the device and device_shutdown can access it. One of the reason I think a device_add will be triggered while being shutdown is inserting the physical PCI device or USB stick etc.

But During shutdown, addition of a new device is not valid. So we can add a check something like

drivers/base/core.c +7 -0
index 76ba02c26aa5..c3795fa1cc26 100644
@@ -3650,6 +3650,13 @@ int device_add(struct device *dev)
        if (!dev)
                goto done;
 
+       if (unlikely(system_state == SYSTEM_HALT ||
+                    system_state == SYSTEM_POWER_OFF ||
+                    system_state == SYSTEM_RESTART)) {
+               error = -ESHUTDOWN;
+               goto done;
+       }
+
        if (!dev->p) {
                error = device_private_init(dev);
                if (error)

We can also partially resolve the races partially by removing the manual kfree(dev->p) from device_add() error path. As put_device free it anyway when reference to devices reaches to 0. So if device_shutdown path takes reference, dev->p will not be released. And few races will be taken care of. But this will not resolve the deadlock and other races like the above change.

removing the kfree(dev->p) from device_add can be a independent patch. like:

drivers/base/core.c +1 -2
index 4d026682944f..76ba02c26aa5 100644
@@ -2614,6 +2614,7 @@ static void device_release(struct kobject *kobj)
        struct device *dev = kobj_to_dev(kobj);
        struct device_private *p = dev->p;
 
+       dev->p = NULL;
        /*
         * Some platform devices are driven without driver attached
         * and managed resources may have been acquired.  Make sure
@@ -3824,8 +3825,6 @@ int device_add(struct device *dev)
 parent_error:
        put_device(parent);
 name_error:
-       kfree(dev->p);
-       dev->p = NULL;
        goto done;
 }
 EXPORT_SYMBOL_GPL(device_add);

~Tarun

Quoted 127 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Modifying the core.async_shutdown module parameter during shutdown causes synchronous suppliers to skip waiting for asynchronous consumers.
- [High] Unregistered consumer devices will cause a NULL pointer dereference during async shutdown dependency waiting.
- [High] Adding a new device with async shutdown enabled while its synchronous supplier is being processed leads to an unresolvable deadlock.
- [High] Dereferencing dev->p->dead without NULL checks causes a crash if a concurrent device_add() fails.
--

commit cf98cdf76df805443cc885868cb3d9e13da5501c
Author: David Jeffery <[email protected]>

driver core: async device shutdown infrastructure

This commit allows devices to opt into asynchronous shutdown, executing in parallel with unrelated devices after their dependencies shut down. Synchronous shutdown devices will wait for their asynchronous dependencies. The feature is enabled by default and can be toggled via the core.async_shutdown parameter.

Quoted 3 lines
drivers/base/core.c +0 -0

[ ... ]

Quoted 4 lines
drivers/base/core.c +4 -0
+static bool wants_async_shutdown(struct device *dev)
+{
+	return async_shutdown && dev_async_shutdown(dev);
+}

drivers/base/core.c

Quoted 10 lines
drivers/base/core.c +10 -0
+
+static int wait_for_device_shutdown(struct device *dev, void *data)
+{
+	bool async = *(bool *)data;
+
+	if (async || wants_async_shutdown(dev))
+		wait_for_completion(&dev->p->complete);
+
+	return 0;
+}

[Severity: High]
If a user-space process toggles the 0644 core.async_shutdown sysfs parameter from 1 to 0 during shutdown, could this cause synchronous suppliers to skip waiting for their asynchronous consumers?

If start_async_shutdown_devices() schedules an async consumer, and then the parameter is toggled before the sync loop processes its supplier, the call to wants_async_shutdown(dev) here would evaluate to false.

This would skip the wait_for_completion(), potentially causing the supplier (e.g., a PCI bus) to shut down while its consumer is still active.

[Severity: High]
Could this result in a NULL pointer dereference if the consumer is not yet registered?

The device_link_add() function allows creating links to consumer devices that haven't been registered yet, meaning their dev->p is NULL because device_private_init() hasn't run.

If wait_for_shutdown_dependencies() is called from the async pool, where the async argument is forced to true, the wait_for_completion() will unconditionally evaluate &dev->p->complete, crashing on the NULL pointer.

[ ... ]

drivers/base/core.c

Quoted 4 lines
drivers/base/core.c +1 -0
+static void __shutdown_one_device(struct device *dev)
 {
 	if (dev->p->dead)
 		return;

[Severity: High]
Could this cause a NULL pointer dereference if a concurrent device_add() fails?

If a concurrent device_add() fails after calling kobject_add(), its error path calls kobject_del() and then frees and nullifies dev->p. Evaluating dev->p->dead without checking if dev->p is valid could crash here.

[ ... ]

Quoted 3 lines
drivers/base/core.c +3 -0
+static void start_async_shutdown_devices(void)
+{
+	struct device *dev, *next, *ndev, *needs_put = NULL;

[ ... ]

drivers/base/core.c

Quoted 9 lines
drivers/base/core.c +9 -0
+			needs_put = ndev;
+			spin_lock(&devices_kset->list_lock);
+			/*
+			 * If the next device has been marked dead while the
+			 * spinlock was released, it may no longer be on the
+			 * devices_kset list. Restart the list walk to be safe
+			 */
+			if (ndev && ndev->p->dead)
+				goto restart;

[Severity: High]
Could ndev->p be NULL if a concurrent device_add() fails?

If start_async_shutdown_devices() obtains a reference to next via get_device(), and concurrently device_add() fails and clears dev->p, reacquiring the lock and evaluating ndev->p->dead here could result in a NULL pointer dereference.

[ ... ]

Quoted 2 lines

void device_shutdown(void)
{

[ ... ]

drivers/base/core.c

Quoted 9 lines
drivers/base/core.c +4 -1
-		shutdown_one_device(dev);
+		if (!wants_async_shutdown(dev) || !shutdown_device_async(dev)) {
+			wait_for_shutdown_dependencies(dev, false);
+			shutdown_one_device(dev);
+		}
 
 		spin_lock(&devices_kset->list_lock);
 	}
 	spin_unlock(&devices_kset->list_lock);

[Severity: High]
Can this sequence lead to an unresolvable deadlock if a new device is added concurrently?

If a new device 'C' with DEV_FLAG_ASYNC_SHUTDOWN is added via hotplug after its synchronous supplier 'S' has already been popped from devices_kset by this loop, 'C' is appended to devices_kset.

'S' will call wait_for_shutdown_dependencies() and find 'C', blocking on wait_for_completion(&C->p->complete) because wants_async_shutdown(C) is true.

Since 'C' was added after start_async_shutdown_devices() ran, it is not running in the async pool. Because the sync loop is blocked on 'S', it will never reach 'C' to start it, deadlocking the shutdown process permanently.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

PATCH v18 4/5 PCI: Enable async shutdown support

In reply to David Jeffery

Like 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
index b63cd0c310bc..7cf3dabc885e 100644
@@ -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);
+	dev_set_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);
+	dev_set_async_shutdown(&dev->dev);
 
 	/*
 	 * Add the device to our list of discovered devices
Signature · 1 line
2.54.0
#10 lore raw

Re: [PATCH 4/5] PCI: Enable async shutdown support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] In pci_register_host_bridge(), a failure in device_register(&bus->dev) leads to a double-free of the bridge->dev object.
- [High] In pci_device_add(), failure of device_add() is ignored, leaving a partially-initialized device in the subsystem lists. --

commit 32aeb814f8bbaee48b0baf0572f23452cd1e50da
Author: David Jeffery <[email protected]>

PCI: Enable async shutdown support

This commit adds asynchronous shutdown support for PCI devices. It aims to reduce overall shutdown time in the same way async suspend does.

drivers/pci/probe.c static int pci_register_host_bridge(struct pci_host_bridge *bridge) · line 1049

Quoted 9 lines
drivers/pci/probe.c +1 -0
index dd0abbc63e18d..af23814464060 100644
@@ -1046,6 +1046,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 
 	bus->bridge = get_device(&bridge->dev);
 	device_enable_async_suspend(bus->bridge);
+	dev_set_async_shutdown(bus->bridge);

[Severity: Critical]
This is a pre-existing issue, and was not introduced by the patch under review, but does the error handling in pci_register_host_bridge() lead to a double-free of the bridge->dev object?

If device_register(&bus->dev) fails, the code jumps to the unregister label:

unregister:

	put_device(&bridge->dev);
	device_del(&bridge->dev);
free:
	...
	if (bus_registered)
		put_device(&bus->dev);

Because bus_registered is set to true right before checking the error from device_register(), the free path calls put_device(&bus->dev).

Releasing the bus triggers release_pcibus_dev(), which implicitly drops the pci_bus->bridge reference via put_device(pci_bus->bridge). Since the code explicitly drops the reference at the unregister label as well, does this drop the object's refcount to 0 prematurely?

[ ... ]

drivers/pci/probe.c void pci_device_add(struct pci_dev *dev, struct pci_bus *bus) · line 2755

Quoted 8 lines
drivers/pci/probe.c +1 -0
@@ -2748,6 +2749,7 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
 	pci_reassigndev_resource_alignment(dev);
 
 	pci_init_capabilities(dev);
+	dev_set_async_shutdown(&dev->dev);
 
 	/*
 	 * Add the device to our list of discovered devices

[Severity: High]
This isn't a bug introduced by this patch, but I noticed the failure of device_add() is ignored later in pci_device_add():

	ret = device_add(&dev->dev);
	WARN_ON(ret < 0);

If device_add() fails, the device remains in the bus->devices list but is not tracked by the driver core.

When the PCI device or bus is later removed, pci_destroy_dev() unconditionally calls device_del(&dev->dev). Calling device_del() on a device that was never successfully added via device_add() violates the driver core API.

Could this lead to a kernel crash or NULL pointer dereference when a PCI device is enumerated under memory pressure?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#11 lore raw

PATCH v18 5/5 scsi: Enable async shutdown support

In reply to David Jeffery

Like 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
index e047747d4ecf..bf691acc7a67 100644
@@ -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);
+	dev_set_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);
+	dev_set_async_shutdown(&shost->shost_dev);
 
 	get_device(&shost->shost_gendev);
 	error = device_add(&shost->shost_dev);
drivers/scsi/scsi_sysfs.c +3 -0
index dfc3559e7e04..8fd317aef37b 100644
@@ -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);
+	dev_set_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);
+	dev_set_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);
+	dev_set_async_shutdown(&sdev->sdev_dev);
 	error = device_add(&sdev->sdev_dev);
 	if (error) {
 		sdev_printk(KERN_INFO, sdev,
Signature · 1 line
2.54.0
#12 lore raw

Re: [PATCH 5/5] scsi: Enable async shutdown support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Runtime PM usage counter and PM state leak on device_add error path.
- [High] Concurrent lockless bitfield mutations cause Data Race and PM state corruption. --

commit 10ebc4309581b7d74ed1d3564b5f1af2a5cf5785
Author: David Jeffery <[email protected]>

scsi: Enable async shutdown support

Like scsi's async suspend support, allow scsi devices to be shut down asynchronously to reduce system shutdown time.

drivers/scsi/scsi_sysfs.c static int scsi_target_add(struct scsi_target *starget) · line 1389

Quoted 9 lines
drivers/scsi/scsi_sysfs.c +1 -0
index dfc3559e7e04f..8fd317aef37b4 100644
@@ -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);
+	dev_set_async_shutdown(&starget->dev);

[Severity: High]
This is a pre-existing issue, but does adding dev_set_async_shutdown() here exacerbate a data race with system suspend?

In scsi_target_add(), device_add() is called earlier, exposing the device to the PM core. If a system-wide suspend happens concurrently, device_prepare() may lock the device and set the is_prepared bitfield.

Since device_enable_async_suspend() and dev_set_async_shutdown() locklessly mutate bitfields in dev->power in the same memory word, couldn't the resulting read-modify-write instructions overwrite and clear the is_prepared flag set by the PM core?

Quoted 2 lines
drivers/scsi/scsi_sysfs.c +0 -0
 	return 0;
 }

[ ... ]

drivers/scsi/scsi_sysfs.c int scsi_sysfs_add_sdev(struct scsi_device *sdev) · line 1416

Quoted 5 lines
drivers/scsi/scsi_sysfs.c +1 -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);
+	dev_set_async_shutdown(&sdev->sdev_gendev);

[Severity: High]
This isn't a bug introduced by this patch, but could the error path for device_add(&sdev->sdev_gendev) leak the PM usage counter and runtime state?

When scsi_sysfs_add_sdev() is called, it increments the PM usage count via scsi_autopm_get_device(sdev) and enables runtime PM. If the subsequent device_add(&sdev->sdev_gendev) fails, the function returns the error directly without dropping the usage counter or disabling runtime PM:

scsi_sysfs_add_sdev() {
    ...
    scsi_autopm_get_device(sdev);
    ...
    error = device_add(&sdev->sdev_gendev);
    if (error) {
        sdev_printk(...);
        return error;
    }
}

Would this prevent the device and its parent target from ever entering runtime suspend, causing unbounded power drain?

Quoted 2 lines
drivers/scsi/scsi_sysfs.c +0 -0
 	scsi_autopm_get_target(starget);
 	pm_runtime_set_active(&sdev->sdev_gendev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5