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 b698c0c5f006b74a552d1c09163e16341fa169f4
Author: wangzhi16 <[email protected]>
AuthorDate: Wed Jan 7 18:05:30 2026 +0800

    sched/spinlock: Ignore switch checks in interrupt context.
    
    Previously, the switch hook was located outside the `irq_dispatch()` 
function, so it wouldn't be checked even if a context switch occurred while 
`irq_dispatch()` was holding a spinlock.
    
    Now, the switch hook is located inside the `irq_dispatch()` function, and 
it additionally checks for spinlock holding during context switches. Therefore, 
interrupt contexts need to be ignored.
    
    Assisted-By: mimo-v2.6-pro
    Co-authored-by: ouyangxiangzhen <[email protected]>
    Signed-off-by: wangzhi16 <[email protected]>
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 sched/irq/irq_spinlock.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/sched/irq/irq_spinlock.c b/sched/irq/irq_spinlock.c
index d6f8c0f5bb4..34805a05d18 100644
--- a/sched/irq/irq_spinlock.c
+++ b/sched/irq/irq_spinlock.c
@@ -82,6 +82,17 @@ void spinlock_mark_locking(FAR spinlock_debug_t *info)
     }
 #endif
 
+  /* Calling `spinlock_mark_locking` does not necessarily disable
+   * interrupts. If interrupts are not disabled, an interrupt might
+   * corrupt the previous `wait_spinlock` information. So, interrupt
+   * context is not considered.
+   */
+
+  if (up_interrupt_context())
+    {
+      return;
+    }
+
   /* Check if the current tcb is not waiting for other spinlock. */
 
   if (rtcb->wait_spinlock != NULL)
@@ -117,6 +128,11 @@ void spinlock_mark_locked(FAR spinlock_debug_t *info)
     }
 #endif
 
+  if (up_interrupt_context())
+    {
+      return;
+    }
+
   /* Check if the spinlock has been held. */
 
   if (info->holder != NULL)
@@ -165,6 +181,11 @@ void spinlock_mark_unlocked(FAR spinlock_debug_t *info)
     }
 #endif
 
+  if (up_interrupt_context())
+    {
+      return;
+    }
+
   /* Check if locking and unlocking are the same task. */
 
   if (info->holder != rtcb)
@@ -196,7 +217,7 @@ void spinlock_mark_unlocked(FAR spinlock_debug_t *info)
 
 void spinlock_switch_context(FAR struct tcb_s *from)
 {
-  if (!sq_empty(&from->hold_spinlock))
+  if (!sq_empty(&from->hold_spinlock) && !up_interrupt_context())
     {
       _alert("Detect thread(%d) switches context while holding spinlock!\n",
              from->pid);

Reply via email to