Per CPU variable

Source: Internet
Author: User

The best synchronization technology is to put the design of critical resources that do not need to be synchronized first. This is a way of thinking, because each explicit synchronization primitive has a performance overhead that cannot be ignored.

The simplest and most important synchronization technology includes declaring kernel variables or data structures as per-CPU variables ). Each CPU variable is mainly an array of data structures, and each CPU of the system corresponds to an element of the array.

A cpu should not access the array elements corresponding to other CPUs. In addition, it can read or modify its own elements without worrying about competition conditions, because it is the only CPU that is eligible to do so. However, this also means that each CPU variable can only be used in special circumstances, that is, when it determines that the data on the CPU of the system is logically independent.

Array elements of each CPU are arranged in the primary memory so that each data structure is stored in different lines of the hardware cache. Therefore, concurrent access to each CPU array will not cause high-speed cache row theft and failure (this operation will bring about expensive system overhead ).

Although each CPU variable provides protection for concurrent access from different CPUs, it does not protect access from Asynchronous functions (Interrupt handlers and delayable functions, in this case, another synchronization technology is required.

In addition, in a single processor and multi-processor system, kernel preemption can enable competition for each CPU variable. The general principle is that the kernel control path should access each CPU variable when preemption is disabled. Because when a kernel control path obtains the address of its local copy of each CPU variable, and then it is preemptible and transferred to another CPU, but still references the address of the original CPU element, this is very dangerous.

This blog introduces some useful macros for each CPU.

1 define_per_cpu (type, name)

Statically allocate an array of each CPU. The array name and structure type are as follows:
# Define declare_per_cpu (type, name) extern _ typeof _ (type) per_cpu __# # Name

2 per_cpu (name, CPU)

Obtain an array element of each CPU selected for the CPU using the define_per_cpu Macro. the CPU is specified by the CPU and the array name is name:
# Define per_cpu (VAR, CPU) (* (void) (CPU), & per_cpu __# # var ))

3 _ get_cpu_var (name)

Select the local CPU element for each CPU array name:
# DEFINE _ get_cpu_var (VAR) per_cpu __# # VaR

4 get_cpu_var (name)

Disable kernel preemption first, and then select an element for the local CPU in each CPU array name:
# Define get_cpu_var (VAR) (* ({preempt_disable (); & __ get_cpu_var (VAR );}))

5 put_cpu_var (name)

Enable kernel preemption (without name ):
# Define put_cpu_var (VAR) preempt_enable ()

6 alloc_percpu (type)

Dynamically allocate each CPU array of the type data structure and return its address:
# Define alloc_percpu (type) (type *) (_ alloc_percpu (sizeof (type ))))
Static inline void * _ alloc_percpu (size_t size)
{
Void * ret = kmalloc (size, gfp_kernel );
If (RET)
Memset (Ret, 0, size );
Return ret;
}

7 free_percpu (pointer)

Release the dynamically allocated CPU array, with pointer indicating its address:
Static inline void free_percpu (const void * PTR)
{
Kfree (PTR );
}

8 per_cpu_ptr (pointer, CPU)

Returns the address of the CPU element corresponding to the CPU in each CPU array. pointer provides the array address:
# Define per_cpu_ptr (PTR, CPU) ({(void) (CPU); (PTR );})

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.