Back to home page

OSCL-LXR

 
 

    


0001 ===============================================================
0002 Softlockup detector and hardlockup detector (aka nmi_watchdog)
0003 ===============================================================
0004 
0005 The Linux kernel can act as a watchdog to detect both soft and hard
0006 lockups.
0007 
0008 A 'softlockup' is defined as a bug that causes the kernel to loop in
0009 kernel mode for more than 20 seconds (see "Implementation" below for
0010 details), without giving other tasks a chance to run. The current
0011 stack trace is displayed upon detection and, by default, the system
0012 will stay locked up. Alternatively, the kernel can be configured to
0013 panic; a sysctl, "kernel.softlockup_panic", a kernel parameter,
0014 "softlockup_panic" (see "Documentation/admin-guide/kernel-parameters.rst" for
0015 details), and a compile option, "BOOTPARAM_SOFTLOCKUP_PANIC", are
0016 provided for this.
0017 
0018 A 'hardlockup' is defined as a bug that causes the CPU to loop in
0019 kernel mode for more than 10 seconds (see "Implementation" below for
0020 details), without letting other interrupts have a chance to run.
0021 Similarly to the softlockup case, the current stack trace is displayed
0022 upon detection and the system will stay locked up unless the default
0023 behavior is changed, which can be done through a sysctl,
0024 'hardlockup_panic', a compile time knob, "BOOTPARAM_HARDLOCKUP_PANIC",
0025 and a kernel parameter, "nmi_watchdog"
0026 (see "Documentation/admin-guide/kernel-parameters.rst" for details).
0027 
0028 The panic option can be used in combination with panic_timeout (this
0029 timeout is set through the confusingly named "kernel.panic" sysctl),
0030 to cause the system to reboot automatically after a specified amount
0031 of time.
0032 
0033 Implementation
0034 ==============
0035 
0036 The soft and hard lockup detectors are built on top of the hrtimer and
0037 perf subsystems, respectively. A direct consequence of this is that,
0038 in principle, they should work in any architecture where these
0039 subsystems are present.
0040 
0041 A periodic hrtimer runs to generate interrupts and kick the watchdog
0042 job. An NMI perf event is generated every "watchdog_thresh"
0043 (compile-time initialized to 10 and configurable through sysctl of the
0044 same name) seconds to check for hardlockups. If any CPU in the system
0045 does not receive any hrtimer interrupt during that time the
0046 'hardlockup detector' (the handler for the NMI perf event) will
0047 generate a kernel warning or call panic, depending on the
0048 configuration.
0049 
0050 The watchdog job runs in a stop scheduling thread that updates a
0051 timestamp every time it is scheduled. If that timestamp is not updated
0052 for 2*watchdog_thresh seconds (the softlockup threshold) the
0053 'softlockup detector' (coded inside the hrtimer callback function)
0054 will dump useful debug information to the system log, after which it
0055 will call panic if it was instructed to do so or resume execution of
0056 other kernel code.
0057 
0058 The period of the hrtimer is 2*watchdog_thresh/5, which means it has
0059 two or three chances to generate an interrupt before the hardlockup
0060 detector kicks in.
0061 
0062 As explained above, a kernel knob is provided that allows
0063 administrators to configure the period of the hrtimer and the perf
0064 event. The right value for a particular environment is a trade-off
0065 between fast response to lockups and detection overhead.
0066 
0067 By default, the watchdog runs on all online cores.  However, on a
0068 kernel configured with NO_HZ_FULL, by default the watchdog runs only
0069 on the housekeeping cores, not the cores specified in the "nohz_full"
0070 boot argument.  If we allowed the watchdog to run by default on
0071 the "nohz_full" cores, we would have to run timer ticks to activate
0072 the scheduler, which would prevent the "nohz_full" functionality
0073 from protecting the user code on those cores from the kernel.
0074 Of course, disabling it by default on the nohz_full cores means that
0075 when those cores do enter the kernel, by default we will not be
0076 able to detect if they lock up.  However, allowing the watchdog
0077 to continue to run on the housekeeping (non-tickless) cores means
0078 that we will continue to detect lockups properly on those cores.
0079 
0080 In either case, the set of cores excluded from running the watchdog
0081 may be adjusted via the kernel.watchdog_cpumask sysctl.  For
0082 nohz_full cores, this may be useful for debugging a case where the
0083 kernel seems to be hanging on the nohz_full cores.