Skip to content

Commit

Permalink
[smart] update sched_setaffinity() to use thread(task) ID (RT-Thread#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
polarvid authored May 28, 2024
1 parent 326150e commit f179ce1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion components/lwp/lwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
26 changes: 8 additions & 18 deletions components/lwp/lwp_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions components/lwp/lwp_syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit f179ce1

Please sign in to comment.