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 fe8fdd51186e23ce8d850739bc9c0a0ef4899cda
Author: wangzhi16 <[email protected]>
AuthorDate: Mon Mar 30 17:31:27 2026 +0800

    sched/spinlock: separate trace and notrace irq paths to fix recursion
    
    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 <[email protected]>
    Signed-off-by: wangzhi16 <[email protected]>
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/spinlock.h | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h
index 168c1e5c959..b8d853ba663 100644
--- a/include/nuttx/spinlock.h
+++ b/include/nuttx/spinlock.h
@@ -459,14 +459,14 @@ static inline_function
 irqstate_t spin_lock_irqsave_notrace(FAR volatile spinlock_t *lock)
 {
   irqstate_t flags;
-  flags = irq_save();
+  flags = up_irq_save();
 
   spin_lock_notrace(lock);
 
   return flags;
 }
 #else
-#  define spin_lock_irqsave_notrace(l) ((void)(l), irq_save())
+#  define spin_lock_irqsave_notrace(l) ((void)(l), up_irq_save())
 #endif
 
 /****************************************************************************
@@ -497,7 +497,7 @@ irqstate_t spin_lock_irqsave_notrace(FAR volatile 
spinlock_t *lock)
 static inline_function
 irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
 {
-  irqstate_t flags;
+  irqstate_t flags = irq_save();
 
   /* Mark that we want to hold lock */
 
@@ -513,7 +513,7 @@ irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
 
   /* Lock without trace note */
 
-  flags = spin_lock_irqsave_notrace(lock);
+  spin_lock_notrace(lock);
 
   /* Get the lock, end counting busy-waiting */
 
@@ -768,7 +768,7 @@ void rspin_lock(FAR rspinlock_t *lock);
 static inline_function noinstrument_function
 irqstate_t rspin_lock_irqsave_notrace(FAR rspinlock_t *lock)
 {
-  irqstate_t flags = irq_save();
+  irqstate_t flags = up_irq_save();
   rspin_lock_notrace(lock);
   return flags;
 }
@@ -781,9 +781,8 @@ irqstate_t rspin_lock_irqsave(FAR rspinlock_t *lock)
   return flags;
 }
 #else
-#  define rspin_lock_notrace(lock) ((void)(lock))
 #  define rspin_lock_irqsave(l) ((void)(l), irq_save())
-#  define rspin_lock_irqsave_notrace(l) rspin_lock_irqsave(l)
+#  define rspin_lock_irqsave_notrace(l) ((void)(l), up_irq_save())
 #endif
 
 static inline_function
@@ -914,15 +913,15 @@ bool rspin_trylock(FAR rspinlock_t *lock);
 #ifdef CONFIG_SPINLOCK
 #  define spin_trylock_irqsave_notrace(l, f) \
 ({ \
-  f = irq_save(); \
+  f = up_irq_save(); \
   spin_trylock_notrace(l) ? \
-  true : ({ irq_restore(f); false; }); \
+  true : ({ up_irq_restore(f); false; }); \
 })
 #else
 #  define spin_trylock_irqsave_notrace(l, f) \
 ({ \
   (void)(l); \
-  f = irq_save(); \
+  f = up_irq_save(); \
   true; \
 })
 #endif /* CONFIG_SPINLOCK */
@@ -1002,10 +1001,10 @@ void spin_unlock_irqrestore_notrace(FAR volatile 
spinlock_t *lock,
 {
   spin_unlock_notrace(lock);
 
-  irq_restore(flags);
+  up_irq_restore(flags);
 }
 #else
-#  define spin_unlock_irqrestore_notrace(l, f) ((void)(l), irq_restore(f))
+#  define spin_unlock_irqrestore_notrace(l, f) ((void)(l), up_irq_restore(f))
 #endif
 
 /****************************************************************************
@@ -1155,7 +1154,7 @@ void rspin_unlock_irqrestore_notrace(FAR rspinlock_t 
*lock,
 {
   if (rspin_unlock_notrace(lock))
     {
-      irq_restore(flags);
+      up_irq_restore(flags);
     }
   else
     {
@@ -1176,9 +1175,8 @@ void rspin_unlock_irqrestore(FAR rspinlock_t *lock, 
irqstate_t flags)
     }
 }
 #else
-#  define rspin_unlock_notrace(lock) rspin_unlock(lock)
 #  define rspin_unlock_irqrestore(l, f) ((void)(l), irq_restore(f))
-#  define rspin_unlock_irqrestore_notrace(l, f) rspin_unlock_irqrestore(l, f)
+#  define rspin_unlock_irqrestore_notrace(l, f) ((void)(l), up_irq_restore(f))
 #endif
 
 static inline_function

Reply via email to