watchdog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. __raw_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. if (watchdog_enabled) {
  124. unsigned cpu;
  125. for_each_present_cpu(cpu) {
  126. if (per_cpu(watchdog_nmi_touch, cpu) != true)
  127. per_cpu(watchdog_nmi_touch, cpu) = true;
  128. }
  129. }
  130. touch_softlockup_watchdog();
  131. }
  132. EXPORT_SYMBOL(touch_nmi_watchdog);
  133. #endif
  134. void touch_softlockup_watchdog_sync(void)
  135. {
  136. __raw_get_cpu_var(softlockup_touch_sync) = true;
  137. __raw_get_cpu_var(watchdog_touch_ts) = 0;
  138. }
  139. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  140. /* watchdog detector functions */
  141. static int is_hardlockup(void)
  142. {
  143. unsigned long hrint = __get_cpu_var(hrtimer_interrupts);
  144. if (__get_cpu_var(hrtimer_interrupts_saved) == hrint)
  145. return 1;
  146. __get_cpu_var(hrtimer_interrupts_saved) = hrint;
  147. return 0;
  148. }
  149. #endif
  150. static int is_softlockup(unsigned long touch_ts)
  151. {
  152. unsigned long now = get_timestamp(smp_processor_id());
  153. /* Warn about unreasonable delays: */
  154. if (time_after(now, touch_ts + softlockup_thresh))
  155. return now - touch_ts;
  156. return 0;
  157. }
  158. static int
  159. watchdog_panic(struct notifier_block *this, unsigned long event, void *ptr)
  160. {
  161. did_panic = 1;
  162. return NOTIFY_DONE;
  163. }
  164. static struct notifier_block panic_block = {
  165. .notifier_call = watchdog_panic,
  166. };
  167. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  168. static struct perf_event_attr wd_hw_attr = {
  169. .type = PERF_TYPE_HARDWARE,
  170. .config = PERF_COUNT_HW_CPU_CYCLES,
  171. .size = sizeof(struct perf_event_attr),
  172. .pinned = 1,
  173. .disabled = 1,
  174. };
  175. /* Callback function for perf event subsystem */
  176. void watchdog_overflow_callback(struct perf_event *event, int nmi,
  177. struct perf_sample_data *data,
  178. struct pt_regs *regs)
  179. {
  180. /* Ensure the watchdog never gets throttled */
  181. event->hw.interrupts = 0;
  182. if (__get_cpu_var(watchdog_nmi_touch) == true) {
  183. __get_cpu_var(watchdog_nmi_touch) = false;
  184. return;
  185. }
  186. /* check for a hardlockup
  187. * This is done by making sure our timer interrupt
  188. * is incrementing. The timer interrupt should have
  189. * fired multiple times before we overflow'd. If it hasn't
  190. * then this is a good indication the cpu is stuck
  191. */
  192. if (is_hardlockup()) {
  193. int this_cpu = smp_processor_id();
  194. /* only print hardlockups once */
  195. if (__get_cpu_var(hard_watchdog_warn) == true)
  196. return;
  197. if (hardlockup_panic)
  198. panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  199. else
  200. WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  201. __get_cpu_var(hard_watchdog_warn) = true;
  202. return;
  203. }
  204. __get_cpu_var(hard_watchdog_warn) = false;
  205. return;
  206. }
  207. static void watchdog_interrupt_count(void)
  208. {
  209. __get_cpu_var(hrtimer_interrupts)++;
  210. }
  211. #else
  212. static inline void watchdog_interrupt_count(void) { return; }
  213. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  214. /* watchdog kicker functions */
  215. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  216. {
  217. unsigned long touch_ts = __get_cpu_var(watchdog_touch_ts);
  218. struct pt_regs *regs = get_irq_regs();
  219. int duration;
  220. /* kick the hardlockup detector */
  221. watchdog_interrupt_count();
  222. /* kick the softlockup detector */
  223. wake_up_process(__get_cpu_var(softlockup_watchdog));
  224. /* .. and repeat */
  225. hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
  226. if (touch_ts == 0) {
  227. if (unlikely(__get_cpu_var(softlockup_touch_sync))) {
  228. /*
  229. * If the time stamp was touched atomically
  230. * make sure the scheduler tick is up to date.
  231. */
  232. __get_cpu_var(softlockup_touch_sync) = false;
  233. sched_clock_tick();
  234. }
  235. __touch_watchdog();
  236. return HRTIMER_RESTART;
  237. }
  238. /* check for a softlockup
  239. * This is done by making sure a high priority task is
  240. * being scheduled. The task touches the watchdog to
  241. * indicate it is getting cpu time. If it hasn't then
  242. * this is a good indication some task is hogging the cpu
  243. */
  244. duration = is_softlockup(touch_ts);
  245. if (unlikely(duration)) {
  246. /* only warn once */
  247. if (__get_cpu_var(soft_watchdog_warn) == true)
  248. return HRTIMER_RESTART;
  249. printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  250. smp_processor_id(), duration,
  251. current->comm, task_pid_nr(current));
  252. print_modules();
  253. print_irqtrace_events(current);
  254. if (regs)
  255. show_regs(regs);
  256. else
  257. dump_stack();
  258. if (softlockup_panic)
  259. panic("softlockup: hung tasks");
  260. __get_cpu_var(soft_watchdog_warn) = true;
  261. } else
  262. __get_cpu_var(soft_watchdog_warn) = false;
  263. return HRTIMER_RESTART;
  264. }
  265. /*
  266. * The watchdog thread - touches the timestamp.
  267. */
  268. static int watchdog(void *unused)
  269. {
  270. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  271. struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
  272. sched_setscheduler(current, SCHED_FIFO, &param);
  273. /* initialize timestamp */
  274. __touch_watchdog();
  275. /* kick off the timer for the hardlockup detector */
  276. /* done here because hrtimer_start can only pin to smp_processor_id() */
  277. hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
  278. HRTIMER_MODE_REL_PINNED);
  279. set_current_state(TASK_INTERRUPTIBLE);
  280. /*
  281. * Run briefly once per second to reset the softlockup timestamp.
  282. * If this gets delayed for more than 60 seconds then the
  283. * debug-printout triggers in watchdog_timer_fn().
  284. */
  285. while (!kthread_should_stop()) {
  286. __touch_watchdog();
  287. schedule();
  288. if (kthread_should_stop())
  289. break;
  290. set_current_state(TASK_INTERRUPTIBLE);
  291. }
  292. __set_current_state(TASK_RUNNING);
  293. return 0;
  294. }
  295. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  296. static int watchdog_nmi_enable(int cpu)
  297. {
  298. struct perf_event_attr *wd_attr;
  299. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  300. /* is it already setup and enabled? */
  301. if (event && event->state > PERF_EVENT_STATE_OFF)
  302. goto out;
  303. /* it is setup but not enabled */
  304. if (event != NULL)
  305. goto out_enable;
  306. /* Try to register using hardware perf events */
  307. wd_attr = &wd_hw_attr;
  308. wd_attr->sample_period = hw_nmi_get_sample_period();
  309. event = perf_event_create_kernel_counter(wd_attr, cpu, -1, watchdog_overflow_callback);
  310. if (!IS_ERR(event)) {
  311. printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
  312. goto out_save;
  313. }
  314. printk(KERN_ERR "NMI watchdog failed to create perf event on cpu%i: %p\n", cpu, event);
  315. return -1;
  316. /* success path */
  317. out_save:
  318. per_cpu(watchdog_ev, cpu) = event;
  319. out_enable:
  320. perf_event_enable(per_cpu(watchdog_ev, cpu));
  321. out:
  322. return 0;
  323. }
  324. static void watchdog_nmi_disable(int cpu)
  325. {
  326. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  327. if (event) {
  328. perf_event_disable(event);
  329. per_cpu(watchdog_ev, cpu) = NULL;
  330. /* should be in cleanup, but blocks oprofile */
  331. perf_event_release_kernel(event);
  332. }
  333. return;
  334. }
  335. #else
  336. static int watchdog_nmi_enable(int cpu) { return 0; }
  337. static void watchdog_nmi_disable(int cpu) { return; }
  338. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  339. /* prepare/enable/disable routines */
  340. static int watchdog_prepare_cpu(int cpu)
  341. {
  342. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  343. WARN_ON(per_cpu(softlockup_watchdog, cpu));
  344. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  345. hrtimer->function = watchdog_timer_fn;
  346. return 0;
  347. }
  348. static int watchdog_enable(int cpu)
  349. {
  350. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  351. /* enable the perf event */
  352. if (watchdog_nmi_enable(cpu) != 0)
  353. return -1;
  354. /* create the watchdog thread */
  355. if (!p) {
  356. p = kthread_create(watchdog, (void *)(unsigned long)cpu, "watchdog/%d", cpu);
  357. if (IS_ERR(p)) {
  358. printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);
  359. return -1;
  360. }
  361. kthread_bind(p, cpu);
  362. per_cpu(watchdog_touch_ts, cpu) = 0;
  363. per_cpu(softlockup_watchdog, cpu) = p;
  364. wake_up_process(p);
  365. }
  366. /* if any cpu succeeds, watchdog is considered enabled for the system */
  367. watchdog_enabled = 1;
  368. return 0;
  369. }
  370. static void watchdog_disable(int cpu)
  371. {
  372. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  373. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  374. /*
  375. * cancel the timer first to stop incrementing the stats
  376. * and waking up the kthread
  377. */
  378. hrtimer_cancel(hrtimer);
  379. /* disable the perf event */
  380. watchdog_nmi_disable(cpu);
  381. /* stop the watchdog thread */
  382. if (p) {
  383. per_cpu(softlockup_watchdog, cpu) = NULL;
  384. kthread_stop(p);
  385. }
  386. }
  387. static void watchdog_enable_all_cpus(void)
  388. {
  389. int cpu;
  390. int result = 0;
  391. for_each_online_cpu(cpu)
  392. result += watchdog_enable(cpu);
  393. if (result)
  394. printk(KERN_ERR "watchdog: failed to be enabled on some cpus\n");
  395. }
  396. static void watchdog_disable_all_cpus(void)
  397. {
  398. int cpu;
  399. for_each_online_cpu(cpu)
  400. watchdog_disable(cpu);
  401. /* if all watchdogs are disabled, then they are disabled for the system */
  402. watchdog_enabled = 0;
  403. }
  404. /* sysctl functions */
  405. #ifdef CONFIG_SYSCTL
  406. /*
  407. * proc handler for /proc/sys/kernel/nmi_watchdog
  408. */
  409. int proc_dowatchdog_enabled(struct ctl_table *table, int write,
  410. void __user *buffer, size_t *length, loff_t *ppos)
  411. {
  412. proc_dointvec(table, write, buffer, length, ppos);
  413. if (watchdog_enabled)
  414. watchdog_enable_all_cpus();
  415. else
  416. watchdog_disable_all_cpus();
  417. return 0;
  418. }
  419. int proc_dowatchdog_thresh(struct ctl_table *table, int write,
  420. void __user *buffer,
  421. size_t *lenp, loff_t *ppos)
  422. {
  423. return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  424. }
  425. #endif /* CONFIG_SYSCTL */
  426. /*
  427. * Create/destroy watchdog threads as CPUs come and go:
  428. */
  429. static int __cpuinit
  430. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  431. {
  432. int hotcpu = (unsigned long)hcpu;
  433. switch (action) {
  434. case CPU_UP_PREPARE:
  435. case CPU_UP_PREPARE_FROZEN:
  436. if (watchdog_prepare_cpu(hotcpu))
  437. return NOTIFY_BAD;
  438. break;
  439. case CPU_ONLINE:
  440. case CPU_ONLINE_FROZEN:
  441. if (watchdog_enable(hotcpu))
  442. return NOTIFY_BAD;
  443. break;
  444. #ifdef CONFIG_HOTPLUG_CPU
  445. case CPU_UP_CANCELED:
  446. case CPU_UP_CANCELED_FROZEN:
  447. watchdog_disable(hotcpu);
  448. break;
  449. case CPU_DEAD:
  450. case CPU_DEAD_FROZEN:
  451. watchdog_disable(hotcpu);
  452. break;
  453. #endif /* CONFIG_HOTPLUG_CPU */
  454. }
  455. return NOTIFY_OK;
  456. }
  457. static struct notifier_block __cpuinitdata cpu_nfb = {
  458. .notifier_call = cpu_callback
  459. };
  460. static int __init spawn_watchdog_task(void)
  461. {
  462. void *cpu = (void *)(long)smp_processor_id();
  463. int err;
  464. if (no_watchdog)
  465. return 0;
  466. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  467. WARN_ON(err == NOTIFY_BAD);
  468. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  469. register_cpu_notifier(&cpu_nfb);
  470. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  471. return 0;
  472. }
  473. early_initcall(spawn_watchdog_task);