watchdog.c 14 KB

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