watchdog.c 16 KB

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