Linux Watchdog driver development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fil Dunsky" <filipp.dunsky@gmail.com>
Cc: linux-watchdog@vger.kernel.org
Subject: Re: [PATCH v5] watchdog: Add ARM TWD (MPCore private) watchdog driver
Date: Wed, 18 Nov 2026 09:12:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-220535@kernel.org> (raw)
In-Reply-To: <20261118-arm-twd-wdt-v5-1-886265008e78@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] watchdog: arm_twd_wdt: race in twd_wdt_set_timeout() causes
  immediate reset
- [Medium] watchdog: arm_twd_wdt: twd_wdt_probe() arms watchdog before
  cleanup setup
- [Medium] watchdog: arm_twd_wdt: keepalive reset and active ping worker
  on suspend
- [Medium] watchdog: arm_twd_wdt: missing barrier between keepalive and
  armed state
- [Low] watchdog: arm_twd_wdt: use of obsolete __MODULE_STRING() macro
--

commit 2b76b6ecd425333171034f709700b36407dec65d
Author: Fil Dunsky <filipp.dunsky@gmail.com>

watchdog: Add ARM TWD (MPCore private) watchdog driver

This commit adds a watchdog driver for the ARM11 MPCore, Cortex-A5 and
Cortex-A9 private (TWD) watchdog, using CPU0's watchdog as the system
watchdog and registering a restart handler.

> diff --git a/drivers/watchdog/arm_twd_wdt.c b/drivers/watchdog/arm_twd_wdt.c
> new file mode 100644
> index 0000000000000..068b6483a8ac3
> --- /dev/null
> +++ b/drivers/watchdog/arm_twd_wdt.c
> @@ -0,0 +1,465 @@

[ ... ]

> +static bool nowayout = WATCHDOG_NOWAYOUT;
> +module_param(nowayout, bool, 0);
> +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
> +		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");

[Severity: Low]
__MODULE_STRING() is marked obsolete in include/linux/module.h, and commit
b152ca999c5f ("watchdog: Replace __MODULE_STRING() with __stringify()")
removed all other uses in drivers/watchdog/.  Could this include
<linux/stringify.h> and use __stringify(WATCHDOG_NOWAYOUT) instead?

[ ... ]

> +static u32 twd_wdt_ticks(struct twd_wdt *wdt)
> +{
> +	s64 expires = atomic64_read(&wdt->keepalive) +
> +		      (s64)wdt->wdd.timeout * NSEC_PER_SEC;
> +	s64 left = expires - ktime_get_ns();
> +	u64 ticks;
> +
> +	if (left <= 0)
> +		return 1;
> +
> +	ticks = mul_u64_u64_div_u64(READ_ONCE(wdt->rate), left,
> +				    (u64)NSEC_PER_SEC * (TWD_WDT_PRESCALE + 1));
> +
> +	return clamp_t(u64, ticks, 1, U32_MAX);
> +}

[ ... ]

> +static void twd_wdt_set_armed(struct twd_wdt *wdt, bool armed)
> +{
> +	guard(mutex)(&wdt->lock);
> +
> +	atomic64_set(&wdt->keepalive, ktime_get_ns());
> +	WRITE_ONCE(wdt->armed, armed);
> +	twd_wdt_call(wdt, armed ? twd_wdt_cpu_start : twd_wdt_cpu_stop);
> +}

[Severity: Medium]
Can CPU0 in twd_wdt_cpu_pm_notify() observe wdt->armed == true before the
new wdt->keepalive value and trigger an immediate reset?

On 32-bit ARM, atomic64_set() and atomic64_read() are relaxed and include
no memory barriers.  If twd_wdt_start() runs on CPU1 while CPU0 is exiting
a power-down idle state with interrupts disabled, the IPI from
twd_wdt_call() waits until CPU0 re-enables interrupts:

CPU1                              CPU0 (IRQs disabled)
twd_wdt_start()                   cpu_pm_exit()
  twd_wdt_set_armed(..., true)      twd_wdt_cpu_pm_notify()
    atomic64_set(&wdt->keepalive,     if (READ_ONCE(wdt->armed) && ...)
                 now)                   twd_wdt_cpu_start()
    WRITE_ONCE(wdt->armed, true)          twd_wdt_ticks()
    twd_wdt_call(...)                       atomic64_read(&wdt->keepalive)
                                              --> sees 0 or stale timestamp
                                            left <= 0 -> returns 1
                                          writel(1, TWD_WDT_LOAD)  <-- reset

Pairing smp_store_release(&wdt->armed, armed) in twd_wdt_set_armed() with
smp_load_acquire(&wdt->armed) in twd_wdt_cpu_pm_notify() would order the
keepalive store before wdt->armed becomes visible.

[ ... ]

> +static int twd_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
> +{
> +	wdd->timeout = t;
> +	if (watchdog_active(wdd) || watchdog_hw_running(wdd))
> +		twd_wdt_ping(wdd);
> +
> +	return 0;
> +}

[Severity: Medium]
Can decreasing wdd->timeout in twd_wdt_set_timeout() reset the board
immediately if a clock rate change or CPU0 idle exit happens before
twd_wdt_ping() updates wdt->keepalive?

watchdog_set_timeout() holds wd_data->lock, not wdt->lock.  When userspace
reduces the timeout (for example from 60s to 30s when 35s have elapsed
since the last keepalive), wdd->timeout is updated before twd_wdt_ping()
acquires wdt->lock and refreshes wdt->keepalive:

CPU0                              CPU1
twd_wdt_clk_notify()              twd_wdt_set_timeout(wdd, 30)
  guard(mutex)(&wdt->lock)          wdd->timeout = 30;
                                    twd_wdt_ping()
                                      guard(mutex)(&wdt->lock)  [blocks]
  twd_wdt_call(...,
               twd_wdt_cpu_ping)
    twd_wdt_ticks()
      expires = old_keepalive + 30s
      left = -5s <= 0 -> returns 1
    writel(1, TWD_WDT_LOAD)  <-- immediate reset

In addition, watchdog_ioctl() for WDIOC_SETTIMEOUT in
drivers/watchdog/watchdog_dev.c already calls watchdog_ping(wdd) right
after watchdog_set_timeout(), so calling twd_wdt_ping() here sends a
redundant IPI to CPU0 on every WDIOC_SETTIMEOUT ioctl.

[ ... ]

> +	if (wdt->running) {
> +		/* Left running by the bootloader: take it over without a stop */
> +		dev_info(dev, "running, taking over\n");
> +		twd_wdt_start(&wdt->wdd);
> +		set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
> +	}
> +
> +	wdt->pm_nb.notifier_call = twd_wdt_cpu_pm_notify;
> +	ret = cpu_pm_register_notifier(&wdt->pm_nb);
> +	if (ret)
> +		return ret;
> +
> +	ret = devm_add_action_or_reset(dev, twd_wdt_remove_pm_notifier, wdt);
> +	if (ret)
> +		return ret;
> +
> +	ret = devm_add_action_or_reset(dev, twd_wdt_disable, wdt);
> +	if (ret)
> +		return ret;

[Severity: Medium]
If cpu_pm_register_notifier() or the first devm_add_action_or_reset()
fails when wdt->running is true, does twd_wdt_probe() return with the
reprogrammed watchdog still armed and its clock disabled by devres?

twd_wdt_start() reprograms the hardware counter to wdd.timeout (default
30s, which may be much shorter than the bootloader's timeout) before the
twd_wdt_disable cleanup action and CPU PM notifier are registered.
Registering the CPU PM notifier and twd_wdt_disable action before taking
over the running watchdog would avoid leaving it armed on error or losing
its state if CPU0 enters idle during probe.

[ ... ]

> +/* The watchdog loses its state in suspend, set it up again at resume */
> +static int twd_wdt_suspend(struct device *dev)
> +{
> +	struct twd_wdt *wdt = dev_get_drvdata(dev);
> +
> +	wdt->resume_armed = wdt->armed;
> +	if (wdt->resume_armed)
> +		twd_wdt_stop(&wdt->wdd);
> +
> +	return 0;
> +}
> +
> +static int twd_wdt_resume(struct device *dev)
> +{
> +	struct twd_wdt *wdt = dev_get_drvdata(dev);
> +
> +	if (wdt->resume_armed)
> +		twd_wdt_start(&wdt->wdd);
> +
> +	return 0;
> +}

[Severity: Medium]
Does calling twd_wdt_stop() and twd_wdt_start() across suspend and resume
reset wdt->keepalive to ktime_get_ns(), contrary to the commit message?

The commit message notes that on resume from suspend the time of the last
keepalive is kept and the watchdog is loaded with the time left from it so
the timeout is not extended.  However, twd_wdt_stop() and twd_wdt_start()
both call twd_wdt_set_armed(), which overwrites wdt->keepalive with
ktime_get_ns() and reloads a full timeout on resume.

Also, twd_wdt_probe() does not call watchdog_stop_ping_on_suspend(), and
twd_wdt_suspend() reads wdt->armed without wdt->lock while leaving
WDOG_HW_RUNNING set, so watchdog_ping_work() can run after
twd_wdt_suspend() and write TWD_WDT_LOAD while the device is suspended.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-arm-twd-wdt-v5-1-886265008e78@gmail.com?part=1

      reply	other threads:[~2026-11-18  9:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:50 [PATCH v5] watchdog: Add ARM TWD (MPCore private) watchdog driver Fil Dunsky via B4 Relay
2026-11-18  9:12 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-220535@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=filipp.dunsky@gmail.com \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox