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

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


The following commit(s) were added to refs/heads/master by this push:
     new c12aa5663d sched/affinity: Fix CPU_LOCKED functionality for some SMP 
calls
c12aa5663d is described below

commit c12aa5663d3046138eb975a8bc872d8348c73e21
Author: Ville Juven <ville.ju...@unikie.com>
AuthorDate: Fri Apr 25 11:51:36 2025 +0300

    sched/affinity: Fix CPU_LOCKED functionality for some SMP calls
    
    For some SMP calls it is necessary to lock the current CPU for the process
    receiving the SMP call. This is done by setting the CPU affinity to the
    current CPU and preventing the CPU selection algorithm from switching
    CPUs.
    
      dtcb->flags |= TCB_FLAG_CPU_LOCKED;
      CPU_SET(dtcb->cpu, &dtcb->affinity);
    
    However, this logic is currently broken, as CPU_SET is defined as:
    
      #define CPU_SET(c,s) do { *(s) |= (1u << (c)); } while (0)
    
    In order to assign tcb->cpu (the current CPU) to the affinity mask, the
    mask must be cleared first by calling CPU_ZERO.
---
 sched/sched/sched_backtrace.c   | 1 +
 sched/sched/sched_setpriority.c | 1 +
 sched/sched/sched_suspend.c     | 1 +
 sched/signal/sig_dispatch.c     | 1 +
 sched/task/task_restart.c       | 1 +
 sched/task/task_terminate.c     | 1 +
 6 files changed, 6 insertions(+)

diff --git a/sched/sched/sched_backtrace.c b/sched/sched/sched_backtrace.c
index 85d42f1a0f..1b1e10b7f6 100644
--- a/sched/sched/sched_backtrace.c
+++ b/sched/sched/sched_backtrace.c
@@ -130,6 +130,7 @@ int sched_backtrace(pid_t tid, FAR void **buffer, int size, 
int skip)
                   arg.need_restore = true;
 
                   tcb->flags |= TCB_FLAG_CPU_LOCKED;
+                  CPU_ZERO(&tcb->affinity);
                   CPU_SET(tcb->cpu, &tcb->affinity);
                 }
 
diff --git a/sched/sched/sched_setpriority.c b/sched/sched/sched_setpriority.c
index 112e1dbb59..069ead5e32 100644
--- a/sched/sched/sched_setpriority.c
+++ b/sched/sched/sched_setpriority.c
@@ -240,6 +240,7 @@ static inline void nxsched_running_setpriority(FAR struct 
tcb_s *tcb,
                   arg.need_restore = true;
 
                   tcb->flags |= TCB_FLAG_CPU_LOCKED;
+                  CPU_ZERO(&tcb->affinity);
                   CPU_SET(tcb->cpu, &tcb->affinity);
                 }
 
diff --git a/sched/sched/sched_suspend.c b/sched/sched/sched_suspend.c
index a559fa05e8..2b19d7871f 100644
--- a/sched/sched/sched_suspend.c
+++ b/sched/sched/sched_suspend.c
@@ -161,6 +161,7 @@ void nxsched_suspend(FAR struct tcb_s *tcb)
               arg.need_restore = true;
 
               tcb->flags |= TCB_FLAG_CPU_LOCKED;
+              CPU_ZERO(&tcb->affinity);
               CPU_SET(tcb->cpu, &tcb->affinity);
             }
 
diff --git a/sched/signal/sig_dispatch.c b/sched/signal/sig_dispatch.c
index 8bea2d2618..6a251f9b49 100644
--- a/sched/signal/sig_dispatch.c
+++ b/sched/signal/sig_dispatch.c
@@ -181,6 +181,7 @@ static int nxsig_queue_action(FAR struct tcb_s *stcb, 
siginfo_t *info)
                       arg.need_restore   = true;
 
                       stcb->flags        |= TCB_FLAG_CPU_LOCKED;
+                      CPU_ZERO(&stcb->affinity);
                       CPU_SET(stcb->cpu, &stcb->affinity);
                     }
 
diff --git a/sched/task/task_restart.c b/sched/task/task_restart.c
index 19f99e4291..cb934b200d 100644
--- a/sched/task/task_restart.c
+++ b/sched/task/task_restart.c
@@ -241,6 +241,7 @@ static int nxtask_restart(pid_t pid)
           arg.need_restore = true;
 
           tcb->flags |= TCB_FLAG_CPU_LOCKED;
+          CPU_ZERO(&tcb->affinity);
           CPU_SET(tcb->cpu, &tcb->affinity);
         }
 
diff --git a/sched/task/task_terminate.c b/sched/task/task_terminate.c
index 4e6f334756..05c2810707 100644
--- a/sched/task/task_terminate.c
+++ b/sched/task/task_terminate.c
@@ -144,6 +144,7 @@ int nxtask_terminate(pid_t pid)
       tcb_flags = dtcb->flags;
       dtcb->flags |= TCB_FLAG_CPU_LOCKED;
       affinity = dtcb->affinity;
+      CPU_ZERO(&dtcb->affinity);
       CPU_SET(dtcb->cpu, &dtcb->affinity);
 
       ret = nxsched_smp_call_single(dtcb->cpu, terminat_handler,

Reply via email to