watchdog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Detect hard and soft lockups on a system
  3. *
  4. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  5. *
  6. * this code detects hard lockups: incidents in where on a CPU
  7. * the kernel does not respond to anything except NMI.
  8. *
  9. * Note: Most of this code is borrowed heavily from softlockup.c,
  10. * so thanks to Ingo for the initial implementation.
  11. * Some chunks also taken from arch/x86/kernel/apic/nmi.c, thanks
  12. * to those contributors as well.
  13. */
  14. #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>
  25. #include <asm/irq_regs.h>
  26. #include <linux/perf_event.h>
  27. int watchdog_enabled = 1;
  28. int __read_mostly watchdog_thresh = 10;
  29. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  30. static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
  31. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  32. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  33. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  34. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  35. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  36. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  37. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  38. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  39. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  40. #endif
  41. /* boot commands */
  42. /*
  43. * Should we panic when a soft-lockup or hard-lockup occurs:
  44. */
  45. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  46. static int hardlockup_panic =
  47. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  48. static int __init hardlockup_panic_setup(char *str)
  49. {
  50. if (!strncmp(str, "panic", 5))
  51. hardlockup_panic = 1;
  52. else if (!strncmp(str, "nopanic", 7))
  53. hardlockup_panic = 0;
  54. else if (!strncmp(str, "0", 1))
  55. watchdog_enabled = 0;
  56. return 1;
  57. }
  58. __setup("nmi_watchdog=", hardlockup_panic_setup);
  59. #endif
  60. unsigned int __read_mostly softlockup_panic =
  61. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  62. static int __init softlockup_panic_setup(char *str)
  63. {
  64. softlockup_panic = simple_strtoul(str, NULL, 0);
  65. return 1;
  66. }
  67. __setup("softlockup_panic=", softlockup_panic_setup);
  68. static int __init nowatchdog_setup(char *str)
  69. {
  70. watchdog_enabled = 0;
  71. return 1;
  72. }
  73. __setup("nowatchdog", nowatchdog_setup);
  74. /* deprecated */
  75. static int __init nosoftlockup_setup(char *str)
  76. {
  77. watchdog_enabled = 0;
  78. return 1;
  79. }
  80. __setup("nosoftlockup", nosoftlockup_setup);
  81. /* */
  82. /*
  83. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  84. * lockups can have false positives under extreme conditions. So we generally
  85. * want a higher threshold for soft lockups than for hard lockups. So we couple
  86. * the thresholds with a factor: we make the soft threshold twice the amount of
  87. * time the hard threshold is.
  88. */
  89. static int get_softlockup_thresh(void)
  90. {
  91. return watchdog_thresh * 2;
  92. }
  93. /*
  94. * Returns seconds, approximately. We don't need nanosecond
  95. * resolution, and we don't need to waste time with a big divide when
  96. * 2^30ns == 1.074s.
  97. */
  98. static unsigned long get_timestamp(int this_cpu)
  99. {
  100. return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
  101. }
  102. static unsigned long get_sample_period(void)
  103. {
  104. /*
  105. * convert watchdog_thresh from seconds to ns
  106. * the divide by 5 is to give hrtimer 5 chances to
  107. * increment before the hardlockup detector generates
  108. * a warning
  109. */
  110. return get_softlockup_thresh() * (NSEC_PER_SEC / 5);
  111. }
  112. /* Commands for resetting the watchdog */
  113. static void __touch_watchdog(void)
  114. {
  115. int this_cpu = smp_processor_id();
  116. __this_cpu_write(watchdog_touch_ts, get_timestamp(this_cpu));
  117. }
  118. void touch_softlockup_watchdog(void)
  119. {
  120. __this_cpu_write(watchdog_touch_ts, 0);
  121. }
  122. EXPORT_SYMBOL(touch_softlockup_watchdog);
  123. void touch_all_softlockup_watchdogs(void)
  124. {
  125. int cpu;
  126. /*
  127. * this is done lockless
  128. * do we care if a 0 races with a timestamp?
  129. * all it means is the softlock check starts one cycle later
  130. */
  131. for_each_online_cpu(cpu)
  132. per_cpu(watchdog_touch_ts, cpu) = 0;
  133. }
  134. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  135. void touch_nmi_watchdog(void)
  136. {
  137. if (watchdog_enabled) {
  138. unsigned cpu;
  139. for_each_present_cpu(cpu) {
  140. if (per_cpu(watchdog_nmi_touch, cpu) != true)
  141. per_cpu(watchdog_nmi_touch, cpu) = true;
  142. }
  143. }
  144. touch_softlockup_watchdog();
  145. }
  146. EXPORT_SYMBOL(touch_nmi_watchdog);
  147. #endif
  148. void touch_softlockup_watchdog_sync(void)
  149. {
  150. __raw_get_cpu_var(softlockup_touch_sync) = true;
  151. __raw_get_cpu_var(watchdog_touch_ts) = 0;
  152. }
  153. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  154. /* watchdog detector functions */
  155. static int is_hardlockup(void)
  156. {
  157. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  158. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  159. return 1;
  160. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  161. return 0;
  162. }
  163. #endif
  164. static int is_softlockup(unsigned long touch_ts)
  165. {
  166. unsigned long now = get_timestamp(smp_processor_id());
  167. /* Warn about unreasonable delays: */
  168. if (time_after(now, touch_ts + get_softlockup_thresh()))
  169. return now - touch_ts;
  170. return 0;
  171. }
  172. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  173. static struct perf_event_attr wd_hw_attr = {
  174. .type = PERF_TYPE_HARDWARE,
  175. .config = PERF_COUNT_HW_CPU_CYCLES,
  176. .size = sizeof(struct perf_event_attr),
  177. .pinned = 1,
  178. .disabled = 1,
  179. };
  180. /* Callback function for perf event subsystem */
  181. static void watchdog_overflow_callback(struct perf_event *event, int nmi,
  182. struct perf_sample_data *data,
  183. struct pt_regs *regs)
  184. {
  185. /* Ensure the watchdog never gets throttled */
  186. event->hw.interrupts = 0;
  187. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  188. __this_cpu_write(watchdog_nmi_touch, false);
  189. return;
  190. }
  191. /* check for a hardlockup
  192. * This is done by making sure our timer interrupt
  193. * is incrementing. The timer interrupt should have
  194. * fired multiple times before we overflow'd. If it hasn't
  195. * then this is a good indication the cpu is stuck
  196. */
  197. if (is_hardlockup()) {
  198. int this_cpu = smp_processor_id();
  199. /* only print hardlockups once */
  200. if (__this_cpu_read(hard_watchdog_warn) == true)
  201. return;
  202. if (hardlockup_panic)
  203. panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  204. else
  205. WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  206. __this_cpu_write(hard_watchdog_warn, true);
  207. return;
  208. }
  209. __this_cpu_write(hard_watchdog_warn, false);
  210. return;
  211. }
  212. static void watchdog_interrupt_count(void)
  213. {
  214. __this_cpu_inc(hrtimer_interrupts);
  215. }
  216. #else
  217. static inline void watchdog_interrupt_count(void) { return; }
  218. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  219. /* watchdog kicker functions */
  220. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  221. {
  222. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  223. struct pt_regs *regs = get_irq_regs();
  224. int duration;
  225. /* kick the hardlockup detector */
  226. watchdog_interrupt_count();
  227. /* kick the softlockup detector */
  228. wake_up_process(__this_cpu_read(softlockup_watchdog));
  229. /* .. and repeat */
  230. hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
  231. if (touch_ts == 0) {
  232. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  233. /*
  234. * If the time stamp was touched atomically
  235. * make sure the scheduler tick is up to date.
  236. */
  237. __this_cpu_write(softlockup_touch_sync, false);
  238. sched_clock_tick();
  239. }
  240. __touch_watchdog();
  241. return HRTIMER_RESTART;
  242. }
  243. /* check for a softlockup
  244. * This is done by making sure a high priority task is
  245. * being scheduled. The task touches the watchdog to
  246. * indicate it is getting cpu time. If it hasn't then
  247. * this is a good indication some task is hogging the cpu
  248. */
  249. duration = is_softlockup(touch_ts);
  250. if (unlikely(duration)) {
  251. /* only warn once */
  252. if (__this_cpu_read(soft_watchdog_warn) == true)
  253. return HRTIMER_RESTART;
  254. printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  255. smp_processor_id(), duration,
  256. current->comm, task_pid_nr(current));
  257. print_modules();
  258. print_irqtrace_events(current);
  259. if (regs)
  260. show_regs(regs);
  261. else
  262. dump_stack();
  263. if (softlockup_panic)
  264. panic("softlockup: hung tasks");
  265. __this_cpu_write(soft_watchdog_warn, true);
  266. } else
  267. __this_cpu_write(soft_watchdog_warn, false);
  268. return HRTIMER_RESTART;
  269. }
  270. /*
  271. * The watchdog thread - touches the timestamp.
  272. */
  273. static int watchdog(void *unused)
  274. {
  275. static struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  276. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  277. sched_setscheduler(current, SCHED_FIFO, &param);
  278. /* initialize timestamp */
  279. __touch_watchdog();
  280. /* kick off the timer for the hardlockup detector */
  281. /* done here because hrtimer_start can only pin to smp_processor_id() */
  282. hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
  283. HRTIMER_MODE_REL_PINNED);
  284. set_current_state(TASK_INTERRUPTIBLE);
  285. /*
  286. * Run briefly once per second to reset the softlockup timestamp.
  287. * If this gets delayed for more than 60 seconds then the
  288. * debug-printout triggers in watchdog_timer_fn().
  289. */
  290. while (!kthread_should_stop()) {
  291. __touch_watchdog();
  292. schedule();
  293. if (kthread_should_stop())
  294. break;
  295. set_current_state(TASK_INTERRUPTIBLE);
  296. }
  297. __set_current_state(TASK_RUNNING);
  298. return 0;
  299. }
  300. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  301. static int watchdog_nmi_enable(int cpu)
  302. {
  303. struct perf_event_attr *wd_attr;
  304. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  305. /* is it already setup and enabled? */
  306. if (event && event->state > PERF_EVENT_STATE_OFF)
  307. goto out;
  308. /* it is setup but not enabled */
  309. if (event != NULL)
  310. goto out_enable;
  311. /* Try to register using hardware perf events */
  312. wd_attr = &wd_hw_attr;
  313. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  314. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback);
  315. if (!IS_ERR(event)) {
  316. printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
  317. goto out_save;
  318. }
  319. /* vary the KERN level based on the returned errno */
  320. if (PTR_ERR(event) == -EOPNOTSUPP)
  321. printk(KERN_INFO "NMI watchdog disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  322. else if (PTR_ERR(event) == -ENOENT)
  323. printk(KERN_WARNING "NMI watchdog disabled (cpu%i): hardware events not enabled\n", cpu);
  324. else
  325. printk(KERN_ERR "NMI watchdog disabled (cpu%i): unable to create perf event: %ld\n", cpu, PTR_ERR(event));
  326. return PTR_ERR(event);
  327. /* success path */
  328. out_save:
  329. per_cpu(watchdog_ev, cpu) = event;
  330. out_enable:
  331. perf_event_enable(per_cpu(watchdog_ev, cpu));
  332. out:
  333. return 0;
  334. }
  335. static void watchdog_nmi_disable(int cpu)
  336. {
  337. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  338. if (event) {
  339. perf_event_disable(event);
  340. per_cpu(watchdog_ev, cpu) = NULL;
  341. /* should be in cleanup, but blocks oprofile */
  342. perf_event_release_kernel(event);
  343. }
  344. return;
  345. }
  346. #else
  347. static int watchdog_nmi_enable(int cpu) { return 0; }
  348. static void watchdog_nmi_disable(int cpu) { return; }
  349. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  350. /* prepare/enable/disable routines */
  351. static void watchdog_prepare_cpu(int cpu)
  352. {
  353. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  354. WARN_ON(per_cpu(softlockup_watchdog, cpu));
  355. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  356. hrtimer->function = watchdog_timer_fn;
  357. }
  358. static int watchdog_enable(int cpu)
  359. {
  360. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  361. int err = 0;
  362. /* enable the perf event */
  363. err = watchdog_nmi_enable(cpu);
  364. /* Regardless of err above, fall through and start softlockup */
  365. /* create the watchdog thread */
  366. if (!p) {
  367. p = kthread_create(watchdog, (void *)(unsigned long)cpu, "watchdog/%d", cpu);
  368. if (IS_ERR(p)) {
  369. printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);
  370. if (!err) {
  371. /* if hardlockup hasn't already set this */
  372. err = PTR_ERR(p);
  373. /* and disable the perf event */
  374. watchdog_nmi_disable(cpu);
  375. }
  376. goto out;
  377. }
  378. kthread_bind(p, cpu);
  379. per_cpu(watchdog_touch_ts, cpu) = 0;
  380. per_cpu(softlockup_watchdog, cpu) = p;
  381. wake_up_process(p);
  382. }
  383. out:
  384. return err;
  385. }
  386. static void watchdog_disable(int cpu)
  387. {
  388. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  389. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  390. /*
  391. * cancel the timer first to stop incrementing the stats
  392. * and waking up the kthread
  393. */
  394. hrtimer_cancel(hrtimer);
  395. /* disable the perf event */
  396. watchdog_nmi_disable(cpu);
  397. /* stop the watchdog thread */
  398. if (p) {
  399. per_cpu(softlockup_watchdog, cpu) = NULL;
  400. kthread_stop(p);
  401. }
  402. }
  403. static void watchdog_enable_all_cpus(void)
  404. {
  405. int cpu;
  406. watchdog_enabled = 0;
  407. for_each_online_cpu(cpu)
  408. if (!watchdog_enable(cpu))
  409. /* if any cpu succeeds, watchdog is considered
  410. enabled for the system */
  411. watchdog_enabled = 1;
  412. if (!watchdog_enabled)
  413. printk(KERN_ERR "watchdog: failed to be enabled on some cpus\n");
  414. }
  415. static void watchdog_disable_all_cpus(void)
  416. {
  417. int cpu;
  418. for_each_online_cpu(cpu)
  419. watchdog_disable(cpu);
  420. /* if all watchdogs are disabled, then they are disabled for the system */
  421. watchdog_enabled = 0;
  422. }
  423. /* sysctl functions */
  424. #ifdef CONFIG_SYSCTL
  425. /*
  426. * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
  427. */
  428. int proc_dowatchdog(struct ctl_table *table, int write,
  429. void __user *buffer, size_t *lenp, loff_t *ppos)
  430. {
  431. int ret;
  432. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  433. if (ret || !write)
  434. goto out;
  435. if (watchdog_enabled && watchdog_thresh)
  436. watchdog_enable_all_cpus();
  437. else
  438. watchdog_disable_all_cpus();
  439. out:
  440. return ret;
  441. }
  442. #endif /* CONFIG_SYSCTL */
  443. /*
  444. * Create/destroy watchdog threads as CPUs come and go:
  445. */
  446. static int __cpuinit
  447. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  448. {
  449. int hotcpu = (unsigned long)hcpu;
  450. switch (action) {
  451. case CPU_UP_PREPARE:
  452. case CPU_UP_PREPARE_FROZEN:
  453. watchdog_prepare_cpu(hotcpu);
  454. break;
  455. case CPU_ONLINE:
  456. case CPU_ONLINE_FROZEN:
  457. if (watchdog_enabled)
  458. watchdog_enable(hotcpu);
  459. break;
  460. #ifdef CONFIG_HOTPLUG_CPU
  461. case CPU_UP_CANCELED:
  462. case CPU_UP_CANCELED_FROZEN:
  463. watchdog_disable(hotcpu);
  464. break;
  465. case CPU_DEAD:
  466. case CPU_DEAD_FROZEN:
  467. watchdog_disable(hotcpu);
  468. break;
  469. #endif /* CONFIG_HOTPLUG_CPU */
  470. }
  471. /*
  472. * hardlockup and softlockup are not important enough
  473. * to block cpu bring up. Just always succeed and
  474. * rely on printk output to flag problems.
  475. */
  476. return NOTIFY_OK;
  477. }
  478. static struct notifier_block __cpuinitdata cpu_nfb = {
  479. .notifier_call = cpu_callback
  480. };
  481. void __init lockup_detector_init(void)
  482. {
  483. void *cpu = (void *)(long)smp_processor_id();
  484. int err;
  485. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  486. WARN_ON(notifier_to_errno(err));
  487. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  488. register_cpu_notifier(&cpu_nfb);
  489. return;
  490. }