Search code examples
linuxmultithreadinglinux-kernelschedulingcpuset

Could the affinity of thread created by kernel be set by "cpuset"?


The affinity of user processes could be set by cpuset(7).

Could the affinity of thread created by kernel be set through the cpuset(7)?

I found that the affinity of some kthreads could be set by cpuset indeed([rcu_sched],[rcu_bh]),some kthreads couldn't([nvme-delete-wq],[kthreadd],i got the error: " echo: write error: Invalid argument").

If you have a better solution, please let me know.


Solution

  • cpuset(7) is a manual page that describes a Linux userspace API in general. As the page states, you can use the sched_setaffinity(2) syscall to restrict a task to a specific set of CPUs.

    The fact that sched_setaffinity(2) is a syscall should already make you notice that the functionality is intended for userspace usage. If you are writing kernel code, kernel threads have different internal APIs for this purpose (see kthread.h):

    • kthread_bind(), which can be used to bind the kthread to a single CPU specified by its numerical ID.
    • kthread_bind_mask(), which can be used to bind the kthread to one or more CPUs defined by a struct cpumask. You can initialize the right struct cpumask through cpumask_set_cpu(). This API is similar to the sched_setaffinity(2) syscall, but for kthreads.