Understanding the Linux Kernel - Kernel Synchronization

Kernel preemption (kernel preemption) (Linux 2.6)

The main features of preemptive kernel: a process in kernel mode operation, may be substituted during the execution of another process kernel function.
Preemption conditions:
1. Only when the kernel is executing an exception handler (especially system calls), and kernel preemption is not an explicit disabled, it may seize the kernel
2. The local CPU must open the local interrupt, or can not complete kernel preemption.
 
The critical area is a piece of code before another kernel control path can enter the critical section, enter the critical section of kernel control path must be completely finished code execution
 
Kernel Synchronization technology:
1 per CPU variables
The CPU core variable declaration for each variable (per-cpu variable). Each of the main CPU is a variable element of each array corresponding to an array of CPU data structures, system.
A CPU should not access other CPU corresponding array element, while it can be freely read or modify its own elements without worrying about race conditions occur. This means that each CPU only when it is determined variable data on the CPU in the system It is independent when they could use the logic.
2, an atomic operation (atomic operations)
Several assembly language instruction with "read - modify - write." Type, that is, they access memory twice, first read the original value, the second time to write a new value to avoid the "read - modify - write" command caused the easiest way to a race condition is to ensure that such an operation is atomic in chip level. any such operation must be performed in a single instruction, intermediate without interruption, and avoid other CPU access the same memory cell.
Linux kernel provides atomic_t type (one atom access counter).
typedef  struct {      
int counter; 
 } atomic_t; 
3, optimization and memory barrier
In fact, all the synchronization primitives act as memory optimization and barriers.
Optimization barrier (optimization barrier) primitives to ensure that the compiler can not be confused on assembly language instructions before primitive assembly language instructions on the operation and after the primitive operations. In Linux, is optimized barrier barrier () macro, it expands to asm volatile ( "" ::: "memory" ). optimization barrier does not guarantee the current CPU to mix assembly language instructions to perform --- this is a working memory barrier.
Memory barrier (memory barrier) primitives to ensure that, before the operation after primitives begin operation before the original language has been completed. Therefore, the memory barrier similar to the firewall, so that any assembly language instructions can not pass.
In a multiprocessor system, all atoms act as memory barrier operations, as they all use the lock byte.
4, spinlock (Spin Lock) : a multi-processor environment of a particular lock, if the kernel control path spin lock open, continues to acquire the lock and its own execution contrary, if the kernel control path. found locked by the locking operation of the kernel control path on another CPU, and rotates around, a tight loop is repeatedly executed instructions until the lock is released.
Read / write spin locks:
Order Lock (seqlock) : read / write spin locks similar, but given a higher priority write, even if the reader is reading, they also allow the writer to continue to run, the advantages writers never wait (unless another write who is writing), the disadvantage is that sometimes the reader has to read the same data repeatedly until it gets a valid copy.
5, read - copy - update (the RCU)
RCU is to protect the plurality of synchronization techniques in many cases the CPU reads the data structure designed. RCU allows multiple readers and writers concurrently (with the lock has been improved to the order to execute only a write-in). Moreover, RCU is not using locks.
The key idea is to limit the scope of the RCP, as follows:
(1) RCU dynamically allocated and only the protected data structure referenced by a pointer.
(2) being protected RCU critical section species, any kernel control path can not sleep.
RCU is Linux 2.6 Zhong added features used in the network layer and the virtual file system.
6, semaphore
Linux provides two semaphores:
(1) the amount of the core signal used by the kernel control path
(2) System V IPC semaphores, is used by the user mode process.
Read / write semaphore:
7, disable local interrupts
One of the main mechanisms to ensure that a core set of statements to be treated as a critical area is interrupt disabled, even when the hardware device generates an IRQ signal, but also to interrupt disable kernel control path continues, which provides an efficient way to ensure that the interrupt handler to access data structures are also protected. However, protection operation is not prohibited local interrupt handler in the interrupt another CPU concurrent access to the data structure, therefore, in a multiprocessor system, a local interrupt is prohibited are often used with optional lock.
 8, is prohibited and may delay the activation function
 
KEY: Different types of competitive conditions using different synchronization methods ......
 
Examples avoid race conditions:
1, the reference counter (reference counter)
Widely used in the kernel in order to avoid race conditions due to the concurrent allocation and release of resources generated.
 
 
 
 
 
 
 
 
 
 
Published 11 original articles · won praise 2 · Views 672

Guess you like

Origin blog.csdn.net/liheng301/article/details/42786993