watchdog.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Watchdog support on powerpc systems.
  4. *
  5. * Copyright 2017, IBM Corporation.
  6. *
  7. * This uses code from arch/sparc/kernel/nmi.c and kernel/watchdog.c
  8. */
  9. #define pr_fmt(fmt) "watchdog: " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/param.h>
  12. #include <linux/init.h>
  13. #include <linux/percpu.h>
  14. #include <linux/cpu.h>
  15. #include <linux/nmi.h>
  16. #include <linux/module.h>
  17. #include <linux/export.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/reboot.h>
  21. #include <linux/slab.h>
  22. #include <linux/kdebug.h>
  23. #include <linux/sched/debug.h>
  24. #include <linux/delay.h>
  25. #include <linux/processor.h>
  26. #include <linux/smp.h>
  27. #include <asm/interrupt.h>
  28. #include <asm/paca.h>
  29. #include <asm/nmi.h>
  30. /*
  31. * The powerpc watchdog ensures that each CPU is able to service timers.
  32. * The watchdog sets up a simple timer on each CPU to run once per timer
  33. * period, and updates a per-cpu timestamp and a "pending" cpumask. This is
  34. * the heartbeat.
  35. *
  36. * Then there are two systems to check that the heartbeat is still running.
  37. * The local soft-NMI, and the SMP checker.
  38. *
  39. * The soft-NMI checker can detect lockups on the local CPU. When interrupts
  40. * are disabled with local_irq_disable(), platforms that use soft-masking
  41. * can leave hardware interrupts enabled and handle them with a masked
  42. * interrupt handler. The masked handler can send the timer interrupt to the
  43. * watchdog's soft_nmi_interrupt(), which appears to Linux as an NMI
  44. * interrupt, and can be used to detect CPUs stuck with IRQs disabled.
  45. *
  46. * The soft-NMI checker will compare the heartbeat timestamp for this CPU
  47. * with the current time, and take action if the difference exceeds the
  48. * watchdog threshold.
  49. *
  50. * The limitation of the soft-NMI watchdog is that it does not work when
  51. * interrupts are hard disabled or otherwise not being serviced. This is
  52. * solved by also having a SMP watchdog where all CPUs check all other
  53. * CPUs heartbeat.
  54. *
  55. * The SMP checker can detect lockups on other CPUs. A global "pending"
  56. * cpumask is kept, containing all CPUs which enable the watchdog. Each
  57. * CPU clears their pending bit in their heartbeat timer. When the bitmask
  58. * becomes empty, the last CPU to clear its pending bit updates a global
  59. * timestamp and refills the pending bitmask.
  60. *
  61. * In the heartbeat timer, if any CPU notices that the global timestamp has
  62. * not been updated for a period exceeding the watchdog threshold, then it
  63. * means the CPU(s) with their bit still set in the pending mask have had
  64. * their heartbeat stop, and action is taken.
  65. *
  66. * Some platforms implement true NMI IPIs, which can be used by the SMP
  67. * watchdog to detect an unresponsive CPU and pull it out of its stuck
  68. * state with the NMI IPI, to get crash/debug data from it. This way the
  69. * SMP watchdog can detect hardware interrupts off lockups.
  70. */
  71. static cpumask_t wd_cpus_enabled __read_mostly;
  72. static u64 wd_panic_timeout_tb __read_mostly; /* timebase ticks until panic */
  73. static u64 wd_smp_panic_timeout_tb __read_mostly; /* panic other CPUs */
  74. static u64 wd_timer_period_ms __read_mostly; /* interval between heartbeat */
  75. static DEFINE_PER_CPU(struct hrtimer, wd_hrtimer);
  76. static DEFINE_PER_CPU(u64, wd_timer_tb);
  77. /* SMP checker bits */
  78. static unsigned long __wd_smp_lock;
  79. static unsigned long __wd_reporting;
  80. static unsigned long __wd_nmi_output;
  81. static cpumask_t wd_smp_cpus_pending;
  82. static cpumask_t wd_smp_cpus_stuck;
  83. static u64 wd_smp_last_reset_tb;
  84. #ifdef CONFIG_PPC_PSERIES
  85. static u64 wd_timeout_pct;
  86. #endif
  87. /*
  88. * Try to take the exclusive watchdog action / NMI IPI / printing lock.
  89. * wd_smp_lock must be held. If this fails, we should return and wait
  90. * for the watchdog to kick in again (or another CPU to trigger it).
  91. *
  92. * Importantly, if hardlockup_panic is set, wd_try_report failure should
  93. * not delay the panic, because whichever other CPU is reporting will
  94. * call panic.
  95. */
  96. static bool wd_try_report(void)
  97. {
  98. if (__wd_reporting)
  99. return false;
  100. __wd_reporting = 1;
  101. return true;
  102. }
  103. /* End printing after successful wd_try_report. wd_smp_lock not required. */
  104. static void wd_end_reporting(void)
  105. {
  106. smp_mb(); /* End printing "critical section" */
  107. WARN_ON_ONCE(__wd_reporting == 0);
  108. WRITE_ONCE(__wd_reporting, 0);
  109. }
  110. static inline void wd_smp_lock(unsigned long *flags)
  111. {
  112. /*
  113. * Avoid locking layers if possible.
  114. * This may be called from low level interrupt handlers at some
  115. * point in future.
  116. */
  117. raw_local_irq_save(*flags);
  118. hard_irq_disable(); /* Make it soft-NMI safe */
  119. while (unlikely(test_and_set_bit_lock(0, &__wd_smp_lock))) {
  120. raw_local_irq_restore(*flags);
  121. spin_until_cond(!test_bit(0, &__wd_smp_lock));
  122. raw_local_irq_save(*flags);
  123. hard_irq_disable();
  124. }
  125. }
  126. static inline void wd_smp_unlock(unsigned long *flags)
  127. {
  128. clear_bit_unlock(0, &__wd_smp_lock);
  129. raw_local_irq_restore(*flags);
  130. }
  131. static void wd_lockup_ipi(struct pt_regs *regs)
  132. {
  133. int cpu = raw_smp_processor_id();
  134. u64 tb = get_tb();
  135. pr_emerg("CPU %d Hard LOCKUP\n", cpu);
  136. pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n",
  137. cpu, tb, per_cpu(wd_timer_tb, cpu),
  138. tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000);
  139. print_modules();
  140. print_irqtrace_events(current);
  141. if (regs)
  142. show_regs(regs);
  143. else
  144. dump_stack();
  145. /*
  146. * __wd_nmi_output must be set after we printk from NMI context.
  147. *
  148. * printk from NMI context defers printing to the console to irq_work.
  149. * If that NMI was taken in some code that is hard-locked, then irqs
  150. * are disabled so irq_work will never fire. That can result in the
  151. * hard lockup messages being delayed (indefinitely, until something
  152. * else kicks the console drivers).
  153. *
  154. * Setting __wd_nmi_output will cause another CPU to notice and kick
  155. * the console drivers for us.
  156. *
  157. * xchg is not needed here (it could be a smp_mb and store), but xchg
  158. * gives the memory ordering and atomicity required.
  159. */
  160. xchg(&__wd_nmi_output, 1);
  161. /* Do not panic from here because that can recurse into NMI IPI layer */
  162. }
  163. static bool set_cpu_stuck(int cpu)
  164. {
  165. cpumask_set_cpu(cpu, &wd_smp_cpus_stuck);
  166. cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
  167. /*
  168. * See wd_smp_clear_cpu_pending()
  169. */
  170. smp_mb();
  171. if (cpumask_empty(&wd_smp_cpus_pending)) {
  172. wd_smp_last_reset_tb = get_tb();
  173. cpumask_andnot(&wd_smp_cpus_pending,
  174. &wd_cpus_enabled,
  175. &wd_smp_cpus_stuck);
  176. return true;
  177. }
  178. return false;
  179. }
  180. static void watchdog_smp_panic(int cpu)
  181. {
  182. static cpumask_t wd_smp_cpus_ipi; // protected by reporting
  183. unsigned long flags;
  184. u64 tb, last_reset;
  185. int c;
  186. wd_smp_lock(&flags);
  187. /* Double check some things under lock */
  188. tb = get_tb();
  189. last_reset = wd_smp_last_reset_tb;
  190. if ((s64)(tb - last_reset) < (s64)wd_smp_panic_timeout_tb)
  191. goto out;
  192. if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
  193. goto out;
  194. if (!wd_try_report())
  195. goto out;
  196. for_each_online_cpu(c) {
  197. if (!cpumask_test_cpu(c, &wd_smp_cpus_pending))
  198. continue;
  199. if (c == cpu)
  200. continue; // should not happen
  201. __cpumask_set_cpu(c, &wd_smp_cpus_ipi);
  202. if (set_cpu_stuck(c))
  203. break;
  204. }
  205. if (cpumask_empty(&wd_smp_cpus_ipi)) {
  206. wd_end_reporting();
  207. goto out;
  208. }
  209. wd_smp_unlock(&flags);
  210. pr_emerg("CPU %d detected hard LOCKUP on other CPUs %*pbl\n",
  211. cpu, cpumask_pr_args(&wd_smp_cpus_ipi));
  212. pr_emerg("CPU %d TB:%lld, last SMP heartbeat TB:%lld (%lldms ago)\n",
  213. cpu, tb, last_reset, tb_to_ns(tb - last_reset) / 1000000);
  214. if (!sysctl_hardlockup_all_cpu_backtrace) {
  215. /*
  216. * Try to trigger the stuck CPUs, unless we are going to
  217. * get a backtrace on all of them anyway.
  218. */
  219. for_each_cpu(c, &wd_smp_cpus_ipi) {
  220. smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
  221. __cpumask_clear_cpu(c, &wd_smp_cpus_ipi);
  222. }
  223. } else {
  224. trigger_allbutself_cpu_backtrace();
  225. cpumask_clear(&wd_smp_cpus_ipi);
  226. }
  227. if (hardlockup_panic)
  228. nmi_panic(NULL, "Hard LOCKUP");
  229. wd_end_reporting();
  230. return;
  231. out:
  232. wd_smp_unlock(&flags);
  233. }
  234. static void wd_smp_clear_cpu_pending(int cpu)
  235. {
  236. if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
  237. if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
  238. struct pt_regs *regs = get_irq_regs();
  239. unsigned long flags;
  240. pr_emerg("CPU %d became unstuck TB:%lld\n",
  241. cpu, get_tb());
  242. print_irqtrace_events(current);
  243. if (regs)
  244. show_regs(regs);
  245. else
  246. dump_stack();
  247. wd_smp_lock(&flags);
  248. cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
  249. wd_smp_unlock(&flags);
  250. } else {
  251. /*
  252. * The last CPU to clear pending should have reset the
  253. * watchdog so we generally should not find it empty
  254. * here if our CPU was clear. However it could happen
  255. * due to a rare race with another CPU taking the
  256. * last CPU out of the mask concurrently.
  257. *
  258. * We can't add a warning for it. But just in case
  259. * there is a problem with the watchdog that is causing
  260. * the mask to not be reset, try to kick it along here.
  261. */
  262. if (unlikely(cpumask_empty(&wd_smp_cpus_pending)))
  263. goto none_pending;
  264. }
  265. return;
  266. }
  267. /*
  268. * All other updates to wd_smp_cpus_pending are performed under
  269. * wd_smp_lock. All of them are atomic except the case where the
  270. * mask becomes empty and is reset. This will not happen here because
  271. * cpu was tested to be in the bitmap (above), and a CPU only clears
  272. * its own bit. _Except_ in the case where another CPU has detected a
  273. * hard lockup on our CPU and takes us out of the pending mask. So in
  274. * normal operation there will be no race here, no problem.
  275. *
  276. * In the lockup case, this atomic clear-bit vs a store that refills
  277. * other bits in the accessed word wll not be a problem. The bit clear
  278. * is atomic so it will not cause the store to get lost, and the store
  279. * will never set this bit so it will not overwrite the bit clear. The
  280. * only way for a stuck CPU to return to the pending bitmap is to
  281. * become unstuck itself.
  282. */
  283. cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
  284. /*
  285. * Order the store to clear pending with the load(s) to check all
  286. * words in the pending mask to check they are all empty. This orders
  287. * with the same barrier on another CPU. This prevents two CPUs
  288. * clearing the last 2 pending bits, but neither seeing the other's
  289. * store when checking if the mask is empty, and missing an empty
  290. * mask, which ends with a false positive.
  291. */
  292. smp_mb();
  293. if (cpumask_empty(&wd_smp_cpus_pending)) {
  294. unsigned long flags;
  295. none_pending:
  296. /*
  297. * Double check under lock because more than one CPU could see
  298. * a clear mask with the lockless check after clearing their
  299. * pending bits.
  300. */
  301. wd_smp_lock(&flags);
  302. if (cpumask_empty(&wd_smp_cpus_pending)) {
  303. wd_smp_last_reset_tb = get_tb();
  304. cpumask_andnot(&wd_smp_cpus_pending,
  305. &wd_cpus_enabled,
  306. &wd_smp_cpus_stuck);
  307. }
  308. wd_smp_unlock(&flags);
  309. }
  310. }
  311. static void watchdog_timer_interrupt(int cpu)
  312. {
  313. u64 tb = get_tb();
  314. per_cpu(wd_timer_tb, cpu) = tb;
  315. wd_smp_clear_cpu_pending(cpu);
  316. if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
  317. watchdog_smp_panic(cpu);
  318. if (__wd_nmi_output && xchg(&__wd_nmi_output, 0)) {
  319. /*
  320. * Something has called printk from NMI context. It might be
  321. * stuck, so this triggers a flush that will get that
  322. * printk output to the console.
  323. *
  324. * See wd_lockup_ipi.
  325. */
  326. printk_trigger_flush();
  327. }
  328. }
  329. DEFINE_INTERRUPT_HANDLER_NMI(soft_nmi_interrupt)
  330. {
  331. unsigned long flags;
  332. int cpu = raw_smp_processor_id();
  333. u64 tb;
  334. /* should only arrive from kernel, with irqs disabled */
  335. WARN_ON_ONCE(!arch_irq_disabled_regs(regs));
  336. if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
  337. return 0;
  338. __this_cpu_inc(irq_stat.soft_nmi_irqs);
  339. tb = get_tb();
  340. if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
  341. /*
  342. * Taking wd_smp_lock here means it is a soft-NMI lock, which
  343. * means we can't take any regular or irqsafe spin locks while
  344. * holding this lock. This is why timers can't printk while
  345. * holding the lock.
  346. */
  347. wd_smp_lock(&flags);
  348. if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
  349. wd_smp_unlock(&flags);
  350. return 0;
  351. }
  352. if (!wd_try_report()) {
  353. wd_smp_unlock(&flags);
  354. /* Couldn't report, try again in 100ms */
  355. mtspr(SPRN_DEC, 100 * tb_ticks_per_usec * 1000);
  356. return 0;
  357. }
  358. set_cpu_stuck(cpu);
  359. wd_smp_unlock(&flags);
  360. pr_emerg("CPU %d self-detected hard LOCKUP @ %pS\n",
  361. cpu, (void *)regs->nip);
  362. pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n",
  363. cpu, tb, per_cpu(wd_timer_tb, cpu),
  364. tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000);
  365. print_modules();
  366. print_irqtrace_events(current);
  367. show_regs(regs);
  368. xchg(&__wd_nmi_output, 1); // see wd_lockup_ipi
  369. if (sysctl_hardlockup_all_cpu_backtrace)
  370. trigger_allbutself_cpu_backtrace();
  371. if (hardlockup_panic)
  372. nmi_panic(regs, "Hard LOCKUP");
  373. wd_end_reporting();
  374. }
  375. /*
  376. * We are okay to change DEC in soft_nmi_interrupt because the masked
  377. * handler has marked a DEC as pending, so the timer interrupt will be
  378. * replayed as soon as local irqs are enabled again.
  379. */
  380. if (wd_panic_timeout_tb < 0x7fffffff)
  381. mtspr(SPRN_DEC, wd_panic_timeout_tb);
  382. return 0;
  383. }
  384. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  385. {
  386. int cpu = smp_processor_id();
  387. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  388. return HRTIMER_NORESTART;
  389. if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
  390. return HRTIMER_NORESTART;
  391. watchdog_timer_interrupt(cpu);
  392. hrtimer_forward_now(hrtimer, ms_to_ktime(wd_timer_period_ms));
  393. return HRTIMER_RESTART;
  394. }
  395. void arch_touch_nmi_watchdog(void)
  396. {
  397. unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
  398. int cpu = smp_processor_id();
  399. u64 tb;
  400. if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
  401. return;
  402. tb = get_tb();
  403. if (tb - per_cpu(wd_timer_tb, cpu) >= ticks) {
  404. per_cpu(wd_timer_tb, cpu) = tb;
  405. wd_smp_clear_cpu_pending(cpu);
  406. }
  407. }
  408. EXPORT_SYMBOL(arch_touch_nmi_watchdog);
  409. static void start_watchdog(void *arg)
  410. {
  411. struct hrtimer *hrtimer = this_cpu_ptr(&wd_hrtimer);
  412. int cpu = smp_processor_id();
  413. unsigned long flags;
  414. if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
  415. WARN_ON(1);
  416. return;
  417. }
  418. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  419. return;
  420. if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
  421. return;
  422. wd_smp_lock(&flags);
  423. cpumask_set_cpu(cpu, &wd_cpus_enabled);
  424. if (cpumask_weight(&wd_cpus_enabled) == 1) {
  425. cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
  426. wd_smp_last_reset_tb = get_tb();
  427. }
  428. wd_smp_unlock(&flags);
  429. *this_cpu_ptr(&wd_timer_tb) = get_tb();
  430. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  431. hrtimer->function = watchdog_timer_fn;
  432. hrtimer_start(hrtimer, ms_to_ktime(wd_timer_period_ms),
  433. HRTIMER_MODE_REL_PINNED);
  434. }
  435. static int start_watchdog_on_cpu(unsigned int cpu)
  436. {
  437. return smp_call_function_single(cpu, start_watchdog, NULL, true);
  438. }
  439. static void stop_watchdog(void *arg)
  440. {
  441. struct hrtimer *hrtimer = this_cpu_ptr(&wd_hrtimer);
  442. int cpu = smp_processor_id();
  443. unsigned long flags;
  444. if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
  445. return; /* Can happen in CPU unplug case */
  446. hrtimer_cancel(hrtimer);
  447. wd_smp_lock(&flags);
  448. cpumask_clear_cpu(cpu, &wd_cpus_enabled);
  449. wd_smp_unlock(&flags);
  450. wd_smp_clear_cpu_pending(cpu);
  451. }
  452. static int stop_watchdog_on_cpu(unsigned int cpu)
  453. {
  454. return smp_call_function_single(cpu, stop_watchdog, NULL, true);
  455. }
  456. static void watchdog_calc_timeouts(void)
  457. {
  458. u64 threshold = watchdog_thresh;
  459. #ifdef CONFIG_PPC_PSERIES
  460. threshold += (READ_ONCE(wd_timeout_pct) * threshold) / 100;
  461. #endif
  462. wd_panic_timeout_tb = threshold * ppc_tb_freq;
  463. /* Have the SMP detector trigger a bit later */
  464. wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
  465. /* 2/5 is the factor that the perf based detector uses */
  466. wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
  467. }
  468. void watchdog_nmi_stop(void)
  469. {
  470. int cpu;
  471. for_each_cpu(cpu, &wd_cpus_enabled)
  472. stop_watchdog_on_cpu(cpu);
  473. }
  474. void watchdog_nmi_start(void)
  475. {
  476. int cpu;
  477. watchdog_calc_timeouts();
  478. for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
  479. start_watchdog_on_cpu(cpu);
  480. }
  481. /*
  482. * Invoked from core watchdog init.
  483. */
  484. int __init watchdog_nmi_probe(void)
  485. {
  486. int err;
  487. err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  488. "powerpc/watchdog:online",
  489. start_watchdog_on_cpu,
  490. stop_watchdog_on_cpu);
  491. if (err < 0) {
  492. pr_warn("could not be initialized");
  493. return err;
  494. }
  495. return 0;
  496. }
  497. #ifdef CONFIG_PPC_PSERIES
  498. void watchdog_nmi_set_timeout_pct(u64 pct)
  499. {
  500. pr_info("Set the NMI watchdog timeout factor to %llu%%\n", pct);
  501. WRITE_ONCE(wd_timeout_pct, pct);
  502. lockup_detector_reconfigure();
  503. }
  504. #endif