1*4882a593Smuzhiyun=============================================================== 2*4882a593SmuzhiyunSoftlockup detector and hardlockup detector (aka nmi_watchdog) 3*4882a593Smuzhiyun=============================================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunThe Linux kernel can act as a watchdog to detect both soft and hard 6*4882a593Smuzhiyunlockups. 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunA 'softlockup' is defined as a bug that causes the kernel to loop in 9*4882a593Smuzhiyunkernel mode for more than 20 seconds (see "Implementation" below for 10*4882a593Smuzhiyundetails), without giving other tasks a chance to run. The current 11*4882a593Smuzhiyunstack trace is displayed upon detection and, by default, the system 12*4882a593Smuzhiyunwill stay locked up. Alternatively, the kernel can be configured to 13*4882a593Smuzhiyunpanic; a sysctl, "kernel.softlockup_panic", a kernel parameter, 14*4882a593Smuzhiyun"softlockup_panic" (see "Documentation/admin-guide/kernel-parameters.rst" for 15*4882a593Smuzhiyundetails), and a compile option, "BOOTPARAM_SOFTLOCKUP_PANIC", are 16*4882a593Smuzhiyunprovided for this. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunA 'hardlockup' is defined as a bug that causes the CPU to loop in 19*4882a593Smuzhiyunkernel mode for more than 10 seconds (see "Implementation" below for 20*4882a593Smuzhiyundetails), without letting other interrupts have a chance to run. 21*4882a593SmuzhiyunSimilarly to the softlockup case, the current stack trace is displayed 22*4882a593Smuzhiyunupon detection and the system will stay locked up unless the default 23*4882a593Smuzhiyunbehavior is changed, which can be done through a sysctl, 24*4882a593Smuzhiyun'hardlockup_panic', a compile time knob, "BOOTPARAM_HARDLOCKUP_PANIC", 25*4882a593Smuzhiyunand a kernel parameter, "nmi_watchdog" 26*4882a593Smuzhiyun(see "Documentation/admin-guide/kernel-parameters.rst" for details). 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunThe panic option can be used in combination with panic_timeout (this 29*4882a593Smuzhiyuntimeout is set through the confusingly named "kernel.panic" sysctl), 30*4882a593Smuzhiyunto cause the system to reboot automatically after a specified amount 31*4882a593Smuzhiyunof time. 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunImplementation 34*4882a593Smuzhiyun============== 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunThe soft and hard lockup detectors are built on top of the hrtimer and 37*4882a593Smuzhiyunperf subsystems, respectively. A direct consequence of this is that, 38*4882a593Smuzhiyunin principle, they should work in any architecture where these 39*4882a593Smuzhiyunsubsystems are present. 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunA periodic hrtimer runs to generate interrupts and kick the watchdog 42*4882a593Smuzhiyuntask. An NMI perf event is generated every "watchdog_thresh" 43*4882a593Smuzhiyun(compile-time initialized to 10 and configurable through sysctl of the 44*4882a593Smuzhiyunsame name) seconds to check for hardlockups. If any CPU in the system 45*4882a593Smuzhiyundoes not receive any hrtimer interrupt during that time the 46*4882a593Smuzhiyun'hardlockup detector' (the handler for the NMI perf event) will 47*4882a593Smuzhiyungenerate a kernel warning or call panic, depending on the 48*4882a593Smuzhiyunconfiguration. 49*4882a593Smuzhiyun 50*4882a593SmuzhiyunThe watchdog task is a high priority kernel thread that updates a 51*4882a593Smuzhiyuntimestamp every time it is scheduled. If that timestamp is not updated 52*4882a593Smuzhiyunfor 2*watchdog_thresh seconds (the softlockup threshold) the 53*4882a593Smuzhiyun'softlockup detector' (coded inside the hrtimer callback function) 54*4882a593Smuzhiyunwill dump useful debug information to the system log, after which it 55*4882a593Smuzhiyunwill call panic if it was instructed to do so or resume execution of 56*4882a593Smuzhiyunother kernel code. 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunThe period of the hrtimer is 2*watchdog_thresh/5, which means it has 59*4882a593Smuzhiyuntwo or three chances to generate an interrupt before the hardlockup 60*4882a593Smuzhiyundetector kicks in. 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunAs explained above, a kernel knob is provided that allows 63*4882a593Smuzhiyunadministrators to configure the period of the hrtimer and the perf 64*4882a593Smuzhiyunevent. The right value for a particular environment is a trade-off 65*4882a593Smuzhiyunbetween fast response to lockups and detection overhead. 66*4882a593Smuzhiyun 67*4882a593SmuzhiyunBy default, the watchdog runs on all online cores. However, on a 68*4882a593Smuzhiyunkernel configured with NO_HZ_FULL, by default the watchdog runs only 69*4882a593Smuzhiyunon the housekeeping cores, not the cores specified in the "nohz_full" 70*4882a593Smuzhiyunboot argument. If we allowed the watchdog to run by default on 71*4882a593Smuzhiyunthe "nohz_full" cores, we would have to run timer ticks to activate 72*4882a593Smuzhiyunthe scheduler, which would prevent the "nohz_full" functionality 73*4882a593Smuzhiyunfrom protecting the user code on those cores from the kernel. 74*4882a593SmuzhiyunOf course, disabling it by default on the nohz_full cores means that 75*4882a593Smuzhiyunwhen those cores do enter the kernel, by default we will not be 76*4882a593Smuzhiyunable to detect if they lock up. However, allowing the watchdog 77*4882a593Smuzhiyunto continue to run on the housekeeping (non-tickless) cores means 78*4882a593Smuzhiyunthat we will continue to detect lockups properly on those cores. 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunIn either case, the set of cores excluded from running the watchdog 81*4882a593Smuzhiyunmay be adjusted via the kernel.watchdog_cpumask sysctl. For 82*4882a593Smuzhiyunnohz_full cores, this may be useful for debugging a case where the 83*4882a593Smuzhiyunkernel seems to be hanging on the nohz_full cores. 84