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 2ccf503903b550c4956387d14680ee0f67cb1fd6
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Tue Nov 24 10:07:16 2026 +0800

    spinlock: mark the lock as held before the busy-wait accounting
    
    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 <[email protected]>
---
 include/nuttx/spinlock.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h
index b6c34b41d0b..b6825319c85 100644
--- a/include/nuttx/spinlock.h
+++ b/include/nuttx/spinlock.h
@@ -236,6 +236,10 @@ static inline_function void spin_lock(FAR volatile 
spinlock_t *lock)
 
   spin_lock_notrace(lock);
 
+  /* Mark the spinlock has been locked */
+
+  spinlock_mark_locked(&lock->info);
+
   /* Get the lock, end counting busy-waiting */
 
   nxsched_critmon_busywait(false, return_address(0));
@@ -243,10 +247,6 @@ static inline_function void spin_lock(FAR volatile 
spinlock_t *lock)
   /* Notify that we have the spinlock */
 
   sched_note_spinlock_locked(lock);
-
-  /* Mark the spinlock has been locked */
-
-  spinlock_mark_locked(&lock->info);
 }
 #else
 #  define spin_lock(lock) ((void)(lock))
@@ -332,13 +332,13 @@ static inline_function bool spin_trylock(FAR volatile 
spinlock_t *lock)
   locked = spin_trylock_notrace(lock);
   if (locked)
     {
-      /* Notify that we have the spinlock */
-
-      sched_note_spinlock_locked(lock);
-
       /* Mark the spinlock has been locked */
 
       spinlock_mark_locked(&lock->info);
+
+      /* Notify that we have the spinlock */
+
+      sched_note_spinlock_locked(lock);
     }
   else
     {
@@ -515,6 +515,10 @@ irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
 
   spin_lock_notrace(lock);
 
+  /* Mark the spinlock has been locked */
+
+  spinlock_mark_locked(&lock->info);
+
   /* Get the lock, end counting busy-waiting */
 
   nxsched_critmon_busywait(false, return_address(0));
@@ -523,10 +527,6 @@ irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock)
 
   sched_note_spinlock_locked(lock);
 
-  /* Mark the spinlock has been locked */
-
-  spinlock_mark_locked(&lock->info);
-
   return flags;
 }
 #else

Reply via email to