summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShell <[email protected]>2024-05-29 06:53:22 +0800
committerGitHub <[email protected]>2024-05-29 06:53:22 +0800
commitf179ce12b71c01a5fba3b79f00e245178f482483 (patch)
treeb70bfae5edffe38d4363f99d89438aaea15d81ad
parent326150e2f4991679f5a4e39cfdc42378a328e983 (diff)
[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 <[email protected]>
-rw-r--r--components/lwp/lwp.h2
-rw-r--r--components/lwp/lwp_pid.c26
-rw-r--r--components/lwp/lwp_syscall.c6
3 files changed, 15 insertions, 19 deletions
diff --git a/components/lwp/lwp.h b/components/lwp/lwp.h
index c504ef60ad..060e9ff34c 100644
--- a/components/lwp/lwp.h
+++ b/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);
diff --git a/components/lwp/lwp_pid.c b/components/lwp/lwp_pid.c
index 63a635d2f5..8b149dd542 100644
--- a/components/lwp/lwp_pid.c
+++ b/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;
}
diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c
index f7fe959eab..27285e34c5 100644
--- a/components/lwp/lwp_syscall.c
+++ b/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);
}
}