watchdog.c 15 KB

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