watchdog.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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/module.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/smpboot.h>
  19. #include <linux/sched/rt.h>
  20. #include <asm/irq_regs.h>
  21. #include <linux/kvm_para.h>
  22. #include <linux/perf_event.h>
  23. int watchdog_user_enabled = 1;
  24. int __read_mostly watchdog_thresh = 10;
  25. #ifdef CONFIG_SMP
  26. int __read_mostly sysctl_softlockup_all_cpu_backtrace;
  27. #else
  28. #define sysctl_softlockup_all_cpu_backtrace 0
  29. #endif
  30. static int __read_mostly watchdog_running;
  31. static u64 __read_mostly sample_period;
  32. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  33. static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
  34. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  35. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  36. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  37. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  38. static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
  39. static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
  40. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  41. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  42. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  43. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  44. #endif
  45. #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
  46. static cpumask_t __read_mostly watchdog_cpus;
  47. #endif
  48. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  49. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  50. #endif
  51. static unsigned long soft_lockup_nmi_warn;
  52. /* boot commands */
  53. /*
  54. * Should we panic when a soft-lockup or hard-lockup occurs:
  55. */
  56. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  57. static int hardlockup_panic =
  58. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  59. static bool hardlockup_detector_enabled = true;
  60. /*
  61. * We may not want to enable hard lockup detection by default in all cases,
  62. * for example when running the kernel as a guest on a hypervisor. In these
  63. * cases this function can be called to disable hard lockup detection. This
  64. * function should only be executed once by the boot processor before the
  65. * kernel command line parameters are parsed, because otherwise it is not
  66. * possible to override this in hardlockup_panic_setup().
  67. */
  68. void watchdog_enable_hardlockup_detector(bool val)
  69. {
  70. hardlockup_detector_enabled = val;
  71. }
  72. bool watchdog_hardlockup_detector_is_enabled(void)
  73. {
  74. return hardlockup_detector_enabled;
  75. }
  76. static int __init hardlockup_panic_setup(char *str)
  77. {
  78. if (!strncmp(str, "panic", 5))
  79. hardlockup_panic = 1;
  80. else if (!strncmp(str, "nopanic", 7))
  81. hardlockup_panic = 0;
  82. else if (!strncmp(str, "0", 1))
  83. watchdog_user_enabled = 0;
  84. else if (!strncmp(str, "1", 1) || !strncmp(str, "2", 1)) {
  85. /*
  86. * Setting 'nmi_watchdog=1' or 'nmi_watchdog=2' (legacy option)
  87. * has the same effect.
  88. */
  89. watchdog_user_enabled = 1;
  90. watchdog_enable_hardlockup_detector(true);
  91. }
  92. return 1;
  93. }
  94. __setup("nmi_watchdog=", hardlockup_panic_setup);
  95. #endif
  96. unsigned int __read_mostly softlockup_panic =
  97. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  98. static int __init softlockup_panic_setup(char *str)
  99. {
  100. softlockup_panic = simple_strtoul(str, NULL, 0);
  101. return 1;
  102. }
  103. __setup("softlockup_panic=", softlockup_panic_setup);
  104. static int __init nowatchdog_setup(char *str)
  105. {
  106. watchdog_user_enabled = 0;
  107. return 1;
  108. }
  109. __setup("nowatchdog", nowatchdog_setup);
  110. /* deprecated */
  111. static int __init nosoftlockup_setup(char *str)
  112. {
  113. watchdog_user_enabled = 0;
  114. return 1;
  115. }
  116. __setup("nosoftlockup", nosoftlockup_setup);
  117. /* */
  118. #ifdef CONFIG_SMP
  119. static int __init softlockup_all_cpu_backtrace_setup(char *str)
  120. {
  121. sysctl_softlockup_all_cpu_backtrace =
  122. !!simple_strtol(str, NULL, 0);
  123. return 1;
  124. }
  125. __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
  126. #endif
  127. /*
  128. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  129. * lockups can have false positives under extreme conditions. So we generally
  130. * want a higher threshold for soft lockups than for hard lockups. So we couple
  131. * the thresholds with a factor: we make the soft threshold twice the amount of
  132. * time the hard threshold is.
  133. */
  134. static int get_softlockup_thresh(void)
  135. {
  136. return watchdog_thresh * 2;
  137. }
  138. /*
  139. * Returns seconds, approximately. We don't need nanosecond
  140. * resolution, and we don't need to waste time with a big divide when
  141. * 2^30ns == 1.074s.
  142. */
  143. static unsigned long get_timestamp(void)
  144. {
  145. return local_clock() >> 30LL; /* 2^30 ~= 10^9 */
  146. }
  147. static void set_sample_period(void)
  148. {
  149. /*
  150. * convert watchdog_thresh from seconds to ns
  151. * the divide by 5 is to give hrtimer several chances (two
  152. * or three with the current relation between the soft
  153. * and hard thresholds) to increment before the
  154. * hardlockup detector generates a warning
  155. */
  156. sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
  157. }
  158. /* Commands for resetting the watchdog */
  159. static void __touch_watchdog(void)
  160. {
  161. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  162. }
  163. void touch_softlockup_watchdog(void)
  164. {
  165. /*
  166. * Preemption can be enabled. It doesn't matter which CPU's timestamp
  167. * gets zeroed here, so use the raw_ operation.
  168. */
  169. raw_cpu_write(watchdog_touch_ts, 0);
  170. }
  171. EXPORT_SYMBOL(touch_softlockup_watchdog);
  172. void touch_all_softlockup_watchdogs(void)
  173. {
  174. int cpu;
  175. /*
  176. * this is done lockless
  177. * do we care if a 0 races with a timestamp?
  178. * all it means is the softlock check starts one cycle later
  179. */
  180. for_each_online_cpu(cpu)
  181. per_cpu(watchdog_touch_ts, cpu) = 0;
  182. }
  183. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  184. void touch_nmi_watchdog(void)
  185. {
  186. /*
  187. * Using __raw here because some code paths have
  188. * preemption enabled. If preemption is enabled
  189. * then interrupts should be enabled too, in which
  190. * case we shouldn't have to worry about the watchdog
  191. * going off.
  192. */
  193. raw_cpu_write(watchdog_nmi_touch, true);
  194. touch_softlockup_watchdog();
  195. }
  196. EXPORT_SYMBOL(touch_nmi_watchdog);
  197. #endif
  198. void touch_softlockup_watchdog_sync(void)
  199. {
  200. __this_cpu_write(softlockup_touch_sync, true);
  201. __this_cpu_write(watchdog_touch_ts, 0);
  202. }
  203. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  204. /* watchdog detector functions */
  205. static int is_hardlockup(void)
  206. {
  207. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  208. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  209. return 1;
  210. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  211. return 0;
  212. }
  213. #endif
  214. #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
  215. static unsigned int watchdog_next_cpu(unsigned int cpu)
  216. {
  217. cpumask_t cpus = watchdog_cpus;
  218. unsigned int next_cpu;
  219. next_cpu = cpumask_next(cpu, &cpus);
  220. if (next_cpu >= nr_cpu_ids)
  221. next_cpu = cpumask_first(&cpus);
  222. if (next_cpu == cpu)
  223. return nr_cpu_ids;
  224. return next_cpu;
  225. }
  226. static int is_hardlockup_other_cpu(unsigned int cpu)
  227. {
  228. unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
  229. if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
  230. return 1;
  231. per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
  232. return 0;
  233. }
  234. static void watchdog_check_hardlockup_other_cpu(void)
  235. {
  236. unsigned int next_cpu;
  237. /*
  238. * Test for hardlockups every 3 samples. The sample period is
  239. * watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
  240. * watchdog_thresh (over by 20%).
  241. */
  242. if (__this_cpu_read(hrtimer_interrupts) % 3 != 0)
  243. return;
  244. /* check for a hardlockup on the next cpu */
  245. next_cpu = watchdog_next_cpu(smp_processor_id());
  246. if (next_cpu >= nr_cpu_ids)
  247. return;
  248. smp_rmb();
  249. if (per_cpu(watchdog_nmi_touch, next_cpu) == true) {
  250. per_cpu(watchdog_nmi_touch, next_cpu) = false;
  251. return;
  252. }
  253. if (is_hardlockup_other_cpu(next_cpu)) {
  254. /* only warn once */
  255. if (per_cpu(hard_watchdog_warn, next_cpu) == true)
  256. return;
  257. if (hardlockup_panic)
  258. panic("Watchdog detected hard LOCKUP on cpu %u", next_cpu);
  259. else
  260. WARN(1, "Watchdog detected hard LOCKUP on cpu %u", next_cpu);
  261. per_cpu(hard_watchdog_warn, next_cpu) = true;
  262. } else {
  263. per_cpu(hard_watchdog_warn, next_cpu) = false;
  264. }
  265. }
  266. #else
  267. static inline void watchdog_check_hardlockup_other_cpu(void) { return; }
  268. #endif
  269. static int is_softlockup(unsigned long touch_ts)
  270. {
  271. unsigned long now = get_timestamp();
  272. /* Warn about unreasonable delays: */
  273. if (time_after(now, touch_ts + get_softlockup_thresh()))
  274. return now - touch_ts;
  275. return 0;
  276. }
  277. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  278. static struct perf_event_attr wd_hw_attr = {
  279. .type = PERF_TYPE_HARDWARE,
  280. .config = PERF_COUNT_HW_CPU_CYCLES,
  281. .size = sizeof(struct perf_event_attr),
  282. .pinned = 1,
  283. .disabled = 1,
  284. };
  285. /* Callback function for perf event subsystem */
  286. static void watchdog_overflow_callback(struct perf_event *event,
  287. struct perf_sample_data *data,
  288. struct pt_regs *regs)
  289. {
  290. /* Ensure the watchdog never gets throttled */
  291. event->hw.interrupts = 0;
  292. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  293. __this_cpu_write(watchdog_nmi_touch, false);
  294. return;
  295. }
  296. /* check for a hardlockup
  297. * This is done by making sure our timer interrupt
  298. * is incrementing. The timer interrupt should have
  299. * fired multiple times before we overflow'd. If it hasn't
  300. * then this is a good indication the cpu is stuck
  301. */
  302. if (is_hardlockup()) {
  303. int this_cpu = smp_processor_id();
  304. /* only print hardlockups once */
  305. if (__this_cpu_read(hard_watchdog_warn) == true)
  306. return;
  307. if (hardlockup_panic)
  308. panic("Watchdog detected hard LOCKUP on cpu %d",
  309. this_cpu);
  310. else
  311. WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
  312. this_cpu);
  313. __this_cpu_write(hard_watchdog_warn, true);
  314. return;
  315. }
  316. __this_cpu_write(hard_watchdog_warn, false);
  317. return;
  318. }
  319. #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
  320. static void watchdog_interrupt_count(void)
  321. {
  322. __this_cpu_inc(hrtimer_interrupts);
  323. }
  324. static int watchdog_nmi_enable(unsigned int cpu);
  325. static void watchdog_nmi_disable(unsigned int cpu);
  326. /* watchdog kicker functions */
  327. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  328. {
  329. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  330. struct pt_regs *regs = get_irq_regs();
  331. int duration;
  332. int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
  333. /* kick the hardlockup detector */
  334. watchdog_interrupt_count();
  335. /* test for hardlockups on the next cpu */
  336. watchdog_check_hardlockup_other_cpu();
  337. /* kick the softlockup detector */
  338. wake_up_process(__this_cpu_read(softlockup_watchdog));
  339. /* .. and repeat */
  340. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  341. if (touch_ts == 0) {
  342. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  343. /*
  344. * If the time stamp was touched atomically
  345. * make sure the scheduler tick is up to date.
  346. */
  347. __this_cpu_write(softlockup_touch_sync, false);
  348. sched_clock_tick();
  349. }
  350. /* Clear the guest paused flag on watchdog reset */
  351. kvm_check_and_clear_guest_paused();
  352. __touch_watchdog();
  353. return HRTIMER_RESTART;
  354. }
  355. /* check for a softlockup
  356. * This is done by making sure a high priority task is
  357. * being scheduled. The task touches the watchdog to
  358. * indicate it is getting cpu time. If it hasn't then
  359. * this is a good indication some task is hogging the cpu
  360. */
  361. duration = is_softlockup(touch_ts);
  362. if (unlikely(duration)) {
  363. /*
  364. * If a virtual machine is stopped by the host it can look to
  365. * the watchdog like a soft lockup, check to see if the host
  366. * stopped the vm before we issue the warning
  367. */
  368. if (kvm_check_and_clear_guest_paused())
  369. return HRTIMER_RESTART;
  370. /* only warn once */
  371. if (__this_cpu_read(soft_watchdog_warn) == true) {
  372. /*
  373. * When multiple processes are causing softlockups the
  374. * softlockup detector only warns on the first one
  375. * because the code relies on a full quiet cycle to
  376. * re-arm. The second process prevents the quiet cycle
  377. * and never gets reported. Use task pointers to detect
  378. * this.
  379. */
  380. if (__this_cpu_read(softlockup_task_ptr_saved) !=
  381. current) {
  382. __this_cpu_write(soft_watchdog_warn, false);
  383. __touch_watchdog();
  384. }
  385. return HRTIMER_RESTART;
  386. }
  387. if (softlockup_all_cpu_backtrace) {
  388. /* Prevent multiple soft-lockup reports if one cpu is already
  389. * engaged in dumping cpu back traces
  390. */
  391. if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
  392. /* Someone else will report us. Let's give up */
  393. __this_cpu_write(soft_watchdog_warn, true);
  394. return HRTIMER_RESTART;
  395. }
  396. }
  397. pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  398. smp_processor_id(), duration,
  399. current->comm, task_pid_nr(current));
  400. __this_cpu_write(softlockup_task_ptr_saved, current);
  401. print_modules();
  402. print_irqtrace_events(current);
  403. if (regs)
  404. show_regs(regs);
  405. else
  406. dump_stack();
  407. if (softlockup_all_cpu_backtrace) {
  408. /* Avoid generating two back traces for current
  409. * given that one is already made above
  410. */
  411. trigger_allbutself_cpu_backtrace();
  412. clear_bit(0, &soft_lockup_nmi_warn);
  413. /* Barrier to sync with other cpus */
  414. smp_mb__after_atomic();
  415. }
  416. add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
  417. if (softlockup_panic)
  418. panic("softlockup: hung tasks");
  419. __this_cpu_write(soft_watchdog_warn, true);
  420. } else
  421. __this_cpu_write(soft_watchdog_warn, false);
  422. return HRTIMER_RESTART;
  423. }
  424. static void watchdog_set_prio(unsigned int policy, unsigned int prio)
  425. {
  426. struct sched_param param = { .sched_priority = prio };
  427. sched_setscheduler(current, policy, &param);
  428. }
  429. static void watchdog_enable(unsigned int cpu)
  430. {
  431. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  432. /* kick off the timer for the hardlockup detector */
  433. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  434. hrtimer->function = watchdog_timer_fn;
  435. /* Enable the perf event */
  436. watchdog_nmi_enable(cpu);
  437. /* done here because hrtimer_start can only pin to smp_processor_id() */
  438. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  439. HRTIMER_MODE_REL_PINNED);
  440. /* initialize timestamp */
  441. watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
  442. __touch_watchdog();
  443. }
  444. static void watchdog_disable(unsigned int cpu)
  445. {
  446. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  447. watchdog_set_prio(SCHED_NORMAL, 0);
  448. hrtimer_cancel(hrtimer);
  449. /* disable the perf event */
  450. watchdog_nmi_disable(cpu);
  451. }
  452. static void watchdog_cleanup(unsigned int cpu, bool online)
  453. {
  454. watchdog_disable(cpu);
  455. }
  456. static int watchdog_should_run(unsigned int cpu)
  457. {
  458. return __this_cpu_read(hrtimer_interrupts) !=
  459. __this_cpu_read(soft_lockup_hrtimer_cnt);
  460. }
  461. /*
  462. * The watchdog thread function - touches the timestamp.
  463. *
  464. * It only runs once every sample_period seconds (4 seconds by
  465. * default) to reset the softlockup timestamp. If this gets delayed
  466. * for more than 2*watchdog_thresh seconds then the debug-printout
  467. * triggers in watchdog_timer_fn().
  468. */
  469. static void watchdog(unsigned int cpu)
  470. {
  471. __this_cpu_write(soft_lockup_hrtimer_cnt,
  472. __this_cpu_read(hrtimer_interrupts));
  473. __touch_watchdog();
  474. }
  475. #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
  476. /*
  477. * People like the simple clean cpu node info on boot.
  478. * Reduce the watchdog noise by only printing messages
  479. * that are different from what cpu0 displayed.
  480. */
  481. static unsigned long cpu0_err;
  482. static int watchdog_nmi_enable(unsigned int cpu)
  483. {
  484. struct perf_event_attr *wd_attr;
  485. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  486. /*
  487. * Some kernels need to default hard lockup detection to
  488. * 'disabled', for example a guest on a hypervisor.
  489. */
  490. if (!watchdog_hardlockup_detector_is_enabled()) {
  491. event = ERR_PTR(-ENOENT);
  492. goto handle_err;
  493. }
  494. /* is it already setup and enabled? */
  495. if (event && event->state > PERF_EVENT_STATE_OFF)
  496. goto out;
  497. /* it is setup but not enabled */
  498. if (event != NULL)
  499. goto out_enable;
  500. wd_attr = &wd_hw_attr;
  501. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  502. /* Try to register using hardware perf events */
  503. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  504. handle_err:
  505. /* save cpu0 error for future comparision */
  506. if (cpu == 0 && IS_ERR(event))
  507. cpu0_err = PTR_ERR(event);
  508. if (!IS_ERR(event)) {
  509. /* only print for cpu0 or different than cpu0 */
  510. if (cpu == 0 || cpu0_err)
  511. pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
  512. goto out_save;
  513. }
  514. /* skip displaying the same error again */
  515. if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
  516. return PTR_ERR(event);
  517. /* vary the KERN level based on the returned errno */
  518. if (PTR_ERR(event) == -EOPNOTSUPP)
  519. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  520. else if (PTR_ERR(event) == -ENOENT)
  521. pr_warn("disabled (cpu%i): hardware events not enabled\n",
  522. cpu);
  523. else
  524. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  525. cpu, PTR_ERR(event));
  526. return PTR_ERR(event);
  527. /* success path */
  528. out_save:
  529. per_cpu(watchdog_ev, cpu) = event;
  530. out_enable:
  531. perf_event_enable(per_cpu(watchdog_ev, cpu));
  532. out:
  533. return 0;
  534. }
  535. static void watchdog_nmi_disable(unsigned int cpu)
  536. {
  537. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  538. if (event) {
  539. perf_event_disable(event);
  540. per_cpu(watchdog_ev, cpu) = NULL;
  541. /* should be in cleanup, but blocks oprofile */
  542. perf_event_release_kernel(event);
  543. }
  544. if (cpu == 0) {
  545. /* watchdog_nmi_enable() expects this to be zero initially. */
  546. cpu0_err = 0;
  547. }
  548. }
  549. #else
  550. #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
  551. static int watchdog_nmi_enable(unsigned int cpu)
  552. {
  553. /*
  554. * The new cpu will be marked online before the first hrtimer interrupt
  555. * runs on it. If another cpu tests for a hardlockup on the new cpu
  556. * before it has run its first hrtimer, it will get a false positive.
  557. * Touch the watchdog on the new cpu to delay the first check for at
  558. * least 3 sampling periods to guarantee one hrtimer has run on the new
  559. * cpu.
  560. */
  561. per_cpu(watchdog_nmi_touch, cpu) = true;
  562. smp_wmb();
  563. cpumask_set_cpu(cpu, &watchdog_cpus);
  564. return 0;
  565. }
  566. static void watchdog_nmi_disable(unsigned int cpu)
  567. {
  568. unsigned int next_cpu = watchdog_next_cpu(cpu);
  569. /*
  570. * Offlining this cpu will cause the cpu before this one to start
  571. * checking the one after this one. If this cpu just finished checking
  572. * the next cpu and updating hrtimer_interrupts_saved, and then the
  573. * previous cpu checks it within one sample period, it will trigger a
  574. * false positive. Touch the watchdog on the next cpu to prevent it.
  575. */
  576. if (next_cpu < nr_cpu_ids)
  577. per_cpu(watchdog_nmi_touch, next_cpu) = true;
  578. smp_wmb();
  579. cpumask_clear_cpu(cpu, &watchdog_cpus);
  580. }
  581. #else
  582. static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
  583. static void watchdog_nmi_disable(unsigned int cpu) { return; }
  584. #endif /* CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU */
  585. #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
  586. static struct smp_hotplug_thread watchdog_threads = {
  587. .store = &softlockup_watchdog,
  588. .thread_should_run = watchdog_should_run,
  589. .thread_fn = watchdog,
  590. .thread_comm = "watchdog/%u",
  591. .setup = watchdog_enable,
  592. .cleanup = watchdog_cleanup,
  593. .park = watchdog_disable,
  594. .unpark = watchdog_enable,
  595. };
  596. static void restart_watchdog_hrtimer(void *info)
  597. {
  598. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  599. int ret;
  600. /*
  601. * No need to cancel and restart hrtimer if it is currently executing
  602. * because it will reprogram itself with the new period now.
  603. * We should never see it unqueued here because we are running per-cpu
  604. * with interrupts disabled.
  605. */
  606. ret = hrtimer_try_to_cancel(hrtimer);
  607. if (ret == 1)
  608. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  609. HRTIMER_MODE_REL_PINNED);
  610. }
  611. static void update_timers(int cpu)
  612. {
  613. /*
  614. * Make sure that perf event counter will adopt to a new
  615. * sampling period. Updating the sampling period directly would
  616. * be much nicer but we do not have an API for that now so
  617. * let's use a big hammer.
  618. * Hrtimer will adopt the new period on the next tick but this
  619. * might be late already so we have to restart the timer as well.
  620. */
  621. watchdog_nmi_disable(cpu);
  622. smp_call_function_single(cpu, restart_watchdog_hrtimer, NULL, 1);
  623. watchdog_nmi_enable(cpu);
  624. }
  625. static void update_timers_all_cpus(void)
  626. {
  627. int cpu;
  628. get_online_cpus();
  629. for_each_online_cpu(cpu)
  630. update_timers(cpu);
  631. put_online_cpus();
  632. }
  633. static int watchdog_enable_all_cpus(bool sample_period_changed)
  634. {
  635. int err = 0;
  636. if (!watchdog_running) {
  637. err = smpboot_register_percpu_thread(&watchdog_threads);
  638. if (err)
  639. pr_err("Failed to create watchdog threads, disabled\n");
  640. else
  641. watchdog_running = 1;
  642. } else if (sample_period_changed) {
  643. update_timers_all_cpus();
  644. }
  645. return err;
  646. }
  647. /* prepare/enable/disable routines */
  648. /* sysctl functions */
  649. #ifdef CONFIG_SYSCTL
  650. static void watchdog_disable_all_cpus(void)
  651. {
  652. if (watchdog_running) {
  653. watchdog_running = 0;
  654. smpboot_unregister_percpu_thread(&watchdog_threads);
  655. }
  656. }
  657. /*
  658. * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
  659. */
  660. int proc_dowatchdog(struct ctl_table *table, int write,
  661. void __user *buffer, size_t *lenp, loff_t *ppos)
  662. {
  663. int err, old_thresh, old_enabled;
  664. bool old_hardlockup;
  665. static DEFINE_MUTEX(watchdog_proc_mutex);
  666. mutex_lock(&watchdog_proc_mutex);
  667. old_thresh = ACCESS_ONCE(watchdog_thresh);
  668. old_enabled = ACCESS_ONCE(watchdog_user_enabled);
  669. old_hardlockup = watchdog_hardlockup_detector_is_enabled();
  670. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  671. if (err || !write)
  672. goto out;
  673. set_sample_period();
  674. /*
  675. * Watchdog threads shouldn't be enabled if they are
  676. * disabled. The 'watchdog_running' variable check in
  677. * watchdog_*_all_cpus() function takes care of this.
  678. */
  679. if (watchdog_user_enabled && watchdog_thresh) {
  680. /*
  681. * Prevent a change in watchdog_thresh accidentally overriding
  682. * the enablement of the hardlockup detector.
  683. */
  684. if (watchdog_user_enabled != old_enabled)
  685. watchdog_enable_hardlockup_detector(true);
  686. err = watchdog_enable_all_cpus(old_thresh != watchdog_thresh);
  687. } else
  688. watchdog_disable_all_cpus();
  689. /* Restore old values on failure */
  690. if (err) {
  691. watchdog_thresh = old_thresh;
  692. watchdog_user_enabled = old_enabled;
  693. watchdog_enable_hardlockup_detector(old_hardlockup);
  694. }
  695. out:
  696. mutex_unlock(&watchdog_proc_mutex);
  697. return err;
  698. }
  699. #endif /* CONFIG_SYSCTL */
  700. void __init lockup_detector_init(void)
  701. {
  702. set_sample_period();
  703. if (watchdog_user_enabled)
  704. watchdog_enable_all_cpus(false);
  705. }