views:

42

answers:

2

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.

+1  A: 

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.
John Feminella
I've seen this before and assumed it was only meant for userland processes, however if I set the pid argument to 0 it actually works.I managed to do it without recompiling the kernel using a function pointer and looking up sched_setaffinity in /boot/System.map, just for testing it out.long (*extern_sched_setaffinity)(pid_t pid, const struct cpumask *in_mask) = (void*)0xffffffff81066a70; on my system.Thanks.
Stephen Pape
This isn't likely to be a good idea, unless you're just setting the affinity of a kernel thread created specifically by your driver. Otherwise, driver code can run in the context of many different processes at different times, each of which has its *own* scheduler affinity. If you just want to execute a short section of code without being bounced to another CPU, you can use `preempt_disable()` and `preempt_enable()` to create a preemption-disabled critical section.
caf
@caf: I assumed that what you described is the case, because he says "my code". Good advice in any case, though.
John Feminella
+1  A: 

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.

Eric Seppanen