From e30d86061f2e399aa7fbfe062db841dfea8af356 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 20:55:29 +0800 Subject: [PATCH 01/23] sched/hrtimer: Convert global variables to per-CPU and add initialization Convert the hrtimer global variables (g_hrtimer_lock, g_hrtimer_tree, g_hrtimer_head, g_hrtimer_list, g_hrtimer_last_expired and g_hrtimer_running) to per-CPU variables with the BMP/SMP macros for SMP support, and add hrtimer_initialize() called from nx_start() to initialize the per-CPU queue at runtime. Co-authored-by: buxiasen Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- sched/hrtimer/hrtimer.h | 55 ++++++++++++++++++++++++------ sched/hrtimer/hrtimer_initialize.c | 23 +++++++++---- sched/init/nx_start.c | 4 +++ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index 4e6a38df6aeb7..b27f47c16a310 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -90,23 +90,31 @@ RB_HEAD(hrtimer_tree_s, hrtimer_s); /* Seqcount protecting access to the hrtimer queue and timer state */ -extern seqcount_t g_hrtimer_lock; +#ifdef CONFIG_SMP +DECLARE_PER_CPU_BMP(seqcount_t, g_hrtimer_lock); +#define g_hrtimer_lock this_cpu_var_bmp(g_hrtimer_lock) +#endif /* The time of the last hrtimer_process() run, used as the reference * point for hrtimer_last_expired(). */ -extern uint64_t g_hrtimer_last_expired; +DECLARE_PER_CPU_BMP(uint64_t, g_hrtimer_last_expired); +#define g_hrtimer_last_expired this_cpu_var_bmp(g_hrtimer_last_expired) #ifdef CONFIG_HRTIMER_TREE /* Red-Black tree containing all active high-resolution timers */ -extern struct hrtimer_tree_s g_hrtimer_tree; -extern FAR struct hrtimer_s *g_hrtimer_head; +DECLARE_PER_CPU_BMP(struct hrtimer_tree_s, g_hrtimer_tree); +#define g_hrtimer_tree this_cpu_var_bmp(g_hrtimer_tree) + +DECLARE_PER_CPU_BMP(FAR struct hrtimer_s *, g_hrtimer_head); +#define g_hrtimer_head this_cpu_var_bmp(g_hrtimer_head) #else /* List containing all active high-resolution timers */ -extern struct list_node g_hrtimer_list; +DECLARE_PER_CPU_BMP(struct list_node, g_hrtimer_list); +#define g_hrtimer_list this_cpu_var_bmp(g_hrtimer_list) #endif #ifdef CONFIG_SMP @@ -114,7 +122,7 @@ extern struct list_node g_hrtimer_list; * for each CPU in SMP configurations. Index corresponds to CPU ID. */ -extern uintptr_t g_hrtimer_running[CONFIG_SMP_NCPUS]; +DECLARE_PER_CPU_SMP(uintptr_t, g_hrtimer_running); #endif /**************************************************************************** @@ -211,6 +219,30 @@ static inline_function uint64_t hrtimer_last_expired(void) return g_hrtimer_last_expired; } +/**************************************************************************** + * Name: hrtimer_initialize + * + * Description: + * Called from nx_start() to initialize the per-CPU hrtimer data. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline_function void hrtimer_initialize(void) +{ +#ifdef CONFIG_HRTIMER_TREE + RB_INIT(&g_hrtimer_tree); + g_hrtimer_head = NULL; +#else + list_initialize(&g_hrtimer_list); +#endif +} + /**************************************************************************** * Name: hrtimer_next_expired * @@ -508,7 +540,7 @@ DEFINE_HRTIMER_READ(32, uint32_t) #ifdef CONFIG_SMP # define hrtimer_mark_running(timer, cpu) \ - (g_hrtimer_running[cpu] = (uintptr_t)(timer)) + (per_cpu_var_smp(g_hrtimer_running, cpu) = (uintptr_t)(timer)) #else # define hrtimer_mark_running(timer, cpu) UNUSED(cpu) #endif @@ -557,7 +589,7 @@ void hrtimer_unmark_running(int cpu) #ifdef CONFIG_SMP # define hrtimer_is_running(timer, cpu) \ - (hrtimer_read_ptr(&g_hrtimer_running[cpu]) == (uintptr_t)(timer)) + (hrtimer_read_ptr(&per_cpu_var_smp(g_hrtimer_running, cpu)) == (uintptr_t)(timer)) #else # define hrtimer_is_running(timer, cpu) (true) #endif @@ -601,18 +633,19 @@ int hrtimer_cancel_running(FAR hrtimer_t *timer) for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++) { /* This is a faster implementation equivalent to - * (g_hrtimer_running[cpu] & (~(uintptr_t)0x1u)) == timer, + * (per_cpu_var_smp(g_hrtimer_running, cpu) & + * (~(uintptr_t)0x1u)) == timer, * Assuming the pointer of the timer is at least 2-bytes aligned * (the last bit must be zero). */ - if ((g_hrtimer_running[cpu] ^ cancelled) <= 0x1u) + if ((per_cpu_var_smp(g_hrtimer_running, cpu) ^ cancelled) <= 0x1u) { /* Set the timer to the cancelled state and revoke the write * ownership of the timer from the queue. */ - g_hrtimer_running[cpu] = cancelled; + per_cpu_var_smp(g_hrtimer_running, cpu) = cancelled; refs++; } } diff --git a/sched/hrtimer/hrtimer_initialize.c b/sched/hrtimer/hrtimer_initialize.c index e89db89691c3a..d467595a2c858 100644 --- a/sched/hrtimer/hrtimer_initialize.c +++ b/sched/hrtimer/hrtimer_initialize.c @@ -37,7 +37,7 @@ * for each CPU in SMP configurations. Index corresponds to CPU ID. */ -uintptr_t g_hrtimer_running[CONFIG_SMP_NCPUS]; +DEFINE_PER_CPU_BSS_SMP(uintptr_t, g_hrtimer_running); /* Global spinlock protecting the high-resolution timer subsystem. * @@ -46,7 +46,9 @@ uintptr_t g_hrtimer_running[CONFIG_SMP_NCPUS]; * timer queue is modified. */ -seqcount_t g_hrtimer_lock = SEQLOCK_INITIALIZER; +#undef g_hrtimer_lock +DEFINE_PER_CPU_BMP(seqcount_t, g_hrtimer_lock) = SEQLOCK_INITIALIZER; +#define g_hrtimer_lock this_cpu_var_bmp(g_hrtimer_lock) #endif /* CONFIG_SMP */ /* HRTimer queue for all active high-resolution timers. @@ -58,13 +60,22 @@ seqcount_t g_hrtimer_lock = SEQLOCK_INITIALIZER; * both configurations. */ -uint64_t g_hrtimer_last_expired; +#undef g_hrtimer_last_expired +DEFINE_PER_CPU_BSS_BMP(uint64_t, g_hrtimer_last_expired); +#define g_hrtimer_last_expired this_cpu_var_bmp(g_hrtimer_last_expired) #ifdef CONFIG_HRTIMER_TREE -struct hrtimer_tree_s g_hrtimer_tree; -struct FAR hrtimer_s *g_hrtimer_head; +#undef g_hrtimer_tree +DEFINE_PER_CPU_BSS_BMP(struct hrtimer_tree_s, g_hrtimer_tree); +#define g_hrtimer_tree this_cpu_var_bmp(g_hrtimer_tree) + +#undef g_hrtimer_head +DEFINE_PER_CPU_BSS_BMP(FAR struct hrtimer_s *, g_hrtimer_head); +#define g_hrtimer_head this_cpu_var_bmp(g_hrtimer_head) #else -struct list_node g_hrtimer_list = LIST_INITIAL_VALUE(g_hrtimer_list); +#undef g_hrtimer_list +DEFINE_PER_CPU_BSS_BMP(struct list_node, g_hrtimer_list); +#define g_hrtimer_list this_cpu_var_bmp(g_hrtimer_list) #endif /**************************************************************************** diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index 53d4d28172b7a..867ed1f7aca90 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -555,6 +555,10 @@ void nx_start(void) wdlist_initialize(); + /* Initialize hrtimer *****************************************************/ + + hrtimer_initialize(); + /* Initialize task list table *********************************************/ tasklist_initialize(); From 14fd1c8cd806484489ddcdbe5aae609af9a0e97f Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Wed, 4 Nov 2026 10:53:59 +0800 Subject: [PATCH 02/23] hrtimer: introduce hrtimer_is_active() The hrtimer structure content should not be accessed directly by users, so provide a predicate for the "is the timer armed" question needed by the watchdog upper half status query. It is defined by the remaining time reported by hrtimer_gettime(): a non-zero remainder means the timer is pending and has not expired yet, which matches the former wdog func based test except at the instant of expiration. The gettime implementation clamps a negative remainder to zero, so unused and cancelled timers are reported inactive without wraparound. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- include/nuttx/hrtimer.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/nuttx/hrtimer.h b/include/nuttx/hrtimer.h index 2a54bfb96d532..dddb17eac9198 100644 --- a/include/nuttx/hrtimer.h +++ b/include/nuttx/hrtimer.h @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -242,6 +243,35 @@ int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t func, uint64_t hrtimer_gettime(FAR const hrtimer_t *timer); +/**************************************************************************** + * Name: hrtimer_is_active + * + * Description: + * Check whether the hrtimer is armed. The test is defined by the + * remaining time reported by hrtimer_gettime(): a non-zero remainder + * means the timer is pending and has not expired yet. A timer whose + * expiration time has been reached reports inactive even if it has + * not been processed yet, and a zero-delay timer is inactive at the + * moment it is queued. + * + * Input Parameters: + * timer - Pointer to the high-resolution timer instance. + * + * Returned Value: + * true if the timer is armed and has not expired yet. + * + ****************************************************************************/ + +static inline_function +bool hrtimer_is_active(FAR const hrtimer_t *timer) +{ + /* A non-zero remainder means the timer is pending and has not expired + * yet. Evaluate the argument once. + */ + + return hrtimer_gettime(timer) != 0; +} + #ifdef __cplusplus } #endif From 3bb65826bc879b1e4644b5713c665f37ab5cdc14 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Thu, 2 Apr 2026 09:34:24 +0800 Subject: [PATCH 03/23] sched: Remove conditional compilation for HRTIMER. This change removes conditional compilation for CONFIG_HRTIMER, simplifying the wdog code. The legacy tick-based watchdog code is removed, unifying the implementation to use hrtimer. This is part of the effort to enable CONFIG_HRTIMER by default. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- .../fvp-armv8r/configs/citest/defconfig | 2 - .../c906/smartl-c906/configs/sotest/defconfig | 1 - .../rv-virt/configs/citest64/defconfig | 1 - boards/sim/sim/sim/configs/ostest/defconfig | 2 - .../tc3x/a2g-tc375-lite/configs/nsh/defconfig | 1 - .../a2g-tc397-5v-tft/configs/nsh/defconfig | 1 - drivers/timers/arch_alarm.c | 2 +- include/nuttx/arch.h | 7 +- sched/Kconfig | 12 +-- sched/hrtimer/CMakeLists.txt | 7 +- sched/hrtimer/Make.defs | 6 +- sched/hrtimer/hrtimer.h | 5 +- sched/sched/sched_processtick.c | 4 - sched/sched/sched_processtickless.c | 36 ++----- sched/wdog/wd_initialize.c | 19 +--- sched/wdog/wd_start.c | 27 +---- sched/wdog/wdog.h | 98 +++---------------- 17 files changed, 35 insertions(+), 196 deletions(-) diff --git a/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig b/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig index 168dc7bf97572..b036f13597d1b 100644 --- a/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig +++ b/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig @@ -27,8 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXPERIMENTAL=y CONFIG_FS_PROCFS=y CONFIG_FS_ROMFS=y -CONFIG_HRTIMER=y -CONFIG_HRTIMER_LIST=y CONFIG_IDLETHREAD_STACKSIZE=8192 CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INTELHEX_BINARY=y diff --git a/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig b/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig index 9e5caac9e0d08..fa62f811656b5 100644 --- a/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig +++ b/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig @@ -27,7 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXAMPLES_SOTEST=y CONFIG_FS_PROCFS=y CONFIG_FS_ROMFS=y -CONFIG_HRTIMER=y CONFIG_IDLETHREAD_STACKSIZE=2048 CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INIT_STACKSIZE=3072 diff --git a/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig index bb45d675e0ca5..8736def1fcd4c 100644 --- a/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig +++ b/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig @@ -47,7 +47,6 @@ CONFIG_FS_PROCFS=y CONFIG_FS_ROMFS=y CONFIG_FS_TMPFS=y CONFIG_HAVE_CXX=y -CONFIG_HRTIMER=y CONFIG_IDLETHREAD_STACKSIZE=2048 CONFIG_INIT_ENTRYPOINT="init_main" CONFIG_INIT_STACKSIZE=4096 diff --git a/boards/sim/sim/sim/configs/ostest/defconfig b/boards/sim/sim/sim/configs/ostest/defconfig index 823979e2aed56..c2b1460267808 100644 --- a/boards/sim/sim/sim/configs/ostest/defconfig +++ b/boards/sim/sim/sim/configs/ostest/defconfig @@ -18,8 +18,6 @@ CONFIG_DEBUG_ASSERTIONS=y CONFIG_DEBUG_FEATURES=y CONFIG_DEBUG_SYMBOLS=y CONFIG_FS_NAMED_SEMAPHORES=y -CONFIG_HRTIMER=y -CONFIG_HRTIMER_LIST=y CONFIG_IDLETHREAD_STACKSIZE=4096 CONFIG_INIT_ENTRYPOINT="ostest_main" CONFIG_MM_KASAN=y diff --git a/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig b/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig index 5770b9dcb8cb6..ba0ce93921fba 100644 --- a/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig +++ b/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig @@ -27,7 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXPERIMENTAL=y CONFIG_FS_PROCFS=y CONFIG_FS_TMPFS=y -CONFIG_HRTIMER=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_LIBC_MEMFD_ERROR=y CONFIG_LIBM=y diff --git a/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig b/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig index c95e415d2e5bf..717e8838c2c42 100644 --- a/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig +++ b/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig @@ -27,7 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXPERIMENTAL=y CONFIG_FS_PROCFS=y CONFIG_FS_TMPFS=y -CONFIG_HRTIMER=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_LIBC_MEMFD_ERROR=y CONFIG_LIBM=y diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index 18ba8f56f1452..4c4c5e4ed3038 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -409,7 +409,7 @@ int weak_function up_alarm_tick_cancel(FAR clock_t *ticks) * ****************************************************************************/ -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) +#ifdef CONFIG_SCHED_TICKLESS int weak_function up_alarm_start(FAR const struct timespec *ts) { int ret = -EAGAIN; diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index 5f5f5abc75622..4924df77f78f1 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -2117,8 +2117,7 @@ int up_alarm_tick_cancel(FAR clock_t *ticks); * ****************************************************************************/ -#if (defined(CONFIG_HRTIMER) && defined(CONFIG_ALARM_ARCH)) || \ - (defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)) +#if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM) int up_alarm_start(FAR const struct timespec *ts); int up_alarm_tick_start(clock_t ticks); #endif @@ -2189,10 +2188,8 @@ int up_timer_tick_cancel(FAR clock_t *ticks); * ****************************************************************************/ -#if (defined(CONFIG_HRTIMER) && defined(CONFIG_TIMER_ARCH)) || \ - (defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_SCHED_TICKLESS_ALARM)) +#if defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_SCHED_TICKLESS_ALARM) int up_timer_start(FAR const struct timespec *ts); -int up_timer_tick_start(clock_t ticks); #endif /**************************************************************************** diff --git a/sched/Kconfig b/sched/Kconfig index 4fbd978845564..1c40d321aee92 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -2337,17 +2337,9 @@ config CUSTOM_SEMAPHORE_MAXVALUE Enable to support custom max value for semaphores. When this option is enabled, the max value of a semaphore can be set -config HRTIMER - bool "High resolution timer support" - default n - ---help--- - Enable to support high resolution timer - -if HRTIMER - choice prompt "HRTimer management data structure type" - default HRTIMER_TREE + default HRTIMER_LIST config HRTIMER_LIST bool "List-based timer management" @@ -2384,5 +2376,3 @@ config HRTIMER_PRECISION_SHIFT 10 - 1024 ns (~1 us) precision 15 - 32768 ns (~32 us) precision 20 - 1048576 ns (~1 ms) precision - -endif # HRTIMER diff --git a/sched/hrtimer/CMakeLists.txt b/sched/hrtimer/CMakeLists.txt index 74020614a53bb..bf8bfde0c70b2 100644 --- a/sched/hrtimer/CMakeLists.txt +++ b/sched/hrtimer/CMakeLists.txt @@ -22,9 +22,8 @@ # Add hrtimer-related files to the build set(CSRCS) -if(CONFIG_HRTIMER) - list(APPEND CSRCS hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c - hrtimer_gettime.c) -endif() + +list(APPEND CSRCS hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c + hrtimer_gettime.c) target_sources(sched PRIVATE ${CSRCS}) diff --git a/sched/hrtimer/Make.defs b/sched/hrtimer/Make.defs index dfd326ad61b6f..9acdd8485a9e6 100644 --- a/sched/hrtimer/Make.defs +++ b/sched/hrtimer/Make.defs @@ -22,10 +22,8 @@ # Add hrtimer-related files to the build -ifeq ($(CONFIG_HRTIMER),y) - CSRCS += hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c - CSRCS += hrtimer_gettime.c -endif +CSRCS += hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c +CSRCS += hrtimer_gettime.c # Include hrtimer build support diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index b27f47c16a310..9bdfdf5983f3a 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -39,8 +39,6 @@ #include "sched/sched.h" -#ifdef CONFIG_HRTIMER - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -445,7 +443,7 @@ static inline_function void hrtimer_insert(FAR hrtimer_t *hrtimer) { /* Until curr->expired has not timed out relative to expired */ - if (!clock_compare(curr->expired, expired)) + if (!HRTIMER_TIME_BEFORE_EQ(curr->expired, expired)) { break; } @@ -808,5 +806,4 @@ static inline_function void hrtimer_process(uint64_t now) hrtimer_unlock(flags); } -#endif /* CONFIG_HRTIMER */ #endif /* __SCHED_HRTIMER_HRTIMER_H */ diff --git a/sched/sched/sched_processtick.c b/sched/sched/sched_processtick.c index 5ada01f80460d..6c91d315d1e08 100644 --- a/sched/sched/sched_processtick.c +++ b/sched/sched/sched_processtick.c @@ -186,11 +186,7 @@ void nxsched_process_timer(void) /* Process watchdogs */ -#ifdef CONFIG_HRTIMER hrtimer_process(clock_systime_nsec()); -#else - wd_timer(clock_systime_ticks()); -#endif #ifdef CONFIG_SYSTEMTICK_HOOK /* Call out to a user-provided function in order to perform board-specific, diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index a6cd637e0d162..b2a106c19dd11 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -343,39 +343,15 @@ clock_t nxsched_process_scheduler(clock_t ticks, clock_t elapsed, void nxsched_process_timer(void) { -#ifdef CONFIG_HRTIMER - uint64_t nsec = clock_systime_nsec(); - clock_t ticks = NSEC2TICK(nsec); - - clock_update_sched_ticks(ticks); - - hrtimer_process(nsec); -#else - irqstate_t flags; - clock_t ticks; - - flags = enter_critical_section(); - -#ifdef CONFIG_CLOCK_TIMEKEEPING - /* Process wall time */ - - clock_update_wall_time(); -#endif - - /* Do not move the up_timer_gettick out of the critical section, - * it will violate the invariant that the g_timer_tick should be monotonic. - */ - - up_timer_gettick(&ticks); - - /* Update sched ticks */ + uint64_t nsec; + struct timespec ts; - clock_update_sched_ticks(ticks); + up_timer_gettime(&ts); + nsec = clock_time2nsec(&ts); - wd_timer(ticks); + clock_update_sched_ticks(NSEC2TICK(nsec)); - leave_critical_section(flags); -#endif + hrtimer_process(nsec); } /**************************************************************************** diff --git a/sched/wdog/wd_initialize.c b/sched/wdog/wd_initialize.c index 6a05753e39d76..6b1ad3e4afc0d 100644 --- a/sched/wdog/wd_initialize.c +++ b/sched/wdog/wd_initialize.c @@ -43,26 +43,17 @@ DEFINE_PER_CPU_BSS_BMP(struct list_node, g_wdactivelist); #define g_wdactivelist this_cpu_var_bmp(g_wdactivelist) -#ifdef CONFIG_HRTIMER -# undef g_wdtimer +#undef g_wdtimer DEFINE_PER_CPU_BSS_BMP(struct hrtimer_s, g_wdtimer); -# define g_wdtimer this_cpu_var_bmp(g_wdtimer) -#endif +#define g_wdtimer this_cpu_var_bmp(g_wdtimer) -# undef g_wdtimernested +#undef g_wdtimernested DEFINE_PER_CPU_BSS_BMP(bool, g_wdtimernested); -# define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) - -#ifdef CONFIG_HRTIMER - -#endif - -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) +#define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) #undef g_wdexpired DEFINE_PER_CPU_BSS_BMP(clock_t, g_wdexpired); -# define g_wdexpired this_cpu_var_bmp(g_wdexpired) -#endif +#define g_wdexpired this_cpu_var_bmp(g_wdexpired) /**************************************************************************** * Public Functions diff --git a/sched/wdog/wd_start.c b/sched/wdog/wd_start.c index 7cb3e8e0481c2..f3537dd0f3582 100644 --- a/sched/wdog/wd_start.c +++ b/sched/wdog/wd_start.c @@ -309,9 +309,6 @@ int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, flags = enter_critical_section(); - /* If the wdog is canceling, restarting the wdog is not allowed. */ - -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) /* We need to reassess timer if the watchdog * list head has changed. */ @@ -334,18 +331,7 @@ int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, wd_timer_start(wd_next_expire(), false); } -#else - UNUSED(reassess); - - /* Check if the watchdog has been started. If so, delete it. */ - - if (WDOG_ISACTIVE(wdog)) - { - list_delete_fast(&wdog->node); - } - wd_insert(wdog, ticks, wdentry, arg); -#endif leave_critical_section(flags); sched_note_wdog_start(wdentry, (FAR void *)(uintptr_t)ticks); @@ -380,23 +366,14 @@ int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, * ****************************************************************************/ -#ifndef CONFIG_HRTIMER -void wd_timer(clock_t ticks) -{ - /* Check if there are any active watchdogs to process */ - - wd_expiration(ticks); -} -#else uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired) { /* Check if there are any active watchdogs to process */ - clock_t tick = div_const(expired, NSEC_PER_TICK); + clock_t tick = div_const(expired, NSEC_PER_TICK); clock_t delay = wd_expiration(tick) - tick; - uint64_t nsec = delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? + uint64_t nsec = (uint64_t)delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? TICK2NSEC(delay) : HRTIMER_MAX_DELAY; return nsec; } -#endif diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h index 873005b083c6a..b2227db088e04 100644 --- a/sched/wdog/wdog.h +++ b/sched/wdog/wdog.h @@ -38,10 +38,7 @@ #include #include #include - -#ifdef CONFIG_HRTIMER -# include -#endif +#include /**************************************************************************** * Pre-processor Definitions @@ -64,17 +61,13 @@ extern "C" DECLARE_PER_CPU_BMP(struct list_node, g_wdactivelist); #define g_wdactivelist this_cpu_var_bmp(g_wdactivelist) -#ifdef CONFIG_HRTIMER DECLARE_PER_CPU_BMP(struct hrtimer_s, g_wdtimer); -# define g_wdtimer this_cpu_var_bmp(g_wdtimer) -#endif +#define g_wdtimer this_cpu_var_bmp(g_wdtimer) -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) DECLARE_PER_CPU_BMP(bool, g_wdtimernested); -# define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) +#define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) DECLARE_PER_CPU_BMP(clock_t, g_wdexpired); -# define g_wdexpired this_cpu_var_bmp(g_wdexpired) -#endif +#define g_wdexpired this_cpu_var_bmp(g_wdexpired) /**************************************************************************** * Public Function Prototypes @@ -106,36 +99,25 @@ DECLARE_PER_CPU_BMP(clock_t, g_wdexpired); * ****************************************************************************/ -#ifndef CONFIG_HRTIMER -void wd_timer(clock_t ticks); -#else uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired); -#endif /**************************************************************************** * Inline functions ****************************************************************************/ -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) -# define wd_in_callback() (g_wdtimernested) -# define wd_set_nested(f) (g_wdtimernested = (f)) -# define wd_update_expire(expired) (g_wdexpired = (expired)) -#else -# define wd_in_callback() (false) -# define wd_set_nested(f) -# define wd_update_expire(expired) -#endif +#define wd_in_callback() (g_wdtimernested) +#define wd_set_nested(f) (g_wdtimernested = (f)) +#define wd_update_expire(expired) (g_wdexpired = (expired)) -#ifdef CONFIG_HRTIMER static inline_function void wd_timer_start(clock_t tick, bool in_expiration) { if (!in_expiration) { - clock_t interval = clock_compare(g_wdexpired, tick) ? - tick - g_wdexpired : 0; - uint64_t nsec = interval <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? - TICK2NSEC(g_wdexpired + interval) : - TICK2NSEC(g_wdexpired) + HRTIMER_MAX_DELAY; + clock_t delay = clock_compare(g_wdexpired, tick) ? + tick - g_wdexpired : 0; + uint64_t nsec = delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? + TICK2NSEC(g_wdexpired + delay) : + TICK2NSEC((uint64_t)g_wdexpired) + HRTIMER_MAX_DELAY; hrtimer_start(&g_wdtimer, wd_timer, nsec, HRTIMER_MODE_ABS); } } @@ -144,62 +126,6 @@ static inline_function void wd_timer_cancel(void) { hrtimer_cancel(&g_wdtimer); } -#elif defined(CONFIG_SCHED_TICKLESS) -static inline_function clock_t wd_adjust_next_tick(clock_t tick) -{ - clock_t next_tick = tick; -#ifdef CONFIG_SCHED_TICKLESS_LIMIT_MAX_SLEEP - clock_t interval = clock_compare(g_wdexpired, tick) ? - tick - g_wdexpired : 0u; - interval = interval <= g_oneshot_maxticks ? - interval : g_oneshot_maxticks; - next_tick = g_wdexpired + interval; -#endif - -#if CONFIG_TIMER_ADJUST_NSEC > 0 - /* Normally, timer event cannot triggered on exact time due to the - * existence of interrupt latency. - * Assuming that the interrupt latency is distributed within - * [Best-Case Execution Time, Worst-Case Execution Time], - * we can set the timer adjustment value to the BCET to reduce the latency. - * After the adjustment, the timer interrupt latency will be - * [0, WCET - BCET]. - * Please use this carefully, if the timer adjustment value is not the - * best-case interrupt latency, it will immediately fired another timer - * interrupt, which may result in a much larger timer interrupt latency. - */ - - next_tick -= CONFIG_TIMER_ADJUST_NSEC / NSEC_PER_TICK; -#endif - - return next_tick; -} - -static inline_function void wd_timer_start(clock_t tick, bool in_expiration) -{ - clock_t next_tick = wd_adjust_next_tick(tick); -#ifdef CONFIG_SCHED_TICKLESS_ALARM - up_alarm_tick_start(next_tick); -#else - clock_t now = clock_systime_ticks(); - clock_t delay = clock_compare(now, next_tick) ? next_tick - now : 0; - up_timer_tick_start(delay); -#endif -} - -static inline_function void wd_timer_cancel(void) -{ - struct timespec ts; -#ifdef CONFIG_SCHED_TICKLESS_ALARM - up_alarm_cancel(&ts); -#else - up_timer_cancel(&ts); -#endif -} -#else -# define wd_timer_start(next_tick, in_expiration) -# define wd_timer_cancel() -#endif static inline_function clock_t wd_next_expire(void) { From 2f44b9ffdc023da5c611a2285bb2896f860a4eb0 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 10:03:40 +0800 Subject: [PATCH 04/23] sched/tickless: Convert watchdog to hrtimer in tickless scheduler Replace the wdog that triggers the scheduler event with an hrtimer: - g_sched_event becomes an hrtimer_t. - nxsched_wdog_expiration() becomes nxsched_hrtimer_expiration(), which returns the delay to the next scheduler event so that the hrtimer core re-arms the timer itself. - nxsched_process_event() returns the next delay instead of re-arming the timer, and nxsched_timer_start() arms the hrtimer with hrtimer_start(). An in-expiration flag lets nxsched_reassess_timer() skip the re-arm when the callback has already done it. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- sched/sched/sched_processtickless.c | 61 +++++++++++++++++------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index b2a106c19dd11..7a371c7fe644d 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -93,9 +93,13 @@ static clock_t nxsched_process_scheduler(clock_t ticks, clock_t elapsed, static DEFINE_PER_CPU_BSS_BMP(clock_t, g_timer_tick); #define g_timer_tick this_cpu_var_bmp(g_timer_tick) -/* Wdog timer for scheduler event. */ +/* HRTimer for scheduler event. */ -static struct wdog_s g_sched_event; +static hrtimer_t g_sched_event; + +/* True while the scheduler event hrtimer callback is running. */ + +static bool g_in_sched_expiration; /**************************************************************************** * Private Functions @@ -134,10 +138,9 @@ int up_timer_tick_start(clock_t ticks) } #endif -static void nxsched_process_event(clock_t ticks, bool noswitches) +static clock_t nxsched_process_event(clock_t ticks, bool noswitches) { - clock_t next; - clock_t elapsed; + clock_t elapsed = 0; /* Ensure the g_timer_tick is monotonic. */ @@ -147,21 +150,27 @@ static void nxsched_process_event(clock_t ticks, bool noswitches) elapsed = ticks - g_timer_tick; g_timer_tick = ticks; + } - /* Process the timer ticks and set up the next interval (or not) */ + /* Process the timer ticks and set up the next interval (or not) */ - next = nxsched_process_scheduler(ticks, elapsed, noswitches); - nxsched_timer_start(ticks, next); - } + return nxsched_process_scheduler(ticks, elapsed, noswitches); } -static void nxsched_wdog_expiration(wdparm_t arg) +static uint64_t nxsched_hrtimer_expiration(const hrtimer_t *timer, + uint64_t expired) { - /* Since the g_wdexpired has updated in the nxsched_timer_expiration, - * here we do not need to get tick again. - */ + clock_t delay; + clock_t now = div_const(expired, NSEC_PER_TICK); + irqstate_t flags = enter_critical_section(); - nxsched_process_event(g_wdexpired, false); + g_in_sched_expiration = true; + delay = nxsched_process_event(now, false); + g_in_sched_expiration = false; + + leave_critical_section(flags); + + return delay != CLOCK_MAX ? TICK2NSEC(delay) + 1u : 0u; } /**************************************************************************** @@ -363,7 +372,7 @@ void nxsched_process_timer(void) * * - If the watchdog at the head of the expiration list changes (or if its * delay changes. This can occur as a consequence of the actions of - * wd_start() or wd_cancel(). + * hrtimer_start() or hrtimer_cancel(). * - When pre-emption is re-enabled. A previous time slice may have * expired while pre-emption was enabled and now needs to be executed. * @@ -393,14 +402,16 @@ void nxsched_process_timer(void) void nxsched_reassess_timer(void) { - /* If we are in the wdog callback, there is no need to get tick again. */ + clock_t next; + clock_t now; + + up_timer_gettick(&now); + next = nxsched_process_event(now, true); - if (!wd_in_callback()) + if (!g_in_sched_expiration) { - up_timer_gettick(&g_wdexpired); + nxsched_timer_start(now, next); } - - nxsched_process_event(g_wdexpired, true); } /**************************************************************************** @@ -427,13 +438,15 @@ void nxsched_timer_start(clock_t now, clock_t delay) { if (delay != CLOCK_MAX) { - DEBUGASSERT(delay <= UINT32_MAX); - wd_start_abstick(&g_sched_event, now + delay, - nxsched_wdog_expiration, 0u); + DEBUGASSERT((uint64_t)delay <= UINT32_MAX && + (uint64_t)delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK); + hrtimer_start(&g_sched_event, nxsched_hrtimer_expiration, + TICK2NSEC(now + delay), + HRTIMER_MODE_ABS); } else { - wd_cancel(&g_sched_event); + hrtimer_cancel(&g_sched_event); } } From d0b0ccf0a03a6789ce7c4787c630950f03f8247f Mon Sep 17 00:00:00 2001 From: dengwenqi Date: Tue, 1 Dec 2026 10:19:40 +0800 Subject: [PATCH 05/23] sched/sched: Fix Coverity CID 3933998 (MISRA C-2012 Rule 9.1) In nxsched_process_timer(), the local struct timespec ts is declared without an initializer. up_timer_gettime(&ts) is expected to fill it, but Coverity's MISRA Rule 9.1 check cannot see across the call and flags clock_time2nsec(&ts) on the next line as reading an object with automatic storage duration before it has been set. Zero-initialize ts at declaration so every member has a defined value before any read. up_timer_gettime() still overwrites it as before, so runtime behavior is unchanged. Coverity CID: 3933998 Checker: MISRA C-2012 Rule 9.1 Co-authored-by: ouyangxiangzhen Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: dengwenqi --- sched/sched/sched_processtickless.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index 7a371c7fe644d..b16045e91d05d 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -353,7 +353,10 @@ clock_t nxsched_process_scheduler(clock_t ticks, clock_t elapsed, void nxsched_process_timer(void) { uint64_t nsec; - struct timespec ts; + struct timespec ts = + { + 0 + }; up_timer_gettime(&ts); nsec = clock_time2nsec(&ts); From fe4bf199a2dd96cd03832acce75cc0a39c28296a Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Wed, 8 Apr 2026 20:54:56 +0800 Subject: [PATCH 06/23] sched/wqueue: Add hrtimer support for workqueue Add hrtimer support to workqueue module: - Add hrtimer to kwork_wqueue_s structure - Use hrtimer in work_timer_reset - Add work_timer_expired hrtimer callback Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- include/nuttx/wqueue.h | 1 + sched/wqueue/kwork_queue.c | 2 +- sched/wqueue/kwork_thread.c | 22 +++++----------------- sched/wqueue/wqueue.h | 35 ++++++----------------------------- 4 files changed, 13 insertions(+), 47 deletions(-) diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h index ba2f025f79897..83de174124583 100644 --- a/include/nuttx/wqueue.h +++ b/include/nuttx/wqueue.h @@ -36,6 +36,7 @@ #include #include #include +#include /**************************************************************************** * Pre-processor Definitions diff --git a/sched/wqueue/kwork_queue.c b/sched/wqueue/kwork_queue.c index 2b0e4413a64eb..9faa84e77fc4f 100644 --- a/sched/wqueue/kwork_queue.c +++ b/sched/wqueue/kwork_queue.c @@ -77,7 +77,7 @@ static int work_qqueue(FAR struct kwork_wqueue_s *wqueue, bool retimer; if (wqueue == NULL || work == NULL || worker == NULL || - delay < 0 || delay > WDOG_MAX_DELAY) + (uint64_t)delay > HRTIMER_MAX_DELAY / NSEC_PER_TICK) { return -EINVAL; } diff --git a/sched/wqueue/kwork_thread.c b/sched/wqueue/kwork_thread.c index b5c5f0d49415e..030496f962748 100644 --- a/sched/wqueue/kwork_thread.c +++ b/sched/wqueue/kwork_thread.c @@ -25,6 +25,7 @@ ****************************************************************************/ #include +#include #include #include @@ -488,31 +489,18 @@ void work_queue_dispatch(FAR struct kwork_wqueue_s *wqueue) * ****************************************************************************/ -#ifdef CONFIG_HRTIMER uint64_t work_timer_expired(FAR const hrtimer_t *timer, uint64_t expired) -{ - FAR struct kwork_wqueue_s *wq = - container_of(timer, struct kwork_wqueue_s, timer); - - UNUSED(expired); - - /* Wake up at least one worker thread to dispatch the expired work. */ - - nxsem_post(&wq->sem); - return 0; -} -#else -void work_timer_expired(wdparm_t arg) { /* The work time expired callback will wake up at least one worker thread * to dispatch the expired work. */ - FAR struct kwork_wqueue_s *wq = (FAR struct kwork_wqueue_s *)arg; + FAR struct kwork_wqueue_s *wq = container_of(timer, struct kwork_wqueue_s, + timer); nxsem_post(&wq->sem); + return 0; } -#endif /**************************************************************************** * Name: work_queue_alloc @@ -547,7 +535,7 @@ static FAR struct kwork_wqueue_s *work_queue_alloc(int nthreads) list_initialize(&wqueue->expired); list_initialize(&wqueue->pending); - wqueue->timer.func = NULL; + hrtimer_init(&wqueue->timer); nxsem_init(&wqueue->sem, 0, 0); nxsem_init(&wqueue->exsem, 0, 0); wqueue->nthreads = nthreads; diff --git a/sched/wqueue/wqueue.h b/sched/wqueue/wqueue.h index 6d53f677c3eb3..f0dfead03f539 100644 --- a/sched/wqueue/wqueue.h +++ b/sched/wqueue/wqueue.h @@ -38,10 +38,7 @@ #include #include #include - -#ifdef CONFIG_HRTIMER -# include -#endif +#include #ifdef CONFIG_SCHED_WORKQUEUE @@ -90,11 +87,7 @@ struct kwork_wqueue_s #ifdef CONFIG_SMP atomic_t nposting; /* Wakeups posted after the unlock */ #endif -#ifdef CONFIG_HRTIMER - hrtimer_t timer; /* Timer of the pending queue */ -#else - struct wdog_s timer; /* Timer of the pending queue */ -#endif + struct hrtimer_s timer; /* Timer of the pending queue */ }; /* This structure defines the state of one high-priority work queue. This @@ -296,11 +289,7 @@ bool work_remove(FAR struct kwork_wqueue_s *wqueue, * ****************************************************************************/ -#ifdef CONFIG_HRTIMER uint64_t work_timer_expired(FAR const hrtimer_t *timer, uint64_t expired); -#else -void work_timer_expired(wdparm_t arg); -#endif /**************************************************************************** * Name: work_timer_isactive/work_timer_cancel/work_timer_cancel_sync @@ -311,16 +300,10 @@ void work_timer_expired(wdparm_t arg); * ****************************************************************************/ -#ifdef CONFIG_HRTIMER -# define work_timer_isactive(wqueue) ((wqueue)->timer.func != NULL) +# define work_timer_isactive(wqueue) hrtimer_is_active(&(wqueue)->timer) # define work_timer_cancel(wqueue) hrtimer_cancel(&(wqueue)->timer) # define work_timer_cancel_sync(wqueue) \ hrtimer_cancel_sync(&(wqueue)->timer) -#else -# define work_timer_isactive(wqueue) WDOG_ISACTIVE(&(wqueue)->timer) -# define work_timer_cancel(wqueue) wd_cancel(&(wqueue)->timer) -# define work_timer_cancel_sync(wqueue) wd_cancel(&(wqueue)->timer) -#endif /**************************************************************************** * Name: work_timer_start @@ -329,10 +312,9 @@ void work_timer_expired(wdparm_t arg); * Internal public function to start the timer of the workqueue at the * absolute tick of the first pending work. * - * With CONFIG_HRTIMER the timer is an hrtimer, which has its own lock. - * So the caller can hold wqueue->lock: the order is always wqueue->lock - * first, the hrtimer lock second. wd_start_abstick() enters the - * critical section instead. + * The timer is an hrtimer, which has its own lock. So the caller can + * hold wqueue->lock: the order is always wqueue->lock first, the + * hrtimer lock second. * * Input Parameters: * wqueue - The work queue. @@ -346,13 +328,8 @@ void work_timer_expired(wdparm_t arg); static inline_function void work_timer_start(FAR struct kwork_wqueue_s *wqueue, clock_t qtime) { -#ifdef CONFIG_HRTIMER hrtimer_start(&wqueue->timer, work_timer_expired, TICK2NSEC(qtime), HRTIMER_MODE_ABS); -#else - wd_start_abstick(&wqueue->timer, qtime, - work_timer_expired, (wdparm_t)wqueue); -#endif } /**************************************************************************** From a66e9f7de90d94f4edf9e265df0461a16c19f7b9 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Mon, 27 Apr 2026 20:36:29 +0800 Subject: [PATCH 07/23] sched/tickless: Remove tick-based alarm/timer interfaces Remove the up_alarm_tick_start, up_alarm_tick_cancel, up_timer_tick_start and up_timer_tick_cancel wrappers that merely converted ticks to timespec and delegated to the timespec variants. No in-tree caller uses them after the hrtimer conversion, so they are dead code. Also replace the NULL check in up_timer_getmask with DEBUGASSERT since the caller always ensures the lower-half is registered. This simplifies the arch interface and reduces code memory footprint. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- drivers/timers/arch_alarm.c | 40 ++++------------------------- drivers/timers/arch_timer.c | 26 ------------------- include/nuttx/arch.h | 3 --- sched/sched/sched_processtickless.c | 20 --------------- 4 files changed, 5 insertions(+), 84 deletions(-) diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index 4c4c5e4ed3038..b5f2bd98e0100 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -284,17 +284,14 @@ void up_alarm_set_lowerhalf(FAR struct oneshot_lowerhalf_s *lower) void weak_function up_timer_getmask(FAR clock_t *mask) { - *mask = 0; + clock_t maxticks = 0; - if (g_oneshot_lower != NULL) - { - clock_t maxticks = 0; + DEBUGASSERT(g_oneshot_lower); - ONESHOT_TICK_MAX_DELAY(g_oneshot_lower, &maxticks); + ONESHOT_TICK_MAX_DELAY(g_oneshot_lower, &maxticks); - *mask = maxticks == 0 ? 0 : - UINT64_MAX >> (sizeof(clock_t) * 8u - flsx(maxticks)); - } + *mask = maxticks == 0 ? 0 : + UINT64_MAX >> (sizeof(clock_t) * 8u - flsx(maxticks)); } int weak_function up_timer_gettick(FAR clock_t *ticks) @@ -368,21 +365,6 @@ int weak_function up_alarm_cancel(FAR struct timespec *ts) return ret; } -int weak_function up_alarm_tick_cancel(FAR clock_t *ticks) -{ - int ret = -EAGAIN; - - if (g_oneshot_lower != NULL) - { - ret = ONESHOT_TICK_CANCEL(g_oneshot_lower, ticks); - -#ifndef CONFIG_ONESHOT_COUNT - ONESHOT_TICK_CURRENT(g_oneshot_lower, ticks); -#endif - } - - return ret; -} #endif /**************************************************************************** @@ -421,18 +403,6 @@ int weak_function up_alarm_start(FAR const struct timespec *ts) return ret; } - -int weak_function up_alarm_tick_start(clock_t ticks) -{ - int ret = -EAGAIN; - - if (g_oneshot_lower != NULL) - { - ret = ONESHOT_TICK_ABSOLUTE(g_oneshot_lower, ticks); - } - - return ret; -} #endif /**************************************************************************** diff --git a/drivers/timers/arch_timer.c b/drivers/timers/arch_timer.c index e5c82e5dfecb8..75654c9909150 100644 --- a/drivers/timers/arch_timer.c +++ b/drivers/timers/arch_timer.c @@ -354,19 +354,6 @@ int weak_function up_timer_gettime(struct timespec *ts) ****************************************************************************/ #ifdef CONFIG_SCHED_TICKLESS -int weak_function up_timer_tick_cancel(FAR clock_t *ticks) -{ - int ret = -EAGAIN; - - if (g_timer.lower != NULL) - { - *ticks = update_timeout(g_oneshot_maxticks); - ret = OK; - } - - return ret; -} - int weak_function up_timer_cancel(FAR struct timespec *ts) { int ret = -EAGAIN; @@ -414,19 +401,6 @@ int weak_function up_timer_cancel(FAR struct timespec *ts) ****************************************************************************/ #ifdef CONFIG_SCHED_TICKLESS -int weak_function up_timer_tick_start(clock_t ticks) -{ - int ret = -EAGAIN; - - if (g_timer.lower != NULL) - { - update_timeout(ticks); - ret = OK; - } - - return ret; -} - int weak_function up_timer_start(FAR const struct timespec *ts) { int ret = -EAGAIN; diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index 4924df77f78f1..3440ff929370c 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -2089,7 +2089,6 @@ void up_timer_getmask(FAR clock_t *mask); #if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM) int up_alarm_cancel(FAR struct timespec *ts); -int up_alarm_tick_cancel(FAR clock_t *ticks); #endif /**************************************************************************** @@ -2119,7 +2118,6 @@ int up_alarm_tick_cancel(FAR clock_t *ticks); #if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM) int up_alarm_start(FAR const struct timespec *ts); -int up_alarm_tick_start(clock_t ticks); #endif /**************************************************************************** @@ -2160,7 +2158,6 @@ int up_alarm_tick_start(clock_t ticks); #if defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_SCHED_TICKLESS_ALARM) int up_timer_cancel(FAR struct timespec *ts); -int up_timer_tick_cancel(FAR clock_t *ticks); #endif /**************************************************************************** diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index b16045e91d05d..39a2ecbc52332 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -118,26 +118,6 @@ int up_timer_gettick(FAR clock_t *ticks) } #endif -#if defined(CONFIG_SCHED_TICKLESS_ALARM) && !defined(CONFIG_ALARM_ARCH) -int up_alarm_tick_start(clock_t ticks) -{ - struct timespec ts; - - clock_ticks2time(&ts, ticks); - return up_alarm_start(&ts); -} -#endif - -#if !defined(CONFIG_SCHED_TICKLESS_ALARM) && !defined(CONFIG_TIMER_ARCH) -int up_timer_tick_start(clock_t ticks) -{ - struct timespec ts; - - clock_ticks2time(&ts, ticks); - return up_timer_start(&ts); -} -#endif - static clock_t nxsched_process_event(clock_t ticks, bool noswitches) { clock_t elapsed = 0; From 9b0c3ca8a3996e16cca20ee55b2fbd9c4c0d8b8a Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Mon, 30 Nov 2026 17:01:40 +0800 Subject: [PATCH 08/23] sched/hrtimer: Extract the hrtimer queue into reusable interfaces Replace the hrtimer tree/list/head globals (g_hrtimer_tree, g_hrtimer_list, g_hrtimer_head) with a struct hrtimer_queue_s instance (g_hrtimer_queue), and move the queue operations (hrtimer_queue_initialize/first/insert/remove) into out-of-line functions in hrtimer_queue.c. The queue owns its tree (or sorted list) and, in tree mode, the cached earliest node; the empty queue (NULL head) is a valid state. The out-of-line functions keep the code size down: the same entry points serve both the hrtimer expiry loop and, in the next step, the wdog driver timer. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- include/nuttx/hrtimer.h | 26 +++++ sched/hrtimer/CMakeLists.txt | 10 +- sched/hrtimer/Make.defs | 2 +- sched/hrtimer/hrtimer.h | 122 ++++++--------------- sched/hrtimer/hrtimer_cancel.c | 2 +- sched/hrtimer/hrtimer_initialize.c | 18 ++-- sched/hrtimer/hrtimer_queue.c | 167 +++++++++++++++++++++++++++++ sched/hrtimer/hrtimer_start.c | 4 +- 8 files changed, 243 insertions(+), 108 deletions(-) create mode 100644 sched/hrtimer/hrtimer_queue.c diff --git a/include/nuttx/hrtimer.h b/include/nuttx/hrtimer.h index dddb17eac9198..a314152df7e15 100644 --- a/include/nuttx/hrtimer.h +++ b/include/nuttx/hrtimer.h @@ -97,6 +97,32 @@ typedef struct hrtimer_s uint64_t expired; /* Absolute expiration time (ns) */ } hrtimer_t; +#ifdef CONFIG_HRTIMER_TREE +RB_HEAD(hrtimer_tree_s, hrtimer_s); /* RB-Tree */ +#endif + +/* High-resolution timer queue + * + * Contains all active high-resolution timers sorted by absolute expiration + * time, plus the cached earliest timer for O(1) first access. Each CPU + * owns a private queue instance. + * + * The queue does not embed any lock. Callers of the queue helpers + * (hrtimer_queue_insert, hrtimer_queue_remove and hrtimer_queue_first) + * are responsible for providing their own synchronization (e.g. + * enter/leave_critical_section). + */ + +typedef struct hrtimer_queue_s +{ +#ifdef CONFIG_HRTIMER_TREE + struct hrtimer_tree_s tree; /* RB-tree of active timers */ + hrtimer_t *head; /* Cached earliest expired node */ +#else + struct list_node list; /* Sorted list of active timers */ +#endif +} hrtimer_queue_t; + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/sched/hrtimer/CMakeLists.txt b/sched/hrtimer/CMakeLists.txt index bf8bfde0c70b2..4f7c47c2d67c8 100644 --- a/sched/hrtimer/CMakeLists.txt +++ b/sched/hrtimer/CMakeLists.txt @@ -23,7 +23,13 @@ # Add hrtimer-related files to the build set(CSRCS) -list(APPEND CSRCS hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c - hrtimer_gettime.c) +list( + APPEND + CSRCS + hrtimer_cancel.c + hrtimer_queue.c + hrtimer_initialize.c + hrtimer_start.c + hrtimer_gettime.c) target_sources(sched PRIVATE ${CSRCS}) diff --git a/sched/hrtimer/Make.defs b/sched/hrtimer/Make.defs index 9acdd8485a9e6..6d63344910df4 100644 --- a/sched/hrtimer/Make.defs +++ b/sched/hrtimer/Make.defs @@ -22,7 +22,7 @@ # Add hrtimer-related files to the build -CSRCS += hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c +CSRCS += hrtimer_cancel.c hrtimer_queue.c hrtimer_initialize.c hrtimer_start.c CSRCS += hrtimer_gettime.c # Include hrtimer build support diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index 9bdfdf5983f3a..f8ee04cc5897c 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -100,20 +100,12 @@ DECLARE_PER_CPU_BMP(seqcount_t, g_hrtimer_lock); DECLARE_PER_CPU_BMP(uint64_t, g_hrtimer_last_expired); #define g_hrtimer_last_expired this_cpu_var_bmp(g_hrtimer_last_expired) -#ifdef CONFIG_HRTIMER_TREE -/* Red-Black tree containing all active high-resolution timers */ - -DECLARE_PER_CPU_BMP(struct hrtimer_tree_s, g_hrtimer_tree); -#define g_hrtimer_tree this_cpu_var_bmp(g_hrtimer_tree) - -DECLARE_PER_CPU_BMP(FAR struct hrtimer_s *, g_hrtimer_head); -#define g_hrtimer_head this_cpu_var_bmp(g_hrtimer_head) -#else -/* List containing all active high-resolution timers */ +/* Queue holding all active high-resolution timers sorted by + * absolute expiration time. + */ -DECLARE_PER_CPU_BMP(struct list_node, g_hrtimer_list); -#define g_hrtimer_list this_cpu_var_bmp(g_hrtimer_list) -#endif +DECLARE_PER_CPU_BMP(struct hrtimer_queue_s, g_hrtimer_queue); +#define g_hrtimer_queue this_cpu_var_bmp(g_hrtimer_queue) #ifdef CONFIG_SMP /* Array of pointers to currently running high-resolution timers @@ -123,6 +115,16 @@ DECLARE_PER_CPU_BMP(struct list_node, g_hrtimer_list); DECLARE_PER_CPU_SMP(uintptr_t, g_hrtimer_running); #endif +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +void hrtimer_queue_initialize(FAR struct hrtimer_queue_s *queue); +bool hrtimer_queue_insert(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer); +bool hrtimer_queue_remove(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer); + /**************************************************************************** * Inline Functions ****************************************************************************/ @@ -177,27 +179,6 @@ void hrtimer_unlock(irqstate_t flags) #endif } -/**************************************************************************** - * Name: hrtimer_get_first - * - * Description: - * Return the earliest expiring pending timer. - * - * Returned Value: - * Pointer to the earliest timer, or NULL if none are pending. - * - ****************************************************************************/ - -static inline_function FAR hrtimer_t *hrtimer_get_first(void) -{ -#ifdef CONFIG_HRTIMER_TREE - return g_hrtimer_head; -#else - return predict_false(list_is_empty(&g_hrtimer_list)) ? - NULL : list_first_entry(&g_hrtimer_list, FAR hrtimer_t, node); -#endif -} - /**************************************************************************** * Name: hrtimer_last_expired * @@ -233,12 +214,7 @@ static inline_function uint64_t hrtimer_last_expired(void) static inline_function void hrtimer_initialize(void) { -#ifdef CONFIG_HRTIMER_TREE - RB_INIT(&g_hrtimer_tree); - g_hrtimer_head = NULL; -#else - list_initialize(&g_hrtimer_list); -#endif + hrtimer_queue_initialize(&g_hrtimer_queue); } /**************************************************************************** @@ -255,9 +231,20 @@ static inline_function void hrtimer_initialize(void) * ****************************************************************************/ +static inline_function +FAR hrtimer_t *hrtimer_queue_first(FAR struct hrtimer_queue_s *queue) +{ +#ifdef CONFIG_HRTIMER_TREE + return queue->head; +#else + return predict_false(list_is_empty(&queue->list)) ? + NULL : list_first_entry(&queue->list, FAR hrtimer_t, node); +#endif +} + static inline_function uint64_t hrtimer_next_expired(void) { - FAR hrtimer_t *first = hrtimer_get_first(); + FAR hrtimer_t *first = hrtimer_queue_first(&g_hrtimer_queue); return predict_true(first != NULL) ? first->expired : hrtimer_last_expired() + INT64_MAX; @@ -395,24 +382,6 @@ RB_PROTOTYPE(hrtimer_tree_s, hrtimer_s, node, hrtimer_compare); * ****************************************************************************/ -static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer) -{ -#ifdef CONFIG_HRTIMER_TREE - if (g_hrtimer_head == hrtimer) - { - g_hrtimer_head = RB_NEXT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); - } - - RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); -#else - list_delete_fast(&hrtimer->node); -#endif - - /* Explicitly mark the timer as dequeued. */ - - hrtimer->func = NULL; -} - /**************************************************************************** * Name: hrtimer_insert * @@ -425,34 +394,6 @@ static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer) * ****************************************************************************/ -static inline_function void hrtimer_insert(FAR hrtimer_t *hrtimer) -{ -#ifdef CONFIG_HRTIMER_TREE - RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); - - if (predict_false(g_hrtimer_head == NULL) || - HRTIMER_TIME_BEFORE(hrtimer->expired, g_hrtimer_head->expired)) - { - g_hrtimer_head = hrtimer; - } -#else - FAR hrtimer_t *curr; - uint64_t expired = hrtimer->expired; - - list_for_every_entry(&g_hrtimer_list, curr, hrtimer_t, node) - { - /* Until curr->expired has not timed out relative to expired */ - - if (!HRTIMER_TIME_BEFORE_EQ(curr->expired, expired)) - { - break; - } - } - - list_add_before(&curr->node, &hrtimer->node); -#endif -} - /**************************************************************************** * Name: hrtimer_read_64/32 * @@ -747,7 +688,8 @@ static inline_function void hrtimer_process(uint64_t now) * the queue. */ - while (predict_true((hrtimer = hrtimer_get_first()) != NULL)) + while (predict_true((hrtimer = + hrtimer_queue_first(&g_hrtimer_queue)) != NULL)) { expired = hrtimer->expired; @@ -767,7 +709,7 @@ static inline_function void hrtimer_process(uint64_t now) /* Remove the expired timer from the timer queue */ - hrtimer_remove(hrtimer); + hrtimer_queue_remove(&g_hrtimer_queue, hrtimer); /* Leave critical section before invoking the callback */ @@ -795,7 +737,7 @@ static inline_function void hrtimer_process(uint64_t now) { hrtimer->expired = expired + delay; hrtimer->func = func; - hrtimer_insert(hrtimer); + hrtimer_queue_insert(&g_hrtimer_queue, hrtimer); } hrtimer_unmark_running(cpu); diff --git a/sched/hrtimer/hrtimer_cancel.c b/sched/hrtimer/hrtimer_cancel.c index 0df910eea4c7e..4ee10ef455575 100644 --- a/sched/hrtimer/hrtimer_cancel.c +++ b/sched/hrtimer/hrtimer_cancel.c @@ -104,7 +104,7 @@ int hrtimer_cancel(FAR hrtimer_t *hrtimer) hrtimer->expired = hrtimer_last_expired(); - hrtimer_remove(hrtimer); + hrtimer_queue_remove(&g_hrtimer_queue, hrtimer); /* Reprogram the hardware timer only if the earliest expiration * changed. diff --git a/sched/hrtimer/hrtimer_initialize.c b/sched/hrtimer/hrtimer_initialize.c index d467595a2c858..3c1c8a95c8b72 100644 --- a/sched/hrtimer/hrtimer_initialize.c +++ b/sched/hrtimer/hrtimer_initialize.c @@ -64,19 +64,13 @@ DEFINE_PER_CPU_BMP(seqcount_t, g_hrtimer_lock) = SEQLOCK_INITIALIZER; DEFINE_PER_CPU_BSS_BMP(uint64_t, g_hrtimer_last_expired); #define g_hrtimer_last_expired this_cpu_var_bmp(g_hrtimer_last_expired) -#ifdef CONFIG_HRTIMER_TREE -#undef g_hrtimer_tree -DEFINE_PER_CPU_BSS_BMP(struct hrtimer_tree_s, g_hrtimer_tree); -#define g_hrtimer_tree this_cpu_var_bmp(g_hrtimer_tree) +/* Queue holding all active high-resolution timers sorted by + * absolute expiration time. + */ -#undef g_hrtimer_head -DEFINE_PER_CPU_BSS_BMP(FAR struct hrtimer_s *, g_hrtimer_head); -#define g_hrtimer_head this_cpu_var_bmp(g_hrtimer_head) -#else -#undef g_hrtimer_list -DEFINE_PER_CPU_BSS_BMP(struct list_node, g_hrtimer_list); -#define g_hrtimer_list this_cpu_var_bmp(g_hrtimer_list) -#endif +#undef g_hrtimer_queue +DEFINE_PER_CPU_BSS_BMP(struct hrtimer_queue_s, g_hrtimer_queue); +#define g_hrtimer_queue this_cpu_var_bmp(g_hrtimer_queue) /**************************************************************************** * Public Functions diff --git a/sched/hrtimer/hrtimer_queue.c b/sched/hrtimer/hrtimer_queue.c new file mode 100644 index 0000000000000..e9d14b1122f24 --- /dev/null +++ b/sched/hrtimer/hrtimer_queue.c @@ -0,0 +1,167 @@ +/**************************************************************************** + * sched/hrtimer/hrtimer_queue.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hrtimer/hrtimer.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: hrtimer_queue_initialize + * + * Description: + * Initialize the hrtimer queue. The queue is embedded in the caller's + * data (zero-initialized BSS is also acceptable for tree mode, but + * explicit initialization keeps the sentinel consistent). + * + * Input Parameters: + * queue - Pointer to the hrtimer queue. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void hrtimer_queue_initialize(FAR struct hrtimer_queue_s *queue) +{ +#ifdef CONFIG_HRTIMER_TREE + RB_INIT(&queue->tree); + queue->head = NULL; +#else + list_initialize(&queue->list); +#endif +} + +/**************************************************************************** + * Name: hrtimer_queue_insert + * + * Description: + * Insert a timer into the hrtimer queue according to its expiration + * time. + * + * Input Parameters: + * queue - Pointer to the hrtimer queue. + * hrtimer - Pointer to the timer to insert. + * + * Returned Value: + * true if the timer is added at the head of the queue, false otherwise. + * + * Assumption: + * The caller must hold the queue lock. + * The caller must ensure that the timer is not already in the queue. + * + ****************************************************************************/ + +bool hrtimer_queue_insert(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer) +{ +#ifdef CONFIG_HRTIMER_TREE + bool is_head = false; + + RB_INSERT(hrtimer_tree_s, &queue->tree, hrtimer); + + /* An empty queue has a NULL head. */ + + if (predict_false(queue->head == NULL) || + HRTIMER_TIME_BEFORE(hrtimer->expired, queue->head->expired)) + { + queue->head = hrtimer; + is_head = true; + } + + return is_head; +#else + FAR hrtimer_t *curr; + uint64_t expired = hrtimer->expired; + + list_for_every_entry(&queue->list, curr, hrtimer_t, node) + { + /* Until curr->expired has not timed out relative to expired */ + + if (!HRTIMER_TIME_BEFORE_EQ(curr->expired, expired)) + { + break; + } + } + + list_add_before(&curr->node, &hrtimer->node); + return list_is_head(&queue->list, &hrtimer->node); +#endif +} + +/**************************************************************************** + * Name: hrtimer_queue_remove + * + * Description: + * Remove a timer from the queue and mark it as dequeued. + * + * Input Parameters: + * queue - Pointer to the hrtimer queue. + * hrtimer - Pointer to the timer to remove. + * + * Returned Value: + * true if the timer was the head of the queue, false otherwise. + * + * Assumption: + * The caller must hold the queue lock. + * The caller must ensure that the timer is in the queue. + * + ****************************************************************************/ + +bool hrtimer_queue_remove(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer) +{ +#ifdef CONFIG_HRTIMER_TREE + bool is_head = queue->head == hrtimer; + + if (is_head) + { + queue->head = RB_NEXT(hrtimer_tree_s, &queue->tree, hrtimer); + } + + RB_REMOVE(hrtimer_tree_s, &queue->tree, hrtimer); +#else + bool is_head = list_is_head(&queue->list, &hrtimer->node); + + list_delete_fast(&hrtimer->node); +#endif + + /* Explicitly mark the timer as dequeued. */ + + hrtimer->func = NULL; + + return is_head; +} diff --git a/sched/hrtimer/hrtimer_start.c b/sched/hrtimer/hrtimer_start.c index 0f75e6c6b61a7..89006bef695c7 100644 --- a/sched/hrtimer/hrtimer_start.c +++ b/sched/hrtimer/hrtimer_start.c @@ -79,7 +79,7 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, hrtimer_entry_t func, if (hrtimer_is_pending(hrtimer)) { - hrtimer_remove(hrtimer); + hrtimer_queue_remove(&g_hrtimer_queue, hrtimer); } hrtimer->func = func; @@ -87,7 +87,7 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, hrtimer_entry_t func, /* Insert the timer into the hrtimer queue. */ - hrtimer_insert(hrtimer); + hrtimer_queue_insert(&g_hrtimer_queue, hrtimer); /* Reprogram the hardware timer only if the earliest expiration changed. */ From 6a828e8c8bf3c562880edb83b1eb1eac9420ce35 Mon Sep 17 00:00:00 2001 From: dengwenqi Date: Mon, 18 May 2026 22:07:31 +0800 Subject: [PATCH 09/23] sched/wdog: Fix Coverity CID 3933988 unchecked return value Wrap the hrtimer_start() call in wd_timer_start() with DEBUGVERIFY() so the int return value (OK on success, negated errno on failure) is no longer silently discarded. Coverity flags this as CID 3933988 (Unchecked return value, MISRA C-2012 Rule 17.7). wd_timer_start() is a void inline used from interrupt-disabled watchdog scheduling paths and cannot meaningfully propagate an hrtimer_start() failure to its caller. DEBUGVERIFY() is the canonical NuttX pattern for this case (see sched/init/nx_start.c, sched/timer/timer_settime.c, sched/task/task_recover.c): it asserts that the call did not return a negative errno when CONFIG_DEBUG_ASSERTIONS is enabled, and reduces to ((void)(f)) in release builds, leaving generated code unchanged. is already pulled in transitively via in this header. Coverity CID: 3933988 Type: Unchecked return value (Error handling issues) File: sched/wdog/wdog.h:123 Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: dengwenqi --- sched/wdog/wdog.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h index b2227db088e04..e3c521b6e49d4 100644 --- a/sched/wdog/wdog.h +++ b/sched/wdog/wdog.h @@ -118,7 +118,8 @@ static inline_function void wd_timer_start(clock_t tick, bool in_expiration) uint64_t nsec = delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? TICK2NSEC(g_wdexpired + delay) : TICK2NSEC((uint64_t)g_wdexpired) + HRTIMER_MAX_DELAY; - hrtimer_start(&g_wdtimer, wd_timer, nsec, HRTIMER_MODE_ABS); + DEBUGVERIFY(hrtimer_start(&g_wdtimer, wd_timer, nsec, + HRTIMER_MODE_ABS)); } } From bca1026fb3e5d4b14585d28ffa24bfcba9f3ab6c Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Fri, 21 Aug 2026 14:04:44 +0800 Subject: [PATCH 10/23] sched/wdog: Inline wdog interfaces with NULL checks Move NULL pointer checks from .c files to inline functions in the header, allowing the compiler to optimize away redundant checks: - Rename wd_start_abstick() to wd_start_abstick_internal() and create inline wd_start_abstick() wrapper with NULL checks. - Create inline wd_start() that validates delay range and delegates to wd_start_abstick(). - Rename wd_cancel() to wd_cancel_internal() and create inline wd_cancel() wrapper with NULL check. - Simplify wd_start_next() to use ternary expression. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- include/nuttx/wdog.h | 42 ++++++++++++++++---------- sched/wdog/wd_cancel.c | 68 +++++++++++++++++++----------------------- sched/wdog/wd_start.c | 67 +++++++++++++++-------------------------- sched/wdog/wdog.h | 18 +++++------ 4 files changed, 88 insertions(+), 107 deletions(-) diff --git a/include/nuttx/wdog.h b/include/nuttx/wdog.h index 28a0c237cc7fb..498e4af807959 100644 --- a/include/nuttx/wdog.h +++ b/include/nuttx/wdog.h @@ -133,13 +133,23 @@ extern "C" * indicate the nature of any failure. * * Assumptions: + * The wdog and wdentry should not be NULL. * The watchdog routine runs in the context of the timer interrupt handler * and is subject to all ISR restrictions. * ****************************************************************************/ +int wd_start_abstick_internal(FAR struct wdog_s *wdog, clock_t ticks, + wdentry_t wdentry, wdparm_t arg); + +static inline_function int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, - wdentry_t wdentry, wdparm_t arg); + wdentry_t wdentry, wdparm_t arg) +{ + return wdog && wdentry ? + wd_start_abstick_internal(wdog, ticks, wdentry, arg) : + -EINVAL; +} /**************************************************************************** * Name: wd_start @@ -181,16 +191,11 @@ static inline_function int wd_start(FAR struct wdog_s *wdog, clock_t delay, wdentry_t wdentry, wdparm_t arg) { - int ret = -EINVAL; - /* Ensure delay is within the range the wdog can handle. */ - if (delay >= 0 && delay <= WDOG_MAX_DELAY) - { - ret = wd_start_abstick(wdog, clock_delay2abstick(delay), wdentry, arg); - } - - return ret; + return (uint64_t)delay <= WDOG_MAX_DELAY ? + wd_start_abstick(wdog, clock_delay2abstick(delay), wdentry, arg) : + -EINVAL; } /**************************************************************************** @@ -328,12 +333,9 @@ int wd_start_next(FAR struct wdog_s *wdog, clock_t delay, { /* Ensure delay is within the range the wdog can handle. */ - if (delay <= 0 || delay > WDOG_MAX_DELAY) - { - return -EINVAL; - } - - return wd_start_abstick(wdog, wdog->expired + delay, wdentry, arg); + return (uint64_t)delay <= WDOG_MAX_DELAY ? + wd_start_abstick(wdog, wdog->expired + delay, wdentry, arg) : + -EINVAL; } /**************************************************************************** @@ -350,9 +352,17 @@ int wd_start_next(FAR struct wdog_s *wdog, clock_t delay, * Zero (OK) is returned on success; A negated errno value is returned to * indicate the nature of any failure. * + * Assumptions: + * The wdog is not NULL. + * ****************************************************************************/ -int wd_cancel(FAR struct wdog_s *wdog); +int wd_cancel_internal(FAR struct wdog_s *wdog); + +static inline_function int wd_cancel(FAR struct wdog_s *wdog) +{ + return wdog ? wd_cancel_internal(wdog) : -EINVAL; +} /**************************************************************************** * Name: wd_gettime diff --git a/sched/wdog/wd_cancel.c b/sched/wdog/wd_cancel.c index 5d4cbd7c339b8..aa17e5070f60c 100644 --- a/sched/wdog/wd_cancel.c +++ b/sched/wdog/wd_cancel.c @@ -43,7 +43,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: wd_cancel + * Name: wd_cancel_internal * * Description: * This function cancels a currently running watchdog timer. Watchdog @@ -56,60 +56,54 @@ * Zero (OK) is returned on success; A negated errno value is returned to * indicate the nature of any failure. * + * Assumptions: + * The wdog and wdentry is not NULL. + * ****************************************************************************/ -int wd_cancel(FAR struct wdog_s *wdog) +int wd_cancel_internal(FAR struct wdog_s *wdog) { FAR struct wdog_s *first; - irqstate_t flags; int ret = -EINVAL; + irqstate_t flags = enter_critical_section(); - if (wdog != NULL) - { - /* Prohibit timer interactions with the timer queue until the - * cancellation is complete - */ - - flags = enter_critical_section(); + /* Make sure that the watchdog is valid and still active. */ - /* Make sure that the watchdog is valid and still active. */ + if (WDOG_ISACTIVE(wdog)) + { + first = list_first_entry(&g_wdactivelist, struct wdog_s, node); - if (WDOG_ISACTIVE(wdog)) - { - first = list_first_entry(&g_wdactivelist, struct wdog_s, node); + /* Now, remove the watchdog from the timer queue */ - /* Now, remove the watchdog from the timer queue */ + list_delete_fast(&wdog->node); - list_delete_fast(&wdog->node); + /* Mark the watchdog inactive */ - /* Mark the watchdog inactive */ + wdog->func = NULL; - wdog->func = NULL; + if (first == wdog && !wd_in_callback()) + { + /* If the watchdog is at the head of the timer queue, then + * we will need to re-adjust the interval timer that will + * generate the next interval event. + */ - if (first == wdog && !wd_in_callback()) + if (!list_is_empty(&g_wdactivelist)) { - /* If the watchdog is at the head of the timer queue, then - * we will need to re-adjust the interval timer that will - * generate the next interval event. - */ - - if (!list_is_empty(&g_wdactivelist)) - { - wd_timer_start(wd_next_expire(), false); - } - else - { - wd_timer_cancel(); - } + wd_timer_start(wd_next_expire()); + } + else + { + wd_timer_cancel(); } - - ret = OK; } - leave_critical_section(flags); - sched_note_wdog_cancel((FAR void *)wdog->func, - (FAR void *)(uintptr_t)wdog->expired); + ret = OK; } + leave_critical_section(flags); + sched_note_wdog_cancel((FAR void *)wdog->func, + (FAR void *)(uintptr_t)wdog->expired); + return ret; } diff --git a/sched/wdog/wd_start.c b/sched/wdog/wd_start.c index f3537dd0f3582..52a851a5b5ba0 100644 --- a/sched/wdog/wd_start.c +++ b/sched/wdog/wd_start.c @@ -154,11 +154,6 @@ static inline_function clock_t wd_expiration(clock_t ticks) wd_set_nested(false); - if (next_ticks != ticks) - { - wd_timer_start(next_ticks, true); - } - leave_critical_section(flags); return next_ticks; @@ -256,7 +251,7 @@ bool wd_insert(FAR struct wdog_s *wdog, clock_t expired, ****************************************************************************/ /**************************************************************************** - * Name: wd_start_abstick + * Name: wd_start_abstick_internal * * Description: * This function adds a watchdog timer to the active timer queue. The @@ -286,59 +281,45 @@ bool wd_insert(FAR struct wdog_s *wdog, clock_t expired, * indicate the nature of any failure. * * Assumptions: + * The wdog and wdentry is not NULL. * The watchdog routine runs in the context of the timer interrupt handler * and is subject to all ISR restrictions. * ****************************************************************************/ -int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, - wdentry_t wdentry, wdparm_t arg) +int wd_start_abstick_internal(FAR struct wdog_s *wdog, clock_t ticks, + wdentry_t wdentry, wdparm_t arg) { - irqstate_t flags; - bool reassess = false; - int ret = -EINVAL; + bool reassess = false; + irqstate_t flags = enter_critical_section(); - /* Verify the wdog and setup parameters */ + DEBUGASSERT(wdog && wdentry); - if (wdog != NULL && wdentry != NULL) + /* We need to reassess timer if the watchdog list head has changed. */ + + if (WDOG_ISACTIVE(wdog)) { - /* NOTE: There is a race condition here... the caller may receive - * the watchdog between the time that wd_start_abstick is called and - * the critical section is established. - */ + reassess |= list_is_head(&g_wdactivelist, &wdog->node); + list_delete_fast(&wdog->node); + } - flags = enter_critical_section(); + reassess |= wd_insert(wdog, ticks, wdentry, arg); - /* We need to reassess timer if the watchdog - * list head has changed. + if (reassess && !wd_in_callback()) + { + /* Resume the interval timer that will generate the next + * interval event. If the timer at the head of the list + * changed, then this will pick that new delay. */ - if (WDOG_ISACTIVE(wdog)) - { - reassess |= list_is_head(&g_wdactivelist, &wdog->node); - list_delete_fast(&wdog->node); - } - - reassess |= wd_insert(wdog, ticks, wdentry, arg); - reassess &= !wd_in_callback(); - - if (reassess) - { - /* Resume the interval timer that will generate the next - * interval event. If the timer at the head of the list - * changed, then this will pick that new delay. - */ + wd_timer_start(wd_next_expire()); + } - wd_timer_start(wd_next_expire(), false); - } + leave_critical_section(flags); - leave_critical_section(flags); - sched_note_wdog_start(wdentry, - (FAR void *)(uintptr_t)ticks); - ret = OK; - } + sched_note_wdog_start(wdentry, (FAR void *)(uintptr_t)ticks); - return ret; + return OK; } /**************************************************************************** diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h index e3c521b6e49d4..e3b77786787a3 100644 --- a/sched/wdog/wdog.h +++ b/sched/wdog/wdog.h @@ -109,18 +109,14 @@ uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired); #define wd_set_nested(f) (g_wdtimernested = (f)) #define wd_update_expire(expired) (g_wdexpired = (expired)) -static inline_function void wd_timer_start(clock_t tick, bool in_expiration) +static inline_function void wd_timer_start(clock_t tick) { - if (!in_expiration) - { - clock_t delay = clock_compare(g_wdexpired, tick) ? - tick - g_wdexpired : 0; - uint64_t nsec = delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? - TICK2NSEC(g_wdexpired + delay) : - TICK2NSEC((uint64_t)g_wdexpired) + HRTIMER_MAX_DELAY; - DEBUGVERIFY(hrtimer_start(&g_wdtimer, wd_timer, nsec, - HRTIMER_MODE_ABS)); - } + clock_t delay = clock_compare(g_wdexpired, tick) ? + tick - g_wdexpired : 0; + uint64_t nsec = (uint64_t)delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? + TICK2NSEC(g_wdexpired + delay) : + TICK2NSEC(g_wdexpired) + HRTIMER_MAX_DELAY; + hrtimer_start(&g_wdtimer, wd_timer, nsec, HRTIMER_MODE_ABS); } static inline_function void wd_timer_cancel(void) From b1f627780373493b18ad1c9534f209dddb316850 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Mon, 30 Nov 2026 17:16:07 +0800 Subject: [PATCH 11/23] sched/wdog: Refactor wdog on top of the hrtimer queue Replace the ad-hoc doubly-linked wdog list (g_wdactivelist) with a struct hrtimer_queue_s instance (g_wdqueue), reusing the hrtimer queue infrastructure. Key changes: - struct wdog_s now embeds struct hrtimer_s timer only; the user callback is stored directly in timer.func and the absolute deadline in timer.expired. WDOG_ISACTIVE checks timer.func. - wd_start_abstick_internal / wd_cancel_internal mutate g_wdqueue via hrtimer_queue_insert/hrtimer_queue_remove and, when the queue head changes, re-arm the driver hrtimer (g_wdtimer) through the hrtimer API (a no-op inside wd_timer() processing, detected by wd_in_callback()). - wd_timer() processes the wdog queue entirely under enter_critical_section(), invoking user callbacks with the critical section held; it returns the delay until the next wdog expiration, or 0 when the queue is empty. The g_wdtimernested flag marks callback processing in progress for wd_in_callback(). - wd_get_next_expire() reports the queue head; wd_gettime() is now an inline built on hrtimer_gettime(), and the standalone wd_gettime.c implementation is removed. - ipfrag uses WDOG_ISACTIVE() instead of peeking at the old func member; the wdog scheduler notes keep their argument swallowing. Note: the absolute-time interfaces (wd_start_abstick_internal and the deadline arithmetic built on clock_t) produce wrong deadlines once a 32-bit clock_t wraps. The design assumes clock_t never overflows. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- include/nuttx/wdog.h | 45 +++---- net/ipfrag/ipfrag.c | 2 +- sched/wdog/CMakeLists.txt | 3 +- sched/wdog/Make.defs | 2 +- sched/wdog/wd_cancel.c | 40 +++--- sched/wdog/wd_gettime.c | 77 ----------- sched/wdog/wd_initialize.c | 24 ++-- sched/wdog/wd_start.c | 259 ++++++++++++------------------------- sched/wdog/wdog.h | 62 ++++----- 9 files changed, 165 insertions(+), 349 deletions(-) delete mode 100644 sched/wdog/wd_gettime.c diff --git a/include/nuttx/wdog.h b/include/nuttx/wdog.h index 498e4af807959..8cc3b36b92219 100644 --- a/include/nuttx/wdog.h +++ b/include/nuttx/wdog.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -40,21 +41,15 @@ * Pre-processor Definitions ****************************************************************************/ -#define WDOG_ISACTIVE(w) ((w)->func != NULL) - -/* The maximum delay tick are supposed to be CLOCK_MAX >> 1. - * However, if there are expired wdog timers in the wdog queue, - * clock_compare might be incorrect when the delay is CLOCK_MAX >> 1. - * e.g. Current tick is 123, and there is an expired wdog timer with the - * expired ticks 100. If we insert a wdog timer with delay CLOCK_MAX >> 1, - * Then clock_compare(100, 123 + CLOCK_MAX >> 1) will return false, leading - * to the new wdog timer queued before the expired wdog timer. - * So we limited the delay to CLOCK_MAX >> 2, which is 2^30 - 1 or 2^62 - 1. - * Assuming all expired wdog timer can be processed within WDOG_MAX_DELAY - * ticks, this ensure the correct enqueue of the wdog timer. +#define WDOG_ISACTIVE(w) ((w)->timer.func != NULL) + +/* The wdog delay is limited to the maximum time span the hrtimer can + * represent: HRTIMER_MAX_DELAY (2^62 nanoseconds) converted to ticks. + * Any request beyond that is rejected with -EINVAL at wd_start() / + * wd_start_next(). */ -#define WDOG_MAX_DELAY (CLOCK_MAX >> 2) +#define WDOG_MAX_DELAY (HRTIMER_MAX_DELAY / NSEC_PER_TICK) /**************************************************************************** * Public Type Declarations @@ -84,13 +79,14 @@ typedef CODE void (*wdentry_t)(wdparm_t arg); struct wdog_s { - struct list_node node; /* Supports a doubly linked list */ + struct hrtimer_s timer; /* Embedded high-resolution timer; func stores + * the user callback (wdentry_t) cast to + * hrtimer_entry_t; expired stores the absolute + * expiration time in nanoseconds */ wdparm_t arg; /* Callback argument */ - wdentry_t func; /* Function to execute when delay expires */ #ifdef CONFIG_PIC FAR void *picbase; /* PIC base address */ #endif - clock_t expired; /* Timer associated with the absolute time */ }; /**************************************************************************** @@ -139,7 +135,7 @@ extern "C" * ****************************************************************************/ -int wd_start_abstick_internal(FAR struct wdog_s *wdog, clock_t ticks, +int wd_start_abstick_internal(FAR struct wdog_s *wdog, uint64_t nsec, wdentry_t wdentry, wdparm_t arg); static inline_function @@ -147,7 +143,8 @@ int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, wdentry_t wdentry, wdparm_t arg) { return wdog && wdentry ? - wd_start_abstick_internal(wdog, ticks, wdentry, arg) : + wd_start_abstick_internal(wdog, TICK2NSEC((uint64_t)ticks), + wdentry, arg) : -EINVAL; } @@ -333,9 +330,10 @@ int wd_start_next(FAR struct wdog_s *wdog, clock_t delay, { /* Ensure delay is within the range the wdog can handle. */ - return (uint64_t)delay <= WDOG_MAX_DELAY ? - wd_start_abstick(wdog, wdog->expired + delay, wdentry, arg) : - -EINVAL; + return delay <= WDOG_MAX_DELAY && wdog && wdentry ? + wd_start_abstick_internal(wdog, + wdog->timer.expired + TICK2NSEC((uint64_t)delay), + wdentry, arg) : -EINVAL; } /**************************************************************************** @@ -381,7 +379,10 @@ static inline_function int wd_cancel(FAR struct wdog_s *wdog) * ****************************************************************************/ -clock_t wd_gettime(FAR struct wdog_s *wdog); +static inline_function clock_t wd_gettime(FAR struct wdog_s *wdog) +{ + return wdog ? NSEC2TICK(hrtimer_gettime(&wdog->timer)) : 0; +} #ifdef __cplusplus } diff --git a/net/ipfrag/ipfrag.c b/net/ipfrag/ipfrag.c index 54c132de2c377..4158eaee349c3 100644 --- a/net/ipfrag/ipfrag.c +++ b/net/ipfrag/ipfrag.c @@ -1074,7 +1074,7 @@ int32_t ip_fragout_slice(FAR struct iob_s *iob, uint8_t domain, uint16_t mtu, void ip_frag_startwdog(void) { - if (g_wdfragtimeout.func == NULL) + if (!WDOG_ISACTIVE(&g_wdfragtimeout)) { wd_start(&g_wdfragtimeout, REASSEMBLY_TIMEOUT_TICKS, ip_fragin_timerout_expiry, (wdparm_t)NULL); diff --git a/sched/wdog/CMakeLists.txt b/sched/wdog/CMakeLists.txt index e38a78e368ed0..c2cc5b8457a45 100644 --- a/sched/wdog/CMakeLists.txt +++ b/sched/wdog/CMakeLists.txt @@ -20,5 +20,4 @@ # # ############################################################################## -target_sources(sched PRIVATE wd_initialize.c wd_start.c wd_cancel.c - wd_gettime.c) +target_sources(sched PRIVATE wd_initialize.c wd_start.c wd_cancel.c) diff --git a/sched/wdog/Make.defs b/sched/wdog/Make.defs index e3fbbe4004a4e..118bbccf55c91 100644 --- a/sched/wdog/Make.defs +++ b/sched/wdog/Make.defs @@ -20,7 +20,7 @@ # ############################################################################ -CSRCS += wd_initialize.c wd_start.c wd_cancel.c wd_gettime.c +CSRCS += wd_initialize.c wd_start.c wd_cancel.c # Include wdog build support diff --git a/sched/wdog/wd_cancel.c b/sched/wdog/wd_cancel.c index aa17e5070f60c..f25fab5c3bfcc 100644 --- a/sched/wdog/wd_cancel.c +++ b/sched/wdog/wd_cancel.c @@ -63,38 +63,33 @@ int wd_cancel_internal(FAR struct wdog_s *wdog) { - FAR struct wdog_s *first; - int ret = -EINVAL; - irqstate_t flags = enter_critical_section(); + int ret = -EINVAL; + irqstate_t flags = enter_critical_section(); /* Make sure that the watchdog is valid and still active. */ - if (WDOG_ISACTIVE(wdog)) + if (hrtimer_is_pending(&wdog->timer)) { - first = list_first_entry(&g_wdactivelist, struct wdog_s, node); + /* hrtimer_queue_remove already set timer.func to NULL, releasing + * the queue membership (hrtimer_is_pending checks timer.func). + */ - /* Now, remove the watchdog from the timer queue */ - - list_delete_fast(&wdog->node); - - /* Mark the watchdog inactive */ - - wdog->func = NULL; - - if (first == wdog && !wd_in_callback()) + if (hrtimer_queue_remove(&g_wdqueue, &wdog->timer) && + !wd_in_callback()) { - /* If the watchdog is at the head of the timer queue, then - * we will need to re-adjust the interval timer that will - * generate the next interval event. + /* If the watchdog was at the head of the queue, re-arm or + * cancel the driver hrtimer as appropriate. */ - if (!list_is_empty(&g_wdactivelist)) + FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); + + if (first) { - wd_timer_start(wd_next_expire()); + hrtimer_start_absolute(&g_wdtimer, wd_timer, first->expired); } else { - wd_timer_cancel(); + hrtimer_cancel(&g_wdtimer); } } @@ -102,8 +97,9 @@ int wd_cancel_internal(FAR struct wdog_s *wdog) } leave_critical_section(flags); - sched_note_wdog_cancel((FAR void *)wdog->func, - (FAR void *)(uintptr_t)wdog->expired); + + sched_note_wdog_cancel((FAR void *)wdog->timer.func, + (FAR void *)(uintptr_t)wdog->timer.expired); return ret; } diff --git a/sched/wdog/wd_gettime.c b/sched/wdog/wd_gettime.c deleted file mode 100644 index 8fe8e49a59148..0000000000000 --- a/sched/wdog/wd_gettime.c +++ /dev/null @@ -1,77 +0,0 @@ -/**************************************************************************** - * sched/wdog/wd_gettime.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include - -#include "wdog/wdog.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: wd_gettime - * - * Description: - * This function returns the time remaining before the specified watchdog - * timer expires. - * - * Input Parameters: - * wdog - watchdog ID - * - * Returned Value: - * The time in system ticks remaining until the watchdog time expires. - * Zero means either that wdog is not valid or that the wdog has already - * expired. - * - ****************************************************************************/ - -clock_t wd_gettime(FAR struct wdog_s *wdog) -{ - irqstate_t flags; - clock_t expired; - bool is_active; - clock_t delay = 0; - - if (wdog != NULL && WDOG_ISACTIVE(wdog)) - { - flags = enter_critical_section(); - is_active = WDOG_ISACTIVE(wdog); - expired = wdog->expired; - leave_critical_section(flags); - - if (is_active) - { - delay = expired - clock_systime_ticks(); - delay = delay >= 0 ? delay : 0; - } - } - - return delay; -} diff --git a/sched/wdog/wd_initialize.c b/sched/wdog/wd_initialize.c index 6b1ad3e4afc0d..05288039f3729 100644 --- a/sched/wdog/wd_initialize.c +++ b/sched/wdog/wd_initialize.c @@ -34,27 +34,31 @@ * Public Data ****************************************************************************/ -/* The g_wdactivelist data structure is a singly linked list ordered by - * watchdog expiration time. When watchdog timers expire,the functions on - * this linked list are removed and the function is called. +/* HRTimer queue for all active watchdog timers. + * The queue is ordered by absolute expiration time and may be empty. + * All queue mutations are protected by enter_critical_section() to + * guarantee that the timeout processing executes atomically. */ -#undef g_wdactivelist -DEFINE_PER_CPU_BSS_BMP(struct list_node, g_wdactivelist); -#define g_wdactivelist this_cpu_var_bmp(g_wdactivelist) +#undef g_wdqueue +DEFINE_PER_CPU_BSS_BMP(struct hrtimer_queue_s, g_wdqueue); +#define g_wdqueue this_cpu_var_bmp(g_wdqueue) + +/* HRTimer that sits in the default (system) hrtimer queue. + * When it fires, its callback wd_timer() processes all expired watchdog + * timers from g_wdqueue. + */ #undef g_wdtimer DEFINE_PER_CPU_BSS_BMP(struct hrtimer_s, g_wdtimer); #define g_wdtimer this_cpu_var_bmp(g_wdtimer) +/* Flag set while wd_timer() is processing watchdog callbacks. */ + #undef g_wdtimernested DEFINE_PER_CPU_BSS_BMP(bool, g_wdtimernested); #define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) -#undef g_wdexpired -DEFINE_PER_CPU_BSS_BMP(clock_t, g_wdexpired); -#define g_wdexpired this_cpu_var_bmp(g_wdexpired) - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/sched/wdog/wd_start.c b/sched/wdog/wd_start.c index 52a851a5b5ba0..07e939722c426 100644 --- a/sched/wdog/wd_start.c +++ b/sched/wdog/wd_start.c @@ -37,9 +37,11 @@ #include #include +#include #include #include +#include "hrtimer/hrtimer.h" #include "sched/sched.h" #include "wdog/wdog.h" @@ -89,163 +91,6 @@ * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: wd_expiration - * - * Description: - * Check if the timer for the watchdog at the head of list is ready to - * run. If so, remove the watchdog from the list and execute it. - * - * Input Parameters: - * ticks - current time in ticks - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline_function clock_t wd_expiration(clock_t ticks) -{ - FAR struct wdog_s *wdog; - irqstate_t flags; - wdentry_t func; - wdparm_t arg; - clock_t next_ticks = ticks; - - flags = enter_critical_section(); - - wd_update_expire(ticks); - - wd_set_nested(true); - - /* Process the watchdog at the head of the list as well as any - * other watchdogs that became ready to run at this time - */ - - while (!list_is_empty(&g_wdactivelist)) - { - wdog = list_first_entry(&g_wdactivelist, struct wdog_s, node); - - /* Check if watchdog has expired; - * re-evaluate after updating current ticks if needed - */ - - if (!clock_compare(wdog->expired, ticks)) - { - next_ticks = wdog->expired; - break; - } - - /* Remove the watchdog from the head of the list */ - - list_delete_fast(&wdog->node); - - /* Indicate that the watchdog is no longer active. */ - - func = wdog->func; - arg = wdog->arg; - wdog->func = NULL; - - /* Execute the watchdog function */ - - up_setpicbase(wdog->picbase); - CALL_FUNC(func, arg); - } - - wd_set_nested(false); - - leave_critical_section(flags); - - return next_ticks; -} - -/**************************************************************************** - * Name: wd_insert - * - * Description: - * Insert the timer into the global list to ensure that - * the list is sorted in increasing order of expiration absolute time. - * - * Input Parameters: - * wdog - Watchdog ID - * expired - expired absolute time in clock ticks - * wdentry - Function to call on timeout - * arg - Parameter to pass to wdentry - * - * Assumptions: - * wdog and wdentry is not NULL. - * - * Returned Value: - * Whether the head of the watchdog list has changed. - * - ****************************************************************************/ - -static inline_function -bool wd_insert(FAR struct wdog_s *wdog, clock_t expired, - wdentry_t wdentry, wdparm_t arg) -{ - FAR struct wdog_s *curr; - FAR struct wdog_s *head; - - wdog->func = wdentry; - up_getpicbase(&wdog->picbase); - wdog->arg = arg; - wdog->expired = expired; - - /* Empty list: insert directly as the first active watchdog. */ - - if (list_is_empty(&g_wdactivelist)) - { - list_add_head(&g_wdactivelist, &wdog->node); - return true; - } - - /* Traverse the watchdog list */ - - head = list_first_entry(&g_wdactivelist, struct wdog_s, node); - - /* New earliest expiration: insert at the head and reassess timer. */ - - if (!clock_compare(head->expired, expired)) - { - list_add_head(&g_wdactivelist, &wdog->node); - return true; - } - - curr = list_last_entry(&g_wdactivelist, struct wdog_s, node); - - /* New latest expiration: append at the tail without scanning. */ - - if (clock_compare(curr->expired, expired)) - { - list_add_tail(&g_wdactivelist, &wdog->node); - return false; - } - - list_for_every_entry(&g_wdactivelist, curr, struct wdog_s, node) - { - /* Until curr->expired has not timed out relative to expired */ - - if (!clock_compare(curr->expired, expired)) - { - break; - } - } - - /* There are two cases: - * - Traverse to the end, where curr == &g_wdactivelist. - * - Find a curr such that curr->expected has not timed out - * relative to expired. - * In either case 1 or 2, we just insert the wdog before curr. - */ - - list_add_before(&curr->node, &wdog->node); - - /* Return whether the head of the watchdog list has changed. */ - - return head == curr; -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -287,37 +132,51 @@ bool wd_insert(FAR struct wdog_s *wdog, clock_t expired, * ****************************************************************************/ -int wd_start_abstick_internal(FAR struct wdog_s *wdog, clock_t ticks, +int wd_start_abstick_internal(FAR struct wdog_s *wdog, uint64_t nsec, wdentry_t wdentry, wdparm_t arg) { - bool reassess = false; - irqstate_t flags = enter_critical_section(); + bool reassess = false; + irqstate_t flags = enter_critical_section(); DEBUGASSERT(wdog && wdentry); - /* We need to reassess timer if the watchdog list head has changed. */ + /* If the wdog is already active, remove it from the wdog queue first. */ - if (WDOG_ISACTIVE(wdog)) + if (hrtimer_is_pending(&wdog->timer)) { - reassess |= list_is_head(&g_wdactivelist, &wdog->node); - list_delete_fast(&wdog->node); + reassess = hrtimer_queue_remove(&g_wdqueue, &wdog->timer); } - reassess |= wd_insert(wdog, ticks, wdentry, arg); + /* Populate wdog fields. The user callback (wdentry_t) is stored + * directly in timer.func, reusing the hrtimer's func field. This + * is safe because the wdog queue is only processed by wd_timer(), + * which casts it back to wdentry_t before invoking. + */ + + wdog->arg = arg; +#ifdef CONFIG_PIC + up_getpicbase(&wdog->picbase); +#endif + + wdog->timer.func = (hrtimer_entry_t)wdentry; + wdog->timer.expired = nsec; + + reassess |= hrtimer_queue_insert(&g_wdqueue, &wdog->timer); + + /* Re-arm the driver hrtimer if the queue head changed and we are + * not currently inside wd_timer() processing (in which case wd_timer() + * will return the correct next delay itself). + */ if (reassess && !wd_in_callback()) { - /* Resume the interval timer that will generate the next - * interval event. If the timer at the head of the list - * changed, then this will pick that new delay. - */ - - wd_timer_start(wd_next_expire()); + hrtimer_start_absolute(&g_wdtimer, wd_timer, + hrtimer_queue_first(&g_wdqueue)->expired); } leave_critical_section(flags); - sched_note_wdog_start(wdentry, (FAR void *)(uintptr_t)ticks); + sched_note_wdog_start(wdentry, (FAR void *)(uintptr_t)nsec); return OK; } @@ -347,14 +206,56 @@ int wd_start_abstick_internal(FAR struct wdog_s *wdog, clock_t ticks, * ****************************************************************************/ -uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired) +uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired) { - /* Check if there are any active watchdogs to process */ + wdentry_t func; + wdparm_t arg; + FAR struct wdog_s *wdog; + uint64_t next_expired = expired; + irqstate_t flags = enter_critical_section(); + FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); + + /* Mark callback processing in progress on this CPU. wd_start()/ + * wd_cancel() called from the callbacks below check this via + * wd_in_callback() and skip re-arming the driver hrtimer since + * wd_timer() computes and returns the next delay itself. + */ + + g_wdtimernested = true; + + /* Process all watchdog timers that have expired. The queue may be + * empty, in which case hrtimer_queue_first() returns NULL. + */ + + for (; first != NULL; first = hrtimer_queue_first(&g_wdqueue)) + { + if (!HRTIMER_TIME_BEFORE_EQ(first->expired, expired)) + { + next_expired = first->expired; + break; + } + + wdog = container_of(first, struct wdog_s, timer); + func = (wdentry_t)first->func; + arg = wdog->arg; + + hrtimer_queue_remove(&g_wdqueue, first); + +#ifdef CONFIG_PIC + up_setpicbase(wdog->picbase); +#endif - clock_t tick = div_const(expired, NSEC_PER_TICK); - clock_t delay = wd_expiration(tick) - tick; - uint64_t nsec = (uint64_t)delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? - TICK2NSEC(delay) : HRTIMER_MAX_DELAY; + /* Invoke the user callback with critical-sections. */ + + CALL_FUNC(func, arg); + } + + /* Callback processing finished, clear the in-callback marker. */ + + g_wdtimernested = false; + + leave_critical_section(flags); - return nsec; + return next_expired - expired <= HRTIMER_MAX_DELAY ? + next_expired - expired : HRTIMER_MAX_DELAY; } diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h index e3b77786787a3..4894ef5a87d5b 100644 --- a/sched/wdog/wdog.h +++ b/sched/wdog/wdog.h @@ -40,6 +40,8 @@ #include #include +#include "hrtimer/hrtimer.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -53,21 +55,27 @@ extern "C" { #endif -/* The g_wdactivelist data structure is a singly linked list ordered by - * watchdog expiration time. When watchdog timers expire,the functions on - * this linked list are removed and the function is called. +/* HRTimer queue for all active watchdog timers. + * The queue is ordered by absolute expiration time and may be empty. + * All queue mutations are protected by enter_critical_section() to + * guarantee that the timeout processing executes atomically. */ -DECLARE_PER_CPU_BMP(struct list_node, g_wdactivelist); -#define g_wdactivelist this_cpu_var_bmp(g_wdactivelist) +DECLARE_PER_CPU_BMP(struct hrtimer_queue_s, g_wdqueue); +#define g_wdqueue this_cpu_var_bmp(g_wdqueue) + +/* HRTimer that sits in the default (system) hrtimer queue. + * When it fires, its callback wd_timer() processes all expired watchdog + * timers from g_wdqueue. + */ DECLARE_PER_CPU_BMP(struct hrtimer_s, g_wdtimer); #define g_wdtimer this_cpu_var_bmp(g_wdtimer) +/* Flag set while wd_timer() is processing watchdog callbacks. */ + DECLARE_PER_CPU_BMP(bool, g_wdtimernested); #define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) -DECLARE_PER_CPU_BMP(clock_t, g_wdexpired); -#define g_wdexpired this_cpu_var_bmp(g_wdexpired) /**************************************************************************** * Public Function Prototypes @@ -99,35 +107,18 @@ DECLARE_PER_CPU_BMP(clock_t, g_wdexpired); * ****************************************************************************/ -uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired); +uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired); /**************************************************************************** * Inline functions ****************************************************************************/ -#define wd_in_callback() (g_wdtimernested) -#define wd_set_nested(f) (g_wdtimernested = (f)) -#define wd_update_expire(expired) (g_wdexpired = (expired)) - -static inline_function void wd_timer_start(clock_t tick) -{ - clock_t delay = clock_compare(g_wdexpired, tick) ? - tick - g_wdexpired : 0; - uint64_t nsec = (uint64_t)delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? - TICK2NSEC(g_wdexpired + delay) : - TICK2NSEC(g_wdexpired) + HRTIMER_MAX_DELAY; - hrtimer_start(&g_wdtimer, wd_timer, nsec, HRTIMER_MODE_ABS); -} - -static inline_function void wd_timer_cancel(void) -{ - hrtimer_cancel(&g_wdtimer); -} +/* wd_timer() sets g_wdtimernested while watchdog callbacks are being + * processed so that wd_start()/wd_cancel() invoked from a callback can + * detect this via wd_in_callback() and skip re-arming the driver hrtimer. + */ -static inline_function clock_t wd_next_expire(void) -{ - return list_first_entry(&g_wdactivelist, struct wdog_s, node)->expired; -} +#define wd_in_callback() (g_wdtimernested) /**************************************************************************** * Public Function Prototypes @@ -135,12 +126,13 @@ static inline_function clock_t wd_next_expire(void) static inline_function clock_t wd_get_next_expire(clock_t curr) { - clock_t next = curr; - irqstate_t flags = enter_critical_section(); + clock_t next = curr; + irqstate_t flags = enter_critical_section(); + FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); - if (!list_is_empty(&g_wdactivelist)) + if (first != NULL) { - next = wd_next_expire(); + next = NSEC2TICK(first->expired); } leave_critical_section(flags); @@ -157,7 +149,7 @@ static inline_function clock_t wd_get_next_expire(clock_t curr) static inline_function void wdlist_initialize(void) { - list_initialize(&g_wdactivelist); + hrtimer_queue_initialize(&g_wdqueue); } #ifdef __cplusplus From 6767550cc09881fdff90c2993079fbb92db28853 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Mon, 30 Nov 2026 17:22:09 +0800 Subject: [PATCH 12/23] sched/wdog: Drop g_wdtimernested, use the hrtimer running slot The running slot (g_hrtimer_running) is now maintained on all builds: the array and hrtimer_mark_running()/hrtimer_unmark_running() no longer carry the CONFIG_SMP conditionals, so uniprocessor builds track the slot too at negligible cost. Add hrtimer_is_referenced() to check whether a CPU's running slot holds a timer, in either the running or the cancel-marked form. The cancel-marked form only exists on SMP, where hrtimer_cancel_running() sets it, so the non-SMP variant compares the slot with the timer pointer directly. hrtimer_is_running() keeps its CONFIG_SMP guard: outside SMP the slot always holds the callback's timer while the callback runs, so the check is a compile-time true. Derive wd_in_callback() from hrtimer_is_referenced(): while wd_timer() runs the slot holds g_wdtimer on this CPU, so wd_start()/wd_cancel() called from a watchdog callback detect the in-progress processing and skip re-arming the driver hrtimer. This removes the g_wdtimernested flag and its set/clear in wd_timer(). Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- sched/hrtimer/hrtimer.h | 54 ++++++++++++++++++++++++------ sched/hrtimer/hrtimer_initialize.c | 6 +++- sched/wdog/wd_initialize.c | 6 ---- sched/wdog/wd_start.c | 14 +++----- sched/wdog/wdog.h | 15 ++++----- 5 files changed, 58 insertions(+), 37 deletions(-) diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index f8ee04cc5897c..57f8004536d60 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -107,12 +107,16 @@ DECLARE_PER_CPU_BMP(uint64_t, g_hrtimer_last_expired); DECLARE_PER_CPU_BMP(struct hrtimer_queue_s, g_hrtimer_queue); #define g_hrtimer_queue this_cpu_var_bmp(g_hrtimer_queue) -#ifdef CONFIG_SMP /* Array of pointers to currently running high-resolution timers * for each CPU in SMP configurations. Index corresponds to CPU ID. */ +#ifdef CONFIG_SMP DECLARE_PER_CPU_SMP(uintptr_t, g_hrtimer_running); +# define hrtimer_running_slot(cpu) per_cpu_var_smp(g_hrtimer_running, cpu) +#else +DECLARE_PER_CPU_BMP(uintptr_t, g_hrtimer_running); +# define hrtimer_running_slot(cpu) this_cpu_var_bmp(g_hrtimer_running) #endif /**************************************************************************** @@ -477,12 +481,8 @@ DEFINE_HRTIMER_READ(32, uint32_t) * ****************************************************************************/ -#ifdef CONFIG_SMP -# define hrtimer_mark_running(timer, cpu) \ - (per_cpu_var_smp(g_hrtimer_running, cpu) = (uintptr_t)(timer)) -#else -# define hrtimer_mark_running(timer, cpu) UNUSED(cpu) -#endif +#define hrtimer_mark_running(timer, cpu) \ + (hrtimer_running_slot(cpu) = (uintptr_t)(timer)) /**************************************************************************** * Name: hrtimer_unmark_running @@ -528,13 +528,45 @@ void hrtimer_unmark_running(int cpu) #ifdef CONFIG_SMP # define hrtimer_is_running(timer, cpu) \ - (hrtimer_read_ptr(&per_cpu_var_smp(g_hrtimer_running, cpu)) == (uintptr_t)(timer)) + (hrtimer_read_ptr(&hrtimer_running_slot(cpu)) == (uintptr_t)(timer)) #else # define hrtimer_is_running(timer, cpu) (true) #endif #define hrtimer_is_cancelling(timer, cpu) \ hrtimer_is_running((uintptr_t)(timer) | 0x1u, cpu) +/**************************************************************************** + * Name: hrtimer_is_referenced + * + * Description: + * Check if the CPU core holds the timer in its running slot, either as + * the running timer or in the cancel-marked form (slot = timer | 1). + * + * Input Parameters: + * timer - The timer to be checked. + * cpu - The CPU core Id. + * + * Returned Value: + * true if the timer is referenced by the CPU core, false otherwise. + * + * Assumption: + * This function can only be called from the critical section. + * + ****************************************************************************/ + +#ifdef CONFIG_SMP +#define hrtimer_is_referenced(timer, cpu) \ + ((hrtimer_read_ptr(&hrtimer_running_slot(cpu)) ^ (uintptr_t)(timer)) <= 0x1u) +#else +/* The cancel-marked form of the running slot is only produced by + * hrtimer_cancel_running(), which is compiled for CONFIG_SMP. On a + * uniprocessor the slot holds the running timer pointer or NULL. + */ + +#define hrtimer_is_referenced(timer, cpu) \ + (hrtimer_read_ptr(&hrtimer_running_slot(cpu)) == (uintptr_t)(timer)) +#endif + /**************************************************************************** * Name: hrtimer_cancel_running * @@ -572,19 +604,19 @@ int hrtimer_cancel_running(FAR hrtimer_t *timer) for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++) { /* This is a faster implementation equivalent to - * (per_cpu_var_smp(g_hrtimer_running, cpu) & + * (hrtimer_running_slot(cpu) & * (~(uintptr_t)0x1u)) == timer, * Assuming the pointer of the timer is at least 2-bytes aligned * (the last bit must be zero). */ - if ((per_cpu_var_smp(g_hrtimer_running, cpu) ^ cancelled) <= 0x1u) + if ((hrtimer_running_slot(cpu) ^ cancelled) <= 0x1u) { /* Set the timer to the cancelled state and revoke the write * ownership of the timer from the queue. */ - per_cpu_var_smp(g_hrtimer_running, cpu) = cancelled; + hrtimer_running_slot(cpu) = cancelled; refs++; } } diff --git a/sched/hrtimer/hrtimer_initialize.c b/sched/hrtimer/hrtimer_initialize.c index 3c1c8a95c8b72..a87ba6251afcf 100644 --- a/sched/hrtimer/hrtimer_initialize.c +++ b/sched/hrtimer/hrtimer_initialize.c @@ -32,13 +32,17 @@ * Public Data ****************************************************************************/ -#ifdef CONFIG_SMP /* Array of pointers to currently running high-resolution timers * for each CPU in SMP configurations. Index corresponds to CPU ID. */ +#ifdef CONFIG_SMP DEFINE_PER_CPU_BSS_SMP(uintptr_t, g_hrtimer_running); +#else +DEFINE_PER_CPU_BSS_BMP(uintptr_t, g_hrtimer_running); +#endif +#ifdef CONFIG_SMP /* Global spinlock protecting the high-resolution timer subsystem. * * This spinlock serializes access to the hrtimer queue and diff --git a/sched/wdog/wd_initialize.c b/sched/wdog/wd_initialize.c index 05288039f3729..7ff1529157ed3 100644 --- a/sched/wdog/wd_initialize.c +++ b/sched/wdog/wd_initialize.c @@ -53,12 +53,6 @@ DEFINE_PER_CPU_BSS_BMP(struct hrtimer_queue_s, g_wdqueue); DEFINE_PER_CPU_BSS_BMP(struct hrtimer_s, g_wdtimer); #define g_wdtimer this_cpu_var_bmp(g_wdtimer) -/* Flag set while wd_timer() is processing watchdog callbacks. */ - -#undef g_wdtimernested -DEFINE_PER_CPU_BSS_BMP(bool, g_wdtimernested); -#define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/sched/wdog/wd_start.c b/sched/wdog/wd_start.c index 07e939722c426..ed8504d8eec65 100644 --- a/sched/wdog/wd_start.c +++ b/sched/wdog/wd_start.c @@ -215,14 +215,12 @@ uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired) irqstate_t flags = enter_critical_section(); FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); - /* Mark callback processing in progress on this CPU. wd_start()/ - * wd_cancel() called from the callbacks below check this via - * wd_in_callback() and skip re-arming the driver hrtimer since - * wd_timer() computes and returns the next delay itself. + /* wd_in_callback() below distinguishes the in-progress processing + * through the hrtimer running slot, so wd_start()/wd_cancel() called + * from the callbacks skip re-arming the driver hrtimer: wd_timer() + * computes and returns the next delay itself. */ - g_wdtimernested = true; - /* Process all watchdog timers that have expired. The queue may be * empty, in which case hrtimer_queue_first() returns NULL. */ @@ -250,10 +248,6 @@ uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired) CALL_FUNC(func, arg); } - /* Callback processing finished, clear the in-callback marker. */ - - g_wdtimernested = false; - leave_critical_section(flags); return next_expired - expired <= HRTIMER_MAX_DELAY ? diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h index 4894ef5a87d5b..3ef62325bd92b 100644 --- a/sched/wdog/wdog.h +++ b/sched/wdog/wdog.h @@ -72,11 +72,6 @@ DECLARE_PER_CPU_BMP(struct hrtimer_queue_s, g_wdqueue); DECLARE_PER_CPU_BMP(struct hrtimer_s, g_wdtimer); #define g_wdtimer this_cpu_var_bmp(g_wdtimer) -/* Flag set while wd_timer() is processing watchdog callbacks. */ - -DECLARE_PER_CPU_BMP(bool, g_wdtimernested); -#define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) - /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -113,12 +108,14 @@ uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired); * Inline functions ****************************************************************************/ -/* wd_timer() sets g_wdtimernested while watchdog callbacks are being - * processed so that wd_start()/wd_cancel() invoked from a callback can - * detect this via wd_in_callback() and skip re-arming the driver hrtimer. +/* While wd_timer() runs, the hrtimer core keeps this CPU's running slot + * (g_hrtimer_running) set to g_wdtimer, so wd_start()/wd_cancel() + * invoked from a watchdog callback can detect the in-progress processing + * via wd_in_callback() and skip re-arming the driver hrtimer. The + * XOR-compare also matches the cancel-marked form (slot = timer | 1). */ -#define wd_in_callback() (g_wdtimernested) +#define wd_in_callback() hrtimer_is_referenced(&g_wdtimer, this_cpu()) /**************************************************************************** * Public Function Prototypes From d98e92d7d8384d5354cccc2d2a7b8cf1d230362e Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 10:16:14 +0800 Subject: [PATCH 13/23] sched/tickless: Use the hrtimer running slot in the tickless scheduler Replace the g_in_sched_expiration flag with hrtimer_is_running(): the hrtimer core keeps the running slot set to g_sched_event while the expiration callback runs, so nxsched_reassess_timer() can detect that the re-arm is already handled without an extra flag. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- sched/sched/sched_processtickless.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index 39a2ecbc52332..1aa23476bbb12 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -99,7 +99,8 @@ static hrtimer_t g_sched_event; /* True while the scheduler event hrtimer callback is running. */ -static bool g_in_sched_expiration; +#define sched_in_expiration() \ + hrtimer_is_referenced(&g_sched_event, this_cpu()) /**************************************************************************** * Private Functions @@ -144,9 +145,7 @@ static uint64_t nxsched_hrtimer_expiration(const hrtimer_t *timer, clock_t now = div_const(expired, NSEC_PER_TICK); irqstate_t flags = enter_critical_section(); - g_in_sched_expiration = true; delay = nxsched_process_event(now, false); - g_in_sched_expiration = false; leave_critical_section(flags); @@ -391,7 +390,7 @@ void nxsched_reassess_timer(void) up_timer_gettick(&now); next = nxsched_process_event(now, true); - if (!g_in_sched_expiration) + if (!sched_in_expiration()) { nxsched_timer_start(now, next); } From e2486a3ad804ac7595c45d5671dc0813d509ffd7 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Mon, 30 Nov 2026 20:20:57 +0800 Subject: [PATCH 14/23] sched/wdog: read the cancel note fields under the critical section wd_cancel_internal() saves the callback and the deadline into local variables with the critical section held, so the scheduler notes emitted after leave_critical_section() can still report the cancelled wdog's func/expired: hrtimer_queue_remove() clears timer.func while the queue lock is held, and reading it after the unlock would observe the cleared value. The empty sched_note_wdog_cancel() form now swallows its arguments so the call site does not trip over unused-parameter warnings when the scheduler note instrumentation is compiled out. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- include/nuttx/sched_note.h | 2 +- sched/wdog/wd_cancel.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/nuttx/sched_note.h b/include/nuttx/sched_note.h index 65d2a65f7dadd..01b4505f95813 100644 --- a/include/nuttx/sched_note.h +++ b/include/nuttx/sched_note.h @@ -701,7 +701,7 @@ void sched_note_wdog_enter(FAR void *handler, FAR const void *arg); void sched_note_wdog_leave(FAR void *handler, FAR const void *arg); #else # define sched_note_wdog_start(h,a) -# define sched_note_wdog_cancel(h,a) +# define sched_note_wdog_cancel(h,a) (void)(h); (void)(a) # define sched_note_wdog_enter(h,a) # define sched_note_wdog_leave(h,a) #endif diff --git a/sched/wdog/wd_cancel.c b/sched/wdog/wd_cancel.c index f25fab5c3bfcc..c8ef8a8734109 100644 --- a/sched/wdog/wd_cancel.c +++ b/sched/wdog/wd_cancel.c @@ -65,6 +65,8 @@ int wd_cancel_internal(FAR struct wdog_s *wdog) { int ret = -EINVAL; irqstate_t flags = enter_critical_section(); + hrtimer_entry_t func = wdog->timer.func; + uint64_t expired = wdog->timer.expired; /* Make sure that the watchdog is valid and still active. */ @@ -98,8 +100,7 @@ int wd_cancel_internal(FAR struct wdog_s *wdog) leave_critical_section(flags); - sched_note_wdog_cancel((FAR void *)wdog->timer.func, - (FAR void *)(uintptr_t)wdog->timer.expired); + sched_note_wdog_cancel((FAR void *)func, (FAR void *)(uintptr_t)expired); return ret; } From 7bdbbadd82a01ced0186906503a13ff099514dae Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Mon, 30 Nov 2026 19:46:14 +0800 Subject: [PATCH 15/23] boards/stm32l476vg-disco/nsh: Reduce PREALLOC_TIMERS to save 120 bytes ksram The preallocated POSIX timer pool occupies ksram even when unused; the nsh config only needs a small number of timers, so reduce the pool from 4 to 2 entries to save 120 bytes of ksram. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig b/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig index 8f07bd33dcf5d..1a854e53c74a4 100644 --- a/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig +++ b/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig @@ -44,7 +44,7 @@ CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_DISABLE_IFUPDOWN=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_READLINE=y -CONFIG_PREALLOC_TIMERS=4 +CONFIG_PREALLOC_TIMERS=2 CONFIG_RAM_SIZE=98304 CONFIG_RAM_START=0x20000000 CONFIG_RAW_BINARY=y From 3d3a1a8b8e208c2163a74d788ea652a2c7ba1030 Mon Sep 17 00:00:00 2001 From: zhangyuan29 Date: Tue, 1 Dec 2026 16:00:42 +0800 Subject: [PATCH 16/23] libc/misc: move tempbuffer pool from .data to .bss The tempbuffer pool struct carried a non-zero free_bitmap initializer, which forced the whole struct -- including the 512-byte buffer array -- into .data and consumed that much flash. Invert the bitmap to used_bitmap (0 = free, its natural zero value) and drop the initializer so the struct lands in .bss instead. On imx93-qsb:rpmsg, which loads .data into a small ITCM, the data load image ends at 0x0fffe00c instead of 0x0fffe204, and .bss grows by the same 512 bytes in DTCM. API and behavior are unchanged. Co-authored-by: ouyangxiangzhen Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: zhangyuan29 --- libs/libc/misc/lib_tempbuffer.c | 34 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/libs/libc/misc/lib_tempbuffer.c b/libs/libc/misc/lib_tempbuffer.c index eaba5776aaac9..bec9af18dac75 100644 --- a/libs/libc/misc/lib_tempbuffer.c +++ b/libs/libc/misc/lib_tempbuffer.c @@ -47,7 +47,7 @@ struct tempbuffer_s { - atomic_t free_bitmap; /* Bitmap of free buffer */ + atomic_t used_bitmap; /* Bitmap of used buffer (0 = free) */ char buffer[CONFIG_LIBC_MAX_TEMPBUFFER][TEMP_MAX_SIZE]; }; @@ -55,10 +55,13 @@ struct tempbuffer_s * Private Data ****************************************************************************/ -static struct tempbuffer_s g_tempbuffer = -{ - (1u << CONFIG_LIBC_MAX_TEMPBUFFER) - 1, -}; +/* Zero-initialized so the whole structure lives in .bss instead of .data. + * A cleared used_bitmap means every slot is free, so no explicit initializer + * is needed. Keeping the 512-byte buffer out of .data saves that much + * flash. + */ + +static struct tempbuffer_s g_tempbuffer; /**************************************************************************** * Private Functions @@ -88,19 +91,24 @@ FAR void *lib_get_tempbuffer(size_t nbytes) { if (nbytes <= TEMP_MAX_SIZE) { + /* A cleared bit means the slot is free, so invert the bitmap + * (masked to the valid slots) and take the lowest free slot. + */ + for (; ; ) { int32_t update; - int32_t free_bitmap = atomic_read(&g_tempbuffer.free_bitmap); - int index = ffsl(free_bitmap) - 1; + int32_t used_bitmap = atomic_read(&g_tempbuffer.used_bitmap); + int index = ffsl(~used_bitmap & + ((1ul << CONFIG_LIBC_MAX_TEMPBUFFER) - 1)) - 1; if (index < 0 || index >= CONFIG_LIBC_MAX_TEMPBUFFER) { break; } - update = free_bitmap & ~(1u << index); - if (atomic_cmpxchg(&g_tempbuffer.free_bitmap, &free_bitmap, + update = used_bitmap | (1u << index); + if (atomic_cmpxchg(&g_tempbuffer.used_bitmap, &used_bitmap, update)) { return g_tempbuffer.buffer[index]; @@ -141,9 +149,11 @@ void lib_put_tempbuffer(FAR void *buffer) if (index >= 0 && index < CONFIG_LIBC_MAX_TEMPBUFFER) { - DEBUGASSERT((atomic_read(&g_tempbuffer.free_bitmap) & - (1u << index)) == 0); - atomic_or_acquire(&g_tempbuffer.free_bitmap, 1u << index); + /* Mark the corresponding bit as free (clear the used bit) */ + + DEBUGASSERT((atomic_read(&g_tempbuffer.used_bitmap) & + (1u << index)) != 0); + atomic_and_acquire(&g_tempbuffer.used_bitmap, ~(1u << index)); return; } From 065612cc4d28a68df1b49ba32b725cecae56bb84 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 16:08:58 +0800 Subject: [PATCH 17/23] boards/imx93-qsb/rpmsg: disable the libc temp buffer The applications built for this configuration do not need the global libc temporary buffer; use stack variables instead. The pool now lives in .bss, so this saves 512 bytes of DTCM. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig b/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig index 85d9ab2b531a8..1b8c7c2ff78e0 100644 --- a/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig +++ b/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig @@ -8,6 +8,7 @@ # CONFIG_ARCH_LEDS is not set # CONFIG_ARCH_RAMFUNCS is not set # CONFIG_DEBUG_FUNCTIONNAME is not set +# CONFIG_LIBC_TEMPBUFFER is not set # CONFIG_NSH_CMDOPT_HEXDUMP is not set # CONFIG_RPMSG_PROCFS is not set CONFIG_ARCH="arm" From e0cd03db2aec7b3939a4e339e5a48f10f9f3c8ca Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 16:11:23 +0800 Subject: [PATCH 18/23] boards/lx_cpu/nsh: disable the libc temp buffer The applications built for this configuration do not need the global libc temporary buffer; use stack variables instead. Together with the LTO build this keeps the image inside the FLASH region. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig b/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig index 4a00424e79aea..f4d1b0293c2e3 100644 --- a/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig +++ b/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig @@ -6,6 +6,7 @@ # modifications. # # CONFIG_EXAMPLES_ELF_FSMOUNT is not set +# CONFIG_LIBC_TEMPBUFFER is not set # CONFIG_NSH_DISABLE_SHUTDOWN is not set # CONFIG_SPI_EXCHANGE is not set CONFIG_ALLOW_BSD_COMPONENTS=y From 73eb6e0c478239638238df9d0a42a3adc8e0163f Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 19:49:10 +0800 Subject: [PATCH 19/23] boards/lx_cpu/nsh: Enable LTO The nsh image overflows the FLASH region, so enable link-time optimization to shrink the linked image. LTO links the PIC ELF programs (CONFIG_PIC plus the external ELF test programs) through the compiler driver, and lto1 does not receive -fpic on that link: it rejects the explicitly passed -mpic-register=r9 as useless without -fpic, and -Werror turns that into a link failure of bin/errno and friends. The option is redundant anyway -- with -msingle-pic-base on an EABI target GCC already defaults the PIC register to r9. The GCC ARM options manual documents this: "For single PIC base case, the default is 'R9' if target is EABI based or stack-checking is enabled, otherwise the default is 'R10'." https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html Drop it from CELFFLAGS and CXXELFFLAGS; those flags are also handed to the ELF link through LDELFEXEFLAGS. [skip-cmake-parity] Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- arch/arm/src/common/Toolchain.defs | 4 ++-- boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/arm/src/common/Toolchain.defs b/arch/arm/src/common/Toolchain.defs index b4acab756e19d..cbebe416936bc 100644 --- a/arch/arm/src/common/Toolchain.defs +++ b/arch/arm/src/common/Toolchain.defs @@ -737,8 +737,8 @@ ifeq ($(CONFIG_FDPIC),y) LDMODULEFLAGS = -m armelf_linux_fdpiceabi -shared -z now \ -T $(call CONVERT_PATH,$(TOPDIR)/libs/libc/elf/gnu-elf.ld) else - CELFFLAGS += $(PICFLAGS) -mpic-register=r9 - CXXELFFLAGS += $(PICFLAGS) -mpic-register=r9 + CELFFLAGS += $(PICFLAGS) + CXXELFFLAGS += $(PICFLAGS) # Generate an executable elf, need to ignore undefined symbols LDELFFLAGS += $(LDXLINKER) --unresolved-symbols=ignore-in-object-files diff --git a/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig b/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig index f4d1b0293c2e3..8e65676f147f7 100644 --- a/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig +++ b/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig @@ -90,6 +90,7 @@ CONFIG_LPC17_40_SSP1=y CONFIG_LPC17_40_UART0=y CONFIG_LPC17_40_USBDEV=y CONFIG_LPC17_40_USBHOST=y +CONFIG_LTO_FULL=y CONFIG_M25P_SUBSECTOR_ERASE=y CONFIG_MMCSD=y CONFIG_MMCSD_SDIO=y From 4026b0cea5c4c848f34cce28e3cc70872022ee54 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 19:50:45 +0800 Subject: [PATCH 20/23] boards/eoss3/quickfeather: Enable LTO Enable link-time optimization on the nsh configuration so that code growth is optimized across translation units while the image stays inside the small FLASH region. LTO links the PIC ELF programs through the compiler driver. The redundant -mpic-register=r9 that lto1 rejects is already dropped from CELFFLAGS by the preceding lx_cpu change, so the external ELF test programs keep linking with -Werror. r9 is GCC's default PIC register with -msingle-pic-base on an EABI target. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/eoss3/quickfeather/configs/nsh/defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/boards/arm/eoss3/quickfeather/configs/nsh/defconfig b/boards/arm/eoss3/quickfeather/configs/nsh/defconfig index 9466f0efd7a34..07db3b93242c2 100644 --- a/boards/arm/eoss3/quickfeather/configs/nsh/defconfig +++ b/boards/arm/eoss3/quickfeather/configs/nsh/defconfig @@ -22,6 +22,7 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_FS_PROCFS=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INIT_STACKSIZE=4096 +CONFIG_LTO_FULL=y CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_DISABLE_BASENAME=y CONFIG_NSH_DISABLE_CMP=y From f8e66dde5476545b07a7751730b2354f85311993 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Tue, 1 Dec 2026 19:52:37 +0800 Subject: [PATCH 21/23] boards/eoss3/quickfeather: Enable compiler optimization The configuration does not select an optimization level, so the Kconfig choice defaults to DEBUG_NOOPT and the whole image is built without -O. That also makes the LTO build pointless: at -O0 the link shrinks by only a few bytes. Select DEBUG_FULLOPT, which builds with -Os, and keep -g for the debug symbols. Together with LTO this drops .text from 259,924 to 100,960 bytes and .data from 488 to 420 bytes, leaving plenty of room in the 256 KiB FLASH region. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/eoss3/quickfeather/configs/nsh/defconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/boards/arm/eoss3/quickfeather/configs/nsh/defconfig b/boards/arm/eoss3/quickfeather/configs/nsh/defconfig index 07db3b93242c2..224c38ff2272a 100644 --- a/boards/arm/eoss3/quickfeather/configs/nsh/defconfig +++ b/boards/arm/eoss3/quickfeather/configs/nsh/defconfig @@ -15,6 +15,7 @@ CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_LOOPSPERMSEC=61440 CONFIG_BUILTIN=y CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y CONFIG_DEBUG_SYMBOLS=y CONFIG_DISABLE_MQUEUE=y CONFIG_EOSS3_UART=y From 57d89b36de552d1bb632a3d6e17f7f06e56169ad Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Wed, 2 Dec 2026 14:04:49 +0800 Subject: [PATCH 22/23] boards/stm32f3/nucleo-f302r8: Disable stack dump on the cansock config The hrtimer conversion adds a little code and the cansock image no longer fits in the 64 KiB flash: GCC 13 overflows it by 40 bytes (measured 252 bytes with the local toolchain). Dropping the stack dump frees about 1 KiB in this configuration, at the cost of the register dump on a crash. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig | 1 - 1 file changed, 1 deletion(-) diff --git a/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig b/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig index b2680c26b99b3..68e9fc69e8962 100644 --- a/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig +++ b/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig @@ -19,7 +19,6 @@ CONFIG_ARCH_CHIP_STM32=y CONFIG_ARCH_CHIP_STM32F302R8=y CONFIG_ARCH_CHIP_STM32F3=y CONFIG_ARCH_INTERRUPTSTACK=1024 -CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_INITTHREAD_STACKSIZE=1024 CONFIG_BOARD_LOOPSPERMSEC=8499 CONFIG_BUILTIN=y From 8a162ce3c0f25fbe9f4e45a4dacc35740b0d45d2 Mon Sep 17 00:00:00 2001 From: ouyangxiangzhen Date: Wed, 2 Dec 2026 19:34:08 +0800 Subject: [PATCH 23/23] boards/stm32g4/nucleo-g431rb: Disable stack dump on the cansock config The hrtimer conversion adds a little code and the 128 KiB image overflows by 72 bytes with GCC 13. Dropping the stack dump frees about 1 KiB and keeps the configuration inside the flash, at the cost of the register dump on a crash. Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen --- boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig | 1 - 1 file changed, 1 deletion(-) diff --git a/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig b/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig index 26ea0807d5cf5..344c2682c40c6 100644 --- a/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig +++ b/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig @@ -20,7 +20,6 @@ CONFIG_ARCH_CHIP_STM32=y CONFIG_ARCH_CHIP_STM32G431R=y CONFIG_ARCH_CHIP_STM32G4=y CONFIG_ARCH_INTERRUPTSTACK=1024 -CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_LOOPSPERMSEC=8499 CONFIG_BOARD_NUCLEO_G431RB_USE_HSE=y CONFIG_BUILTIN=y