Skip to content

Commit c1e031c

Browse files
committed
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 <ouyangxiangzhen@xiaomi.com>
1 parent 7811d6f commit c1e031c

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎Documentation/debugging/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This page contains a collection of guides on how to debug problems with NuttX.
1717
stackcheck.rst
1818
stackrecord.rst
1919
stackusage.rst
20+
spinlockdebug.rst
2021
disabling_stackdumpdebug.rst
2122
debuggingflash_nuttxonarm.rst
2223
cortexmhardfaults.rst
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
====================================
2+
Spinlock Debug
3+
====================================
4+
5+
Overview
6+
--------
7+
8+
``CONFIG_SPINLOCK_DEBUG`` adds a set of debug tools that verify safe
9+
spinlock use, in particular on SMP builds. When enabled, every
10+
``spin_lock`` / ``spin_unlock`` (and the recursive ``rspin_lock`` /
11+
``rspin_unlock`` variants) records the holder and checks the locking
12+
sequence of the running task. A violation is reported with ``_alert()``
13+
messages and triggers ``PANIC()`` so that the fault is caught at the point
14+
of misuse instead of later as a hang or corruption.
15+
16+
With ``CONFIG_SPINLOCK_BACKTRACE`` additionally enabled, the holder's call
17+
stack is recorded when a spinlock is locked and printed in the dump, which
18+
makes it easier to identify the code path that holds a lock.
19+
20+
Configuration
21+
-------------
22+
23+
.. code-block:: kconfig
24+
25+
config SPINLOCK_DEBUG
26+
bool "Support debugging safe use of spinlock"
27+
depends on DEBUG_FEATURES
28+
29+
config SPINLOCK_BACKTRACE
30+
bool "Enable spinlock backtrace"
31+
depends on SPINLOCK_DEBUG && LIBC_BACKTRACE_DEPTH > 0
32+
33+
* ``CONFIG_SPINLOCK_DEBUG`` depends on ``DEBUG_FEATURES`` and defaults to
34+
``n``. It is primarily useful on SMP configurations where lock
35+
ordering bugs are otherwise hard to reproduce.
36+
* ``CONFIG_SPINLOCK_BACKTRACE`` additionally requires
37+
``LIBC_BACKTRACE_DEPTH > 0``; ``spinlock_debug_t`` then carries a
38+
``stack`` field.
39+
* Both options build with or without ``CONFIG_SPINLOCK_INLINE``; the
40+
recursive ``rspinlock`` family is covered as well.
41+
42+
Detected misuse
43+
---------------
44+
45+
The debug tools detect the following conditions while a task runs:
46+
47+
1. A task waits for more than one spinlock at the same time: it marks the
48+
first ``wait_spinlock`` and then enters a busy-wait on a second lock.
49+
2. A task relocks a spinlock it already holds (the non-recursive
50+
``spinlock_t`` is not reentrant).
51+
3. A task acquires a lock different from the one it was marked as waiting
52+
for.
53+
4. A task unlocks a spinlock it does not hold.
54+
5. A task unlocks spinlocks in the wrong (non LIFO) order.
55+
6. A task switches context while holding a spinlock.
56+
57+
For each violation the current ``hold_spinlock`` queue of the task is
58+
dumped before ``PANIC()``.
59+
60+
Unlock ordering
61+
---------------
62+
63+
On unlock, the holder is marked as released *before* the lock value is
64+
written back. Performing ``spinlock_mark_unlocked()`` first avoids an SMP
65+
race window in which another thread observes the lock as free but still
66+
sees the previous ``holder`` and is misreported as relocking the lock.
67+
68+
Backtrace
69+
---------
70+
71+
When ``CONFIG_SPINLOCK_BACKTRACE`` is enabled, ``spinlock_mark_locked()``
72+
records the caller stack via ``backtrace_record()`` into the ``stack``
73+
field of ``spinlock_debug_t``; ``spinlock_mark_unlocked()`` removes the
74+
record. The dump printed on a violation formats the recorded stack, for
75+
example::
76+
77+
Spinlock(0x40210a40) backtrace: 0x1234abcd 0x1234cdc0 ...
78+
79+
Without backtrace, the dump prints a short locked notice, matching the
80+
smaller ``spinlock_debug_t`` layout.
81+
82+
Notes
83+
-----
84+
85+
* The checks run on the busy-wait (non-``trylock``) acquisition path,
86+
because a trylock cannot cause a circular wait. Interrupt context is
87+
skipped.
88+
* In SMP builds the critical-section lock (``g_cpu_irqlock``) is excluded
89+
from the multiple-wait check, since it is legitimately acquired from
90+
inside a wait, for example from the assert path.

0 commit comments

Comments
 (0)