Re: [PATCH v4 5/7] watchdog: pretimeout: Protect governor access with RCU for NMI safety
From: Guenter Roeck
Date: Sun Nov 15 2026 - 17:51:23 EST
On Wed, Nov 11, 2026 at 07:16:27PM -0800, Mayank Rungta wrote:
> When watchdog pretimeout interrupts are configured as NMIs (or
> pseudo-NMIs), pretimeout handlers execute in NMI context. Accessing
> the watchdog governor via spinlocks in watchdog_notify_pretimeout() is
> unsafe in NMI context and can cause deadlocks if an NMI arrives while a
> spinlock is held.
>
> Protect governor assignment and dereference with RCU instead of
> spinlocks, allowing lockless, NMI-safe governor notifications while
> preserving safe runtime governor switching.
>
> Reviewed-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
> Signed-off-by: Mayank Rungta <mrungta@xxxxxxxxxx>
This patch is independent of the rest of the series, so I applied it
to wachdog-next.
Thanks,
Guenter
> ---
> drivers/watchdog/watchdog_pretimeout.c | 45 ++++++++++++++------------
> include/linux/watchdog.h | 2 +-
> 2 files changed, 26 insertions(+), 21 deletions(-)
>
> diff --git a/drivers/watchdog/watchdog_pretimeout.c b/drivers/watchdog/watchdog_pretimeout.c
> index 02e09b9e396d..7fb586c77403 100644
> --- a/drivers/watchdog/watchdog_pretimeout.c
> +++ b/drivers/watchdog/watchdog_pretimeout.c
> @@ -4,6 +4,7 @@
> */
>
> #include <linux/list.h>
> +#include <linux/rcupdate.h>
> #include <linux/slab.h>
> #include <linux/spinlock.h>
> #include <linux/string.h>
> @@ -67,12 +68,14 @@ int watchdog_pretimeout_available_governors_get(char *buf)
>
> int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf)
> {
> + const struct watchdog_governor *gov;
> int count = 0;
>
> - spin_lock_irq(&pretimeout_lock);
> - if (wdd->gov)
> - count = sysfs_emit(buf, "%s\n", wdd->gov->name);
> - spin_unlock_irq(&pretimeout_lock);
> + rcu_read_lock();
> + gov = rcu_dereference(wdd->gov);
> + if (gov)
> + count = sysfs_emit(buf, "%s\n", gov->name);
> + rcu_read_unlock();
>
> return count;
> }
> @@ -91,7 +94,7 @@ int watchdog_pretimeout_governor_set(struct watchdog_device *wdd,
> }
>
> spin_lock_irq(&pretimeout_lock);
> - wdd->gov = priv->gov;
> + rcu_assign_pointer(wdd->gov, priv->gov);
> spin_unlock_irq(&pretimeout_lock);
>
> mutex_unlock(&governor_lock);
> @@ -101,16 +104,13 @@ int watchdog_pretimeout_governor_set(struct watchdog_device *wdd,
>
> void watchdog_notify_pretimeout(struct watchdog_device *wdd)
> {
> - unsigned long flags;
> + const struct watchdog_governor *gov;
>
> - spin_lock_irqsave(&pretimeout_lock, flags);
> - if (!wdd->gov) {
> - spin_unlock_irqrestore(&pretimeout_lock, flags);
> - return;
> - }
> -
> - wdd->gov->pretimeout(wdd);
> - spin_unlock_irqrestore(&pretimeout_lock, flags);
> + rcu_read_lock();
> + gov = rcu_dereference(wdd->gov);
> + if (gov)
> + gov->pretimeout(wdd);
> + rcu_read_unlock();
> }
> EXPORT_SYMBOL_GPL(watchdog_notify_pretimeout);
>
> @@ -140,8 +140,8 @@ int watchdog_register_governor(struct watchdog_governor *gov)
> default_gov = gov;
>
> list_for_each_entry(p, &pretimeout_list, entry)
> - if (!p->wdd->gov)
> - p->wdd->gov = default_gov;
> + if (!rcu_access_pointer(p->wdd->gov))
> + rcu_assign_pointer(p->wdd->gov, default_gov);
> spin_unlock_irq(&pretimeout_lock);
> }
>
> @@ -170,11 +170,14 @@ void watchdog_unregister_governor(struct watchdog_governor *gov)
> if (default_gov == gov)
> default_gov = NULL;
> list_for_each_entry(p, &pretimeout_list, entry)
> - if (p->wdd->gov == gov)
> - p->wdd->gov = default_gov;
> + if (rcu_dereference_protected(p->wdd->gov,
> + lockdep_is_held(&pretimeout_lock)) == gov)
> + rcu_assign_pointer(p->wdd->gov, default_gov);
> spin_unlock_irq(&pretimeout_lock);
>
> mutex_unlock(&governor_lock);
> +
> + synchronize_rcu();
> }
> EXPORT_SYMBOL(watchdog_unregister_governor);
>
> @@ -192,7 +195,7 @@ int watchdog_register_pretimeout(struct watchdog_device *wdd)
> spin_lock_irq(&pretimeout_lock);
> list_add(&p->entry, &pretimeout_list);
> p->wdd = wdd;
> - wdd->gov = default_gov;
> + rcu_assign_pointer(wdd->gov, default_gov);
> spin_unlock_irq(&pretimeout_lock);
>
> return 0;
> @@ -206,7 +209,7 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
> return;
>
> spin_lock_irq(&pretimeout_lock);
> - wdd->gov = NULL;
> + rcu_assign_pointer(wdd->gov, NULL);
>
> list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
> if (p->wdd == wdd) {
> @@ -216,4 +219,6 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
> }
> }
> spin_unlock_irq(&pretimeout_lock);
> +
> + synchronize_rcu();
> }
> diff --git a/include/linux/watchdog.h b/include/linux/watchdog.h
> index 29cd03686154..5a3c35968cc8 100644
> --- a/include/linux/watchdog.h
> +++ b/include/linux/watchdog.h
> @@ -105,7 +105,7 @@ struct watchdog_device {
> const struct attribute_group **groups;
> const struct watchdog_info *info;
> const struct watchdog_ops *ops;
> - const struct watchdog_governor *gov;
> + const struct watchdog_governor __rcu *gov;
> unsigned int bootstatus;
> unsigned int timeout;
> unsigned int pretimeout;