shut down devices asynchronously
Subsystems (4)
v19 cover shut down devices asynchronously
This is rebased on top of device-core/driver-core-next. And can also be applied easily against linux-next/master
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 V18:
- Fix deadlock and race condition on concurrent device_add
- Use local variable to device_shutdown for caching async_shutdown
- Added dev->p check to avoid NULL pointer deference and tell if the device is not registered so no need to be shutdown. This is to handle the cases when devices consumers list might have devices that are not yet registered.
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)
David Jeffery (5):
driver core: separate function to shutdown one device driver core: do not always lock parent in shutdown driver core: async device shutdown infrastructure
PCI: Enable async shutdown support
scsi: Enable async shutdown support
Tarun Sahu (2):
device_core: rely on put_device to free dev->p driver core: Prevent device_add() during system shutdown
.../admin-guide/kernel-parameters.txt | 10 + drivers/base/base.h | 2 + drivers/base/core.c | 265 +++++++++++++++--- drivers/pci/probe.c | 2 + drivers/scsi/hosts.c | 2 + drivers/scsi/scsi_sysfs.c | 3 + include/linux/device.h | 2 + 7 files changed, 248 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0.229.g6434b31f56-goog
PATCH v19 1/7 device_core: rely on put_device to free dev->p
device_add allocate private_data for device and assigns to dev->p. If device_add fails in later steps of the function, it cleans up this dev->p which is not necessary because In the next call, put_device free it anyway (if reference to the device is 0 which will be unless someone concurrently get the reference to this device).
This avoids unnecessary races introduced in system. After device is added in device_kset->list by device_add and later steps in the device_add function failures occur, it will free dev->p manually, while in between there might be a user of device_kset->list will take reference to the device just added by device_add. and might try to access dev->p. So relying on put_device to free dev->p prevents such problem.
--- drivers/base/core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/base/core.c +1 -2
@@ -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);
Signature · 1 line
2.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 1/7] device_core: rely on put_device to free dev->p
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
device_addallocateprivate_datafor device and assigns todev->p. Ifdevice_addfails in later steps of the function, it cleans up thisdev->pwhich is not necessary because In the next call,put_devicefree it anyway (if reference to the device is 0 which will be unless someone concurrently get the reference to this device).This avoids unnecessary races introduced in system. After device is added in
device_kset->listbydevice_addand later steps in thedevice_addfunction failures occur, it will freedev->pmanually, while in between there might be a user ofdevice_kset->listwill take reference to the device just added bydevice_add. and might try to accessdev->p. So relying onput_deviceto freedev->pprevents such problem.
Quoted 2 lines
Signed-off-by Tarun Sahu <[email protected]>Signed-off-by David Jeffery <[email protected]>
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
PATCH v19 2/7 driver core: Prevent device_add() during system shutdown
In reply to Tarun SahuIn Async device shutdown, device_kset->list lock is released to handle asynchronisation and hold again to get entry from device_kset->list. Which will leave window when device_add can try to add the device to device_kset list and temper with ongoing shutdown process. New added device can be async type or sync type and might also introduce new dependency which can cause device_shutdown path to deadlock. S is waiting C to finish but C is never scheduled as it was added recently from device_add path. And C can only be scheduled when main loops continue to reach to C which is waiting on S.
So, When a system enters shutdown (SYSTEM_HALT, SYSTEM_POWER_OFF, or SYSTEM_RESTART), new devices should not be allowed to be added. Adding system_state check (system_is_shutting_down()) to avoid device_add incase of these states of the system.
While device_add() performs an initial check of system_is_shutting_down(), a race window exists between this initial check and kobject_add(), during which device_shutdown() may already be scanning devices_kset->list. If device_shutdown() passes the device after kobject_add() registers it onto devices_kset->list, device_add() would otherwise complete device initialization and driver matching, leaving an active device running after system shutdown finishes.
Fix this TOCTOU race by re-checking system_is_shutting_down() under devices_kset->list_lock right after kobject_add().
--- drivers/base/core.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
drivers/base/core.c +34 -0
@@ -47,6 +47,22 @@ static bool fw_devlink_drv_reg_done;static bool fw_devlink_best_effort;static struct workqueue_struct *device_link_wq; +/** +* system_is_shutting_down - Check if system state is not active. +* +* When system state is not active and in shutdown state, new devices +* should not be allowed to be added. +* +* If system_state is SYSTEM_HALT || SYSTEM_POWER_OFF || SYSTEM_RESTART +* this function will return true. +*/ +static inline bool system_is_shutting_down (void ) +{ +return system_state ==SYSTEM_HALT || + system_state ==SYSTEM_POWER_OFF || + system_state ==SYSTEM_RESTART ; +} +/** * __fwnode_link_add - Create a link between two fwnode_handles. * @con: Consumer end of the link.
@@ -3650,6 +3666,11 @@ int device_add(struct device *dev)if (!dev) goto done; +if (unlikely (system_is_shutting_down ())) { + error = -ESHUTDOWN ; + goto done; + } +if (!dev->p) { error =device_private_init (dev);if (error)
@@ -3699,6 +3720,18 @@ int device_add(struct device *dev) goto Error; } +/* +* Check system_state again under list_lock to prevent a TOCTOU race +* where device_shutdown() runs concurrently and misses this device. +*/ +spin_lock (&devices_kset->list_lock); +if (unlikely (system_is_shutting_down ())) { +spin_unlock (&devices_kset->list_lock); + error = -ESHUTDOWN ; + goto ShutdownError; + } +spin_unlock (&devices_kset->list_lock); +/* notify platform of device entry */ device_platform_notify (dev);
@@ -3818,6 +3851,7 @@ int device_add(struct device *dev) attrError:device_platform_notify_remove (dev);kobject_uevent (&dev->kobj,KOBJ_REMOVE ); + ShutdownError: glue_dir =get_glue_dir (dev);kobject_del (&dev->kobj); Error:
Signature · 1 line
2.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 2/7] driver core: Prevent device_add() during system shutdown
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 23 lines
In Async device shutdown,
device_kset->listlock is released to handle asynchronisation and hold again to get entry fromdevice_kset->list. Which will leave window whendevice_addcan try to add the device todevice_ksetlist and temper with ongoing shutdown process. New added device can be async type or sync type and might also introduce new dependency which can causedevice_shutdownpath to deadlock. S is waiting C to finish but C is never scheduled as it was added recently fromdevice_addpath. And C can only be scheduled when main loops continue to reach to C which is waiting on S.So, When a system enters shutdown (
SYSTEM_HALT,SYSTEM_POWER_OFF, orSYSTEM_RESTART), new devices should not be allowed to be added. Addingsystem_statecheck (system_is_shutting_down()) to avoiddevice_addincase of these states of the system.While
device_add()performs an initial check ofsystem_is_shutting_down(), a race window exists between this initial check andkobject_add(), during whichdevice_shutdown()may already be scanningdevices_kset->list. Ifdevice_shutdown()passes the device afterkobject_add()registers it ontodevices_kset->list,device_add()would otherwise complete device initialization and driver matching, leaving an active device running after system shutdown finishes.
Quoted 5 lines
Fix this TOCTOU race by re-checking
system_is_shutting_down()underdevices_kset->list_lockright afterkobject_add().Signed-off-by Tarun Sahu <[email protected]>Signed-off-by David Jeffery <[email protected]>
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
PATCH v19 3/7 driver core: separate function to shutdown one device
In reply to Tarun SahuFrom: David Jeffery <[email protected]>
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
@@ -4898,12 +4898,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 ();
@@ -4925,7 +4961,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
@@ -4934,36 +4970,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.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 3/7] driver core: separate function to shutdown one device
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The refactored `device_shutdown` discards the parent device pointer returned by…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The refactored device_shutdown discards the parent device pointer returned by get_device(dev->parent) and re-reads dev->parent inside shutdown_one_device(), creating a TOCTOU race condition that can lead to reference leaks and use-after-free. --
--- Patch [3]: [PATCH v19 3/7] driver core: separate function to shutdown one device --- commit c3cdb15ac9b6efba6077cade25c652c124eaa9c7
Author: David Jeffery <[email protected]>
driver core: separate function to shutdown one device
This commit refactors device_shutdown() by extracting the shutdown logic for a single device into a new separate function, shutdown_one_device(). This change is intended as a preparatory step for supporting asynchronous device shutdown.
drivers/base/core.c int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) · line 4903
Quoted 11 lines
drivers/base/core.c +3
-0@@ -4898,12 +4898,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;
[Severity: High]
Does this lockless read of dev->parent introduce a race condition?
In the old code, the parent device pointer was captured and pinned in device_shutdown() using parent = get_device(dev->parent).
With this refactoring, dev->parent is re-read here. If the parent changes concurrently (for example, via device_move()), could we end up operating on a parent device that we do not hold a reference to?
Quoted 4 lines
drivers/base/core.c +4
-0+ +/* hold lock to avoid race with probe/release */ +if (parent) +device_lock (parent);
[ ... ]
drivers/base/core.c void device_shutdown(void) · line 4964
Quoted 6 lines
drivers/base/core.c +1
-1@@ -4925,7 +4961,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);
[Severity: High]
Is there a reference leak here since the return value of get_device() is now discarded?
If dev->parent changes right after this call, the original parent's reference will be leaked. Later, shutdown_one_device() will lock and drop a reference to the new parent instead, potentially leading to unbalanced reference counts and use-after-free.
Should we pass the pinned parent device pointer into shutdown_one_device() as an argument to ensure we lock and release the exact same device we took a reference to?
Quoted 3 lines
drivers/base/core.c +0
-0get_device (dev);/* * Make sure the device is off the kset list, in the
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v19 3/7] driver core: separate function to shutdown one device
Quoted 3 lines · On Thu, Jul 16, 2026 at 10:50 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The refactoreddevice_shutdowndiscards the parent device pointer returned byget_device(dev->parent)and re-readsdev->parentinsideshutdown_one_device(), creating a TOCTOU race condition that can lead to reference leaks and use-after-free.
This gets cleaned up in the next patch, but I'll rework it so that this patch has no functional code changes and is only a code move into a separate function.
Quoted 68 lines
--
--- Patch [3]: [PATCH v19 3/7] driver core: separate function to shutdown one device --- commit
c3cdb15ac9b6efba6077cade25c652c124eaa9c7
Author: David Jeffery <[email protected]>driver core: separate function to shutdown one device
This commit refactors
device_shutdown()by extracting the shutdown logic for a single device into a new separate function,shutdown_one_device(). This change is intended as a preparatory step for supporting asynchronous device shutdown.drivers/base/core.c int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid) · line 4903
Quoted 11 lines
drivers/base/core.c +3
-0@@ -4898,12 +4898,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;[Severity: High]
Does this lockless read ofdev->parentintroduce a race condition?In the old code, the parent device pointer was captured and pinned in
device_shutdown()using parent =get_device(dev->parent).With this refactoring,
dev->parentis re-read here. If the parent changes concurrently (for example, viadevice_move()), could we end up operating on a parent device that we do not hold a reference to?Quoted 4 lines
drivers/base/core.c +4
-0+ +/* hold lock to avoid race with probe/release */ +if (parent) +device_lock (parent);[ ... ]
drivers/base/core.c void device_shutdown(void) · line 4964
Quoted 6 lines
drivers/base/core.c +1
-1@@ -4925,7 +4961,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);[Severity: High]
Is there a reference leak here since the return value ofget_device()is now discarded?If
dev->parentchanges right after this call, the original parent's reference will be leaked. Later,shutdown_one_device()will lock and drop a reference to the new parent instead, potentially leading to unbalanced reference counts and use-after-free.Should we pass the pinned parent device pointer into
shutdown_one_device()as an argument to ensure we lock and release the exact same device we took a reference to?drivers/base/core.c void device_shutdown(void) · line 4967
Quoted 3 lines
drivers/base/core.c +0
-0get_device (dev);/* * Make sure the device is off the kset list, in the --
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v19 4/7 driver core: do not always lock parent in shutdown
In reply to Tarun SahuFrom: David Jeffery <[email protected]>
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
@@ -4898,14 +4898,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 || dev->p->dead) +return ;/* Don't allow any more runtime suspends */ pm_runtime_get_noresume (dev);
@@ -4925,12 +4921,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 && !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); }
@@ -4956,12 +4972,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.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 4/7] driver core: do not always lock parent in shutdown
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:
New issues:
- [High] Unprotected read of dev->parent introduces a Use-After-Free race with device_move() during reference acquisition.
Pre-existing issues:
- [High] Dropping and re-acquiring device_lock(dev) allows dev->parent to change, resulting in locking an obsolete parent. --
--- Patch [4]: [PATCH v19 4/7] driver core: do not always lock parent in shutdown --- commit 6be56e5bea0fe214a5199c9f806c49e62e60127f
Author: David Jeffery <[email protected]>
driver core: do not always lock parent in shutdown
This patch modifies the device shutdown process to only lock a parent device if it is strictly needed, rather than unconditionally. It also defers acquiring a reference to the parent device until the point where the parent needs to be locked.
Quoted 4 lines
drivers/base/core.c +0
-0
[ ... ]
drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4929
Quoted 10 lines
drivers/base/core.c +7
-0@@ -4925,12 +4921,32 @@ static void shutdown_one_device(struct device *dev)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 && !dev->p->dead && + (parent =get_device (dev->parent))) {
[Severity: High]
Does moving the parent reference acquisition out of the list_lock critical section introduce a race condition where the parent could be freed?
In device_shutdown(), get_device(dev->parent) was previously called while holding devices_kset->list_lock. Now that it is evaluated locklessly in shutdown_one_device():
CPU1shutdown_one_device()
evaluates dev->parent, but is preempted before get_device() is called
CPU2device_move()
changes dev->parent and calls put_device(old_parent) dropping the last reference and freeing the memory
When CPU1 resumes, could it pass the freed pointer into get_device() and dereference it?
drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4941
Quoted 7 lines
drivers/base/core.c +7
-0+/* the parent lock needs to be acquired first, so re-lock */ +device_unlock (dev); + +device_lock (parent); +device_lock (dev); + +__shutdown_one_device (dev);
[Severity: High]
This is a pre-existing issue, but does dropping and re-acquiring the device lock here allow the topology to change?
If a concurrent device_move() reparents the device while device_lock(dev) is dropped, it appears we might lock the obsolete parent. When we re-lock dev and call __shutdown_one_device(), we haven't verified that dev->parent still matches the locked parent.
Could this defeat the need_parent_lock synchronization and allow a concurrent probe or release on the new parent to race with the shutdown?
Quoted 10 lines
drivers/base/core.c +6
-0+device_unlock (dev);device_unlock (parent); +put_device (parent); + }else { +__shutdown_one_device (dev); +device_unlock (dev); + }put_device (dev); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v19 4/7] driver core: do not always lock parent in shutdown
Quoted 8 lines · On Thu, Jul 16, 2026 at 10:59 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Unprotected read ofdev->parentintroduces a Use-After-Free race withdevice_move()during reference acquisition.Pre-existing issues:
- [High] Dropping and re-acquiringdevice_lock(dev)allowsdev->parentto change, resulting in locking an obsolete parent.
We could add additional locking around device_move to exclude moves during these times to quiet both the AI tool's concerns. However, both these concerns are already not possible in reality. The only user of need_parent_lock in the kernel which will use this code path is usb devices, and none of the rare devices which use device_move will move a usb device which has need_parent_lock set. Thus, there is no actual device_move which can cause these conditions the AI tool complains about.
The AI tool is finding a non-existent issue by applying a very specific special state as a general condition. If really wanted, I could add a patch which locks out moves during these times as a form of future-proofing, but from my view of the code all it would be doing is creating additional code and complexity to the driver core while currently fixing nothing.
Quoted 85 lines
--
--- Patch [4]: [PATCH v19 4/7] driver core: do not always lock parent in shutdown --- commit
6be56e5bea0fe214a5199c9f806c49e62e60127f
Author: David Jeffery <[email protected]>driver core: do not always lock parent in shutdown
This patch modifies the device shutdown process to only lock a parent device if it is strictly needed, rather than unconditionally. It also defers acquiring a reference to the parent device until the point where the parent needs to be locked.
Quoted 4 lines
drivers/base/core.c +0
-0[ ... ]
drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4929
Quoted 10 lines
drivers/base/core.c +7
-0@@ -4925,12 +4921,32 @@ static void shutdown_one_device(struct device *dev)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 && !dev->p->dead && + (parent =get_device (dev->parent))) {[Severity: High]
Does moving the parent reference acquisition out of thelist_lockcritical section introduce a race condition where the parent could be freed?In
device_shutdown(),get_device(dev->parent)was previously called while holdingdevices_kset->list_lock. Now that it is evaluated locklessly inshutdown_one_device():CPU1
shutdown_one_device()evaluates dev->parent, but is preempted before get_device() is calledCPU2
device_move()
changesdev->parentand callsput_device(old_parent)dropping the last reference and freeing the memoryWhen CPU1 resumes, could it pass the freed pointer into
get_device()and dereference it?drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4941
Quoted 7 lines
drivers/base/core.c +7
-0+/* the parent lock needs to be acquired first, so re-lock */ +device_unlock (dev); + +device_lock (parent); +device_lock (dev); + +__shutdown_one_device (dev);[Severity: High]
This is a pre-existing issue, but does dropping and re-acquiring the device lock here allow the topology to change?If a concurrent
device_move()reparents the device whiledevice_lock(dev)is dropped, it appears we might lock the obsolete parent. When we re-lock dev and call__shutdown_one_device(), we haven't verified thatdev->parentstill matches the locked parent.Could this defeat the
need_parent_locksynchronization and allow a concurrent probe or release on the new parent to race with the shutdown?drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4951
Quoted 10 lines
drivers/base/core.c +6
-0+device_unlock (dev);device_unlock (parent); +put_device (parent); + }else { +__shutdown_one_device (dev); +device_unlock (dev); + }put_device (dev); }--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v19 4/7] driver core: do not always lock parent in shutdown
Quoted 19 lines · David Jeffery
Quoted 8 lines · On Thu, Jul 16, 2026 at 10:59 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Unprotected read ofdev->parentintroduces a Use-After-Free race withdevice_move()during reference acquisition.Pre-existing issues:
- [High] Dropping and re-acquiringdevice_lock(dev)allowsdev->parentto change, resulting in locking an obsolete parent.We could add additional locking around
device_moveto exclude moves during these times to quiet both the AI tool's concerns. However, both these concerns are already not possible in reality. The only user ofneed_parent_lockin the kernel which will use this code path is usb devices, and none of the rare devices which usedevice_movewill move a usb device which hasneed_parent_lockset. Thus, there is no actualdevice_movewhich can cause these conditions the AI tool complains about.
Quoted 7 lines
The AI tool is finding a non-existent issue by applying a very specific special state as a general condition. If really wanted, I could add a patch which locks out moves during these times as a form of future-proofing, but from my view of the code all it would be doing is creating additional code and complexity to the driver core while currently fixing nothing.
I agree, it is very special case. device_move is associated with virtual devices only and need_parent_lock is used with phsyical bus (Only with USB) to prevent port reset during device probe/unbind. Even in future, I dont see a need where a virtual device using device_move will need need_parent_lock ever.
But I suggest, We can add the comment like:
/*
* device_move() should not be used on devices with need_parent_lock
* set, as concurrent reparenting during device_shutdown() introduces
* a potential race when locking the parent device.
*/
WDYT?
~Tarun
Quoted 85 lines
Quoted 85 lines
--
--- Patch [4]: [PATCH v19 4/7] driver core: do not always lock parent in shutdown --- commit
6be56e5bea0fe214a5199c9f806c49e62e60127f
Author: David Jeffery <[email protected]>driver core: do not always lock parent in shutdown
This patch modifies the device shutdown process to only lock a parent device if it is strictly needed, rather than unconditionally. It also defers acquiring a reference to the parent device until the point where the parent needs to be locked.
Quoted 4 lines
drivers/base/core.c +0
-0[ ... ]
drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4929
Quoted 10 lines
drivers/base/core.c +7
-0@@ -4925,12 +4921,32 @@ static void shutdown_one_device(struct device *dev)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 && !dev->p->dead && + (parent =get_device (dev->parent))) {[Severity: High]
Does moving the parent reference acquisition out of thelist_lockcritical section introduce a race condition where the parent could be freed?In
device_shutdown(),get_device(dev->parent)was previously called while holdingdevices_kset->list_lock. Now that it is evaluated locklessly inshutdown_one_device():CPU1
shutdown_one_device()evaluates dev->parent, but is preempted before get_device() is calledCPU2
device_move()
changesdev->parentand callsput_device(old_parent)dropping the last reference and freeing the memoryWhen CPU1 resumes, could it pass the freed pointer into
get_device()and dereference it?drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4941
Quoted 7 lines
drivers/base/core.c +7
-0+/* the parent lock needs to be acquired first, so re-lock */ +device_unlock (dev); + +device_lock (parent); +device_lock (dev); + +__shutdown_one_device (dev);[Severity: High]
This is a pre-existing issue, but does dropping and re-acquiring the device lock here allow the topology to change?If a concurrent
device_move()reparents the device whiledevice_lock(dev)is dropped, it appears we might lock the obsolete parent. When we re-lock dev and call__shutdown_one_device(), we haven't verified thatdev->parentstill matches the locked parent.Could this defeat the
need_parent_locksynchronization and allow a concurrent probe or release on the new parent to race with the shutdown?drivers/base/core.c static void shutdown_one_device(struct device *dev) · line 4951
Quoted 10 lines
drivers/base/core.c +6
-0+device_unlock (dev);device_unlock (parent); +put_device (parent); + }else { +__shutdown_one_device (dev); +device_unlock (dev); + }put_device (dev); }--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v19 5/7 driver core: async device shutdown infrastructure
In reply to Tarun SahuFrom: David Jeffery <[email protected]>
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 | 141 +++++++++++++++++- include/linux/device.h | 2 + 4 files changed, 154 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +10 -0
@@ -989,6 +989,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
@@ -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 +140 -1
199 lines not on this page · raw message
include/linux/device.h +2 -0
@@ -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.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 5/7] driver core: async device shutdown infrastructure
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
From: David Jeffery <[email protected]>
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.
Quoted 6 lines
Enabled by default, async shutdown can be explicitly enabled or disabled by using the kernel parameter "core.async_shutdown=<bool>"
Signed-off-by David Jeffery <[email protected]>Signed-off-by Stuart Hayes <[email protected]>Signed-off-by Tarun Sahu <[email protected]>
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=5
PATCH v19 6/7 PCI: Enable async shutdown support
In reply to Tarun SahuFrom: David Jeffery <[email protected]>
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
@@ -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);pci_set_bus_of_node (bus);pci_set_bus_msi_domain (bus); if (bridge->msi_domain && !dev_get_msi_domain(&bus->dev) &&
@@ -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
Signature · 1 line
2.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 6/7] PCI: Enable async shutdown support
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but there is a double-put bug in pci_register_host_bridge()'s error handling path when device_register(&bus->dev) fails, leading to a Use-After-Free and Double Free on the host bridge device. --
--- Patch [6]: [PATCH v19 6/7] PCI: Enable async shutdown support --- commit bf029d21ad5b4df727aa0c1ae7b24c789b0e433a
Author: David Jeffery <[email protected]>
PCI: Enable async shutdown support
Like its async suspend support, allow PCI device shutdown to be performed asynchronously to reduce shutdown time.
drivers/pci/probe.c static int pci_register_host_bridge(struct pci_host_bridge *bridge) · line 1051
Quoted 11 lines
drivers/pci/probe.c +1
-0@@ -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);pci_set_bus_of_node (bus);pci_set_bus_msi_domain (bus);
[Severity: High]
This problem wasn't introduced by this patch, but there appears to be a pre-existing double-put bug in pci_register_host_bridge() when handling registration failures.
Further down in pci_register_host_bridge(), if device_register(&bus->dev) fails, the code jumps to the unregister label:
unregister:
put_device(&bridge->dev);
device_del(&bridge->dev);free:
Does this path drop the caller's initial reference prematurely?
In the free block, because bus_registered is true, it calls put_device(&bus->dev). The device's release function (release_pcibus_dev) explicitly calls put_device(pci_bus->bridge), dropping the reference correctly taken by bus->bridge = get_device(&bridge->dev).
Because the unregister block explicitly drops the caller's initial reference early, the bridge is freed. The function then returns an error to the caller, which will likely call put_device(&bridge->dev) to clean up its initial reference.
Can this sequence cause a double-free and use-after-free on the already freed bridge pointer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v19 7/7 scsi: Enable async shutdown support
In reply to Tarun SahuFrom: David Jeffery <[email protected]>
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
@@ -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
@@ -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.55.0.229.g6434b31f56-goog
[email protected]
AI reviewRe: [PATCH v19 7/7] scsi: Enable async shutdown support
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: David Jeffery <[email protected]>
Like scsi's async suspend support, allow scsi devices to be shut down asynchronously to reduce system shutdown time.
Signed-off-by David Jeffery <[email protected]>Signed-off-by Stuart Hayes <[email protected]>
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=7