Linux kernel preemption

Source: Internet
Author: User

I posted a blog post without further research. You can also refer to section 4.6 of lkd

Early linux kernels cannot be preemptible. Its scheduling method is: a process can voluntarily start a scheduling by using the schedule () function. Voluntary mandatory scheduling can only happen on the eve of every return from the system call and the eve of every return from the interruption or exception handling to the user space (this kind of mandatory scheduling is also called user preemption ). However, system space interruption or exceptions will not cause scheduling. This method simplifies kernel implementation. However, the following two problems often exist:

    • If such an interruption occurs in the kernel, the return of this interruption will not cause scheduling. Instead, the system call or interruption (exception) will occur when the CPU enters the kernel space from the user space) scheduling occurs only when the response is returned.
    • Another problem is priority inversion. In Linux, any operation in the core State takes precedence over the user State process, which may lead to a reverse priority problem. For example, a low-priority user process fails to respond to a high-priority task due to soft/hard interruptions.

The current Linux kernel is added with the kernel preempt mechanism. Kernel preemption refers to the userProgramThe system can be preemptible during execution of the system call. The process is temporarily suspended to enable the new high-priority process to run. This preemption cannot be performed securely anywhere in the kernel, such as in the critical section.CodeCannot be preemptible. A critical section is a sequence of commands that cannot be executed by more than one process at a time. In the Linux kernel, these parts require spin lock protection.

Kernel preemption requires that all variables and data structures that may be shared by more than one process in the kernel should be protected by the mutex mechanism, or both should be placed in the critical section. In a preemptible kernel, it is considered that if the kernel is not in an interrupt processing program and is not in the critical Code protected by mutually exclusive mechanisms such as spinlock, the process can be switched securely.

The Linux kernel adds a mutex mechanism to the critical code for protection. At the same time, it also inserts a scheduling checkpoint on the code path that is too long to interrupt the execution path for a long time. In this way, the task can quickly switch the Process status and is ready for kernel preemption.

Linux kernel preemption can be performed only when the kernel is executing an exception handler (usually called by the system) and allows kernel preemption. Kernel preemption is prohibited as follows:

(1) kernel preemption is not allowed when the kernel executes the interrupt processing routine, and then the kernel preemption is executed when the return result is interrupted.

(2) When the kernel executes Soft Interrupt or tasklet, kernel preemption is prohibited. When Soft Interrupt returns, kernel preemption is executed.

(3) kernel preemption is prohibited in the critical section. The critical section protection function controls the preemption by using the preemption count macro. If the count is greater than 0, kernel preemption is prohibited.

The principle of preemptible kernel implementation is that if the need_resched of the current execution process is marked when the spin lock is released or the return from the interruption, the preemptible scheduling is performed.

 

Detailed background and implementation:Http://blog.csdn.net/sailor_8318/article/details/2870184

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.