[PATCH v10 2/5] driver core: don't always lock parent in shutdown

Stuart Hayes posted 5 patches 3 months, 2 weeks ago
[PATCH v10 2/5] driver core: don't always lock parent in shutdown
Posted by Stuart Hayes 3 months, 2 weeks ago
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.

Signed-off-by: Stuart Hayes <stuart.w.hayes@gmail.com>
Signed-off-by: David Jeffery <djeffery@redhat.com>
---
 drivers/base/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index cbc0099d8ef2..58c772785606 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -4823,7 +4823,7 @@ void device_shutdown(void)
 		spin_unlock(&devices_kset->list_lock);
 
 		/* hold lock to avoid race with probe/release */
-		if (parent)
+		if (parent && dev->bus && dev->bus->need_parent_lock)
 			device_lock(parent);
 		device_lock(dev);
 
@@ -4847,7 +4847,7 @@ void device_shutdown(void)
 		}
 
 		device_unlock(dev);
-		if (parent)
+		if (parent && dev->bus && dev->bus->need_parent_lock)
 			device_unlock(parent);
 
 		put_device(dev);
-- 
2.39.3
Re: [PATCH v10 2/5] driver core: don't always lock parent in shutdown
Posted by Greg Kroah-Hartman 3 months, 1 week ago
On Wed, Jun 25, 2025 at 03:18:50PM -0500, Stuart Hayes wrote:
> 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.
> 
> Signed-off-by: Stuart Hayes <stuart.w.hayes@gmail.com>
> Signed-off-by: David Jeffery <djeffery@redhat.com>
> ---
>  drivers/base/core.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index cbc0099d8ef2..58c772785606 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -4823,7 +4823,7 @@ void device_shutdown(void)
>  		spin_unlock(&devices_kset->list_lock);
>  
>  		/* hold lock to avoid race with probe/release */
> -		if (parent)
> +		if (parent && dev->bus && dev->bus->need_parent_lock)
>  			device_lock(parent);

What about parents for a device that is not on a bus?  Don't they need
to be properly locked?


>  		device_lock(dev);
>  
> @@ -4847,7 +4847,7 @@ void device_shutdown(void)
>  		}
>  
>  		device_unlock(dev);
> -		if (parent)
> +		if (parent && dev->bus && dev->bus->need_parent_lock)
>  			device_unlock(parent);

Same here.

thanks,

greg k-h
Re: [PATCH v10 2/5] driver core: don't always lock parent in shutdown
Posted by David Jeffery 3 months, 1 week ago
On Tue, Jul 1, 2025 at 5:03 AM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Wed, Jun 25, 2025 at 03:18:50PM -0500, Stuart Hayes wrote:
> > 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.
> >
> > Signed-off-by: Stuart Hayes <stuart.w.hayes@gmail.com>
> > Signed-off-by: David Jeffery <djeffery@redhat.com>
> > ---
> >  drivers/base/core.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/base/core.c b/drivers/base/core.c
> > index cbc0099d8ef2..58c772785606 100644
> > --- a/drivers/base/core.c
> > +++ b/drivers/base/core.c
> > @@ -4823,7 +4823,7 @@ void device_shutdown(void)
> >               spin_unlock(&devices_kset->list_lock);
> >
> >               /* hold lock to avoid race with probe/release */
> > -             if (parent)
> > +             if (parent && dev->bus && dev->bus->need_parent_lock)
> >                       device_lock(parent);
>
> What about parents for a device that is not on a bus?  Don't they need
> to be properly locked?

From my examination of the code and history, I do not believe so.
Locking the parent was added before need_parent_lock was added, and
when the other locations changed to depend on need_parent_lock to lock
both, device_shutdown was left always locking both.

It is simple enough to change the if checks to:

if (parent && (!dev->bus || dev->bus->need_parent_lock))

if you think my understanding is wrong and some bus-less devices have
come to depend on the behavior.

David Jeffery