watchdog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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/perf_event.h>
  26. int watchdog_enabled = 1;
  27. int __read_mostly watchdog_thresh = 10;
  28. static int __read_mostly watchdog_disabled;
  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. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  35. static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
  36. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  37. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  38. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  39. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  40. #endif
  41. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  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 u64 get_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. return 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 = raw_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. /*
  142. * Using __raw here because some code paths have
  143. * preemption enabled. If preemption is enabled
  144. * then interrupts should be enabled too, in which
  145. * case we shouldn't have to worry about the watchdog
  146. * going off.
  147. */
  148. __raw_get_cpu_var(watchdog_nmi_touch) = true;
  149. touch_softlockup_watchdog();
  150. }
  151. EXPORT_SYMBOL(touch_nmi_watchdog);
  152. #endif
  153. void touch_softlockup_watchdog_sync(void)
  154. {
  155. __raw_get_cpu_var(softlockup_touch_sync) = true;
  156. __raw_get_cpu_var(watchdog_touch_ts) = 0;
  157. }
  158. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  159. /* watchdog detector functions */
  160. static int is_hardlockup(void)
  161. {
  162. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  163. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  164. return 1;
  165. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  166. return 0;
  167. }
  168. #endif
  169. #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
  170. static int is_hardlockup_other_cpu(int cpu)
  171. {
  172. unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
  173. if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
  174. return 1;
  175. per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
  176. return 0;
  177. }
  178. static void watchdog_check_hardlockup_other_cpu(void)
  179. {
  180. int cpu;
  181. /*
  182. * Test for hardlockups every 3 samples. The sample period is
  183. * watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
  184. * watchdog_thresh (over by 20%).
  185. */
  186. if (__this_cpu_read(hrtimer_interrupts) % 3 != 0)
  187. return;
  188. /* check for a hardlockup on the next cpu */
  189. cpu = cpumask_next(smp_processor_id(), cpu_online_mask);
  190. if (cpu >= nr_cpu_ids)
  191. cpu = cpumask_first(cpu_online_mask);
  192. if (cpu == smp_processor_id())
  193. return;
  194. if (per_cpu(watchdog_nmi_touch, cpu) == true) {
  195. per_cpu(watchdog_nmi_touch, cpu) = false;
  196. return;
  197. }
  198. if (is_hardlockup_other_cpu(cpu)) {
  199. /* only warn once */
  200. if (per_cpu(hard_watchdog_warn, cpu) == true)
  201. return;
  202. if (hardlockup_panic)
  203. panic("Watchdog detected hard LOCKUP on cpu %d", cpu);
  204. else
  205. WARN(1, "Watchdog detected hard LOCKUP on cpu %d", cpu);
  206. per_cpu(hard_watchdog_warn, cpu) = true;
  207. } else {
  208. per_cpu(hard_watchdog_warn, cpu) = false;
  209. }
  210. }
  211. #else
  212. static inline void watchdog_check_hardlockup_other_cpu(void) { return; }
  213. #endif
  214. static int is_softlockup(unsigned long touch_ts)
  215. {
  216. unsigned long now = get_timestamp(smp_processor_id());
  217. /* Warn about unreasonable delays: */
  218. if (time_after(now, touch_ts + get_softlockup_thresh()))
  219. return now - touch_ts;
  220. return 0;
  221. }
  222. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  223. static struct perf_event_attr wd_hw_attr = {
  224. .type = PERF_TYPE_HARDWARE,
  225. .config = PERF_COUNT_HW_CPU_CYCLES,
  226. .size = sizeof(struct perf_event_attr),
  227. .pinned = 1,
  228. .disabled = 1,
  229. };
  230. /* Callback function for perf event subsystem */
  231. static void watchdog_overflow_callback(struct perf_event *event,
  232. struct perf_sample_data *data,
  233. struct pt_regs *regs)
  234. {
  235. /* Ensure the watchdog never gets throttled */
  236. event->hw.interrupts = 0;
  237. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  238. __this_cpu_write(watchdog_nmi_touch, false);
  239. return;
  240. }
  241. /* check for a hardlockup
  242. * This is done by making sure our timer interrupt
  243. * is incrementing. The timer interrupt should have
  244. * fired multiple times before we overflow'd. If it hasn't
  245. * then this is a good indication the cpu is stuck
  246. */
  247. if (is_hardlockup()) {
  248. int this_cpu = smp_processor_id();
  249. /* only print hardlockups once */
  250. if (__this_cpu_read(hard_watchdog_warn) == true)
  251. return;
  252. if (hardlockup_panic)
  253. panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  254. else
  255. WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  256. __this_cpu_write(hard_watchdog_warn, true);
  257. return;
  258. }
  259. __this_cpu_write(hard_watchdog_warn, false);
  260. return;
  261. }
  262. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  263. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  264. static void watchdog_interrupt_count(void)
  265. {
  266. __this_cpu_inc(hrtimer_interrupts);
  267. }
  268. static int watchdog_nmi_enable(unsigned int cpu);
  269. static void watchdog_nmi_disable(unsigned int cpu);
  270. /* watchdog kicker functions */
  271. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  272. {
  273. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  274. struct pt_regs *regs = get_irq_regs();
  275. int duration;
  276. /* kick the hardlockup detector */
  277. watchdog_interrupt_count();
  278. /* test for hardlockups on the next cpu */
  279. watchdog_check_hardlockup_other_cpu();
  280. /* kick the softlockup detector */
  281. wake_up_process(__this_cpu_read(softlockup_watchdog));
  282. /* .. and repeat */
  283. hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
  284. if (touch_ts == 0) {
  285. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  286. /*
  287. * If the time stamp was touched atomically
  288. * make sure the scheduler tick is up to date.
  289. */
  290. __this_cpu_write(softlockup_touch_sync, false);
  291. sched_clock_tick();
  292. }
  293. __touch_watchdog();
  294. return HRTIMER_RESTART;
  295. }
  296. /* check for a softlockup
  297. * This is done by making sure a high priority task is
  298. * being scheduled. The task touches the watchdog to
  299. * indicate it is getting cpu time. If it hasn't then
  300. * this is a good indication some task is hogging the cpu
  301. */
  302. duration = is_softlockup(touch_ts);
  303. if (unlikely(duration)) {
  304. /* only warn once */
  305. if (__this_cpu_read(soft_watchdog_warn) == true)
  306. return HRTIMER_RESTART;
  307. printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  308. smp_processor_id(), duration,
  309. current->comm, task_pid_nr(current));
  310. print_modules();
  311. print_irqtrace_events(current);
  312. if (regs)
  313. show_regs(regs);
  314. else
  315. dump_stack();
  316. if (softlockup_panic)
  317. panic("softlockup: hung tasks");
  318. __this_cpu_write(soft_watchdog_warn, true);
  319. } else
  320. __this_cpu_write(soft_watchdog_warn, false);
  321. return HRTIMER_RESTART;
  322. }
  323. static void watchdog_set_prio(unsigned int policy, unsigned int prio)
  324. {
  325. struct sched_param param = { .sched_priority = prio };
  326. sched_setscheduler(current, policy, &param);
  327. }
  328. static void watchdog_enable(unsigned int cpu)
  329. {
  330. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  331. if (!watchdog_enabled) {
  332. kthread_park(current);
  333. return;
  334. }
  335. /* Enable the perf event */
  336. watchdog_nmi_enable(cpu);
  337. /* kick off the timer for the hardlockup detector */
  338. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  339. hrtimer->function = watchdog_timer_fn;
  340. /* done here because hrtimer_start can only pin to smp_processor_id() */
  341. hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
  342. HRTIMER_MODE_REL_PINNED);
  343. /* initialize timestamp */
  344. watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
  345. __touch_watchdog();
  346. }
  347. static void watchdog_disable(unsigned int cpu)
  348. {
  349. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  350. if (!watchdog_enabled)
  351. return;
  352. watchdog_set_prio(SCHED_NORMAL, 0);
  353. hrtimer_cancel(hrtimer);
  354. /* disable the perf event */
  355. watchdog_nmi_disable(cpu);
  356. }
  357. static int watchdog_should_run(unsigned int cpu)
  358. {
  359. return __this_cpu_read(hrtimer_interrupts) !=
  360. __this_cpu_read(soft_lockup_hrtimer_cnt);
  361. }
  362. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  363. static int watchdog_nmi_enable(unsigned int cpu)
  364. {
  365. struct perf_event_attr *wd_attr;
  366. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  367. /* is it already setup and enabled? */
  368. if (event && event->state > PERF_EVENT_STATE_OFF)
  369. goto out;
  370. /* it is setup but not enabled */
  371. if (event != NULL)
  372. goto out_enable;
  373. wd_attr = &wd_hw_attr;
  374. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  375. /* Try to register using hardware perf events */
  376. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  377. if (!IS_ERR(event)) {
  378. pr_info("enabled, takes one hw-pmu counter.\n");
  379. goto out_save;
  380. }
  381. /* vary the KERN level based on the returned errno */
  382. if (PTR_ERR(event) == -EOPNOTSUPP)
  383. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  384. else if (PTR_ERR(event) == -ENOENT)
  385. pr_warning("disabled (cpu%i): hardware events not enabled\n",
  386. cpu);
  387. else
  388. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  389. cpu, PTR_ERR(event));
  390. return PTR_ERR(event);
  391. /* success path */
  392. out_save:
  393. per_cpu(watchdog_ev, cpu) = event;
  394. out_enable:
  395. perf_event_enable(per_cpu(watchdog_ev, cpu));
  396. out:
  397. return 0;
  398. }
  399. static void watchdog_nmi_disable(unsigned int cpu)
  400. {
  401. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  402. if (event) {
  403. perf_event_disable(event);
  404. per_cpu(watchdog_ev, cpu) = NULL;
  405. /* should be in cleanup, but blocks oprofile */
  406. perf_event_release_kernel(event);
  407. }
  408. return;
  409. }
  410. #else
  411. static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
  412. static void watchdog_nmi_disable(unsigned int cpu) { return; }
  413. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  414. /* prepare/enable/disable routines */
  415. static void watchdog_prepare_cpu(int cpu)
  416. /* sysctl functions */
  417. #ifdef CONFIG_SYSCTL
  418. static void watchdog_enable_all_cpus(void)
  419. {
  420. unsigned int cpu;
  421. if (watchdog_disabled) {
  422. watchdog_disabled = 0;
  423. for_each_online_cpu(cpu)
  424. kthread_unpark(per_cpu(softlockup_watchdog, cpu));
  425. }
  426. }
  427. static void watchdog_disable_all_cpus(void)
  428. {
  429. unsigned int cpu;
  430. if (!watchdog_disabled) {
  431. watchdog_disabled = 1;
  432. for_each_online_cpu(cpu)
  433. kthread_park(per_cpu(softlockup_watchdog, cpu));
  434. }
  435. }
  436. /*
  437. * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
  438. */
  439. int proc_dowatchdog(struct ctl_table *table, int write,
  440. void __user *buffer, size_t *lenp, loff_t *ppos)
  441. {
  442. int ret;
  443. if (watchdog_disabled < 0)
  444. return -ENODEV;
  445. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  446. if (ret || !write)
  447. return ret;
  448. if (watchdog_enabled && watchdog_thresh)
  449. watchdog_enable_all_cpus();
  450. else
  451. watchdog_disable_all_cpus();
  452. return ret;
  453. }
  454. #endif /* CONFIG_SYSCTL */
  455. static struct smp_hotplug_thread watchdog_threads = {
  456. .store = &softlockup_watchdog,
  457. .thread_should_run = watchdog_should_run,
  458. .thread_fn = watchdog,
  459. .thread_comm = "watchdog/%u",
  460. .setup = watchdog_enable,
  461. .park = watchdog_disable,
  462. .unpark = watchdog_enable,
  463. };
  464. void __init lockup_detector_init(void)
  465. {
  466. if (smpboot_register_percpu_thread(&watchdog_threads)) {
  467. pr_err("Failed to create watchdog threads, disabled\n");
  468. watchdog_disabled = -ENODEV;
  469. }
  470. }