This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 4ae4faf97306aac8183f88379b3638dee1be4877
Author: Bowen Wang <[email protected]>
AuthorDate: Fri Nov 6 00:21:04 2026 +0800

    drivers/timers/watchdog: detach the channels under the lock on unregister
    
    watchdog_unregister() freed the supervision channels while a keepalive
    trigger running in interrupt context, or on another cpu, could still be
    walking them in watchdog_channel_expired(). Detach the list under
    upper->slock and free the channels outside of it.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/timers/watchdog.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/timers/watchdog.c b/drivers/timers/watchdog.c
index 589668cf57f..5fdffd23c8f 100644
--- a/drivers/timers/watchdog.c
+++ b/drivers/timers/watchdog.c
@@ -1246,6 +1246,8 @@ void watchdog_unregister(FAR void *handle)
   FAR struct watchdog_lowerhalf_s *lower;
 #ifdef CONFIG_WATCHDOG_AUTOMONITOR_TASKS
   FAR struct watchdog_user_s *user;
+  struct list_node users;
+  irqstate_t flags;
 #endif
 
   /* Recover the pointer to the upper-half driver state */
@@ -1267,7 +1269,17 @@ void watchdog_unregister(FAR void *handle)
 #endif
 
 #ifdef CONFIG_WATCHDOG_AUTOMONITOR_TASKS
-  while ((user = list_remove_head_type(&upper->users, struct watchdog_user_s,
+  /* A keepalive trigger running in interrupt context, or on another cpu,
+   * may still walk the channels: detach them under the lock and free them
+   * outside of it.
+   */
+
+  flags = spin_lock_irqsave(&upper->slock);
+  list_move(&upper->users, &users);
+  list_initialize(&upper->users);
+  spin_unlock_irqrestore(&upper->slock, flags);
+
+  while ((user = list_remove_head_type(&users, struct watchdog_user_s,
                                        node)) != NULL)
     {
       kmm_free(user);

Reply via email to