watchdog/hardlockup: rename some "NMI watchdog" constants/function
[linux-2.6-block.git] / kernel / watchdog.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
58687acb
DZ
2/*
3 * Detect hard and soft lockups on a system
4 *
5 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
6 *
86f5e6a7
FLVC
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
58687acb
DZ
10 * to those contributors as well.
11 */
12
5f92a7b0 13#define pr_fmt(fmt) "watchdog: " fmt
4501980a 14
58687acb
DZ
15#include <linux/mm.h>
16#include <linux/cpu.h>
17#include <linux/nmi.h>
18#include <linux/init.h>
58687acb
DZ
19#include <linux/module.h>
20#include <linux/sysctl.h>
fe4ba3c3 21#include <linux/tick.h>
e6017571 22#include <linux/sched/clock.h>
b17b0153 23#include <linux/sched/debug.h>
78634061 24#include <linux/sched/isolation.h>
9cf57731 25#include <linux/stop_machine.h>
58687acb
DZ
26
27#include <asm/irq_regs.h>
5d1c0f4a 28#include <linux/kvm_para.h>
58687acb 29
946d1977 30static DEFINE_MUTEX(watchdog_mutex);
ab992dc3 31
05a4a952 32#if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
df95d308 33# define WATCHDOG_HARDLOCKUP_DEFAULT 1
84d56e66 34#else
df95d308 35# define WATCHDOG_HARDLOCKUP_DEFAULT 0
84d56e66 36#endif
05a4a952 37
09154985
TG
38unsigned long __read_mostly watchdog_enabled;
39int __read_mostly watchdog_user_enabled = 1;
df95d308
DA
40static int __read_mostly watchdog_hardlockup_user_enabled = WATCHDOG_HARDLOCKUP_DEFAULT;
41static int __read_mostly watchdog_softlockup_user_enabled = 1;
7feeb9cd 42int __read_mostly watchdog_thresh = 10;
df95d308 43static int __read_mostly watchdog_hardlockup_available;
7feeb9cd 44
7feeb9cd
TG
45struct cpumask watchdog_cpumask __read_mostly;
46unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
47
05a4a952 48#ifdef CONFIG_HARDLOCKUP_DETECTOR
f117955a
GP
49
50# ifdef CONFIG_SMP
51int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
52# endif /* CONFIG_SMP */
53
05a4a952
NP
54/*
55 * Should we panic when a soft-lockup or hard-lockup occurs:
56 */
57unsigned int __read_mostly hardlockup_panic =
67fca000 58 IS_ENABLED(CONFIG_BOOTPARAM_HARDLOCKUP_PANIC);
05a4a952
NP
59/*
60 * We may not want to enable hard lockup detection by default in all cases,
61 * for example when running the kernel as a guest on a hypervisor. In these
62 * cases this function can be called to disable hard lockup detection. This
63 * function should only be executed once by the boot processor before the
64 * kernel command line parameters are parsed, because otherwise it is not
65 * possible to override this in hardlockup_panic_setup().
66 */
7a355820 67void __init hardlockup_detector_disable(void)
05a4a952 68{
df95d308 69 watchdog_hardlockup_user_enabled = 0;
05a4a952
NP
70}
71
72static int __init hardlockup_panic_setup(char *str)
73{
74 if (!strncmp(str, "panic", 5))
75 hardlockup_panic = 1;
76 else if (!strncmp(str, "nopanic", 7))
77 hardlockup_panic = 0;
78 else if (!strncmp(str, "0", 1))
df95d308 79 watchdog_hardlockup_user_enabled = 0;
05a4a952 80 else if (!strncmp(str, "1", 1))
df95d308 81 watchdog_hardlockup_user_enabled = 1;
05a4a952
NP
82 return 1;
83}
84__setup("nmi_watchdog=", hardlockup_panic_setup);
85
368a7e2c 86#endif /* CONFIG_HARDLOCKUP_DETECTOR */
05a4a952 87
81972551
DA
88#if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF)
89
77c12fc9
DA
90static DEFINE_PER_CPU(atomic_t, hrtimer_interrupts);
91static DEFINE_PER_CPU(int, hrtimer_interrupts_saved);
1610611a 92static DEFINE_PER_CPU(bool, watchdog_hardlockup_warned);
ed92e1ef 93static DEFINE_PER_CPU(bool, watchdog_hardlockup_touched);
1610611a 94static unsigned long watchdog_hardlockup_all_cpu_dumped;
81972551 95
ed92e1ef
DA
96notrace void arch_touch_nmi_watchdog(void)
97{
98 /*
99 * Using __raw here because some code paths have
100 * preemption enabled. If preemption is enabled
101 * then interrupts should be enabled too, in which
102 * case we shouldn't have to worry about the watchdog
103 * going off.
104 */
105 raw_cpu_write(watchdog_hardlockup_touched, true);
106}
107EXPORT_SYMBOL(arch_touch_nmi_watchdog);
108
77c12fc9 109static bool is_hardlockup(unsigned int cpu)
81972551 110{
77c12fc9 111 int hrint = atomic_read(&per_cpu(hrtimer_interrupts, cpu));
81972551 112
77c12fc9 113 if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
81972551
DA
114 return true;
115
77c12fc9
DA
116 /*
117 * NOTE: we don't need any fancy atomic_t or READ_ONCE/WRITE_ONCE
118 * for hrtimer_interrupts_saved. hrtimer_interrupts_saved is
119 * written/read by a single CPU.
120 */
121 per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
1610611a 122
81972551
DA
123 return false;
124}
125
126static void watchdog_hardlockup_kick(void)
127{
77c12fc9 128 atomic_inc(raw_cpu_ptr(&hrtimer_interrupts));
81972551
DA
129}
130
77c12fc9 131void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs)
81972551 132{
ed92e1ef
DA
133 if (per_cpu(watchdog_hardlockup_touched, cpu)) {
134 per_cpu(watchdog_hardlockup_touched, cpu) = false;
135 return;
136 }
137
1610611a
DA
138 /*
139 * Check for a hardlockup by making sure the CPU's timer
140 * interrupt is incrementing. The timer interrupt should have
141 * fired multiple times before we overflow'd. If it hasn't
81972551
DA
142 * then this is a good indication the cpu is stuck
143 */
77c12fc9 144 if (is_hardlockup(cpu)) {
1610611a 145 unsigned int this_cpu = smp_processor_id();
77c12fc9 146 struct cpumask backtrace_mask = *cpu_online_mask;
81972551 147
1610611a 148 /* Only print hardlockups once. */
77c12fc9 149 if (per_cpu(watchdog_hardlockup_warned, cpu))
81972551
DA
150 return;
151
77c12fc9 152 pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", cpu);
81972551
DA
153 print_modules();
154 print_irqtrace_events(current);
77c12fc9
DA
155 if (cpu == this_cpu) {
156 if (regs)
157 show_regs(regs);
158 else
159 dump_stack();
160 cpumask_clear_cpu(cpu, &backtrace_mask);
161 } else {
162 if (trigger_single_cpu_backtrace(cpu))
163 cpumask_clear_cpu(cpu, &backtrace_mask);
164 }
81972551
DA
165
166 /*
77c12fc9
DA
167 * Perform multi-CPU dump only once to avoid multiple
168 * hardlockups generating interleaving traces
81972551
DA
169 */
170 if (sysctl_hardlockup_all_cpu_backtrace &&
1610611a 171 !test_and_set_bit(0, &watchdog_hardlockup_all_cpu_dumped))
77c12fc9 172 trigger_cpumask_backtrace(&backtrace_mask);
81972551
DA
173
174 if (hardlockup_panic)
175 nmi_panic(regs, "Hard LOCKUP");
176
77c12fc9 177 per_cpu(watchdog_hardlockup_warned, cpu) = true;
1610611a 178 } else {
77c12fc9 179 per_cpu(watchdog_hardlockup_warned, cpu) = false;
81972551 180 }
81972551
DA
181}
182
183#else /* CONFIG_HARDLOCKUP_DETECTOR_PERF */
184
185static inline void watchdog_hardlockup_kick(void) { }
186
187#endif /* !CONFIG_HARDLOCKUP_DETECTOR_PERF */
188
05a4a952
NP
189/*
190 * These functions can be overridden if an architecture implements its
191 * own hardlockup detector.
a10a842f 192 *
df95d308 193 * watchdog_hardlockup_enable/disable can be implemented to start and stop when
b124ac45 194 * softlockup watchdog start and stop. The arch must select the
a10a842f 195 * SOFTLOCKUP_DETECTOR Kconfig.
05a4a952 196 */
df95d308 197void __weak watchdog_hardlockup_enable(unsigned int cpu)
05a4a952 198{
146c9d0e 199 hardlockup_detector_perf_enable();
05a4a952 200}
941154bd 201
df95d308 202void __weak watchdog_hardlockup_disable(unsigned int cpu)
05a4a952 203{
941154bd 204 hardlockup_detector_perf_disable();
05a4a952
NP
205}
206
df95d308
DA
207/* Return 0, if a hardlockup watchdog is available. Error code otherwise */
208int __weak __init watchdog_hardlockup_probe(void)
a994a314
TG
209{
210 return hardlockup_detector_perf_init();
211}
212
6592ad2f 213/**
df95d308 214 * watchdog_hardlockup_stop - Stop the watchdog for reconfiguration
6592ad2f 215 *
6b9dc480 216 * The reconfiguration steps are:
df95d308 217 * watchdog_hardlockup_stop();
6592ad2f 218 * update_variables();
df95d308 219 * watchdog_hardlockup_start();
6b9dc480 220 */
df95d308 221void __weak watchdog_hardlockup_stop(void) { }
6b9dc480
TG
222
223/**
df95d308 224 * watchdog_hardlockup_start - Start the watchdog after reconfiguration
6592ad2f 225 *
df95d308 226 * Counterpart to watchdog_hardlockup_stop().
6b9dc480
TG
227 *
228 * The following variables have been updated in update_variables() and
229 * contain the currently valid configuration:
7feeb9cd 230 * - watchdog_enabled
a10a842f
NP
231 * - watchdog_thresh
232 * - watchdog_cpumask
a10a842f 233 */
df95d308 234void __weak watchdog_hardlockup_start(void) { }
a10a842f 235
09154985
TG
236/**
237 * lockup_detector_update_enable - Update the sysctl enable bit
238 *
df95d308
DA
239 * Caller needs to make sure that the hard watchdogs are off, so this
240 * can't race with watchdog_hardlockup_disable().
09154985
TG
241 */
242static void lockup_detector_update_enable(void)
243{
244 watchdog_enabled = 0;
245 if (!watchdog_user_enabled)
246 return;
df95d308
DA
247 if (watchdog_hardlockup_available && watchdog_hardlockup_user_enabled)
248 watchdog_enabled |= WATCHDOG_HARDLOCKUP_ENABLED;
249 if (watchdog_softlockup_user_enabled)
250 watchdog_enabled |= WATCHDOG_SOFTOCKUP_ENABLED;
09154985
TG
251}
252
05a4a952
NP
253#ifdef CONFIG_SOFTLOCKUP_DETECTOR
254
fef06efc
PM
255/*
256 * Delay the soflockup report when running a known slow code.
257 * It does _not_ affect the timestamp of the last successdul reschedule.
258 */
259#define SOFTLOCKUP_DELAY_REPORT ULONG_MAX
11e31f60 260
f117955a
GP
261#ifdef CONFIG_SMP
262int __read_mostly sysctl_softlockup_all_cpu_backtrace;
263#endif
264
e7e04615
SS
265static struct cpumask watchdog_allowed_mask __read_mostly;
266
2b9d7f23
TG
267/* Global variables, exported for sysctl */
268unsigned int __read_mostly softlockup_panic =
67fca000 269 IS_ENABLED(CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC);
2eb2527f 270
9cf57731 271static bool softlockup_initialized __read_mostly;
0f34c400 272static u64 __read_mostly sample_period;
58687acb 273
fef06efc 274/* Timestamp taken after the last successful reschedule. */
58687acb 275static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
fef06efc
PM
276/* Timestamp of the last softlockup report. */
277static DEFINE_PER_CPU(unsigned long, watchdog_report_ts);
58687acb
DZ
278static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
279static DEFINE_PER_CPU(bool, softlockup_touch_sync);
ed235875 280static unsigned long soft_lockup_nmi_warn;
58687acb 281
58687acb
DZ
282static int __init nowatchdog_setup(char *str)
283{
09154985 284 watchdog_user_enabled = 0;
58687acb
DZ
285 return 1;
286}
287__setup("nowatchdog", nowatchdog_setup);
288
58687acb
DZ
289static int __init nosoftlockup_setup(char *str)
290{
df95d308 291 watchdog_softlockup_user_enabled = 0;
58687acb
DZ
292 return 1;
293}
294__setup("nosoftlockup", nosoftlockup_setup);
195daf66 295
11295055
LO
296static int __init watchdog_thresh_setup(char *str)
297{
298 get_option(&str, &watchdog_thresh);
299 return 1;
300}
301__setup("watchdog_thresh=", watchdog_thresh_setup);
302
941154bd
TG
303static void __lockup_detector_cleanup(void);
304
4eec42f3
MSB
305/*
306 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
307 * lockups can have false positives under extreme conditions. So we generally
308 * want a higher threshold for soft lockups than for hard lockups. So we couple
309 * the thresholds with a factor: we make the soft threshold twice the amount of
310 * time the hard threshold is.
311 */
6e9101ae 312static int get_softlockup_thresh(void)
4eec42f3
MSB
313{
314 return watchdog_thresh * 2;
315}
58687acb
DZ
316
317/*
318 * Returns seconds, approximately. We don't need nanosecond
319 * resolution, and we don't need to waste time with a big divide when
320 * 2^30ns == 1.074s.
321 */
c06b4f19 322static unsigned long get_timestamp(void)
58687acb 323{
545a2bf7 324 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
58687acb
DZ
325}
326
0f34c400 327static void set_sample_period(void)
58687acb
DZ
328{
329 /*
586692a5 330 * convert watchdog_thresh from seconds to ns
86f5e6a7
FLVC
331 * the divide by 5 is to give hrtimer several chances (two
332 * or three with the current relation between the soft
333 * and hard thresholds) to increment before the
334 * hardlockup detector generates a warning
58687acb 335 */
0f34c400 336 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
7edaeb68 337 watchdog_update_hrtimer_threshold(sample_period);
58687acb
DZ
338}
339
fef06efc
PM
340static void update_report_ts(void)
341{
342 __this_cpu_write(watchdog_report_ts, get_timestamp());
343}
344
58687acb 345/* Commands for resetting the watchdog */
7c0012f5 346static void update_touch_ts(void)
58687acb 347{
c06b4f19 348 __this_cpu_write(watchdog_touch_ts, get_timestamp());
fef06efc 349 update_report_ts();
58687acb
DZ
350}
351
03e0d461
TH
352/**
353 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
354 *
355 * Call when the scheduler may have stalled for legitimate reasons
356 * preventing the watchdog task from executing - e.g. the scheduler
357 * entering idle state. This should only be used for scheduler events.
358 * Use touch_softlockup_watchdog() for everything else.
359 */
cb9d7fd5 360notrace void touch_softlockup_watchdog_sched(void)
58687acb 361{
7861144b 362 /*
fef06efc
PM
363 * Preemption can be enabled. It doesn't matter which CPU's watchdog
364 * report period gets restarted here, so use the raw_ operation.
7861144b 365 */
fef06efc 366 raw_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
58687acb 367}
03e0d461 368
cb9d7fd5 369notrace void touch_softlockup_watchdog(void)
03e0d461
TH
370{
371 touch_softlockup_watchdog_sched();
82607adc 372 wq_watchdog_touch(raw_smp_processor_id());
03e0d461 373}
0167c781 374EXPORT_SYMBOL(touch_softlockup_watchdog);
58687acb 375
332fbdbc 376void touch_all_softlockup_watchdogs(void)
58687acb
DZ
377{
378 int cpu;
379
380 /*
d57108d4
TG
381 * watchdog_mutex cannpt be taken here, as this might be called
382 * from (soft)interrupt context, so the access to
383 * watchdog_allowed_cpumask might race with a concurrent update.
384 *
385 * The watchdog time stamp can race against a concurrent real
386 * update as well, the only side effect might be a cycle delay for
387 * the softlockup check.
58687acb 388 */
89e28ce6 389 for_each_cpu(cpu, &watchdog_allowed_mask) {
fef06efc 390 per_cpu(watchdog_report_ts, cpu) = SOFTLOCKUP_DELAY_REPORT;
89e28ce6
WQ
391 wq_watchdog_touch(cpu);
392 }
58687acb
DZ
393}
394
58687acb
DZ
395void touch_softlockup_watchdog_sync(void)
396{
f7f66b05 397 __this_cpu_write(softlockup_touch_sync, true);
fef06efc 398 __this_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
58687acb
DZ
399}
400
0f90b88d
PM
401static int is_softlockup(unsigned long touch_ts,
402 unsigned long period_ts,
403 unsigned long now)
58687acb 404{
df95d308 405 if ((watchdog_enabled & WATCHDOG_SOFTOCKUP_ENABLED) && watchdog_thresh) {
195daf66 406 /* Warn about unreasonable delays. */
fef06efc 407 if (time_after(now, period_ts + get_softlockup_thresh()))
195daf66
UO
408 return now - touch_ts;
409 }
58687acb
DZ
410 return 0;
411}
412
05a4a952 413/* watchdog detector functions */
be45bf53
PZ
414static DEFINE_PER_CPU(struct completion, softlockup_completion);
415static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
416
9cf57731 417/*
b124ac45 418 * The watchdog feed function - touches the timestamp.
9cf57731
PZ
419 *
420 * It only runs once every sample_period seconds (4 seconds by
421 * default) to reset the softlockup timestamp. If this gets delayed
422 * for more than 2*watchdog_thresh seconds then the debug-printout
423 * triggers in watchdog_timer_fn().
424 */
425static int softlockup_fn(void *data)
426{
7c0012f5 427 update_touch_ts();
be45bf53 428 complete(this_cpu_ptr(&softlockup_completion));
9cf57731
PZ
429
430 return 0;
431}
432
58687acb
DZ
433/* watchdog kicker functions */
434static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
435{
0f90b88d 436 unsigned long touch_ts, period_ts, now;
58687acb
DZ
437 struct pt_regs *regs = get_irq_regs();
438 int duration;
ed235875 439 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
58687acb 440
01f0a027 441 if (!watchdog_enabled)
b94f5118
DZ
442 return HRTIMER_NORESTART;
443
81972551 444 watchdog_hardlockup_kick();
58687acb
DZ
445
446 /* kick the softlockup detector */
be45bf53
PZ
447 if (completion_done(this_cpu_ptr(&softlockup_completion))) {
448 reinit_completion(this_cpu_ptr(&softlockup_completion));
449 stop_one_cpu_nowait(smp_processor_id(),
450 softlockup_fn, NULL,
451 this_cpu_ptr(&softlockup_stop_work));
452 }
58687acb
DZ
453
454 /* .. and repeat */
0f34c400 455 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
58687acb 456
0f90b88d
PM
457 /*
458 * Read the current timestamp first. It might become invalid anytime
459 * when a virtual machine is stopped by the host or when the watchog
460 * is touched from NMI.
461 */
462 now = get_timestamp();
9bf3bc94
PM
463 /*
464 * If a virtual machine is stopped by the host it can look to
0f90b88d 465 * the watchdog like a soft lockup. This function touches the watchdog.
9bf3bc94
PM
466 */
467 kvm_check_and_clear_guest_paused();
0f90b88d
PM
468 /*
469 * The stored timestamp is comparable with @now only when not touched.
470 * It might get touched anytime from NMI. Make sure that is_softlockup()
471 * uses the same (valid) value.
472 */
473 period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
9bf3bc94
PM
474
475 /* Reset the interval when touched by known problematic code. */
fef06efc 476 if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
909ea964 477 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
58687acb
DZ
478 /*
479 * If the time stamp was touched atomically
480 * make sure the scheduler tick is up to date.
481 */
909ea964 482 __this_cpu_write(softlockup_touch_sync, false);
58687acb
DZ
483 sched_clock_tick();
484 }
5d1c0f4a 485
fef06efc 486 update_report_ts();
58687acb
DZ
487 return HRTIMER_RESTART;
488 }
489
0f90b88d
PM
490 /* Check for a softlockup. */
491 touch_ts = __this_cpu_read(watchdog_touch_ts);
492 duration = is_softlockup(touch_ts, period_ts, now);
58687acb 493 if (unlikely(duration)) {
9f113bf7
PM
494 /*
495 * Prevent multiple soft-lockup reports if one cpu is already
496 * engaged in dumping all cpu back traces.
497 */
ed235875 498 if (softlockup_all_cpu_backtrace) {
9f113bf7 499 if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
ed235875 500 return HRTIMER_RESTART;
ed235875
AT
501 }
502
c9ad17c9 503 /* Start period for the next softlockup warning. */
fef06efc 504 update_report_ts();
c9ad17c9 505
656c3b79 506 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
26e09c6e 507 smp_processor_id(), duration,
58687acb
DZ
508 current->comm, task_pid_nr(current));
509 print_modules();
510 print_irqtrace_events(current);
511 if (regs)
512 show_regs(regs);
513 else
514 dump_stack();
515
ed235875 516 if (softlockup_all_cpu_backtrace) {
ed235875 517 trigger_allbutself_cpu_backtrace();
9f113bf7 518 clear_bit_unlock(0, &soft_lockup_nmi_warn);
ed235875
AT
519 }
520
69361eef 521 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
58687acb
DZ
522 if (softlockup_panic)
523 panic("softlockup: hung tasks");
1bc503cb 524 }
58687acb
DZ
525
526 return HRTIMER_RESTART;
527}
528
bcd951cf 529static void watchdog_enable(unsigned int cpu)
58687acb 530{
01f0a027 531 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
be45bf53 532 struct completion *done = this_cpu_ptr(&softlockup_completion);
58687acb 533
9cf57731
PZ
534 WARN_ON_ONCE(cpu != smp_processor_id());
535
be45bf53
PZ
536 init_completion(done);
537 complete(done);
538
01f0a027 539 /*
df95d308 540 * Start the timer first to prevent the hardlockup watchdog triggering
01f0a027
TG
541 * before the timer has a chance to fire.
542 */
d2ab4cf4 543 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
3935e895 544 hrtimer->function = watchdog_timer_fn;
01f0a027 545 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
d2ab4cf4 546 HRTIMER_MODE_REL_PINNED_HARD);
3935e895 547
01f0a027 548 /* Initialize timestamp */
7c0012f5 549 update_touch_ts();
df95d308
DA
550 /* Enable the hardlockup detector */
551 if (watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)
552 watchdog_hardlockup_enable(cpu);
bcd951cf 553}
58687acb 554
bcd951cf
TG
555static void watchdog_disable(unsigned int cpu)
556{
01f0a027 557 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
58687acb 558
9cf57731
PZ
559 WARN_ON_ONCE(cpu != smp_processor_id());
560
01f0a027 561 /*
df95d308
DA
562 * Disable the hardlockup detector first. That prevents that a large
563 * delay between disabling the timer and disabling the hardlockup
564 * detector causes a false positive.
01f0a027 565 */
df95d308 566 watchdog_hardlockup_disable(cpu);
01f0a027 567 hrtimer_cancel(hrtimer);
be45bf53 568 wait_for_completion(this_cpu_ptr(&softlockup_completion));
58687acb
DZ
569}
570
9cf57731 571static int softlockup_stop_fn(void *data)
b8900bc0 572{
9cf57731
PZ
573 watchdog_disable(smp_processor_id());
574 return 0;
b8900bc0
FW
575}
576
9cf57731 577static void softlockup_stop_all(void)
bcd951cf 578{
9cf57731
PZ
579 int cpu;
580
581 if (!softlockup_initialized)
582 return;
583
584 for_each_cpu(cpu, &watchdog_allowed_mask)
585 smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
586
587 cpumask_clear(&watchdog_allowed_mask);
bcd951cf
TG
588}
589
9cf57731 590static int softlockup_start_fn(void *data)
bcd951cf 591{
9cf57731
PZ
592 watchdog_enable(smp_processor_id());
593 return 0;
bcd951cf 594}
58687acb 595
9cf57731 596static void softlockup_start_all(void)
2eb2527f 597{
9cf57731 598 int cpu;
2eb2527f 599
9cf57731
PZ
600 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
601 for_each_cpu(cpu, &watchdog_allowed_mask)
602 smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
2eb2527f
TG
603}
604
9cf57731 605int lockup_detector_online_cpu(unsigned int cpu)
2eb2527f 606{
7dd47617
TG
607 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
608 watchdog_enable(cpu);
9cf57731 609 return 0;
2eb2527f
TG
610}
611
9cf57731 612int lockup_detector_offline_cpu(unsigned int cpu)
2eb2527f 613{
7dd47617
TG
614 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
615 watchdog_disable(cpu);
9cf57731 616 return 0;
2eb2527f
TG
617}
618
7c56a873 619static void __lockup_detector_reconfigure(void)
2eb2527f 620{
e31d6883 621 cpus_read_lock();
df95d308 622 watchdog_hardlockup_stop();
9cf57731
PZ
623
624 softlockup_stop_all();
2eb2527f 625 set_sample_period();
09154985
TG
626 lockup_detector_update_enable();
627 if (watchdog_enabled && watchdog_thresh)
9cf57731
PZ
628 softlockup_start_all();
629
df95d308 630 watchdog_hardlockup_start();
e31d6883
TG
631 cpus_read_unlock();
632 /*
633 * Must be called outside the cpus locked section to prevent
634 * recursive locking in the perf code.
635 */
636 __lockup_detector_cleanup();
2eb2527f
TG
637}
638
7c56a873
LD
639void lockup_detector_reconfigure(void)
640{
641 mutex_lock(&watchdog_mutex);
642 __lockup_detector_reconfigure();
643 mutex_unlock(&watchdog_mutex);
644}
645
2eb2527f 646/*
b124ac45 647 * Create the watchdog infrastructure and configure the detector(s).
2eb2527f 648 */
5587185d 649static __init void lockup_detector_setup(void)
2eb2527f 650{
2eb2527f
TG
651 /*
652 * If sysctl is off and watchdog got disabled on the command line,
653 * nothing to do here.
654 */
09154985
TG
655 lockup_detector_update_enable();
656
2eb2527f
TG
657 if (!IS_ENABLED(CONFIG_SYSCTL) &&
658 !(watchdog_enabled && watchdog_thresh))
659 return;
660
2eb2527f 661 mutex_lock(&watchdog_mutex);
7c56a873 662 __lockup_detector_reconfigure();
9cf57731 663 softlockup_initialized = true;
2eb2527f
TG
664 mutex_unlock(&watchdog_mutex);
665}
666
2b9d7f23 667#else /* CONFIG_SOFTLOCKUP_DETECTOR */
7c56a873 668static void __lockup_detector_reconfigure(void)
6592ad2f 669{
e31d6883 670 cpus_read_lock();
df95d308 671 watchdog_hardlockup_stop();
09154985 672 lockup_detector_update_enable();
df95d308 673 watchdog_hardlockup_start();
e31d6883 674 cpus_read_unlock();
6592ad2f 675}
7c56a873
LD
676void lockup_detector_reconfigure(void)
677{
678 __lockup_detector_reconfigure();
679}
5587185d 680static inline void lockup_detector_setup(void)
34ddaa3e 681{
7c56a873 682 __lockup_detector_reconfigure();
34ddaa3e 683}
2b9d7f23 684#endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
05a4a952 685
941154bd
TG
686static void __lockup_detector_cleanup(void)
687{
688 lockdep_assert_held(&watchdog_mutex);
689 hardlockup_detector_perf_cleanup();
690}
691
692/**
693 * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
694 *
695 * Caller must not hold the cpu hotplug rwsem.
696 */
697void lockup_detector_cleanup(void)
698{
699 mutex_lock(&watchdog_mutex);
700 __lockup_detector_cleanup();
701 mutex_unlock(&watchdog_mutex);
702}
703
6554fd8c
TG
704/**
705 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
706 *
707 * Special interface for parisc. It prevents lockup detector warnings from
708 * the default pm_poweroff() function which busy loops forever.
709 */
710void lockup_detector_soft_poweroff(void)
711{
712 watchdog_enabled = 0;
713}
714
58cf690a
UO
715#ifdef CONFIG_SYSCTL
716
b124ac45 717/* Propagate any changes to the watchdog infrastructure */
d57108d4 718static void proc_watchdog_update(void)
a0c9cbb9 719{
e8b62b2d
TG
720 /* Remove impossible cpus to keep sysctl output clean. */
721 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
7c56a873 722 __lockup_detector_reconfigure();
a0c9cbb9
UO
723}
724
ef246a21
UO
725/*
726 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
727 *
df95d308
DA
728 * caller | table->data points to | 'which'
729 * -------------------|----------------------------------|-------------------------------
730 * proc_watchdog | watchdog_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED |
731 * | | WATCHDOG_SOFTOCKUP_ENABLED
732 * -------------------|----------------------------------|-------------------------------
733 * proc_nmi_watchdog | watchdog_hardlockup_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED
734 * -------------------|----------------------------------|-------------------------------
735 * proc_soft_watchdog | watchdog_softlockup_user_enabled | WATCHDOG_SOFTOCKUP_ENABLED
ef246a21
UO
736 */
737static int proc_watchdog_common(int which, struct ctl_table *table, int write,
32927393 738 void *buffer, size_t *lenp, loff_t *ppos)
ef246a21 739{
09154985 740 int err, old, *param = table->data;
ef246a21 741
946d1977 742 mutex_lock(&watchdog_mutex);
ef246a21 743
ef246a21 744 if (!write) {
09154985
TG
745 /*
746 * On read synchronize the userspace interface. This is a
747 * racy snapshot.
748 */
749 *param = (watchdog_enabled & which) != 0;
ef246a21
UO
750 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
751 } else {
09154985 752 old = READ_ONCE(*param);
ef246a21 753 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
09154985 754 if (!err && old != READ_ONCE(*param))
d57108d4 755 proc_watchdog_update();
ef246a21 756 }
946d1977 757 mutex_unlock(&watchdog_mutex);
ef246a21
UO
758 return err;
759}
760
83a80a39
UO
761/*
762 * /proc/sys/kernel/watchdog
763 */
764int proc_watchdog(struct ctl_table *table, int write,
32927393 765 void *buffer, size_t *lenp, loff_t *ppos)
83a80a39 766{
df95d308
DA
767 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED |
768 WATCHDOG_SOFTOCKUP_ENABLED,
83a80a39
UO
769 table, write, buffer, lenp, ppos);
770}
771
772/*
773 * /proc/sys/kernel/nmi_watchdog
58687acb 774 */
83a80a39 775int proc_nmi_watchdog(struct ctl_table *table, int write,
32927393 776 void *buffer, size_t *lenp, loff_t *ppos)
83a80a39 777{
df95d308 778 if (!watchdog_hardlockup_available && write)
a994a314 779 return -ENOTSUPP;
df95d308 780 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED,
83a80a39
UO
781 table, write, buffer, lenp, ppos);
782}
783
784/*
785 * /proc/sys/kernel/soft_watchdog
786 */
787int proc_soft_watchdog(struct ctl_table *table, int write,
32927393 788 void *buffer, size_t *lenp, loff_t *ppos)
83a80a39 789{
df95d308 790 return proc_watchdog_common(WATCHDOG_SOFTOCKUP_ENABLED,
83a80a39
UO
791 table, write, buffer, lenp, ppos);
792}
58687acb 793
83a80a39
UO
794/*
795 * /proc/sys/kernel/watchdog_thresh
796 */
797int proc_watchdog_thresh(struct ctl_table *table, int write,
32927393 798 void *buffer, size_t *lenp, loff_t *ppos)
58687acb 799{
d57108d4 800 int err, old;
58687acb 801
946d1977 802 mutex_lock(&watchdog_mutex);
bcd951cf 803
d57108d4 804 old = READ_ONCE(watchdog_thresh);
b8900bc0 805 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
83a80a39 806
d57108d4
TG
807 if (!err && write && old != READ_ONCE(watchdog_thresh))
808 proc_watchdog_update();
e04ab2bc 809
946d1977 810 mutex_unlock(&watchdog_mutex);
b8900bc0 811 return err;
58687acb 812}
fe4ba3c3
CM
813
814/*
815 * The cpumask is the mask of possible cpus that the watchdog can run
816 * on, not the mask of cpus it is actually running on. This allows the
817 * user to specify a mask that will include cpus that have not yet
818 * been brought online, if desired.
819 */
820int proc_watchdog_cpumask(struct ctl_table *table, int write,
32927393 821 void *buffer, size_t *lenp, loff_t *ppos)
fe4ba3c3
CM
822{
823 int err;
824
946d1977 825 mutex_lock(&watchdog_mutex);
8c073d27 826
fe4ba3c3 827 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
05ba3de7 828 if (!err && write)
e8b62b2d 829 proc_watchdog_update();
5490125d 830
946d1977 831 mutex_unlock(&watchdog_mutex);
fe4ba3c3
CM
832 return err;
833}
dd0693fd
XN
834
835static const int sixty = 60;
836
837static struct ctl_table watchdog_sysctls[] = {
838 {
839 .procname = "watchdog",
840 .data = &watchdog_user_enabled,
841 .maxlen = sizeof(int),
842 .mode = 0644,
843 .proc_handler = proc_watchdog,
844 .extra1 = SYSCTL_ZERO,
845 .extra2 = SYSCTL_ONE,
846 },
847 {
848 .procname = "watchdog_thresh",
849 .data = &watchdog_thresh,
850 .maxlen = sizeof(int),
851 .mode = 0644,
852 .proc_handler = proc_watchdog_thresh,
853 .extra1 = SYSCTL_ZERO,
854 .extra2 = (void *)&sixty,
855 },
856 {
857 .procname = "nmi_watchdog",
df95d308 858 .data = &watchdog_hardlockup_user_enabled,
dd0693fd
XN
859 .maxlen = sizeof(int),
860 .mode = NMI_WATCHDOG_SYSCTL_PERM,
861 .proc_handler = proc_nmi_watchdog,
862 .extra1 = SYSCTL_ZERO,
863 .extra2 = SYSCTL_ONE,
864 },
865 {
866 .procname = "watchdog_cpumask",
867 .data = &watchdog_cpumask_bits,
868 .maxlen = NR_CPUS,
869 .mode = 0644,
870 .proc_handler = proc_watchdog_cpumask,
871 },
872#ifdef CONFIG_SOFTLOCKUP_DETECTOR
873 {
874 .procname = "soft_watchdog",
df95d308 875 .data = &watchdog_softlockup_user_enabled,
dd0693fd
XN
876 .maxlen = sizeof(int),
877 .mode = 0644,
878 .proc_handler = proc_soft_watchdog,
879 .extra1 = SYSCTL_ZERO,
880 .extra2 = SYSCTL_ONE,
881 },
882 {
883 .procname = "softlockup_panic",
884 .data = &softlockup_panic,
885 .maxlen = sizeof(int),
886 .mode = 0644,
887 .proc_handler = proc_dointvec_minmax,
888 .extra1 = SYSCTL_ZERO,
889 .extra2 = SYSCTL_ONE,
890 },
891#ifdef CONFIG_SMP
892 {
893 .procname = "softlockup_all_cpu_backtrace",
894 .data = &sysctl_softlockup_all_cpu_backtrace,
895 .maxlen = sizeof(int),
896 .mode = 0644,
897 .proc_handler = proc_dointvec_minmax,
898 .extra1 = SYSCTL_ZERO,
899 .extra2 = SYSCTL_ONE,
900 },
901#endif /* CONFIG_SMP */
902#endif
903#ifdef CONFIG_HARDLOCKUP_DETECTOR
904 {
905 .procname = "hardlockup_panic",
906 .data = &hardlockup_panic,
907 .maxlen = sizeof(int),
908 .mode = 0644,
909 .proc_handler = proc_dointvec_minmax,
910 .extra1 = SYSCTL_ZERO,
911 .extra2 = SYSCTL_ONE,
912 },
913#ifdef CONFIG_SMP
914 {
915 .procname = "hardlockup_all_cpu_backtrace",
916 .data = &sysctl_hardlockup_all_cpu_backtrace,
917 .maxlen = sizeof(int),
918 .mode = 0644,
919 .proc_handler = proc_dointvec_minmax,
920 .extra1 = SYSCTL_ZERO,
921 .extra2 = SYSCTL_ONE,
922 },
923#endif /* CONFIG_SMP */
924#endif
925 {}
926};
927
928static void __init watchdog_sysctl_init(void)
929{
930 register_sysctl_init("kernel", watchdog_sysctls);
931}
932#else
933#define watchdog_sysctl_init() do { } while (0)
58687acb
DZ
934#endif /* CONFIG_SYSCTL */
935
004417a6 936void __init lockup_detector_init(void)
58687acb 937{
13316b31 938 if (tick_nohz_full_enabled())
314b08ff 939 pr_info("Disabling watchdog on nohz_full cores by default\n");
13316b31 940
de201559 941 cpumask_copy(&watchdog_cpumask,
04d4e665 942 housekeeping_cpumask(HK_TYPE_TIMER));
fe4ba3c3 943
df95d308
DA
944 if (!watchdog_hardlockup_probe())
945 watchdog_hardlockup_available = true;
5587185d 946 lockup_detector_setup();
dd0693fd 947 watchdog_sysctl_init();
58687acb 948}