Linux Watchdog driver development
 help / color / mirror / Atom feed
From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Wim Van Sebroeck <wim@linux-watchdog.org>,
	Guenter Roeck <linux@roeck-us.net>,
	Aaro Koskinen <aaro.koskinen@iki.fi>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Cavium Inc <support@cavium.com>,
	David Daney <ddaney@caviumnetworks.com>,
	linux-watchdog@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH 5/6] watchdog: octeon-wdt: Serialise countdown and IRQ state with raw spinlock
Date: Thu,  3 Dec 2026 17:30:08 +0000	[thread overview]
Message-ID: <20261203173039.1142495-6-lee@kernel.org> (raw)
In-Reply-To: <20261203173039.1142495-1-lee@kernel.org>

octeon_wdt_poke_irq() runs in hardirq context (IRQF_NO_THREAD) and
updates per_cpu_countdown[], disables the watchdog IRQ, and clears
irq_enabled_cpus without locking.  Concurrently, octeon_wdt_ping()
writes per_cpu_countdown[] and tests irq_enabled_cpus in process
context.  This allows octeon_wdt_ping() to miss the cleared bit and
leave the watchdog IRQ permanently disabled, or lose a countdown reset.

Protect per_cpu_countdown[], irq_enabled_cpus, and do_countdown with a
raw_spinlock_t across the IRQ handler, ping, start, stop, and CPU
hotplug callbacks, deferring enable_irq() and panic() until after the
lock is released, and guard octeon_wdt_ping()'s online-CPU iteration
with cpus_read_lock().

Fixes: 4c076fb41ac9 ("WATCHDOG: Add watchdog driver for OCTEON SOCs")
Closes: https://sashiko.dev/#/bug/linux-1d98fa88-4055-42e9-b51b-69f0073793a8
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
 drivers/watchdog/octeon-wdt-main.c | 43 ++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 5 deletions(-)

diff --git a/drivers/watchdog/octeon-wdt-main.c b/drivers/watchdog/octeon-wdt-main.c
index d29a63899847..718617437542 100644
--- a/drivers/watchdog/octeon-wdt-main.c
+++ b/drivers/watchdog/octeon-wdt-main.c
@@ -55,6 +55,7 @@
 #include <linux/cpu.h>
 #include <linux/irq.h>
 #include <linux/irqdomain.h>
+#include <linux/spinlock.h>
 #include <linux/stringify.h>
 
 #include <asm/mipsregs.h>
@@ -85,6 +86,7 @@ static unsigned int countdown_reset;
 static unsigned int per_cpu_countdown[NR_CPUS];
 
 static cpumask_t irq_enabled_cpus;
+static DEFINE_RAW_SPINLOCK(octeon_wdt_lock);
 
 #define WD_TIMO 60			/* Default heartbeat = 60 seconds */
 
@@ -138,7 +140,10 @@ static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
 	int cpu = raw_smp_processor_id();
 	unsigned int core = cpu2core(cpu);
 	int node = cpu_to_node(cpu);
+	unsigned long flags;
+	bool should_panic = false;
 
+	raw_spin_lock_irqsave(&octeon_wdt_lock, flags);
 	if (do_countdown) {
 		if (per_cpu_countdown[cpu] > 0) {
 			/* We're alive, poke the watchdog */
@@ -148,13 +153,17 @@ static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
 			/* Bad news, you are about to reboot. */
 			disable_irq_nosync(cpl);
 			cpumask_clear_cpu(cpu, &irq_enabled_cpus);
-			if (panic_on_timeout)
-				panic("Watchdog timeout");
+			should_panic = panic_on_timeout;
 		}
 	} else {
 		/* Not open, just ping away... */
 		cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(core), 1);
 	}
+	raw_spin_unlock_irqrestore(&octeon_wdt_lock, flags);
+
+	if (should_panic)
+		panic("Watchdog timeout");
+
 	return IRQ_HANDLED;
 }
 
@@ -333,6 +342,7 @@ static int octeon_wdt_cpu_to_irq(int cpu)
 static int octeon_wdt_cpu_pre_down(unsigned int cpu)
 {
 	unsigned int core;
+	unsigned long flags;
 	int node;
 	union cvmx_ciu_wdogx ciu_wdog;
 
@@ -348,6 +358,10 @@ static int octeon_wdt_cpu_pre_down(unsigned int cpu)
 	cvmx_write_csr_node(node, CVMX_CIU_WDOGX(core), ciu_wdog.u64);
 
 	free_irq(octeon_wdt_cpu_to_irq(cpu), octeon_wdt_poke_irq);
+
+	raw_spin_lock_irqsave(&octeon_wdt_lock, flags);
+	cpumask_clear_cpu(cpu, &irq_enabled_cpus);
+	raw_spin_unlock_irqrestore(&octeon_wdt_lock, flags);
 	return 0;
 }
 
@@ -355,6 +369,7 @@ static int octeon_wdt_cpu_online(unsigned int cpu)
 {
 	unsigned int core;
 	unsigned int irq;
+	unsigned long flags;
 	union cvmx_ciu_wdogx ciu_wdog;
 	int node;
 	struct irq_domain *domain;
@@ -369,7 +384,10 @@ static int octeon_wdt_cpu_online(unsigned int cpu)
 	ciu_wdog.u64 = 0;
 	cvmx_write_csr_node(node, CVMX_CIU_WDOGX(core), ciu_wdog.u64);
 
+	raw_spin_lock_irqsave(&octeon_wdt_lock, flags);
 	per_cpu_countdown[cpu] = countdown_reset;
+	cpumask_set_cpu(cpu, &irq_enabled_cpus);
+	raw_spin_unlock_irqrestore(&octeon_wdt_lock, flags);
 
 	if (octeon_has_feature(OCTEON_FEATURE_CIU3)) {
 		/* Must get the domain for the watchdog block */
@@ -392,8 +410,6 @@ static int octeon_wdt_cpu_online(unsigned int cpu)
 		irq_set_affinity(irq, cpumask_of(cpu));
 	}
 
-	cpumask_set_cpu(cpu, &irq_enabled_cpus);
-
 	/* Poke the watchdog to clear out its state */
 	cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(core), 1);
 
@@ -415,18 +431,27 @@ static int octeon_wdt_ping(struct watchdog_device __always_unused *wdog)
 	if (disable)
 		return 0;
 
+	cpus_read_lock();
 	for_each_online_cpu(cpu) {
+		unsigned long flags;
+		bool need_enable = false;
+
 		coreid = cpu2core(cpu);
 		node = cpu_to_node(cpu);
+		raw_spin_lock_irqsave(&octeon_wdt_lock, flags);
 		cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(coreid), 1);
 		per_cpu_countdown[cpu] = countdown_reset;
 		if ((countdown_reset || !do_countdown) &&
 		    !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
 			/* We have to enable the irq */
-			enable_irq(octeon_wdt_cpu_to_irq(cpu));
 			cpumask_set_cpu(cpu, &irq_enabled_cpus);
+			need_enable = true;
 		}
+		raw_spin_unlock_irqrestore(&octeon_wdt_lock, flags);
+		if (need_enable)
+			enable_irq(octeon_wdt_cpu_to_irq(cpu));
 	}
+	cpus_read_unlock();
 	return 0;
 }
 
@@ -488,14 +513,22 @@ static int octeon_wdt_set_timeout(struct watchdog_device *wdog,
 
 static int octeon_wdt_start(struct watchdog_device *wdog)
 {
+	unsigned long flags;
+
 	octeon_wdt_ping(wdog);
+	raw_spin_lock_irqsave(&octeon_wdt_lock, flags);
 	do_countdown = 1;
+	raw_spin_unlock_irqrestore(&octeon_wdt_lock, flags);
 	return 0;
 }
 
 static int octeon_wdt_stop(struct watchdog_device *wdog)
 {
+	unsigned long flags;
+
+	raw_spin_lock_irqsave(&octeon_wdt_lock, flags);
 	do_countdown = 0;
+	raw_spin_unlock_irqrestore(&octeon_wdt_lock, flags);
 	octeon_wdt_ping(wdog);
 	return 0;
 }
-- 
2.56.1.911.g883d8bcb20-goog


  parent reply	other threads:[~2026-12-03 17:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 17:30 [PATCH 0/6] watchdog: Fix High issues reported by Sashiko Lee Jones
2026-12-03 17:30 ` [PATCH 1/6] watchdog: da9063: Stop ping worker on system suspend Lee Jones
2026-12-03 17:30 ` [PATCH 2/6] watchdog: dw_wdt: Guard shared IRQ handler during system suspend and resume Lee Jones
2026-12-03 17:30 ` [PATCH 3/6] watchdog: npcm: Enable clock before checking running state in probe Lee Jones
2026-12-03 17:30 ` [PATCH 4/6] watchdog: sbsa: Evaluate MediaTek WS0 workaround after IRQ fallback Lee Jones
2026-12-03 17:30 ` Lee Jones [this message]
2026-12-03 17:30 ` [PATCH 6/6] watchdog: rzv2h: Avoid 32-bit overflow in max_hw_heartbeat_ms calculation Lee Jones

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=20261203173039.1142495-6-lee@kernel.org \
    --to=lee@kernel.org \
    --cc=aaro.koskinen@iki.fi \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=ddaney@caviumnetworks.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=stable@vger.kernel.org \
    --cc=support@cavium.com \
    --cc=wim@linux-watchdog.org \
    /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