blob: 562652c9c8157d3c87f8ff63d9dfd61d83807a24 [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>
Frederic Weisbecker78634062017-10-27 04:42:28 +020027#include <linux/sched/isolation.h>
Don Zickus58687ac2010-05-07 17:11:44 -040028
29#include <asm/irq_regs.h>
Eric B Munson5d1c0f42012-03-10 14:37:28 -050030#include <linux/kvm_para.h>
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -070031#include <linux/kthread.h>
Don Zickus58687ac2010-05-07 17:11:44 -040032
Thomas Gleixner946d1972017-09-12 21:37:01 +020033static DEFINE_MUTEX(watchdog_mutex);
Peter Zijlstraab992dc2015-05-18 11:31:50 +020034
Nicholas Piggin05a4a952017-07-12 14:35:46 -070035#if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
Thomas Gleixner09154982017-09-12 21:37:17 +020036# define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED | NMI_WATCHDOG_ENABLED)
37# define NMI_WATCHDOG_DEFAULT 1
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070038#else
Thomas Gleixner09154982017-09-12 21:37:17 +020039# define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED)
40# define NMI_WATCHDOG_DEFAULT 0
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070041#endif
Nicholas Piggin05a4a952017-07-12 14:35:46 -070042
Thomas Gleixner09154982017-09-12 21:37:17 +020043unsigned long __read_mostly watchdog_enabled;
44int __read_mostly watchdog_user_enabled = 1;
45int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
46int __read_mostly soft_watchdog_user_enabled = 1;
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +020047int __read_mostly watchdog_thresh = 10;
Thomas Gleixnera994a312017-09-12 21:37:19 +020048int __read_mostly nmi_watchdog_available;
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +020049
50struct cpumask watchdog_allowed_mask __read_mostly;
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +020051
52struct cpumask watchdog_cpumask __read_mostly;
53unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
54
Nicholas Piggin05a4a952017-07-12 14:35:46 -070055#ifdef CONFIG_HARDLOCKUP_DETECTOR
Nicholas Piggin05a4a952017-07-12 14:35:46 -070056/*
57 * Should we panic when a soft-lockup or hard-lockup occurs:
58 */
59unsigned int __read_mostly hardlockup_panic =
60 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
61/*
62 * We may not want to enable hard lockup detection by default in all cases,
63 * for example when running the kernel as a guest on a hypervisor. In these
64 * cases this function can be called to disable hard lockup detection. This
65 * function should only be executed once by the boot processor before the
66 * kernel command line parameters are parsed, because otherwise it is not
67 * possible to override this in hardlockup_panic_setup().
68 */
Thomas Gleixner7a355822017-09-12 21:37:02 +020069void __init hardlockup_detector_disable(void)
Nicholas Piggin05a4a952017-07-12 14:35:46 -070070{
Thomas Gleixner09154982017-09-12 21:37:17 +020071 nmi_watchdog_user_enabled = 0;
Nicholas Piggin05a4a952017-07-12 14:35:46 -070072}
73
74static int __init hardlockup_panic_setup(char *str)
75{
76 if (!strncmp(str, "panic", 5))
77 hardlockup_panic = 1;
78 else if (!strncmp(str, "nopanic", 7))
79 hardlockup_panic = 0;
80 else if (!strncmp(str, "0", 1))
Thomas Gleixner09154982017-09-12 21:37:17 +020081 nmi_watchdog_user_enabled = 0;
Nicholas Piggin05a4a952017-07-12 14:35:46 -070082 else if (!strncmp(str, "1", 1))
Thomas Gleixner09154982017-09-12 21:37:17 +020083 nmi_watchdog_user_enabled = 1;
Nicholas Piggin05a4a952017-07-12 14:35:46 -070084 return 1;
85}
86__setup("nmi_watchdog=", hardlockup_panic_setup);
87
Thomas Gleixner368a7e22017-09-12 21:37:07 +020088# ifdef CONFIG_SMP
89int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
Nicholas Piggin05a4a952017-07-12 14:35:46 -070090
Thomas Gleixner368a7e22017-09-12 21:37:07 +020091static int __init hardlockup_all_cpu_backtrace_setup(char *str)
92{
93 sysctl_hardlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
94 return 1;
95}
96__setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
97# endif /* CONFIG_SMP */
98#endif /* CONFIG_HARDLOCKUP_DETECTOR */
Nicholas Piggin05a4a952017-07-12 14:35:46 -070099
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700100/*
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700101 * These functions can be overridden if an architecture implements its
102 * own hardlockup detector.
Nicholas Piggina10a8422017-07-12 14:35:49 -0700103 *
104 * watchdog_nmi_enable/disable can be implemented to start and stop when
105 * softlockup watchdog threads start and stop. The arch must select the
106 * SOFTLOCKUP_DETECTOR Kconfig.
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700107 */
108int __weak watchdog_nmi_enable(unsigned int cpu)
109{
Thomas Gleixner146c9d02017-09-12 21:37:21 +0200110 hardlockup_detector_perf_enable();
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700111 return 0;
112}
Thomas Gleixner941154b2017-09-12 21:37:04 +0200113
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700114void __weak watchdog_nmi_disable(unsigned int cpu)
115{
Thomas Gleixner941154b2017-09-12 21:37:04 +0200116 hardlockup_detector_perf_disable();
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700117}
118
Thomas Gleixnera994a312017-09-12 21:37:19 +0200119/* Return 0, if a NMI watchdog is available. Error code otherwise */
120int __weak __init watchdog_nmi_probe(void)
121{
122 return hardlockup_detector_perf_init();
123}
124
Thomas Gleixner6592ad22017-09-12 21:37:16 +0200125/**
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200126 * watchdog_nmi_stop - Stop the watchdog for reconfiguration
Thomas Gleixner6592ad22017-09-12 21:37:16 +0200127 *
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200128 * The reconfiguration steps are:
129 * watchdog_nmi_stop();
Thomas Gleixner6592ad22017-09-12 21:37:16 +0200130 * update_variables();
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200131 * watchdog_nmi_start();
132 */
133void __weak watchdog_nmi_stop(void) { }
134
135/**
136 * watchdog_nmi_start - Start the watchdog after reconfiguration
Thomas Gleixner6592ad22017-09-12 21:37:16 +0200137 *
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200138 * Counterpart to watchdog_nmi_stop().
139 *
140 * The following variables have been updated in update_variables() and
141 * contain the currently valid configuration:
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +0200142 * - watchdog_enabled
Nicholas Piggina10a8422017-07-12 14:35:49 -0700143 * - watchdog_thresh
144 * - watchdog_cpumask
Nicholas Piggina10a8422017-07-12 14:35:49 -0700145 */
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200146void __weak watchdog_nmi_start(void) { }
Nicholas Piggina10a8422017-07-12 14:35:49 -0700147
Thomas Gleixner09154982017-09-12 21:37:17 +0200148/**
149 * lockup_detector_update_enable - Update the sysctl enable bit
150 *
151 * Caller needs to make sure that the NMI/perf watchdogs are off, so this
152 * can't race with watchdog_nmi_disable().
153 */
154static void lockup_detector_update_enable(void)
155{
156 watchdog_enabled = 0;
157 if (!watchdog_user_enabled)
158 return;
Thomas Gleixnera994a312017-09-12 21:37:19 +0200159 if (nmi_watchdog_available && nmi_watchdog_user_enabled)
Thomas Gleixner09154982017-09-12 21:37:17 +0200160 watchdog_enabled |= NMI_WATCHDOG_ENABLED;
161 if (soft_watchdog_user_enabled)
162 watchdog_enabled |= SOFT_WATCHDOG_ENABLED;
163}
164
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700165#ifdef CONFIG_SOFTLOCKUP_DETECTOR
166
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200167/* Global variables, exported for sysctl */
168unsigned int __read_mostly softlockup_panic =
169 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200170
Thomas Gleixner0b62bf82017-10-02 20:59:09 +0200171static bool softlockup_threads_initialized __read_mostly;
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800172static u64 __read_mostly sample_period;
Don Zickus58687ac2010-05-07 17:11:44 -0400173
174static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
175static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
176static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
177static DEFINE_PER_CPU(bool, softlockup_touch_sync);
Don Zickus58687ac2010-05-07 17:11:44 -0400178static DEFINE_PER_CPU(bool, soft_watchdog_warn);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000179static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
180static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
chai wenb1a8de12014-10-09 15:25:17 -0700181static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
Don Zickus58687ac2010-05-07 17:11:44 -0400182static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
Aaron Tomlined235872014-06-23 13:22:05 -0700183static unsigned long soft_lockup_nmi_warn;
Don Zickus58687ac2010-05-07 17:11:44 -0400184
Don Zickus58687ac2010-05-07 17:11:44 -0400185static int __init softlockup_panic_setup(char *str)
186{
187 softlockup_panic = simple_strtoul(str, NULL, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400188 return 1;
189}
190__setup("softlockup_panic=", softlockup_panic_setup);
191
192static int __init nowatchdog_setup(char *str)
193{
Thomas Gleixner09154982017-09-12 21:37:17 +0200194 watchdog_user_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -0400195 return 1;
196}
197__setup("nowatchdog", nowatchdog_setup);
198
Don Zickus58687ac2010-05-07 17:11:44 -0400199static int __init nosoftlockup_setup(char *str)
200{
Thomas Gleixner09154982017-09-12 21:37:17 +0200201 soft_watchdog_user_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -0400202 return 1;
203}
204__setup("nosoftlockup", nosoftlockup_setup);
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700205
Aaron Tomlined235872014-06-23 13:22:05 -0700206#ifdef CONFIG_SMP
Thomas Gleixner368a7e22017-09-12 21:37:07 +0200207int __read_mostly sysctl_softlockup_all_cpu_backtrace;
208
Aaron Tomlined235872014-06-23 13:22:05 -0700209static int __init softlockup_all_cpu_backtrace_setup(char *str)
210{
Thomas Gleixner368a7e22017-09-12 21:37:07 +0200211 sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
Aaron Tomlined235872014-06-23 13:22:05 -0700212 return 1;
213}
214__setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700215#endif
Don Zickus58687ac2010-05-07 17:11:44 -0400216
Thomas Gleixner941154b2017-09-12 21:37:04 +0200217static void __lockup_detector_cleanup(void);
218
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700219/*
220 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
221 * lockups can have false positives under extreme conditions. So we generally
222 * want a higher threshold for soft lockups than for hard lockups. So we couple
223 * the thresholds with a factor: we make the soft threshold twice the amount of
224 * time the hard threshold is.
225 */
Ingo Molnar6e9101a2011-05-24 05:43:18 +0200226static int get_softlockup_thresh(void)
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700227{
228 return watchdog_thresh * 2;
229}
Don Zickus58687ac2010-05-07 17:11:44 -0400230
231/*
232 * Returns seconds, approximately. We don't need nanosecond
233 * resolution, and we don't need to waste time with a big divide when
234 * 2^30ns == 1.074s.
235 */
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900236static unsigned long get_timestamp(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400237{
Cyril Bur545a2bf2015-02-12 15:01:24 -0800238 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
Don Zickus58687ac2010-05-07 17:11:44 -0400239}
240
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800241static void set_sample_period(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400242{
243 /*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700244 * convert watchdog_thresh from seconds to ns
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -0500245 * the divide by 5 is to give hrtimer several chances (two
246 * or three with the current relation between the soft
247 * and hard thresholds) to increment before the
248 * hardlockup detector generates a warning
Don Zickus58687ac2010-05-07 17:11:44 -0400249 */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800250 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
Thomas Gleixner7edaeb62017-08-15 09:50:13 +0200251 watchdog_update_hrtimer_threshold(sample_period);
Don Zickus58687ac2010-05-07 17:11:44 -0400252}
253
254/* Commands for resetting the watchdog */
255static void __touch_watchdog(void)
256{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900257 __this_cpu_write(watchdog_touch_ts, get_timestamp());
Don Zickus58687ac2010-05-07 17:11:44 -0400258}
259
Tejun Heo03e0d462015-12-08 11:28:04 -0500260/**
261 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
262 *
263 * Call when the scheduler may have stalled for legitimate reasons
264 * preventing the watchdog task from executing - e.g. the scheduler
265 * entering idle state. This should only be used for scheduler events.
266 * Use touch_softlockup_watchdog() for everything else.
267 */
268void touch_softlockup_watchdog_sched(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400269{
Andrew Morton78611442014-04-18 15:07:12 -0700270 /*
271 * Preemption can be enabled. It doesn't matter which CPU's timestamp
272 * gets zeroed here, so use the raw_ operation.
273 */
274 raw_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400275}
Tejun Heo03e0d462015-12-08 11:28:04 -0500276
277void touch_softlockup_watchdog(void)
278{
279 touch_softlockup_watchdog_sched();
Tejun Heo82607adc2015-12-08 11:28:04 -0500280 wq_watchdog_touch(raw_smp_processor_id());
Tejun Heo03e0d462015-12-08 11:28:04 -0500281}
Ingo Molnar0167c782010-05-13 08:53:33 +0200282EXPORT_SYMBOL(touch_softlockup_watchdog);
Don Zickus58687ac2010-05-07 17:11:44 -0400283
Don Zickus332fbdb2010-05-07 17:11:45 -0400284void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400285{
286 int cpu;
287
288 /*
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200289 * watchdog_mutex cannpt be taken here, as this might be called
290 * from (soft)interrupt context, so the access to
291 * watchdog_allowed_cpumask might race with a concurrent update.
292 *
293 * The watchdog time stamp can race against a concurrent real
294 * update as well, the only side effect might be a cycle delay for
295 * the softlockup check.
Don Zickus58687ac2010-05-07 17:11:44 -0400296 */
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200297 for_each_cpu(cpu, &watchdog_allowed_mask)
Don Zickus58687ac2010-05-07 17:11:44 -0400298 per_cpu(watchdog_touch_ts, cpu) = 0;
Tejun Heo82607adc2015-12-08 11:28:04 -0500299 wq_watchdog_touch(-1);
Don Zickus58687ac2010-05-07 17:11:44 -0400300}
301
Don Zickus58687ac2010-05-07 17:11:44 -0400302void touch_softlockup_watchdog_sync(void)
303{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500304 __this_cpu_write(softlockup_touch_sync, true);
305 __this_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400306}
307
Don Zickus26e09c62010-05-17 18:06:04 -0400308static int is_softlockup(unsigned long touch_ts)
Don Zickus58687ac2010-05-07 17:11:44 -0400309{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900310 unsigned long now = get_timestamp();
Don Zickus58687ac2010-05-07 17:11:44 -0400311
Ulrich Obergfell39d2da22015-11-05 18:44:56 -0800312 if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700313 /* Warn about unreasonable delays. */
314 if (time_after(now, touch_ts + get_softlockup_thresh()))
315 return now - touch_ts;
316 }
Don Zickus58687ac2010-05-07 17:11:44 -0400317 return 0;
318}
319
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700320/* watchdog detector functions */
321bool is_hardlockup(void)
322{
323 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
324
325 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
326 return true;
327
328 __this_cpu_write(hrtimer_interrupts_saved, hrint);
329 return false;
330}
331
Don Zickus58687ac2010-05-07 17:11:44 -0400332static void watchdog_interrupt_count(void)
333{
Christoph Lameter909ea962010-12-08 16:22:55 +0100334 __this_cpu_inc(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400335}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000336
Don Zickus58687ac2010-05-07 17:11:44 -0400337/* watchdog kicker functions */
338static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
339{
Christoph Lameter909ea962010-12-08 16:22:55 +0100340 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400341 struct pt_regs *regs = get_irq_regs();
342 int duration;
Aaron Tomlined235872014-06-23 13:22:05 -0700343 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
Don Zickus58687ac2010-05-07 17:11:44 -0400344
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200345 if (!watchdog_enabled)
Don Zickusb94f5112017-01-24 15:17:53 -0800346 return HRTIMER_NORESTART;
347
Don Zickus58687ac2010-05-07 17:11:44 -0400348 /* kick the hardlockup detector */
349 watchdog_interrupt_count();
350
351 /* kick the softlockup detector */
Christoph Lameter909ea962010-12-08 16:22:55 +0100352 wake_up_process(__this_cpu_read(softlockup_watchdog));
Don Zickus58687ac2010-05-07 17:11:44 -0400353
354 /* .. and repeat */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800355 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
Don Zickus58687ac2010-05-07 17:11:44 -0400356
357 if (touch_ts == 0) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100358 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
Don Zickus58687ac2010-05-07 17:11:44 -0400359 /*
360 * If the time stamp was touched atomically
361 * make sure the scheduler tick is up to date.
362 */
Christoph Lameter909ea962010-12-08 16:22:55 +0100363 __this_cpu_write(softlockup_touch_sync, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400364 sched_clock_tick();
365 }
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500366
367 /* Clear the guest paused flag on watchdog reset */
368 kvm_check_and_clear_guest_paused();
Don Zickus58687ac2010-05-07 17:11:44 -0400369 __touch_watchdog();
370 return HRTIMER_RESTART;
371 }
372
373 /* check for a softlockup
374 * This is done by making sure a high priority task is
375 * being scheduled. The task touches the watchdog to
376 * indicate it is getting cpu time. If it hasn't then
377 * this is a good indication some task is hogging the cpu
378 */
Don Zickus26e09c62010-05-17 18:06:04 -0400379 duration = is_softlockup(touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400380 if (unlikely(duration)) {
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500381 /*
382 * If a virtual machine is stopped by the host it can look to
383 * the watchdog like a soft lockup, check to see if the host
384 * stopped the vm before we issue the warning
385 */
386 if (kvm_check_and_clear_guest_paused())
387 return HRTIMER_RESTART;
388
Don Zickus58687ac2010-05-07 17:11:44 -0400389 /* only warn once */
chai wenb1a8de12014-10-09 15:25:17 -0700390 if (__this_cpu_read(soft_watchdog_warn) == true) {
391 /*
392 * When multiple processes are causing softlockups the
393 * softlockup detector only warns on the first one
394 * because the code relies on a full quiet cycle to
395 * re-arm. The second process prevents the quiet cycle
396 * and never gets reported. Use task pointers to detect
397 * this.
398 */
399 if (__this_cpu_read(softlockup_task_ptr_saved) !=
400 current) {
401 __this_cpu_write(soft_watchdog_warn, false);
402 __touch_watchdog();
403 }
Don Zickus58687ac2010-05-07 17:11:44 -0400404 return HRTIMER_RESTART;
chai wenb1a8de12014-10-09 15:25:17 -0700405 }
Don Zickus58687ac2010-05-07 17:11:44 -0400406
Aaron Tomlined235872014-06-23 13:22:05 -0700407 if (softlockup_all_cpu_backtrace) {
408 /* Prevent multiple soft-lockup reports if one cpu is already
409 * engaged in dumping cpu back traces
410 */
411 if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
412 /* Someone else will report us. Let's give up */
413 __this_cpu_write(soft_watchdog_warn, true);
414 return HRTIMER_RESTART;
415 }
416 }
417
Fabian Frederick656c3b72014-08-06 16:04:03 -0700418 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
Don Zickus26e09c62010-05-17 18:06:04 -0400419 smp_processor_id(), duration,
Don Zickus58687ac2010-05-07 17:11:44 -0400420 current->comm, task_pid_nr(current));
chai wenb1a8de12014-10-09 15:25:17 -0700421 __this_cpu_write(softlockup_task_ptr_saved, current);
Don Zickus58687ac2010-05-07 17:11:44 -0400422 print_modules();
423 print_irqtrace_events(current);
424 if (regs)
425 show_regs(regs);
426 else
427 dump_stack();
428
Aaron Tomlined235872014-06-23 13:22:05 -0700429 if (softlockup_all_cpu_backtrace) {
430 /* Avoid generating two back traces for current
431 * given that one is already made above
432 */
433 trigger_allbutself_cpu_backtrace();
434
435 clear_bit(0, &soft_lockup_nmi_warn);
436 /* Barrier to sync with other cpus */
437 smp_mb__after_atomic();
438 }
439
Josh Hunt69361ee2014-08-08 14:22:31 -0700440 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
Don Zickus58687ac2010-05-07 17:11:44 -0400441 if (softlockup_panic)
442 panic("softlockup: hung tasks");
Christoph Lameter909ea962010-12-08 16:22:55 +0100443 __this_cpu_write(soft_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400444 } else
Christoph Lameter909ea962010-12-08 16:22:55 +0100445 __this_cpu_write(soft_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400446
447 return HRTIMER_RESTART;
448}
449
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000450static void watchdog_set_prio(unsigned int policy, unsigned int prio)
Don Zickus58687ac2010-05-07 17:11:44 -0400451{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000452 struct sched_param param = { .sched_priority = prio };
453
454 sched_setscheduler(current, policy, &param);
455}
456
457static void watchdog_enable(unsigned int cpu)
458{
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200459 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
Don Zickus58687ac2010-05-07 17:11:44 -0400460
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200461 /*
462 * Start the timer first to prevent the NMI watchdog triggering
463 * before the timer has a chance to fire.
464 */
Bjørn Mork3935e8952012-12-19 20:51:31 +0100465 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
466 hrtimer->function = watchdog_timer_fn;
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800467 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
Don Zickus58687ac2010-05-07 17:11:44 -0400468 HRTIMER_MODE_REL_PINNED);
469
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200470 /* Initialize timestamp */
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000471 __touch_watchdog();
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200472 /* Enable the perf event */
Thomas Gleixner146c9d02017-09-12 21:37:21 +0200473 if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
474 watchdog_nmi_enable(cpu);
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200475
476 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
Don Zickus58687ac2010-05-07 17:11:44 -0400477}
478
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000479static void watchdog_disable(unsigned int cpu)
480{
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200481 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000482
483 watchdog_set_prio(SCHED_NORMAL, 0);
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200484 /*
485 * Disable the perf event first. That prevents that a large delay
486 * between disabling the timer and disabling the perf event causes
487 * the perf NMI to detect a false positive.
488 */
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000489 watchdog_nmi_disable(cpu);
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200490 hrtimer_cancel(hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000491}
492
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200493static void watchdog_cleanup(unsigned int cpu, bool online)
494{
495 watchdog_disable(cpu);
496}
497
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000498static int watchdog_should_run(unsigned int cpu)
499{
500 return __this_cpu_read(hrtimer_interrupts) !=
501 __this_cpu_read(soft_lockup_hrtimer_cnt);
502}
503
504/*
505 * The watchdog thread function - touches the timestamp.
506 *
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800507 * It only runs once every sample_period seconds (4 seconds by
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000508 * default) to reset the softlockup timestamp. If this gets delayed
509 * for more than 2*watchdog_thresh seconds then the debug-printout
510 * triggers in watchdog_timer_fn().
511 */
512static void watchdog(unsigned int cpu)
513{
514 __this_cpu_write(soft_lockup_hrtimer_cnt,
515 __this_cpu_read(hrtimer_interrupts));
516 __touch_watchdog();
517}
Don Zickus58687ac2010-05-07 17:11:44 -0400518
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200519static struct smp_hotplug_thread watchdog_threads = {
520 .store = &softlockup_watchdog,
521 .thread_should_run = watchdog_should_run,
522 .thread_fn = watchdog,
523 .thread_comm = "watchdog/%u",
524 .setup = watchdog_enable,
525 .cleanup = watchdog_cleanup,
526 .park = watchdog_disable,
527 .unpark = watchdog_enable,
528};
529
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200530static void softlockup_update_smpboot_threads(void)
531{
532 lockdep_assert_held(&watchdog_mutex);
533
534 if (!softlockup_threads_initialized)
535 return;
536
537 smpboot_update_cpumask_percpu_thread(&watchdog_threads,
538 &watchdog_allowed_mask);
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200539}
540
541/* Temporarily park all watchdog threads */
542static void softlockup_park_all_threads(void)
543{
544 cpumask_clear(&watchdog_allowed_mask);
545 softlockup_update_smpboot_threads();
546}
547
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200548/* Unpark enabled threads */
549static void softlockup_unpark_threads(void)
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200550{
551 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
552 softlockup_update_smpboot_threads();
553}
554
Thomas Gleixner55871852017-10-04 10:03:04 +0200555static void lockup_detector_reconfigure(void)
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200556{
Thomas Gleixnere31d6882017-10-03 16:37:53 +0200557 cpus_read_lock();
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200558 watchdog_nmi_stop();
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200559 softlockup_park_all_threads();
560 set_sample_period();
Thomas Gleixner09154982017-09-12 21:37:17 +0200561 lockup_detector_update_enable();
562 if (watchdog_enabled && watchdog_thresh)
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200563 softlockup_unpark_threads();
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200564 watchdog_nmi_start();
Thomas Gleixnere31d6882017-10-03 16:37:53 +0200565 cpus_read_unlock();
566 /*
567 * Must be called outside the cpus locked section to prevent
568 * recursive locking in the perf code.
569 */
570 __lockup_detector_cleanup();
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200571}
572
573/*
Thomas Gleixner55871852017-10-04 10:03:04 +0200574 * Create the watchdog thread infrastructure and configure the detector(s).
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200575 *
576 * The threads are not unparked as watchdog_allowed_mask is empty. When
577 * the threads are sucessfully initialized, take the proper locks and
578 * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
579 */
Thomas Gleixner55871852017-10-04 10:03:04 +0200580static __init void lockup_detector_setup(void)
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200581{
582 int ret;
583
584 /*
585 * If sysctl is off and watchdog got disabled on the command line,
586 * nothing to do here.
587 */
Thomas Gleixner09154982017-09-12 21:37:17 +0200588 lockup_detector_update_enable();
589
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200590 if (!IS_ENABLED(CONFIG_SYSCTL) &&
591 !(watchdog_enabled && watchdog_thresh))
592 return;
593
594 ret = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
595 &watchdog_allowed_mask);
596 if (ret) {
597 pr_err("Failed to initialize soft lockup detector threads\n");
598 return;
599 }
600
601 mutex_lock(&watchdog_mutex);
602 softlockup_threads_initialized = true;
Thomas Gleixner55871852017-10-04 10:03:04 +0200603 lockup_detector_reconfigure();
Thomas Gleixner2eb25272017-09-12 21:37:10 +0200604 mutex_unlock(&watchdog_mutex);
605}
606
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200607#else /* CONFIG_SOFTLOCKUP_DETECTOR */
608static inline int watchdog_park_threads(void) { return 0; }
609static inline void watchdog_unpark_threads(void) { }
610static inline int watchdog_enable_all_cpus(void) { return 0; }
611static inline void watchdog_disable_all_cpus(void) { }
Thomas Gleixner55871852017-10-04 10:03:04 +0200612static void lockup_detector_reconfigure(void)
Thomas Gleixner6592ad22017-09-12 21:37:16 +0200613{
Thomas Gleixnere31d6882017-10-03 16:37:53 +0200614 cpus_read_lock();
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200615 watchdog_nmi_stop();
Thomas Gleixner09154982017-09-12 21:37:17 +0200616 lockup_detector_update_enable();
Thomas Gleixner6b9dc482017-10-02 12:34:50 +0200617 watchdog_nmi_start();
Thomas Gleixnere31d6882017-10-03 16:37:53 +0200618 cpus_read_unlock();
Thomas Gleixner6592ad22017-09-12 21:37:16 +0200619}
Thomas Gleixner55871852017-10-04 10:03:04 +0200620static inline void lockup_detector_setup(void)
Thomas Gleixner34ddaa32017-10-03 16:39:02 +0200621{
Thomas Gleixner55871852017-10-04 10:03:04 +0200622 lockup_detector_reconfigure();
Thomas Gleixner34ddaa32017-10-03 16:39:02 +0200623}
Thomas Gleixner2b9d7f22017-09-12 21:37:06 +0200624#endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
Nicholas Piggin05a4a952017-07-12 14:35:46 -0700625
Thomas Gleixner941154b2017-09-12 21:37:04 +0200626static void __lockup_detector_cleanup(void)
627{
628 lockdep_assert_held(&watchdog_mutex);
629 hardlockup_detector_perf_cleanup();
630}
631
632/**
633 * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
634 *
635 * Caller must not hold the cpu hotplug rwsem.
636 */
637void lockup_detector_cleanup(void)
638{
639 mutex_lock(&watchdog_mutex);
640 __lockup_detector_cleanup();
641 mutex_unlock(&watchdog_mutex);
642}
643
Thomas Gleixner6554fd82017-09-12 21:36:57 +0200644/**
645 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
646 *
647 * Special interface for parisc. It prevents lockup detector warnings from
648 * the default pm_poweroff() function which busy loops forever.
649 */
650void lockup_detector_soft_poweroff(void)
651{
652 watchdog_enabled = 0;
653}
654
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800655#ifdef CONFIG_SYSCTL
656
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200657/* Propagate any changes to the watchdog threads */
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200658static void proc_watchdog_update(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400659{
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200660 /* Remove impossible cpus to keep sysctl output clean. */
661 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
Thomas Gleixner55871852017-10-04 10:03:04 +0200662 lockup_detector_reconfigure();
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700663}
664
665/*
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700666 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
667 *
Thomas Gleixner7feeb9c2017-09-12 21:37:15 +0200668 * caller | table->data points to | 'which'
669 * -------------------|----------------------------|--------------------------
670 * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED |
671 * | | SOFT_WATCHDOG_ENABLED
672 * -------------------|----------------------------|--------------------------
673 * proc_nmi_watchdog | nmi_watchdog_user_enabled | NMI_WATCHDOG_ENABLED
674 * -------------------|----------------------------|--------------------------
675 * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700676 */
677static int proc_watchdog_common(int which, struct ctl_table *table, int write,
678 void __user *buffer, size_t *lenp, loff_t *ppos)
679{
Thomas Gleixner09154982017-09-12 21:37:17 +0200680 int err, old, *param = table->data;
Don Zickus58687ac2010-05-07 17:11:44 -0400681
Thomas Gleixner946d1972017-09-12 21:37:01 +0200682 mutex_lock(&watchdog_mutex);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000683
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700684 if (!write) {
Thomas Gleixner09154982017-09-12 21:37:17 +0200685 /*
686 * On read synchronize the userspace interface. This is a
687 * racy snapshot.
688 */
689 *param = (watchdog_enabled & which) != 0;
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700690 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
691 } else {
Thomas Gleixner09154982017-09-12 21:37:17 +0200692 old = READ_ONCE(*param);
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700693 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Thomas Gleixner09154982017-09-12 21:37:17 +0200694 if (!err && old != READ_ONCE(*param))
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200695 proc_watchdog_update();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700696 }
Thomas Gleixner946d1972017-09-12 21:37:01 +0200697 mutex_unlock(&watchdog_mutex);
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700698 return err;
699}
700
701/*
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700702 * /proc/sys/kernel/watchdog
703 */
704int proc_watchdog(struct ctl_table *table, int write,
705 void __user *buffer, size_t *lenp, loff_t *ppos)
706{
707 return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
708 table, write, buffer, lenp, ppos);
709}
710
711/*
712 * /proc/sys/kernel/nmi_watchdog
713 */
714int proc_nmi_watchdog(struct ctl_table *table, int write,
715 void __user *buffer, size_t *lenp, loff_t *ppos)
716{
Thomas Gleixnera994a312017-09-12 21:37:19 +0200717 if (!nmi_watchdog_available && write)
718 return -ENOTSUPP;
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700719 return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
720 table, write, buffer, lenp, ppos);
721}
722
723/*
724 * /proc/sys/kernel/soft_watchdog
725 */
726int proc_soft_watchdog(struct ctl_table *table, int write,
727 void __user *buffer, size_t *lenp, loff_t *ppos)
728{
729 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
730 table, write, buffer, lenp, ppos);
731}
732
733/*
734 * /proc/sys/kernel/watchdog_thresh
735 */
736int proc_watchdog_thresh(struct ctl_table *table, int write,
737 void __user *buffer, size_t *lenp, loff_t *ppos)
738{
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200739 int err, old;
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700740
Thomas Gleixner946d1972017-09-12 21:37:01 +0200741 mutex_lock(&watchdog_mutex);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700742
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200743 old = READ_ONCE(watchdog_thresh);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200744 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700745
Thomas Gleixnerd57108d2017-09-12 21:37:11 +0200746 if (!err && write && old != READ_ONCE(watchdog_thresh))
747 proc_watchdog_update();
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700748
Thomas Gleixner946d1972017-09-12 21:37:01 +0200749 mutex_unlock(&watchdog_mutex);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200750 return err;
Don Zickus58687ac2010-05-07 17:11:44 -0400751}
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700752
753/*
754 * The cpumask is the mask of possible cpus that the watchdog can run
755 * on, not the mask of cpus it is actually running on. This allows the
756 * user to specify a mask that will include cpus that have not yet
757 * been brought online, if desired.
758 */
759int proc_watchdog_cpumask(struct ctl_table *table, int write,
760 void __user *buffer, size_t *lenp, loff_t *ppos)
761{
762 int err;
763
Thomas Gleixner946d1972017-09-12 21:37:01 +0200764 mutex_lock(&watchdog_mutex);
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700765
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700766 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
Thomas Gleixner05ba3de2017-09-12 21:37:08 +0200767 if (!err && write)
Thomas Gleixnere8b62b22017-09-12 21:37:12 +0200768 proc_watchdog_update();
Thomas Gleixner54901252017-09-12 21:36:59 +0200769
Thomas Gleixner946d1972017-09-12 21:37:01 +0200770 mutex_unlock(&watchdog_mutex);
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700771 return err;
772}
Don Zickus58687ac2010-05-07 17:11:44 -0400773#endif /* CONFIG_SYSCTL */
774
Peter Zijlstra004417a2010-11-25 18:38:29 +0100775void __init lockup_detector_init(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400776{
Frederic Weisbecker13316b32017-10-27 04:42:29 +0200777 if (tick_nohz_full_enabled())
Frederic Weisbecker314b08ff2015-09-04 15:45:09 -0700778 pr_info("Disabling watchdog on nohz_full cores by default\n");
Frederic Weisbecker13316b32017-10-27 04:42:29 +0200779
780 cpumask_copy(&watchdog_cpumask, housekeeping_cpumask());
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700781
Thomas Gleixnera994a312017-09-12 21:37:19 +0200782 if (!watchdog_nmi_probe())
783 nmi_watchdog_available = true;
Thomas Gleixner55871852017-10-04 10:03:04 +0200784 lockup_detector_setup();
Don Zickus58687ac2010-05-07 17:11:44 -0400785}