watchdog.c 14 KB

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