Browse Source

[smart] update sched_setaffinity() to use thread(task) ID (#9004)

Correct `sched_setaffinity()` to use the thread IDs (TIDs) instead of
process IDs (PIDs). The previous implementation used PIDs, which
caused issues since affinity settings need to be applied at the
thread level.

As the manual documented, the signature is:

>        int sched_setaffinity(pid_t pid, size_t cpusetsize,
>                             const cpu_set_t *mask);

Yes, it's tricky, the identification passing in is called **'PID'**.
But when we talk about 'pid' from GNU libc, it's the **'task-id'**,
aka, `thread->tid` known in kernel.

Changes were made by updating the function signatures and logic in
`lwp.h`, `lwp_pid.c`, and `lwp_syscall.c` to accept TIDs. Specifically,
the `lwp_setaffinity` function and related internal functions now
operate using thread IDs and adjust thread affinity settings accordingly

Signed-off-by: Shell <smokewood@qq.com>
Shell 11 months ago
parent
commit
f179ce12b7
3 changed files with 15 additions and 19 deletions
  1. 1 1
      components/lwp/lwp.h
  2. 8 18
      components/lwp/lwp_pid.c
  3. 6 0
      components/lwp/lwp_syscall.c

+ 1 - 1
components/lwp/lwp.h

@@ -249,7 +249,7 @@ void lwp_user_setting_restore(rt_thread_t thread);
 void lwp_uthread_ctx_save(void *ctx);
 void lwp_uthread_ctx_restore(void);
 
-int lwp_setaffinity(pid_t pid, int cpu);
+int lwp_setaffinity(int tid, int cpu);
 
 pid_t exec(char *filename, int debug, int argc, char **argv);
 

+ 8 - 18
components/lwp/lwp_pid.c

@@ -1600,35 +1600,25 @@ static void _resr_cleanup(struct rt_lwp *lwp)
     }
 }
 
-static int _lwp_setaffinity(pid_t pid, int cpu)
+static int _lwp_setaffinity(int tid, int cpu)
 {
-    struct rt_lwp *lwp;
+    rt_thread_t thread;
     int ret = -1;
 
-    lwp_pid_lock_take();
-    lwp = lwp_from_pid_locked(pid);
+    thread = lwp_tid_get_thread_and_inc_ref(tid);
 
-    if (lwp)
+    if (thread)
     {
 #ifdef RT_USING_SMP
-        rt_list_t *list;
-
-        lwp->bind_cpu = cpu;
-        for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
-        {
-            rt_thread_t thread;
-
-            thread = rt_list_entry(list, struct rt_thread, sibling);
-            rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_size_t)cpu);
-        }
+        rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_ubase_t)cpu);
 #endif
         ret = 0;
     }
-    lwp_pid_lock_release();
+    lwp_tid_dec_ref(thread);
     return ret;
 }
 
-int lwp_setaffinity(pid_t pid, int cpu)
+int lwp_setaffinity(int tid, int cpu)
 {
     int ret;
 
@@ -1638,7 +1628,7 @@ int lwp_setaffinity(pid_t pid, int cpu)
         cpu = RT_CPUS_NR;
     }
 #endif
-    ret = _lwp_setaffinity(pid, cpu);
+    ret = _lwp_setaffinity(tid, cpu);
     return ret;
 }
 

+ 6 - 0
components/lwp/lwp_syscall.c

@@ -5426,6 +5426,12 @@ sysret_t sys_sched_setaffinity(pid_t pid, size_t size, void *set)
         if (CPU_ISSET_S(i, size, kset))
         {
             kmem_put(kset);
+
+            /**
+             * yes it's tricky.
+             * But when we talk about 'pid' from GNU libc, it's the 'task-id'
+             * aka 'thread->tid' known in kernel.
+             */
             return lwp_setaffinity(pid, i);
         }
     }