1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * linux/include/linux/nmi.h 4 */ 5 #ifndef LINUX_NMI_H 6 #define LINUX_NMI_H 7 8 #include <linux/sched.h> 9 #include <asm/irq.h> 10 #if defined(CONFIG_HAVE_NMI_WATCHDOG) 11 #include <asm/nmi.h> 12 #endif 13 14 #ifdef CONFIG_LOCKUP_DETECTOR 15 void lockup_detector_init(void); 16 void lockup_detector_soft_poweroff(void); 17 void lockup_detector_cleanup(void); 18 19 extern int watchdog_user_enabled; 20 extern int watchdog_thresh; 21 extern unsigned long watchdog_enabled; 22 23 extern struct cpumask watchdog_cpumask; 24 extern unsigned long *watchdog_cpumask_bits; 25 #ifdef CONFIG_SMP 26 extern int sysctl_softlockup_all_cpu_backtrace; 27 extern int sysctl_hardlockup_all_cpu_backtrace; 28 #else 29 #define sysctl_softlockup_all_cpu_backtrace 0 30 #define sysctl_hardlockup_all_cpu_backtrace 0 31 #endif /* !CONFIG_SMP */ 32 33 #else /* CONFIG_LOCKUP_DETECTOR */ 34 static inline void lockup_detector_init(void) { } 35 static inline void lockup_detector_soft_poweroff(void) { } 36 static inline void lockup_detector_cleanup(void) { } 37 #endif /* !CONFIG_LOCKUP_DETECTOR */ 38 39 #ifdef CONFIG_SOFTLOCKUP_DETECTOR 40 extern void touch_softlockup_watchdog_sched(void); 41 extern void touch_softlockup_watchdog(void); 42 extern void touch_softlockup_watchdog_sync(void); 43 extern void touch_all_softlockup_watchdogs(void); 44 extern unsigned int softlockup_panic; 45 46 extern int lockup_detector_online_cpu(unsigned int cpu); 47 extern int lockup_detector_offline_cpu(unsigned int cpu); 48 #else /* CONFIG_SOFTLOCKUP_DETECTOR */ 49 static inline void touch_softlockup_watchdog_sched(void) { } 50 static inline void touch_softlockup_watchdog(void) { } 51 static inline void touch_softlockup_watchdog_sync(void) { } 52 static inline void touch_all_softlockup_watchdogs(void) { } 53 54 #define lockup_detector_online_cpu NULL 55 #define lockup_detector_offline_cpu NULL 56 #endif /* CONFIG_SOFTLOCKUP_DETECTOR */ 57 58 #ifdef CONFIG_DETECT_HUNG_TASK 59 void reset_hung_task_detector(void); 60 #else 61 static inline void reset_hung_task_detector(void) { } 62 #endif 63 64 /* 65 * The run state of the lockup detectors is controlled by the content of the 66 * 'watchdog_enabled' variable. Each lockup detector has its dedicated bit - 67 * bit 0 for the hard lockup detector and bit 1 for the soft lockup detector. 68 * 69 * 'watchdog_user_enabled', 'watchdog_hardlockup_user_enabled' and 70 * 'watchdog_softlockup_user_enabled' are variables that are only used as an 71 * 'interface' between the parameters in /proc/sys/kernel and the internal 72 * state bits in 'watchdog_enabled'. The 'watchdog_thresh' variable is 73 * handled differently because its value is not boolean, and the lockup 74 * detectors are 'suspended' while 'watchdog_thresh' is equal zero. 75 */ 76 #define WATCHDOG_HARDLOCKUP_ENABLED_BIT 0 77 #define WATCHDOG_SOFTOCKUP_ENABLED_BIT 1 78 #define WATCHDOG_HARDLOCKUP_ENABLED (1 << WATCHDOG_HARDLOCKUP_ENABLED_BIT) 79 #define WATCHDOG_SOFTOCKUP_ENABLED (1 << WATCHDOG_SOFTOCKUP_ENABLED_BIT) 80 81 #if defined(CONFIG_HARDLOCKUP_DETECTOR) 82 extern void hardlockup_detector_disable(void); 83 extern unsigned int hardlockup_panic; 84 #else 85 static inline void hardlockup_detector_disable(void) {} 86 #endif 87 88 #if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF) 89 void arch_touch_nmi_watchdog(void); 90 void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs); 91 #elif !defined(CONFIG_HAVE_NMI_WATCHDOG) 92 static inline void arch_touch_nmi_watchdog(void) { } 93 #endif 94 95 #if defined(CONFIG_HAVE_NMI_WATCHDOG) || defined(CONFIG_HARDLOCKUP_DETECTOR) 96 # define NMI_WATCHDOG_SYSCTL_PERM 0644 97 #else 98 # define NMI_WATCHDOG_SYSCTL_PERM 0444 99 #endif 100 101 #if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF) 102 extern void hardlockup_detector_perf_stop(void); 103 extern void hardlockup_detector_perf_restart(void); 104 extern void hardlockup_detector_perf_disable(void); 105 extern void hardlockup_detector_perf_enable(void); 106 extern void hardlockup_detector_perf_cleanup(void); 107 extern int hardlockup_detector_perf_init(void); 108 #else 109 static inline void hardlockup_detector_perf_stop(void) { } 110 static inline void hardlockup_detector_perf_restart(void) { } 111 static inline void hardlockup_detector_perf_disable(void) { } 112 static inline void hardlockup_detector_perf_enable(void) { } 113 static inline void hardlockup_detector_perf_cleanup(void) { } 114 # if !defined(CONFIG_HAVE_NMI_WATCHDOG) 115 static inline int hardlockup_detector_perf_init(void) { return -ENODEV; } 116 # else 117 static inline int hardlockup_detector_perf_init(void) { return 0; } 118 # endif 119 #endif 120 121 void watchdog_hardlockup_stop(void); 122 void watchdog_hardlockup_start(void); 123 int watchdog_hardlockup_probe(void); 124 void watchdog_hardlockup_enable(unsigned int cpu); 125 void watchdog_hardlockup_disable(unsigned int cpu); 126 127 void lockup_detector_reconfigure(void); 128 129 /** 130 * touch_nmi_watchdog - manually reset the hardlockup watchdog timeout. 131 * 132 * If we support detecting hardlockups, touch_nmi_watchdog() may be 133 * used to pet the watchdog (reset the timeout) - for code which 134 * intentionally disables interrupts for a long time. This call is stateless. 135 * 136 * Though this function has "nmi" in the name, the hardlockup watchdog might 137 * not be backed by NMIs. This function will likely be renamed to 138 * touch_hardlockup_watchdog() in the future. 139 */ 140 static inline void touch_nmi_watchdog(void) 141 { 142 /* 143 * Pass on to the hardlockup detector selected via CONFIG_. Note that 144 * the hardlockup detector may not be arch-specific nor using NMIs 145 * and the arch_touch_nmi_watchdog() function will likely be renamed 146 * in the future. 147 */ 148 arch_touch_nmi_watchdog(); 149 150 /* 151 * Touching the hardlock detector implicitly resets the 152 * softlockup detector too 153 */ 154 touch_softlockup_watchdog(); 155 } 156 157 /* 158 * Create trigger_all_cpu_backtrace() out of the arch-provided 159 * base function. Return whether such support was available, 160 * to allow calling code to fall back to some other mechanism: 161 */ 162 #ifdef arch_trigger_cpumask_backtrace 163 static inline bool trigger_all_cpu_backtrace(void) 164 { 165 arch_trigger_cpumask_backtrace(cpu_online_mask, false); 166 return true; 167 } 168 169 static inline bool trigger_allbutself_cpu_backtrace(void) 170 { 171 arch_trigger_cpumask_backtrace(cpu_online_mask, true); 172 return true; 173 } 174 175 static inline bool trigger_cpumask_backtrace(struct cpumask *mask) 176 { 177 arch_trigger_cpumask_backtrace(mask, false); 178 return true; 179 } 180 181 static inline bool trigger_single_cpu_backtrace(int cpu) 182 { 183 arch_trigger_cpumask_backtrace(cpumask_of(cpu), false); 184 return true; 185 } 186 187 /* generic implementation */ 188 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask, 189 bool exclude_self, 190 void (*raise)(cpumask_t *mask)); 191 bool nmi_cpu_backtrace(struct pt_regs *regs); 192 193 #else 194 static inline bool trigger_all_cpu_backtrace(void) 195 { 196 return false; 197 } 198 static inline bool trigger_allbutself_cpu_backtrace(void) 199 { 200 return false; 201 } 202 static inline bool trigger_cpumask_backtrace(struct cpumask *mask) 203 { 204 return false; 205 } 206 static inline bool trigger_single_cpu_backtrace(int cpu) 207 { 208 return false; 209 } 210 #endif 211 212 #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF 213 u64 hw_nmi_get_sample_period(int watchdog_thresh); 214 #endif 215 216 #if defined(CONFIG_HARDLOCKUP_CHECK_TIMESTAMP) && \ 217 defined(CONFIG_HARDLOCKUP_DETECTOR_PERF) 218 void watchdog_update_hrtimer_threshold(u64 period); 219 #else 220 static inline void watchdog_update_hrtimer_threshold(u64 period) { } 221 #endif 222 223 struct ctl_table; 224 int proc_watchdog(struct ctl_table *, int, void *, size_t *, loff_t *); 225 int proc_nmi_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *); 226 int proc_soft_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *); 227 int proc_watchdog_thresh(struct ctl_table *, int , void *, size_t *, loff_t *); 228 int proc_watchdog_cpumask(struct ctl_table *, int, void *, size_t *, loff_t *); 229 230 #ifdef CONFIG_HAVE_ACPI_APEI_NMI 231 #include <asm/nmi.h> 232 #endif 233 234 #ifdef CONFIG_NMI_CHECK_CPU 235 void nmi_backtrace_stall_snap(const struct cpumask *btp); 236 void nmi_backtrace_stall_check(const struct cpumask *btp); 237 #else 238 static inline void nmi_backtrace_stall_snap(const struct cpumask *btp) {} 239 static inline void nmi_backtrace_stall_check(const struct cpumask *btp) {} 240 #endif 241 242 #endif 243