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