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 cc86b01641866d6b31a80939c371aaa3e9bf4e5b
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Fri Nov 20 10:28:55 2026 +0800

    Documentation: document the spinlock debug tools
    
    Add a debugging page describing CONFIG_SPINLOCK_DEBUG and
    CONFIG_SPINLOCK_BACKTRACE: the config dependencies, the misuse
    conditions detected by the holder tracking (relock, cross-thread
    lock acquire, multiple-wait, unlock by a non-holder, out-of-order
    unlock), the holder call-stack recording, and the unlock ordering
    requirement used by the mark functions.
    
    This is a series-original add: the debug tools themselves already
    landed in the merge stream but no documentation page described them.
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 Documentation/debugging/index.rst         |  1 +
 Documentation/debugging/spinlockdebug.rst | 90 +++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)

diff --git a/Documentation/debugging/index.rst 
b/Documentation/debugging/index.rst
index 0f65ebe3a66..ff93553c499 100644
--- a/Documentation/debugging/index.rst
+++ b/Documentation/debugging/index.rst
@@ -17,6 +17,7 @@ This page contains a collection of guides on how to debug 
problems with NuttX.
   stackcheck.rst
   stackrecord.rst
   stackusage.rst
+  spinlockdebug.rst
   disabling_stackdumpdebug.rst
   debuggingflash_nuttxonarm.rst
   cortexmhardfaults.rst
diff --git a/Documentation/debugging/spinlockdebug.rst 
b/Documentation/debugging/spinlockdebug.rst
new file mode 100644
index 00000000000..7ded475981e
--- /dev/null
+++ b/Documentation/debugging/spinlockdebug.rst
@@ -0,0 +1,90 @@
+====================================
+Spinlock Debug
+====================================
+
+Overview
+--------
+
+``CONFIG_SPINLOCK_DEBUG`` adds a set of debug tools that verify safe
+spinlock use, in particular on SMP builds.  When enabled, every
+``spin_lock`` / ``spin_unlock`` (and the recursive ``rspin_lock`` /
+``rspin_unlock`` variants) records the holder and checks the locking
+sequence of the running task.  A violation is reported with ``_alert()``
+messages and triggers ``PANIC()`` so that the fault is caught at the point
+of misuse instead of later as a hang or corruption.
+
+With ``CONFIG_SPINLOCK_BACKTRACE`` additionally enabled, the holder's call
+stack is recorded when a spinlock is locked and printed in the dump, which
+makes it easier to identify the code path that holds a lock.
+
+Configuration
+-------------
+
+.. code-block:: kconfig
+
+    config SPINLOCK_DEBUG
+        bool "Support debugging safe use of spinlock"
+        depends on DEBUG_FEATURES
+
+    config SPINLOCK_BACKTRACE
+        bool "Enable spinlock backtrace"
+        depends on SPINLOCK_DEBUG && LIBC_BACKTRACE_DEPTH > 0
+
+* ``CONFIG_SPINLOCK_DEBUG`` depends on ``DEBUG_FEATURES`` and defaults to
+  ``n``.  It is primarily useful on SMP configurations where lock
+  ordering bugs are otherwise hard to reproduce.
+* ``CONFIG_SPINLOCK_BACKTRACE`` additionally requires
+  ``LIBC_BACKTRACE_DEPTH > 0``; ``spinlock_debug_t`` then carries a
+  ``stack`` field.
+* Both options build with or without ``CONFIG_SPINLOCK_INLINE``; the
+  recursive ``rspinlock`` family is covered as well.
+
+Detected misuse
+---------------
+
+The debug tools detect the following conditions while a task runs:
+
+1. A task waits for more than one spinlock at the same time: it marks the
+   first ``wait_spinlock`` and then enters a busy-wait on a second lock.
+2. A task relocks a spinlock it already holds (the non-recursive
+   ``spinlock_t`` is not reentrant).
+3. A task acquires a lock different from the one it was marked as waiting
+   for.
+4. A task unlocks a spinlock it does not hold.
+5. A task unlocks spinlocks in the wrong (non LIFO) order.
+6. A task switches context while holding a spinlock.
+
+For each violation the current ``hold_spinlock`` queue of the task is
+dumped before ``PANIC()``.
+
+Unlock ordering
+---------------
+
+On unlock, the holder is marked as released *before* the lock value is
+written back.  Performing ``spinlock_mark_unlocked()`` first avoids an SMP
+race window in which another thread observes the lock as free but still
+sees the previous ``holder`` and is misreported as relocking the lock.
+
+Backtrace
+---------
+
+When ``CONFIG_SPINLOCK_BACKTRACE`` is enabled, ``spinlock_mark_locked()``
+records the caller stack via ``backtrace_record()`` into the ``stack``
+field of ``spinlock_debug_t``; ``spinlock_mark_unlocked()`` removes the
+record.  The dump printed on a violation formats the recorded stack, for
+example::
+
+    Spinlock(0x40210a40) backtrace: 0x1234abcd 0x1234cdc0 ...
+
+Without backtrace, the dump prints a short locked notice, matching the
+smaller ``spinlock_debug_t`` layout.
+
+Notes
+-----
+
+* The checks run on the busy-wait (non-``trylock``) acquisition path,
+  because a trylock cannot cause a circular wait.  Interrupt context is
+  skipped.
+* In SMP builds the critical-section lock (``g_cpu_irqlock``) is excluded
+  from the multiple-wait check, since it is legitimately acquired from
+  inside a wait, for example from the assert path.
\ No newline at end of file

Reply via email to