5

as most C programmers know libc gives a non portable functions for thread cpu affinity tuning (pthread_attr_setaffinity_np()). However, what I do not really know is how can this be done when implementing a kernel module. Any answer that mentions or redirects to some real examples would be rather helpful.

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35
user1533288
  • 291
  • 3
  • 6
  • Could you please explain it in more detail: do you want to change affinity of some existing thread or spawn a new thread with a given affinity from your kernel module? What are you trying to accomplish this way? This information may help the experts here to give you answers. – Eugene Aug 07 '13 at 07:07
  • Sorry for the delayed answer. What I want to do is to establish two threads on different cores each, so that I can make some measurements about the cache coherency problem. To make my measurements precise I need to add inlined assembly but I also have to disable preemption. To do so, I have to have a linux kernel module for the task. However, for such a module the libc library is invisible and for this reason I want to see which function changes the affinity of THREAD, no of a PROCESS in a LKM. I hope this is clear now... – user1533288 Aug 08 '13 at 01:21

2 Answers2

3

You should use kthreads, which stands for kernel threads. To create such on specified cpu, you should invoke kthread_create_on_cpu(). It is defined in include/linux/kthread.h. Thread will be created in sleep state, so you should call wake_up_process() on it. That's all.

You can get one example of using kthreads in my answer in this question.

Community
  • 1
  • 1
Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35
1

You can use kthread_bind() function.

Artem Romanov
  • 166
  • 1
  • 14