linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] PM-runtime: fix deadlock when canceling hrtimer
@ 2019-02-21  7:59 Vincent Guittot
  2019-02-21  9:35 ` Rafael J. Wysocki
  0 siblings, 1 reply; 3+ messages in thread
From: Vincent Guittot @ 2019-02-21  7:59 UTC (permalink / raw)
  To: linux-pm, linux-kernel, rjw, ulf.hansson; +Cc: sunzhaosheng, Vincent Guittot

When rpm_resume() desactivates the autosuspend timer, it should only try
to cancel hrtimer but not wait for the handler to finish because both
rpm_resume() and pm_suspend_timer_fn() are taking the power.lock.
We can have the deadlock sequence:
CPU0                              CPU1
rpm_resume()
  spin_lock_irqsave
                                  pm_suspend_timer_fn()
                                    spin_lock_irqsave
  pm_runtime_deactivate_timer()
    hrtimer_cancel()

hrtimer_try_to_cancel() is enough because dev->power.timer_expires is also
set to 0.

Fixes: 8234f6734c5d ("PM-runtime: Switch autosuspend over to using hrtimers")
Reported-by: Sunzhaosheng Sun(Zhaosheng) <sunzhaosheng@hisilicon.com>
Signed-off-by: Vincent Guittot <vincent.guittot@linaro.org>
---
 drivers/base/power/runtime.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
index 04407d9..78937c4 100644
--- a/drivers/base/power/runtime.c
+++ b/drivers/base/power/runtime.c
@@ -121,7 +121,7 @@ EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
 static void pm_runtime_deactivate_timer(struct device *dev)
 {
 	if (dev->power.timer_expires > 0) {
-		hrtimer_cancel(&dev->power.suspend_timer);
+		hrtimer_try_to_cancel(&dev->power.suspend_timer);
 		dev->power.timer_expires = 0;
 	}
 }
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] PM-runtime: fix deadlock when canceling hrtimer
  2019-02-21  7:59 [PATCH] PM-runtime: fix deadlock when canceling hrtimer Vincent Guittot
@ 2019-02-21  9:35 ` Rafael J. Wysocki
  2019-02-21 10:02   ` Vincent Guittot
  0 siblings, 1 reply; 3+ messages in thread
From: Rafael J. Wysocki @ 2019-02-21  9:35 UTC (permalink / raw)
  To: Vincent Guittot
  Cc: Linux PM, Linux Kernel Mailing List, Rafael J. Wysocki,
	Ulf Hansson, Zhaosheng Sun

On Thu, Feb 21, 2019 at 8:59 AM Vincent Guittot
<vincent.guittot@linaro.org> wrote:
>
> When rpm_resume() desactivates the autosuspend timer, it should only try
> to cancel hrtimer but not wait for the handler to finish because both
> rpm_resume() and pm_suspend_timer_fn() are taking the power.lock.
> We can have the deadlock sequence:
> CPU0                              CPU1
> rpm_resume()
>   spin_lock_irqsave
>                                   pm_suspend_timer_fn()
>                                     spin_lock_irqsave
>   pm_runtime_deactivate_timer()
>     hrtimer_cancel()

Everybody has missed this, not good.

> hrtimer_try_to_cancel() is enough because dev->power.timer_expires is also
> set to 0.
>
> Fixes: 8234f6734c5d ("PM-runtime: Switch autosuspend over to using hrtimers")
> Reported-by: Sunzhaosheng Sun(Zhaosheng) <sunzhaosheng@hisilicon.com>
> Signed-off-by: Vincent Guittot <vincent.guittot@linaro.org>
> ---
>  drivers/base/power/runtime.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> index 04407d9..78937c4 100644
> --- a/drivers/base/power/runtime.c
> +++ b/drivers/base/power/runtime.c
> @@ -121,7 +121,7 @@ EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
>  static void pm_runtime_deactivate_timer(struct device *dev)
>  {
>         if (dev->power.timer_expires > 0) {
> -               hrtimer_cancel(&dev->power.suspend_timer);
> +               hrtimer_try_to_cancel(&dev->power.suspend_timer);
>                 dev->power.timer_expires = 0;
>         }
>  }
> --

Applied with some changelog modifications, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] PM-runtime: fix deadlock when canceling hrtimer
  2019-02-21  9:35 ` Rafael J. Wysocki
@ 2019-02-21 10:02   ` Vincent Guittot
  0 siblings, 0 replies; 3+ messages in thread
From: Vincent Guittot @ 2019-02-21 10:02 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Linux PM, Linux Kernel Mailing List, Rafael J. Wysocki,
	Ulf Hansson, Zhaosheng Sun

On Thu, 21 Feb 2019 at 10:35, Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Thu, Feb 21, 2019 at 8:59 AM Vincent Guittot
> <vincent.guittot@linaro.org> wrote:
> >
> > When rpm_resume() desactivates the autosuspend timer, it should only try
> > to cancel hrtimer but not wait for the handler to finish because both
> > rpm_resume() and pm_suspend_timer_fn() are taking the power.lock.
> > We can have the deadlock sequence:
> > CPU0                              CPU1
> > rpm_resume()
> >   spin_lock_irqsave
> >                                   pm_suspend_timer_fn()
> >                                     spin_lock_irqsave
> >   pm_runtime_deactivate_timer()
> >     hrtimer_cancel()
>
> Everybody has missed this, not good.

yes. default behavior if different between timer and hrtimer
del_timer() is equivalent to hrtimer_try_to_cancel()
and hrtimer_cancel() to del_timer_sync()

>
> > hrtimer_try_to_cancel() is enough because dev->power.timer_expires is also
> > set to 0.
> >
> > Fixes: 8234f6734c5d ("PM-runtime: Switch autosuspend over to using hrtimers")
> > Reported-by: Sunzhaosheng Sun(Zhaosheng) <sunzhaosheng@hisilicon.com>
> > Signed-off-by: Vincent Guittot <vincent.guittot@linaro.org>
> > ---
> >  drivers/base/power/runtime.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> > index 04407d9..78937c4 100644
> > --- a/drivers/base/power/runtime.c
> > +++ b/drivers/base/power/runtime.c
> > @@ -121,7 +121,7 @@ EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
> >  static void pm_runtime_deactivate_timer(struct device *dev)
> >  {
> >         if (dev->power.timer_expires > 0) {
> > -               hrtimer_cancel(&dev->power.suspend_timer);
> > +               hrtimer_try_to_cancel(&dev->power.suspend_timer);
> >                 dev->power.timer_expires = 0;
> >         }
> >  }
> > --
>
> Applied with some changelog modifications, thanks!

Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-02-21 10:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-21  7:59 [PATCH] PM-runtime: fix deadlock when canceling hrtimer Vincent Guittot
2019-02-21  9:35 ` Rafael J. Wysocki
2019-02-21 10:02   ` Vincent Guittot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).