Per_cpu variable in Kernel

Source: Internet
Author: User
Per-CPU
The introduction of variables effectively solves the competition between the SMP system processor and lock. Each CPU only needs to access its local variables. This article describes the implementation and related operations of the per-CPU variable on the 2.6 kernel. In the system compilation phase, we have manually defined all the per-CPU variables. These variables are defined using the macro define_per_cpu:

  1. 11 # define define_per_cpu (type, name)
    \
  2. 12 _ attribute _ (_ Section _ (". Data. percpu ")))
    _ Typeof _ (type) per_cpu __## name

From the code above, we can see that all the manually defined per-CPU variables are placed in the. Data. percpu segment. Note that the preceding macro is defined only in the SMP architecture. If it is not an SMP-structured computer, simply put all the per-CPU variables in the place where the global variables should be placed.

Per-CPU variable definition for a single CPU:

  1. 27 # else
    /*! SMP
    */
  2. 28
  3. 29 # define define_per_cpu (type, name)
    \
  4. 30 _ typeof _ (type) per_cpu __## name

After learning the above Code, we must also clarify that the per-CPU variable used in a single-CPU computer is the per-CPU variable defined in the above macro in the global data zone. In the SMP architecture, we use a variable that is not placed in the. Data. percpu segment. Imagine which CPU should be used if this variable is used? In fact, in SMP, each CPU uses copies of these per-CPU variables in the. Data. percpu segment, and several such replicas are created for several CPUs.

During system initialization, The start_kernel () function calls the setup_per_cpu_areas () function to allocate space for each CPU's per-CPU variable copy. Note that the alloc memory distributor has not yet been created, this function calls the alloc_bootmem function to allocate physical space for these replicas during initialization.

  1. 332 static void _ init setup_per_cpu_areas (void)
  2. /*
    */
  3. 333 {
  4. 334 unsigned long size, I;
  5. 335 char * PTR;
  6. 336
  7. 337/* Copy Section
    For each CPU
    (We discard the original)
    */
  8. 338 size = align (_ per_cpu_end
    -_ Per_cpu_start, smp_cache_bytes );
  9. 339 # ifdef config_modules
  10. 340 if (size
    <Percpu_enough_room)
  11. 341 size = percpu_enough_room;
  12. 342 # endif
  13. 343
  14. 344 PTR = alloc_bootmem (size
    * Nr_cpus );
  15. 345
  16. 346 for (I
    = 0; I
    <Nr_cpus; I ++, PTR
    + = Size)
    {
  17. 347 _ per_cpu_offset [I]
    = PTR-_ per_cpu_start;
  18. 348 memcpy (PTR, _ per_cpu_start, _ per_cpu_end
    -_ Per_cpu_start );
  19. 349}
  20. 350}
  21. 351 # endif /*
    ! _ Generic_per_cpu
    */

The preceding function allocates the physical space occupied by the per-CPU replicas of each CPU, the _ per_cpu_offset [nr_cpus] array is also initialized to find copies of these per-CPU variables for the specified CPU.


 

  1. 15 # define per_cpu (VAR, CPU)
    (* Reloc_hide (& per_cpu __# # var, _ per_cpu_offset [CPU])
  2. 16 # DEFINE _ get_cpu_var (VAR) per_cpu (VAR, smp_processor_id ())

The two macros are used to obtain the per-CPU variable of the specified CPU, and the other is used to obtain the per-CPU variable of the local CPU. You can analyze them by yourself.

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.