blob: baae9fc950312fe22c230e75760be1b14b2f461e [file] [log] [blame]
Don Zickus58687ac2010-05-07 17:11:44 -04001/*
2 * Detect hard and soft lockups on a system
3 *
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5 *
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -05006 * Note: Most of this code is borrowed heavily from the original softlockup
7 * detector, so thanks to Ingo for the initial implementation.
8 * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
Don Zickus58687ac2010-05-07 17:11:44 -04009 * to those contributors as well.
10 */
11
Kefeng Wang5f92a7b2017-07-14 14:49:46 -070012#define pr_fmt(fmt) "watchdog: " fmt
Andrew Morton45019802012-03-23 15:01:55 -070013
Don Zickus58687ac2010-05-07 17:11:44 -040014#include <linux/mm.h>
15#include <linux/cpu.h>
16#include <linux/nmi.h>
17#include <linux/init.h>
Don Zickus58687ac2010-05-07 17:11:44 -040018#include <linux/module.h>
19#include <linux/sysctl.h>
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000020#include <linux/smpboot.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060021#include <linux/sched/rt.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010022#include <uapi/linux/sched/types.h>
Chris Metcalffe4ba3c2015-06-24 16:55:45 -070023#include <linux/tick.h>
Tejun Heo82607adc2015-12-08 11:28:04 -050024#include <linux/workqueue.h>
Ingo Molnare6017572017-02-01 16:36:40 +010025#include <linux/sched/clock.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010026#include <linux/sched/debug.h>
Don Zickus58687ac2010-05-07 17:11:44 -040027
28#include <asm/irq_regs.h>
Eric B Munson5d1c0f42012-03-10 14:37:28 -050029#include <linux/kvm_para.h>
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -070030#include <linux/kthread.h>
Don Zickus58687ac2010-05-07 17:11:44 -040031
Thomas Gleixner946d1972017-09-12 21:37:01 +020032static DEFINE_MUTEX(watchdog_mutex);
Peter Zijlstraab992dc2015-05-18 11:31:50 +020033
Nicholas Piggin05a4a952017-07-12 14:35:46 -070034#if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
35unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED |
36 NMI_WATCHDOG_ENABLED;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070037#else
Babu Moger249e52e2016-12-14 15:06:21 -080038unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070039#endif
Nicholas Piggin05a4a952017-07-12 14:35:46 -070040
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +020041int __read_mostly nmi_watchdog_user_enabled;
42int __read_mostly soft_watchdog_user_enabled;
43int __read_mostly watchdog_user_enabled;
44int __read_mostly watchdog_thresh = 10;
45
46struct cpumask watchdog_allowed_mask __read_mostly;
47static bool softlockup_threads_initialized __read_mostly;
48
49struct cpumask watchdog_cpumask __read_mostly;
50unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
51
Nicholas Piggin05a4a952017-07-12 14:35:46 -070052#ifdef CONFIG_HARDLOCKUP_DETECTOR
Nicholas Piggin05a4a952017-07-12 14:35:46 -070053/*
54 * Should we panic when a soft-lockup or hard-lockup occurs:
55 */
56unsigned int __read_mostly hardlockup_panic =
57 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
58/*
59 * We may not want to enable hard lockup detection by default in all cases,
60 * for example when running the kernel as a guest on a hypervisor. In these
61 * cases this function can be called to disable hard lockup detection. This
62 * function should only be executed once by the boot processor before the
63 * kernel command line parameters are parsed, because otherwise it is not
64 * possible to override this in hardlockup_panic_setup().
65 */
Thomas Gleixner7a355822017-09-12 21:37:02 +020066void __init hardlockup_detector_disable(void)
Nicholas Piggin05a4a952017-07-12 14:35:46 -070067{
68 watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
69}
70
71static int __init hardlockup_panic_setup(char *str)
72{
73 if (!strncmp(str, "panic", 5))
74 hardlockup_panic = 1;
75 else if (!strncmp(str, "nopanic", 7))
76 hardlockup_panic = 0;
77 else if (!strncmp(str, "0", 1))
78 watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
79 else if (!strncmp(str, "1", 1))
80 watchdog_enabled |= NMI_WATCHDOG_ENABLED;
81 return 1;
82}
83__setup("nmi_watchdog=", hardlockup_panic_setup);
84
Thomas Gleixner368a7e22017-09-12 21:37:07 +020085# ifdef CONFIG_SMP
86int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
Nicholas Piggin05a4a952017-07-12 14:35:46 -070087
Thomas Gleixner368a7e22017-09-12 21:37:07 +020088static int __init hardlockup_all_cpu_backtrace_setup(char *str)
89{
90 sysctl_hardlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
91 return 1;
92}
93__setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
94# endif /* CONFIG_SMP */
95#endif /* CONFIG_HARDLOCKUP_DETECTOR */
Nicholas Piggin05a4a952017-07-12 14:35:46 -070096
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070097/*
Nicholas Piggin05a4a952017-07-12 14:35:46 -070098 * These functions can be overridden if an architecture implements its
99 * own hardlockup detector.
Nicholas Piggina10a8422017-07-12 14:35:49 -0700100 *
101 * watchdog_nmi_enable/disable can be implemented to start and stop when
102 * softlockup watchdog threads start and stop. The arch must select the
103 * SOFTLOCKUP_DETECTOR Kconfig.
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700104 */
105int __weak watchdog_nmi_enable(unsigned int cpu)
106{
107 return 0;
108}
Thomas Gleixner941154b2017-09-12 21:37:04 +0200109
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700110void __weak watchdog_nmi_disable(unsigned int cpu)
111{
Thomas Gleixner941154b2017-09-12 21:37:04 +0200112 hardlockup_detector_perf_disable();
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700113}
114
Nicholas Piggina10a8422017-07-12 14:35:49 -0700115/*
116 * watchdog_nmi_reconfigure can be implemented to be notified after any
117 * watchdog configuration change. The arch hardlockup watchdog should
118 * respond to the following variables:
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +0200119 * - watchdog_enabled
Nicholas Piggina10a8422017-07-12 14:35:49 -0700120 * - watchdog_thresh
121 * - watchdog_cpumask
122 * - sysctl_hardlockup_all_cpu_backtrace
123 * - hardlockup_panic
Nicholas Piggina10a8422017-07-12 14:35:49 -0700124 */
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200125void __weak watchdog_nmi_reconfigure(void) { }
Nicholas Piggina10a8422017-07-12 14:35:49 -0700126
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700127#ifdef CONFIG_SOFTLOCKUP_DETECTOR
128
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200129/* Global variables, exported for sysctl */
130unsigned int __read_mostly softlockup_panic =
131 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200132
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800133static u64 __read_mostly sample_period;
Don Zickus58687ac2010-05-07 17:11:44 -0400134
135static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
136static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
137static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
138static DEFINE_PER_CPU(bool, softlockup_touch_sync);
Don Zickus58687ac2010-05-07 17:11:44 -0400139static DEFINE_PER_CPU(bool, soft_watchdog_warn);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000140static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
141static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
chai wenb1a8de12014-10-09 15:25:17 -0700142static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
Don Zickus58687ac2010-05-07 17:11:44 -0400143static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
Aaron Tomlined235872014-06-23 13:22:05 -0700144static unsigned long soft_lockup_nmi_warn;
Don Zickus58687ac2010-05-07 17:11:44 -0400145
Don Zickus58687ac2010-05-07 17:11:44 -0400146static int __init softlockup_panic_setup(char *str)
147{
148 softlockup_panic = simple_strtoul(str, NULL, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400149 return 1;
150}
151__setup("softlockup_panic=", softlockup_panic_setup);
152
153static int __init nowatchdog_setup(char *str)
154{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700155 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -0400156 return 1;
157}
158__setup("nowatchdog", nowatchdog_setup);
159
Don Zickus58687ac2010-05-07 17:11:44 -0400160static int __init nosoftlockup_setup(char *str)
161{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700162 watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
Don Zickus58687ac2010-05-07 17:11:44 -0400163 return 1;
164}
165__setup("nosoftlockup", nosoftlockup_setup);
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700166
Aaron Tomlined235872014-06-23 13:22:05 -0700167#ifdef CONFIG_SMP
Thomas Gleixner368a7e22017-09-12 21:37:07 +0200168int __read_mostly sysctl_softlockup_all_cpu_backtrace;
169
Aaron Tomlined235872014-06-23 13:22:05 -0700170static int __init softlockup_all_cpu_backtrace_setup(char *str)
171{
Thomas Gleixner368a7e22017-09-12 21:37:07 +0200172 sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
Aaron Tomlined235872014-06-23 13:22:05 -0700173 return 1;
174}
175__setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700176#endif
Don Zickus58687ac2010-05-07 17:11:44 -0400177
Thomas Gleixner941154b2017-09-12 21:37:04 +0200178static void __lockup_detector_cleanup(void);
179
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700180/*
181 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
182 * lockups can have false positives under extreme conditions. So we generally
183 * want a higher threshold for soft lockups than for hard lockups. So we couple
184 * the thresholds with a factor: we make the soft threshold twice the amount of
185 * time the hard threshold is.
186 */
Ingo Molnar6e9101a2011-05-24 05:43:18 +0200187static int get_softlockup_thresh(void)
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700188{
189 return watchdog_thresh * 2;
190}
Don Zickus58687ac2010-05-07 17:11:44 -0400191
192/*
193 * Returns seconds, approximately. We don't need nanosecond
194 * resolution, and we don't need to waste time with a big divide when
195 * 2^30ns == 1.074s.
196 */
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900197static unsigned long get_timestamp(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400198{
Cyril Bur545a2bf2015-02-12 15:01:24 -0800199 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
Don Zickus58687ac2010-05-07 17:11:44 -0400200}
201
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800202static void set_sample_period(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400203{
204 /*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700205 * convert watchdog_thresh from seconds to ns
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -0500206 * the divide by 5 is to give hrtimer several chances (two
207 * or three with the current relation between the soft
208 * and hard thresholds) to increment before the
209 * hardlockup detector generates a warning
Don Zickus58687ac2010-05-07 17:11:44 -0400210 */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800211 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
Thomas Gleixner7edaeb62017-08-15 09:50:13 +0200212 watchdog_update_hrtimer_threshold(sample_period);
Don Zickus58687ac2010-05-07 17:11:44 -0400213}
214
215/* Commands for resetting the watchdog */
216static void __touch_watchdog(void)
217{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900218 __this_cpu_write(watchdog_touch_ts, get_timestamp());
Don Zickus58687ac2010-05-07 17:11:44 -0400219}
220
Tejun Heo03e0d462015-12-08 11:28:04 -0500221/**
222 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
223 *
224 * Call when the scheduler may have stalled for legitimate reasons
225 * preventing the watchdog task from executing - e.g. the scheduler
226 * entering idle state. This should only be used for scheduler events.
227 * Use touch_softlockup_watchdog() for everything else.
228 */
229void touch_softlockup_watchdog_sched(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400230{
Andrew Morton78611442014-04-18 15:07:12 -0700231 /*
232 * Preemption can be enabled. It doesn't matter which CPU's timestamp
233 * gets zeroed here, so use the raw_ operation.
234 */
235 raw_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400236}
Tejun Heo03e0d462015-12-08 11:28:04 -0500237
238void touch_softlockup_watchdog(void)
239{
240 touch_softlockup_watchdog_sched();
Tejun Heo82607adc2015-12-08 11:28:04 -0500241 wq_watchdog_touch(raw_smp_processor_id());
Tejun Heo03e0d462015-12-08 11:28:04 -0500242}
Ingo Molnar0167c782010-05-13 08:53:33 +0200243EXPORT_SYMBOL(touch_softlockup_watchdog);
Don Zickus58687ac2010-05-07 17:11:44 -0400244
Don Zickus332fbdb2010-05-07 17:11:45 -0400245void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400246{
247 int cpu;
248
249 /*
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200250 * watchdog_mutex cannpt be taken here, as this might be called
251 * from (soft)interrupt context, so the access to
252 * watchdog_allowed_cpumask might race with a concurrent update.
253 *
254 * The watchdog time stamp can race against a concurrent real
255 * update as well, the only side effect might be a cycle delay for
256 * the softlockup check.
Don Zickus58687ac2010-05-07 17:11:44 -0400257 */
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200258 for_each_cpu(cpu, &watchdog_allowed_mask)
Don Zickus58687ac2010-05-07 17:11:44 -0400259 per_cpu(watchdog_touch_ts, cpu) = 0;
Tejun Heo82607adc2015-12-08 11:28:04 -0500260 wq_watchdog_touch(-1);
Don Zickus58687ac2010-05-07 17:11:44 -0400261}
262
Don Zickus58687ac2010-05-07 17:11:44 -0400263void touch_softlockup_watchdog_sync(void)
264{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500265 __this_cpu_write(softlockup_touch_sync, true);
266 __this_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400267}
268
Don Zickus26e09c62010-05-17 18:06:04 -0400269static int is_softlockup(unsigned long touch_ts)
Don Zickus58687ac2010-05-07 17:11:44 -0400270{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900271 unsigned long now = get_timestamp();
Don Zickus58687ac2010-05-07 17:11:44 -0400272
Ulrich Obergfell39d2da22015-11-05 18:44:56 -0800273 if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700274 /* Warn about unreasonable delays. */
275 if (time_after(now, touch_ts + get_softlockup_thresh()))
276 return now - touch_ts;
277 }
Don Zickus58687ac2010-05-07 17:11:44 -0400278 return 0;
279}
280
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700281/* watchdog detector functions */
282bool is_hardlockup(void)
283{
284 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
285
286 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
287 return true;
288
289 __this_cpu_write(hrtimer_interrupts_saved, hrint);
290 return false;
291}
292
Don Zickus58687ac2010-05-07 17:11:44 -0400293static void watchdog_interrupt_count(void)
294{
Christoph Lameter909ea962010-12-08 16:22:55 +0100295 __this_cpu_inc(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400296}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000297
Don Zickus58687ac2010-05-07 17:11:44 -0400298/* watchdog kicker functions */
299static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
300{
Christoph Lameter909ea962010-12-08 16:22:55 +0100301 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400302 struct pt_regs *regs = get_irq_regs();
303 int duration;
Aaron Tomlined235872014-06-23 13:22:05 -0700304 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
Don Zickus58687ac2010-05-07 17:11:44 -0400305
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200306 if (!watchdog_enabled)
Don Zickusb94f5112017-01-24 15:17:53 -0800307 return HRTIMER_NORESTART;
308
Don Zickus58687ac2010-05-07 17:11:44 -0400309 /* kick the hardlockup detector */
310 watchdog_interrupt_count();
311
312 /* kick the softlockup detector */
Christoph Lameter909ea962010-12-08 16:22:55 +0100313 wake_up_process(__this_cpu_read(softlockup_watchdog));
Don Zickus58687ac2010-05-07 17:11:44 -0400314
315 /* .. and repeat */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800316 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
Don Zickus58687ac2010-05-07 17:11:44 -0400317
318 if (touch_ts == 0) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100319 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
Don Zickus58687ac2010-05-07 17:11:44 -0400320 /*
321 * If the time stamp was touched atomically
322 * make sure the scheduler tick is up to date.
323 */
Christoph Lameter909ea962010-12-08 16:22:55 +0100324 __this_cpu_write(softlockup_touch_sync, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400325 sched_clock_tick();
326 }
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500327
328 /* Clear the guest paused flag on watchdog reset */
329 kvm_check_and_clear_guest_paused();
Don Zickus58687ac2010-05-07 17:11:44 -0400330 __touch_watchdog();
331 return HRTIMER_RESTART;
332 }
333
334 /* check for a softlockup
335 * This is done by making sure a high priority task is
336 * being scheduled. The task touches the watchdog to
337 * indicate it is getting cpu time. If it hasn't then
338 * this is a good indication some task is hogging the cpu
339 */
Don Zickus26e09c62010-05-17 18:06:04 -0400340 duration = is_softlockup(touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400341 if (unlikely(duration)) {
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500342 /*
343 * If a virtual machine is stopped by the host it can look to
344 * the watchdog like a soft lockup, check to see if the host
345 * stopped the vm before we issue the warning
346 */
347 if (kvm_check_and_clear_guest_paused())
348 return HRTIMER_RESTART;
349
Don Zickus58687ac2010-05-07 17:11:44 -0400350 /* only warn once */
chai wenb1a8de12014-10-09 15:25:17 -0700351 if (__this_cpu_read(soft_watchdog_warn) == true) {
352 /*
353 * When multiple processes are causing softlockups the
354 * softlockup detector only warns on the first one
355 * because the code relies on a full quiet cycle to
356 * re-arm. The second process prevents the quiet cycle
357 * and never gets reported. Use task pointers to detect
358 * this.
359 */
360 if (__this_cpu_read(softlockup_task_ptr_saved) !=
361 current) {
362 __this_cpu_write(soft_watchdog_warn, false);
363 __touch_watchdog();
364 }
Don Zickus58687ac2010-05-07 17:11:44 -0400365 return HRTIMER_RESTART;
chai wenb1a8de12014-10-09 15:25:17 -0700366 }
Don Zickus58687ac2010-05-07 17:11:44 -0400367
Aaron Tomlined235872014-06-23 13:22:05 -0700368 if (softlockup_all_cpu_backtrace) {
369 /* Prevent multiple soft-lockup reports if one cpu is already
370 * engaged in dumping cpu back traces
371 */
372 if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
373 /* Someone else will report us. Let's give up */
374 __this_cpu_write(soft_watchdog_warn, true);
375 return HRTIMER_RESTART;
376 }
377 }
378
Fabian Frederick656c3b72014-08-06 16:04:03 -0700379 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
Don Zickus26e09c62010-05-17 18:06:04 -0400380 smp_processor_id(), duration,
Don Zickus58687ac2010-05-07 17:11:44 -0400381 current->comm, task_pid_nr(current));
chai wenb1a8de12014-10-09 15:25:17 -0700382 __this_cpu_write(softlockup_task_ptr_saved, current);
Don Zickus58687ac2010-05-07 17:11:44 -0400383 print_modules();
384 print_irqtrace_events(current);
385 if (regs)
386 show_regs(regs);
387 else
388 dump_stack();
389
Aaron Tomlined235872014-06-23 13:22:05 -0700390 if (softlockup_all_cpu_backtrace) {
391 /* Avoid generating two back traces for current
392 * given that one is already made above
393 */
394 trigger_allbutself_cpu_backtrace();
395
396 clear_bit(0, &soft_lockup_nmi_warn);
397 /* Barrier to sync with other cpus */
398 smp_mb__after_atomic();
399 }
400
Josh Hunt69361ee2014-08-08 14:22:31 -0700401 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
Don Zickus58687ac2010-05-07 17:11:44 -0400402 if (softlockup_panic)
403 panic("softlockup: hung tasks");
Christoph Lameter909ea962010-12-08 16:22:55 +0100404 __this_cpu_write(soft_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400405 } else
Christoph Lameter909ea962010-12-08 16:22:55 +0100406 __this_cpu_write(soft_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400407
408 return HRTIMER_RESTART;
409}
410
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000411static void watchdog_set_prio(unsigned int policy, unsigned int prio)
Don Zickus58687ac2010-05-07 17:11:44 -0400412{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000413 struct sched_param param = { .sched_priority = prio };
414
415 sched_setscheduler(current, policy, &param);
416}
417
418static void watchdog_enable(unsigned int cpu)
419{
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200420 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
Don Zickus58687ac2010-05-07 17:11:44 -0400421
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200422 /*
423 * Start the timer first to prevent the NMI watchdog triggering
424 * before the timer has a chance to fire.
425 */
Bjørn Mork3935e8952012-12-19 20:51:31 +0100426 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
427 hrtimer->function = watchdog_timer_fn;
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800428 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
Don Zickus58687ac2010-05-07 17:11:44 -0400429 HRTIMER_MODE_REL_PINNED);
430
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200431 /* Initialize timestamp */
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000432 __touch_watchdog();
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200433 /* Enable the perf event */
434 watchdog_nmi_enable(cpu);
435
436 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
Don Zickus58687ac2010-05-07 17:11:44 -0400437}
438
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000439static void watchdog_disable(unsigned int cpu)
440{
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200441 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000442
443 watchdog_set_prio(SCHED_NORMAL, 0);
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200444 /*
445 * Disable the perf event first. That prevents that a large delay
446 * between disabling the timer and disabling the perf event causes
447 * the perf NMI to detect a false positive.
448 */
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000449 watchdog_nmi_disable(cpu);
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200450 hrtimer_cancel(hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000451}
452
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200453static void watchdog_cleanup(unsigned int cpu, bool online)
454{
455 watchdog_disable(cpu);
456}
457
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000458static int watchdog_should_run(unsigned int cpu)
459{
460 return __this_cpu_read(hrtimer_interrupts) !=
461 __this_cpu_read(soft_lockup_hrtimer_cnt);
462}
463
464/*
465 * The watchdog thread function - touches the timestamp.
466 *
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800467 * It only runs once every sample_period seconds (4 seconds by
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000468 * default) to reset the softlockup timestamp. If this gets delayed
469 * for more than 2*watchdog_thresh seconds then the debug-printout
470 * triggers in watchdog_timer_fn().
471 */
472static void watchdog(unsigned int cpu)
473{
474 __this_cpu_write(soft_lockup_hrtimer_cnt,
475 __this_cpu_read(hrtimer_interrupts));
476 __touch_watchdog();
477}
Don Zickus58687ac2010-05-07 17:11:44 -0400478
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200479static struct smp_hotplug_thread watchdog_threads = {
480 .store = &softlockup_watchdog,
481 .thread_should_run = watchdog_should_run,
482 .thread_fn = watchdog,
483 .thread_comm = "watchdog/%u",
484 .setup = watchdog_enable,
485 .cleanup = watchdog_cleanup,
486 .park = watchdog_disable,
487 .unpark = watchdog_enable,
488};
489
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200490static void softlockup_update_smpboot_threads(void)
491{
492 lockdep_assert_held(&watchdog_mutex);
493
494 if (!softlockup_threads_initialized)
495 return;
496
497 smpboot_update_cpumask_percpu_thread(&watchdog_threads,
498 &watchdog_allowed_mask);
499 __lockup_detector_cleanup();
500}
501
502/* Temporarily park all watchdog threads */
503static void softlockup_park_all_threads(void)
504{
505 cpumask_clear(&watchdog_allowed_mask);
506 softlockup_update_smpboot_threads();
507}
508
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200509/* Unpark enabled threads */
510static void softlockup_unpark_threads(void)
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200511{
512 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
513 softlockup_update_smpboot_threads();
514}
515
516static void softlockup_reconfigure_threads(bool enabled)
517{
518 softlockup_park_all_threads();
519 set_sample_period();
520 if (enabled)
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200521 softlockup_unpark_threads();
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200522}
523
524/*
525 * Create the watchdog thread infrastructure.
526 *
527 * The threads are not unparked as watchdog_allowed_mask is empty. When
528 * the threads are sucessfully initialized, take the proper locks and
529 * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
530 */
531static __init void softlockup_init_threads(void)
532{
533 int ret;
534
535 /*
536 * If sysctl is off and watchdog got disabled on the command line,
537 * nothing to do here.
538 */
539 if (!IS_ENABLED(CONFIG_SYSCTL) &&
540 !(watchdog_enabled && watchdog_thresh))
541 return;
542
543 ret = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
544 &watchdog_allowed_mask);
545 if (ret) {
546 pr_err("Failed to initialize soft lockup detector threads\n");
547 return;
548 }
549
550 mutex_lock(&watchdog_mutex);
551 softlockup_threads_initialized = true;
552 softlockup_reconfigure_threads(watchdog_enabled && watchdog_thresh);
553 mutex_unlock(&watchdog_mutex);
554}
555
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200556#else /* CONFIG_SOFTLOCKUP_DETECTOR */
557static inline int watchdog_park_threads(void) { return 0; }
558static inline void watchdog_unpark_threads(void) { }
559static inline int watchdog_enable_all_cpus(void) { return 0; }
560static inline void watchdog_disable_all_cpus(void) { }
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200561static inline void softlockup_init_threads(void) { }
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200562static inline void softlockup_reconfigure_threads(bool enabled) { }
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200563#endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700564
Thomas Gleixner941154b2017-09-12 21:37:04 +0200565static void __lockup_detector_cleanup(void)
566{
567 lockdep_assert_held(&watchdog_mutex);
568 hardlockup_detector_perf_cleanup();
569}
570
571/**
572 * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
573 *
574 * Caller must not hold the cpu hotplug rwsem.
575 */
576void lockup_detector_cleanup(void)
577{
578 mutex_lock(&watchdog_mutex);
579 __lockup_detector_cleanup();
580 mutex_unlock(&watchdog_mutex);
581}
582
Thomas Gleixner6554fd82017-09-12 21:36:57 +0200583/**
584 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
585 *
586 * Special interface for parisc. It prevents lockup detector warnings from
587 * the default pm_poweroff() function which busy loops forever.
588 */
589void lockup_detector_soft_poweroff(void)
590{
591 watchdog_enabled = 0;
592}
593
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800594#ifdef CONFIG_SYSCTL
595
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200596/* Propagate any changes to the watchdog threads */
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200597static void proc_watchdog_update(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400598{
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200599 /* Remove impossible cpus to keep sysctl output clean. */
600 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200601 softlockup_reconfigure_threads(watchdog_enabled && watchdog_thresh);
Nicholas Piggina10a8422017-07-12 14:35:49 -0700602 watchdog_nmi_reconfigure();
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700603}
604
605/*
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700606 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
607 *
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +0200608 * caller | table->data points to | 'which'
609 * -------------------|----------------------------|--------------------------
610 * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED |
611 * | | SOFT_WATCHDOG_ENABLED
612 * -------------------|----------------------------|--------------------------
613 * proc_nmi_watchdog | nmi_watchdog_user_enabled | NMI_WATCHDOG_ENABLED
614 * -------------------|----------------------------|--------------------------
615 * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700616 */
617static int proc_watchdog_common(int which, struct ctl_table *table, int write,
618 void __user *buffer, size_t *lenp, loff_t *ppos)
619{
620 int err, old, new;
621 int *watchdog_param = (int *)table->data;
Don Zickus58687ac2010-05-07 17:11:44 -0400622
Thomas Gleixnerb7a34982017-09-12 21:37:00 +0200623 cpu_hotplug_disable();
Thomas Gleixner946d1972017-09-12 21:37:01 +0200624 mutex_lock(&watchdog_mutex);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000625
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700626 /*
627 * If the parameter is being read return the state of the corresponding
628 * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
629 * run state of the lockup detectors.
630 */
631 if (!write) {
632 *watchdog_param = (watchdog_enabled & which) != 0;
633 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
634 } else {
635 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
636 if (err)
637 goto out;
638
639 /*
640 * There is a race window between fetching the current value
641 * from 'watchdog_enabled' and storing the new value. During
642 * this race window, watchdog_nmi_enable() can sneak in and
643 * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
644 * The 'cmpxchg' detects this race and the loop retries.
645 */
646 do {
647 old = watchdog_enabled;
648 /*
649 * If the parameter value is not zero set the
650 * corresponding bit(s), else clear it(them).
651 */
652 if (*watchdog_param)
653 new = old | which;
654 else
655 new = old & ~which;
656 } while (cmpxchg(&watchdog_enabled, old, new) != old);
657
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200658 if (old != new)
659 proc_watchdog_update();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700660 }
661out:
Thomas Gleixner946d1972017-09-12 21:37:01 +0200662 mutex_unlock(&watchdog_mutex);
Thomas Gleixnerb7a34982017-09-12 21:37:00 +0200663 cpu_hotplug_enable();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700664 return err;
665}
666
667/*
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700668 * /proc/sys/kernel/watchdog
669 */
670int proc_watchdog(struct ctl_table *table, int write,
671 void __user *buffer, size_t *lenp, loff_t *ppos)
672{
673 return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
674 table, write, buffer, lenp, ppos);
675}
676
677/*
678 * /proc/sys/kernel/nmi_watchdog
679 */
680int proc_nmi_watchdog(struct ctl_table *table, int write,
681 void __user *buffer, size_t *lenp, loff_t *ppos)
682{
683 return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
684 table, write, buffer, lenp, ppos);
685}
686
687/*
688 * /proc/sys/kernel/soft_watchdog
689 */
690int proc_soft_watchdog(struct ctl_table *table, int write,
691 void __user *buffer, size_t *lenp, loff_t *ppos)
692{
693 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
694 table, write, buffer, lenp, ppos);
695}
696
697/*
698 * /proc/sys/kernel/watchdog_thresh
699 */
700int proc_watchdog_thresh(struct ctl_table *table, int write,
701 void __user *buffer, size_t *lenp, loff_t *ppos)
702{
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200703 int err, old;
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700704
Thomas Gleixnerb7a34982017-09-12 21:37:00 +0200705 cpu_hotplug_disable();
Thomas Gleixner946d1972017-09-12 21:37:01 +0200706 mutex_lock(&watchdog_mutex);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700707
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200708 old = READ_ONCE(watchdog_thresh);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200709 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700710
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200711 if (!err && write && old != READ_ONCE(watchdog_thresh))
712 proc_watchdog_update();
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700713
Thomas Gleixner946d1972017-09-12 21:37:01 +0200714 mutex_unlock(&watchdog_mutex);
Thomas Gleixnerb7a34982017-09-12 21:37:00 +0200715 cpu_hotplug_enable();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200716 return err;
Don Zickus58687ac2010-05-07 17:11:44 -0400717}
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700718
719/*
720 * The cpumask is the mask of possible cpus that the watchdog can run
721 * on, not the mask of cpus it is actually running on. This allows the
722 * user to specify a mask that will include cpus that have not yet
723 * been brought online, if desired.
724 */
725int proc_watchdog_cpumask(struct ctl_table *table, int write,
726 void __user *buffer, size_t *lenp, loff_t *ppos)
727{
728 int err;
729
Thomas Gleixnerb7a34982017-09-12 21:37:00 +0200730 cpu_hotplug_disable();
Thomas Gleixner946d1972017-09-12 21:37:01 +0200731 mutex_lock(&watchdog_mutex);
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700732
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700733 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
Thomas Gleixner05ba3de2017-09-12 21:37:08 +0200734 if (!err && write)
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200735 proc_watchdog_update();
Thomas Gleixner54901252017-09-12 21:36:59 +0200736
Thomas Gleixner946d1972017-09-12 21:37:01 +0200737 mutex_unlock(&watchdog_mutex);
Thomas Gleixnerb7a34982017-09-12 21:37:00 +0200738 cpu_hotplug_enable();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700739 return err;
740}
Don Zickus58687ac2010-05-07 17:11:44 -0400741#endif /* CONFIG_SYSCTL */
742
Peter Zijlstra004417a2010-11-25 18:38:29 +0100743void __init lockup_detector_init(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400744{
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700745#ifdef CONFIG_NO_HZ_FULL
746 if (tick_nohz_full_enabled()) {
Frederic Weisbecker314b08ff2015-09-04 15:45:09 -0700747 pr_info("Disabling watchdog on nohz_full cores by default\n");
748 cpumask_copy(&watchdog_cpumask, housekeeping_mask);
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700749 } else
750 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
751#else
752 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
753#endif
754
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200755 softlockup_init_threads();
Don Zickus58687ac2010-05-07 17:11:44 -0400756}