84 lines · plain
1===============================================================2Softlockup detector and hardlockup detector (aka nmi_watchdog)3===============================================================4 5The Linux kernel can act as a watchdog to detect both soft and hard6lockups.7 8A 'softlockup' is defined as a bug that causes the kernel to loop in9kernel mode for more than 20 seconds (see "Implementation" below for10details), without giving other tasks a chance to run. The current11stack trace is displayed upon detection and, by default, the system12will stay locked up. Alternatively, the kernel can be configured to13panic; a sysctl, "kernel.softlockup_panic", a kernel parameter,14"softlockup_panic" (see "Documentation/admin-guide/kernel-parameters.rst" for15details), and a compile option, "BOOTPARAM_SOFTLOCKUP_PANIC", are16provided for this.17 18A 'hardlockup' is defined as a bug that causes the CPU to loop in19kernel mode for more than 10 seconds (see "Implementation" below for20details), without letting other interrupts have a chance to run.21Similarly to the softlockup case, the current stack trace is displayed22upon detection and the system will stay locked up unless the default23behavior is changed, which can be done through a sysctl,24'hardlockup_panic', a compile time knob, "BOOTPARAM_HARDLOCKUP_PANIC",25and a kernel parameter, "nmi_watchdog"26(see "Documentation/admin-guide/kernel-parameters.rst" for details).27 28The panic option can be used in combination with panic_timeout (this29timeout is set through the confusingly named "kernel.panic" sysctl),30to cause the system to reboot automatically after a specified amount31of time.32 33Implementation34==============35 36The soft and hard lockup detectors are built on top of the hrtimer and37perf subsystems, respectively. A direct consequence of this is that,38in principle, they should work in any architecture where these39subsystems are present.40 41A periodic hrtimer runs to generate interrupts and kick the watchdog42job. An NMI perf event is generated every "watchdog_thresh"43(compile-time initialized to 10 and configurable through sysctl of the44same name) seconds to check for hardlockups. If any CPU in the system45does not receive any hrtimer interrupt during that time the46'hardlockup detector' (the handler for the NMI perf event) will47generate a kernel warning or call panic, depending on the48configuration.49 50The watchdog job runs in a stop scheduling thread that updates a51timestamp every time it is scheduled. If that timestamp is not updated52for 2*watchdog_thresh seconds (the softlockup threshold) the53'softlockup detector' (coded inside the hrtimer callback function)54will dump useful debug information to the system log, after which it55will call panic if it was instructed to do so or resume execution of56other kernel code.57 58The period of the hrtimer is 2*watchdog_thresh/5, which means it has59two or three chances to generate an interrupt before the hardlockup60detector kicks in.61 62As explained above, a kernel knob is provided that allows63administrators to configure the period of the hrtimer and the perf64event. The right value for a particular environment is a trade-off65between fast response to lockups and detection overhead.66 67By default, the watchdog runs on all online cores. However, on a68kernel configured with NO_HZ_FULL, by default the watchdog runs only69on the housekeeping cores, not the cores specified in the "nohz_full"70boot argument. If we allowed the watchdog to run by default on71the "nohz_full" cores, we would have to run timer ticks to activate72the scheduler, which would prevent the "nohz_full" functionality73from protecting the user code on those cores from the kernel.74Of course, disabling it by default on the nohz_full cores means that75when those cores do enter the kernel, by default we will not be76able to detect if they lock up. However, allowing the watchdog77to continue to run on the housekeeping (non-tickless) cores means78that we will continue to detect lockups properly on those cores.79 80In either case, the set of cores excluded from running the watchdog81may be adjusted via the kernel.watchdog_cpumask sysctl. For82nohz_full cores, this may be useful for debugging a case where the83kernel seems to be hanging on the nohz_full cores.84