Skip to contentSkip to conversation

watchdog: mediatek: Register reset controller before watchdog device

PATCH New Guenter Roeck 1 participant 1 message Nov 6

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 watchdog: mediatek: Register reset controller before watchdog device

In mtk_wdt_probe(), devm_watchdog_register_device() is called before registering the TOPRGU reset controller via toprgu_register_reset_controller().

Because devm_watchdog_register_device() immediately exposes /dev/watchdog to userspace, mtk_wdt_probe() transiently publishes the watchdog character device, registers reboot and restart handlers, and logs that the watchdog is enabled before probe can still fail in toprgu_register_reset_controller(). Furthermore, if userspace opens /dev/watchdog during this window and starts the hardware counter, and toprgu_register_reset_controller() then fails, devres unregisters /dev/watchdog and cancels the core keepalive worker without stopping the hardware watchdog (as WDOG_STOP_ON_UNREGISTER is not set), leaving the timer armed and causing an unexpected system reset.

Initialize the TOPRGU reset controller data and call toprgu_register_reset_controller() before mtk_wdt_init() and devm_watchdog_register_device() so the watchdog is only initialized and exposed after reset controller registration succeeds.

Fixes c254e103082b ("watchdog: mtk_wdt: mt8183: Add reset controller")
---
 drivers/watchdog/mtk_wdt.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/watchdog/mtk_wdt.c +12 -12
index ff72e0fb1154..2d530098a421 100644
@@ -540,6 +540,18 @@ static int mtk_wdt_probe(struct platform_device *pdev)
 	mtk_wdt->reset_by_toprgu =
 		of_property_read_bool(dev->of_node, "mediatek,reset-by-toprgu");
 
+	wdt_data = of_device_get_match_data(dev);
+	if (wdt_data) {
+		mtk_wdt->toprgu_sw_rst_num = wdt_data->toprgu_sw_rst_num;
+		mtk_wdt->toprgu_sw_rst_tb = wdt_data->toprgu_sw_rst_tb;
+		mtk_wdt->has_swsysrst_en = wdt_data->has_swsysrst_en;
+
+		err = toprgu_register_reset_controller(pdev,
+						       wdt_data->toprgu_sw_rst_num);
+		if (err)
+			return err;
+	}
+
 	mtk_wdt_init(&mtk_wdt->wdt_dev);
 
 	watchdog_stop_on_reboot(&mtk_wdt->wdt_dev);
@@ -553,18 +565,6 @@ static int mtk_wdt_probe(struct platform_device *pdev)
 	dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
 		 mtk_wdt->wdt_dev.timeout, nowayout);
 
-	wdt_data = of_device_get_match_data(dev);
-	if (wdt_data) {
-		mtk_wdt->toprgu_sw_rst_num = wdt_data->toprgu_sw_rst_num;
-		mtk_wdt->toprgu_sw_rst_tb = wdt_data->toprgu_sw_rst_tb;
-		mtk_wdt->has_swsysrst_en = wdt_data->has_swsysrst_en;
-
-		err = toprgu_register_reset_controller(pdev,
-						       wdt_data->toprgu_sw_rst_num);
-		if (err)
-			return err;
-	}
-
 	return 0;
 }
 
Signature · 1 line
2.45.2