Where are DECLARE_PER_CPU variables stored in kernel

Viewed 597

I am trying to understand how current macro retrieves struct task_struct of the process.

I am trying to understand for x86 architecture, and after exploring kernel source, struck at the following code:

#include <linux/compiler.h>
#include <asm/percpu.h>

#ifndef __ASSEMBLY__
struct task_struct;

DECLARE_PER_CPU(struct task_struct *, current_task);

static __always_inline struct task_struct *get_current(void)
{
    return percpu_read_stable(current_task);
}

#define current get_current()

#endif /* __ASSEMBLY__ */

#endif /* _ASM_X86_CURRENT_H */
  1. Where are the variables declared in DECLARE_PER_CPU stored in memory.
  2. Are they at fixed location or in CPU Registers.
  3. I am still unable to get, how this will give the task_struct pointer

Can anyone explain it. Thanks for your time and patience

1 Answers

from what i understood from the sources that i will mention below, the answers to your questions are such:

  1. the variables are already defined by the DEFINE_PER_CPU macro, and the use of DECLARE_PER_CPU is there to tell the compiler that an external reference is being made. the section in which current_struct variable is stored depends whether the CONFIG_SMP is defined on 32bit arch's, if its defined it will be at ".data.percpu" section otherwise, it will be at ".data" section. In 64 bit arch it will always be at ".data.percpu". the variables which are declared by DECLARE_PER_CPU will be stored on the stack.

  2. let me qoute:

On boot up, an area is allocated by the size of the ".data.percpu" section + PERCPU_ENOUGH_ROOM times NR_CPUS. The __per_cpu_offset[] array holds the difference between the ".data.percpu" section and the location where the data is actually stored. __per_cpu_offset[0] holds the difference for the variables assigned to cpu 0, __per_cpu_offset[1] holds the difference for the variables to cpu 1, and so on.

so, it depandes on the order of the other declarations of "per cpu" variables

  1. the macro per_cpu_read_stable is used to read the "current_task" per-cpu variable.

Related