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

From: David Jeffery

Date: Fri Aug 21 2026 - 10:28:39 EST


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.

Signed-off-by: Stuart Hayes <stuart.w.hayes@xxxxxxxxx>
Signed-off-by: David Jeffery <djeffery@xxxxxxxxxx>
Signed-off-by: Tarun Sahu <tarunsahu@xxxxxxxxxx>
Tested-by: Laurence Oberman <loberman@xxxxxxxxxx>
---

The sashiko patch analysis may complain about the parent locking and
possible use of device_move. This issue cannot currently happen as
devices with need_parent_lock do not use device_move. An earlier patch
also adds a warning for this condition should some future code change
violate the incompatibility between need_parent_lock and device_move.


drivers/base/core.c | 44 ++++++++++++++++++++++++++++----------------
1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index a94d24b30c30..cd725466c32d 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -4910,12 +4910,10 @@ 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)
+static void __shutdown_one_device(struct device *dev)
{
- /* 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);
@@ -4935,13 +4933,33 @@ static void shutdown_one_device(struct device *dev, struct device *parent)
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(dev);
- put_device(parent);
}

/**
@@ -4949,7 +4967,7 @@ static void shutdown_one_device(struct device *dev, struct device *parent)
*/
void device_shutdown(void)
{
- struct device *dev, *parent;
+ struct device *dev;

wait_for_device_probe();
device_block_probing();
@@ -4966,12 +4984,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
- */
- parent = get_device(dev->parent);
get_device(dev);
/*
* Make sure the device is off the kset list, in the
@@ -4980,7 +4992,7 @@ void device_shutdown(void)
list_del_init(&dev->kobj.entry);
spin_unlock(&devices_kset->list_lock);

- shutdown_one_device(dev, parent);
+ shutdown_one_device(dev);

spin_lock(&devices_kset->list_lock);
}
--
2.55.0