Address of per-cpu variable
-
13-07-2021 - |
Question
I am writing a small linux loadable kernel module for version 2.6.32 running on x86-64 SMP.
My question is: is there a way to obtain the offset of the address of a per-cpu variable declared in the kernel. By offset I mean the offset of the address from the percpu base, which is found in the gs register. Specifically I am trying to find the offset of the current_task variable, which is a pointer to the task-struct of the current task I believe.
I am looking at arch/x86/include/asm/current.h, which has the get_current() function. This function reads the current_task per-cpu variable using the macro percpu_read_stable. As far as I can understand percpu_read_stable basically expands into an asm routine like this:
asm("movq %%gs:%P1, %0"
: "=r" (ret__)
: "m" (per_cpu__current_task))
This is in arch/x86/include/asm/percpu.h. I want to read the offset after gs from my module. If I try to simple do a printk with the per_cpu_current_task variable, the module is killed.
Thank you for your attention!
Solution
Okay, I figured out the offset for this particular symbol. This one is exported by the kernel. Hence there is an entry in /proc/kallsyms
000000000000cbc0 D per_cpu__current_task
So the offset is 0xcbc0 for this particular variable. Of course the offset would vary for other versions.