blob: 27689422aa9284274f3514286dfafc6da04b2880 [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
Andrew Morton45019802012-03-23 15:01:55 -070012#define pr_fmt(fmt) "NMI watchdog: " fmt
13
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>
18#include <linux/delay.h>
19#include <linux/freezer.h>
20#include <linux/kthread.h>
21#include <linux/lockdep.h>
22#include <linux/notifier.h>
23#include <linux/module.h>
24#include <linux/sysctl.h>
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000025#include <linux/smpboot.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060026#include <linux/sched/rt.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>
Don Zickus58687ac2010-05-07 17:11:44 -040030#include <linux/perf_event.h>
31
Marcin Slusarz41350382011-01-28 11:00:31 -050032int watchdog_enabled = 1;
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -070033int __read_mostly watchdog_thresh = 10;
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000034static int __read_mostly watchdog_disabled;
Chuansheng Liu0f34c402012-12-17 15:59:50 -080035static u64 __read_mostly sample_period;
Don Zickus58687ac2010-05-07 17:11:44 -040036
37static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
38static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
39static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
40static DEFINE_PER_CPU(bool, softlockup_touch_sync);
Don Zickus58687ac2010-05-07 17:11:44 -040041static DEFINE_PER_CPU(bool, soft_watchdog_warn);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000042static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
43static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
Frederic Weisbecker23637d42010-05-15 23:15:20 +020044#ifdef CONFIG_HARDLOCKUP_DETECTOR
Don Zickuscafcd802010-05-14 11:11:21 -040045static DEFINE_PER_CPU(bool, hard_watchdog_warn);
46static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
Don Zickus58687ac2010-05-07 17:11:44 -040047static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
48static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
49#endif
50
Don Zickus58687ac2010-05-07 17:11:44 -040051/* boot commands */
52/*
53 * Should we panic when a soft-lockup or hard-lockup occurs:
54 */
Frederic Weisbecker23637d42010-05-15 23:15:20 +020055#ifdef CONFIG_HARDLOCKUP_DETECTOR
Don Zickusfef2c9b2011-03-22 16:34:16 -070056static int hardlockup_panic =
57 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
Don Zickus58687ac2010-05-07 17:11:44 -040058
59static int __init hardlockup_panic_setup(char *str)
60{
61 if (!strncmp(str, "panic", 5))
62 hardlockup_panic = 1;
Don Zickusfef2c9b2011-03-22 16:34:16 -070063 else if (!strncmp(str, "nopanic", 7))
64 hardlockup_panic = 0;
Don Zickus5dc30552010-11-29 17:07:17 -050065 else if (!strncmp(str, "0", 1))
Marcin Slusarz41350382011-01-28 11:00:31 -050066 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -040067 return 1;
68}
69__setup("nmi_watchdog=", hardlockup_panic_setup);
70#endif
71
72unsigned int __read_mostly softlockup_panic =
73 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
74
75static int __init softlockup_panic_setup(char *str)
76{
77 softlockup_panic = simple_strtoul(str, NULL, 0);
78
79 return 1;
80}
81__setup("softlockup_panic=", softlockup_panic_setup);
82
83static int __init nowatchdog_setup(char *str)
84{
Marcin Slusarz41350382011-01-28 11:00:31 -050085 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -040086 return 1;
87}
88__setup("nowatchdog", nowatchdog_setup);
89
90/* deprecated */
91static int __init nosoftlockup_setup(char *str)
92{
Marcin Slusarz41350382011-01-28 11:00:31 -050093 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -040094 return 1;
95}
96__setup("nosoftlockup", nosoftlockup_setup);
97/* */
98
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -070099/*
100 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
101 * lockups can have false positives under extreme conditions. So we generally
102 * want a higher threshold for soft lockups than for hard lockups. So we couple
103 * the thresholds with a factor: we make the soft threshold twice the amount of
104 * time the hard threshold is.
105 */
Ingo Molnar6e9101a2011-05-24 05:43:18 +0200106static int get_softlockup_thresh(void)
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700107{
108 return watchdog_thresh * 2;
109}
Don Zickus58687ac2010-05-07 17:11:44 -0400110
111/*
112 * Returns seconds, approximately. We don't need nanosecond
113 * resolution, and we don't need to waste time with a big divide when
114 * 2^30ns == 1.074s.
115 */
116static unsigned long get_timestamp(int this_cpu)
117{
118 return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
119}
120
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800121static void set_sample_period(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400122{
123 /*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700124 * convert watchdog_thresh from seconds to ns
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -0500125 * the divide by 5 is to give hrtimer several chances (two
126 * or three with the current relation between the soft
127 * and hard thresholds) to increment before the
128 * hardlockup detector generates a warning
Don Zickus58687ac2010-05-07 17:11:44 -0400129 */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800130 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
Don Zickus58687ac2010-05-07 17:11:44 -0400131}
132
133/* Commands for resetting the watchdog */
134static void __touch_watchdog(void)
135{
Don Zickus26e09c62010-05-17 18:06:04 -0400136 int this_cpu = smp_processor_id();
Don Zickus58687ac2010-05-07 17:11:44 -0400137
Christoph Lameter909ea962010-12-08 16:22:55 +0100138 __this_cpu_write(watchdog_touch_ts, get_timestamp(this_cpu));
Don Zickus58687ac2010-05-07 17:11:44 -0400139}
140
Don Zickus332fbdb2010-05-07 17:11:45 -0400141void touch_softlockup_watchdog(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400142{
Christoph Lameter909ea962010-12-08 16:22:55 +0100143 __this_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400144}
Ingo Molnar0167c782010-05-13 08:53:33 +0200145EXPORT_SYMBOL(touch_softlockup_watchdog);
Don Zickus58687ac2010-05-07 17:11:44 -0400146
Don Zickus332fbdb2010-05-07 17:11:45 -0400147void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400148{
149 int cpu;
150
151 /*
152 * this is done lockless
153 * do we care if a 0 races with a timestamp?
154 * all it means is the softlock check starts one cycle later
155 */
156 for_each_online_cpu(cpu)
157 per_cpu(watchdog_touch_ts, cpu) = 0;
158}
159
Don Zickuscafcd802010-05-14 11:11:21 -0400160#ifdef CONFIG_HARDLOCKUP_DETECTOR
Don Zickus58687ac2010-05-07 17:11:44 -0400161void touch_nmi_watchdog(void)
162{
Don Zickus68d3f1d2010-08-31 23:00:07 -0400163 if (watchdog_enabled) {
164 unsigned cpu;
165
166 for_each_present_cpu(cpu) {
167 if (per_cpu(watchdog_nmi_touch, cpu) != true)
168 per_cpu(watchdog_nmi_touch, cpu) = true;
169 }
170 }
Don Zickus332fbdb2010-05-07 17:11:45 -0400171 touch_softlockup_watchdog();
Don Zickus58687ac2010-05-07 17:11:44 -0400172}
173EXPORT_SYMBOL(touch_nmi_watchdog);
174
Don Zickuscafcd802010-05-14 11:11:21 -0400175#endif
176
Don Zickus58687ac2010-05-07 17:11:44 -0400177void touch_softlockup_watchdog_sync(void)
178{
179 __raw_get_cpu_var(softlockup_touch_sync) = true;
180 __raw_get_cpu_var(watchdog_touch_ts) = 0;
181}
182
Frederic Weisbecker23637d42010-05-15 23:15:20 +0200183#ifdef CONFIG_HARDLOCKUP_DETECTOR
Don Zickus58687ac2010-05-07 17:11:44 -0400184/* watchdog detector functions */
Don Zickus26e09c62010-05-17 18:06:04 -0400185static int is_hardlockup(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400186{
Christoph Lameter909ea962010-12-08 16:22:55 +0100187 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400188
Christoph Lameter909ea962010-12-08 16:22:55 +0100189 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
Don Zickus58687ac2010-05-07 17:11:44 -0400190 return 1;
191
Christoph Lameter909ea962010-12-08 16:22:55 +0100192 __this_cpu_write(hrtimer_interrupts_saved, hrint);
Don Zickus58687ac2010-05-07 17:11:44 -0400193 return 0;
194}
195#endif
196
Don Zickus26e09c62010-05-17 18:06:04 -0400197static int is_softlockup(unsigned long touch_ts)
Don Zickus58687ac2010-05-07 17:11:44 -0400198{
Don Zickus26e09c62010-05-17 18:06:04 -0400199 unsigned long now = get_timestamp(smp_processor_id());
Don Zickus58687ac2010-05-07 17:11:44 -0400200
201 /* Warn about unreasonable delays: */
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700202 if (time_after(now, touch_ts + get_softlockup_thresh()))
Don Zickus58687ac2010-05-07 17:11:44 -0400203 return now - touch_ts;
204
205 return 0;
206}
207
Frederic Weisbecker23637d42010-05-15 23:15:20 +0200208#ifdef CONFIG_HARDLOCKUP_DETECTOR
Cyrill Gorcunov1880c4a2011-06-23 16:49:18 +0400209
Don Zickus58687ac2010-05-07 17:11:44 -0400210static struct perf_event_attr wd_hw_attr = {
211 .type = PERF_TYPE_HARDWARE,
212 .config = PERF_COUNT_HW_CPU_CYCLES,
213 .size = sizeof(struct perf_event_attr),
214 .pinned = 1,
215 .disabled = 1,
216};
217
218/* Callback function for perf event subsystem */
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200219static void watchdog_overflow_callback(struct perf_event *event,
Don Zickus58687ac2010-05-07 17:11:44 -0400220 struct perf_sample_data *data,
221 struct pt_regs *regs)
222{
Peter Zijlstrac6db67c2010-08-20 11:49:15 +0200223 /* Ensure the watchdog never gets throttled */
224 event->hw.interrupts = 0;
225
Christoph Lameter909ea962010-12-08 16:22:55 +0100226 if (__this_cpu_read(watchdog_nmi_touch) == true) {
227 __this_cpu_write(watchdog_nmi_touch, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400228 return;
229 }
230
231 /* check for a hardlockup
232 * This is done by making sure our timer interrupt
233 * is incrementing. The timer interrupt should have
234 * fired multiple times before we overflow'd. If it hasn't
235 * then this is a good indication the cpu is stuck
236 */
Don Zickus26e09c62010-05-17 18:06:04 -0400237 if (is_hardlockup()) {
238 int this_cpu = smp_processor_id();
239
Don Zickus58687ac2010-05-07 17:11:44 -0400240 /* only print hardlockups once */
Christoph Lameter909ea962010-12-08 16:22:55 +0100241 if (__this_cpu_read(hard_watchdog_warn) == true)
Don Zickus58687ac2010-05-07 17:11:44 -0400242 return;
243
244 if (hardlockup_panic)
245 panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
246 else
247 WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
248
Christoph Lameter909ea962010-12-08 16:22:55 +0100249 __this_cpu_write(hard_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400250 return;
251 }
252
Christoph Lameter909ea962010-12-08 16:22:55 +0100253 __this_cpu_write(hard_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400254 return;
255}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000256#endif /* CONFIG_HARDLOCKUP_DETECTOR */
257
Don Zickus58687ac2010-05-07 17:11:44 -0400258static void watchdog_interrupt_count(void)
259{
Christoph Lameter909ea962010-12-08 16:22:55 +0100260 __this_cpu_inc(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400261}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000262
263static int watchdog_nmi_enable(unsigned int cpu);
264static void watchdog_nmi_disable(unsigned int cpu);
Don Zickus58687ac2010-05-07 17:11:44 -0400265
266/* watchdog kicker functions */
267static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
268{
Christoph Lameter909ea962010-12-08 16:22:55 +0100269 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400270 struct pt_regs *regs = get_irq_regs();
271 int duration;
272
273 /* kick the hardlockup detector */
274 watchdog_interrupt_count();
275
276 /* kick the softlockup detector */
Christoph Lameter909ea962010-12-08 16:22:55 +0100277 wake_up_process(__this_cpu_read(softlockup_watchdog));
Don Zickus58687ac2010-05-07 17:11:44 -0400278
279 /* .. and repeat */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800280 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
Don Zickus58687ac2010-05-07 17:11:44 -0400281
282 if (touch_ts == 0) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100283 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
Don Zickus58687ac2010-05-07 17:11:44 -0400284 /*
285 * If the time stamp was touched atomically
286 * make sure the scheduler tick is up to date.
287 */
Christoph Lameter909ea962010-12-08 16:22:55 +0100288 __this_cpu_write(softlockup_touch_sync, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400289 sched_clock_tick();
290 }
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500291
292 /* Clear the guest paused flag on watchdog reset */
293 kvm_check_and_clear_guest_paused();
Don Zickus58687ac2010-05-07 17:11:44 -0400294 __touch_watchdog();
295 return HRTIMER_RESTART;
296 }
297
298 /* check for a softlockup
299 * This is done by making sure a high priority task is
300 * being scheduled. The task touches the watchdog to
301 * indicate it is getting cpu time. If it hasn't then
302 * this is a good indication some task is hogging the cpu
303 */
Don Zickus26e09c62010-05-17 18:06:04 -0400304 duration = is_softlockup(touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400305 if (unlikely(duration)) {
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500306 /*
307 * If a virtual machine is stopped by the host it can look to
308 * the watchdog like a soft lockup, check to see if the host
309 * stopped the vm before we issue the warning
310 */
311 if (kvm_check_and_clear_guest_paused())
312 return HRTIMER_RESTART;
313
Don Zickus58687ac2010-05-07 17:11:44 -0400314 /* only warn once */
Christoph Lameter909ea962010-12-08 16:22:55 +0100315 if (__this_cpu_read(soft_watchdog_warn) == true)
Don Zickus58687ac2010-05-07 17:11:44 -0400316 return HRTIMER_RESTART;
317
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500318 printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
Don Zickus26e09c62010-05-17 18:06:04 -0400319 smp_processor_id(), duration,
Don Zickus58687ac2010-05-07 17:11:44 -0400320 current->comm, task_pid_nr(current));
321 print_modules();
322 print_irqtrace_events(current);
323 if (regs)
324 show_regs(regs);
325 else
326 dump_stack();
327
328 if (softlockup_panic)
329 panic("softlockup: hung tasks");
Christoph Lameter909ea962010-12-08 16:22:55 +0100330 __this_cpu_write(soft_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400331 } else
Christoph Lameter909ea962010-12-08 16:22:55 +0100332 __this_cpu_write(soft_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400333
334 return HRTIMER_RESTART;
335}
336
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000337static void watchdog_set_prio(unsigned int policy, unsigned int prio)
Don Zickus58687ac2010-05-07 17:11:44 -0400338{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000339 struct sched_param param = { .sched_priority = prio };
340
341 sched_setscheduler(current, policy, &param);
342}
343
344static void watchdog_enable(unsigned int cpu)
345{
Don Zickus26e09c62010-05-17 18:06:04 -0400346 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
Don Zickus58687ac2010-05-07 17:11:44 -0400347
Bjørn Mork3935e8952012-12-19 20:51:31 +0100348 /* kick off the timer for the hardlockup detector */
349 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
350 hrtimer->function = watchdog_timer_fn;
351
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000352 if (!watchdog_enabled) {
353 kthread_park(current);
354 return;
355 }
356
357 /* Enable the perf event */
358 watchdog_nmi_enable(cpu);
Don Zickus58687ac2010-05-07 17:11:44 -0400359
Don Zickus58687ac2010-05-07 17:11:44 -0400360 /* done here because hrtimer_start can only pin to smp_processor_id() */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800361 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
Don Zickus58687ac2010-05-07 17:11:44 -0400362 HRTIMER_MODE_REL_PINNED);
363
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000364 /* initialize timestamp */
365 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
366 __touch_watchdog();
Don Zickus58687ac2010-05-07 17:11:44 -0400367}
368
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000369static void watchdog_disable(unsigned int cpu)
370{
371 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
372
373 watchdog_set_prio(SCHED_NORMAL, 0);
374 hrtimer_cancel(hrtimer);
375 /* disable the perf event */
376 watchdog_nmi_disable(cpu);
377}
378
379static int watchdog_should_run(unsigned int cpu)
380{
381 return __this_cpu_read(hrtimer_interrupts) !=
382 __this_cpu_read(soft_lockup_hrtimer_cnt);
383}
384
385/*
386 * The watchdog thread function - touches the timestamp.
387 *
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800388 * It only runs once every sample_period seconds (4 seconds by
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000389 * default) to reset the softlockup timestamp. If this gets delayed
390 * for more than 2*watchdog_thresh seconds then the debug-printout
391 * triggers in watchdog_timer_fn().
392 */
393static void watchdog(unsigned int cpu)
394{
395 __this_cpu_write(soft_lockup_hrtimer_cnt,
396 __this_cpu_read(hrtimer_interrupts));
397 __touch_watchdog();
398}
Don Zickus58687ac2010-05-07 17:11:44 -0400399
Frederic Weisbecker23637d42010-05-15 23:15:20 +0200400#ifdef CONFIG_HARDLOCKUP_DETECTOR
Don Zickusa7027042012-06-13 09:35:48 -0400401/*
402 * People like the simple clean cpu node info on boot.
403 * Reduce the watchdog noise by only printing messages
404 * that are different from what cpu0 displayed.
405 */
406static unsigned long cpu0_err;
407
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000408static int watchdog_nmi_enable(unsigned int cpu)
Don Zickus58687ac2010-05-07 17:11:44 -0400409{
410 struct perf_event_attr *wd_attr;
411 struct perf_event *event = per_cpu(watchdog_ev, cpu);
412
413 /* is it already setup and enabled? */
414 if (event && event->state > PERF_EVENT_STATE_OFF)
415 goto out;
416
417 /* it is setup but not enabled */
418 if (event != NULL)
419 goto out_enable;
420
Don Zickus58687ac2010-05-07 17:11:44 -0400421 wd_attr = &wd_hw_attr;
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700422 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
Cyrill Gorcunov1880c4a2011-06-23 16:49:18 +0400423
424 /* Try to register using hardware perf events */
Avi Kivity4dc0da82011-06-29 18:42:35 +0300425 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
Don Zickusa7027042012-06-13 09:35:48 -0400426
427 /* save cpu0 error for future comparision */
428 if (cpu == 0 && IS_ERR(event))
429 cpu0_err = PTR_ERR(event);
430
Don Zickus58687ac2010-05-07 17:11:44 -0400431 if (!IS_ERR(event)) {
Don Zickusa7027042012-06-13 09:35:48 -0400432 /* only print for cpu0 or different than cpu0 */
433 if (cpu == 0 || cpu0_err)
434 pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
Don Zickus58687ac2010-05-07 17:11:44 -0400435 goto out_save;
436 }
437
Don Zickusa7027042012-06-13 09:35:48 -0400438 /* skip displaying the same error again */
439 if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
440 return PTR_ERR(event);
Don Zickus5651f7f2011-02-09 14:02:33 -0500441
442 /* vary the KERN level based on the returned errno */
443 if (PTR_ERR(event) == -EOPNOTSUPP)
Andrew Morton45019802012-03-23 15:01:55 -0700444 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
Don Zickus5651f7f2011-02-09 14:02:33 -0500445 else if (PTR_ERR(event) == -ENOENT)
Andrew Morton45019802012-03-23 15:01:55 -0700446 pr_warning("disabled (cpu%i): hardware events not enabled\n",
447 cpu);
Don Zickus5651f7f2011-02-09 14:02:33 -0500448 else
Andrew Morton45019802012-03-23 15:01:55 -0700449 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
450 cpu, PTR_ERR(event));
Akinobu Mitaeac24332010-08-31 23:00:08 -0400451 return PTR_ERR(event);
Don Zickus58687ac2010-05-07 17:11:44 -0400452
453 /* success path */
454out_save:
455 per_cpu(watchdog_ev, cpu) = event;
456out_enable:
457 perf_event_enable(per_cpu(watchdog_ev, cpu));
458out:
459 return 0;
460}
461
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000462static void watchdog_nmi_disable(unsigned int cpu)
Don Zickus58687ac2010-05-07 17:11:44 -0400463{
464 struct perf_event *event = per_cpu(watchdog_ev, cpu);
465
466 if (event) {
467 perf_event_disable(event);
468 per_cpu(watchdog_ev, cpu) = NULL;
469
470 /* should be in cleanup, but blocks oprofile */
471 perf_event_release_kernel(event);
472 }
473 return;
474}
475#else
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000476static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
477static void watchdog_nmi_disable(unsigned int cpu) { return; }
Frederic Weisbecker23637d42010-05-15 23:15:20 +0200478#endif /* CONFIG_HARDLOCKUP_DETECTOR */
Don Zickus58687ac2010-05-07 17:11:44 -0400479
480/* prepare/enable/disable routines */
Vasily Averin4ff81952011-10-31 17:11:18 -0700481/* sysctl functions */
482#ifdef CONFIG_SYSCTL
Don Zickus58687ac2010-05-07 17:11:44 -0400483static void watchdog_enable_all_cpus(void)
484{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000485 unsigned int cpu;
Marcin Slusarz39735762011-01-28 11:00:32 -0500486
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000487 if (watchdog_disabled) {
488 watchdog_disabled = 0;
489 for_each_online_cpu(cpu)
490 kthread_unpark(per_cpu(softlockup_watchdog, cpu));
491 }
Don Zickus58687ac2010-05-07 17:11:44 -0400492}
493
494static void watchdog_disable_all_cpus(void)
495{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000496 unsigned int cpu;
Don Zickus58687ac2010-05-07 17:11:44 -0400497
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000498 if (!watchdog_disabled) {
499 watchdog_disabled = 1;
500 for_each_online_cpu(cpu)
501 kthread_park(per_cpu(softlockup_watchdog, cpu));
502 }
Don Zickus58687ac2010-05-07 17:11:44 -0400503}
504
Don Zickus58687ac2010-05-07 17:11:44 -0400505/*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700506 * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
Don Zickus58687ac2010-05-07 17:11:44 -0400507 */
508
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700509int proc_dowatchdog(struct ctl_table *table, int write,
510 void __user *buffer, size_t *lenp, loff_t *ppos)
Don Zickus58687ac2010-05-07 17:11:44 -0400511{
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700512 int ret;
Don Zickus58687ac2010-05-07 17:11:44 -0400513
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000514 if (watchdog_disabled < 0)
515 return -ENODEV;
516
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700517 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700518 if (ret || !write)
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000519 return ret;
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700520
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800521 set_sample_period();
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700522 if (watchdog_enabled && watchdog_thresh)
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700523 watchdog_enable_all_cpus();
524 else
525 watchdog_disable_all_cpus();
526
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700527 return ret;
Don Zickus58687ac2010-05-07 17:11:44 -0400528}
Don Zickus58687ac2010-05-07 17:11:44 -0400529#endif /* CONFIG_SYSCTL */
530
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000531static struct smp_hotplug_thread watchdog_threads = {
532 .store = &softlockup_watchdog,
533 .thread_should_run = watchdog_should_run,
534 .thread_fn = watchdog,
535 .thread_comm = "watchdog/%u",
536 .setup = watchdog_enable,
537 .park = watchdog_disable,
538 .unpark = watchdog_enable,
Don Zickus58687ac2010-05-07 17:11:44 -0400539};
540
Peter Zijlstra004417a2010-11-25 18:38:29 +0100541void __init lockup_detector_init(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400542{
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800543 set_sample_period();
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000544 if (smpboot_register_percpu_thread(&watchdog_threads)) {
545 pr_err("Failed to create watchdog threads, disabled\n");
546 watchdog_disabled = -ENODEV;
547 }
Don Zickus58687ac2010-05-07 17:11:44 -0400548}