Skip to contentSkip to conversation

rtc: mxc_v2: fix possible race condition

PATCH New Alexandre Belloni 1 participant 2 messages May 19, 2018

Subsystems (2)
lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/2 rtc: mxc_v2: fix possible race condition

The IRQ is requested before the struct rtc is allocated and registered, but this struct is used in the IRQ handler. This may lead to a NULL pointer dereference.

Switch to devm_rtc_allocate_device/rtc_register_device to allocate the rtc before requesting the IRQ.

---
 drivers/rtc/rtc-mxc_v2.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/rtc/rtc-mxc_v2.c +9 -7
index 9e14efb990b2..4cc121a41fe0 100644
@@ -343,6 +343,12 @@ static int mxc_rtc_probe(struct platform_device *pdev)
 		return ret;
 	}
 
+	pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(pdata->rtc))
+		return PTR_ERR(pdata->rtc);
+
+	pdata->rtc->ops = &mxc_rtc_ops;
+
 	clk_disable(pdata->clk);
 	platform_set_drvdata(pdev, pdata);
 	ret =
@@ -354,15 +360,11 @@ static int mxc_rtc_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	pdata->rtc =
-	    devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops,
-				     THIS_MODULE);
-	if (IS_ERR(pdata->rtc)) {
+	ret = rtc_register_device(pdata->rtc);
+	if (ret < 0)
 		clk_unprepare(pdata->clk);
-		return PTR_ERR(pdata->rtc);
-	}
 
-	return 0;
+	return ret;
 }
 
 static int mxc_rtc_remove(struct platform_device *pdev)
Signature · 1 line
2.17.0
author maintainer
#1 lore raw

PATCH 2/2 rtc: mxc_v2: let the core handle rtc range

This RTC is a 32-bit second counter.

This also solves an issue where mxc_rtc_set_alarm() can return with the lock taken.

---
 drivers/rtc/rtc-mxc_v2.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/rtc/rtc-mxc_v2.c +1 -10
index 4cc121a41fe0..24ca74ca632a 100644
@@ -165,11 +165,6 @@ static int mxc_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	time64_t time = rtc_tm_to_time64(tm);
 	int ret;
 
-	if (time > U32_MAX) {
-		dev_err(dev, "RTC exceeded by %llus\n", time - U32_MAX);
-		return -EINVAL;
-	}
-
 	ret = mxc_rtc_lock(pdata);
 	if (ret)
 		return ret;
@@ -248,11 +243,6 @@ static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 	if (ret)
 		return ret;
 
-	if (time > U32_MAX) {
-		dev_err(dev, "Hopefully I am out of service by then :-(\n");
-		return -EINVAL;
-	}
-
 	writel((u32)time, pdata->ioaddr + SRTC_LPSAR);
 
 	/* clear alarm interrupt status bit */
@@ -348,6 +338,7 @@ static int mxc_rtc_probe(struct platform_device *pdev)
 		return PTR_ERR(pdata->rtc);
 
 	pdata->rtc->ops = &mxc_rtc_ops;
+	pdata->rtc->range_max = U32_MAX;
 
 	clk_disable(pdata->clk);
 	platform_set_drvdata(pdev, pdata);
Signature · 1 line
2.17.0