watchdog.c 19 KB

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