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 <[email protected]>
Signed-off-by: David Jeffery <[email protected]>
Tested-by: Laurence Oberman <[email protected]>
---
 drivers/base/core.c | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 74c693cd19cf..3b3d983b1747 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -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
-- 
2.54.0


Reply via email to