1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Detect hard lockups on a system using perf 4 * 5 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc. 6 * 7 * Note: Most of this code is borrowed heavily from the original softlockup 8 * detector, so thanks to Ingo for the initial implementation. 9 * Some chunks also taken from the old x86-specific nmi watchdog code, thanks 10 * to those contributors as well. 11 */ 12 13 #define pr_fmt(fmt) "NMI watchdog: " fmt 14 15 #include <linux/nmi.h> 16 #include <linux/atomic.h> 17 #include <linux/module.h> 18 #include <linux/sched/debug.h> 19 20 #include <asm/irq_regs.h> 21 #include <linux/perf_event.h> 22 23 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); 24 25 static atomic_t watchdog_cpus = ATOMIC_INIT(0); 26 27 #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP 28 static DEFINE_PER_CPU(ktime_t, last_timestamp); 29 static DEFINE_PER_CPU(unsigned int, nmi_rearmed); 30 static ktime_t watchdog_hrtimer_sample_threshold __read_mostly; 31 32 void watchdog_update_hrtimer_threshold(u64 period) 33 { 34 /* 35 * The hrtimer runs with a period of (watchdog_threshold * 2) / 5 36 * 37 * So it runs effectively with 2.5 times the rate of the NMI 38 * watchdog. That means the hrtimer should fire 2-3 times before 39 * the NMI watchdog expires. The NMI watchdog on x86 is based on 40 * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles 41 * might run way faster than expected and the NMI fires in a 42 * smaller period than the one deduced from the nominal CPU 43 * frequency. Depending on the Turbo-Mode factor this might be fast 44 * enough to get the NMI period smaller than the hrtimer watchdog 45 * period and trigger false positives. 46 * 47 * The sample threshold is used to check in the NMI handler whether 48 * the minimum time between two NMI samples has elapsed. That 49 * prevents false positives. 50 * 51 * Set this to 4/5 of the actual watchdog threshold period so the 52 * hrtimer is guaranteed to fire at least once within the real 53 * watchdog threshold. 54 */ 55 watchdog_hrtimer_sample_threshold = period * 2; 56 } 57 58 static bool watchdog_check_timestamp(void) 59 { 60 ktime_t delta, now = ktime_get_mono_fast_ns(); 61 62 delta = now - __this_cpu_read(last_timestamp); 63 if (delta < watchdog_hrtimer_sample_threshold) { 64 /* 65 * If ktime is jiffies based, a stalled timer would prevent 66 * jiffies from being incremented and the filter would look 67 * at a stale timestamp and never trigger. 68 */ 69 if (__this_cpu_inc_return(nmi_rearmed) < 10) 70 return false; 71 } 72 __this_cpu_write(nmi_rearmed, 0); 73 __this_cpu_write(last_timestamp, now); 74 return true; 75 } 76 77 static void watchdog_init_timestamp(void) 78 { 79 __this_cpu_write(nmi_rearmed, 0); 80 __this_cpu_write(last_timestamp, ktime_get_mono_fast_ns()); 81 } 82 #else 83 static inline bool watchdog_check_timestamp(void) { return true; } 84 static inline void watchdog_init_timestamp(void) { } 85 #endif 86 87 static struct perf_event_attr wd_hw_attr = { 88 .type = PERF_TYPE_HARDWARE, 89 .config = PERF_COUNT_HW_CPU_CYCLES, 90 .size = sizeof(struct perf_event_attr), 91 .pinned = 1, 92 .disabled = 1, 93 }; 94 95 static struct perf_event_attr fallback_wd_hw_attr = { 96 .type = PERF_TYPE_HARDWARE, 97 .config = PERF_COUNT_HW_CPU_CYCLES, 98 .size = sizeof(struct perf_event_attr), 99 .pinned = 1, 100 .disabled = 1, 101 }; 102 103 /* Callback function for perf event subsystem */ 104 static void watchdog_overflow_callback(struct perf_event *event, 105 struct perf_sample_data *data, 106 struct pt_regs *regs) 107 { 108 /* Ensure the watchdog never gets throttled */ 109 event->hw.interrupts = 0; 110 111 if (!watchdog_check_timestamp()) 112 return; 113 114 watchdog_hardlockup_check(smp_processor_id(), regs); 115 } 116 117 static int hardlockup_detector_event_create(void) 118 { 119 unsigned int cpu; 120 struct perf_event_attr *wd_attr; 121 struct perf_event *evt; 122 123 /* 124 * Preemption is not disabled because memory will be allocated. 125 * Ensure CPU-locality by calling this in per-CPU kthread. 126 */ 127 WARN_ON(!is_percpu_thread()); 128 cpu = raw_smp_processor_id(); 129 wd_attr = &wd_hw_attr; 130 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); 131 132 /* Try to register using hardware perf events */ 133 evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL, 134 watchdog_overflow_callback, NULL); 135 if (IS_ERR(evt)) { 136 wd_attr = &fallback_wd_hw_attr; 137 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); 138 evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL, 139 watchdog_overflow_callback, NULL); 140 } 141 142 if (IS_ERR(evt)) { 143 pr_debug("Perf event create on CPU %d failed with %ld\n", cpu, 144 PTR_ERR(evt)); 145 return PTR_ERR(evt); 146 } 147 WARN_ONCE(this_cpu_read(watchdog_ev), "unexpected watchdog_ev leak"); 148 this_cpu_write(watchdog_ev, evt); 149 return 0; 150 } 151 152 /** 153 * watchdog_hardlockup_enable - Enable the local event 154 * @cpu: The CPU to enable hard lockup on. 155 */ 156 void watchdog_hardlockup_enable(unsigned int cpu) 157 { 158 WARN_ON_ONCE(cpu != smp_processor_id()); 159 160 if (hardlockup_detector_event_create()) 161 return; 162 163 /* use original value for check */ 164 if (!atomic_fetch_inc(&watchdog_cpus)) 165 pr_info("Enabled. Permanently consumes one hw-PMU counter.\n"); 166 167 watchdog_init_timestamp(); 168 perf_event_enable(this_cpu_read(watchdog_ev)); 169 } 170 171 /** 172 * watchdog_hardlockup_disable - Disable the local event 173 * @cpu: The CPU to enable hard lockup on. 174 */ 175 void watchdog_hardlockup_disable(unsigned int cpu) 176 { 177 struct perf_event *event = this_cpu_read(watchdog_ev); 178 179 WARN_ON_ONCE(cpu != smp_processor_id()); 180 181 if (event) { 182 perf_event_disable(event); 183 perf_event_release_kernel(event); 184 this_cpu_write(watchdog_ev, NULL); 185 atomic_dec(&watchdog_cpus); 186 } 187 } 188 189 /** 190 * hardlockup_detector_perf_adjust_period - Adjust the event period due 191 * to current cpu frequency change 192 * @period: The target period to be set 193 */ 194 void hardlockup_detector_perf_adjust_period(u64 period) 195 { 196 struct perf_event *event = this_cpu_read(watchdog_ev); 197 198 if (!(watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)) 199 return; 200 201 if (!event) 202 return; 203 204 if (event->attr.sample_period == period) 205 return; 206 207 if (perf_event_period(event, period)) 208 pr_err("failed to change period to %llu\n", period); 209 } 210 211 /** 212 * hardlockup_detector_perf_stop - Globally stop watchdog events 213 * 214 * Special interface for x86 to handle the perf HT bug. 215 */ 216 void __init hardlockup_detector_perf_stop(void) 217 { 218 int cpu; 219 220 lockdep_assert_cpus_held(); 221 222 for_each_online_cpu(cpu) { 223 struct perf_event *event = per_cpu(watchdog_ev, cpu); 224 225 if (event) 226 perf_event_disable(event); 227 } 228 } 229 230 /** 231 * hardlockup_detector_perf_restart - Globally restart watchdog events 232 * 233 * Special interface for x86 to handle the perf HT bug. 234 */ 235 void __init hardlockup_detector_perf_restart(void) 236 { 237 int cpu; 238 239 lockdep_assert_cpus_held(); 240 241 if (!(watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)) 242 return; 243 244 for_each_online_cpu(cpu) { 245 struct perf_event *event = per_cpu(watchdog_ev, cpu); 246 247 if (event) 248 perf_event_enable(event); 249 } 250 } 251 252 bool __weak __init arch_perf_nmi_is_available(void) 253 { 254 return true; 255 } 256 257 /** 258 * watchdog_hardlockup_probe - Probe whether NMI event is available at all 259 */ 260 int __init watchdog_hardlockup_probe(void) 261 { 262 int ret; 263 264 if (!arch_perf_nmi_is_available()) 265 return -ENODEV; 266 267 ret = hardlockup_detector_event_create(); 268 269 if (ret) { 270 pr_info("Perf NMI watchdog permanently disabled\n"); 271 } else { 272 perf_event_release_kernel(this_cpu_read(watchdog_ev)); 273 this_cpu_write(watchdog_ev, NULL); 274 } 275 return ret; 276 } 277 278 /** 279 * hardlockup_config_perf_event - Overwrite config of wd_hw_attr. 280 * @str: number which identifies the raw perf event to use 281 */ 282 void __init hardlockup_config_perf_event(const char *str) 283 { 284 u64 config; 285 char buf[24]; 286 char *comma = strchr(str, ','); 287 288 if (!comma) { 289 if (kstrtoull(str, 16, &config)) 290 return; 291 } else { 292 unsigned int len = comma - str; 293 294 if (len > sizeof(buf)) 295 return; 296 297 strscpy(buf, str, len); 298 if (kstrtoull(buf, 16, &config)) 299 return; 300 } 301 302 wd_hw_attr.type = PERF_TYPE_RAW; 303 wd_hw_attr.config = config; 304 } 305