Skip to content

spinlock: complete the debug and monitoring tools - #22215

Open
Fix-Point wants to merge 19 commits into
apache:masterfrom
Fix-Point:nuttx-spinlock-debug
Open

Fix-Point wants to merge 19 commits into
apache:masterfrom
Fix-Point:nuttx-spinlock-debug

Conversation

@Fix-Point

@Fix-Point Fix-Point commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR brings the spinlock debug/monitoring tools to the reference level: IRQ-disabled-time monitoring for spinlocks, holder call-stack backtrace, the notrace split and the remaining alignments of the spinlock helpers.

  1. Extend IRQ-disabled-time monitoring to spinlocks (sched/irq: extend interrupt-disabled time monitoring to spinlock + sched/spinlock: fix critmonitor irqdis tracking in notrace and interrupt context): the critmon timers account the interrupt-disabled window while a spinlock is held (nxsched_critmon_irqdisable()), and the accounting stays consistent in the notrace paths and in interrupt context.
  2. Reset critmon state on task restart (task_restart: restore critmon info): a restarted child no longer inherits stale per-task maxima.
  3. Rename csection monitoring symbols to irqdis (sched/irq: rename csection monitoring symbols to irqdis + sched/irq: complete the irqdis rename in documentation and nxgdb): counters are named after the disabled-interrupt window they measure; the rename spans the source tree, Kconfig help, documentation and nxgdb, with a "no remaining references" check.
  4. Spinlock holder backtrace (sched/spinlock: Add backtrace information of lock holder in spinlock debugging.): with CONFIG_SPINLOCK_BACKTRACE the debug tools record the holder call stack so misuse reports show the faulting code.
  5. Trace/notrace split (add sched_note_spinlock trace points, add notrace versions, separate trace and notrace irq paths to fix recursion, sched/note: split composite hook macros into per-type entries): the rspinlock paths distinguish traced and notrace variants so instrumentation does not recurse; composite hooks are split per type.
  6. Seqlock alignment (seqlock: Move the header files to decouple compilation dependencies. + seqlock: align seqlock.h with the downstream seqlock terminal form): the seqlock header is decoupled and its types aligned (seqcount_t.sequence as atomic_t on SMP).
  7. Break irq.h include cycles (sched/irq: break the remaining irq.h include cycles in arch headers + arch/risc-v: fix circular include that breaks critmon build + arch: include nuttx/irq.h where xcpt_t is used): arch headers that define the irq primitives no longer include <nuttx/irq.h> themselves; headers using xcpt_t get the explicit include (fixes the imxrt1180-evk/nsh build).
  8. Helper alignments (spinlock: align rspin_lock_is_hold with the downstream form, spinlock: add noinstrument_function to _notrace functions, spinlock: mark the lock as held before the busy-wait accounting, spinlock: fix the max nesting depth comment): is_hold uses the instrumented window and the local_val count on !CONFIG_SPINLOCK; the notrace helpers carry noinstrument_function; the holder is registered before the accounting/trace steps; the nesting comment matches the UINT16_MAX limit.

Depends-On: #22243 (unlock ordering fix, already merged).

Impact

  • User impact: observable IRQ-disabled durations and holder backtraces for spinlock misuse reports; csection-named symbols renamed to irqdis.
  • Build: CONFIG_SCHED_CRITMONITOR_MAXTIME_IRQDISABLE/BUSYWAIT dimensions cover spinlock holders; CONFIG_SPINLOCK_BACKTRACE (depends on SPINLOCK_DEBUG && LIBC_BACKTRACE_DEPTH > 0) adds holder stack recording; #include <nuttx/irq.h> removed from arch headers and added to 32 xcpt_t users.
  • Compatibility: renamed symbols (nxsched_critmon_csection → nxsched_critmon_irqdisable, crit_* TCB fields → irqdis_*); procfs/nxgdb tools updated.
  • Documentation: docs updated for the rename; spinlock debug page merged via spinlock: mark the lock unlocked before releasing it #22243.
  • Security: no new attack surface.
  • Hardware: none.

Testing

Build Host: Linux x86_64

Boards/Configurations:

  • qemu-armv7a:smp with CONFIG_SPINLOCK_DEBUG=y, CONFIG_SPINLOCK_BACKTRACE=y, CONFIG_SCHED_CRITMONITOR=y, CONFIG_SCHED_BACKTRACE=y, CONFIG_TESTING_OSTEST=y
  • rv-virt:smp (risc-v) with CONFIG_SPINLOCK_DEBUG=y
  • imxrt1180-evk:nsh (restores the xcpt_t include chain)

Verification:

  • tools/checkpatch.sh green on every commit (19/19).
  • Full ostest on qemu-armv7a:smp 4 CPUs with the full feature chain: wqueue/HPWORK reports no false relock, all segments complete, ostest_main: Exiting with status 0.
  • Log: /disk0/gitcode/.review-tmp/ostest-20261124.log (1423 lines).

@jerpelea jerpelea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please split this PR in 2 logical separate PRs
complete the debug/monitoring tools
fix unlock ordering

GUIDINGLI
GUIDINGLI previously approved these changes Nov 23, 2026
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: hc Issues related to HC architecture Arch: mips Issues related to the MIPS architecture Arch: openrisc Issues related to the OpenRISC architecture Arch: renesas Issues related to the Renesas chips Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: tricore Issues related to the TriCore architecture from Infineon labels Nov 23, 2026
hujun260 and others added 17 commits November 25, 2026 09:38
seqlock: Move the header files to decouple compilation dependencies.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: hujun5 <hujun5@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
The critmon previously only tracked interrupt disable time within
enter/leave_critical_section(). But spinlock and rspinlock also disable
interrupts without being accounted for.

Move nxsched_critmon_csection() to include/nuttx/irq.h and instrument
irq_save()/irq_restore() directly, so all interrupt disable paths are
automatically monitored. Add crit_count nesting counter in tcb to
prevent recursion when perf_gettime() triggers irq_save internally.

Also add critmon calls in rspin_breaklock/restorelock for proper
tracking across context switches, and use up_irq_save directly in
this_task() to avoid recursion.

Remove #include <nuttx/irq.h> from all arch/<arch>/include/irq.h
headers to break the circular dependency introduced by moving
nxsched_critmon_csection() into nuttx/irq.h, since nuttx/irq.h
now depends on arch irq primitives (up_irq_save/up_irq_restore)
which are defined in arch headers.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Rename all csection-specific monitoring symbols to irqdis to better
reflect that the monitoring now covers all IRQ-disabled paths, not
just enter_critical_section.

Symbol renames:
- SCHED_CRITMONITOR_MAXTIME_CSECTION -> MAXTIME_IRQDISABLE (Kconfig)
- nxsched_critmon_csection()         -> nxsched_critmon_irqdisable()
- CHECK_CSECTION                     -> CHECK_IRQDIS
- TCB fields: crit_start/max/count/caller/max_caller
              -> irqdis_start/max/count/caller/max_caller
- g_crit_max                         -> g_irqdis_max

Update comments and Kconfig help text to replace 'critical section'
with 'IRQ disabled' where the statement now covers all irq_save /
irq_restore call sites.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
…lock

rspin_lock/rspin_unlock were missing sched_note_spinlock() trace points
that spin_lock/spin_unlock already have. Add NOTE_SPINLOCK_LOCK,
NOTE_SPINLOCK_LOCKED and NOTE_SPINLOCK_UNLOCK instrumentation to
rspin_lock and rspin_unlock for symmetry with the non-recursive
spinlock API. Only first-time acquisition and final release are
traced, recursive lock/unlock are not instrumented.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: ligd <liguiding1@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Add rspin_lock_notrace, rspin_lock_irqsave_notrace,
rspin_unlock_notrace and rspin_unlock_irqrestore_notrace that skip
spinlock_mark_locking/locked/unlocked and sched_note_spinlock calls.

This mirrors the existing spin_lock_notrace/spin_lock_irqsave_notrace
pattern for non-recursive spinlocks. The notrace variants are needed
by instrumentation code (e.g. noteram driver) that must acquire
rspinlocks without triggering recursive note events.

Refactor rspin_lock to call rspin_lock_notrace internally, and
rspin_unlock to call rspin_unlock_notrace internally, keeping the
trace/mark logic only in the outer wrappers.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: ligd <liguiding1@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
The _notrace variants of spin_lock/unlock_irqsave/restore are called
from within the trace/note subsystem itself. If these variants call
irq_save()/irq_restore() which may be instrumented, it leads to
infinite recursion or ABBA deadlock when the note subsystem tries to
acquire a spinlock while already holding one.

Changes:
- Use up_irq_save()/up_irq_restore() in all _notrace variants to
  bypass any instrumentation in the IRQ save/restore path.
- In spin_lock_irqsave(), call irq_save() first (instrumented), then
  spin_lock_notrace() separately, so the trace note happens outside
  the critical irq-disabled region setup.
- In spin_unlock_irqrestore(), call spin_unlock_notrace() first, emit
  the trace note, then call irq_restore() last.
- For UP (non-SMP) builds, properly implement rspin_lock_notrace and
  rspin_unlock_notrace as standalone macros, and provide dedicated
  rspin_lock_irqsave_notrace/rspin_unlock_irqrestore_notrace using
  up_irq_save()/up_irq_restore() instead of aliasing to the traced
  versions.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
…upt context

1. spinlock.h: enter/leave_critical_section_notrace were calling
   traced irq_save()/irq_restore() (non-SMP) or traced
   rspin_lock_irqsave/rspin_unlock_irqrestore (SMP), which invoke
   nxsched_critmon_irqdisable() and modify irqdis_count. Replace
   with up_irq_save/up_irq_restore (non-SMP) and the _notrace
   spinlock variants (SMP) to avoid unintended critmon side effects.

2. spinlock.h: Remove nxsched_critmon_irqdisable() from the else
   branch of rspin_unlock_irqrestore_notrace() (SMP). The notrace
   variant should not invoke any monitoring code. Move the balance
   call to the traced rspin_unlock_irqrestore() only.

3. sched_critmonitor.c: Skip irqdis tracking entirely when called
   from interrupt context (up_interrupt_context()). IRQ handlers
   run with interrupts already disabled by hardware, so tracking
   irq disable duration inside them is meaningless and can cause
   irqdis_count imbalance across context switches.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This is a series-original fix: it completes the seqlock ripple outside
the original patches' file set; the individual deltas have no single
original patch (they are master-side form divergences plus already
covered rewrites of the seqlock line).

The seqcount_t sequence member is now atomic_t on SMP (picked from
f369edc), so drop the (FAR atomic_t *) casts, use ~1 for the
parity mask, and adopt the downstream header organization (EXTERN
block, single <nuttx/arch.h> include, param form) so seqlock.h matches
the downstream end state exactly.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This is a series-original fix: it completes the symbol rename outside
the original patch's file set; it is not present in the original
history.

The irqdis rename left two dangling consumers: the critical sections
documentation still names the old Kconfig symbol, and the nxgdb TCB
field declarations still reference the old crit_* field names, which
breaks GDB field resolution.  Replace the identifiers so every tree
consumer follows the rename.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This is a series-original fix: it completes the include-cycle break
outside the original patch's file set; it is not present in the
original history.

The irqdis monitoring change moved nxsched_critmon_irqdisable() and
the instrumented irq_save()/irq_restore() into <nuttx/irq.h>, which
now depends on the arch primitives up_irq_save()/up_irq_restore().
The original change removed #include <nuttx/irq.h> from the top-level
arch/<arch>/include/irq.h headers, but arch headers that define
up_irq_save()/up_irq_restore() themselves still pull in
<nuttx/irq.h> first, so a translation unit entering through those
headers sees an implicit declaration of up_irq_save() and then fails
on "static declaration follows non-static declaration".  Drop the
include from those headers as well, per the same rationale.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Enabling CONFIG_SCHED_CRITMONITOR_MAXTIME_IRQDISABLE on RISC-V fails
to compile because arch/mode.h includes nuttx/irq.h, creating a
circular dependency that leaves up_irq_save() undefined at the point
of use. Remove the unnecessary reverse include and add the missing
direct stdbool.h dependency.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: Gan Jing <ganjing@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
…debugging.

Add CONFIG_SPINLOCK_BACKTRACE: when enabled, record the full backtrace
of the task that locked the spinlock and print it on misuse, replacing
the single caller frame.  Mark/clear the recorded backtrace in the
spinlock mark_locked/mark_unlocked paths, and make the backtrace and
pid-hash locks no-trace so the debug paths do not recurse into the
spinlock mark machinery.

Adapted for master:
- sched/sched_gettcb.c: no g_pidhashlock in the master tree (already
  lock-free), the no-trace conversion has no target.
- keeps the otherwise identical vela shape (stack field, conditional
  SPINLOCK_DEBUG_INIT, CONFIG_SPINLOCK_BACKTRACE).

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
The is_hold helper used the raw up_irq_save()/up_irq_restore() window
and read the owner only through the atomic value.  Align with the
downstream implementation: use the instrumented irq_save()/irq_restore()
window on SMP, and on !CONFIG_SPINLOCK builds report the recursive count
from local_val (no atomic read is needed because interrupts are not
disabled and the lock is task-local there).

This is a series-original fix: it completes the alignment of the
merge-stream helper with the reference implementation.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Add noinstrument_function attribute to all spinlock _notrace functions
to prevent them from being instrumented when CONFIG_ARCH_INSTRUMENT_ALL
is enabled. These functions are used in the instrument callback path
and must not trigger instrumentation to avoid infinite recursion.

Pick-adapted: three of the five call sites already carried the
attribute in this tree (from the merged spinlock debug series), so only
the missing spin_trylock_notrace() and spin_lock_irqsave_notrace()
attributes are applied here.

Assisted-By: mimo-v2.6-pro
Signed-off-by: yezhonghui <yezhonghui@xiaomi.com>
spin_lock()/spin_trylock()/spin_lock_irqsave() recorded the holder
(spinlock_mark_locked) after the busy-wait accounting and the sched_note
trace, while the reference implementation marks the lock as held
immediately after acquiring it.  Reorder so that a relock misuse is
detected before the accounting/trace steps and the holder is registered
as soon as the lock is taken.

This is a series-original fix: it aligns the merged spinlock helpers
with the reference ordering.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
The rspinlock nesting comment said the lock can be nested UINT8_MAX
deep while the assert and the count field allow up to UINT16_MAX.
Align the comment with the actual limit.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Breaking the irq.h include cycles removed the reverse
#include <nuttx/irq.h> from the arch irq headers.  Chip headers and
driver headers that use xcpt_t (defined in <nuttx/irq.h>) previously
relied on that transitive include, which turned into a build error
(e.g. imxrt1180-evk/nsh: unknown type name 'xcpt_t' in imxrt_rgpio.h).
Add the explicit #include <nuttx/irq.h> to every arch header that uses
xcpt_t and did not include it directly.

This is a series-original fix: it completes the include-cycle break by
adding the missing direct dependencies.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
@Fix-Point
Fix-Point dismissed stale reviews from GUIDINGLI and xiaoxiang781216 via 5ca873c November 25, 2026 01:41
@Fix-Point
Fix-Point force-pushed the nuttx-spinlock-debug branch from a1f7d1d to 5ca873c Compare November 25, 2026 01:41
@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Area: Drivers Drivers issues and removed Area: Documentation Improvements or additions to documentation labels Nov 25, 2026
@Fix-Point Fix-Point changed the title spinlock: complete the debug/monitoring tools and fix unlock ordering spinlock: complete the debug and monitoring tools Nov 25, 2026
Split sched_note hooks that take a runtime type/event parameter into
separate per-type macros with compile-time constant type.  This
prepares for static key replacement where each branch site needs a
fixed key address.

- irqhandler(i,h,e) -> irq_enter(i,h) + irq_leave(i,h)
- preemption(t,l)   -> preempt_lock(t) + preempt_unlock(t)
- csection(t,e)     -> csection_enter(t) + csection_leave(t)
- spinlock(s,type)  -> spinlock_lock/locked/unlock/abort(s)
- wdog(e,h,a)       -> wdog_start/cancel/enter/leave(h,a)
- wqueue(e,w,a)     -> wqueue_start/cancel/enter/leave(w,a)
- heap(e,h,m,s,c)   -> heap_add/remove/alloc/free(h,m,s,c)

Old composite macros are removed; callers use new specific macros.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: huojianchao <huojianchao@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: hc Issues related to HC architecture Arch: mips Issues related to the MIPS architecture Arch: openrisc Issues related to the OpenRISC architecture Arch: renesas Issues related to the Renesas chips Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: sparc Issues related to the SPARC architecture Arch: tricore Issues related to the TriCore architecture from Infineon Arch: x86 Issues related to the x86 architecture Arch: x86_64 Issues related to the x86_64 architecture Arch: xtensa Issues related to the Xtensa architecture Arch: z16 Issues related to the Z16 architecture Arch: z80 Issues related to the Z80 architecture Area: Drivers Drivers issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants