Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>I guess you're referring to Linux's per-CPU variable infrastructure.<br> Most of the magic is here (<code>asm-generic/percpu.h</code>):</p> <pre><code>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(&amp;per_cpu__##var, __per_cpu_offset[cpu])) #define __get_cpu_var(var) per_cpu(var, smp_processor_id()) </code></pre> <p>The macro <code>RELOC_HIDE(ptr, offset)</code> simply advances <code>ptr</code> by the given offset in bytes (regardless of the pointer type).</p> <p>What does it do?</p> <ol> <li>When defining <code>DEFINE_PER_CPU(int, x)</code>, an integer <code>__per_cpu_x</code> is created in the special <code>.data.percpu</code> section.</li> <li>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).</li> <li>The <code>__per_cpu_offset</code> array is filled with the distances between the copies. Supposing 1000 bytes of per cpu data are used, <code>__per_cpu_offset[n]</code> would contain <code>1000*n</code>.</li> <li>The symbol <code>per_cpu__x</code> will be relocated, during load, to CPU 0's <code>per_cpu__x</code>.</li> <li><code>__get_cpu_var(x)</code>, when running on CPU 3, will translate to <code>*RELOC_HIDE(&amp;per_cpu__x, __per_cpu_offset[3])</code>. This starts with CPU 0's <code>x</code>, adds the offset between CPU 0's data and CPU 3's, and eventually dereferences the resulting pointer.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload