This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 144ab6f1b82dc7b4cfe349215a0d94a753578096
Author: yushuailong <[email protected]>
AuthorDate: Sat Sep 12 15:39:57 2026 +0800

    sched/cpuload: Validate PID before critmon update.
    
    Move the critical-monitor update after PID hash entry validation and keep
    the scheduler critical section held so the TCB remains stable. Invalid or
    stale PIDs now return -ESRCH instead of passing a NULL TCB to
    nxsched_update_critmon().
    
    Also add the declaration spacing required by nxstyle in the modified file.
    
    Assisted-by: OpenAI Codex
    Signed-off-by: yushuailong <[email protected]>
---
 sched/sched/sched_cpuload.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/sched/sched/sched_cpuload.c b/sched/sched/sched_cpuload.c
index d3d89c5cd9c..3eb62043f23 100644
--- a/sched/sched/sched_cpuload.c
+++ b/sched/sched/sched_cpuload.c
@@ -113,6 +113,7 @@ volatile clock_t g_cpuload_total;
 static void cpuload_callback(wdparm_t arg)
 {
   FAR struct wdog_s *wdog = (FAR struct wdog_s *)arg;
+
   nxsched_process_cpuload_ticks(CPULOAD_SAMPLING_PERIOD);
   wd_start_next(wdog, CPULOAD_SAMPLING_PERIOD, cpuload_callback, arg);
 }
@@ -196,6 +197,7 @@ void nxsched_process_cpuload_ticks(clock_t ticks)
   for (i = 0; i < CONFIG_SMP_NCPUS; i++)
     {
       FAR struct tcb_s *rtcb = current_task(i);
+
       nxsched_process_taskload_ticks(rtcb, ticks);
     }
 }
@@ -228,12 +230,6 @@ int clock_cpuload(int pid, FAR struct cpuload_s *cpuload)
 
   DEBUGASSERT(cpuload);
 
-#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
-  /* Update critmon in case of the target thread busyloop */
-
-  nxsched_update_critmon(nxsched_get_tcb(pid));
-#endif
-
   /* Momentarily disable interrupts.  We need (1) the task to stay valid
    * while we are doing these operations and (2) the tick counts to be
    * synchronized when read.
@@ -257,6 +253,12 @@ int clock_cpuload(int pid, FAR struct cpuload_s *cpuload)
 
   if (g_pidhash[hash_index] && g_pidhash[hash_index]->pid == pid)
     {
+#ifdef CONFIG_SCHED_CPULOAD_CRITMONITOR
+      /* Update critmon in case of the target thread busyloop */
+
+      nxsched_update_critmon(g_pidhash[hash_index]);
+#endif
+
       cpuload->total  = g_cpuload_total;
       cpuload->active = g_pidhash[hash_index]->ticks;
       ret = OK;

Reply via email to