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 d2cc7c21ae2d3806792003cc77b1fc8b9e58d472
Author: wangzhi16 <[email protected]>
AuthorDate: Wed Apr 22 15:50:21 2026 +0800

    sched/spinlock: fix critmonitor irqdis tracking in notrace and interrupt 
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 <[email protected]>
    Signed-off-by: wangzhi16 <[email protected]>
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/spinlock.h        | 9 +++++----
 sched/sched/sched_critmonitor.c | 9 ++++++---
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h
index b8d853ba663..7419ae5c2b9 100644
--- a/include/nuttx/spinlock.h
+++ b/include/nuttx/spinlock.h
@@ -1156,10 +1156,6 @@ void rspin_unlock_irqrestore_notrace(FAR rspinlock_t 
*lock,
     {
       up_irq_restore(flags);
     }
-  else
-    {
-      nxsched_critmon_irqdisable(false, false, return_address(0));
-    }
 }
 
 static inline_function
@@ -1171,6 +1167,11 @@ void rspin_unlock_irqrestore(FAR rspinlock_t *lock, 
irqstate_t flags)
     }
   else
     {
+      /* Not last unlock, irq_restore is not called, but we need to
+       * balance the crit_count that was incremented by irq_save()
+       * in the corresponding rspin_lock_irqsave().
+       */
+
       nxsched_critmon_irqdisable(false, false, return_address(0));
     }
 }
diff --git a/sched/sched/sched_critmonitor.c b/sched/sched/sched_critmonitor.c
index 4d46e0c71ec..96f359015c8 100644
--- a/sched/sched/sched_critmonitor.c
+++ b/sched/sched/sched_critmonitor.c
@@ -268,17 +268,20 @@ void nxsched_critmon_irqdisable(bool state, bool force, 
FAR void *caller)
   FAR struct tcb_s *tcb = running_task();
   clock_t current;
 
-  /* If tcb is NULL, this means the thread has exited, exited thread
+  /* IRQ duration is tracked in irqinfo;
+   * critmon records the interrupt-disable time in thread context only.
+   * If tcb is NULL, this means the thread has exited, exited thread
    * is recorded in nxtask_exit().
    */
 
-  if (tcb == NULL || (!force && state && tcb->irqdis_count++ != 0) ||
+  if (up_interrupt_context() || tcb == NULL ||
+      (!force && state && tcb->irqdis_count++ != 0) ||
       (!force && !state && --tcb->irqdis_count != 0))
     {
       return;
     }
 
-  /* Call perf_gettime only tcb->irqdis_count == 0 or 1, prevent recursion. */
+  /* Call perf_gettime only tcb->irqdis_count == 0, prevent recursion. */
 
   current = perf_gettime();
 

Reply via email to