I am writing a kernel module that hooks into system call entry and exit code using kernel tracepoints (trace_sys_enter and trace_sys_exit).
I need to pass data from the system call entry probe to the system call exit probe. I cannot simply use a global variable, as it would be shared between all cores. I do not want the variable to become corrupted by other system calls that might be happening simultaenously on other cores or through preemption.
My current idea is to use per-CPU global variables. My reasoning is that, at any time, a core can only execute one user-space program, and -- I am assuming -- between system call entry and system call exit, a process cannot migrate to another CPU. Is this approach fine?
More specifically:
Can a process migrate to another CPU in the midst of a system call? (This would break my solution.)
Do I need to disable preemption between sys_enter and sys_exit? (I am thinking not, since I do not think one sys_enter can preempt another sys_enter of the same process, i.e. I am assuming a process cannot issue another system call before it observed the previous call's exit.)
Thanks!
For some more background, here's an example of what I want to avoid:
Process 1 Process 2 My Kernel Module
syscall 1 enter
set global call_no = 1
syscall 2 enter
set global call_no = 2
syscall 2 exit
log call_no (2) exited
syscall 1 exit
log call_no (2) exited XXX
(Here, process 2 executes its system call concurrently with process 1, and it leads to an erroneous log at the end, since the global call_no was overwritten by process 2. This is what I need to avoid.)