From user-space we can use cpuset to actually isolate a specific core in our system and execute just one specific process to that core.
I'm trying to do the same thing with a kernel module. So I want the module to get executed in an isolated core. In other words: How do I use cpuset's from inside a kernel module? *
Using linux/cpuset.h in my kernel module doesn't work. So, I have a module like this:
#include <linux/module.h>
#include <linux/cpuset.h>
...
#ifdef CONFIG_CPUSETS
printk(KERN_INFO, "cpusets is enabled!");
#endif
cpuset_init(); // this function is declared in cpuset.h
...
When trying to load this module I get (in dmesg) the following message cpusets is enabled!. But I also receive the message Unknown symbol cpu_init (err 0).
Similarly, I tried using sched_setaffinity from linux/sched.h in order to move all running procceses to a specific core and then run my module to an isolated core. I got the same error mesage: Unknown symbol sched_setaffinity (err 0). I guess I got the "unknown symbols" because those functions have no EXPORT_SYMBOL in the kernel. So I went and tried to call the sys_sched_setaffinity system call (based on this question) but again got this mesage: Unknown symbol sys_sched_setaffinity (err 0)!
Furthermore, I am not looking for a solution that uses isolcpus, which is set while booting. I would like to just load the module and afterwards the isolationo to occur.
- (More precise, I want its kernel threads to execute in isolated cores. I know that I can use affinity to bind threads to specific cores, but this does not guarantee me that the cores are going to be isolated by other processes running on them.)
and
This is a working source code compiled and tested on a Debian box using kernel 3.16. I'll describe how to load and unload first and what the parameter passed means.
All sources can be found on github here...
https://github.com/harryjackson/doc/tree/master/linux/kernel/toy/toy
Build and load the module...
To unload the module use
I'm not using modprobe because it expects some configuration etc. The parameter we're passing to the
toykernel module is the CPU we want to isolate. None of the device operations that get called will run unless they're executing on that CPU.Once the module is loaded you can find it here
Simple operations like
create events that the kernel module catches and produces some output. You can see the output using
dmesg.Source code...
The code above contains the two methods you asked for ie isolation of CPU and on
initrun on an isolated core.On init
get_cpudisables preemption ie anything that comes after it will not be preempted by the kernel and will run on one core. Note, this was done kernel using 3.16, your mileage may vary depending on your kernel version but I think these API's have been around a long timeThis is the Makefile...
Notes.
get_cpuis declared inlinux/smp.hasso you don't actually need to call
preempt_disablebefore callingget_cpu. The get_cpu call is a wrapper around the following sequence of calls...and put_cpu is really doing this...
You can get as fancy as you like using the above. Almost all of this was taken from the following sources..
Google for... smp_call_function_single
Linux Kernel Development, book by Robert Love.
http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/
https://github.com/vsinitsyn/reverse/blob/master/reverse.c