watchdog.c 17 KB

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