Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processor affinity settings for Linux kernel modules?

In Windows, I can set the processor affinity of driver code using KeSetSystemAffinityThread, and check which processor my code is running on using KeGetCurrentProcessorNumber.

I'm trying to do something similar in a Linux kernel module, but the only affinity calls I can see are for userland processes. Is there any way to do this, so that I can run assembly code on a specific processor? (i.e. sgdt)

Edit:

I think I've figured out how to get the current processor. smp_processor_id() seems like it should work.

like image 441
Stephen Avatar asked Sep 01 '26 20:09

Stephen


2 Answers

I think you'll probably have to modify the kernel, but the change isn't too rough. Just export sched_setaffinity in sched.c to modules:

  long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  {
    ...
  }
+ EXPORT_SYMBOL_GPL(sched_setaffinity); // Exported, now callable from your code.
like image 55
John Feminella Avatar answered Sep 03 '26 13:09

John Feminella


smp_processor_id() should tell you what logical processor you're running on.

Some architectures also support the smp_call_function_single kernel function that will use an inter-processor-interrupt to run a function on another processor.

like image 27
Eric Seppanen Avatar answered Sep 03 '26 12:09

Eric Seppanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!