Programs running in the Linux kernel should always prevent competing states caused by concurrency. This will cause the data structure to be broken down and cause the kernel to crash in severe cases. Therefore, the kernel synchronization technology is very important to the driver developed by the kernel. People who do not understand the kernel synchronization technology cannot write secure and robust programs... information & n programs running in the Linux kernel. they must always prevent competing states caused by concurrency. This will cause the data structure to be broken down and cause the kernel to crash in severe cases. Therefore, the kernel synchronization technology is very important to the driver developed by the kernel. Those who do not understand the kernel synchronization technology cannot write secure and robust kernel drivers. Before learning the kernel synchronization technology, you must master several concepts.
1. parallelism, concurrency, and competition: The Linux kernel running on SMP is a real parallel running program. multiple CPUs can access the same data structure at the same time, but on a single processor system, the running of the kernel program is a parallel operation that looks like running at the same time. a CPU can only execute one command at a time. When a normal kernel code accesses a data structure, it is interrupted or preemptible, and the program or preemptible kernel code also accesses this data. in this case, there are two types of kernel control paths that access the data structure. inconsistent access occurs, which leads to a race.
2. critical section: a piece of code that accesses a data structure. this code may be run in different kernel control paths. The kernel synchronization protects the data structure rather than the code, but the actually implemented synchronization technology is the code that protects access to the data structure.
3 kernel preemption definition: when a process executes a kernel function (including common processes and kernel threads that fall into the kernel ). That is, it allows process switching when running in kernel mode.
4. core control path: the environment in which the kernel executes code is divided into three types:
(1) normal system calls and kernel threads. It is characterized by interruptions, sleep allowed, process context, Current macro significance, and can be preemptible by the kernel.
(2) delayed functions: soft interrupt, kernel timer, and tasklet. The feature is that it can be interrupted, but does not allow sleep. there is no process context. the Current macro is meaningless and cannot be preemptible by the kernel.
<1> soft interrupt features: On SMP systems, the same soft interrupt can be executed simultaneously on different CPUs, and different soft interrupts can also be executed simultaneously. In a single CPU system, the same soft interrupt cannot be executed simultaneously, and different soft interrupts cannot be executed simultaneously.
<2> kernel timer: it is a predefined soft interrupt with all features of soft interrupt.
<3> tasklet: It is also a pre-defined soft interrupt in the kernel, but some protection mechanisms are used to make other soft interruptions unavailable. The same tasklet cannot be executed simultaneously in the SMP system. different tasklets can be executed simultaneously in the SMP system.
(3) interrupt handling program: it can be interrupted by other types of interrupt programs. The same interrupt cannot be nested. Interrupt functions can be written as non-Reentrant functions.
5 kernel running environment classification:
(1) a single CPU system that does not support kernel preemption
(2) a single CPU system that supports kernel preemption
(3) SMP system
After understanding these concepts, we can clarify the relationship between kernel control paths, which is important for understanding different synchronization technologies, the following situation only applies to scenarios where a single CPU can be preemptible by the kernel:
1. normal kernel code can be interrupted by interrupt programs, or can be interrupted by delayed functions (in essence, it is also interrupted because the kernel interrupts the processing program every 1 ms of beats, determines whether to run the executable function ). If another process can run in the interrupt processing program or delayed function and has a higher priority than the interrupted process, the interrupted process will lose the processor. Kernel preemption occurs. In this way, other processes will run. this process may call this code, which is equivalent to indirectly interrupting the current code. So to sum up, due to interruptions, normal kernel code can be interrupted by interrupt processing programs and indirectly interrupted by deletable functions, due to interruptions and kernel preemption, normal kernel code must be reentrant. (Interruption is forced to suspend execution, and the processor is not voluntarily abandoned)
2. delayable functions: these functions can be interrupted by interrupt processing programs. they cannot be interrupted by other delayable functions because they can be executed sequentially on the same CPU.
3. interrupt handling program: it can be interrupted by other types of interruptions, but cannot be interrupted by the same type. In the event of an interruption, the interruption of the same type is forbidden.
To cope with the competition caused by code in different kernel control paths, the Linux kernel has applied several kernel synchronization technologies:
1 per-CPU variable: the simplest synchronization technology, which is an array of data structures. each CPU in the system has an element in the array and can be operated only by itself, other CPUs cannot be accessed. However, if an asynchronous function needs to access this variable, it must be protected.
2. atomic operation: This technique is mainly used for single-variable data. the CPU may not be atomic for accessing a variable. However, the kernel can ensure atomic access to variables by using atomic variables of the atomic_t type. Note that only functions provided by the kernel can be used to operate such variables.
Operation.
3. Optimization barrier and memory barrier: to ensure that the compiler will not confuse assembly instructions before Barrier operations with assembly instructions after barrier operations. Memory Barrier: make sure that the barrier operation is completed before the barrier operation starts.
4. spin lock: the spin lock mainly targets SMP systems. on a single processor, the spin lock degrades to a switch that prevents kernel preemption. On the SMP system: the spin lock first prohibits kernel preemption, and then tests the atomic spin lock to install it. If the value is 1, the kernel control path gets the spin-down. If the value is 0, the kernel is preemptible and the cyclic test is performed until the value is 1. this is the origin of the spin lock name. Note that the spin lock can be preemptible. In a single-processor system: in addition to prohibiting kernel preemption, spin_lock does nothing. Key points of use of spin locks: use of spin locks in interrupt and deletable functions is allowed. But be sure to pay attention to deadlocks. If the normal kernel code uses the same lock as the interrupt handler, interruption should be prohibited when the normal kernel code holds the lock. Otherwise, a deadlock may occur. When the delayable function uses the same lock as the normal kernel code, the delayable function should be disabled when the normal kernel code or the lock is obtained. When the same lock is used in the interrupt handler and the code that can be delayed, the interrupt should be prohibited when the function can be delayed or the lock is obtained. The general principle is as follows: interrupt processing functions, delayed functions, and normal kernel code interrupt capabilities from large to small. To obtain a lock in code with low capabilities, you must disable code execution with high capabilities. On a single processor, although cumbersome is just a kernel preemption switch, the locking method is the same as that on the SMP system. But for different purposes, SMP systems aim to prevent deadlocks. A single processor is used to prevent competition. a spin lock deadlock cannot occur on a single processor.
Other synchronization technologies include:
Read/write spin locks. This lock is used to increase the kernel concurrency. As long as no kernel control path is available to modify the data, multiple control paths are allowed to read the same data structure at the same time. Read/write operations have the same priority.
Sequential lock: Similar to read/write locks, but read/write operations have different priorities. read operations have lower priority than write operations. That is, when a Read program holds a lock, it can be interrupted by a write program.
Read-copy-update technology (RCU): to protect the data structure read by multiple CPUs in most cases, this technology does not use the lock technology
Semaphore: a lock that enables a process to sleep. Sleep when the process or the lock is not obtained. when the lock is released, the sleeping process is awakened.
The synchronization technology in the kernel should be used flexibly in writing the driver program so as to compile a high concurrency, stable and robust driver program. First, you need to determine which parts need to be protected. This requires analysis that the data access code may have several kernel control paths for parallel or concurrent execution, and flexible application based on the characteristics of various synchronization technologies, generally, synchronization is not required if synchronization is not required. if synchronization is not required, no lock is required because kernel synchronization will inevitably cause certain performance losses.
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.