watchdog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 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. else if (!strncmp(str, "0", 1))
  53. no_watchdog = 1;
  54. return 1;
  55. }
  56. __setup("nmi_watchdog=", hardlockup_panic_setup);
  57. #endif
  58. unsigned int __read_mostly softlockup_panic =
  59. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  60. static int __init softlockup_panic_setup(char *str)
  61. {
  62. softlockup_panic = simple_strtoul(str, NULL, 0);
  63. return 1;
  64. }
  65. __setup("softlockup_panic=", softlockup_panic_setup);
  66. static int __init nowatchdog_setup(char *str)
  67. {
  68. no_watchdog = 1;
  69. return 1;
  70. }
  71. __setup("nowatchdog", nowatchdog_setup);
  72. /* deprecated */
  73. static int __init nosoftlockup_setup(char *str)
  74. {
  75. no_watchdog = 1;
  76. return 1;
  77. }
  78. __setup("nosoftlockup", nosoftlockup_setup);
  79. /* */
  80. /*
  81. * Returns seconds, approximately. We don't need nanosecond
  82. * resolution, and we don't need to waste time with a big divide when
  83. * 2^30ns == 1.074s.
  84. */
  85. static unsigned long get_timestamp(int this_cpu)
  86. {
  87. return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
  88. }
  89. static unsigned long get_sample_period(void)
  90. {
  91. /*
  92. * convert softlockup_thresh from seconds to ns
  93. * the divide by 5 is to give hrtimer 5 chances to
  94. * increment before the hardlockup detector generates
  95. * a warning
  96. */
  97. return softlockup_thresh / 5 * NSEC_PER_SEC;
  98. }
  99. /* Commands for resetting the watchdog */
  100. static void __touch_watchdog(void)
  101. {
  102. int this_cpu = smp_processor_id();
  103. __this_cpu_write(watchdog_touch_ts, get_timestamp(this_cpu));
  104. }
  105. void touch_softlockup_watchdog(void)
  106. {
  107. __this_cpu_write(watchdog_touch_ts, 0);
  108. }
  109. EXPORT_SYMBOL(touch_softlockup_watchdog);
  110. void touch_all_softlockup_watchdogs(void)
  111. {
  112. int cpu;
  113. /*
  114. * this is done lockless
  115. * do we care if a 0 races with a timestamp?
  116. * all it means is the softlock check starts one cycle later
  117. */
  118. for_each_online_cpu(cpu)
  119. per_cpu(watchdog_touch_ts, cpu) = 0;
  120. }
  121. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  122. void touch_nmi_watchdog(void)
  123. {
  124. if (watchdog_enabled) {
  125. unsigned cpu;
  126. for_each_present_cpu(cpu) {
  127. if (per_cpu(watchdog_nmi_touch, cpu) != true)
  128. per_cpu(watchdog_nmi_touch, cpu) = true;
  129. }
  130. }
  131. touch_softlockup_watchdog();
  132. }
  133. EXPORT_SYMBOL(touch_nmi_watchdog);
  134. #endif
  135. void touch_softlockup_watchdog_sync(void)
  136. {
  137. __raw_get_cpu_var(softlockup_touch_sync) = true;
  138. __raw_get_cpu_var(watchdog_touch_ts) = 0;
  139. }
  140. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  141. /* watchdog detector functions */
  142. static int is_hardlockup(void)
  143. {
  144. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  145. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  146. return 1;
  147. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  148. return 0;
  149. }
  150. #endif
  151. static int is_softlockup(unsigned long touch_ts)
  152. {
  153. unsigned long now = get_timestamp(smp_processor_id());
  154. /* Warn about unreasonable delays: */
  155. if (time_after(now, touch_ts + softlockup_thresh))
  156. return now - touch_ts;
  157. return 0;
  158. }
  159. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  160. static struct perf_event_attr wd_hw_attr = {
  161. .type = PERF_TYPE_HARDWARE,
  162. .config = PERF_COUNT_HW_CPU_CYCLES,
  163. .size = sizeof(struct perf_event_attr),
  164. .pinned = 1,
  165. .disabled = 1,
  166. };
  167. /* Callback function for perf event subsystem */
  168. static void watchdog_overflow_callback(struct perf_event *event, int nmi,
  169. struct perf_sample_data *data,
  170. struct pt_regs *regs)
  171. {
  172. /* Ensure the watchdog never gets throttled */
  173. event->hw.interrupts = 0;
  174. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  175. __this_cpu_write(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 (__this_cpu_read(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. __this_cpu_write(hard_watchdog_warn, true);
  194. return;
  195. }
  196. __this_cpu_write(hard_watchdog_warn, false);
  197. return;
  198. }
  199. static void watchdog_interrupt_count(void)
  200. {
  201. __this_cpu_inc(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 = __this_cpu_read(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(__this_cpu_read(softlockup_watchdog));
  216. /* .. and repeat */
  217. hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
  218. if (touch_ts == 0) {
  219. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  220. /*
  221. * If the time stamp was touched atomically
  222. * make sure the scheduler tick is up to date.
  223. */
  224. __this_cpu_write(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 (__this_cpu_read(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. __this_cpu_write(soft_watchdog_warn, true);
  253. } else
  254. __this_cpu_write(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. static 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, NULL, 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 disabled for cpu%i: unable to create perf event: %ld\n",
  307. cpu, PTR_ERR(event));
  308. return PTR_ERR(event);
  309. /* success path */
  310. out_save:
  311. per_cpu(watchdog_ev, cpu) = event;
  312. out_enable:
  313. perf_event_enable(per_cpu(watchdog_ev, cpu));
  314. out:
  315. return 0;
  316. }
  317. static void watchdog_nmi_disable(int cpu)
  318. {
  319. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  320. if (event) {
  321. perf_event_disable(event);
  322. per_cpu(watchdog_ev, cpu) = NULL;
  323. /* should be in cleanup, but blocks oprofile */
  324. perf_event_release_kernel(event);
  325. }
  326. return;
  327. }
  328. #else
  329. static int watchdog_nmi_enable(int cpu) { return 0; }
  330. static void watchdog_nmi_disable(int cpu) { return; }
  331. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  332. /* prepare/enable/disable routines */
  333. static int watchdog_prepare_cpu(int cpu)
  334. {
  335. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  336. WARN_ON(per_cpu(softlockup_watchdog, cpu));
  337. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  338. hrtimer->function = watchdog_timer_fn;
  339. return 0;
  340. }
  341. static int watchdog_enable(int cpu)
  342. {
  343. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  344. int err;
  345. /* enable the perf event */
  346. err = watchdog_nmi_enable(cpu);
  347. if (err)
  348. return err;
  349. /* create the watchdog thread */
  350. if (!p) {
  351. p = kthread_create(watchdog, (void *)(unsigned long)cpu, "watchdog/%d", cpu);
  352. if (IS_ERR(p)) {
  353. printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);
  354. return PTR_ERR(p);
  355. }
  356. kthread_bind(p, cpu);
  357. per_cpu(watchdog_touch_ts, cpu) = 0;
  358. per_cpu(softlockup_watchdog, cpu) = p;
  359. wake_up_process(p);
  360. }
  361. /* if any cpu succeeds, watchdog is considered enabled for the system */
  362. watchdog_enabled = 1;
  363. return 0;
  364. }
  365. static void watchdog_disable(int cpu)
  366. {
  367. struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
  368. struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
  369. /*
  370. * cancel the timer first to stop incrementing the stats
  371. * and waking up the kthread
  372. */
  373. hrtimer_cancel(hrtimer);
  374. /* disable the perf event */
  375. watchdog_nmi_disable(cpu);
  376. /* stop the watchdog thread */
  377. if (p) {
  378. per_cpu(softlockup_watchdog, cpu) = NULL;
  379. kthread_stop(p);
  380. }
  381. }
  382. static void watchdog_enable_all_cpus(void)
  383. {
  384. int cpu;
  385. int result = 0;
  386. for_each_online_cpu(cpu)
  387. result += watchdog_enable(cpu);
  388. if (result)
  389. printk(KERN_ERR "watchdog: failed to be enabled on some cpus\n");
  390. }
  391. static void watchdog_disable_all_cpus(void)
  392. {
  393. int cpu;
  394. if (no_watchdog)
  395. return;
  396. for_each_online_cpu(cpu)
  397. watchdog_disable(cpu);
  398. /* if all watchdogs are disabled, then they are disabled for the system */
  399. watchdog_enabled = 0;
  400. }
  401. /* sysctl functions */
  402. #ifdef CONFIG_SYSCTL
  403. /*
  404. * proc handler for /proc/sys/kernel/nmi_watchdog
  405. */
  406. int proc_dowatchdog_enabled(struct ctl_table *table, int write,
  407. void __user *buffer, size_t *length, loff_t *ppos)
  408. {
  409. proc_dointvec(table, write, buffer, length, ppos);
  410. if (watchdog_enabled)
  411. watchdog_enable_all_cpus();
  412. else
  413. watchdog_disable_all_cpus();
  414. return 0;
  415. }
  416. int proc_dowatchdog_thresh(struct ctl_table *table, int write,
  417. void __user *buffer,
  418. size_t *lenp, loff_t *ppos)
  419. {
  420. return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  421. }
  422. #endif /* CONFIG_SYSCTL */
  423. /*
  424. * Create/destroy watchdog threads as CPUs come and go:
  425. */
  426. static int __cpuinit
  427. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  428. {
  429. int hotcpu = (unsigned long)hcpu;
  430. int err = 0;
  431. switch (action) {
  432. case CPU_UP_PREPARE:
  433. case CPU_UP_PREPARE_FROZEN:
  434. err = watchdog_prepare_cpu(hotcpu);
  435. break;
  436. case CPU_ONLINE:
  437. case CPU_ONLINE_FROZEN:
  438. err = watchdog_enable(hotcpu);
  439. break;
  440. #ifdef CONFIG_HOTPLUG_CPU
  441. case CPU_UP_CANCELED:
  442. case CPU_UP_CANCELED_FROZEN:
  443. watchdog_disable(hotcpu);
  444. break;
  445. case CPU_DEAD:
  446. case CPU_DEAD_FROZEN:
  447. watchdog_disable(hotcpu);
  448. break;
  449. #endif /* CONFIG_HOTPLUG_CPU */
  450. }
  451. return notifier_from_errno(err);
  452. }
  453. static struct notifier_block __cpuinitdata cpu_nfb = {
  454. .notifier_call = cpu_callback
  455. };
  456. void __init lockup_detector_init(void)
  457. {
  458. void *cpu = (void *)(long)smp_processor_id();
  459. int err;
  460. if (no_watchdog)
  461. return;
  462. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  463. WARN_ON(notifier_to_errno(err));
  464. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  465. register_cpu_notifier(&cpu_nfb);
  466. return;
  467. }