watchdog.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Detect hard and soft lockups on a system
  3. *
  4. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  5. *
  6. * 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
  9. * to those contributors as well.
  10. */
  11. #define pr_fmt(fmt) "NMI watchdog: " fmt
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/nmi.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/freezer.h>
  18. #include <linux/kthread.h>
  19. #include <linux/lockdep.h>
  20. #include <linux/notifier.h>
  21. #include <linux/module.h>
  22. #include <linux/sysctl.h>
  23. #include <asm/irq_regs.h>
  24. #include <linux/kvm_para.h>
  25. #include <linux/perf_event.h>
  26. int watchdog_enabled = 1;
  27. int __read_mostly watchdog_thresh = 10;
  28. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  29. static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
  30. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  31. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  32. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  33. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  34. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  35. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  36. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  37. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  38. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  39. #endif
  40. /* boot commands */
  41. /*
  42. * Should we panic when a soft-lockup or hard-lockup occurs:
  43. */
  44. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  45. static int hardlockup_panic =
  46. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  47. static int __init hardlockup_panic_setup(char *str)
  48. {
  49. if (!strncmp(str, "panic", 5))
  50. hardlockup_panic = 1;
  51. else if (!strncmp(str, "nopanic", 7))
  52. hardlockup_panic = 0;
  53. else if (!strncmp(str, "0", 1))
  54. watchdog_enabled = 0;
  55. return 1;
  56. }
  57. __setup("nmi_watchdog=", hardlockup_panic_setup);
  58. #endif
  59. unsigned int __read_mostly softlockup_panic =
  60. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  61. static int __init softlockup_panic_setup(char *str)
  62. {
  63. softlockup_panic = simple_strtoul(str, NULL, 0);
  64. return 1;
  65. }
  66. __setup("softlockup_panic=", softlockup_panic_setup);
  67. static int __init nowatchdog_setup(char *str)
  68. {
  69. watchdog_enabled = 0;
  70. return 1;
  71. }
  72. __setup("nowatchdog", nowatchdog_setup);
  73. /* deprecated */
  74. static int __init nosoftlockup_setup(char *str)
  75. {
  76. watchdog_enabled = 0;
  77. return 1;
  78. }
  79. __setup("nosoftlockup", nosoftlockup_setup);
  80. /* */
  81. /*
  82. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  83. * lockups can have false positives under extreme conditions. So we generally
  84. * want a higher threshold for soft lockups than for hard lockups. So we couple
  85. * the thresholds with a factor: we make the soft threshold twice the amount of
  86. * time the hard threshold is.
  87. */
  88. static int get_softlockup_thresh(void)
  89. {
  90. return watchdog_thresh * 2;
  91. }
  92. /*
  93. * Returns seconds, approximately. We don't need nanosecond
  94. * resolution, and we don't need to waste time with a big divide when
  95. * 2^30ns == 1.074s.
  96. */
  97. static unsigned long get_timestamp(void)
  98. {
  99. return local_clock() >> 30LL; /* 2^30 ~= 10^9 */
  100. }
  101. static u64 get_sample_period(void)
  102. {
  103. /*
  104. * convert watchdog_thresh from seconds to ns
  105. * the divide by 5 is to give hrtimer several chances (two
  106. * or three with the current relation between the soft
  107. * and hard thresholds) to increment before the
  108. * hardlockup detector generates a warning
  109. */
  110. return get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
  111. }
  112. /* Commands for resetting the watchdog */
  113. static void __touch_watchdog(void)
  114. {
  115. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  116. }
  117. void touch_softlockup_watchdog(void)
  118. {
  119. __this_cpu_write(watchdog_touch_ts, 0);
  120. }
  121. EXPORT_SYMBOL(touch_softlockup_watchdog);
  122. void touch_all_softlockup_watchdogs(void)
  123. {
  124. int cpu;
  125. /*
  126. * this is done lockless
  127. * do we care if a 0 races with a timestamp?
  128. * all it means is the softlock check starts one cycle later
  129. */
  130. for_each_online_cpu(cpu)
  131. per_cpu(watchdog_touch_ts, cpu) = 0;
  132. }
  133. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  134. void touch_nmi_watchdog(void)
  135. {
  136. /*
  137. * Using __raw here because some code paths have
  138. * preemption enabled. If preemption is enabled
  139. * then interrupts should be enabled too, in which
  140. * case we shouldn't have to worry about the watchdog
  141. * going off.
  142. */
  143. __raw_get_cpu_var(watchdog_nmi_touch) = true;
  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();
  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,
  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. /* Clear the guest paused flag on watchdog reset */
  241. kvm_check_and_clear_guest_paused();
  242. __touch_watchdog();
  243. return HRTIMER_RESTART;
  244. }
  245. /* check for a softlockup
  246. * This is done by making sure a high priority task is
  247. * being scheduled. The task touches the watchdog to
  248. * indicate it is getting cpu time. If it hasn't then
  249. * this is a good indication some task is hogging the cpu
  250. */
  251. duration = is_softlockup(touch_ts);
  252. if (unlikely(duration)) {
  253. /*
  254. * If a virtual machine is stopped by the host it can look to
  255. * the watchdog like a soft lockup, check to see if the host
  256. * stopped the vm before we issue the warning
  257. */
  258. if (kvm_check_and_clear_guest_paused())
  259. return HRTIMER_RESTART;
  260. /* only warn once */
  261. if (__this_cpu_read(soft_watchdog_warn) == true)
  262. return HRTIMER_RESTART;
  263. printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  264. smp_processor_id(), duration,
  265. current->comm, task_pid_nr(current));
  266. print_modules();
  267. print_irqtrace_events(current);
  268. if (regs)
  269. show_regs(regs);
  270. else
  271. dump_stack();
  272. if (softlockup_panic)
  273. panic("softlockup: hung tasks");
  274. __this_cpu_write(soft_watchdog_warn, true);
  275. } else
  276. __this_cpu_write(soft_watchdog_warn, false);
  277. return HRTIMER_RESTART;
  278. }
  279. /*
  280. * The watchdog thread - touches the timestamp.
  281. */
  282. static int watchdog(void *unused)
  283. {
  284. struct sched_param param = { .sched_priority = 0 };
  285. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  286. /* initialize timestamp */
  287. __touch_watchdog();
  288. /* kick off the timer for the hardlockup detector */
  289. /* done here because hrtimer_start can only pin to smp_processor_id() */
  290. hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
  291. HRTIMER_MODE_REL_PINNED);
  292. set_current_state(TASK_INTERRUPTIBLE);
  293. /*
  294. * Run briefly (kicked by the hrtimer callback function) once every
  295. * get_sample_period() seconds (4 seconds by default) to reset the
  296. * softlockup timestamp. If this gets delayed for more than
  297. * 2*watchdog_thresh seconds then the debug-printout triggers in
  298. * watchdog_timer_fn().
  299. */
  300. while (!kthread_should_stop()) {
  301. __touch_watchdog();
  302. schedule();
  303. if (kthread_should_stop())
  304. break;
  305. set_current_state(TASK_INTERRUPTIBLE);
  306. }
  307. /*
  308. * Drop the policy/priority elevation during thread exit to avoid a
  309. * scheduling latency spike.
  310. */
  311. __set_current_state(TASK_RUNNING);
  312. sched_setscheduler(current, SCHED_NORMAL, &param);
  313. return 0;
  314. }
  315. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  316. /*
  317. * People like the simple clean cpu node info on boot.
  318. * Reduce the watchdog noise by only printing messages
  319. * that are different from what cpu0 displayed.
  320. */
  321. static unsigned long cpu0_err;
  322. static int watchdog_nmi_enable(int cpu)
  323. {
  324. struct perf_event_attr *wd_attr;
  325. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  326. /* is it already setup and enabled? */
  327. if (event && event->state > PERF_EVENT_STATE_OFF)
  328. goto out;
  329. /* it is setup but not enabled */
  330. if (event != NULL)
  331. goto out_enable;
  332. wd_attr = &wd_hw_attr;
  333. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  334. /* Try to register using hardware perf events */
  335. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  336. /* save cpu0 error for future comparision */
  337. if (cpu == 0 && IS_ERR(event))
  338. cpu0_err = PTR_ERR(event);
  339. if (!IS_ERR(event)) {
  340. /* only print for cpu0 or different than cpu0 */
  341. if (cpu == 0 || cpu0_err)
  342. pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
  343. goto out_save;
  344. }
  345. /* skip displaying the same error again */
  346. if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
  347. return PTR_ERR(event);
  348. /* vary the KERN level based on the returned errno */
  349. if (PTR_ERR(event) == -EOPNOTSUPP)
  350. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  351. else if (PTR_ERR(event) == -ENOENT)
  352. pr_warning("disabled (cpu%i): hardware events not enabled\n",
  353. cpu);
  354. else
  355. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  356. cpu, PTR_ERR(event));
  357. return PTR_ERR(event);
  358. /* success path */
  359. out_save:
  360. per_cpu(watchdog_ev, cpu) = event;
  361. out_enable:
  362. perf_event_enable(per_cpu(watchdog_ev, cpu));
  363. out:
  364. return 0;
  365. }
  366. static void watchdog_nmi_disable(int cpu)
  367. {
  368. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  369. if (event) {
  370. perf_event_disable(event);
  371. per_cpu(watchdog_ev, cpu) = NULL;
  372. /* should be in cleanup, but blocks oprofile */
  373. perf_event_release_kernel(event);
  374. }
  375. return;
  376. }
  377. #else
  378. static int watchdog_nmi_enable(int cpu) { return 0; }
  379. static void watchdog_nmi_disable(int cpu) { return; }
  380. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  381. /* prepare/enable/disable routines */
  382. static void watchdog_prepare_cpu(int cpu)
  383. {
  384. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  385. WARN_ON(per_cpu(softlockup_watchdog, cpu));
  386. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  387. hrtimer->function = watchdog_timer_fn;
  388. }
  389. static int watchdog_enable(int cpu)
  390. {
  391. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  392. int err = 0;
  393. /* enable the perf event */
  394. err = watchdog_nmi_enable(cpu);
  395. /* Regardless of err above, fall through and start softlockup */
  396. /* create the watchdog thread */
  397. if (!p) {
  398. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  399. p = kthread_create_on_node(watchdog, NULL, cpu_to_node(cpu), "watchdog/%d", cpu);
  400. if (IS_ERR(p)) {
  401. pr_err("softlockup watchdog for %i failed\n", cpu);
  402. if (!err) {
  403. /* if hardlockup hasn't already set this */
  404. err = PTR_ERR(p);
  405. /* and disable the perf event */
  406. watchdog_nmi_disable(cpu);
  407. }
  408. goto out;
  409. }
  410. sched_setscheduler(p, SCHED_FIFO, &param);
  411. kthread_bind(p, cpu);
  412. per_cpu(watchdog_touch_ts, cpu) = 0;
  413. per_cpu(softlockup_watchdog, cpu) = p;
  414. wake_up_process(p);
  415. }
  416. out:
  417. return err;
  418. }
  419. static void watchdog_disable(int cpu)
  420. {
  421. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  422. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  423. /*
  424. * cancel the timer first to stop incrementing the stats
  425. * and waking up the kthread
  426. */
  427. hrtimer_cancel(hrtimer);
  428. /* disable the perf event */
  429. watchdog_nmi_disable(cpu);
  430. /* stop the watchdog thread */
  431. if (p) {
  432. per_cpu(softlockup_watchdog, cpu) = NULL;
  433. kthread_stop(p);
  434. }
  435. }
  436. /* sysctl functions */
  437. #ifdef CONFIG_SYSCTL
  438. static void watchdog_enable_all_cpus(void)
  439. {
  440. int cpu;
  441. watchdog_enabled = 0;
  442. for_each_online_cpu(cpu)
  443. if (!watchdog_enable(cpu))
  444. /* if any cpu succeeds, watchdog is considered
  445. enabled for the system */
  446. watchdog_enabled = 1;
  447. if (!watchdog_enabled)
  448. pr_err("failed to be enabled on some cpus\n");
  449. }
  450. static void watchdog_disable_all_cpus(void)
  451. {
  452. int cpu;
  453. for_each_online_cpu(cpu)
  454. watchdog_disable(cpu);
  455. /* if all watchdogs are disabled, then they are disabled for the system */
  456. watchdog_enabled = 0;
  457. }
  458. /*
  459. * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
  460. */
  461. int proc_dowatchdog(struct ctl_table *table, int write,
  462. void __user *buffer, size_t *lenp, loff_t *ppos)
  463. {
  464. int ret;
  465. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  466. if (ret || !write)
  467. goto out;
  468. if (watchdog_enabled && watchdog_thresh)
  469. watchdog_enable_all_cpus();
  470. else
  471. watchdog_disable_all_cpus();
  472. out:
  473. return ret;
  474. }
  475. #endif /* CONFIG_SYSCTL */
  476. /*
  477. * Create/destroy watchdog threads as CPUs come and go:
  478. */
  479. static int
  480. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  481. {
  482. int hotcpu = (unsigned long)hcpu;
  483. switch (action) {
  484. case CPU_UP_PREPARE:
  485. case CPU_UP_PREPARE_FROZEN:
  486. watchdog_prepare_cpu(hotcpu);
  487. break;
  488. case CPU_ONLINE:
  489. case CPU_ONLINE_FROZEN:
  490. if (watchdog_enabled)
  491. watchdog_enable(hotcpu);
  492. break;
  493. #ifdef CONFIG_HOTPLUG_CPU
  494. case CPU_UP_CANCELED:
  495. case CPU_UP_CANCELED_FROZEN:
  496. watchdog_disable(hotcpu);
  497. break;
  498. case CPU_DEAD:
  499. case CPU_DEAD_FROZEN:
  500. watchdog_disable(hotcpu);
  501. break;
  502. #endif /* CONFIG_HOTPLUG_CPU */
  503. }
  504. /*
  505. * hardlockup and softlockup are not important enough
  506. * to block cpu bring up. Just always succeed and
  507. * rely on printk output to flag problems.
  508. */
  509. return NOTIFY_OK;
  510. }
  511. static struct notifier_block cpu_nfb = {
  512. .notifier_call = cpu_callback
  513. };
  514. #ifdef CONFIG_SUSPEND
  515. /*
  516. * On exit from suspend we force an offline->online transition on the boot CPU
  517. * so that the PMU state that was lost while in suspended state gets set up
  518. * properly for the boot CPU. This information is required for restarting the
  519. * NMI watchdog.
  520. */
  521. void lockup_detector_bootcpu_resume(void)
  522. {
  523. void *cpu = (void *)(long)smp_processor_id();
  524. cpu_callback(&cpu_nfb, CPU_DEAD_FROZEN, cpu);
  525. cpu_callback(&cpu_nfb, CPU_UP_PREPARE_FROZEN, cpu);
  526. cpu_callback(&cpu_nfb, CPU_ONLINE_FROZEN, cpu);
  527. }
  528. #endif
  529. void __init lockup_detector_init(void)
  530. {
  531. void *cpu = (void *)(long)smp_processor_id();
  532. int err;
  533. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  534. WARN_ON(notifier_to_errno(err));
  535. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  536. register_cpu_notifier(&cpu_nfb);
  537. return;
  538. }