How to set the affinity of a process from a Linux kernel mode?
Asked Answered
H

1

6

How can the CPU affinity of a process be set in kernel module? In user mode there is a syscall sched_setaffinity, but I am looking for the kernel mode equivalent.

In the Linux kernel code, there is also a function called sched_setaffinity. It's called from the sys_sched_setaffinity function which is called by system_call. From what it seems, this is the function that I want to use. The fact that it has the same name as the system call makes me a bit uneasy, though.

But as we all know, the best thing to do is to just try it. So I did, and my module compiled. However, when I try to load the module, it complains that the name sched_setaffinity is undefined.

Heteronomy answered 20/7, 2009 at 0:28 Comment(1)
Nor sure if it makes any difference, but do you want to set the affinity of an userland process, or of a kthread?Boche
G
3

sched_setaffinity is not exported to modules.

If you modify /usr/src/linux/kernel/sched.c, you can cause sched_setaffinity to be exported to modules.

 long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
 {
...
 }
+EXPORT_SYMBOL_GPL(sched_setaffinity);
Girth answered 20/7, 2009 at 2:28 Comment(1)
That seems to be it. Due to the nature of what I'm working on, it's not feasible to modify the kernel. Still, thanks a lot.Heteronomy

© 2022 - 2024 — McMap. All rights reserved.