Skip to contentSkip to conversation

rtc: ma35d1: fix unchecked IRQ error and IRQ-before-rtcdev ordering

PATCH New Cong Nguyen 2 participants 2 messages Sep 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rtc: ma35d1: fix unchecked IRQ error and IRQ-before-rtcdev ordering

platform_get_irq()'s return goes straight into devm_request_irq()'s unsigned int param unchecked -- a negative -EPROBE_DEFER becomes a huge positive number, so probe fails outright instead of deferring.

The IRQ is also requested before rtc->rtcdev is allocated. devm teardown is LIFO, so unbind or a later probe failure frees rtcdev before the IRQ, and an alarm firing in that window has ma35d1_rtc_interrupt() touch freed memory.

Check irq_num for a negative return, and move the IRQ request to right after devm_rtc_allocate_device() (its irqwork is already initialized there) and before devm_rtc_register_device(), so unbind unregisters the device before the IRQ is freed, not after.

Fixes dc0684adf3b6 ("rtc: Add driver for Nuvoton ma35d1 rtc controller")
Assisted-by LLM

---
Changes in v2:
- Sashiko found 2 issues on v1: dev_err_probe() on the irq_num check duplicated platform_get_irq()'s own internal error log, dropped; and requesting the IRQ right before devm_rtc_register_device() still left a window on unbind where the device is unregistered from userspace after the IRQ is already freed. Moved the request earlier, right after devm_rtc_allocate_device() (its irqwork is already initialized there), so unbind unregisters the device before freeing the IRQ, not after.

 drivers/rtc/rtc-ma35d1.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/rtc/rtc-ma35d1.c +7 -5
index cfcfc28060f6..ae68e5c89e15 100644
@@ -236,11 +236,8 @@ static int ma35d1_rtc_probe(struct platform_device *pdev)
 	}
 
 	rtc->irq_num = platform_get_irq(pdev, 0);
-
-	ret = devm_request_irq(&pdev->dev, rtc->irq_num, ma35d1_rtc_interrupt,
-			       IRQF_NO_SUSPEND, "ma35d1rtc", rtc);
-	if (ret)
-		return dev_err_probe(&pdev->dev, ret, "Failed to request rtc irq\n");
+	if (rtc->irq_num < 0)
+		return rtc->irq_num;
 
 	platform_set_drvdata(pdev, rtc);
 
@@ -254,6 +251,11 @@ static int ma35d1_rtc_probe(struct platform_device *pdev)
 	rtc->rtcdev->range_min = RTC_TIMESTAMP_BEGIN_2000;
 	rtc->rtcdev->range_max = RTC_TIMESTAMP_END_2099;
 
+	ret = devm_request_irq(&pdev->dev, rtc->irq_num, ma35d1_rtc_interrupt,
+			       IRQF_NO_SUSPEND, "ma35d1rtc", rtc);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "Failed to request rtc irq\n");
+
 	ret = devm_rtc_register_device(rtc->rtcdev);
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret, "Failed to register rtc device\n");
Signature · 1 line
2.25.1
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines

platform_get_irq()'s return goes straight into devm_request_irq()'s unsigned int param unchecked -- a negative -EPROBE_DEFER becomes a huge positive number, so probe fails outright instead of deferring.

The IRQ is also requested before rtc->rtcdev is allocated. devm teardown is LIFO, so unbind or a later probe failure frees rtcdev before the IRQ, and an alarm firing in that window has ma35d1_rtc_interrupt() touch freed memory.

Check irq_num for a negative return, and move the IRQ request to right after devm_rtc_allocate_device() (its irqwork is already initialized there) and before devm_rtc_register_device(), so unbind unregisters the device before the IRQ is freed, not after.

Quoted 5 lines
Fixes dc0684adf3b6 ("rtc: Add driver for Nuvoton ma35d1 rtc controller")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1