watchdog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 <linux/smpboot.h>
  24. #include <asm/irq_regs.h>
  25. #include <linux/kvm_para.h>
  26. #include <linux/perf_event.h>
  27. int watchdog_enabled = 1;
  28. int __read_mostly watchdog_thresh = 10;
  29. static int __read_mostly watchdog_disabled;
  30. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  31. static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
  32. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  33. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  34. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  35. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  36. static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
  37. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  38. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  39. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  40. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  41. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  42. #endif
  43. /* boot commands */
  44. /*
  45. * Should we panic when a soft-lockup or hard-lockup occurs:
  46. */
  47. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  48. static int hardlockup_panic =
  49. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  50. static int __init hardlockup_panic_setup(char *str)
  51. {
  52. if (!strncmp(str, "panic", 5))
  53. hardlockup_panic = 1;
  54. else if (!strncmp(str, "nopanic", 7))
  55. hardlockup_panic = 0;
  56. else if (!strncmp(str, "0", 1))
  57. watchdog_enabled = 0;
  58. return 1;
  59. }
  60. __setup("nmi_watchdog=", hardlockup_panic_setup);
  61. #endif
  62. unsigned int __read_mostly softlockup_panic =
  63. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  64. static int __init softlockup_panic_setup(char *str)
  65. {
  66. softlockup_panic = simple_strtoul(str, NULL, 0);
  67. return 1;
  68. }
  69. __setup("softlockup_panic=", softlockup_panic_setup);
  70. static int __init nowatchdog_setup(char *str)
  71. {
  72. watchdog_enabled = 0;
  73. return 1;
  74. }
  75. __setup("nowatchdog", nowatchdog_setup);
  76. /* deprecated */
  77. static int __init nosoftlockup_setup(char *str)
  78. {
  79. watchdog_enabled = 0;
  80. return 1;
  81. }
  82. __setup("nosoftlockup", nosoftlockup_setup);
  83. /* */
  84. /*
  85. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  86. * lockups can have false positives under extreme conditions. So we generally
  87. * want a higher threshold for soft lockups than for hard lockups. So we couple
  88. * the thresholds with a factor: we make the soft threshold twice the amount of
  89. * time the hard threshold is.
  90. */
  91. static int get_softlockup_thresh(void)
  92. {
  93. return watchdog_thresh * 2;
  94. }
  95. /*
  96. * Returns seconds, approximately. We don't need nanosecond
  97. * resolution, and we don't need to waste time with a big divide when
  98. * 2^30ns == 1.074s.
  99. */
  100. static unsigned long get_timestamp(int this_cpu)
  101. {
  102. return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
  103. }
  104. static unsigned long get_sample_period(void)
  105. {
  106. /*
  107. * convert watchdog_thresh from seconds to ns
  108. * the divide by 5 is to give hrtimer several chances (two
  109. * or three with the current relation between the soft
  110. * and hard thresholds) to increment before the
  111. * hardlockup detector generates a warning
  112. */
  113. return get_softlockup_thresh() * (NSEC_PER_SEC / 5);
  114. }
  115. /* Commands for resetting the watchdog */
  116. static void __touch_watchdog(void)
  117. {
  118. int this_cpu = smp_processor_id();
  119. __this_cpu_write(watchdog_touch_ts, get_timestamp(this_cpu));
  120. }
  121. void touch_softlockup_watchdog(void)
  122. {
  123. __this_cpu_write(watchdog_touch_ts, 0);
  124. }
  125. EXPORT_SYMBOL(touch_softlockup_watchdog);
  126. void touch_all_softlockup_watchdogs(void)
  127. {
  128. int cpu;
  129. /*
  130. * this is done lockless
  131. * do we care if a 0 races with a timestamp?
  132. * all it means is the softlock check starts one cycle later
  133. */
  134. for_each_online_cpu(cpu)
  135. per_cpu(watchdog_touch_ts, cpu) = 0;
  136. }
  137. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  138. void touch_nmi_watchdog(void)
  139. {
  140. if (watchdog_enabled) {
  141. unsigned cpu;
  142. for_each_present_cpu(cpu) {
  143. if (per_cpu(watchdog_nmi_touch, cpu) != true)
  144. per_cpu(watchdog_nmi_touch, cpu) = true;
  145. }
  146. }
  147. touch_softlockup_watchdog();
  148. }
  149. EXPORT_SYMBOL(touch_nmi_watchdog);
  150. #endif
  151. void touch_softlockup_watchdog_sync(void)
  152. {
  153. __raw_get_cpu_var(softlockup_touch_sync) = true;
  154. __raw_get_cpu_var(watchdog_touch_ts) = 0;
  155. }
  156. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  157. /* watchdog detector functions */
  158. static int is_hardlockup(void)
  159. {
  160. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  161. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  162. return 1;
  163. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  164. return 0;
  165. }
  166. #endif
  167. static int is_softlockup(unsigned long touch_ts)
  168. {
  169. unsigned long now = get_timestamp(smp_processor_id());
  170. /* Warn about unreasonable delays: */
  171. if (time_after(now, touch_ts + get_softlockup_thresh()))
  172. return now - touch_ts;
  173. return 0;
  174. }
  175. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  176. static struct perf_event_attr wd_hw_attr = {
  177. .type = PERF_TYPE_HARDWARE,
  178. .config = PERF_COUNT_HW_CPU_CYCLES,
  179. .size = sizeof(struct perf_event_attr),
  180. .pinned = 1,
  181. .disabled = 1,
  182. };
  183. /* Callback function for perf event subsystem */
  184. static void watchdog_overflow_callback(struct perf_event *event,
  185. struct perf_sample_data *data,
  186. struct pt_regs *regs)
  187. {
  188. /* Ensure the watchdog never gets throttled */
  189. event->hw.interrupts = 0;
  190. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  191. __this_cpu_write(watchdog_nmi_touch, false);
  192. return;
  193. }
  194. /* check for a hardlockup
  195. * This is done by making sure our timer interrupt
  196. * is incrementing. The timer interrupt should have
  197. * fired multiple times before we overflow'd. If it hasn't
  198. * then this is a good indication the cpu is stuck
  199. */
  200. if (is_hardlockup()) {
  201. int this_cpu = smp_processor_id();
  202. /* only print hardlockups once */
  203. if (__this_cpu_read(hard_watchdog_warn) == true)
  204. return;
  205. if (hardlockup_panic)
  206. panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  207. else
  208. WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  209. __this_cpu_write(hard_watchdog_warn, true);
  210. return;
  211. }
  212. __this_cpu_write(hard_watchdog_warn, false);
  213. return;
  214. }
  215. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  216. static void watchdog_interrupt_count(void)
  217. {
  218. __this_cpu_inc(hrtimer_interrupts);
  219. }
  220. static int watchdog_nmi_enable(unsigned int cpu);
  221. static void watchdog_nmi_disable(unsigned int cpu);
  222. /* watchdog kicker functions */
  223. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  224. {
  225. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  226. struct pt_regs *regs = get_irq_regs();
  227. int duration;
  228. /* kick the hardlockup detector */
  229. watchdog_interrupt_count();
  230. /* kick the softlockup detector */
  231. wake_up_process(__this_cpu_read(softlockup_watchdog));
  232. /* .. and repeat */
  233. hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
  234. if (touch_ts == 0) {
  235. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  236. /*
  237. * If the time stamp was touched atomically
  238. * make sure the scheduler tick is up to date.
  239. */
  240. __this_cpu_write(softlockup_touch_sync, false);
  241. sched_clock_tick();
  242. }
  243. /* Clear the guest paused flag on watchdog reset */
  244. kvm_check_and_clear_guest_paused();
  245. __touch_watchdog();
  246. return HRTIMER_RESTART;
  247. }
  248. /* check for a softlockup
  249. * This is done by making sure a high priority task is
  250. * being scheduled. The task touches the watchdog to
  251. * indicate it is getting cpu time. If it hasn't then
  252. * this is a good indication some task is hogging the cpu
  253. */
  254. duration = is_softlockup(touch_ts);
  255. if (unlikely(duration)) {
  256. /*
  257. * If a virtual machine is stopped by the host it can look to
  258. * the watchdog like a soft lockup, check to see if the host
  259. * stopped the vm before we issue the warning
  260. */
  261. if (kvm_check_and_clear_guest_paused())
  262. return HRTIMER_RESTART;
  263. /* only warn once */
  264. if (__this_cpu_read(soft_watchdog_warn) == true)
  265. return HRTIMER_RESTART;
  266. printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  267. smp_processor_id(), duration,
  268. current->comm, task_pid_nr(current));
  269. print_modules();
  270. print_irqtrace_events(current);
  271. if (regs)
  272. show_regs(regs);
  273. else
  274. dump_stack();
  275. if (softlockup_panic)
  276. panic("softlockup: hung tasks");
  277. __this_cpu_write(soft_watchdog_warn, true);
  278. } else
  279. __this_cpu_write(soft_watchdog_warn, false);
  280. return HRTIMER_RESTART;
  281. }
  282. static void watchdog_set_prio(unsigned int policy, unsigned int prio)
  283. {
  284. struct sched_param param = { .sched_priority = prio };
  285. sched_setscheduler(current, policy, &param);
  286. }
  287. static void watchdog_enable(unsigned int cpu)
  288. {
  289. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  290. if (!watchdog_enabled) {
  291. kthread_park(current);
  292. return;
  293. }
  294. /* Enable the perf event */
  295. watchdog_nmi_enable(cpu);
  296. /* kick off the timer for the hardlockup detector */
  297. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  298. hrtimer->function = watchdog_timer_fn;
  299. /* done here because hrtimer_start can only pin to smp_processor_id() */
  300. hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
  301. HRTIMER_MODE_REL_PINNED);
  302. /* initialize timestamp */
  303. watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
  304. __touch_watchdog();
  305. }
  306. static void watchdog_disable(unsigned int cpu)
  307. {
  308. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  309. watchdog_set_prio(SCHED_NORMAL, 0);
  310. hrtimer_cancel(hrtimer);
  311. /* disable the perf event */
  312. watchdog_nmi_disable(cpu);
  313. }
  314. static int watchdog_should_run(unsigned int cpu)
  315. {
  316. return __this_cpu_read(hrtimer_interrupts) !=
  317. __this_cpu_read(soft_lockup_hrtimer_cnt);
  318. }
  319. /*
  320. * The watchdog thread function - touches the timestamp.
  321. *
  322. * It only runs once every get_sample_period() seconds (4 seconds by
  323. * default) to reset the softlockup timestamp. If this gets delayed
  324. * for more than 2*watchdog_thresh seconds then the debug-printout
  325. * triggers in watchdog_timer_fn().
  326. */
  327. static void watchdog(unsigned int cpu)
  328. {
  329. __this_cpu_write(soft_lockup_hrtimer_cnt,
  330. __this_cpu_read(hrtimer_interrupts));
  331. __touch_watchdog();
  332. }
  333. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  334. /*
  335. * People like the simple clean cpu node info on boot.
  336. * Reduce the watchdog noise by only printing messages
  337. * that are different from what cpu0 displayed.
  338. */
  339. static unsigned long cpu0_err;
  340. static int watchdog_nmi_enable(unsigned int cpu)
  341. {
  342. struct perf_event_attr *wd_attr;
  343. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  344. /* is it already setup and enabled? */
  345. if (event && event->state > PERF_EVENT_STATE_OFF)
  346. goto out;
  347. /* it is setup but not enabled */
  348. if (event != NULL)
  349. goto out_enable;
  350. wd_attr = &wd_hw_attr;
  351. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  352. /* Try to register using hardware perf events */
  353. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  354. /* save cpu0 error for future comparision */
  355. if (cpu == 0 && IS_ERR(event))
  356. cpu0_err = PTR_ERR(event);
  357. if (!IS_ERR(event)) {
  358. /* only print for cpu0 or different than cpu0 */
  359. if (cpu == 0 || cpu0_err)
  360. pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
  361. goto out_save;
  362. }
  363. /* skip displaying the same error again */
  364. if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
  365. return PTR_ERR(event);
  366. /* vary the KERN level based on the returned errno */
  367. if (PTR_ERR(event) == -EOPNOTSUPP)
  368. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  369. else if (PTR_ERR(event) == -ENOENT)
  370. pr_warning("disabled (cpu%i): hardware events not enabled\n",
  371. cpu);
  372. else
  373. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  374. cpu, PTR_ERR(event));
  375. return PTR_ERR(event);
  376. /* success path */
  377. out_save:
  378. per_cpu(watchdog_ev, cpu) = event;
  379. out_enable:
  380. perf_event_enable(per_cpu(watchdog_ev, cpu));
  381. out:
  382. return 0;
  383. }
  384. static void watchdog_nmi_disable(unsigned int cpu)
  385. {
  386. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  387. if (event) {
  388. perf_event_disable(event);
  389. per_cpu(watchdog_ev, cpu) = NULL;
  390. /* should be in cleanup, but blocks oprofile */
  391. perf_event_release_kernel(event);
  392. }
  393. return;
  394. }
  395. #else
  396. static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
  397. static void watchdog_nmi_disable(unsigned int cpu) { return; }
  398. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  399. /* prepare/enable/disable routines */
  400. /* sysctl functions */
  401. #ifdef CONFIG_SYSCTL
  402. static void watchdog_enable_all_cpus(void)
  403. {
  404. unsigned int cpu;
  405. if (watchdog_disabled) {
  406. watchdog_disabled = 0;
  407. for_each_online_cpu(cpu)
  408. kthread_unpark(per_cpu(softlockup_watchdog, cpu));
  409. }
  410. }
  411. static void watchdog_disable_all_cpus(void)
  412. {
  413. unsigned int cpu;
  414. if (!watchdog_disabled) {
  415. watchdog_disabled = 1;
  416. for_each_online_cpu(cpu)
  417. kthread_park(per_cpu(softlockup_watchdog, cpu));
  418. }
  419. }
  420. /*
  421. * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
  422. */
  423. int proc_dowatchdog(struct ctl_table *table, int write,
  424. void __user *buffer, size_t *lenp, loff_t *ppos)
  425. {
  426. int ret;
  427. if (watchdog_disabled < 0)
  428. return -ENODEV;
  429. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  430. if (ret || !write)
  431. return ret;
  432. if (watchdog_enabled && watchdog_thresh)
  433. watchdog_enable_all_cpus();
  434. else
  435. watchdog_disable_all_cpus();
  436. return ret;
  437. }
  438. #endif /* CONFIG_SYSCTL */
  439. static struct smp_hotplug_thread watchdog_threads = {
  440. .store = &softlockup_watchdog,
  441. .thread_should_run = watchdog_should_run,
  442. .thread_fn = watchdog,
  443. .thread_comm = "watchdog/%u",
  444. .setup = watchdog_enable,
  445. .park = watchdog_disable,
  446. .unpark = watchdog_enable,
  447. };
  448. void __init lockup_detector_init(void)
  449. {
  450. if (smpboot_register_percpu_thread(&watchdog_threads)) {
  451. pr_err("Failed to create watchdog threads, disabled\n");
  452. watchdog_disabled = -ENODEV;
  453. }
  454. }