watchdog.c 13 KB

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