watchdog.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Detect hard and soft lockups on a system
  4. *
  5. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  6. *
  7. * Note: Most of this code is borrowed heavily from the original softlockup
  8. * detector, so thanks to Ingo for the initial implementation.
  9. * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
  10. * to those contributors as well.
  11. */
  12. #define pr_fmt(fmt) "watchdog: " fmt
  13. #include <linux/mm.h>
  14. #include <linux/cpu.h>
  15. #include <linux/nmi.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/tick.h>
  20. #include <linux/sched/clock.h>
  21. #include <linux/sched/debug.h>
  22. #include <linux/sched/isolation.h>
  23. #include <linux/stop_machine.h>
  24. #include <asm/irq_regs.h>
  25. #include <linux/kvm_para.h>
  26. static DEFINE_MUTEX(watchdog_mutex);
  27. #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
  28. # define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED | NMI_WATCHDOG_ENABLED)
  29. # define NMI_WATCHDOG_DEFAULT 1
  30. #else
  31. # define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED)
  32. # define NMI_WATCHDOG_DEFAULT 0
  33. #endif
  34. unsigned long __read_mostly watchdog_enabled;
  35. int __read_mostly watchdog_user_enabled = 1;
  36. int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
  37. int __read_mostly soft_watchdog_user_enabled = 1;
  38. int __read_mostly watchdog_thresh = 10;
  39. int __read_mostly nmi_watchdog_available;
  40. struct cpumask watchdog_allowed_mask __read_mostly;
  41. struct cpumask watchdog_cpumask __read_mostly;
  42. unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
  43. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  44. /*
  45. * Should we panic when a soft-lockup or hard-lockup occurs:
  46. */
  47. unsigned int __read_mostly hardlockup_panic =
  48. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  49. /*
  50. * We may not want to enable hard lockup detection by default in all cases,
  51. * for example when running the kernel as a guest on a hypervisor. In these
  52. * cases this function can be called to disable hard lockup detection. This
  53. * function should only be executed once by the boot processor before the
  54. * kernel command line parameters are parsed, because otherwise it is not
  55. * possible to override this in hardlockup_panic_setup().
  56. */
  57. void __init hardlockup_detector_disable(void)
  58. {
  59. nmi_watchdog_user_enabled = 0;
  60. }
  61. static int __init hardlockup_panic_setup(char *str)
  62. {
  63. if (!strncmp(str, "panic", 5))
  64. hardlockup_panic = 1;
  65. else if (!strncmp(str, "nopanic", 7))
  66. hardlockup_panic = 0;
  67. else if (!strncmp(str, "0", 1))
  68. nmi_watchdog_user_enabled = 0;
  69. else if (!strncmp(str, "1", 1))
  70. nmi_watchdog_user_enabled = 1;
  71. return 1;
  72. }
  73. __setup("nmi_watchdog=", hardlockup_panic_setup);
  74. # ifdef CONFIG_SMP
  75. int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
  76. static int __init hardlockup_all_cpu_backtrace_setup(char *str)
  77. {
  78. sysctl_hardlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
  79. return 1;
  80. }
  81. __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
  82. # endif /* CONFIG_SMP */
  83. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  84. /*
  85. * These functions can be overridden if an architecture implements its
  86. * own hardlockup detector.
  87. *
  88. * watchdog_nmi_enable/disable can be implemented to start and stop when
  89. * softlockup watchdog threads start and stop. The arch must select the
  90. * SOFTLOCKUP_DETECTOR Kconfig.
  91. */
  92. int __weak watchdog_nmi_enable(unsigned int cpu)
  93. {
  94. hardlockup_detector_perf_enable();
  95. return 0;
  96. }
  97. void __weak watchdog_nmi_disable(unsigned int cpu)
  98. {
  99. hardlockup_detector_perf_disable();
  100. }
  101. /* Return 0, if a NMI watchdog is available. Error code otherwise */
  102. int __weak __init watchdog_nmi_probe(void)
  103. {
  104. return hardlockup_detector_perf_init();
  105. }
  106. /**
  107. * watchdog_nmi_stop - Stop the watchdog for reconfiguration
  108. *
  109. * The reconfiguration steps are:
  110. * watchdog_nmi_stop();
  111. * update_variables();
  112. * watchdog_nmi_start();
  113. */
  114. void __weak watchdog_nmi_stop(void) { }
  115. /**
  116. * watchdog_nmi_start - Start the watchdog after reconfiguration
  117. *
  118. * Counterpart to watchdog_nmi_stop().
  119. *
  120. * The following variables have been updated in update_variables() and
  121. * contain the currently valid configuration:
  122. * - watchdog_enabled
  123. * - watchdog_thresh
  124. * - watchdog_cpumask
  125. */
  126. void __weak watchdog_nmi_start(void) { }
  127. /**
  128. * lockup_detector_update_enable - Update the sysctl enable bit
  129. *
  130. * Caller needs to make sure that the NMI/perf watchdogs are off, so this
  131. * can't race with watchdog_nmi_disable().
  132. */
  133. static void lockup_detector_update_enable(void)
  134. {
  135. watchdog_enabled = 0;
  136. if (!watchdog_user_enabled)
  137. return;
  138. if (nmi_watchdog_available && nmi_watchdog_user_enabled)
  139. watchdog_enabled |= NMI_WATCHDOG_ENABLED;
  140. if (soft_watchdog_user_enabled)
  141. watchdog_enabled |= SOFT_WATCHDOG_ENABLED;
  142. }
  143. #ifdef CONFIG_SOFTLOCKUP_DETECTOR
  144. #define SOFTLOCKUP_RESET ULONG_MAX
  145. /* Global variables, exported for sysctl */
  146. unsigned int __read_mostly softlockup_panic =
  147. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  148. static bool softlockup_initialized __read_mostly;
  149. static u64 __read_mostly sample_period;
  150. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  151. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  152. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  153. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  154. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  155. static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
  156. static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
  157. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  158. static unsigned long soft_lockup_nmi_warn;
  159. static int __init softlockup_panic_setup(char *str)
  160. {
  161. softlockup_panic = simple_strtoul(str, NULL, 0);
  162. return 1;
  163. }
  164. __setup("softlockup_panic=", softlockup_panic_setup);
  165. static int __init nowatchdog_setup(char *str)
  166. {
  167. watchdog_user_enabled = 0;
  168. return 1;
  169. }
  170. __setup("nowatchdog", nowatchdog_setup);
  171. static int __init nosoftlockup_setup(char *str)
  172. {
  173. soft_watchdog_user_enabled = 0;
  174. return 1;
  175. }
  176. __setup("nosoftlockup", nosoftlockup_setup);
  177. #ifdef CONFIG_SMP
  178. int __read_mostly sysctl_softlockup_all_cpu_backtrace;
  179. static int __init softlockup_all_cpu_backtrace_setup(char *str)
  180. {
  181. sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
  182. return 1;
  183. }
  184. __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
  185. #endif
  186. static void __lockup_detector_cleanup(void);
  187. /*
  188. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  189. * lockups can have false positives under extreme conditions. So we generally
  190. * want a higher threshold for soft lockups than for hard lockups. So we couple
  191. * the thresholds with a factor: we make the soft threshold twice the amount of
  192. * time the hard threshold is.
  193. */
  194. static int get_softlockup_thresh(void)
  195. {
  196. return watchdog_thresh * 2;
  197. }
  198. /*
  199. * Returns seconds, approximately. We don't need nanosecond
  200. * resolution, and we don't need to waste time with a big divide when
  201. * 2^30ns == 1.074s.
  202. */
  203. static unsigned long get_timestamp(void)
  204. {
  205. return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
  206. }
  207. static void set_sample_period(void)
  208. {
  209. /*
  210. * convert watchdog_thresh from seconds to ns
  211. * the divide by 5 is to give hrtimer several chances (two
  212. * or three with the current relation between the soft
  213. * and hard thresholds) to increment before the
  214. * hardlockup detector generates a warning
  215. */
  216. sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
  217. watchdog_update_hrtimer_threshold(sample_period);
  218. }
  219. /* Commands for resetting the watchdog */
  220. static void __touch_watchdog(void)
  221. {
  222. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  223. }
  224. /**
  225. * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
  226. *
  227. * Call when the scheduler may have stalled for legitimate reasons
  228. * preventing the watchdog task from executing - e.g. the scheduler
  229. * entering idle state. This should only be used for scheduler events.
  230. * Use touch_softlockup_watchdog() for everything else.
  231. */
  232. notrace void touch_softlockup_watchdog_sched(void)
  233. {
  234. /*
  235. * Preemption can be enabled. It doesn't matter which CPU's timestamp
  236. * gets zeroed here, so use the raw_ operation.
  237. */
  238. raw_cpu_write(watchdog_touch_ts, SOFTLOCKUP_RESET);
  239. }
  240. notrace void touch_softlockup_watchdog(void)
  241. {
  242. touch_softlockup_watchdog_sched();
  243. wq_watchdog_touch(raw_smp_processor_id());
  244. }
  245. EXPORT_SYMBOL(touch_softlockup_watchdog);
  246. void touch_all_softlockup_watchdogs(void)
  247. {
  248. int cpu;
  249. /*
  250. * watchdog_mutex cannpt be taken here, as this might be called
  251. * from (soft)interrupt context, so the access to
  252. * watchdog_allowed_cpumask might race with a concurrent update.
  253. *
  254. * The watchdog time stamp can race against a concurrent real
  255. * update as well, the only side effect might be a cycle delay for
  256. * the softlockup check.
  257. */
  258. for_each_cpu(cpu, &watchdog_allowed_mask)
  259. per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
  260. wq_watchdog_touch(-1);
  261. }
  262. void touch_softlockup_watchdog_sync(void)
  263. {
  264. __this_cpu_write(softlockup_touch_sync, true);
  265. __this_cpu_write(watchdog_touch_ts, SOFTLOCKUP_RESET);
  266. }
  267. static int is_softlockup(unsigned long touch_ts)
  268. {
  269. unsigned long now = get_timestamp();
  270. if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
  271. /* Warn about unreasonable delays. */
  272. if (time_after(now, touch_ts + get_softlockup_thresh()))
  273. return now - touch_ts;
  274. }
  275. return 0;
  276. }
  277. /* watchdog detector functions */
  278. bool is_hardlockup(void)
  279. {
  280. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  281. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  282. return true;
  283. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  284. return false;
  285. }
  286. static void watchdog_interrupt_count(void)
  287. {
  288. __this_cpu_inc(hrtimer_interrupts);
  289. }
  290. static DEFINE_PER_CPU(struct completion, softlockup_completion);
  291. static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
  292. /*
  293. * The watchdog thread function - touches the timestamp.
  294. *
  295. * It only runs once every sample_period seconds (4 seconds by
  296. * default) to reset the softlockup timestamp. If this gets delayed
  297. * for more than 2*watchdog_thresh seconds then the debug-printout
  298. * triggers in watchdog_timer_fn().
  299. */
  300. static int softlockup_fn(void *data)
  301. {
  302. __this_cpu_write(soft_lockup_hrtimer_cnt,
  303. __this_cpu_read(hrtimer_interrupts));
  304. __touch_watchdog();
  305. complete(this_cpu_ptr(&softlockup_completion));
  306. return 0;
  307. }
  308. /* watchdog kicker functions */
  309. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  310. {
  311. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  312. struct pt_regs *regs = get_irq_regs();
  313. int duration;
  314. int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
  315. if (!watchdog_enabled)
  316. return HRTIMER_NORESTART;
  317. /* kick the hardlockup detector */
  318. watchdog_interrupt_count();
  319. /* kick the softlockup detector */
  320. if (completion_done(this_cpu_ptr(&softlockup_completion))) {
  321. reinit_completion(this_cpu_ptr(&softlockup_completion));
  322. stop_one_cpu_nowait(smp_processor_id(),
  323. softlockup_fn, NULL,
  324. this_cpu_ptr(&softlockup_stop_work));
  325. }
  326. /* .. and repeat */
  327. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  328. if (touch_ts == SOFTLOCKUP_RESET) {
  329. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  330. /*
  331. * If the time stamp was touched atomically
  332. * make sure the scheduler tick is up to date.
  333. */
  334. __this_cpu_write(softlockup_touch_sync, false);
  335. sched_clock_tick();
  336. }
  337. /* Clear the guest paused flag on watchdog reset */
  338. kvm_check_and_clear_guest_paused();
  339. __touch_watchdog();
  340. return HRTIMER_RESTART;
  341. }
  342. /* check for a softlockup
  343. * This is done by making sure a high priority task is
  344. * being scheduled. The task touches the watchdog to
  345. * indicate it is getting cpu time. If it hasn't then
  346. * this is a good indication some task is hogging the cpu
  347. */
  348. duration = is_softlockup(touch_ts);
  349. if (unlikely(duration)) {
  350. /*
  351. * If a virtual machine is stopped by the host it can look to
  352. * the watchdog like a soft lockup, check to see if the host
  353. * stopped the vm before we issue the warning
  354. */
  355. if (kvm_check_and_clear_guest_paused())
  356. return HRTIMER_RESTART;
  357. /* only warn once */
  358. if (__this_cpu_read(soft_watchdog_warn) == true) {
  359. /*
  360. * When multiple processes are causing softlockups the
  361. * softlockup detector only warns on the first one
  362. * because the code relies on a full quiet cycle to
  363. * re-arm. The second process prevents the quiet cycle
  364. * and never gets reported. Use task pointers to detect
  365. * this.
  366. */
  367. if (__this_cpu_read(softlockup_task_ptr_saved) !=
  368. current) {
  369. __this_cpu_write(soft_watchdog_warn, false);
  370. __touch_watchdog();
  371. }
  372. return HRTIMER_RESTART;
  373. }
  374. if (softlockup_all_cpu_backtrace) {
  375. /* Prevent multiple soft-lockup reports if one cpu is already
  376. * engaged in dumping cpu back traces
  377. */
  378. if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
  379. /* Someone else will report us. Let's give up */
  380. __this_cpu_write(soft_watchdog_warn, true);
  381. return HRTIMER_RESTART;
  382. }
  383. }
  384. pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  385. smp_processor_id(), duration,
  386. current->comm, task_pid_nr(current));
  387. __this_cpu_write(softlockup_task_ptr_saved, current);
  388. print_modules();
  389. print_irqtrace_events(current);
  390. if (regs)
  391. show_regs(regs);
  392. else
  393. dump_stack();
  394. if (softlockup_all_cpu_backtrace) {
  395. /* Avoid generating two back traces for current
  396. * given that one is already made above
  397. */
  398. trigger_allbutself_cpu_backtrace();
  399. clear_bit(0, &soft_lockup_nmi_warn);
  400. /* Barrier to sync with other cpus */
  401. smp_mb__after_atomic();
  402. }
  403. add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
  404. if (softlockup_panic)
  405. panic("softlockup: hung tasks");
  406. __this_cpu_write(soft_watchdog_warn, true);
  407. } else
  408. __this_cpu_write(soft_watchdog_warn, false);
  409. return HRTIMER_RESTART;
  410. }
  411. static void watchdog_enable(unsigned int cpu)
  412. {
  413. struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
  414. struct completion *done = this_cpu_ptr(&softlockup_completion);
  415. WARN_ON_ONCE(cpu != smp_processor_id());
  416. init_completion(done);
  417. complete(done);
  418. /*
  419. * Start the timer first to prevent the NMI watchdog triggering
  420. * before the timer has a chance to fire.
  421. */
  422. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  423. hrtimer->function = watchdog_timer_fn;
  424. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  425. HRTIMER_MODE_REL_PINNED);
  426. /* Initialize timestamp */
  427. __touch_watchdog();
  428. /* Enable the perf event */
  429. if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
  430. watchdog_nmi_enable(cpu);
  431. }
  432. static void watchdog_disable(unsigned int cpu)
  433. {
  434. struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
  435. WARN_ON_ONCE(cpu != smp_processor_id());
  436. /*
  437. * Disable the perf event first. That prevents that a large delay
  438. * between disabling the timer and disabling the perf event causes
  439. * the perf NMI to detect a false positive.
  440. */
  441. watchdog_nmi_disable(cpu);
  442. hrtimer_cancel(hrtimer);
  443. wait_for_completion(this_cpu_ptr(&softlockup_completion));
  444. }
  445. static int softlockup_stop_fn(void *data)
  446. {
  447. watchdog_disable(smp_processor_id());
  448. return 0;
  449. }
  450. static void softlockup_stop_all(void)
  451. {
  452. int cpu;
  453. if (!softlockup_initialized)
  454. return;
  455. for_each_cpu(cpu, &watchdog_allowed_mask)
  456. smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
  457. cpumask_clear(&watchdog_allowed_mask);
  458. }
  459. static int softlockup_start_fn(void *data)
  460. {
  461. watchdog_enable(smp_processor_id());
  462. return 0;
  463. }
  464. static void softlockup_start_all(void)
  465. {
  466. int cpu;
  467. cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
  468. for_each_cpu(cpu, &watchdog_allowed_mask)
  469. smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
  470. }
  471. int lockup_detector_online_cpu(unsigned int cpu)
  472. {
  473. if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
  474. watchdog_enable(cpu);
  475. return 0;
  476. }
  477. int lockup_detector_offline_cpu(unsigned int cpu)
  478. {
  479. if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
  480. watchdog_disable(cpu);
  481. return 0;
  482. }
  483. static void lockup_detector_reconfigure(void)
  484. {
  485. cpus_read_lock();
  486. watchdog_nmi_stop();
  487. softlockup_stop_all();
  488. set_sample_period();
  489. lockup_detector_update_enable();
  490. if (watchdog_enabled && watchdog_thresh)
  491. softlockup_start_all();
  492. watchdog_nmi_start();
  493. cpus_read_unlock();
  494. /*
  495. * Must be called outside the cpus locked section to prevent
  496. * recursive locking in the perf code.
  497. */
  498. __lockup_detector_cleanup();
  499. }
  500. /*
  501. * Create the watchdog thread infrastructure and configure the detector(s).
  502. *
  503. * The threads are not unparked as watchdog_allowed_mask is empty. When
  504. * the threads are sucessfully initialized, take the proper locks and
  505. * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
  506. */
  507. static __init void lockup_detector_setup(void)
  508. {
  509. /*
  510. * If sysctl is off and watchdog got disabled on the command line,
  511. * nothing to do here.
  512. */
  513. lockup_detector_update_enable();
  514. if (!IS_ENABLED(CONFIG_SYSCTL) &&
  515. !(watchdog_enabled && watchdog_thresh))
  516. return;
  517. mutex_lock(&watchdog_mutex);
  518. lockup_detector_reconfigure();
  519. softlockup_initialized = true;
  520. mutex_unlock(&watchdog_mutex);
  521. }
  522. #else /* CONFIG_SOFTLOCKUP_DETECTOR */
  523. static void lockup_detector_reconfigure(void)
  524. {
  525. cpus_read_lock();
  526. watchdog_nmi_stop();
  527. lockup_detector_update_enable();
  528. watchdog_nmi_start();
  529. cpus_read_unlock();
  530. }
  531. static inline void lockup_detector_setup(void)
  532. {
  533. lockup_detector_reconfigure();
  534. }
  535. #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
  536. static void __lockup_detector_cleanup(void)
  537. {
  538. lockdep_assert_held(&watchdog_mutex);
  539. hardlockup_detector_perf_cleanup();
  540. }
  541. /**
  542. * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
  543. *
  544. * Caller must not hold the cpu hotplug rwsem.
  545. */
  546. void lockup_detector_cleanup(void)
  547. {
  548. mutex_lock(&watchdog_mutex);
  549. __lockup_detector_cleanup();
  550. mutex_unlock(&watchdog_mutex);
  551. }
  552. /**
  553. * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
  554. *
  555. * Special interface for parisc. It prevents lockup detector warnings from
  556. * the default pm_poweroff() function which busy loops forever.
  557. */
  558. void lockup_detector_soft_poweroff(void)
  559. {
  560. watchdog_enabled = 0;
  561. }
  562. #ifdef CONFIG_SYSCTL
  563. /* Propagate any changes to the watchdog threads */
  564. static void proc_watchdog_update(void)
  565. {
  566. /* Remove impossible cpus to keep sysctl output clean. */
  567. cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
  568. lockup_detector_reconfigure();
  569. }
  570. /*
  571. * common function for watchdog, nmi_watchdog and soft_watchdog parameter
  572. *
  573. * caller | table->data points to | 'which'
  574. * -------------------|----------------------------|--------------------------
  575. * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED |
  576. * | | SOFT_WATCHDOG_ENABLED
  577. * -------------------|----------------------------|--------------------------
  578. * proc_nmi_watchdog | nmi_watchdog_user_enabled | NMI_WATCHDOG_ENABLED
  579. * -------------------|----------------------------|--------------------------
  580. * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
  581. */
  582. static int proc_watchdog_common(int which, struct ctl_table *table, int write,
  583. void __user *buffer, size_t *lenp, loff_t *ppos)
  584. {
  585. int err, old, *param = table->data;
  586. mutex_lock(&watchdog_mutex);
  587. if (!write) {
  588. /*
  589. * On read synchronize the userspace interface. This is a
  590. * racy snapshot.
  591. */
  592. *param = (watchdog_enabled & which) != 0;
  593. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  594. } else {
  595. old = READ_ONCE(*param);
  596. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  597. if (!err && old != READ_ONCE(*param))
  598. proc_watchdog_update();
  599. }
  600. mutex_unlock(&watchdog_mutex);
  601. return err;
  602. }
  603. /*
  604. * /proc/sys/kernel/watchdog
  605. */
  606. int proc_watchdog(struct ctl_table *table, int write,
  607. void __user *buffer, size_t *lenp, loff_t *ppos)
  608. {
  609. return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
  610. table, write, buffer, lenp, ppos);
  611. }
  612. /*
  613. * /proc/sys/kernel/nmi_watchdog
  614. */
  615. int proc_nmi_watchdog(struct ctl_table *table, int write,
  616. void __user *buffer, size_t *lenp, loff_t *ppos)
  617. {
  618. if (!nmi_watchdog_available && write)
  619. return -ENOTSUPP;
  620. return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
  621. table, write, buffer, lenp, ppos);
  622. }
  623. /*
  624. * /proc/sys/kernel/soft_watchdog
  625. */
  626. int proc_soft_watchdog(struct ctl_table *table, int write,
  627. void __user *buffer, size_t *lenp, loff_t *ppos)
  628. {
  629. return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
  630. table, write, buffer, lenp, ppos);
  631. }
  632. /*
  633. * /proc/sys/kernel/watchdog_thresh
  634. */
  635. int proc_watchdog_thresh(struct ctl_table *table, int write,
  636. void __user *buffer, size_t *lenp, loff_t *ppos)
  637. {
  638. int err, old;
  639. mutex_lock(&watchdog_mutex);
  640. old = READ_ONCE(watchdog_thresh);
  641. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  642. if (!err && write && old != READ_ONCE(watchdog_thresh))
  643. proc_watchdog_update();
  644. mutex_unlock(&watchdog_mutex);
  645. return err;
  646. }
  647. /*
  648. * The cpumask is the mask of possible cpus that the watchdog can run
  649. * on, not the mask of cpus it is actually running on. This allows the
  650. * user to specify a mask that will include cpus that have not yet
  651. * been brought online, if desired.
  652. */
  653. int proc_watchdog_cpumask(struct ctl_table *table, int write,
  654. void __user *buffer, size_t *lenp, loff_t *ppos)
  655. {
  656. int err;
  657. mutex_lock(&watchdog_mutex);
  658. err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
  659. if (!err && write)
  660. proc_watchdog_update();
  661. mutex_unlock(&watchdog_mutex);
  662. return err;
  663. }
  664. #endif /* CONFIG_SYSCTL */
  665. void __init lockup_detector_init(void)
  666. {
  667. if (tick_nohz_full_enabled())
  668. pr_info("Disabling watchdog on nohz_full cores by default\n");
  669. cpumask_copy(&watchdog_cpumask,
  670. housekeeping_cpumask(HK_FLAG_TIMER));
  671. if (!watchdog_nmi_probe())
  672. nmi_watchdog_available = true;
  673. lockup_detector_setup();
  674. }