Linux PER-CPU Implementation Analysis __linux

Source: Internet
Author: User

217 static define_per_cpu (struct runqueue, runqueues);
One #define DEFINE_PER_CPU (type, name)
__ATTRIBUTE__ ((__section__ (". Data.percpu")) __typeof__ (type) per_cpu__# #name
13



First of all, there are in the Arch/i386/kernel/vmlinux.lds

  * 'll is freed after Init

  /ALIGN (4096);		/* Init Code and Data *

  /__init_begin =.;



  /* Here omit a number of lines:)/

  

  = ALIGN

  . __per_cpu_start =.;

  Data.percpu  : {* (. data.percpu)}

  __per_cpu_end =.;

  = ALIGN (4096);

  __init_end =.;

  * Freed after the init ends here * *




This shows the __per_cpu_start and __per_cpu_end identities. Data.percpu the beginning and end of this section
And, the whole. Data.percpu This section is between __init_begin and __init_end,
That is, the section's memory is released (free) after the system starts

Because there are
#define DEFINE_PER_CPU (type, name)
__ATTRIBUTE__ ((__section__ (". Data.percpu")) __typeof__ (type) per_cpu__# #name

So
Static define_per_cpu (struct runqueue, runqueues);
will be expanded into
__ATTRIBUTE__ ((__section__ (". Data.percpu")) __typeof__ (struct runqueue)
Per_cpu__runqueues;
Which is defined as a variable per_cpu__runqueues in the. data.percpu section.
Its type is struct runqueue. In fact, here's the so-called variable per_cpu__runqueues,
is actually an offset that identifies the address of the variable.

--------------------
Second, after the system starts, the following function is called in Start_kernel ()
unsigned long __per_cpu_offset[nr_cpus];



static void __init setup_per_cpu_areas (void)

{

	unsigned long size, I;

	char *ptr;

	/* Created by linker magic

	/extern char __per_cpu_start[], __per_cpu_end[];



	/* Copy section for each CPU (we discard the original) *

	/size = ALIGN (__per_cpu_end-__per_cpu_start, Smp_cache_byte S);

#ifdef config_modules

	if (Size < percpu_enough_room)

		size = Percpu_enough_room;

#endif



	ptr = alloc_bootmem (size * nr_cpus);



	for (i = 0; i < Nr_cpus; i++, ptr = size) {

		__per_cpu_offset[i] = ptr-__per_cpu_start;

		memcpy (PTR, __per_cpu_start, __per_cpu_end-__per_cpu_start);

	}






In this function, a proprietary data area is allocated for each CPU, and the data in. DATA.PERCPU is copied to it.
Each CPU has one copy. Since the data is transferred from __per_cpu_start to the CPU's own proprietary data area,
Therefore, accessing the variables can no longer use the original value, such as access to Per_cpu__runqueues
You can no longer use the per_cpu__runqueues, you need to do an offset adjustment,
That is, you need to add each CPU's own proprietary data area first address relative to the __per_cpu_start offset.
Here is __per_cpu_offset[i], where the proprietary data area of CPU I is relative to
The offset of the __per_cpu_start is __per_cpu_offset[i].
This makes it easy to compute the new addresses of the variables in the proprietary data area, such as for Per_cpu_runqueues,
The new address becomes per_cpu_runqueues+__per_cpu_offset[i].

After this process,. data.percpu This section can be released after the system has been initialized.

--------------------
and see how to access the variables per CPU
/* This macro obfuscates arithmetic in a variable address

   so that GCC shouldn ' t recognize the original VAR, and make a Ssumptions about it */

#define RELOC_HIDE (PTR, off)					

  ({unsigned long __ptr;					

    __asm__ ("": "=g" (__ptr): "0" (PTR);		

    (typeof (PTR)) (__ptr + (off)); })



/* var is in discarded Region:offset to particular copy we want/

#define PER_CPU (Var, cpu) (*reloc_hide ;p er_cpu__# #var, __per_cpu_offset[cpu])

#define __GET_CPU_VAR (Var) per_cpu (Var, smp_processor_id ())



# Define Get_cpu_var (Var) (* ({preempt_disable (); &__get_cpu_var (Var);})




For __get_cpu_var (runqueues), the equivalent is extended to
__PER_CPU_OFFSET[SMP_PROCESSOR_ID ()] + per_cpu__runqueues
And it's a lvalue, which means you can perform assignment operations.
This is exactly the new address for the above per_cpu__runqueues variable in the proprietary data area of the corresponding CPU.

Because different per CPU variables have different offsets, and different CPUs have different proprietary data area header addresses,
Therefore, different variables are accessed through the __get_cpu_var ().

--end


Go to: http://www.unixresources.net/linux/clf/linuxK/archive/00/00/47/91/479165.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.