diff mbox series

PM: sleep: core: Set is_prepared to false before checking direct_complete

Message ID 20250211034423.833783-1-xu.yang_2@nxp.com (mailing list archive)
State Changes Requested, archived
Headers show
Series PM: sleep: core: Set is_prepared to false before checking direct_complete | expand

Commit Message

Xu Yang Feb. 11, 2025, 3:44 a.m. UTC
Currently, if power.no_callbacks is true for a device, device_prepare()
will also set power.direct_complete to true. When device_resume() check
power.direct_complete, setting power.is_prepared will be skipped if it
can directly complete. This will cause a warning when add new devices
during resume() stage.

Although power.is_prepared should be cleared in complete() state, commit
(f76b168b6f11 PM: Rename dev_pm_info.in_suspend to is_prepared) allow
clear it in earlier resume() stage. However, we need set is_prepared to
false before checking direct_complete after including direct complete
support.

Take USB as example:
The usb_interface is such a device which setting power.no_callbacks to
true. Then if the user call usb_set_interface() during resume() stage,
the kernel will print below warning since the system will create and
add ep devices.

[  186.461414] usb 1-1: reset high-speed USB device number 3 using ci_hdrc
[  187.102681]  ep_81: PM: parent 1-1:1.1 should not be sleeping
[  187.105010] PM: resume devices took 0.936 seconds

Fixes: aae4518b3124 ("PM / sleep: Mechanism to avoid resuming runtime-suspended devices unnecessarily")
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
---
 drivers/base/power/main.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Greg KH Feb. 11, 2025, 6:43 a.m. UTC | #1
On Tue, Feb 11, 2025 at 11:44:23AM +0800, Xu Yang wrote:
> Currently, if power.no_callbacks is true for a device, device_prepare()
> will also set power.direct_complete to true. When device_resume() check
> power.direct_complete, setting power.is_prepared will be skipped if it
> can directly complete. This will cause a warning when add new devices
> during resume() stage.
> 
> Although power.is_prepared should be cleared in complete() state, commit
> (f76b168b6f11 PM: Rename dev_pm_info.in_suspend to is_prepared) allow
> clear it in earlier resume() stage. However, we need set is_prepared to
> false before checking direct_complete after including direct complete
> support.
> 
> Take USB as example:
> The usb_interface is such a device which setting power.no_callbacks to
> true. Then if the user call usb_set_interface() during resume() stage,
> the kernel will print below warning since the system will create and
> add ep devices.
> 
> [  186.461414] usb 1-1: reset high-speed USB device number 3 using ci_hdrc
> [  187.102681]  ep_81: PM: parent 1-1:1.1 should not be sleeping
> [  187.105010] PM: resume devices took 0.936 seconds
> 
> Fixes: aae4518b3124 ("PM / sleep: Mechanism to avoid resuming runtime-suspended devices unnecessarily")
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> ---
>  drivers/base/power/main.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> index 40e1d8d8a589..69d0f9ca7051 100644
> --- a/drivers/base/power/main.c
> +++ b/drivers/base/power/main.c
> @@ -929,6 +929,12 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
>  	if (dev->power.syscore)
>  		goto Complete;
>  
> +	/*
> +	 * This is a fib.  But we'll allow new children to be added below
> +	 * a resumed device, even if the device hasn't been completed yet.
> +	 */
> +	dev->power.is_prepared = false;
> +
>  	if (dev->power.direct_complete) {
>  		/* Match the pm_runtime_disable() in device_suspend(). */
>  		pm_runtime_enable(dev);
> @@ -941,12 +947,6 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
>  	dpm_watchdog_set(&wd, dev);
>  	device_lock(dev);
>  
> -	/*
> -	 * This is a fib.  But we'll allow new children to be added below
> -	 * a resumed device, even if the device hasn't been completed yet.
> -	 */
> -	dev->power.is_prepared = false;
> -
>  	if (!dev->power.is_suspended)
>  		goto Unlock;
>  
> -- 
> 2.34.1
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an
  older released kernel, yet you do not have a cc: stable line in the
  signed-off-by area at all, which means that the patch will not be
  applied to any older kernel releases.  To properly fix this, please
  follow the documented rules in the
  Documentation/process/stable-kernel-rules.rst file for how to resolve
  this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot
Rafael J. Wysocki Feb. 12, 2025, 8:55 p.m. UTC | #2
On Tue, Feb 11, 2025 at 4:43 AM Xu Yang <xu.yang_2@nxp.com> wrote:
>
> Currently, if power.no_callbacks is true for a device, device_prepare()
> will also set power.direct_complete to true. When device_resume() check
> power.direct_complete, setting power.is_prepared will be skipped if it
> can directly complete. This will cause a warning when add new devices
> during resume() stage.
>
> Although power.is_prepared should be cleared in complete() state, commit
> (f76b168b6f11 PM: Rename dev_pm_info.in_suspend to is_prepared) allow
> clear it in earlier resume() stage. However, we need set is_prepared to
> false before checking direct_complete after including direct complete
> support.
>
> Take USB as example:
> The usb_interface is such a device which setting power.no_callbacks to
> true. Then if the user call usb_set_interface() during resume() stage,
> the kernel will print below warning since the system will create and
> add ep devices.
>
> [  186.461414] usb 1-1: reset high-speed USB device number 3 using ci_hdrc
> [  187.102681]  ep_81: PM: parent 1-1:1.1 should not be sleeping
> [  187.105010] PM: resume devices took 0.936 seconds
>
> Fixes: aae4518b3124 ("PM / sleep: Mechanism to avoid resuming runtime-suspended devices unnecessarily")
> Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> ---
>  drivers/base/power/main.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> index 40e1d8d8a589..69d0f9ca7051 100644
> --- a/drivers/base/power/main.c
> +++ b/drivers/base/power/main.c
> @@ -929,6 +929,12 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
>         if (dev->power.syscore)
>                 goto Complete;
>
> +       /*
> +        * This is a fib.  But we'll allow new children to be added below
> +        * a resumed device, even if the device hasn't been completed yet.
> +        */
> +       dev->power.is_prepared = false;

Well, not really.

This is to allow new children to be added from a resume callback, but
direct_complete devices are still in suspend at this point.  You can't
make this change for all of them.

You can clear power.is_prepared for devices with power.no_pm_callbacks
set before the dev->power.syscore check, though.

> +
>         if (dev->power.direct_complete) {
>                 /* Match the pm_runtime_disable() in device_suspend(). */
>                 pm_runtime_enable(dev);
> @@ -941,12 +947,6 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
>         dpm_watchdog_set(&wd, dev);
>         device_lock(dev);
>
> -       /*
> -        * This is a fib.  But we'll allow new children to be added below
> -        * a resumed device, even if the device hasn't been completed yet.
> -        */
> -       dev->power.is_prepared = false;
> -
>         if (!dev->power.is_suspended)
>                 goto Unlock;
>
> --
Xu Yang Feb. 13, 2025, 10:53 a.m. UTC | #3
On Wed, Feb 12, 2025 at 09:55:00PM +0100, Rafael J. Wysocki wrote:
> On Tue, Feb 11, 2025 at 4:43 AM Xu Yang <xu.yang_2@nxp.com> wrote:
> >
> > Currently, if power.no_callbacks is true for a device, device_prepare()
> > will also set power.direct_complete to true. When device_resume() check
> > power.direct_complete, setting power.is_prepared will be skipped if it
> > can directly complete. This will cause a warning when add new devices
> > during resume() stage.
> >
> > Although power.is_prepared should be cleared in complete() state, commit
> > (f76b168b6f11 PM: Rename dev_pm_info.in_suspend to is_prepared) allow
> > clear it in earlier resume() stage. However, we need set is_prepared to
> > false before checking direct_complete after including direct complete
> > support.
> >
> > Take USB as example:
> > The usb_interface is such a device which setting power.no_callbacks to
> > true. Then if the user call usb_set_interface() during resume() stage,
> > the kernel will print below warning since the system will create and
> > add ep devices.
> >
> > [  186.461414] usb 1-1: reset high-speed USB device number 3 using ci_hdrc
> > [  187.102681]  ep_81: PM: parent 1-1:1.1 should not be sleeping
> > [  187.105010] PM: resume devices took 0.936 seconds
> >
> > Fixes: aae4518b3124 ("PM / sleep: Mechanism to avoid resuming runtime-suspended devices unnecessarily")
> > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> > ---
> >  drivers/base/power/main.c | 12 ++++++------
> >  1 file changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> > index 40e1d8d8a589..69d0f9ca7051 100644
> > --- a/drivers/base/power/main.c
> > +++ b/drivers/base/power/main.c
> > @@ -929,6 +929,12 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
> >         if (dev->power.syscore)
> >                 goto Complete;
> >
> > +       /*
> > +        * This is a fib.  But we'll allow new children to be added below
> > +        * a resumed device, even if the device hasn't been completed yet.
> > +        */
> > +       dev->power.is_prepared = false;
> 
> Well, not really.
> 
> This is to allow new children to be added from a resume callback, but
> direct_complete devices are still in suspend at this point.  You can't
> make this change for all of them.
> 
> You can clear power.is_prepared for devices with power.no_pm_callbacks
> set before the dev->power.syscore check, though.

Okay. Thanks for your suggestion.

Thanks,
Xu Yang

> 
> > +
> >         if (dev->power.direct_complete) {
> >                 /* Match the pm_runtime_disable() in device_suspend(). */
> >                 pm_runtime_enable(dev);
> > @@ -941,12 +947,6 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
> >         dpm_watchdog_set(&wd, dev);
> >         device_lock(dev);
> >
> > -       /*
> > -        * This is a fib.  But we'll allow new children to be added below
> > -        * a resumed device, even if the device hasn't been completed yet.
> > -        */
> > -       dev->power.is_prepared = false;
> > -
> >         if (!dev->power.is_suspended)
> >                 goto Unlock;
> >
> > --
Rafael J. Wysocki Feb. 13, 2025, 9:20 p.m. UTC | #4
On Thu, Feb 13, 2025 at 11:56 AM Xu Yang <xu.yang_2@nxp.com> wrote:
>
> On Wed, Feb 12, 2025 at 09:55:00PM +0100, Rafael J. Wysocki wrote:
> > On Tue, Feb 11, 2025 at 4:43 AM Xu Yang <xu.yang_2@nxp.com> wrote:
> > >
> > > Currently, if power.no_callbacks is true for a device, device_prepare()
> > > will also set power.direct_complete to true. When device_resume() check
> > > power.direct_complete, setting power.is_prepared will be skipped if it
> > > can directly complete. This will cause a warning when add new devices
> > > during resume() stage.
> > >
> > > Although power.is_prepared should be cleared in complete() state, commit
> > > (f76b168b6f11 PM: Rename dev_pm_info.in_suspend to is_prepared) allow
> > > clear it in earlier resume() stage. However, we need set is_prepared to
> > > false before checking direct_complete after including direct complete
> > > support.
> > >
> > > Take USB as example:
> > > The usb_interface is such a device which setting power.no_callbacks to
> > > true. Then if the user call usb_set_interface() during resume() stage,
> > > the kernel will print below warning since the system will create and
> > > add ep devices.
> > >
> > > [  186.461414] usb 1-1: reset high-speed USB device number 3 using ci_hdrc
> > > [  187.102681]  ep_81: PM: parent 1-1:1.1 should not be sleeping
> > > [  187.105010] PM: resume devices took 0.936 seconds
> > >
> > > Fixes: aae4518b3124 ("PM / sleep: Mechanism to avoid resuming runtime-suspended devices unnecessarily")
> > > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> > > ---
> > >  drivers/base/power/main.c | 12 ++++++------
> > >  1 file changed, 6 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> > > index 40e1d8d8a589..69d0f9ca7051 100644
> > > --- a/drivers/base/power/main.c
> > > +++ b/drivers/base/power/main.c
> > > @@ -929,6 +929,12 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
> > >         if (dev->power.syscore)
> > >                 goto Complete;
> > >
> > > +       /*
> > > +        * This is a fib.  But we'll allow new children to be added below
> > > +        * a resumed device, even if the device hasn't been completed yet.
> > > +        */
> > > +       dev->power.is_prepared = false;
> >
> > Well, not really.
> >
> > This is to allow new children to be added from a resume callback, but
> > direct_complete devices are still in suspend at this point.  You can't
> > make this change for all of them.
> >
> > You can clear power.is_prepared for devices with power.no_pm_callbacks
> > set before the dev->power.syscore check, though.
>
> Okay. Thanks for your suggestion.

Actually, this is more complicated than I initially thought, sorry about that.

While it is true that is_prepared may be set early for devices without
PM callbacks, in principle they still need to wait for their parents
and suppliers to get ready before this happens because the new
children may depend on the functionality provided by those devices.

However, IIRC in the USB case the new children are added by the parent
of the device in question during the execution of its resume callback,
so is_prepared needs to be set before that callback runs which is not
guaranteed to happen so long as it is done in device_resume().  It
looks like that would need to be done in device_resume_early(), which
then would be questionable because of the above.

I need to think about this some more.
Xu Yang Feb. 14, 2025, 8:58 a.m. UTC | #5
Hi Rafael,

On Thu, Feb 13, 2025 at 10:20:30PM +0100, Rafael J. Wysocki wrote:
> On Thu, Feb 13, 2025 at 11:56 AM Xu Yang <xu.yang_2@nxp.com> wrote:
> >
> > On Wed, Feb 12, 2025 at 09:55:00PM +0100, Rafael J. Wysocki wrote:
> > > On Tue, Feb 11, 2025 at 4:43 AM Xu Yang <xu.yang_2@nxp.com> wrote:
> > > >
> > > > Currently, if power.no_callbacks is true for a device, device_prepare()
> > > > will also set power.direct_complete to true. When device_resume() check
> > > > power.direct_complete, setting power.is_prepared will be skipped if it
> > > > can directly complete. This will cause a warning when add new devices
> > > > during resume() stage.
> > > >
> > > > Although power.is_prepared should be cleared in complete() state, commit
> > > > (f76b168b6f11 PM: Rename dev_pm_info.in_suspend to is_prepared) allow
> > > > clear it in earlier resume() stage. However, we need set is_prepared to
> > > > false before checking direct_complete after including direct complete
> > > > support.
> > > >
> > > > Take USB as example:
> > > > The usb_interface is such a device which setting power.no_callbacks to
> > > > true. Then if the user call usb_set_interface() during resume() stage,
> > > > the kernel will print below warning since the system will create and
> > > > add ep devices.
> > > >
> > > > [  186.461414] usb 1-1: reset high-speed USB device number 3 using ci_hdrc
> > > > [  187.102681]  ep_81: PM: parent 1-1:1.1 should not be sleeping
> > > > [  187.105010] PM: resume devices took 0.936 seconds
> > > >
> > > > Fixes: aae4518b3124 ("PM / sleep: Mechanism to avoid resuming runtime-suspended devices unnecessarily")
> > > > Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
> > > > ---
> > > >  drivers/base/power/main.c | 12 ++++++------
> > > >  1 file changed, 6 insertions(+), 6 deletions(-)
> > > >
> > > > diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
> > > > index 40e1d8d8a589..69d0f9ca7051 100644
> > > > --- a/drivers/base/power/main.c
> > > > +++ b/drivers/base/power/main.c
> > > > @@ -929,6 +929,12 @@ static void device_resume(struct device *dev, pm_message_t state, bool async)
> > > >         if (dev->power.syscore)
> > > >                 goto Complete;
> > > >
> > > > +       /*
> > > > +        * This is a fib.  But we'll allow new children to be added below
> > > > +        * a resumed device, even if the device hasn't been completed yet.
> > > > +        */
> > > > +       dev->power.is_prepared = false;
> > >
> > > Well, not really.
> > >
> > > This is to allow new children to be added from a resume callback, but
> > > direct_complete devices are still in suspend at this point.  You can't
> > > make this change for all of them.
> > >
> > > You can clear power.is_prepared for devices with power.no_pm_callbacks
> > > set before the dev->power.syscore check, though.
> >
> > Okay. Thanks for your suggestion.
> 
> Actually, this is more complicated than I initially thought, sorry about that.

It doesn't matter.

> 
> While it is true that is_prepared may be set early for devices without
> PM callbacks, in principle they still need to wait for their parents
> and suppliers to get ready before this happens because the new
> children may depend on the functionality provided by those devices.
> 
> However, IIRC in the USB case the new children are added by the parent
> of the device in question during the execution of its resume callback,
> so is_prepared needs to be set before that callback runs which is not
> guaranteed to happen so long as it is done in device_resume().  It
> looks like that would need to be done in device_resume_early(), which
> then would be questionable because of the above.
> 
> I need to think about this some more.

Looking forward to a better solution.

Thanks,
Xu Yang
diff mbox series

Patch

diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 40e1d8d8a589..69d0f9ca7051 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -929,6 +929,12 @@  static void device_resume(struct device *dev, pm_message_t state, bool async)
 	if (dev->power.syscore)
 		goto Complete;
 
+	/*
+	 * This is a fib.  But we'll allow new children to be added below
+	 * a resumed device, even if the device hasn't been completed yet.
+	 */
+	dev->power.is_prepared = false;
+
 	if (dev->power.direct_complete) {
 		/* Match the pm_runtime_disable() in device_suspend(). */
 		pm_runtime_enable(dev);
@@ -941,12 +947,6 @@  static void device_resume(struct device *dev, pm_message_t state, bool async)
 	dpm_watchdog_set(&wd, dev);
 	device_lock(dev);
 
-	/*
-	 * This is a fib.  But we'll allow new children to be added below
-	 * a resumed device, even if the device hasn't been completed yet.
-	 */
-	dev->power.is_prepared = false;
-
 	if (!dev->power.is_suspended)
 		goto Unlock;