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 b2825bdf2f2782ab640d54cbe96001a1b44dc043
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Thu Nov 19 21:22:45 2026 +0800

    spinlock: mark spinlock unlocked before releasing the lock
    
    The spin_unlock()/spin_unlock_irqrestore() path cleared the holder
    after spin_unlock_notrace() had already released the lock value.  On
    SMP this leaves a window in which another thread may acquire the lock
    (see lock == unlocked) while info->holder still refers to the previous
    owner, so mark_locked() misreports a relock.  Reorder to match the
    downstream semantics: mark the spinlock as unlocked first, then release
    the lock value, then emit the trace note.
    
    The recursive (rspin) unlock path already used the correct order.
    
    This is a series-original fix: it restores the mark-before-release
    ordering of the original downstream implementation (regression against
    the merged debug-tools series).
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/spinlock.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h
index 950ec938dc9..2d348d81c64 100644
--- a/include/nuttx/spinlock.h
+++ b/include/nuttx/spinlock.h
@@ -408,6 +408,10 @@ spin_unlock_notrace(FAR volatile spinlock_t *lock)
 #ifdef CONFIG_SPINLOCK
 static inline_function void spin_unlock(FAR volatile spinlock_t *lock)
 {
+  /* Mark the spinlock has been unlocked */
+
+  spinlock_mark_unlocked(&lock->info);
+
   /* Unlock without trace note */
 
   spin_unlock_notrace(lock);
@@ -415,10 +419,6 @@ static inline_function void spin_unlock(FAR volatile 
spinlock_t *lock)
   /* Notify that we are unlocking the spinlock */
 
   sched_note_spinlock_unlock(lock);
-
-  /* Mark the spinlock has been unlocked */
-
-  spinlock_mark_unlocked(&lock->info);
 }
 #else
 #  define spin_unlock(lock) ((void)(lock))
@@ -1011,6 +1011,10 @@ void spin_unlock_irqrestore_notrace(FAR volatile 
spinlock_t *lock,
 static inline_function
 void spin_unlock_irqrestore(FAR volatile spinlock_t *lock, irqstate_t flags)
 {
+  /* Mark the spinlock has been unlocked */
+
+  spinlock_mark_unlocked(&lock->info);
+
   /* Unlock without trace note */
 
   spin_unlock_irqrestore_notrace(lock, flags);
@@ -1018,10 +1022,6 @@ void spin_unlock_irqrestore(FAR volatile spinlock_t 
*lock, irqstate_t flags)
   /* Notify that we are unlocking the spinlock */
 
   sched_note_spinlock_unlock(lock);
-
-  /* Mark the spinlock has been unlocked */
-
-  spinlock_mark_unlocked(&lock->info);
 }
 #else
 #  define spin_unlock_irqrestore(l, f) ((void)(l), irq_restore(f))

Reply via email to