Skip to contentSkip to conversation

watchdog: pika_wdt: replace in_be32/out_be32 with ioread32be/iowrite32be

PATCH In review Rosen Penev 3 participants 3 messages Jun 2 – Jun 7 Watchdog device drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 watchdog: pika_wdt: replace in_be32/out_be32 with ioread32be/iowrite32be

Convert the ppc4xx-specific in_be32/out_be32 to the portable ioread32be/iowrite32be. Also relax the Kconfig dependency from (PPC64 && COMPILE_TEST) to just COMPILE_TEST to get build coverage.

Assisted-by opencode:big-pickle
---
 drivers/watchdog/Kconfig    | 2 +-
 drivers/watchdog/pika_wdt.c | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/watchdog/Kconfig +1 -1
index 8319c503319a..7c5d6f642a96 100644
@@ -2040,7 +2040,7 @@ config 8xxx_WDT
 
 config PIKA_WDT
 	tristate "PIKA FPGA Watchdog"
-	depends on WARP || (PPC64 && COMPILE_TEST)
+	depends on WARP || COMPILE_TEST
 	default WARP
 	help
 	  This enables the watchdog in the PIKA FPGA. Currently used on
drivers/watchdog/pika_wdt.c +4 -4
index 87b8988d2520..2d3c201540a9 100644
@@ -77,10 +77,10 @@ static inline void pikawdt_reset(void)
 	 *           seconds. Valid ranges are 1 to 15 seconds. The value can
 	 *           be modified dynamically.
 	 */
-	unsigned reset = in_be32(pikawdt_private.fpga + 0x14);
+	unsigned reset = ioread32be(pikawdt_private.fpga + 0x14);
 	/* enable with max timeout - 15 seconds */
 	reset |= (1 << 7) + (WDT_HW_TIMEOUT << 8);
-	out_be32(pikawdt_private.fpga + 0x14, reset);
+	iowrite32be(reset, pikawdt_private.fpga + 0x14);
 }
 
 /*
@@ -242,7 +242,7 @@ static int __init pikawdt_init(void)
 		return -ENOMEM;
 	}
 
-	ident.firmware_version = in_be32(pikawdt_private.fpga + 0x1c) & 0xffff;
+	ident.firmware_version = ioread32be(pikawdt_private.fpga + 0x1c) & 0xffff;
 
 	/* POST information is in the sd area. */
 	np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
@@ -264,7 +264,7 @@ static int __init pikawdt_init(void)
 	 * Bit 31,   WDOG: Set to 1 when the last reset was caused by a watchdog
 	 *           timeout.
 	 */
-	post1 = in_be32(fpga + 0x40);
+	post1 = ioread32be(fpga + 0x40);
 	if (post1 & 0x80000000)
 		pikawdt_private.bootstatus = WDIOF_CARDRESET;
 
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [Low] Relaxing Kconfig dependency allows compilation on unsupported architectures, causing build failures.

Pre-existing issues:
- [High] The module fails to delete its background timer on exit, leading to a use-after-free and kernel panic.
- [High] A race condition during an unexpected close allows the watchdog timer to re-arm indefinitely, failing to reset the machine. --

commit 432026085d9a8c09851b8a2cd20ef5d62df73ad1
Author: Rosen Penev <[email protected]>

watchdog: pika_wdt: replace in_be32/out_be32 with ioread32be/iowrite32be

Convert the ppc4xx-specific in_be32/out_be32 to the portable ioread32be/iowrite32be. Also relax the Kconfig dependency from (PPC64 && COMPILE_TEST) to just COMPILE_TEST to get build coverage.

drivers/watchdog/Kconfig config 8xxx_WDT · line 2068

Quoted 10 lines
drivers/watchdog/Kconfig +1 -1
index dc78729ba2a5d..a7e53091f27c5 100644
@@ -2065,7 +2065,7 @@ config 8xxx_WDT
 
 config PIKA_WDT
 	tristate "PIKA FPGA Watchdog"
-	depends on WARP || (PPC64 && COMPILE_TEST)
+	depends on WARP || COMPILE_TEST

[Severity: Low]
Will relaxing this dependency cause build failures on architectures without I/O memory support?

Because the driver utilizes memory-mapped I/O functions like ioread32be() and iowrite32be(), removing the implicit requirement for HAS_IOMEM (which PPC64 previously satisfied) might allow it to be compiled on architectures that lack I/O memory (such as S390 without PCI).

Should this include a dependency on HAS_IOMEM?

[ ... ]

[Severity: High]
This is a pre-existing issue, but does pikawdt_exit() leak a running timer?

If the watchdog device is closed properly via the magic close character, the driver leaves the timer running in the background. If the module is then unloaded, the exit function doesn't stop the timer:

drivers/watchdog/pika_wdt.c:pikawdt_exit() {
        misc_deregister(&pikawdt_miscdev);
        iounmap(pikawdt_private.fpga);
}

When the kernel timer wheel expires and attempts to execute pikawdt_ping() in unmapped memory, will this result in a use-after-free and a kernel panic?

Could a call to timer_delete_sync() be added here to prevent this?

[Severity: High]
This isn't a bug introduced by this patch, but is there a race condition during an unexpected close that defeats the watchdog reset?

In pikawdt_release(), an unexpected close attempts to stop the ping timer:

drivers/watchdog/pika_wdt.c:pikawdt_release() {
        ...
        if (!pikawdt_private.expect_close)
                timer_delete(&pikawdt_private.timer);

        clear_bit(0, &pikawdt_private.open);
        ...
}

Because timer_delete() does not synchronize with concurrently running handlers, if pikawdt_ping() is executing on another CPU, it can evaluate its condition as true and call mod_timer(), re-arming the timer after it was deleted:

drivers/watchdog/pika_wdt.c:pikawdt_ping() {
        ...
        if (time_before(jiffies, pikawdt_private.next_heartbeat) ||
                        (!nowayout && !pikawdt_private.open)) {
                pikawdt_reset();
                mod_timer(&pikawdt_private.timer, jiffies + WDT_TIMEOUT);
        }
        ...
}

Since pikawdt_release() clears the open bit immediately after, the improperly re-armed timer will evaluate its fallback condition (!nowayout && !pikawdt_private.open) as true on subsequent ticks.

Won't this cause it to pat the watchdog forever, completely preventing a system reset during an unexpected daemon termination?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Rosen Penev
Quoted 6 lines · Rosen Penev

Convert the ppc4xx-specific in_be32/out_be32 to the portable ioread32be/iowrite32be. Also relax the Kconfig dependency from (PPC64 && COMPILE_TEST) to just COMPILE_TEST to get build coverage.

Assisted-by opencode:big-pickle

This is an old watchdog driver, not using the watchdog subsystem. Please leave such drivers alone unless fixing a real bug.

Guenter