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.
cpuset(7)is a manual page that describes a Linux userspace API in general. As the page states, you can use thesched_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 (seekthread.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 astruct cpumask. You can initialize the rightstruct cpumaskthroughcpumask_set_cpu(). This API is similar to thesched_setaffinity(2)syscall, but for kthreads.