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 8a43d412f271be7aeec452a82d00567eaf629634
Author: wangzhi16 <[email protected]>
AuthorDate: Thu Jul 17 17:12:25 2025 +0800

    sched/spinlock: Debug tools for spinlock.
    
    Automatically find deadspinlock when assert.
    
    Assisted-By: mimo-v2.6-pro
    Co-authored-by: ouyangxiangzhen <[email protected]>
    Signed-off-by: wangzhi16 <[email protected]>
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/sched.h    |  1 +
 include/nuttx/spinlock.h | 19 ++++++++++
 sched/irq/irq_spinlock.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++--
 sched/misc/assert.c      | 73 +++++++++++++++++++++++++++----------
 sched/misc/deadlock.c    | 38 ++++++++++---------
 5 files changed, 184 insertions(+), 42 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 1f87559b1c1..91bf74fdc9f 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -688,6 +688,7 @@ struct tcb_s
 
 #ifdef CONFIG_SPINLOCK_DEBUG
   sq_queue_t hold_spinlock;
+  FAR spinlock_debug_t *wait_spinlock;
 #endif
 
   struct wdog_s waitdog;                 /* All timed waits use this timer  */
diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h
index 2bde1758417..565ab47258b 100644
--- a/include/nuttx/spinlock.h
+++ b/include/nuttx/spinlock.h
@@ -93,13 +93,17 @@ void nxsched_critmon_busywait(bool state, FAR void *caller);
 #endif
 
 #ifdef CONFIG_SPINLOCK_DEBUG
+void spinlock_mark_locking(FAR spinlock_debug_t *info);
 void spinlock_mark_locked(FAR spinlock_debug_t *info);
 void spinlock_mark_unlocked(FAR spinlock_debug_t *info);
 void spinlock_switch_context(FAR struct tcb_s *from);
+pid_t spinlock_get_holder(FAR spinlock_debug_t *info);
 #else
+#  define spinlock_mark_locking(info)
 #  define spinlock_mark_locked(info)
 #  define spinlock_mark_unlocked(info)
 #  define spinlock_switch_context(from)
+#  define spinlock_get_holder(info) INVALID_PROCESS_ID
 #endif
 
 /****************************************************************************
@@ -223,6 +227,10 @@ void spin_lock_notrace(FAR volatile spinlock_t *lock);
 #ifdef CONFIG_SPINLOCK
 static inline_function void spin_lock(FAR volatile spinlock_t *lock)
 {
+  /* Mark that we want to hold lock */
+
+  spinlock_mark_locking(&lock->info);
+
   /* Notify that we are waiting for a spinlock */
 
   sched_note_spinlock_lock(lock);
@@ -498,6 +506,10 @@ irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
 {
   irqstate_t flags;
 
+  /* Mark that we want to hold lock */
+
+  spinlock_mark_locking(&lock->info);
+
   /* Notify that we are waiting for a spinlock */
 
   sched_note_spinlock_lock(lock);
@@ -701,6 +713,10 @@ void rspin_lock(FAR rspinlock_t *lock)
     }
   else
     {
+      /* Mark that we want to hold lock */
+
+      spinlock_mark_locking(&lock->info);
+
       /* Try seize the ownership of the lock using CAS. */
 
       new_val = RSPINLOCK_VALUE(owner, 1u);
@@ -1330,6 +1346,9 @@ static inline_function void read_unlock(FAR volatile 
rwlock_t *lock)
 #  ifdef CONFIG_SPINLOCK_INLINE
 SPINLOCK_INLINE void write_lock(FAR volatile rwlock_t *lock)
 {
+  /* Mark that we want to hold lock */
+
+  spinlock_mark_locking(&lock->info);
   nxsched_critmon_busywait(true, return_address(0));
 
   for (; ; )
diff --git a/sched/irq/irq_spinlock.c b/sched/irq/irq_spinlock.c
index e3ba86cb5ed..d6f8c0f5bb4 100644
--- a/sched/irq/irq_spinlock.c
+++ b/sched/irq/irq_spinlock.c
@@ -43,8 +43,7 @@
 #include <arch/irq.h>
 
 #include "sched/sched.h"
-
-#if defined(CONFIG_SPINLOCK)
+#include "irq/irq.h"
 
 /****************************************************************************
  * Public Data
@@ -67,10 +66,57 @@ static void dump_spinlock(FAR sq_queue_t *hold_spinlock)
     }
 }
 
+void spinlock_mark_locking(FAR spinlock_debug_t *info)
+{
+  FAR struct tcb_s *rtcb = running_task();
+
+#ifdef CONFIG_SMP
+  /* The critical-section lock is allowed to be acquired while another
+   * spinlock is being waited for (e.g. from the assert path), so it is
+   * not marked to avoid false multi-wait detection.
+   */
+
+  if (info == &g_cpu_irqlock.info)
+    {
+      return;
+    }
+#endif
+
+  /* Check if the current tcb is not waiting for other spinlock. */
+
+  if (rtcb->wait_spinlock != NULL)
+    {
+      _alert("Detect thread(%d) is waiting for multiple spinlocks at the "
+             "same time, lock1(%p) and lock2 (%p)!\n",
+             rtcb->pid, rtcb->wait_spinlock, info);
+      dump_spinlock(&rtcb->hold_spinlock);
+      PANIC();
+    }
+
+  /* Only the method of acquiring lock through busywait needs to be marked.
+   * If the lock is acquired through trylock, no marking is required
+   * because it will not cause a circular wait and lead to deadlock.
+   */
+
+  rtcb->wait_spinlock = info;
+}
+
 void spinlock_mark_locked(FAR spinlock_debug_t *info)
 {
   FAR struct tcb_s *rtcb = running_task();
 
+#ifdef CONFIG_SMP
+  /* The critical-section lock is allowed to be acquired while another
+   * spinlock is being waited for (e.g. from the assert path), so it is
+   * not marked to avoid false multi-wait detection.
+   */
+
+  if (info == &g_cpu_irqlock.info)
+    {
+      return;
+    }
+#endif
+
   /* Check if the spinlock has been held. */
 
   if (info->holder != NULL)
@@ -81,17 +127,44 @@ void spinlock_mark_locked(FAR spinlock_debug_t *info)
       PANIC();
     }
 
+  /* Check if the current tcb acquires the right lock. */
+
+  if (rtcb->wait_spinlock != info)
+    {
+      _alert("Detect thread(%d) wants to acquire lock1(%p), but ends up "
+             "acquiring lock2(%p)!\n",
+             rtcb->pid, rtcb->wait_spinlock, info);
+      dump_spinlock(&rtcb->hold_spinlock);
+      PANIC();
+    }
+
   /* Set info and enqueue. */
 
   info->holder = rtcb;
   info->caller = return_address(0);
   sq_addlast(&info->flink, &rtcb->hold_spinlock);
+
+  /* Reset wait_spinlock. */
+
+  rtcb->wait_spinlock = NULL;
 }
 
 void spinlock_mark_unlocked(FAR spinlock_debug_t *info)
 {
   FAR struct tcb_s *rtcb = running_task();
 
+#ifdef CONFIG_SMP
+  /* The critical-section lock is allowed to be acquired while another
+   * spinlock is being waited for (e.g. from the assert path), so it is
+   * not marked to avoid false multi-wait detection.
+   */
+
+  if (info == &g_cpu_irqlock.info)
+    {
+      return;
+    }
+#endif
+
   /* Check if locking and unlocking are the same task. */
 
   if (info->holder != rtcb)
@@ -132,6 +205,20 @@ void spinlock_switch_context(FAR struct tcb_s *from)
     }
 }
 
-#endif /* CONFIG_SPINLOCK_DEBUG */
+/* Get the pid of the current lock holder as a reference-free snapshot:
+ * take the holder pid and drop the pointer immediately.  Note that this
+ * is inherently racy: the holder may release the lock or exit at any
+ * time, so the pid is valid only at the moment of the read.
+ */
+
+pid_t spinlock_get_holder(FAR spinlock_debug_t *info)
+{
+  if (info->holder != NULL)
+    {
+      return info->holder->pid;
+    }
 
-#endif /* CONFIG_SPINLOCK */
+  return INVALID_PROCESS_ID;
+}
+
+#endif /* CONFIG_SPINLOCK_DEBUG */
diff --git a/sched/misc/assert.c b/sched/misc/assert.c
index 1b2f3da2c44..e1f66056616 100644
--- a/sched/misc/assert.c
+++ b/sched/misc/assert.c
@@ -66,6 +66,22 @@
 
 #define DEADLOCK_MAX 8
 
+/* The dump_lockholder() buffer must hold the deeper of the two backtrace
+ * sources: the spinlock backtrace (CONFIG_LIBC_BACKTRACE_DEPTH) and this
+ * tree's fixed mutex backtrace (CONFIG_LIBC_MUTEX_BACKTRACE, an
+ * independent depth knob).
+ */
+
+#ifdef CONFIG_LIBC_BACKTRACE_DEPTH
+#  if CONFIG_LIBC_BACKTRACE_DEPTH > CONFIG_LIBC_MUTEX_BACKTRACE
+#    define DEADLOCK_BACKTRACE_DEPTH CONFIG_LIBC_BACKTRACE_DEPTH
+#  else
+#    define DEADLOCK_BACKTRACE_DEPTH CONFIG_LIBC_MUTEX_BACKTRACE
+#  endif
+#else
+#  define DEADLOCK_BACKTRACE_DEPTH CONFIG_LIBC_MUTEX_BACKTRACE
+#endif
+
 #ifndef CONFIG_BOARD_RESET_ON_ASSERT
 #  define CONFIG_BOARD_RESET_ON_ASSERT 0
 #endif
@@ -577,45 +593,62 @@ static void dump_tasks(void)
  * Name: dump_lockholder
  ****************************************************************************/
 
-#if CONFIG_LIBC_MUTEX_BACKTRACE > 0
+#ifdef CONFIG_ARCH_DEADLOCKDUMP
 static void dump_lockholder(pid_t tid)
 {
-  char buf[BACKTRACE_BUFFER_SIZE(CONFIG_LIBC_MUTEX_BACKTRACE)];
-  FAR mutex_t *mutex;
+  char buf[BACKTRACE_BUFFER_SIZE(DEADLOCK_BACKTRACE_DEPTH)];
+  FAR struct tcb_s *tcb;
+  FAR void **stack = NULL;
+  int depth = 0;
+  pid_t holder = INVALID_PROCESS_ID;
+
+  /* nxsched_get_tcb() holds no reference on the TCB once it returns, so
+   * use it as an inherently racy snapshot and read what we need from it
+   * immediately.
+   */
 
-  mutex = (FAR mutex_t *)nxsched_get_tcb(tid)->waitobj;
+  tcb = nxsched_get_tcb(tid);
+  DEBUGASSERT(tcb != NULL);
 
-  backtrace_format(buf, sizeof(buf), mutex->backtrace,
-                   CONFIG_LIBC_MUTEX_BACKTRACE);
+  if (tcb->task_state == TSTATE_WAIT_SEM)
+    {
+      FAR mutex_t *mutex = tcb->waitobj;
 
-  _alert("Mutex holder(%d) backtrace:%s\n", nxmutex_get_holder(mutex), buf);
+      holder = nxmutex_get_holder(mutex);
+#  if CONFIG_LIBC_MUTEX_BACKTRACE > 0
+      stack = mutex->backtrace;
+      depth = CONFIG_LIBC_MUTEX_BACKTRACE;
+#  endif
+    }
+#  ifdef CONFIG_SPINLOCK_DEBUG
+  else
+    {
+      FAR spinlock_debug_t *info = tcb->wait_spinlock;
+
+      holder = spinlock_get_holder(info);
+    }
+#  endif
+
+  backtrace_format(buf, sizeof(buf), stack, depth);
+
+  _alert("Deadlock(%d) holder(%d) backtrace: %s\n", tid, holder, buf);
 }
-#else
-#  define dump_lockholder(tid)
-#endif
 
 /****************************************************************************
  * Name: dump_deadlock
  ****************************************************************************/
 
-#ifdef CONFIG_ARCH_DEADLOCKDUMP
 static void dump_deadlock(void)
 {
   pid_t deadlock[DEADLOCK_MAX];
   size_t i = nxsched_collect_deadlock(deadlock, DEADLOCK_MAX);
 
-  if (i > 0)
+  while (i-- > 0)
     {
-      _alert("Deadlock detected\n");
-      while (i-- > 0)
-        {
+      dump_lockholder(deadlock[i]);
 #  ifdef CONFIG_SCHED_BACKTRACE
-          sched_dumpstack(deadlock[i]);
-          dump_lockholder(deadlock[i]);
-#  else
-          _alert("deadlock pid: %d\n", deadlock[i]);
+      sched_dumpstack(deadlock[i]);
 #  endif
-        }
     }
 }
 #endif
diff --git a/sched/misc/deadlock.c b/sched/misc/deadlock.c
index 0f8e350d1ac..dabd652eeab 100644
--- a/sched/misc/deadlock.c
+++ b/sched/misc/deadlock.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/mutex.h>
 #include <nuttx/sched.h>
+#include <nuttx/spinlock.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -43,28 +44,37 @@ struct deadlock_info_s
  ****************************************************************************/
 
 /****************************************************************************
- * Name: getmutex
+ * Name: getholder
  ****************************************************************************/
 
-static FAR mutex_t *getmutex(FAR struct tcb_s *tcb)
+static pid_t getholder(FAR struct tcb_s *tcb)
 {
-  FAR sem_t *sem;
-
   if (tcb == NULL)
     {
-      return NULL;
+      return INVALID_PROCESS_ID;
     }
 
+  /* Spinlock deadlock only occurs in RUNNING threads,
+   * while mutex deadlock only occurs in WAIT_SEM threads.
+   */
+
   if (tcb->task_state == TSTATE_WAIT_SEM)
     {
-      sem = tcb->waitobj;
+      FAR sem_t *sem = tcb->waitobj;
+
       if (sem != NULL && (sem->flags & SEM_TYPE_MUTEX) != 0)
         {
-          return (FAR mutex_t *)sem;
+          return nxmutex_get_holder((FAR mutex_t *)sem);
         }
     }
+#ifdef CONFIG_SPINLOCK_DEBUG
+  else if (tcb->wait_spinlock)
+    {
+      return spinlock_get_holder(tcb->wait_spinlock);
+    }
+#endif
 
-  return NULL;
+  return INVALID_PROCESS_ID;
 }
 
 /****************************************************************************
@@ -73,17 +83,9 @@ static FAR mutex_t *getmutex(FAR struct tcb_s *tcb)
 
 static FAR struct tcb_s *deadlock_next(FAR struct tcb_s *tcb)
 {
-  FAR mutex_t *mutex;
-  pid_t holder;
-
-  mutex = getmutex(tcb);
-  if (mutex == NULL)
-    {
-      return NULL;
-    }
+  pid_t holder = getholder(tcb);
 
-  holder = nxmutex_get_holder(mutex);
-  if (holder < 0)
+  if (holder == INVALID_PROCESS_ID)
     {
       return NULL;
     }

Reply via email to