Demistifying Softlockup & Hardlockup
A 'softlockup' is defined as a bug that causes the kernel to loop in kernel mode for more than 20 seconds.without giving other tasks a chance to run |
A 'hardlockup' is defined as a bug that causes the CPU to loop inkernel mode for more than 10 seconds.
without letting other interrupts have a chance to run. |
Details:
The so-called lockup refers to a section of kernel code that holds the CPU. A serious lockup can cause the entire system to lose its response. Lockup has several features:
- First of all, only the kernel code can cause lockup, because user code can be preempted, it is impossible to form a lockup (only one exception, real-time processes with a SCHED_FIFO priority of 99 may even make the [watchdog/x] kernel even in user mode. Threads can't grab the CPU and form a soft lock. See " Real-Time Process Causes System Lockup? ")
- Secondly, the kernel code must be in the preemption disabled state because Linux is a preemptive kernel. It is only in certain code areas that preemption is forbidden. In these code regions, lockup may be formed.
There are two types of lockup: soft lockup and hard lockup. The difference is that hard lockup occurs when the CPU masks the interrupt.
- Soft lockup means that the CPU is occupied by the kernel code so that other processes cannot be executed. The principle of detecting soft lockup is to assign a timed execution kernel thread [watchdog/x] to each CPU. If the thread is not executed within the set period, it means that soft lockup occurred. [watchdog/x] is The SCHED_FIFO real-time process, with the highest priority of 99, has the priority to run.
- Hard lockup is more serious than soft lockup. CPU can not only execute other processes, but also no longer respond to interrupts. The principle of detecting hard lockup utilizes the PMU's NMI perf event because the NMI interrupt is not maskable and can still be executed if the CPU is no longer responding to interrupts. It then checks to see if the clock interrupt counter hrtimer_interrupts is incrementing. Stagnation means that the clock interrupt is not responded, that is, a hard lockup occurs.
Linux kernel designed a mechanism to detect lockup, called NMI Watchdog , which uses NMI interrupt. NMI is used because lockup may occur when the interrupt is masked. At this time, the only way to get CPU down is through NMI, because NMI interrupts are not maskable. The NMI Watchdog contains a soft lockup detector and a hard lockup detector. The implementation of the kernel after 2.6 is as follows.
The trigger mechanism of the NMI Watchdog consists of two parts:
- A high-precision timer (hrtimer), the corresponding interrupt handling routine is kernel/watchdog.c: watchdog_timer_fn(), in this routine:
- To increment the counter hrtimer_interrupts, this counter is used by the hard lockup detector to determine if the CPU is responding to an interrupt.
- Also wake up the [watchdog/x] kernel thread whose task is to update a timestamp;
- The soft lock detector checks the timestamp. If the soft lockup threshold has not been updated, it means that [watchdog/x] has not been given a chance to run. This means that the CPU is occupied, that is, soft lockup has occurred.
- Based on the PMU's NMI perf event, an NMI interrupt is triggered when the counter of the PMU overflows. The corresponding interrupt handling routine is kernel/watchdog.c: watchdog_overflow_callback(). The hard lockup detector is there, and it checks the number of interrupts of the above hrtimer. (hrtimer_interrupts) Whether to keep incrementing, if stagnant indicates that the hrtimer interrupt did not get a response, that is, a hard lockup occurred.
The hrtimer period is: softlockup_thresh/5.
Note:
Note:
- In the 2.6 kernel:
The value of softlockup_thresh is equal to the kernel parameter kernel.watchdog_thresh, default 60 seconds; - In the 3.10 kernel, the
name of the kernel parameter kernel.watchdog_thresh has not changed, but the meaning has changed to the hard lockup threshold, and the default value is 10 seconds. The
soft lockup threshold is equal to (2*kernel.watchdog_thresh), which is the default value of 20 seconds.
The NMI perf event is based on the PMU. The hard lockup threshold is a fixed 60 seconds in the 2.6 kernel and cannot be manually adjusted. In the 3.10 kernel, it can be manually adjusted because it directly corresponds to the kernel parameter kernel.watchdog_thresh. The default value is 10. second.
What should I do if lockup is detected? You can auto panic, or you can output the information even if it is finished, this can be defined by the kernel parameters:
- Kernel.softlockup_panic: Determines whether panic is detected when soft lockup is detected. The default value is 0.
- Kernel.nmi_watchdog: defines whether to enable nmi watchdog, and whether hard lockup causes panic. The format of this kernel parameter is "=[panic,][nopanic,][num]".
(Note: The latest kernel introduces new kernel parameters. Kernel.hardlockup_panic, you can determine if your kernel supports it by checking for the existence of /proc/sys/kernel/hardlockup_panic.)
Comments
Post a Comment