Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Detect hard lockups on a system |
| 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> |
Don Zickus | 42f930d | 2017-11-01 14:11:27 -0400 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 17 | #include <linux/module.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 18 | #include <linux/sched/debug.h> |
| 19 | |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 20 | #include <asm/irq_regs.h> |
| 21 | #include <linux/perf_event.h> |
| 22 | |
| 23 | static DEFINE_PER_CPU(bool, hard_watchdog_warn); |
| 24 | static DEFINE_PER_CPU(bool, watchdog_nmi_touch); |
| 25 | static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); |
Thomas Gleixner | 9c388a5 | 2017-10-31 22:32:00 +0100 | [diff] [blame] | 26 | static DEFINE_PER_CPU(struct perf_event *, dead_event); |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 27 | static struct cpumask dead_events_mask; |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 28 | |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 29 | static unsigned long hardlockup_allcpu_dumped; |
Don Zickus | 42f930d | 2017-11-01 14:11:27 -0400 | [diff] [blame] | 30 | static atomic_t watchdog_cpus = ATOMIC_INIT(0); |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 31 | |
Nicholas Piggin | f2e0cff | 2017-07-12 14:35:43 -0700 | [diff] [blame] | 32 | void arch_touch_nmi_watchdog(void) |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 33 | { |
| 34 | /* |
| 35 | * Using __raw here because some code paths have |
| 36 | * preemption enabled. If preemption is enabled |
| 37 | * then interrupts should be enabled too, in which |
| 38 | * case we shouldn't have to worry about the watchdog |
| 39 | * going off. |
| 40 | */ |
| 41 | raw_cpu_write(watchdog_nmi_touch, true); |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 42 | } |
Nicholas Piggin | f2e0cff | 2017-07-12 14:35:43 -0700 | [diff] [blame] | 43 | EXPORT_SYMBOL(arch_touch_nmi_watchdog); |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 44 | |
Thomas Gleixner | 7edaeb6 | 2017-08-15 09:50:13 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP |
| 46 | static DEFINE_PER_CPU(ktime_t, last_timestamp); |
| 47 | static DEFINE_PER_CPU(unsigned int, nmi_rearmed); |
| 48 | static ktime_t watchdog_hrtimer_sample_threshold __read_mostly; |
| 49 | |
| 50 | void watchdog_update_hrtimer_threshold(u64 period) |
| 51 | { |
| 52 | /* |
| 53 | * The hrtimer runs with a period of (watchdog_threshold * 2) / 5 |
| 54 | * |
| 55 | * So it runs effectively with 2.5 times the rate of the NMI |
| 56 | * watchdog. That means the hrtimer should fire 2-3 times before |
| 57 | * the NMI watchdog expires. The NMI watchdog on x86 is based on |
| 58 | * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles |
| 59 | * might run way faster than expected and the NMI fires in a |
| 60 | * smaller period than the one deduced from the nominal CPU |
| 61 | * frequency. Depending on the Turbo-Mode factor this might be fast |
| 62 | * enough to get the NMI period smaller than the hrtimer watchdog |
| 63 | * period and trigger false positives. |
| 64 | * |
| 65 | * The sample threshold is used to check in the NMI handler whether |
| 66 | * the minimum time between two NMI samples has elapsed. That |
| 67 | * prevents false positives. |
| 68 | * |
| 69 | * Set this to 4/5 of the actual watchdog threshold period so the |
| 70 | * hrtimer is guaranteed to fire at least once within the real |
| 71 | * watchdog threshold. |
| 72 | */ |
| 73 | watchdog_hrtimer_sample_threshold = period * 2; |
| 74 | } |
| 75 | |
| 76 | static bool watchdog_check_timestamp(void) |
| 77 | { |
| 78 | ktime_t delta, now = ktime_get_mono_fast_ns(); |
| 79 | |
| 80 | delta = now - __this_cpu_read(last_timestamp); |
| 81 | if (delta < watchdog_hrtimer_sample_threshold) { |
| 82 | /* |
| 83 | * If ktime is jiffies based, a stalled timer would prevent |
| 84 | * jiffies from being incremented and the filter would look |
| 85 | * at a stale timestamp and never trigger. |
| 86 | */ |
| 87 | if (__this_cpu_inc_return(nmi_rearmed) < 10) |
| 88 | return false; |
| 89 | } |
| 90 | __this_cpu_write(nmi_rearmed, 0); |
| 91 | __this_cpu_write(last_timestamp, now); |
| 92 | return true; |
| 93 | } |
| 94 | #else |
| 95 | static inline bool watchdog_check_timestamp(void) |
| 96 | { |
| 97 | return true; |
| 98 | } |
| 99 | #endif |
| 100 | |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 101 | static struct perf_event_attr wd_hw_attr = { |
| 102 | .type = PERF_TYPE_HARDWARE, |
| 103 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 104 | .size = sizeof(struct perf_event_attr), |
| 105 | .pinned = 1, |
| 106 | .disabled = 1, |
| 107 | }; |
| 108 | |
| 109 | /* Callback function for perf event subsystem */ |
| 110 | static void watchdog_overflow_callback(struct perf_event *event, |
Thomas Gleixner | 01f0a02 | 2017-09-12 21:37:05 +0200 | [diff] [blame] | 111 | struct perf_sample_data *data, |
| 112 | struct pt_regs *regs) |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 113 | { |
| 114 | /* Ensure the watchdog never gets throttled */ |
| 115 | event->hw.interrupts = 0; |
| 116 | |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 117 | if (__this_cpu_read(watchdog_nmi_touch) == true) { |
| 118 | __this_cpu_write(watchdog_nmi_touch, false); |
| 119 | return; |
| 120 | } |
| 121 | |
Thomas Gleixner | 7edaeb6 | 2017-08-15 09:50:13 +0200 | [diff] [blame] | 122 | if (!watchdog_check_timestamp()) |
| 123 | return; |
| 124 | |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 125 | /* check for a hardlockup |
| 126 | * This is done by making sure our timer interrupt |
| 127 | * is incrementing. The timer interrupt should have |
| 128 | * fired multiple times before we overflow'd. If it hasn't |
| 129 | * then this is a good indication the cpu is stuck |
| 130 | */ |
| 131 | if (is_hardlockup()) { |
| 132 | int this_cpu = smp_processor_id(); |
| 133 | |
| 134 | /* only print hardlockups once */ |
| 135 | if (__this_cpu_read(hard_watchdog_warn) == true) |
| 136 | return; |
| 137 | |
| 138 | pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu); |
| 139 | print_modules(); |
| 140 | print_irqtrace_events(current); |
| 141 | if (regs) |
| 142 | show_regs(regs); |
| 143 | else |
| 144 | dump_stack(); |
| 145 | |
| 146 | /* |
| 147 | * Perform all-CPU dump only once to avoid multiple hardlockups |
| 148 | * generating interleaving traces |
| 149 | */ |
| 150 | if (sysctl_hardlockup_all_cpu_backtrace && |
| 151 | !test_and_set_bit(0, &hardlockup_allcpu_dumped)) |
| 152 | trigger_allbutself_cpu_backtrace(); |
| 153 | |
| 154 | if (hardlockup_panic) |
| 155 | nmi_panic(regs, "Hard LOCKUP"); |
| 156 | |
| 157 | __this_cpu_write(hard_watchdog_warn, true); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | __this_cpu_write(hard_watchdog_warn, false); |
| 162 | return; |
| 163 | } |
| 164 | |
Thomas Gleixner | 178b9f7 | 2017-09-12 21:37:18 +0200 | [diff] [blame] | 165 | static int hardlockup_detector_event_create(void) |
| 166 | { |
| 167 | unsigned int cpu = smp_processor_id(); |
| 168 | struct perf_event_attr *wd_attr; |
| 169 | struct perf_event *evt; |
| 170 | |
| 171 | wd_attr = &wd_hw_attr; |
| 172 | wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh); |
| 173 | |
| 174 | /* Try to register using hardware perf events */ |
| 175 | evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL, |
| 176 | watchdog_overflow_callback, NULL); |
| 177 | if (IS_ERR(evt)) { |
| 178 | pr_info("Perf event create on CPU %d failed with %ld\n", cpu, |
| 179 | PTR_ERR(evt)); |
| 180 | return PTR_ERR(evt); |
| 181 | } |
| 182 | this_cpu_write(watchdog_ev, evt); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 186 | /** |
Thomas Gleixner | 2a1b8ee | 2017-09-12 21:37:20 +0200 | [diff] [blame] | 187 | * hardlockup_detector_perf_enable - Enable the local event |
| 188 | */ |
| 189 | void hardlockup_detector_perf_enable(void) |
| 190 | { |
| 191 | if (hardlockup_detector_event_create()) |
| 192 | return; |
| 193 | |
Don Zickus | 42f930d | 2017-11-01 14:11:27 -0400 | [diff] [blame] | 194 | /* use original value for check */ |
| 195 | if (!atomic_fetch_inc(&watchdog_cpus)) |
Thomas Gleixner | 146c9d0 | 2017-09-12 21:37:21 +0200 | [diff] [blame] | 196 | pr_info("Enabled. Permanently consumes one hw-PMU counter.\n"); |
| 197 | |
Thomas Gleixner | 2a1b8ee | 2017-09-12 21:37:20 +0200 | [diff] [blame] | 198 | perf_event_enable(this_cpu_read(watchdog_ev)); |
| 199 | } |
| 200 | |
| 201 | /** |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 202 | * hardlockup_detector_perf_disable - Disable the local event |
| 203 | */ |
| 204 | void hardlockup_detector_perf_disable(void) |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 205 | { |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 206 | struct perf_event *event = this_cpu_read(watchdog_ev); |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 207 | |
| 208 | if (event) { |
| 209 | perf_event_disable(event); |
Thomas Gleixner | 9c388a5 | 2017-10-31 22:32:00 +0100 | [diff] [blame] | 210 | this_cpu_write(watchdog_ev, NULL); |
| 211 | this_cpu_write(dead_event, event); |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 212 | cpumask_set_cpu(smp_processor_id(), &dead_events_mask); |
Don Zickus | 42f930d | 2017-11-01 14:11:27 -0400 | [diff] [blame] | 213 | atomic_dec(&watchdog_cpus); |
Babu Moger | 73ce051 | 2016-12-14 15:06:24 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
Peter Zijlstra | d0b6e0a | 2017-09-12 21:36:55 +0200 | [diff] [blame] | 216 | |
| 217 | /** |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 218 | * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them |
| 219 | * |
| 220 | * Called from lockup_detector_cleanup(). Serialized by the caller. |
| 221 | */ |
| 222 | void hardlockup_detector_perf_cleanup(void) |
| 223 | { |
| 224 | int cpu; |
| 225 | |
| 226 | for_each_cpu(cpu, &dead_events_mask) { |
Thomas Gleixner | 9c388a5 | 2017-10-31 22:32:00 +0100 | [diff] [blame] | 227 | struct perf_event *event = per_cpu(dead_event, cpu); |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 228 | |
Thomas Gleixner | 115ef3b | 2017-09-25 20:21:54 +0200 | [diff] [blame] | 229 | /* |
| 230 | * Required because for_each_cpu() reports unconditionally |
| 231 | * CPU0 as set on UP kernels. Sigh. |
| 232 | */ |
| 233 | if (event) |
| 234 | perf_event_release_kernel(event); |
Thomas Gleixner | 9c388a5 | 2017-10-31 22:32:00 +0100 | [diff] [blame] | 235 | per_cpu(dead_event, cpu) = NULL; |
Thomas Gleixner | 941154b | 2017-09-12 21:37:04 +0200 | [diff] [blame] | 236 | } |
| 237 | cpumask_clear(&dead_events_mask); |
| 238 | } |
| 239 | |
| 240 | /** |
Peter Zijlstra | d0b6e0a | 2017-09-12 21:36:55 +0200 | [diff] [blame] | 241 | * hardlockup_detector_perf_stop - Globally stop watchdog events |
| 242 | * |
| 243 | * Special interface for x86 to handle the perf HT bug. |
| 244 | */ |
| 245 | void __init hardlockup_detector_perf_stop(void) |
| 246 | { |
| 247 | int cpu; |
| 248 | |
| 249 | lockdep_assert_cpus_held(); |
| 250 | |
| 251 | for_each_online_cpu(cpu) { |
| 252 | struct perf_event *event = per_cpu(watchdog_ev, cpu); |
| 253 | |
| 254 | if (event) |
| 255 | perf_event_disable(event); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * hardlockup_detector_perf_restart - Globally restart watchdog events |
| 261 | * |
| 262 | * Special interface for x86 to handle the perf HT bug. |
| 263 | */ |
| 264 | void __init hardlockup_detector_perf_restart(void) |
| 265 | { |
| 266 | int cpu; |
| 267 | |
| 268 | lockdep_assert_cpus_held(); |
| 269 | |
| 270 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) |
| 271 | return; |
| 272 | |
| 273 | for_each_online_cpu(cpu) { |
| 274 | struct perf_event *event = per_cpu(watchdog_ev, cpu); |
| 275 | |
| 276 | if (event) |
| 277 | perf_event_enable(event); |
| 278 | } |
| 279 | } |
Thomas Gleixner | 178b9f7 | 2017-09-12 21:37:18 +0200 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * hardlockup_detector_perf_init - Probe whether NMI event is available at all |
| 283 | */ |
| 284 | int __init hardlockup_detector_perf_init(void) |
| 285 | { |
| 286 | int ret = hardlockup_detector_event_create(); |
| 287 | |
| 288 | if (ret) { |
Colin Ian King | 77c01d1 | 2017-09-26 10:36:03 +0100 | [diff] [blame] | 289 | pr_info("Perf NMI watchdog permanently disabled\n"); |
Thomas Gleixner | 178b9f7 | 2017-09-12 21:37:18 +0200 | [diff] [blame] | 290 | } else { |
| 291 | perf_event_release_kernel(this_cpu_read(watchdog_ev)); |
| 292 | this_cpu_write(watchdog_ev, NULL); |
| 293 | } |
| 294 | return ret; |
| 295 | } |