public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend.
@ 2012-07-30  1:35 NeilBrown
  2012-07-30 21:01 ` Rafael J. Wysocki
  0 siblings, 1 reply; 4+ messages in thread
From: NeilBrown @ 2012-07-30  1:35 UTC (permalink / raw)
  To: Alessandro Zummo; +Cc: rtc-linux, linux-pm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1585 bytes --]


If an RTC alarm fires just as suspend is happening, it is possible for
suspend to complete and the alarm to be missed.

To avoid the race, we must register the event with the PM core.

As the event is made visible to userspace through a thread which is
only scheduled by the interrupt, we need a pm_stay_awake/pm_relax
pair preventing suspend from the interrupt until the thread completes
its work.

Signed-off-by: NeilBrown <neilb@suse.de>

--
This makes the pm_wakeup_event() call in cmos_interrupt unnecessary as it
provides suspend protection for all RTCs that use rtc_update_irq.

I think the pm_stay_awake//pm_relax is needed - just pm_wakup_event() is 
theoretically not sufficient.

This is because there is no guarantee (that I know of) that the workqueue
thread will actually get scheduled before 'suspend' takes over.

Thanks,
NeilBrown


diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index eb415bd..9592b93 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -582,6 +582,7 @@ enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
 void rtc_update_irq(struct rtc_device *rtc,
 		unsigned long num, unsigned long events)
 {
+	pm_stay_awake(rtc->dev.parent);
 	schedule_work(&rtc->irqwork);
 }
 EXPORT_SYMBOL_GPL(rtc_update_irq);
@@ -844,6 +845,7 @@ void rtc_timer_do_work(struct work_struct *work)
 
 	mutex_lock(&rtc->ops_lock);
 again:
+	pm_relax(rtc->dev.parent);
 	__rtc_read_time(rtc, &tm);
 	now = rtc_tm_to_ktime(tm);
 	while ((next = timerqueue_getnext(&rtc->timerqueue))) {

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 828 bytes --]

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

* Re: [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend.
  2012-07-30  1:35 [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend NeilBrown
@ 2012-07-30 21:01 ` Rafael J. Wysocki
  2012-07-31  2:51   ` NeilBrown
  0 siblings, 1 reply; 4+ messages in thread
From: Rafael J. Wysocki @ 2012-07-30 21:01 UTC (permalink / raw)
  To: NeilBrown; +Cc: Alessandro Zummo, rtc-linux, linux-pm, linux-kernel

On Monday, July 30, 2012, NeilBrown wrote:
> 
> If an RTC alarm fires just as suspend is happening, it is possible for
> suspend to complete and the alarm to be missed.
> 
> To avoid the race, we must register the event with the PM core.
> 
> As the event is made visible to userspace through a thread which is
> only scheduled by the interrupt, we need a pm_stay_awake/pm_relax
> pair preventing suspend from the interrupt until the thread completes
> its work.
> 
> Signed-off-by: NeilBrown <neilb@suse.de>
> 
> --
> This makes the pm_wakeup_event() call in cmos_interrupt unnecessary as it
> provides suspend protection for all RTCs that use rtc_update_irq.

Care to remove the call in cmos_interrupt(), then?

> I think the pm_stay_awake//pm_relax is needed - just pm_wakup_event() is 
> theoretically not sufficient.
> 
> This is because there is no guarantee (that I know of) that the workqueue
> thread will actually get scheduled before 'suspend' takes over.

I think you are right.

Thanks,
Rafael


> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index eb415bd..9592b93 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -582,6 +582,7 @@ enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
>  void rtc_update_irq(struct rtc_device *rtc,
>  		unsigned long num, unsigned long events)
>  {
> +	pm_stay_awake(rtc->dev.parent);
>  	schedule_work(&rtc->irqwork);
>  }
>  EXPORT_SYMBOL_GPL(rtc_update_irq);
> @@ -844,6 +845,7 @@ void rtc_timer_do_work(struct work_struct *work)
>  
>  	mutex_lock(&rtc->ops_lock);
>  again:
> +	pm_relax(rtc->dev.parent);
>  	__rtc_read_time(rtc, &tm);
>  	now = rtc_tm_to_ktime(tm);
>  	while ((next = timerqueue_getnext(&rtc->timerqueue))) {
> 


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

* Re: [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend.
  2012-07-30 21:01 ` Rafael J. Wysocki
@ 2012-07-31  2:51   ` NeilBrown
  2012-08-05 21:11     ` Rafael J. Wysocki
  0 siblings, 1 reply; 4+ messages in thread
From: NeilBrown @ 2012-07-31  2:51 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Alessandro Zummo, rtc-linux, linux-pm, linux-kernel, Paul Fox

[-- Attachment #1: Type: text/plain, Size: 2999 bytes --]

On Mon, 30 Jul 2012 23:01:49 +0200 "Rafael J. Wysocki" <rjw@sisk.pl> wrote:

> On Monday, July 30, 2012, NeilBrown wrote:
> > 
> > If an RTC alarm fires just as suspend is happening, it is possible for
> > suspend to complete and the alarm to be missed.
> > 
> > To avoid the race, we must register the event with the PM core.
> > 
> > As the event is made visible to userspace through a thread which is
> > only scheduled by the interrupt, we need a pm_stay_awake/pm_relax
> > pair preventing suspend from the interrupt until the thread completes
> > its work.
> > 
> > Signed-off-by: NeilBrown <neilb@suse.de>
> > 
> > --
> > This makes the pm_wakeup_event() call in cmos_interrupt unnecessary as it
> > provides suspend protection for all RTCs that use rtc_update_irq.
> 
> Care to remove the call in cmos_interrupt(), then?
> 
> > I think the pm_stay_awake//pm_relax is needed - just pm_wakup_event() is 
> > theoretically not sufficient.
> > 
> > This is because there is no guarantee (that I know of) that the workqueue
> > thread will actually get scheduled before 'suspend' takes over.
> 
> I think you are right.

Thanks.
Here is the revised patch.

NeilBrown


==========
Subject: [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend.

If an RTC alarm fires just as suspend is happening, it is possible for
suspend to complete and the alarm to be missed.

To avoid the race, we must register the event with the PM core.

As the event is made visible to userspace through a thread which is
only scheduled by the interrupt, we need a pm_stay_awake/pm_relax
pair preventing suspend from the interrupt until the thread completes
its work.

This makes the pm_wakeup_event() call in cmos_interrupt unnecessary as
it provides suspend protection for all RTCs that use rtc_update_irq.

Cc: Paul Fox <pgf@laptop.org>
Signed-off-by: NeilBrown <neilb@suse.de>

diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index eb415bd..9592b93 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -582,6 +582,7 @@ enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
 void rtc_update_irq(struct rtc_device *rtc,
 		unsigned long num, unsigned long events)
 {
+	pm_stay_awake(rtc->dev.parent);
 	schedule_work(&rtc->irqwork);
 }
 EXPORT_SYMBOL_GPL(rtc_update_irq);
@@ -844,6 +845,7 @@ void rtc_timer_do_work(struct work_struct *work)
 
 	mutex_lock(&rtc->ops_lock);
 again:
+	pm_relax(rtc->dev.parent);
 	__rtc_read_time(rtc, &tm);
 	now = rtc_tm_to_ktime(tm);
 	while ((next = timerqueue_getnext(&rtc->timerqueue))) {
diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index 132333d..4267789 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -568,7 +568,6 @@ static irqreturn_t cmos_interrupt(int irq, void *p)
 		hpet_mask_rtc_irq_bit(RTC_AIE);
 
 		CMOS_READ(RTC_INTR_FLAGS);
-		pm_wakeup_event(cmos_rtc.dev, 0);
 	}
 	spin_unlock(&rtc_lock);
 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 828 bytes --]

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

* Re: [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend.
  2012-07-31  2:51   ` NeilBrown
@ 2012-08-05 21:11     ` Rafael J. Wysocki
  0 siblings, 0 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2012-08-05 21:11 UTC (permalink / raw)
  To: NeilBrown; +Cc: Alessandro Zummo, rtc-linux, linux-pm, linux-kernel, Paul Fox

On Tuesday, July 31, 2012, NeilBrown wrote:
> On Mon, 30 Jul 2012 23:01:49 +0200 "Rafael J. Wysocki" <rjw@sisk.pl> wrote:
> 
> > On Monday, July 30, 2012, NeilBrown wrote:
> > > 
> > > If an RTC alarm fires just as suspend is happening, it is possible for
> > > suspend to complete and the alarm to be missed.
> > > 
> > > To avoid the race, we must register the event with the PM core.
> > > 
> > > As the event is made visible to userspace through a thread which is
> > > only scheduled by the interrupt, we need a pm_stay_awake/pm_relax
> > > pair preventing suspend from the interrupt until the thread completes
> > > its work.
> > > 
> > > Signed-off-by: NeilBrown <neilb@suse.de>
> > > 
> > > --
> > > This makes the pm_wakeup_event() call in cmos_interrupt unnecessary as it
> > > provides suspend protection for all RTCs that use rtc_update_irq.
> > 
> > Care to remove the call in cmos_interrupt(), then?
> > 
> > > I think the pm_stay_awake//pm_relax is needed - just pm_wakup_event() is 
> > > theoretically not sufficient.
> > > 
> > > This is because there is no guarantee (that I know of) that the workqueue
> > > thread will actually get scheduled before 'suspend' takes over.
> > 
> > I think you are right.
> 
> Thanks.
> Here is the revised patch.
> 
> NeilBrown
> 
> 
> ==========
> Subject: [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend.
> 
> If an RTC alarm fires just as suspend is happening, it is possible for
> suspend to complete and the alarm to be missed.
> 
> To avoid the race, we must register the event with the PM core.
> 
> As the event is made visible to userspace through a thread which is
> only scheduled by the interrupt, we need a pm_stay_awake/pm_relax
> pair preventing suspend from the interrupt until the thread completes
> its work.
> 
> This makes the pm_wakeup_event() call in cmos_interrupt unnecessary as
> it provides suspend protection for all RTCs that use rtc_update_irq.
> 
> Cc: Paul Fox <pgf@laptop.org>
> Signed-off-by: NeilBrown <neilb@suse.de>

Applied to the linux-next branch of the linux-pm.git tree.  I'm going to
push it as a fix for v3.6.

Thanks,
Rafael


> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index eb415bd..9592b93 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -582,6 +582,7 @@ enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
>  void rtc_update_irq(struct rtc_device *rtc,
>  		unsigned long num, unsigned long events)
>  {
> +	pm_stay_awake(rtc->dev.parent);
>  	schedule_work(&rtc->irqwork);
>  }
>  EXPORT_SYMBOL_GPL(rtc_update_irq);
> @@ -844,6 +845,7 @@ void rtc_timer_do_work(struct work_struct *work)
>  
>  	mutex_lock(&rtc->ops_lock);
>  again:
> +	pm_relax(rtc->dev.parent);
>  	__rtc_read_time(rtc, &tm);
>  	now = rtc_tm_to_ktime(tm);
>  	while ((next = timerqueue_getnext(&rtc->timerqueue))) {
> diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
> index 132333d..4267789 100644
> --- a/drivers/rtc/rtc-cmos.c
> +++ b/drivers/rtc/rtc-cmos.c
> @@ -568,7 +568,6 @@ static irqreturn_t cmos_interrupt(int irq, void *p)
>  		hpet_mask_rtc_irq_bit(RTC_AIE);
>  
>  		CMOS_READ(RTC_INTR_FLAGS);
> -		pm_wakeup_event(cmos_rtc.dev, 0);
>  	}
>  	spin_unlock(&rtc_lock);
>  
> 


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

end of thread, other threads:[~2012-08-05 21:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-30  1:35 [PATCH] RTC: Avoid races between RTC alarm wakeup and suspend NeilBrown
2012-07-30 21:01 ` Rafael J. Wysocki
2012-07-31  2:51   ` NeilBrown
2012-08-05 21:11     ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox