watchdog: pika_wdt: portability and timer lifecycle fixes
v3 cover watchdog: pika_wdt: portability and timer lifecycle fixes
Fix two timer lifecycle bugs in the PIKA watchdog driver:
- pikawdt_release() could race with a concurrent open() due to clearing the open bit before stopping the timer.
- pikawdt_exit() unmapped FPGA memory without stopping the ping timer first, potentially executing the handler on freed memory.
Then replace powerpc-specific I/O accessors with generic ones to allow COMPILE_TEST builds on other architectures.
v3: fix sashiko reported issues
v2: fix preexisting issues
Rosen Penev (2):
watchdog: pika_wdt: fix timer lifecycle bugs
watchdog: pika_wdt: enable COMPILE_TEST builds
drivers/watchdog/Kconfig | 3 ++- drivers/watchdog/pika_wdt.c | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-)
--
2.54.0
PATCH v3 1/2 watchdog: pika_wdt: fix timer lifecycle bugs
Two timer lifecycle bugs in the PIKA watchdog driver:
1. pikawdt_release() clears the open bit before stopping the timer, allowing a concurrent pikawdt_open() to start a new timer that is then immediately deleted. Move clear_bit after the timer stop. Use timer_shutdown_sync() so the handler cannot re-arm after the call returns. Add timer_setup() in pikawdt_open() to re-initialize the timer for the next open.
2. pikawdt_exit() unmaps FPGA IO memory without first stopping the ping timer. If the timer handler fires after iounmap(), it executes pikawdt_ping() on freed memory. Use timer_shutdown_sync() which permanently prevents re-arming.
--- drivers/watchdog/pika_wdt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/watchdog/pika_wdt.c +3 -1
@@ -117,6 +117,7 @@ static int pikawdt_open(struct inode *inode, struct file *file)if (test_and_set_bit (0 , &pikawdt_private.open))return -EBUSY ; +timer_setup (&pikawdt_private.timer, pikawdt_ping,0 );pikawdt_start ();return stream_open (inode, file);
@@ -129,7 +130,7 @@ static int pikawdt_release(struct inode *inode, struct file *file) {/* stop internal ping */ if (!pikawdt_private.expect_close) -timer_delete (&pikawdt_private.timer); +timer_shutdown_sync (&pikawdt_private.timer);clear_bit (0 , &pikawdt_private.open); pikawdt_private.expect_close =0 ;
@@ -291,6 +292,7 @@ static void __exit pikawdt_exit(void) {misc_deregister (&pikawdt_miscdev); +timer_shutdown_sync (&pikawdt_private.timer);iounmap (pikawdt_private.fpga); }
--
2.54.0
[email protected]
AI reviewRe: [PATCHv3 1/2] watchdog: pika_wdt: fix timer lifecycle bugs
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Calling timer_setup() unconditionally in pikawdt_open() corrupts the active timer wheel if the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Calling timer_setup() unconditionally in pikawdt_open() corrupts the active timer wheel if the device was previously closed gracefully (magic close). --
commit cf2334cde532efba699588386b4f5a28da397535
Author: Rosen Penev <[email protected]>
watchdog: pika_wdt: fix timer lifecycle bugs
This commit updates the PIKA watchdog driver to use timer_shutdown_sync() during device release and module exit. It also adds a timer_setup() call to pikawdt_open() to re-initialize the timer for subsequent opens.
drivers/watchdog/pika_wdt.c static int pikawdt_open(struct inode *inode, struct file *file) · line 120
Quoted 9 lines
drivers/watchdog/pika_wdt.c +1
-0@@ -117,6 +117,7 @@ static int pikawdt_open(struct inode *inode, struct file *file)if (test_and_set_bit (0 , &pikawdt_private.open))return -EBUSY ; +timer_setup (&pikawdt_private.timer, pikawdt_ping,0 );
[Severity: Critical]
Does this code corrupt the active timer wheel if the device was previously closed gracefully?
The watchdog driver supports a "magic close" feature where writing 'V' before closing sets expect_close. During pikawdt_release(), if expect_close is set, the internal ping timer is intentionally left running:
static int pikawdt_release(struct inode *inode, struct file *file)
{
/* stop internal ping */
if (!pikawdt_private.expect_close)
timer_shutdown_sync(&pikawdt_private.timer);
clear_bit(0, &pikawdt_private.open);
...
}Because the open bit is cleared, a subsequent open is allowed. When re-opened, this new timer_setup() is invoked unconditionally on the actively queued timer.
Calling timer_setup() on a pending timer overwrites its internal linked list pointers, which can lead to a panic when the timer softirq processes the wheel.
Quoted 3 lines
drivers/watchdog/pika_wdt.c +0
-0pikawdt_start ();return stream_open (inode, file);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerRe: [PATCHv3 1/2] watchdog: pika_wdt: fix timer lifecycle bugs
In reply to Rosen PenevQuoted 8 lines · Rosen Penev
Two timer lifecycle bugs in the PIKA watchdog driver:
1.
pikawdt_release()clears the open bit before stopping the timer, allowing a concurrentpikawdt_open()to start a new timer that is then immediately deleted. Moveclear_bitafter the timer stop. Usetimer_shutdown_sync()so the handler cannot re-arm after the call returns. Addtimer_setup()inpikawdt_open()to re-initialize the timer for the next open.
The clear bit is not moved, and the approach is wrong. More on that below.
Quoted 17 lines
2.
pikawdt_exit()unmaps FPGA IO memory without first stopping the ping timer. If the timer handler fires afteriounmap(), it executespikawdt_ping()on freed memory. Usetimer_shutdown_sync()which permanently prevents re-arming.Assisted-by opencode:big-pickleSigned-off-by Rosen Penev <[email protected]>--- drivers/watchdog/pika_wdt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/watchdog/pika_wdt.c +0
-0
drivers/watchdog/pika_wdt.c static int pikawdt_open(struct inode *inode, struct file *file) · line 120
Quoted 4 lines
drivers/watchdog/pika_wdt.c +1
-0@@ -117,6 +117,7 @@ static int pikawdt_open(struct inode *inode, struct file *file)if (test_and_set_bit (0 , &pikawdt_private.open))return -EBUSY ; +timer_setup (&pikawdt_private.timer, pikawdt_ping,0 );
Calling timer_setup() in the open function is wrong because the timer may already be running.
drivers/watchdog/pika_wdt.c static int pikawdt_release(struct inode *inode, struct file *file) · line 133
Quoted 9 lines
drivers/watchdog/pika_wdt.c +1
-1pikawdt_start ();return stream_open (inode, file);@@ -129,7 +130,7 @@ static int pikawdt_release(struct inode *inode, struct file *file) {/* stop internal ping */ if (!pikawdt_private.expect_close) -timer_delete (&pikawdt_private.timer); +timer_shutdown_sync (&pikawdt_private.timer);
This is wrong here. timer_shutdown_sync() is only supposed to be called if the timer will not be re-enabled. Anything else would be an API violation.
Quoted 15 lines
drivers/watchdog/pika_wdt.c +1
-1clear_bit (0 , &pikawdt_private.open); pikawdt_private.expect_close =0 ;@@ -291,6 +292,7 @@ static void __exit pikawdt_exit(void) {misc_deregister (&pikawdt_miscdev); +timer_shutdown_sync (&pikawdt_private.timer);iounmap (pikawdt_private.fpga); } --2.54.0
PATCH v3 2/2 watchdog: pika_wdt: enable COMPILE_TEST builds
In reply to Rosen PenevReplace powerpc-specific I/O accessors (in_be32/out_be32) with generic ones (ioread32be/iowrite32be) so the driver can be compile-tested on other architectures. Relax the Kconfig dependency accordingly and add the required HAS_IOMEM.
--- drivers/watchdog/Kconfig | 3 ++- drivers/watchdog/pika_wdt.c | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/watchdog/Kconfig +2 -1
@@ -2040,7 +2040,8 @@ config 8xxx_WDT config PIKA_WDT tristate "PIKA FPGA Watchdog" - depends on WARP || (PPC64 && COMPILE_TEST) + depends on WARP || COMPILE_TEST + depends on HAS_IOMEM default WARP help This enables the watchdog in the PIKA FPGA. Currently used on
drivers/watchdog/pika_wdt.c +4 -4
@@ -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 ); }/*
@@ -243,7 +243,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" );
@@ -265,7 +265,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 ;
--
2.54.0
[email protected]
AI reviewRe: [PATCHv3 2/2] watchdog: pika_wdt: enable COMPILE_TEST builds
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing timer_delete_sync in the module's __exit function leads to a Use-After-Free and kernel panic on module unload.
- [High] A race condition in pikawdt_release using a non-synchronous timer_delete can resurrect the ping timer and permanently defeat the watchdog on unexpected close. --
commit 6bf97a13e2f6f235845375a27efabc19c1cdb946
Author: Rosen Penev <[email protected]>
watchdog: pika_wdt: enable COMPILE_TEST builds
This commit replaces PowerPC-specific I/O accessors with generic ones in the PIKA FPGA watchdog driver. It updates the Kconfig to allow COMPILE_TEST builds on other architectures and adds the required HAS_IOMEM dependency.
Quoted 4 lines
drivers/watchdog/Kconfig +0
-0
[ ... ]
drivers/watchdog/pika_wdt.c
Quoted 4 lines
drivers/watchdog/pika_wdt.c +0
-0
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but while reviewing this driver I noticed a potential race condition in the watchdog release handler:
drivers/watchdog/pika_wdt.c:pikawdt_release() {
...
/* stop internal ping */
if (!pikawdt_private.expect_close)
timer_delete(&pikawdt_private.timer);
clear_bit(0, &pikawdt_private.open);
...
}If the device is closed without the magic character, pikawdt_release() calls timer_delete(). If pikawdt_ping() is executing concurrently on another CPU:
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);
}
...
}Could timer_delete() return immediately while the concurrent timer handler is running, allowing clear_bit(0, &pikawdt_private.open) to execute before the handler checks the open bit?
If so, would the concurrent handler evaluate (!nowayout && !pikawdt_private.open) as true, re-arm itself via mod_timer(), and continue to ping the watchdog forever?
Should timer_delete_sync() be used here instead?
[Severity: High]
This is a pre-existing issue, but I also noticed a potential use-after-free during module unload:
drivers/watchdog/pika_wdt.c:pikawdt_exit() {
misc_deregister(&pikawdt_miscdev);
iounmap(pikawdt_private.fpga);
}When the watchdog device is closed with the 'magic close' character, the ping timer is intentionally left running in the background. Since misc_deregister() allows the module reference count to reach 0, the module can be unloaded.
Because pikawdt_exit() doesn't call timer_delete_sync(), won't the timer continue to execute pikawdt_ping() from freed module memory and attempt to access the unmapped pikawdt_private.fpga I/O region, leading to a kernel panic?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerRe: [PATCHv3 2/2] watchdog: pika_wdt: enable COMPILE_TEST builds
In reply to Rosen PenevQuoted 7 lines · Rosen Penev
Replace powerpc-specific I/O accessors (in_be32/out_be32) with generic ones (ioread32be/iowrite32be) so the driver can be compile-tested on other architectures. Relax the
Kconfigdependency accordingly and add the requiredHAS_IOMEM.Assisted-by opencode:big-pickleSigned-off-by Rosen Penev <[email protected]>
I really don't think this is worth the trouble just to be able to support COMPILE_TEST on all architectures. This is an old watchdog driver. It should not be touched except to fix bugs.
Didn't I say that before ?
Thanks,
Guenter
Re: [PATCHv3 2/2] watchdog: pika_wdt: enable COMPILE_TEST builds
Quoted 10 lines · Guenter Roeck
Quoted 7 lines · Rosen Penev
Replace powerpc-specific I/O accessors (in_be32/out_be32) with generic ones (ioread32be/iowrite32be) so the driver can be compile-tested on other architectures. Relax the
Kconfigdependency accordingly and add the requiredHAS_IOMEM.Assisted-by opencode:big-pickleSigned-off-by Rosen Penev <[email protected]>
Quoted 5 lines
I really don't think this is worth the trouble just to be able to support
COMPILE_TESTon all architectures. This is an old watchdog driver. It should not be touched except to fix bugs.Didn't I say that before ?
Sounds like this driver should be left alone then.
Quoted 3 lines
Thanks,
Guenter
Guenter Roeck
maintainerRe: [PATCHv3 2/2] watchdog: pika_wdt: enable COMPILE_TEST builds
Quoted 11 lines · Rosen Penev
Quoted 10 lines · Guenter Roeck
Quoted 7 lines · Rosen Penev
Replace powerpc-specific I/O accessors (in_be32/out_be32) with generic ones (ioread32be/iowrite32be) so the driver can be compile-tested on other architectures. Relax the
Kconfigdependency accordingly and add the requiredHAS_IOMEM.Assisted-by opencode:big-pickleSigned-off-by Rosen Penev <[email protected]>
Quoted 6 lines
Quoted 5 lines
I really don't think this is worth the trouble just to be able to support
COMPILE_TESTon all architectures. This is an old watchdog driver. It should not be touched except to fix bugs.Didn't I say that before ?
Sounds like this driver should be left alone then.
Very much so. The driver is for a FPGA based system from around 2007, and the company behind it went bankrupt in 2022. I would be surprised if the current Linux kernel even boots on it.
Guenter