blob: 8ba4b269ab89c8f2c4fa255026761a3ac2573b81 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
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>
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
23static DEFINE_PER_CPU(bool, hard_watchdog_warn);
24static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
25static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
26static DEFINE_PER_CPU(struct perf_event *, dead_event);
27static struct cpumask dead_events_mask;
28
29static unsigned long hardlockup_allcpu_dumped;
30static atomic_t watchdog_cpus = ATOMIC_INIT(0);
31
32notrace void arch_touch_nmi_watchdog(void)
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);
42}
43EXPORT_SYMBOL(arch_touch_nmi_watchdog);
44
45#ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
46static DEFINE_PER_CPU(ktime_t, last_timestamp);
47static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
48static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
49
50void 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
76static 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
95static void watchdog_init_timestamp(void)
96{
97 __this_cpu_write(nmi_rearmed, 0);
98 __this_cpu_write(last_timestamp, ktime_get_mono_fast_ns());
99}
100#else
101static inline bool watchdog_check_timestamp(void) { return true; }
102static inline void watchdog_init_timestamp(void) { }
103#endif
104
105static struct perf_event_attr wd_hw_attr = {
106 .type = PERF_TYPE_HARDWARE,
107 .config = PERF_COUNT_HW_CPU_CYCLES,
108 .size = sizeof(struct perf_event_attr),
109 .pinned = 1,
110 .disabled = 1,
111};
112
113/* Callback function for perf event subsystem */
114static void watchdog_overflow_callback(struct perf_event *event,
115 struct perf_sample_data *data,
116 struct pt_regs *regs)
117{
118 /* Ensure the watchdog never gets throttled */
119 event->hw.interrupts = 0;
120
121 if (!watchdog_check_timestamp())
122 return;
123
124 if (__this_cpu_read(watchdog_nmi_touch) == true) {
125 __this_cpu_write(watchdog_nmi_touch, false);
126 return;
127 }
128
129 /* check for a hardlockup
130 * This is done by making sure our timer interrupt
131 * is incrementing. The timer interrupt should have
132 * fired multiple times before we overflow'd. If it hasn't
133 * then this is a good indication the cpu is stuck
134 */
135 if (is_hardlockup()) {
136 int this_cpu = smp_processor_id();
137
138 /* only print hardlockups once */
139 if (__this_cpu_read(hard_watchdog_warn) == true)
140 return;
141
142 pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n",
143 this_cpu);
144 print_modules();
145 print_irqtrace_events(current);
146 if (regs)
147 show_regs(regs);
148 else
149 dump_stack();
150
151 /*
152 * Perform all-CPU dump only once to avoid multiple hardlockups
153 * generating interleaving traces
154 */
155 if (sysctl_hardlockup_all_cpu_backtrace &&
156 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
157 trigger_allbutself_cpu_backtrace();
158
159 if (hardlockup_panic)
160 nmi_panic(regs, "Hard LOCKUP");
161
162 __this_cpu_write(hard_watchdog_warn, true);
163 return;
164 }
165
166 __this_cpu_write(hard_watchdog_warn, false);
167 return;
168}
169
170static int hardlockup_detector_event_create(void)
171{
172 unsigned int cpu = smp_processor_id();
173 struct perf_event_attr *wd_attr;
174 struct perf_event *evt;
175
176 wd_attr = &wd_hw_attr;
177 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
178
179 /* Try to register using hardware perf events */
180 evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
181 watchdog_overflow_callback, NULL);
182 if (IS_ERR(evt)) {
183 pr_debug("Perf event create on CPU %d failed with %ld\n", cpu,
184 PTR_ERR(evt));
185 return PTR_ERR(evt);
186 }
187 this_cpu_write(watchdog_ev, evt);
188 return 0;
189}
190
191/**
192 * hardlockup_detector_perf_enable - Enable the local event
193 */
194void hardlockup_detector_perf_enable(void)
195{
196 if (hardlockup_detector_event_create())
197 return;
198
199 /* use original value for check */
200 if (!atomic_fetch_inc(&watchdog_cpus))
201 pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
202
203 watchdog_init_timestamp();
204 perf_event_enable(this_cpu_read(watchdog_ev));
205}
206
207/**
208 * hardlockup_detector_perf_disable - Disable the local event
209 */
210void hardlockup_detector_perf_disable(void)
211{
212 struct perf_event *event = this_cpu_read(watchdog_ev);
213
214 if (event) {
215 perf_event_disable(event);
216 this_cpu_write(watchdog_ev, NULL);
217 this_cpu_write(dead_event, event);
218 cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
219 atomic_dec(&watchdog_cpus);
220 }
221}
222
223/**
224 * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them
225 *
226 * Called from lockup_detector_cleanup(). Serialized by the caller.
227 */
228void hardlockup_detector_perf_cleanup(void)
229{
230 int cpu;
231
232 for_each_cpu(cpu, &dead_events_mask) {
233 struct perf_event *event = per_cpu(dead_event, cpu);
234
235 /*
236 * Required because for_each_cpu() reports unconditionally
237 * CPU0 as set on UP kernels. Sigh.
238 */
239 if (event)
240 perf_event_release_kernel(event);
241 per_cpu(dead_event, cpu) = NULL;
242 }
243 cpumask_clear(&dead_events_mask);
244}
245
246/**
247 * hardlockup_detector_perf_stop - Globally stop watchdog events
248 *
249 * Special interface for x86 to handle the perf HT bug.
250 */
251void __init hardlockup_detector_perf_stop(void)
252{
253 int cpu;
254
255 lockdep_assert_cpus_held();
256
257 for_each_online_cpu(cpu) {
258 struct perf_event *event = per_cpu(watchdog_ev, cpu);
259
260 if (event)
261 perf_event_disable(event);
262 }
263}
264
265/**
266 * hardlockup_detector_perf_restart - Globally restart watchdog events
267 *
268 * Special interface for x86 to handle the perf HT bug.
269 */
270void __init hardlockup_detector_perf_restart(void)
271{
272 int cpu;
273
274 lockdep_assert_cpus_held();
275
276 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
277 return;
278
279 for_each_online_cpu(cpu) {
280 struct perf_event *event = per_cpu(watchdog_ev, cpu);
281
282 if (event)
283 perf_event_enable(event);
284 }
285}
286
287/**
288 * hardlockup_detector_perf_init - Probe whether NMI event is available at all
289 */
290int __init hardlockup_detector_perf_init(void)
291{
292 int ret = hardlockup_detector_event_create();
293
294 if (ret) {
295 pr_info("Perf NMI watchdog permanently disabled\n");
296 } else {
297 perf_event_release_kernel(this_cpu_read(watchdog_ev));
298 this_cpu_write(watchdog_ev, NULL);
299 }
300 return ret;
301}