watchdog: arm_twd: Protect the clock snapshot until notifier registration
PATCH 1/1 watchdog: arm_twd: Protect the clock snapshot until notifier registration
A clock rate change can complete after clk_get_rate() returns but before probe registers the clock notifier. The driver then misses that change and keeps the old rate when programming the watchdog counter.
Take temporary rate exclusivity before reading the clock rate and release it after registering the notifier. This protects the snapshot against changes from other consumers, including changes to an ancestor clock, until subsequent transitions can be observed. Release the protection on the zero-rate and notifier registration error paths as well.
---
This is a single-fix follow-up against watchdog-next, rather than a new revision of the accepted driver series. A rate change from another CCF consumer can finish between the rate snapshot and notifier installation. Temporary rate exclusivity closes that handoff without retaining the CCF prepare lock or holding the watchdog mutex across a CCF call.
Validation: the old and fixed clock-backed probe branches, actual CCF functions and the actual TWD notifier were exercised with 25 deterministic interleaving/unwind cases under both GCC and Clang ASan/UBSan. The full modified driver also builds as an ARM module with W=1, with all imports resolved. These are offline checks; no changing-clock hardware test is claimed. The separate maximum-timeout policy for future faster clock rates is unchanged.
--- drivers/watchdog/arm_twd_wdt.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/watchdog/arm_twd_wdt.c +9 -1
@@ -553,13 +553,21 @@ static int twd_wdt_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (clk),"failed to get clock\n" );if (clk) { +/* Keep the rate snapshot stable until its notifier is installed. */ + ret =clk_rate_exclusive_get (clk); +if (ret) +return dev_err_probe (dev, ret,"failed to protect clock rate\n" ); + wdt->rate =clk_get_rate (clk); -if (!wdt->rate) +if (!wdt->rate) { +clk_rate_exclusive_put (clk);return dev_err_probe (dev, -EINVAL ,"clock rate is zero\n" ); + } wdt->max_rate = wdt->rate; wdt->clk_nb.notifier_call = twd_wdt_clk_notify; ret =devm_clk_notifier_register (dev, clk, &wdt->clk_nb); +clk_rate_exclusive_put (clk);if (ret)return dev_err_probe (dev, ret,"failed to register clock notifier\n" ); }else {
---
base-commit: 43495d289b7cc30f95791fb7cd7999d766b659b8
change-id: 20261124-arm-twd-clock-notifier-47c1d95ed0f4
Best regards,
Signature · 1 line
Fil Dunsky <[email protected]>
Guenter Roeck
maintainerQuoted 21 lines · Fil Dunsky
From: Fil Dunsky <[email protected]>
A clock rate change can complete after
clk_get_rate()returns but before probe registers the clock notifier. The driver then misses that change and keeps the old rate when programming the watchdog counter.Take temporary rate exclusivity before reading the clock rate and release it after registering the notifier. This protects the snapshot against changes from other consumers, including changes to an ancestor clock, until subsequent transitions can be observed. Release the protection on the zero-rate and notifier registration error paths as well.
Fixes 52d91ea73e05 ("watchdog: Add ARM TWD (MPCore private) watchdog driver")Signed-off-by Fil Dunsky <[email protected]>---
This is a single-fix follow-up against watchdog-next, rather than a new revision of the accepted driver series. A rate change from another CCF consumer can finish between the rate snapshot and notifier installation. Temporary rate exclusivity closes that handoff without retaining the CCF prepare lock or holding the watchdog mutex across a CCF call.
Quoted 7 lines
Validation: the old and fixed clock-backed probe branches, actual CCF functions and the actual TWD notifier were exercised with 25 deterministic interleaving/unwind cases under both GCC and Clang ASan/UBSan. The full modified driver also builds as an ARM module with W=1, with all imports resolved. These are offline checks; no changing-clock hardware test is claimed. The separate maximum-timeout policy for future faster clock rates is unchanged.
Squashed into original patch.
Thanks,
Guenter