diff mbox

[RFC,v2,1/2] rtc-cmos: Clear interrupt flag if alarm time is

Message ID 1433845413-13960-2-git-send-email-adrianhuang0701@gmail.com
State Superseded
Headers show

Commit Message

Huang Adrian June 9, 2015, 10:23 a.m. UTC
Steps to reproduce the problem:
	1) Enable RTC wake-up option in BIOS Setup
	2) Issue one of these commands in the OS: "poweroff"
	   or "shutdown -h now"
	3) System will shut down and then reboot automatically

Root-cause of the issue:
	1) During the shutdown process, the hwclock utility is used
	   to save the system clock to hardware clock (RTC).
	2) The hwclock utility invokes ioctl() with RTC_UIE_ON. The
	   kernel configures the RTC alarm for the periodic interrupt
	   (every 1 second).
	3) The hwclock uitlity closes the /dev/rtc0 device, and the
	   kernel disables the RTC alarm irq (AIE bit of Register B)
	   via ioctl() with RTC_UIE_OFF. But, the configured alarm
	   time is the current_time + 1.
	4) After the next 1 second is elapsed, the AF (alarm
	   interrupt flag) of Register C is set.
	5) The S5 handler in BIOS is invoked to configure alarm
	   registers (enable AIE bit and configure alarm date/time).
	   But, BIOS does not clear the previous interrupt status
	   during alarm configuration. Therefore, "AF=AIE=1" causes
	   the rtc device to trigger an interrupt.
	6) So, the machine reboots automatically right after shutdown.

This patch makes sure cmos_do_shutdown() is invoked if the configured
alarm time is less equal to current_time + 1 seconds. Therefore,
the interrupt flag can be cleared before powering off the system.

The member 'alarm_expires' is introduced in struct cmos_rtc because
of the following reasons:
	1) The configured alarm time can be retrieved from
	   cmos_read_alarm(), but we need to take the 'wrapped
	   timestamp' and 'time rollover' into consideration. The
	   function __rtc_read_alarm() eliminates the concerns. To
	   avoid the duplicated code in the lower level RTC driver,
	   invoking __rtc_read_alarm from the lower level RTC driver
	   is not encouraged. Moreover, the compilation error 'the
	   undefined __rtc_read_alarm" is observed if the lower level
	   RTC driver is compiled as a kernel module.
	2) The uie_rtctimer.node.expires and aie_timer.node.expires can
	   be retrieved for the configured alarm time. But, the problem
	   is that either of them might configure the CMOS alarm time.
	   We cannot make sure who configured the CMOS alarm time
	   before (uie_rtctimer or aie_timer is enabled and then is
	   disabled).
	3) The patch introduces the member 'alarm_expires' to keep the
	   newly configured alarm time, so the above-mentioned concerns
	   can be eliminated.

The issue goes away after 20-time shutdown tests.

Signed-off-by: Adrian Huang <ahuang12@lenovo.com>
Reviewed-by: Max Asbock <amax@lenovo.com>
---
 drivers/rtc/rtc-cmos.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 60 insertions(+), 5 deletions(-)

Comments

Huang Adrian June 10, 2015, 5:51 a.m. UTC | #1
Hi Alexandre,

This reworked patch is based on your suggestion. Could you kindly
review it? If it looks good to you, I'll ask Borislav, Diego and
Egbert a favor for testing.

Thanks in advance.

-- Adrian

> ---
>  drivers/rtc/rtc-cmos.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 60 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
> index a82556a..4e60ac8 100644
> --- a/drivers/rtc/rtc-cmos.c
> +++ b/drivers/rtc/rtc-cmos.c
> @@ -51,6 +51,7 @@ struct cmos_rtc {
>         struct device           *dev;
>         int                     irq;
>         struct resource         *iomem;
> +       time64_t                alarm_expires;
>
>         void                    (*wake_on)(struct device *);
>         void                    (*wake_off)(struct device *);
> @@ -377,6 +378,8 @@ static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
>
>         spin_unlock_irq(&rtc_lock);
>
> +       cmos->alarm_expires = rtc_tm_to_time64(&t->time);
> +
>         return 0;
>  }
>
> @@ -588,7 +591,7 @@ static struct bin_attribute nvram = {
>
>  /*----------------------------------------------------------------*/
>
> -static struct cmos_rtc cmos_rtc;
> +static struct cmos_rtc cmos_rtc = { .alarm_expires = 0 };
>
>  static irqreturn_t cmos_interrupt(int irq, void *p)
>  {
> @@ -860,6 +863,52 @@ static void __exit cmos_do_remove(struct device *dev)
>         cmos->dev = NULL;
>  }
>
> +static int cmos_aie_poweroff(struct device *dev)
> +{
> +       struct cmos_rtc *cmos = dev_get_drvdata(dev);
> +       struct rtc_time now;
> +       int err;
> +       time64_t t_now;
> +       unsigned char rtc_control;
> +
> +       if (!cmos->alarm_expires)
> +               return -EINVAL;
> +
> +       spin_lock_irq(&rtc_lock);
> +       rtc_control = CMOS_READ(RTC_CONTROL);
> +       spin_unlock_irq(&rtc_lock);
> +
> +       /* We only care about the situation where AIE is disabled. */
> +       if (rtc_control & RTC_AIE)
> +               return -EBUSY;
> +
> +       err = cmos_read_time(dev, &now);
> +       if (err < 0)
> +               return err;
> +       t_now = rtc_tm_to_time64(&now);
> +
> +       /*
> +        * When enabling "RTC wake-up" in BIOS setup, the machine reboots
> +        * automatically right after shutdown on some buggy boxes.
> +        * This automatic rebooting issue won't happen when the alarm
> +        * time is larger than now+1 seconds.
> +        *
> +        * If the alarm time is equal to now+1 seconds, we need to wait
> +        * for 1 second so that AF is set by CMOS hardware. Therefore, we
> +        * can get a chance to clear AF in cmos_do_shutdown().
> +        *
> +        * If the alarm time is less equal to 'now', the function simply
> +        * returns '0' so that we can get a chance to clear AF in
> +        * cmos_do_shutdown().
> +        */
> +       if (cmos->alarm_expires > (t_now + 1))
> +               return -EBUSY;
> +       else if (cmos->alarm_expires == (t_now + 1))
> +               msleep(1000);
> +
> +       return 0;
> +}
> +
>  #ifdef CONFIG_PM
>
>  static int cmos_suspend(struct device *dev)
> @@ -1094,8 +1143,11 @@ static void cmos_pnp_shutdown(struct pnp_dev *pnp)
>         struct device *dev = &pnp->dev;
>         struct cmos_rtc *cmos = dev_get_drvdata(dev);
>
> -       if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
> -               return;
> +       if (system_state == SYSTEM_POWER_OFF) {
> +               cmos_poweroff(dev);
> +               if (cmos_aie_poweroff(dev) < 0)
> +                       return;
> +       }
>
>         cmos_do_shutdown(cmos->irq);
>  }
> @@ -1200,8 +1252,11 @@ static void cmos_platform_shutdown(struct platform_device *pdev)
>         struct device *dev = &pdev->dev;
>         struct cmos_rtc *cmos = dev_get_drvdata(dev);
>
> -       if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
> -               return;
> +       if (system_state == SYSTEM_POWER_OFF) {
> +               cmos_poweroff(dev);
> +               if (cmos_aie_poweroff(dev) < 0)
> +                       return;
> +       }
>
>         cmos_do_shutdown(cmos->irq);
>  }
> --
> 1.9.1
>
Diego Ercolani June 10, 2015, 7:22 a.m. UTC | #2
No problem to me to test, please give a build for openSUSE_13.2 so I can
test on my system
Thank you

2015-06-10 7:51 GMT+02:00 Huang Adrian <adrianhuang0701@gmail.com>:

> Hi Alexandre,
>
> This reworked patch is based on your suggestion. Could you kindly
> review it? If it looks good to you, I'll ask Borislav, Diego and
> Egbert a favor for testing.
>
> Thanks in advance.
>
> -- Adrian
>
> > ---
> >  drivers/rtc/rtc-cmos.c | 65
> ++++++++++++++++++++++++++++++++++++++++++++++----
> >  1 file changed, 60 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
> > index a82556a..4e60ac8 100644
> > --- a/drivers/rtc/rtc-cmos.c
> > +++ b/drivers/rtc/rtc-cmos.c
> > @@ -51,6 +51,7 @@ struct cmos_rtc {
> >         struct device           *dev;
> >         int                     irq;
> >         struct resource         *iomem;
> > +       time64_t                alarm_expires;
> >
> >         void                    (*wake_on)(struct device *);
> >         void                    (*wake_off)(struct device *);
> > @@ -377,6 +378,8 @@ static int cmos_set_alarm(struct device *dev, struct
> rtc_wkalrm *t)
> >
> >         spin_unlock_irq(&rtc_lock);
> >
> > +       cmos->alarm_expires = rtc_tm_to_time64(&t->time);
> > +
> >         return 0;
> >  }
> >
> > @@ -588,7 +591,7 @@ static struct bin_attribute nvram = {
> >
> >  /*----------------------------------------------------------------*/
> >
> > -static struct cmos_rtc cmos_rtc;
> > +static struct cmos_rtc cmos_rtc = { .alarm_expires = 0 };
> >
> >  static irqreturn_t cmos_interrupt(int irq, void *p)
> >  {
> > @@ -860,6 +863,52 @@ static void __exit cmos_do_remove(struct device
> *dev)
> >         cmos->dev = NULL;
> >  }
> >
> > +static int cmos_aie_poweroff(struct device *dev)
> > +{
> > +       struct cmos_rtc *cmos = dev_get_drvdata(dev);
> > +       struct rtc_time now;
> > +       int err;
> > +       time64_t t_now;
> > +       unsigned char rtc_control;
> > +
> > +       if (!cmos->alarm_expires)
> > +               return -EINVAL;
> > +
> > +       spin_lock_irq(&rtc_lock);
> > +       rtc_control = CMOS_READ(RTC_CONTROL);
> > +       spin_unlock_irq(&rtc_lock);
> > +
> > +       /* We only care about the situation where AIE is disabled. */
> > +       if (rtc_control & RTC_AIE)
> > +               return -EBUSY;
> > +
> > +       err = cmos_read_time(dev, &now);
> > +       if (err < 0)
> > +               return err;
> > +       t_now = rtc_tm_to_time64(&now);
> > +
> > +       /*
> > +        * When enabling "RTC wake-up" in BIOS setup, the machine reboots
> > +        * automatically right after shutdown on some buggy boxes.
> > +        * This automatic rebooting issue won't happen when the alarm
> > +        * time is larger than now+1 seconds.
> > +        *
> > +        * If the alarm time is equal to now+1 seconds, we need to wait
> > +        * for 1 second so that AF is set by CMOS hardware. Therefore, we
> > +        * can get a chance to clear AF in cmos_do_shutdown().
> > +        *
> > +        * If the alarm time is less equal to 'now', the function simply
> > +        * returns '0' so that we can get a chance to clear AF in
> > +        * cmos_do_shutdown().
> > +        */
> > +       if (cmos->alarm_expires > (t_now + 1))
> > +               return -EBUSY;
> > +       else if (cmos->alarm_expires == (t_now + 1))
> > +               msleep(1000);
> > +
> > +       return 0;
> > +}
> > +
> >  #ifdef CONFIG_PM
> >
> >  static int cmos_suspend(struct device *dev)
> > @@ -1094,8 +1143,11 @@ static void cmos_pnp_shutdown(struct pnp_dev *pnp)
> >         struct device *dev = &pnp->dev;
> >         struct cmos_rtc *cmos = dev_get_drvdata(dev);
> >
> > -       if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
> > -               return;
> > +       if (system_state == SYSTEM_POWER_OFF) {
> > +               cmos_poweroff(dev);
> > +               if (cmos_aie_poweroff(dev) < 0)
> > +                       return;
> > +       }
> >
> >         cmos_do_shutdown(cmos->irq);
> >  }
> > @@ -1200,8 +1252,11 @@ static void cmos_platform_shutdown(struct
> platform_device *pdev)
> >         struct device *dev = &pdev->dev;
> >         struct cmos_rtc *cmos = dev_get_drvdata(dev);
> >
> > -       if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
> > -               return;
> > +       if (system_state == SYSTEM_POWER_OFF) {
> > +               cmos_poweroff(dev);
> > +               if (cmos_aie_poweroff(dev) < 0)
> > +                       return;
> > +       }
> >
> >         cmos_do_shutdown(cmos->irq);
> >  }
> > --
> > 1.9.1
> >
>
Alexandre Belloni June 14, 2015, 10:37 p.m. UTC | #3
Hi,

On 09/06/2015 at 18:23:32 +0800, Adrian Huang wrote :
> @@ -588,7 +591,7 @@ static struct bin_attribute nvram = {
>  
>  /*----------------------------------------------------------------*/
>  
> -static struct cmos_rtc	cmos_rtc;
> +static struct cmos_rtc	cmos_rtc = { .alarm_expires = 0 };
>  

That is probably unnecessary as the whole struct is already zeroed at
initialization.

>  static irqreturn_t cmos_interrupt(int irq, void *p)
>  {
> @@ -860,6 +863,52 @@ static void __exit cmos_do_remove(struct device *dev)
>  	cmos->dev = NULL;
>  }
>  
> +static int cmos_aie_poweroff(struct device *dev)
> +{
> +	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
> +	struct rtc_time now;
> +	int err;
> +	time64_t t_now;
> +	unsigned char rtc_control;
> +
> +	if (!cmos->alarm_expires)
> +		return -EINVAL;
> +
> +	spin_lock_irq(&rtc_lock);
> +	rtc_control = CMOS_READ(RTC_CONTROL);
> +	spin_unlock_irq(&rtc_lock);
> +
> +	/* We only care about the situation where AIE is disabled. */
> +	if (rtc_control & RTC_AIE)
> +		return -EBUSY;
> +
> +	err = cmos_read_time(dev, &now);
> +	if (err < 0)
> +		return err;
> +	t_now = rtc_tm_to_time64(&now);
> +
> +	/*
> +	 * When enabling "RTC wake-up" in BIOS setup, the machine reboots
> +	 * automatically right after shutdown on some buggy boxes.
> +	 * This automatic rebooting issue won't happen when the alarm
> +	 * time is larger than now+1 seconds.
> +	 *
> +	 * If the alarm time is equal to now+1 seconds, we need to wait
> +	 * for 1 second so that AF is set by CMOS hardware. Therefore, we
> +	 * can get a chance to clear AF in cmos_do_shutdown().
> +	 *
> +	 * If the alarm time is less equal to 'now', the function simply
> +	 * returns '0' so that we can get a chance to clear AF in
> +	 * cmos_do_shutdown().
> +	 */
> +	if (cmos->alarm_expires > (t_now + 1))
> +		return -EBUSY;
> +	else if (cmos->alarm_expires == (t_now + 1))
> +		msleep(1000);

I'm not too happy about that one second wait. Isn't the idea of
updating the alarm to a value in the past to cancel it working?

> +
> +	return 0;
> +}
> +
>  #ifdef CONFIG_PM
>  
>  static int cmos_suspend(struct device *dev)
> @@ -1094,8 +1143,11 @@ static void cmos_pnp_shutdown(struct pnp_dev *pnp)
>  	struct device *dev = &pnp->dev;
>  	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
>  
> -	if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
> -		return;
> +	if (system_state == SYSTEM_POWER_OFF) {
> +		cmos_poweroff(dev);

You should test the return value of cmos_poweroff() at some point as the
return is depending on it in the original code.

However, I'm not sure why one would want to not execute
cmos_do_shutdown() only when CONFIG_PM is not defined.

> +		if (cmos_aie_poweroff(dev) < 0)
> +			return;
> +	}
>  
>  	cmos_do_shutdown(cmos->irq);
>  }
> @@ -1200,8 +1252,11 @@ static void cmos_platform_shutdown(struct platform_device *pdev)
>  	struct device *dev = &pdev->dev;
>  	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
>  
> -	if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
> -		return;
> +	if (system_state == SYSTEM_POWER_OFF) {
> +		cmos_poweroff(dev);

Same comment here.

> +		if (cmos_aie_poweroff(dev) < 0)
> +			return;
> +	}
>  
>  	cmos_do_shutdown(cmos->irq);
>  }
diff mbox

Patch

diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
index a82556a..4e60ac8 100644
--- a/drivers/rtc/rtc-cmos.c
+++ b/drivers/rtc/rtc-cmos.c
@@ -51,6 +51,7 @@  struct cmos_rtc {
 	struct device		*dev;
 	int			irq;
 	struct resource		*iomem;
+	time64_t		alarm_expires;
 
 	void			(*wake_on)(struct device *);
 	void			(*wake_off)(struct device *);
@@ -377,6 +378,8 @@  static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
 
 	spin_unlock_irq(&rtc_lock);
 
+	cmos->alarm_expires = rtc_tm_to_time64(&t->time);
+
 	return 0;
 }
 
@@ -588,7 +591,7 @@  static struct bin_attribute nvram = {
 
 /*----------------------------------------------------------------*/
 
-static struct cmos_rtc	cmos_rtc;
+static struct cmos_rtc	cmos_rtc = { .alarm_expires = 0 };
 
 static irqreturn_t cmos_interrupt(int irq, void *p)
 {
@@ -860,6 +863,52 @@  static void __exit cmos_do_remove(struct device *dev)
 	cmos->dev = NULL;
 }
 
+static int cmos_aie_poweroff(struct device *dev)
+{
+	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
+	struct rtc_time now;
+	int err;
+	time64_t t_now;
+	unsigned char rtc_control;
+
+	if (!cmos->alarm_expires)
+		return -EINVAL;
+
+	spin_lock_irq(&rtc_lock);
+	rtc_control = CMOS_READ(RTC_CONTROL);
+	spin_unlock_irq(&rtc_lock);
+
+	/* We only care about the situation where AIE is disabled. */
+	if (rtc_control & RTC_AIE)
+		return -EBUSY;
+
+	err = cmos_read_time(dev, &now);
+	if (err < 0)
+		return err;
+	t_now = rtc_tm_to_time64(&now);
+
+	/*
+	 * When enabling "RTC wake-up" in BIOS setup, the machine reboots
+	 * automatically right after shutdown on some buggy boxes.
+	 * This automatic rebooting issue won't happen when the alarm
+	 * time is larger than now+1 seconds.
+	 *
+	 * If the alarm time is equal to now+1 seconds, we need to wait
+	 * for 1 second so that AF is set by CMOS hardware. Therefore, we
+	 * can get a chance to clear AF in cmos_do_shutdown().
+	 *
+	 * If the alarm time is less equal to 'now', the function simply
+	 * returns '0' so that we can get a chance to clear AF in
+	 * cmos_do_shutdown().
+	 */
+	if (cmos->alarm_expires > (t_now + 1))
+		return -EBUSY;
+	else if (cmos->alarm_expires == (t_now + 1))
+		msleep(1000);
+
+	return 0;
+}
+
 #ifdef CONFIG_PM
 
 static int cmos_suspend(struct device *dev)
@@ -1094,8 +1143,11 @@  static void cmos_pnp_shutdown(struct pnp_dev *pnp)
 	struct device *dev = &pnp->dev;
 	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
 
-	if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
-		return;
+	if (system_state == SYSTEM_POWER_OFF) {
+		cmos_poweroff(dev);
+		if (cmos_aie_poweroff(dev) < 0)
+			return;
+	}
 
 	cmos_do_shutdown(cmos->irq);
 }
@@ -1200,8 +1252,11 @@  static void cmos_platform_shutdown(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct cmos_rtc	*cmos = dev_get_drvdata(dev);
 
-	if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(dev))
-		return;
+	if (system_state == SYSTEM_POWER_OFF) {
+		cmos_poweroff(dev);
+		if (cmos_aie_poweroff(dev) < 0)
+			return;
+	}
 
 	cmos_do_shutdown(cmos->irq);
 }