linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
@ 2015-12-08 16:28 Steve Twiss
  2015-12-16 23:47 ` Alexandre Belloni
  2015-12-21  8:19 ` Alexandre Belloni
  0 siblings, 2 replies; 5+ messages in thread
From: Steve Twiss @ 2015-12-08 16:28 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni, LINUXKERNEL, RTC-LINUX
  Cc: David Dajun Chen, Support Opensource

From: Steve Twiss <stwiss.opensource@diasemi.com>

This fix alters the ordering of the IRQ and device registrations in the RTC
driver probe function. This change will apply to the RTC driver that supports
both DA9063 and DA9062 PMICs.

A problem could occur with the existing RTC driver if:

A system is started from a cold boot using the PMIC RTC IRQ to initiate a
power on operation. For instance, if an RTC alarm is used to start a
platform from power off.
The existing driver IRQ is requested before the device has been properly
registered.
i.e.
    ret = devm_request_threaded_irq() 
comes before
    rtc->rtc_dev = devm_rtc_device_register();

In this case, the interrupt can be called before the device has been
registered and the handler can be called immediately. The IRQ handler
da9063_alarm_event() contains the function call

    rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);

which in turn tries to access the unavailable rtc->rtc_dev.

The fix is to reorder the functions inside the RTC probe. The IRQ is
requested after the RTC device resource has been registered so that
get_irq_byname is the last thing to happen.

Signed-off-by: Steve Twiss <stwiss.opensource@diasemi.com>

---
This patch applies against linux-next and v4.4-rc4

Regards,
Steve


 drivers/rtc/rtc-da9063.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/rtc/rtc-da9063.c b/drivers/rtc/rtc-da9063.c
index 284b587..d6c853b 100644
--- a/drivers/rtc/rtc-da9063.c
+++ b/drivers/rtc/rtc-da9063.c
@@ -483,24 +483,23 @@ static int da9063_rtc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, rtc);
 
+	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC,
+					   &da9063_rtc_ops, THIS_MODULE);
+	if (IS_ERR(rtc->rtc_dev))
+		return PTR_ERR(rtc->rtc_dev);
+
+	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
+	rtc->rtc_sync = false;
+
 	irq_alarm = platform_get_irq_byname(pdev, "ALARM");
 	ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
 					da9063_alarm_event,
 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
 					"ALARM", rtc);
-	if (ret) {
+	if (ret)
 		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
 			irq_alarm, ret);
-		return ret;
-	}
-
-	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC,
-					   &da9063_rtc_ops, THIS_MODULE);
-	if (IS_ERR(rtc->rtc_dev))
-		return PTR_ERR(rtc->rtc_dev);
 
-	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
-	rtc->rtc_sync = false;
 	return ret;
 }
 
-- 
end-of-patch for PATCH V1


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

* Re: [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
  2015-12-08 16:28 [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on Steve Twiss
@ 2015-12-16 23:47 ` Alexandre Belloni
  2015-12-17 11:37   ` Opensource [Steve Twiss]
  2015-12-21  8:19 ` Alexandre Belloni
  1 sibling, 1 reply; 5+ messages in thread
From: Alexandre Belloni @ 2015-12-16 23:47 UTC (permalink / raw)
  To: Steve Twiss
  Cc: Alessandro Zummo, LINUXKERNEL, RTC-LINUX, David Dajun Chen,
	Support Opensource

Hi,

This seems mostly fine, however ...

On 08/12/2015 at 16:28:39 +0000, Steve Twiss wrote :
>  	irq_alarm = platform_get_irq_byname(pdev, "ALARM");
>  	ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
>  					da9063_alarm_event,
>  					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
>  					"ALARM", rtc);
> -	if (ret) {
> +	if (ret)
>  		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
>  			irq_alarm, ret);
> -		return ret;
> -	}
> -

... now that requesting the interrupt is optional, you probably want to
prevent userspace from thinking it will get an interrupt after setting
the alarm by returning -EINVAL in da9063_rtc_read_alarm() and
da9063_rtc_set_alarm() in that case.

> -	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, DA9063_DRVNAME_RTC,
> -					   &da9063_rtc_ops, THIS_MODULE);
> -	if (IS_ERR(rtc->rtc_dev))
> -		return PTR_ERR(rtc->rtc_dev);
>  
> -	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
> -	rtc->rtc_sync = false;
>  	return ret;
>  }
>  
> -- 
> end-of-patch for PATCH V1
> 

-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

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

* RE: [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
  2015-12-16 23:47 ` Alexandre Belloni
@ 2015-12-17 11:37   ` Opensource [Steve Twiss]
  2015-12-17 11:44     ` Alexandre Belloni
  0 siblings, 1 reply; 5+ messages in thread
From: Opensource [Steve Twiss] @ 2015-12-17 11:37 UTC (permalink / raw)
  To: Alexandre Belloni
  Cc: Alessandro Zummo, LINUXKERNEL, RTC-LINUX, David Dajun Chen,
	Support Opensource

On 16 December 2015 23:47 Alexandre Belloni wrote:
> Subject: Re: [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
> 
> This seems mostly fine, however ...

Hi Alexandre,
Thanks for reviewing this.

> On 08/12/2015 at 16:28:39 +0000, Steve Twiss wrote :
> >  	irq_alarm = platform_get_irq_byname(pdev, "ALARM");
> >  	ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
> >  					da9063_alarm_event,
> >  					IRQF_TRIGGER_LOW |
> IRQF_ONESHOT,
> >  					"ALARM", rtc);
> > -	if (ret) {
> > +	if (ret)
> >  		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d:
> %d\n",
> >  			irq_alarm, ret);
> > -		return ret;
> > -	}
> > -
> 
> ... now that requesting the interrupt is optional, you probably want to
> prevent userspace from thinking it will get an interrupt after setting
> the alarm by returning -EINVAL in da9063_rtc_read_alarm() and
> da9063_rtc_set_alarm() in that case.
> 

.. I'm not quite certain I understand.
Does the patch looks worse that it is?
This part,

+	if (ret)
		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
			irq_alarm, ret);
 -		return ret;

looks like it has erased the return ret,

> 
> > -	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev,
> DA9063_DRVNAME_RTC,
> > -					   &da9063_rtc_ops, THIS_MODULE);
> > -	if (IS_ERR(rtc->rtc_dev))
> > -		return PTR_ERR(rtc->rtc_dev);
> >
> > -	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
> > -	rtc->rtc_sync = false;
> >  	return ret;

But it does exist at the end of the patch.
So there will still be an error sent if the IRQ is not requested properly.
Is this what you meant in your previous e-mail?

Regards,
Stephen

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

* Re: [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
  2015-12-17 11:37   ` Opensource [Steve Twiss]
@ 2015-12-17 11:44     ` Alexandre Belloni
  0 siblings, 0 replies; 5+ messages in thread
From: Alexandre Belloni @ 2015-12-17 11:44 UTC (permalink / raw)
  To: Opensource [Steve Twiss]
  Cc: Alessandro Zummo, LINUXKERNEL, RTC-LINUX, David Dajun Chen,
	Support Opensource

On 17/12/2015 at 11:37:06 +0000, Opensource [Steve Twiss] wrote :
> On 16 December 2015 23:47 Alexandre Belloni wrote:
> > Subject: Re: [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
> > 
> > This seems mostly fine, however ...
> 
> Hi Alexandre,
> Thanks for reviewing this.
> 
> > On 08/12/2015 at 16:28:39 +0000, Steve Twiss wrote :
> > >  	irq_alarm = platform_get_irq_byname(pdev, "ALARM");
> > >  	ret = devm_request_threaded_irq(&pdev->dev, irq_alarm, NULL,
> > >  					da9063_alarm_event,
> > >  					IRQF_TRIGGER_LOW |
> > IRQF_ONESHOT,
> > >  					"ALARM", rtc);
> > > -	if (ret) {
> > > +	if (ret)
> > >  		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d:
> > %d\n",
> > >  			irq_alarm, ret);
> > > -		return ret;
> > > -	}
> > > -
> > 
> > ... now that requesting the interrupt is optional, you probably want to
> > prevent userspace from thinking it will get an interrupt after setting
> > the alarm by returning -EINVAL in da9063_rtc_read_alarm() and
> > da9063_rtc_set_alarm() in that case.
> > 
> 
> .. I'm not quite certain I understand.
> Does the patch looks worse that it is?
> This part,
> 
> +	if (ret)
> 		dev_err(&pdev->dev, "Failed to request ALARM IRQ %d: %d\n",
> 			irq_alarm, ret);
>  -		return ret;
> 
> looks like it has erased the return ret,
> 
> > 
> > > -	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev,
> > DA9063_DRVNAME_RTC,
> > > -					   &da9063_rtc_ops, THIS_MODULE);
> > > -	if (IS_ERR(rtc->rtc_dev))
> > > -		return PTR_ERR(rtc->rtc_dev);
> > >
> > > -	da9063_data_to_tm(data, &rtc->alarm_time, rtc);
> > > -	rtc->rtc_sync = false;
> > >  	return ret;
> 
> But it does exist at the end of the patch.
> So there will still be an error sent if the IRQ is not requested properly.
> Is this what you meant in your previous e-mail?
> 

Indeed, you are right, I'll apply that patch.


-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

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

* Re: [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on
  2015-12-08 16:28 [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on Steve Twiss
  2015-12-16 23:47 ` Alexandre Belloni
@ 2015-12-21  8:19 ` Alexandre Belloni
  1 sibling, 0 replies; 5+ messages in thread
From: Alexandre Belloni @ 2015-12-21  8:19 UTC (permalink / raw)
  To: Steve Twiss
  Cc: Alessandro Zummo, LINUXKERNEL, RTC-LINUX, David Dajun Chen,
	Support Opensource

On 08/12/2015 at 16:28:39 +0000, Steve Twiss wrote :
> From: Steve Twiss <stwiss.opensource@diasemi.com>
> 
> This fix alters the ordering of the IRQ and device registrations in the RTC
> driver probe function. This change will apply to the RTC driver that supports
> both DA9063 and DA9062 PMICs.
> 
> A problem could occur with the existing RTC driver if:
> 
> A system is started from a cold boot using the PMIC RTC IRQ to initiate a
> power on operation. For instance, if an RTC alarm is used to start a
> platform from power off.
> The existing driver IRQ is requested before the device has been properly
> registered.
> i.e.
>     ret = devm_request_threaded_irq() 
> comes before
>     rtc->rtc_dev = devm_rtc_device_register();
> 
> In this case, the interrupt can be called before the device has been
> registered and the handler can be called immediately. The IRQ handler
> da9063_alarm_event() contains the function call
> 
>     rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
> 
> which in turn tries to access the unavailable rtc->rtc_dev.
> 
> The fix is to reorder the functions inside the RTC probe. The IRQ is
> requested after the RTC device resource has been registered so that
> get_irq_byname is the last thing to happen.
> 
> Signed-off-by: Steve Twiss <stwiss.opensource@diasemi.com>

This patch has been applied and landed in v4.4-rc6


-- 
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

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

end of thread, other threads:[~2015-12-21  8:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-08 16:28 [PATCH V1] rtc: da9063: access ordering error during RTC interrupt system power on Steve Twiss
2015-12-16 23:47 ` Alexandre Belloni
2015-12-17 11:37   ` Opensource [Steve Twiss]
2015-12-17 11:44     ` Alexandre Belloni
2015-12-21  8:19 ` Alexandre Belloni

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).