Skip to content

Commit 29615d0

Browse files
rtc: sunxi: 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. Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Acked-by: Maxime Ripard <maxime.ripard@bootlin.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
1 parent 696fa1d commit 29615d0

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

drivers/rtc/rtc-sunxi.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ static int sunxi_rtc_probe(struct platform_device *pdev)
445445
platform_set_drvdata(pdev, chip);
446446
chip->dev = &pdev->dev;
447447

448+
chip->rtc = devm_rtc_allocate_device(&pdev->dev);
449+
if (IS_ERR(chip->rtc))
450+
return PTR_ERR(chip->rtc);
451+
448452
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449453
chip->base = devm_ioremap_resource(&pdev->dev, res);
450454
if (IS_ERR(chip->base))
@@ -481,30 +485,21 @@ static int sunxi_rtc_probe(struct platform_device *pdev)
481485
writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND, chip->base +
482486
SUNXI_ALRM_IRQ_STA);
483487

484-
chip->rtc = rtc_device_register("rtc-sunxi", &pdev->dev,
485-
&sunxi_rtc_ops, THIS_MODULE);
486-
if (IS_ERR(chip->rtc)) {
488+
chip->rtc->ops = &sunxi_rtc_ops;
489+
490+
ret = rtc_register_device(chip->rtc);
491+
if (ret) {
487492
dev_err(&pdev->dev, "unable to register device\n");
488-
return PTR_ERR(chip->rtc);
493+
return ret;
489494
}
490495

491496
dev_info(&pdev->dev, "RTC enabled\n");
492497

493498
return 0;
494499
}
495500

496-
static int sunxi_rtc_remove(struct platform_device *pdev)
497-
{
498-
struct sunxi_rtc_dev *chip = platform_get_drvdata(pdev);
499-
500-
rtc_device_unregister(chip->rtc);
501-
502-
return 0;
503-
}
504-
505501
static struct platform_driver sunxi_rtc_driver = {
506502
.probe = sunxi_rtc_probe,
507-
.remove = sunxi_rtc_remove,
508503
.driver = {
509504
.name = "sunxi-rtc",
510505
.of_match_table = sunxi_rtc_dt_ids,

0 commit comments

Comments
 (0)