Linux 2.6 Kernel read Note kernel sync

Source: Internet
Author: User
Tags semaphore

July 26, 2014 kernel preemption and kernel control path design

A definition of kernel preemption: If a process is executing kernel functions in the kernel state, a kernel switchover is allowed (that is, the process being replaced is the process in which the kernel function resides), and the kernel is preempted.


The Linux kernel provides the ability to turn on and off kernel preemption, and the kernel cannot preempt when the Current_thread_info preempt_count field is greater than 0 o'clock. This switch can be controlled by adding this field to the Preempt_disable and preempt_enable.

interrupts and soft interrupts and preemption control are mainly used to current_thread_info->preempt_count field, this field is divided into 4 parts: 0-7 bits is a preemption counter, 8-15 bits is a soft interrupt counter, 16-27 bits is a hard interrupt counter, 28 bits is Preempt_ The active flag.


The selection on the kernel design simplifies the synchronization of the kernel control path to some extent:

1. All interrupt handlers correspond to interrupts from pic and disable IRQ lines. In addition, the same interrupt event is not allowed until the end of the interrupt handler.

2. Interrupt handlers, soft interrupts, and tasklet can neither be preempted nor blocked, so they cannot be suspended for long periods of time. In the worst case, there will be a slight delay in their execution because other interrupts may occur during execution (nested execution of the kernel control path).

3. The kernel control path that performs interrupt processing cannot be interrupted by a deferred function or a kernel control path to the system invoke service routine.

4. Soft interrupts and Tasklet cannot be interleaved on a given CPU.

5. The same tasklet cannot be executed on several CPUs at the same time, but different types of tasklet can be executed concurrently on several CPUs.


July 26, 2014 synchronization Primitives

The various synchronization technologies used by the kernel:


Technology
Description Scope of application
Per CPU variable Replicating data between multiple CPUs All CPUs
Atomic operation atomically "read-Modify-write" operation on a counter All CPUs
Memory barrier Avoid order reordering Local CPU or all CPUs
Spin lock Locking when busy waiting All CPUs
Signal Volume Locking blocking Wait (sleep) All CPUs
Sequential lock The lock based on the access counter All CPUs
Prohibition of local interrupts Disable interrupt handling on a single CPU Local CPU
Prohibition of local soft interrupts Prohibit the execution of a lazy function on a single CPU Local CPU
Read-copy-write Access to shared data structures through pointers rather than locks All CPUs

per-CPU variables: The main practice is to declare an array of variables of nr_cpus size, each of which uses its own variables. To be careful not to access variables that belong to other CPUs, you can use the Get_cpu_var () (no preemption) macro to get the variable and end the use of the variable with the Put_cpu_var () (Start preemption) macro.

Atomic manipulation: The atomic operation of integer values is achieved through atomic assembly directives such as Lock,inc,dec and so on.

Memory Barrier: Avoid the compiler, CPU to reorder the instructions, of course, this may lose the optimization of command rearrangement.

Spin lock: Try to get the lock before entering the critical section, and if you can't get it, always be there. In fact, at the bottom or through the lock,inc,dec and so on atomic assembly instructions to achieve. Since the spin lock wastes CPU cycles, it is only suitable for small synchronization scenarios in critical areas.

Semaphore: try to get the semaphore before entering the critical section, and if you can't get it, put the current process into the semaphore waiting queue to sleep. The bottom layer is achieved by a spin lock. Semaphores cannot be used in interrupt handlers and deferred functions (tasklets) because they do not allow sleep.

Sequential locks: Similar to read-write locks, but read-write operations that differ from read-write locks have the same priority, and sequential lock write operations have a higher priority.

Local interrupt suppression: Local interrupts are closed and turned on through the CLI and STI. The kernel provides local_irq_disable and local_irq_enable to implement these functions. The kernel also provides local_irq_save and local_irq_restore the ability to save and restore EFlags registers based on the functionality of the previous two macros, which is important for interrupt nesting execution.

Local soft interrupt suppression: Local_bh_disable and local_bh_enable enable local soft-break shutdown and turn-on capabilities. Macro expansion is actually the current_thread_info->preempt_count of the soft interrupt part of the addition and subtraction operation.

Read-copy-write: A technique introduced in order to improve the concurrency of the kernel, the main scenario is suitable for pointer data, the read operation does not need synchronization, the write operation, the first copy copy, and then modify the copy, and finally at the appropriate time (because other read operations may be in progress) the original data pointer to the address of the copy.

Linux 2.6 Kernel read Note kernel sync

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.