Skip to content

watchdog: core: Fix locking, lifetime, suspend, and state management bugs

Guenter Roeck linux-watchdog 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest8 patches 2 repliesIn review
  2. v18 patches 9 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8watchdog: core: Clear wd_data pointer on errorsdrivers/watchdog/watchdog_dev.c+7 -00 replies
1/8watchdog: core: Clear wd_data pointer on errorsdrivers/watchdog/watchdog_dev.c+7 -00 replies
2/8watchdog: core: Add missing locksdrivers/watchdog/watchdog_dev.c+36 -60 replies
2/8watchdog: core: Add missing locksdrivers/watchdog/watchdog_dev.c+36 -60 replies
3/8watchdog: core: Prevent ping worker from re-arming timer on suspenddrivers/watchdog/watchdog_core.h drivers/watchdog/watchdog_dev.c+12 -40 replies
3/8watchdog: core: Prevent ping worker from re-arming timer on suspenddrivers/watchdog/watchdog_core.h drivers/watchdog/watchdog_dev.c+12 -40 replies
4/8watchdog: core: Stop pretimeout hrtimer on suspenddrivers/watchdog/watchdog_dev.c drivers/watchdog/watchdog_hrtimer_pretimeout.c+5 -10 replies
4/8watchdog: core: Stop pretimeout hrtimer on suspenddrivers/watchdog/watchdog_dev.c drivers/watchdog/watchdog_hrtimer_pretimeout.c+5 -10 replies
5/8watchdog: core: Restore WDOG_HW_RUNNING if stopping watchdog failsdrivers/watchdog/watchdog_dev.c+1 -00 replies
5/8watchdog: core: Restore WDOG_HW_RUNNING if stopping watchdog failsdrivers/watchdog/watchdog_dev.c+1 -02 replies
6/8watchdog: core: Cancel timer if cdev_device_add() failsdrivers/watchdog/watchdog_dev.c+55 -360 replies
7/8watchdog: core: Fix unbalanced module_put() in watchdog_open()drivers/watchdog/watchdog_dev.c+2 -10 replies
8/8watchdog: core: Update last_keepalive in watchdog_start()drivers/watchdog/watchdog_dev.c+5 -10 replies

Reviews trailers per patch

All 13 patches have a Reviewed-by or Acked-by.

PatchTzung-Bi Shih
1/8R-b R-b
1/8R-b R-b
2/8R-b R-b
2/8R-b R-b
3/8R-b R-b
3/8R-b R-b
4/8R-b R-b
4/8R-b R-b
5/8R-b
5/8R-b
6/8R-b
7/8R-b
8/8R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/8] watchdog: core: Fix locking, lifetime, suspend, and state management bugs

This series fixes several locking, reference counting, object lifetime, suspend/resume, and state management bugs in the watchdog core character device and timer handling:

1. watchdog: core: Clear wd_data pointer on errors
Clear wdd->wd_data on all registration error paths in watchdog_cdev_register(), and clear wd_data->wdd under wd_data->lock when failing after misc_register() may have exposed the device to userspace, preventing dangling pointers.

2. watchdog: core: Add missing locks
Acquire wd_data->lock in watchdog_open(), across watchdog_stop() and pretimeout teardown in watchdog_cdev_unregister(), and in watchdog_set_last_hw_keepalive(). Also protect old_wd_data with old_wd_data_lock to prevent races when opening or unregistering /dev/watchdog.

3. watchdog: core: Prevent ping worker from re-arming timer on suspend Introduce a _WDOG_SUSPENDED flag in wd_data->status, set under wd_data->lock during suspend and cleared on resume, and check it in watchdog_worker_should_ping(), watchdog_need_worker(), and __watchdog_ping() so an in-flight worker or deferred ping cannot re-arm wd_data->timer while suspended.

4. watchdog: core: Stop pretimeout hrtimer on suspend
Stop the software pretimeout hrtimer in watchdog_dev_suspend(), prevent watchdog_hrtimer_pretimeout_start() from arming it while _WDOG_SUSPENDED is set, and restart it in watchdog_dev_resume() if the hardware watchdog is running.

5. watchdog: core: Restore WDOG_HW_RUNNING if stopping watchdog fails In watchdog_stop(), WDOG_HW_RUNNING is cleared before calling wdd->ops->stop(). Restore WDOG_HW_RUNNING if wdd->ops->stop() returns an error so the core continues to track the running hardware state and retains its module and device references.

v2: Move restore of WDOG_HW_RUNNING to stop() error handler

6. watchdog: core: Cancel timer if cdev_device_add() fails Initialize wd_data and acquire running-watchdog module and device references prior to calling misc_register(). On registration failure (and in watchdog_cdev_unregister()), stop the watchdog if appropriate, stop the pretimeout hrtimer, release unclaimed running-watchdog references if the device is not open, and cancel wd_data->timer and wd_data->work before dropping the initial device reference.

7. watchdog: core: Fix unbalanced module_put() in watchdog_open() In watchdog_open(), try_module_get() is skipped when hw_running is true because the module reference was already taken when the running watchdog was registered. Guard module_put() on the watchdog_start() error path with !hw_running to avoid dropping a reference that watchdog_open() did not acquire.

8. watchdog: core: Update last_keepalive in watchdog_start() When starting a watchdog whose hardware is already running, set WDOG_ACTIVE and update wd_data->last_keepalive to started_at before calling __watchdog_ping() so that watchdog_get_timeleft() reports the correct remaining time and watchdog_update_worker() inside __watchdog_ping() evaluates the active state and new keepalive timestamp rather than a potentially expired open_deadline. If __watchdog_ping() fails, clear WDOG_ACTIVE and update the worker.

Disclaimer: I started this series to fix a number of bugs reported by Sashiko in the watchdog core. After several fix-review rounds, I did not get closer to fixing all issues reported by Sashiko; either my patches turned out to be incomplete or buggy. I finally gave up and fed Sashiko's review feedback into an AI engine, asking it to fix the reported problems. It still took some 10+ rounds of review/fix, but the resulting patches should fix at least the most critical race conditions in the watchdog core.

Given the complexity of the changes, the plan is to apply the series during the next commit window, to be released with v7.5, and to eventually back-port it to older kernel branches.

----------------------------------------------------------------
Guenter Roeck (8):
watchdog: core: Clear wd_data pointer on errors
watchdog: core: Add missing locks
watchdog: core: Prevent ping worker from re-arming timer on suspend
watchdog: core: Stop pretimeout hrtimer on suspend
watchdog: core: Restore WDOG_HW_RUNNING if stopping watchdog fails
watchdog: core: Cancel timer if cdev_device_add() fails
watchdog: core: Fix unbalanced module_put() in watchdog_open()
watchdog: core: Update last_keepalive in watchdog_start()

 drivers/watchdog/watchdog_core.h               |   1 +
 drivers/watchdog/watchdog_dev.c                | 155 ++++++++++++++++++-------
 drivers/watchdog/watchdog_hrtimer_pretimeout.c |   3 +-
 3 files changed, 117 insertions(+), 42 deletions(-)