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 028c61f61a2c967b6d4fb9767ff4ada04da589b1 Author: wangzhi16 <[email protected]> AuthorDate: Wed Feb 4 16:10:36 2026 +0800 sched/irq: extend interrupt-disabled time monitoring to spinlock The critmon previously only tracked interrupt disable time within enter/leave_critical_section(). But spinlock and rspinlock also disable interrupts without being accounted for. Move nxsched_critmon_csection() to include/nuttx/irq.h and instrument irq_save()/irq_restore() directly, so all interrupt disable paths are automatically monitored. Add crit_count nesting counter in tcb to prevent recursion when perf_gettime() triggers irq_save internally. Also add critmon calls in rspin_breaklock/restorelock for proper tracking across context switches, and use up_irq_save directly in this_task() to avoid recursion. Remove #include <nuttx/irq.h> from all arch/<arch>/include/irq.h headers to break the circular dependency introduced by moving nxsched_critmon_csection() into nuttx/irq.h, since nuttx/irq.h now depends on arch irq primitives (up_irq_save/up_irq_restore) which are defined in arch headers. Assisted-By: mimo-v2.6-pro Co-authored-by: ouyangxiangzhen <[email protected]> Signed-off-by: wangzhi16 <[email protected]> Signed-off-by: ouyangxiangzhen <[email protected]> --- arch/arm/include/irq.h | 4 ---- arch/arm64/include/irq.h | 5 +---- arch/avr/include/irq.h | 4 ---- arch/hc/include/irq.h | 4 ---- arch/mips/include/irq.h | 4 ---- arch/misoc/include/irq.h | 1 - arch/or1k/include/irq.h | 4 ---- arch/renesas/include/irq.h | 1 - arch/sparc/include/irq.h | 4 ---- arch/tricore/include/irq.h | 4 ---- arch/x86/include/irq.h | 5 +---- arch/x86_64/include/irq.h | 7 ++++--- arch/xtensa/include/irq.h | 3 --- arch/z16/include/irq.h | 1 - arch/z80/include/irq.h | 5 ++++- include/nuttx/irq.h | 39 ++++++++++++++++++++++++++++++++------- include/nuttx/sched.h | 1 + include/nuttx/spinlock.h | 21 ++++++++++++++++++++- sched/Kconfig | 6 ++++-- sched/irq/irq_csection.c | 6 ------ sched/sched/sched.h | 10 ++-------- sched/sched/sched_critmonitor.c | 38 +++++++++++++++++++++++++++----------- sched/task/task_restart.c | 1 + 23 files changed, 97 insertions(+), 81 deletions(-) diff --git a/arch/arm/include/irq.h b/arch/arm/include/irq.h index 58de3789a70..388d25b8883 100644 --- a/arch/arm/include/irq.h +++ b/arch/arm/include/irq.h @@ -50,10 +50,6 @@ #include <arch/chip/irq.h> -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include ARM architecture-specific IRQ definitions (including register * save structure and up_irq_save()/up_irq_restore() functions) */ diff --git a/arch/arm64/include/irq.h b/arch/arm64/include/irq.h index 9f92d62c3d0..b2b6d5d9f72 100644 --- a/arch/arm64/include/irq.h +++ b/arch/arm64/include/irq.h @@ -36,6 +36,7 @@ #ifndef __ASSEMBLY__ # include <stdint.h> +# include <stdbool.h> # include <arch/syscall.h> # include <nuttx/macro.h> #endif @@ -44,10 +45,6 @@ #include <arch/chip/irq.h> -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /**************************************************************************** * Pre-processor Prototypes ****************************************************************************/ diff --git a/arch/avr/include/irq.h b/arch/avr/include/irq.h index c1d1c3a5df9..3f2640a8621 100644 --- a/arch/avr/include/irq.h +++ b/arch/avr/include/irq.h @@ -36,10 +36,6 @@ # include <stdbool.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/hc/include/irq.h b/arch/hc/include/irq.h index 24cc38de6b6..03883aad520 100644 --- a/arch/hc/include/irq.h +++ b/arch/hc/include/irq.h @@ -36,10 +36,6 @@ # include <stdbool.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/mips/include/irq.h b/arch/mips/include/irq.h index ec9ed1a06de..cb291b176db 100644 --- a/arch/mips/include/irq.h +++ b/arch/mips/include/irq.h @@ -36,10 +36,6 @@ # include <stdbool.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/misoc/include/irq.h b/arch/misoc/include/irq.h index 8f0001b4b8d..952a6e175f9 100644 --- a/arch/misoc/include/irq.h +++ b/arch/misoc/include/irq.h @@ -36,7 +36,6 @@ # include <stdbool.h> #endif -#include <nuttx/irq.h> #include <arch/chip/irq.h> #ifdef CONFIG_ARCH_CHIP_LM32 diff --git a/arch/or1k/include/irq.h b/arch/or1k/include/irq.h index a764b26b13a..ca7309ad836 100644 --- a/arch/or1k/include/irq.h +++ b/arch/or1k/include/irq.h @@ -36,10 +36,6 @@ # include <stdbool.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/renesas/include/irq.h b/arch/renesas/include/irq.h index 334dd69f223..6bb73d05133 100644 --- a/arch/renesas/include/irq.h +++ b/arch/renesas/include/irq.h @@ -36,7 +36,6 @@ # include <stdbool.h> #endif -#include <nuttx/irq.h> #include <arch/chip/irq.h> /**************************************************************************** diff --git a/arch/sparc/include/irq.h b/arch/sparc/include/irq.h index 9c9c40be6a2..063f581efd4 100644 --- a/arch/sparc/include/irq.h +++ b/arch/sparc/include/irq.h @@ -36,10 +36,6 @@ # include <stdbool.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/tricore/include/irq.h b/arch/tricore/include/irq.h index 8883fbc60c2..0cc69de26bf 100644 --- a/arch/tricore/include/irq.h +++ b/arch/tricore/include/irq.h @@ -37,10 +37,6 @@ # include <syscall.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/x86/include/irq.h b/arch/x86/include/irq.h index 28ccdeb0eff..b367cc19d4f 100644 --- a/arch/x86/include/irq.h +++ b/arch/x86/include/irq.h @@ -33,12 +33,9 @@ #ifndef __ASSEMBLY__ # include <stddef.h> +# include <stdint.h> #endif -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> - /* Include chip-specific IRQ definitions (including IRQ numbers) */ #include <arch/chip/irq.h> diff --git a/arch/x86_64/include/irq.h b/arch/x86_64/include/irq.h index 0c684cd43c6..4c86ed936e8 100644 --- a/arch/x86_64/include/irq.h +++ b/arch/x86_64/include/irq.h @@ -31,9 +31,10 @@ * Included Files ****************************************************************************/ -/* Include NuttX-specific IRQ definitions */ - -#include <nuttx/irq.h> +#ifndef __ASSEMBLY__ +# include <stdint.h> +# include <stdbool.h> +#endif /* Include chip-specific IRQ definitions (including IRQ numbers) */ diff --git a/arch/xtensa/include/irq.h b/arch/xtensa/include/irq.h index ea36801bbe2..84eaf24f8eb 100644 --- a/arch/xtensa/include/irq.h +++ b/arch/xtensa/include/irq.h @@ -31,10 +31,7 @@ * Included Files ****************************************************************************/ -/* Include NuttX-specific IRQ definitions */ - #include <nuttx/config.h> -#include <nuttx/irq.h> #include <sys/types.h> #ifndef __ASSEMBLY__ diff --git a/arch/z16/include/irq.h b/arch/z16/include/irq.h index bd3e6d946d9..e5b8ff25c6c 100644 --- a/arch/z16/include/irq.h +++ b/arch/z16/include/irq.h @@ -36,7 +36,6 @@ # include <stdbool.h> #endif -#include <nuttx/irq.h> #include <arch/chip/irq.h> /**************************************************************************** diff --git a/arch/z80/include/irq.h b/arch/z80/include/irq.h index 2f7e17c731c..95ba7af5b5d 100644 --- a/arch/z80/include/irq.h +++ b/arch/z80/include/irq.h @@ -31,7 +31,10 @@ * Included Files ****************************************************************************/ -#include <nuttx/irq.h> +#ifndef __ASSEMBLY__ +# include <stdint.h> +#endif + #include <arch/chip/irq.h> /**************************************************************************** diff --git a/include/nuttx/irq.h b/include/nuttx/irq.h index da5142a4571..09cfeb6f32e 100644 --- a/include/nuttx/irq.h +++ b/include/nuttx/irq.h @@ -138,13 +138,6 @@ #define IRQ_WAKE_THREAD 1 -/* Disable/restore local interrupts from code above the arch layer */ - -#ifndef __ASSEMBLY__ -# define irq_save() up_irq_save() -# define irq_restore(flags) up_irq_restore(flags) -#endif - /* Scheduling monitor */ #ifndef CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD @@ -246,6 +239,38 @@ int ndx_to_irq(int ndx); * Public Function Prototypes ****************************************************************************/ +/**************************************************************************** + * Name: irq_save / irq_restore + * + * Description: + * Wrapper around up_irq_save/up_irq_restore with IRQ disabled duration + * monitoring via nxsched_critmon_csection. + * + ****************************************************************************/ + +#ifndef __ASSEMBLY__ +# if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 +void nxsched_critmon_csection(bool state, bool force, FAR void *caller); + +static inline_function irqstate_t irq_save(void) +{ + irqstate_t flags = up_irq_save(); + nxsched_critmon_csection(true, false, return_address(0)); + return flags; +} + +static inline_function void irq_restore(irqstate_t flags) +{ + nxsched_critmon_csection(false, false, return_address(0)); + up_irq_restore(flags); +} +# else +# define nxsched_critmon_csection(state, force, caller) +# define irq_save() up_irq_save() +# define irq_restore(flags) up_irq_restore(flags) +# endif +#endif /* __ASSEMBLY__ */ + /**************************************************************************** * Name: irq_attach * diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 0271228e6da..7c2fd7c540f 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -789,6 +789,7 @@ struct tcb_s #if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 clock_t crit_start; /* Time critical section entered */ clock_t crit_max; /* Max time in critical section */ + int crit_count; /* Nest count of critical section */ void *crit_caller; /* Caller of critical section */ void *crit_max_caller; /* Caller of max critical section */ #endif diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h index 2d348d81c64..7471a8a91d3 100644 --- a/include/nuttx/spinlock.h +++ b/include/nuttx/spinlock.h @@ -1118,8 +1118,15 @@ void rspin_unlock_irqrestore(FAR rspinlock_t *lock, irqstate_t flags) { irq_restore(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(). + */ - /* If not last rspinlock restore, irq_restore should not required */ + nxsched_critmon_csection(false, false, return_address(0)); + } } #else # define rspin_unlock_irqrestore(l, f) ((void)(l), irq_restore(f)) @@ -1145,6 +1152,12 @@ uint16_t rspin_breaklock(FAR rspinlock_t *lock) if (RSPINLOCK_OWNER(val) == owner) { + /* When context switch, we need to mark that current thread has + * enabled interrupt. + */ + + nxsched_critmon_csection(false, true, return_address(0)); + oldcount = RSPINLOCK_COUNT(val); lock->local_val = RSPINLOCK_VALUE(owner, 1u); rspin_unlock(lock); @@ -1162,6 +1175,12 @@ void rspin_restorelock(FAR rspinlock_t *lock, uint16_t count) rspin_lock(lock); val = lock->local_val; lock->local_val = RSPINLOCK_VALUE(RSPINLOCK_OWNER(val), count); + + /* When context switch, we need to mark that current thread has + * disabled interrupt. + */ + + nxsched_critmon_csection(true, true, return_address(0)); } } #else diff --git a/sched/Kconfig b/sched/Kconfig index e71c30b9267..d9ff0175c89 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -1137,10 +1137,12 @@ config SCHED_CRITMONITOR_MAXTIME_PREEMPTION For debugging system latency, 0 means disabled. config SCHED_CRITMONITOR_MAXTIME_CSECTION - int "Csection (enter_critical_section) max holding time" + int "IRQ disabled (irq_save/irq_restore) max holding time" default SCHED_CRITMONITOR_MAXTIME_PREEMPTION ---help--- - Csection holding time should be smaller than + IRQ disabled time (via irq_save/irq_restore, covering + enter_critical_section, spin_lock_irqsave, + rspin_lock_irqsave, etc.) should be smaller than SCHED_CRITMONITOR_MAXTIME_CSECTION, or system will give a warning. For debugging system latency, 0 means disabled. diff --git a/sched/irq/irq_csection.c b/sched/irq/irq_csection.c index 0cb0ee19a2f..656b411eb5a 100644 --- a/sched/irq/irq_csection.c +++ b/sched/irq/irq_csection.c @@ -310,9 +310,6 @@ irqstate_t enter_critical_section(void) rtcb = this_task(); if (rtcb->irqcount == 1) { -#if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 - nxsched_critmon_csection(rtcb, true, return_address(0)); -#endif #ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION sched_note_csection(rtcb, true); #endif @@ -471,9 +468,6 @@ void leave_critical_section(irqstate_t flags) rtcb = this_task(); if (rtcb->irqcount == 1) { -# if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 - nxsched_critmon_csection(rtcb, false, return_address(0)); -# endif # ifdef CONFIG_SCHED_INSTRUMENTATION_CSECTION sched_note_csection(rtcb, false); # endif diff --git a/sched/sched/sched.h b/sched/sched/sched.h index 92865f671f2..fcc67cb6dbf 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -420,9 +420,8 @@ static inline_function FAR struct tcb_s *this_task(void) FAR struct tcb_s *tcb; irqstate_t flags; - /* If the CPU supports suppression of interprocessor interrupts, then - * simple disabling interrupts will provide sufficient protection for - * the following operations. + /* Use up_irq_save directly to avoid recursion when called from + * irq_save() -> nxsched_critmon_csection() -> running_task() path. */ flags = up_irq_save(); @@ -484,11 +483,6 @@ void nxsched_critmon_preemption(FAR struct tcb_s *tcb, bool state, # define nxsched_critmon_preemption(t, s, c) #endif -#if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 -void nxsched_critmon_csection(FAR struct tcb_s *tcb, bool state, - FAR void *caller); -#endif - #ifdef CONFIG_SCHED_CAPABILITIES bool nxsched_may_control(pid_t pid); #else diff --git a/sched/sched/sched_critmonitor.c b/sched/sched/sched_critmonitor.c index 25f7df9090d..32403ae0b88 100644 --- a/sched/sched/sched_critmonitor.c +++ b/sched/sched/sched_critmonitor.c @@ -63,7 +63,7 @@ if (pid > 0 && \ elapsed > CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION) \ { \ - CRITMONITOR_PANIC("PID %d hold critical section too long " \ + CRITMONITOR_PANIC("PID %d disable interrupt too long " \ "%ju\n", pid, (uintmax_t)elapsed); \ } \ } \ @@ -251,33 +251,49 @@ void nxsched_critmon_preemption(FAR struct tcb_s *tcb, bool state, * Name: nxsched_critmon_csection * * Description: - * Called when a thread enters or leaves a critical section. + * Called by irq_save/irq_restore wrappers when a thread disables or + * re-enables interrupts. Covers all IRQ-disabled paths including + * enter_critical_section, spin_lock_irqsave, rspin_lock_irqsave, etc. * * Assumptions: - * - Called within a critical section. - * - Never called from an interrupt handler - * - Caller is the address of the function that is entering the critical + * - Called with interrupts already disabled (on entry) or about to be + * re-enabled (on exit). + * - Might be called from an interrupt handler * ****************************************************************************/ #if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 -void nxsched_critmon_csection(FAR struct tcb_s *tcb, bool state, - FAR void *caller) +void nxsched_critmon_csection(bool state, bool force, FAR void *caller) { - clock_t current = perf_gettime(); + FAR struct tcb_s *tcb = running_task(); + clock_t current; + + /* If tcb is NULL, this means the thread has exited, exited thread + * is recorded in nxtask_exit(). + */ + + if (tcb == NULL || (!force && state && tcb->crit_count++ != 0) || + (!force && !state && --tcb->crit_count != 0)) + { + return; + } + + /* Call perf_gettime only tcb->crit_count == 0 or 1, prevent recursion. */ + + current = perf_gettime(); - /* Are we entering or leaving the critical section? */ + /* Are we disabling or enabling interrupts? */ if (state) { - /* Entering... Save the start time. */ + /* Disabling... Save the start time. */ tcb->crit_start = current; tcb->crit_caller = caller; } else { - /* Leaving .. Check for the max elapsed time */ + /* Enabling.. Check for the max elapsed time */ clock_t elapsed = current - tcb->crit_start; int cpu = this_cpu(); diff --git a/sched/task/task_restart.c b/sched/task/task_restart.c index 6c3ed1af3ef..6ee71e78358 100644 --- a/sched/task/task_restart.c +++ b/sched/task/task_restart.c @@ -284,6 +284,7 @@ static int nxtask_restart(pid_t pid) #if CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION >= 0 tcb->crit_start = 0; tcb->crit_max = 0; + tcb->crit_count = 0; tcb->crit_caller = NULL; tcb->crit_max_caller = NULL; #endif
