如何在Linux内核中实现percpu指针? [英] How are percpu pointers implemented in the Linux kernel?
问题描述
在多处理器上,每个内核可以有自己的变量.我以为它们是在不同地址中的不同变量,尽管它们的过程相同且名称相同.
On multiprocessor, each core can have its own variables. I thought they are different variables in different addresses, although they are in same process and have the same name.
但是我想知道内核如何实现这一点?它是否分配了一块内存来存放所有的percpu指针,并且每次它通过shift或其他方式将指针重定向到某个地址时?
But I am wondering, how does the kernel implement this? Does it dispense a piece of memory to deposit all the percpu pointers, and every time it redirects the pointer to certain address with shift or something?
推荐答案
常规全局变量不是每个CPU的.自动变量位于堆栈中,并且不同的CPU使用不同的堆栈,因此自然会得到单独的变量.
Normal global variables are not per CPU. Automatic variables are on the stack, and different CPUs use different stack, so naturally they get separate variables.
我猜您指的是Linux的每CPU变量基础结构.
大多数魔法都在这里(asm-generic/percpu.h
):
I guess you're referring to Linux's per-CPU variable infrastructure.
Most of the magic is here (asm-generic/percpu.h
):
extern unsigned long __per_cpu_offset[NR_CPUS];
#define per_cpu_offset(x) (__per_cpu_offset[x])
/* Separate out the type, so (int[3], foo) works. */
#define DEFINE_PER_CPU(type, name) \
__attribute__((__section__(".data.percpu"))) __typeof__(type) per_cpu__##name
/* var is in discarded region: offset to particular copy we want */
#define per_cpu(var, cpu) (*RELOC_HIDE(&per_cpu__##var, __per_cpu_offset[cpu]))
#define __get_cpu_var(var) per_cpu(var, smp_processor_id())
宏RELOC_HIDE(ptr, offset)
只是将ptr
前进给定的字节偏移量(与指针类型无关).
The macro RELOC_HIDE(ptr, offset)
simply advances ptr
by the given offset in bytes (regardless of the pointer type).
它是做什么的?
- 定义
DEFINE_PER_CPU(int, x)
时,会在特殊的.data.percpu
部分中创建一个整数__per_cpu_x
. - 加载内核时,此部分将加载多次-每个CPU加载一次(魔术的这一部分不在上面的代码中).
-
__per_cpu_offset
数组填充有副本之间的距离.假设每个cpu数据使用了1000个字节,则__per_cpu_offset[n]
将包含1000*n
. - 在加载过程中,符号
per_cpu__x
将重定位到CPU 0的per_cpu__x
.
在CPU 3上运行时, -
__get_cpu_var(x)
将转换为*RELOC_HIDE(&per_cpu__x, __per_cpu_offset[3])
.从CPU 0的x
开始,在CPU 0的数据和CPU 3的数据之间增加偏移量,并最终取消对结果指针的引用.
- When defining
DEFINE_PER_CPU(int, x)
, an integer__per_cpu_x
is created in the special.data.percpu
section. - When the kernel is loaded, this section is loaded multiple times - once per CPU (this part of the magic isn't in the code above).
- The
__per_cpu_offset
array is filled with the distances between the copies. Supposing 1000 bytes of per cpu data are used,__per_cpu_offset[n]
would contain1000*n
. - The symbol
per_cpu__x
will be relocated, during load, to CPU 0'sper_cpu__x
. __get_cpu_var(x)
, when running on CPU 3, will translate to*RELOC_HIDE(&per_cpu__x, __per_cpu_offset[3])
. This starts with CPU 0'sx
, adds the offset between CPU 0's data and CPU 3's, and eventually dereferences the resulting pointer.
这篇关于如何在Linux内核中实现percpu指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!