Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 02/10] rtc: mt6397: handle non-DT platform device matching in probe
       [not found] <20261123200012.502284-1-lee@kernel.org>
@ 2026-11-23 19:59 ` Lee Jones
  2026-11-23 19:59 ` [PATCH 03/10] rtc: ma35d1: allocate RTC device before requesting interrupt Lee Jones
  2026-11-23 19:59 ` [PATCH 07/10] rtc: stmp3xxx: unregister child watchdog platform device on unbind Lee Jones
  2 siblings, 0 replies; 3+ messages in thread
From: Lee Jones @ 2026-11-23 19:59 UTC (permalink / raw)
  To: lee, Eddie Huang, Sean Wang, Alexandre Belloni, Matthias Brugger,
	AngeloGioacchino Del Regno, Ryan Brue, Tianping.Fang,
	Yingjoe Chen, Hsin-Hsiung Wang, Sebastian Reichel,
	linux-arm-kernel, linux-mediatek, linux-rtc, linux-kernel
  Cc: stable

The parent MediaTek PMIC MFD driver instantiates RTC platform
subdevices even when the optional RTC child Device Tree node is omitted,
and devices can also be bound via sysfs driver_override.  In those
cases, of_device_get_match_data() returns NULL, yet mtk_rtc_probe()
succeeds with rtc->data left NULL and mtk_rtc_write_trigger()
subsequently crashes when dereferencing rtc->data->wrtgr.

Provide a platform_device_id table covering the MediaTek PMIC RTC cells
and fall back to platform_get_device_id() when no OF match data is
present, returning -ENODEV if neither match path provides driver data.

Fixes: 29ee40091e27 ("rtc: mt6397: Add support for the MediaTek MT6358 RTC")
Closes: https://sashiko.dev/#/bug/linux-35993d35-d776-4f86-9bd2-42c6ee078f88
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/rtc/rtc-mt6397.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/drivers/rtc/rtc-mt6397.c b/drivers/rtc/rtc-mt6397.c
index ab402f9f85c3..c40644ae509a 100644
--- a/drivers/rtc/rtc-mt6397.c
+++ b/drivers/rtc/rtc-mt6397.c
@@ -343,6 +343,14 @@ static int mtk_rtc_probe(struct platform_device *pdev)
 	rtc->addr_base = res->start;
 
 	rtc->data = of_device_get_match_data(&pdev->dev);
+	if (!rtc->data) {
+		const struct platform_device_id *id = platform_get_device_id(pdev);
+
+		if (!id)
+			return -ENODEV;
+
+		rtc->data = (const struct mtk_rtc_data *)id->driver_data;
+	}
 
 	rtc->irq = platform_get_irq(pdev, 0);
 	if (rtc->irq < 0)
@@ -427,6 +435,16 @@ static const struct of_device_id mt6397_rtc_of_match[] = {
 };
 MODULE_DEVICE_TABLE(of, mt6397_rtc_of_match);
 
+static const struct platform_device_id mt6397_rtc_id[] = {
+	{ "mt6323-rtc", (kernel_ulong_t)&mt6397_rtc_data },
+	{ "mt6357-rtc", (kernel_ulong_t)&mt6358_rtc_data },
+	{ "mt6358-rtc", (kernel_ulong_t)&mt6358_rtc_data },
+	{ "mt6359-rtc", (kernel_ulong_t)&mt6358_rtc_data },
+	{ "mt6397-rtc", (kernel_ulong_t)&mt6397_rtc_data },
+	{ }
+};
+MODULE_DEVICE_TABLE(platform, mt6397_rtc_id);
+
 static struct platform_driver mtk_rtc_driver = {
 	.driver = {
 		.name = "mt6397-rtc",
@@ -434,6 +452,7 @@ static struct platform_driver mtk_rtc_driver = {
 		.pm = &mt6397_pm_ops,
 	},
 	.probe	= mtk_rtc_probe,
+	.id_table = mt6397_rtc_id,
 };
 
 module_platform_driver(mtk_rtc_driver);
-- 
2.56.1.750.gf70dc682cf-goog



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

* [PATCH 03/10] rtc: ma35d1: allocate RTC device before requesting interrupt
       [not found] <20261123200012.502284-1-lee@kernel.org>
  2026-11-23 19:59 ` [PATCH 02/10] rtc: mt6397: handle non-DT platform device matching in probe Lee Jones
@ 2026-11-23 19:59 ` Lee Jones
  2026-11-23 19:59 ` [PATCH 07/10] rtc: stmp3xxx: unregister child watchdog platform device on unbind Lee Jones
  2 siblings, 0 replies; 3+ messages in thread
From: Lee Jones @ 2026-11-23 19:59 UTC (permalink / raw)
  To: lee, Jacky Huang, Shan-Chun Hung, Alexandre Belloni,
	Ming-Jen Chen, linux-arm-kernel, linux-rtc, linux-kernel
  Cc: stable

ma35d1_rtc_probe() registers ma35d1_rtc_interrupt() via
devm_request_irq() before allocating rtc->rtcdev via
devm_rtc_allocate_device().  During driver unbind or if
devm_rtc_register_device() fails, devres unwinds in reverse order and
frees struct rtc_device in devm_rtc_release_device() before
devm_irq_release() unregisters the interrupt handler.  An RTC interrupt
arriving in that teardown window passes a dangling rtc->rtcdev pointer
to rtc_update_irq(), dereferencing freed memory and scheduling a freed
work_struct.

Allocate and initialise rtc->rtcdev before requesting the IRQ so that
devres frees the interrupt handler before releasing the RTC device.

Fixes: dc0684adf3b6 ("rtc: Add driver for Nuvoton ma35d1 rtc controller")
Closes: https://sashiko.dev/#/bug/linux-26674087-baed-4ef2-83a0-78888ea8d16a
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/rtc/rtc-ma35d1.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/rtc/rtc-ma35d1.c b/drivers/rtc/rtc-ma35d1.c
index eb0da8b091dc..beeef6db92d9 100644
--- a/drivers/rtc/rtc-ma35d1.c
+++ b/drivers/rtc/rtc-ma35d1.c
@@ -235,13 +235,6 @@ static int ma35d1_rtc_probe(struct platform_device *pdev)
 			return dev_err_probe(&pdev->dev, ret, "rtc init failed\n");
 	}
 
-	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");
-
 	platform_set_drvdata(pdev, rtc);
 
 	device_init_wakeup(&pdev->dev, true);
@@ -254,6 +247,13 @@ 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;
 
+	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");
+
 	ret = devm_rtc_register_device(rtc->rtcdev);
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret, "Failed to register rtc device\n");
-- 
2.56.1.750.gf70dc682cf-goog



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

* [PATCH 07/10] rtc: stmp3xxx: unregister child watchdog platform device on unbind
       [not found] <20261123200012.502284-1-lee@kernel.org>
  2026-11-23 19:59 ` [PATCH 02/10] rtc: mt6397: handle non-DT platform device matching in probe Lee Jones
  2026-11-23 19:59 ` [PATCH 03/10] rtc: ma35d1: allocate RTC device before requesting interrupt Lee Jones
@ 2026-11-23 19:59 ` Lee Jones
  2 siblings, 0 replies; 3+ messages in thread
From: Lee Jones @ 2026-11-23 19:59 UTC (permalink / raw)
  To: lee, Alexandre Belloni, Maxime Coquelin, Alexandre Torgue,
	Peter Hartley, dmitry pervushin, Wolfram Sang, Andrew Morton,
	Wim Van Sebroeck, linux-rtc, linux-stm32, linux-arm-kernel,
	linux-kernel
  Cc: stable

stmp3xxx_wdt_register() creates and registers the child
"stmp3xxx_rtc_wdt" platform device without tracking or unregistering it
when the parent RTC driver unbinds, and also assigns static wdt_pdata
directly to dev.platform_data where platform_device_release() passes it
to kfree().  After unbind, devres unmaps rtc_data->io and frees
rtc_data while the parent drvdata is cleared, so subsequent watchdog
operations or reboot notifications invoking stmp3xxx_wdt_set_timeout()
dereference torn-down parent state.

Register the child platform device via platform_device_register_data()
so its platform data is heap-allocated, and tie
platform_device_unregister() to the parent device with
devm_add_action_or_reset() so the watchdog device is torn down before
parent resources are released.

Fixes: 1a71fb84fda6 ("rtc: stmp3xxx: add wdt-accessor function")
Closes: https://sashiko.dev/#/bug/linux-4ddbfe9c-a03c-4a01-a3ce-357545121630
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/rtc/rtc-stmp3xxx.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/drivers/rtc/rtc-stmp3xxx.c b/drivers/rtc/rtc-stmp3xxx.c
index d79e282e3817..a5281b519540 100644
--- a/drivers/rtc/rtc-stmp3xxx.c
+++ b/drivers/rtc/rtc-stmp3xxx.c
@@ -92,24 +92,29 @@ static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout)
 	}
 }
 
-static struct stmp3xxx_wdt_pdata wdt_pdata = {
+static const struct stmp3xxx_wdt_pdata wdt_pdata = {
 	.wdt_set_timeout = stmp3xxx_wdt_set_timeout,
 };
 
+static void stmp3xxx_wdt_unregister(void *data)
+{
+	platform_device_unregister(data);
+}
+
 static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
 {
-	int rc = -1;
-	struct platform_device *wdt_pdev =
-		platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
-
-	if (wdt_pdev) {
-		wdt_pdev->dev.parent = &rtc_pdev->dev;
-		wdt_pdev->dev.platform_data = &wdt_pdata;
-		rc = platform_device_add(wdt_pdev);
-		if (rc)
-			platform_device_put(wdt_pdev);
-	}
+	struct platform_device *wdt_pdev;
+	int rc;
 
+	wdt_pdev = platform_device_register_data(&rtc_pdev->dev,
+						 "stmp3xxx_rtc_wdt",
+						 rtc_pdev->id, &wdt_pdata,
+						 sizeof(wdt_pdata));
+	rc = PTR_ERR_OR_ZERO(wdt_pdev);
+	if (!rc)
+		rc = devm_add_action_or_reset(&rtc_pdev->dev,
+					      stmp3xxx_wdt_unregister,
+					      wdt_pdev);
 	if (rc)
 		dev_err(&rtc_pdev->dev,
 			"failed to register stmp3xxx_rtc_wdt\n");
-- 
2.56.1.750.gf70dc682cf-goog



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

end of thread, other threads:[~2026-11-23 20:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261123200012.502284-1-lee@kernel.org>
2026-11-23 19:59 ` [PATCH 02/10] rtc: mt6397: handle non-DT platform device matching in probe Lee Jones
2026-11-23 19:59 ` [PATCH 03/10] rtc: ma35d1: allocate RTC device before requesting interrupt Lee Jones
2026-11-23 19:59 ` [PATCH 07/10] rtc: stmp3xxx: unregister child watchdog platform device on unbind Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox