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