watchdog.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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) "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 <uapi/linux/sched/types.h>
  21. #include <linux/tick.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/sched/clock.h>
  24. #include <linux/sched/debug.h>
  25. #include <asm/irq_regs.h>
  26. #include <linux/kvm_para.h>
  27. #include <linux/kthread.h>
  28. /* Watchdog configuration */
  29. static DEFINE_MUTEX(watchdog_proc_mutex);
  30. int __read_mostly nmi_watchdog_enabled;
  31. #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
  32. unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED |
  33. NMI_WATCHDOG_ENABLED;
  34. #else
  35. unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
  36. #endif
  37. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  38. /* boot commands */
  39. /*
  40. * Should we panic when a soft-lockup or hard-lockup occurs:
  41. */
  42. unsigned int __read_mostly hardlockup_panic =
  43. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  44. /*
  45. * We may not want to enable hard lockup detection by default in all cases,
  46. * for example when running the kernel as a guest on a hypervisor. In these
  47. * cases this function can be called to disable hard lockup detection. This
  48. * function should only be executed once by the boot processor before the
  49. * kernel command line parameters are parsed, because otherwise it is not
  50. * possible to override this in hardlockup_panic_setup().
  51. */
  52. void hardlockup_detector_disable(void)
  53. {
  54. watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
  55. }
  56. static int __init hardlockup_panic_setup(char *str)
  57. {
  58. if (!strncmp(str, "panic", 5))
  59. hardlockup_panic = 1;
  60. else if (!strncmp(str, "nopanic", 7))
  61. hardlockup_panic = 0;
  62. else if (!strncmp(str, "0", 1))
  63. watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
  64. else if (!strncmp(str, "1", 1))
  65. watchdog_enabled |= NMI_WATCHDOG_ENABLED;
  66. return 1;
  67. }
  68. __setup("nmi_watchdog=", hardlockup_panic_setup);
  69. #endif
  70. #ifdef CONFIG_SOFTLOCKUP_DETECTOR
  71. int __read_mostly soft_watchdog_enabled;
  72. #endif
  73. int __read_mostly watchdog_user_enabled;
  74. int __read_mostly watchdog_thresh = 10;
  75. #ifdef CONFIG_SMP
  76. int __read_mostly sysctl_softlockup_all_cpu_backtrace;
  77. int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
  78. #endif
  79. struct cpumask watchdog_cpumask __read_mostly;
  80. unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
  81. /*
  82. * The 'watchdog_running' variable is set to 1 when the watchdog threads
  83. * are registered/started and is set to 0 when the watchdog threads are
  84. * unregistered/stopped, so it is an indicator whether the threads exist.
  85. */
  86. static int __read_mostly watchdog_running;
  87. /*
  88. * If a subsystem has a need to deactivate the watchdog temporarily, it
  89. * can use the suspend/resume interface to achieve this. The content of
  90. * the 'watchdog_suspended' variable reflects this state. Existing threads
  91. * are parked/unparked by the lockup_detector_{suspend|resume} functions
  92. * (see comment blocks pertaining to those functions for further details).
  93. *
  94. * 'watchdog_suspended' also prevents threads from being registered/started
  95. * or unregistered/stopped via parameters in /proc/sys/kernel, so the state
  96. * of 'watchdog_running' cannot change while the watchdog is deactivated
  97. * temporarily (see related code in 'proc' handlers).
  98. */
  99. int __read_mostly watchdog_suspended;
  100. /*
  101. * These functions can be overridden if an architecture implements its
  102. * own hardlockup detector.
  103. *
  104. * watchdog_nmi_enable/disable can be implemented to start and stop when
  105. * softlockup watchdog threads start and stop. The arch must select the
  106. * SOFTLOCKUP_DETECTOR Kconfig.
  107. */
  108. int __weak watchdog_nmi_enable(unsigned int cpu)
  109. {
  110. return 0;
  111. }
  112. void __weak watchdog_nmi_disable(unsigned int cpu)
  113. {
  114. }
  115. /*
  116. * watchdog_nmi_reconfigure can be implemented to be notified after any
  117. * watchdog configuration change. The arch hardlockup watchdog should
  118. * respond to the following variables:
  119. * - nmi_watchdog_enabled
  120. * - watchdog_thresh
  121. * - watchdog_cpumask
  122. * - sysctl_hardlockup_all_cpu_backtrace
  123. * - hardlockup_panic
  124. * - watchdog_suspended
  125. */
  126. void __weak watchdog_nmi_reconfigure(void)
  127. {
  128. }
  129. #ifdef CONFIG_SOFTLOCKUP_DETECTOR
  130. /* Helper for online, unparked cpus. */
  131. #define for_each_watchdog_cpu(cpu) \
  132. for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
  133. atomic_t watchdog_park_in_progress = ATOMIC_INIT(0);
  134. static u64 __read_mostly sample_period;
  135. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  136. static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
  137. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  138. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  139. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  140. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  141. static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
  142. static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
  143. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  144. static unsigned long soft_lockup_nmi_warn;
  145. unsigned int __read_mostly softlockup_panic =
  146. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  147. static int __init softlockup_panic_setup(char *str)
  148. {
  149. softlockup_panic = simple_strtoul(str, NULL, 0);
  150. return 1;
  151. }
  152. __setup("softlockup_panic=", softlockup_panic_setup);
  153. static int __init nowatchdog_setup(char *str)
  154. {
  155. watchdog_enabled = 0;
  156. return 1;
  157. }
  158. __setup("nowatchdog", nowatchdog_setup);
  159. static int __init nosoftlockup_setup(char *str)
  160. {
  161. watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
  162. return 1;
  163. }
  164. __setup("nosoftlockup", nosoftlockup_setup);
  165. #ifdef CONFIG_SMP
  166. static int __init softlockup_all_cpu_backtrace_setup(char *str)
  167. {
  168. sysctl_softlockup_all_cpu_backtrace =
  169. !!simple_strtol(str, NULL, 0);
  170. return 1;
  171. }
  172. __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
  173. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  174. static int __init hardlockup_all_cpu_backtrace_setup(char *str)
  175. {
  176. sysctl_hardlockup_all_cpu_backtrace =
  177. !!simple_strtol(str, NULL, 0);
  178. return 1;
  179. }
  180. __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
  181. #endif
  182. #endif
  183. /*
  184. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  185. * lockups can have false positives under extreme conditions. So we generally
  186. * want a higher threshold for soft lockups than for hard lockups. So we couple
  187. * the thresholds with a factor: we make the soft threshold twice the amount of
  188. * time the hard threshold is.
  189. */
  190. static int get_softlockup_thresh(void)
  191. {
  192. return watchdog_thresh * 2;
  193. }
  194. /*
  195. * Returns seconds, approximately. We don't need nanosecond
  196. * resolution, and we don't need to waste time with a big divide when
  197. * 2^30ns == 1.074s.
  198. */
  199. static unsigned long get_timestamp(void)
  200. {
  201. return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
  202. }
  203. static void set_sample_period(void)
  204. {
  205. /*
  206. * convert watchdog_thresh from seconds to ns
  207. * the divide by 5 is to give hrtimer several chances (two
  208. * or three with the current relation between the soft
  209. * and hard thresholds) to increment before the
  210. * hardlockup detector generates a warning
  211. */
  212. sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
  213. }
  214. /* Commands for resetting the watchdog */
  215. static void __touch_watchdog(void)
  216. {
  217. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  218. }
  219. /**
  220. * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
  221. *
  222. * Call when the scheduler may have stalled for legitimate reasons
  223. * preventing the watchdog task from executing - e.g. the scheduler
  224. * entering idle state. This should only be used for scheduler events.
  225. * Use touch_softlockup_watchdog() for everything else.
  226. */
  227. void touch_softlockup_watchdog_sched(void)
  228. {
  229. /*
  230. * Preemption can be enabled. It doesn't matter which CPU's timestamp
  231. * gets zeroed here, so use the raw_ operation.
  232. */
  233. raw_cpu_write(watchdog_touch_ts, 0);
  234. }
  235. void touch_softlockup_watchdog(void)
  236. {
  237. touch_softlockup_watchdog_sched();
  238. wq_watchdog_touch(raw_smp_processor_id());
  239. }
  240. EXPORT_SYMBOL(touch_softlockup_watchdog);
  241. void touch_all_softlockup_watchdogs(void)
  242. {
  243. int cpu;
  244. /*
  245. * this is done lockless
  246. * do we care if a 0 races with a timestamp?
  247. * all it means is the softlock check starts one cycle later
  248. */
  249. for_each_watchdog_cpu(cpu)
  250. per_cpu(watchdog_touch_ts, cpu) = 0;
  251. wq_watchdog_touch(-1);
  252. }
  253. void touch_softlockup_watchdog_sync(void)
  254. {
  255. __this_cpu_write(softlockup_touch_sync, true);
  256. __this_cpu_write(watchdog_touch_ts, 0);
  257. }
  258. static int is_softlockup(unsigned long touch_ts)
  259. {
  260. unsigned long now = get_timestamp();
  261. if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
  262. /* Warn about unreasonable delays. */
  263. if (time_after(now, touch_ts + get_softlockup_thresh()))
  264. return now - touch_ts;
  265. }
  266. return 0;
  267. }
  268. /* watchdog detector functions */
  269. bool is_hardlockup(void)
  270. {
  271. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  272. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  273. return true;
  274. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  275. return false;
  276. }
  277. static void watchdog_interrupt_count(void)
  278. {
  279. __this_cpu_inc(hrtimer_interrupts);
  280. }
  281. static int watchdog_enable_all_cpus(void);
  282. static void watchdog_disable_all_cpus(void);
  283. /* watchdog kicker functions */
  284. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  285. {
  286. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  287. struct pt_regs *regs = get_irq_regs();
  288. int duration;
  289. int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
  290. if (atomic_read(&watchdog_park_in_progress) != 0)
  291. return HRTIMER_NORESTART;
  292. /* kick the hardlockup detector */
  293. watchdog_interrupt_count();
  294. /* kick the softlockup detector */
  295. wake_up_process(__this_cpu_read(softlockup_watchdog));
  296. /* .. and repeat */
  297. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  298. if (touch_ts == 0) {
  299. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  300. /*
  301. * If the time stamp was touched atomically
  302. * make sure the scheduler tick is up to date.
  303. */
  304. __this_cpu_write(softlockup_touch_sync, false);
  305. sched_clock_tick();
  306. }
  307. /* Clear the guest paused flag on watchdog reset */
  308. kvm_check_and_clear_guest_paused();
  309. __touch_watchdog();
  310. return HRTIMER_RESTART;
  311. }
  312. /* check for a softlockup
  313. * This is done by making sure a high priority task is
  314. * being scheduled. The task touches the watchdog to
  315. * indicate it is getting cpu time. If it hasn't then
  316. * this is a good indication some task is hogging the cpu
  317. */
  318. duration = is_softlockup(touch_ts);
  319. if (unlikely(duration)) {
  320. /*
  321. * If a virtual machine is stopped by the host it can look to
  322. * the watchdog like a soft lockup, check to see if the host
  323. * stopped the vm before we issue the warning
  324. */
  325. if (kvm_check_and_clear_guest_paused())
  326. return HRTIMER_RESTART;
  327. /* only warn once */
  328. if (__this_cpu_read(soft_watchdog_warn) == true) {
  329. /*
  330. * When multiple processes are causing softlockups the
  331. * softlockup detector only warns on the first one
  332. * because the code relies on a full quiet cycle to
  333. * re-arm. The second process prevents the quiet cycle
  334. * and never gets reported. Use task pointers to detect
  335. * this.
  336. */
  337. if (__this_cpu_read(softlockup_task_ptr_saved) !=
  338. current) {
  339. __this_cpu_write(soft_watchdog_warn, false);
  340. __touch_watchdog();
  341. }
  342. return HRTIMER_RESTART;
  343. }
  344. if (softlockup_all_cpu_backtrace) {
  345. /* Prevent multiple soft-lockup reports if one cpu is already
  346. * engaged in dumping cpu back traces
  347. */
  348. if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
  349. /* Someone else will report us. Let's give up */
  350. __this_cpu_write(soft_watchdog_warn, true);
  351. return HRTIMER_RESTART;
  352. }
  353. }
  354. pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  355. smp_processor_id(), duration,
  356. current->comm, task_pid_nr(current));
  357. __this_cpu_write(softlockup_task_ptr_saved, current);
  358. print_modules();
  359. print_irqtrace_events(current);
  360. if (regs)
  361. show_regs(regs);
  362. else
  363. dump_stack();
  364. if (softlockup_all_cpu_backtrace) {
  365. /* Avoid generating two back traces for current
  366. * given that one is already made above
  367. */
  368. trigger_allbutself_cpu_backtrace();
  369. clear_bit(0, &soft_lockup_nmi_warn);
  370. /* Barrier to sync with other cpus */
  371. smp_mb__after_atomic();
  372. }
  373. add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
  374. if (softlockup_panic)
  375. panic("softlockup: hung tasks");
  376. __this_cpu_write(soft_watchdog_warn, true);
  377. } else
  378. __this_cpu_write(soft_watchdog_warn, false);
  379. return HRTIMER_RESTART;
  380. }
  381. static void watchdog_set_prio(unsigned int policy, unsigned int prio)
  382. {
  383. struct sched_param param = { .sched_priority = prio };
  384. sched_setscheduler(current, policy, &param);
  385. }
  386. static void watchdog_enable(unsigned int cpu)
  387. {
  388. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  389. /* kick off the timer for the hardlockup detector */
  390. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  391. hrtimer->function = watchdog_timer_fn;
  392. /* Enable the perf event */
  393. watchdog_nmi_enable(cpu);
  394. /* done here because hrtimer_start can only pin to smp_processor_id() */
  395. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  396. HRTIMER_MODE_REL_PINNED);
  397. /* initialize timestamp */
  398. watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
  399. __touch_watchdog();
  400. }
  401. static void watchdog_disable(unsigned int cpu)
  402. {
  403. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  404. watchdog_set_prio(SCHED_NORMAL, 0);
  405. hrtimer_cancel(hrtimer);
  406. /* disable the perf event */
  407. watchdog_nmi_disable(cpu);
  408. }
  409. static void watchdog_cleanup(unsigned int cpu, bool online)
  410. {
  411. watchdog_disable(cpu);
  412. }
  413. static int watchdog_should_run(unsigned int cpu)
  414. {
  415. return __this_cpu_read(hrtimer_interrupts) !=
  416. __this_cpu_read(soft_lockup_hrtimer_cnt);
  417. }
  418. /*
  419. * The watchdog thread function - touches the timestamp.
  420. *
  421. * It only runs once every sample_period seconds (4 seconds by
  422. * default) to reset the softlockup timestamp. If this gets delayed
  423. * for more than 2*watchdog_thresh seconds then the debug-printout
  424. * triggers in watchdog_timer_fn().
  425. */
  426. static void watchdog(unsigned int cpu)
  427. {
  428. __this_cpu_write(soft_lockup_hrtimer_cnt,
  429. __this_cpu_read(hrtimer_interrupts));
  430. __touch_watchdog();
  431. /*
  432. * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
  433. * failure path. Check for failures that can occur asynchronously -
  434. * for example, when CPUs are on-lined - and shut down the hardware
  435. * perf event on each CPU accordingly.
  436. *
  437. * The only non-obvious place this bit can be cleared is through
  438. * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a
  439. * pr_info here would be too noisy as it would result in a message
  440. * every few seconds if the hardlockup was disabled but the softlockup
  441. * enabled.
  442. */
  443. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  444. watchdog_nmi_disable(cpu);
  445. }
  446. static struct smp_hotplug_thread watchdog_threads = {
  447. .store = &softlockup_watchdog,
  448. .thread_should_run = watchdog_should_run,
  449. .thread_fn = watchdog,
  450. .thread_comm = "watchdog/%u",
  451. .setup = watchdog_enable,
  452. .cleanup = watchdog_cleanup,
  453. .park = watchdog_disable,
  454. .unpark = watchdog_enable,
  455. };
  456. /*
  457. * park all watchdog threads that are specified in 'watchdog_cpumask'
  458. *
  459. * This function returns an error if kthread_park() of a watchdog thread
  460. * fails. In this situation, the watchdog threads of some CPUs can already
  461. * be parked and the watchdog threads of other CPUs can still be runnable.
  462. * Callers are expected to handle this special condition as appropriate in
  463. * their context.
  464. *
  465. * This function may only be called in a context that is protected against
  466. * races with CPU hotplug - for example, via get_online_cpus().
  467. */
  468. static int watchdog_park_threads(void)
  469. {
  470. int cpu, ret = 0;
  471. atomic_set(&watchdog_park_in_progress, 1);
  472. for_each_watchdog_cpu(cpu) {
  473. ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
  474. if (ret)
  475. break;
  476. }
  477. atomic_set(&watchdog_park_in_progress, 0);
  478. return ret;
  479. }
  480. /*
  481. * unpark all watchdog threads that are specified in 'watchdog_cpumask'
  482. *
  483. * This function may only be called in a context that is protected against
  484. * races with CPU hotplug - for example, via get_online_cpus().
  485. */
  486. static void watchdog_unpark_threads(void)
  487. {
  488. int cpu;
  489. for_each_watchdog_cpu(cpu)
  490. kthread_unpark(per_cpu(softlockup_watchdog, cpu));
  491. }
  492. static int update_watchdog_all_cpus(void)
  493. {
  494. int ret;
  495. ret = watchdog_park_threads();
  496. if (ret)
  497. return ret;
  498. watchdog_unpark_threads();
  499. return 0;
  500. }
  501. static int watchdog_enable_all_cpus(void)
  502. {
  503. int err = 0;
  504. if (!watchdog_running) {
  505. err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
  506. &watchdog_cpumask);
  507. if (err)
  508. pr_err("Failed to create watchdog threads, disabled\n");
  509. else
  510. watchdog_running = 1;
  511. } else {
  512. /*
  513. * Enable/disable the lockup detectors or
  514. * change the sample period 'on the fly'.
  515. */
  516. err = update_watchdog_all_cpus();
  517. if (err) {
  518. watchdog_disable_all_cpus();
  519. pr_err("Failed to update lockup detectors, disabled\n");
  520. }
  521. }
  522. if (err)
  523. watchdog_enabled = 0;
  524. return err;
  525. }
  526. static void watchdog_disable_all_cpus(void)
  527. {
  528. if (watchdog_running) {
  529. watchdog_running = 0;
  530. smpboot_unregister_percpu_thread(&watchdog_threads);
  531. }
  532. }
  533. #ifdef CONFIG_SYSCTL
  534. static int watchdog_update_cpus(void)
  535. {
  536. return smpboot_update_cpumask_percpu_thread(
  537. &watchdog_threads, &watchdog_cpumask);
  538. }
  539. #endif
  540. #else /* SOFTLOCKUP */
  541. static int watchdog_park_threads(void)
  542. {
  543. return 0;
  544. }
  545. static void watchdog_unpark_threads(void)
  546. {
  547. }
  548. static int watchdog_enable_all_cpus(void)
  549. {
  550. return 0;
  551. }
  552. static void watchdog_disable_all_cpus(void)
  553. {
  554. }
  555. #ifdef CONFIG_SYSCTL
  556. static int watchdog_update_cpus(void)
  557. {
  558. return 0;
  559. }
  560. #endif
  561. static void set_sample_period(void)
  562. {
  563. }
  564. #endif /* SOFTLOCKUP */
  565. /*
  566. * Suspend the hard and soft lockup detector by parking the watchdog threads.
  567. */
  568. int lockup_detector_suspend(void)
  569. {
  570. int ret = 0;
  571. get_online_cpus();
  572. mutex_lock(&watchdog_proc_mutex);
  573. /*
  574. * Multiple suspend requests can be active in parallel (counted by
  575. * the 'watchdog_suspended' variable). If the watchdog threads are
  576. * running, the first caller takes care that they will be parked.
  577. * The state of 'watchdog_running' cannot change while a suspend
  578. * request is active (see related code in 'proc' handlers).
  579. */
  580. if (watchdog_running && !watchdog_suspended)
  581. ret = watchdog_park_threads();
  582. if (ret == 0)
  583. watchdog_suspended++;
  584. else {
  585. watchdog_disable_all_cpus();
  586. pr_err("Failed to suspend lockup detectors, disabled\n");
  587. watchdog_enabled = 0;
  588. }
  589. watchdog_nmi_reconfigure();
  590. mutex_unlock(&watchdog_proc_mutex);
  591. return ret;
  592. }
  593. /*
  594. * Resume the hard and soft lockup detector by unparking the watchdog threads.
  595. */
  596. void lockup_detector_resume(void)
  597. {
  598. mutex_lock(&watchdog_proc_mutex);
  599. watchdog_suspended--;
  600. /*
  601. * The watchdog threads are unparked if they were previously running
  602. * and if there is no more active suspend request.
  603. */
  604. if (watchdog_running && !watchdog_suspended)
  605. watchdog_unpark_threads();
  606. watchdog_nmi_reconfigure();
  607. mutex_unlock(&watchdog_proc_mutex);
  608. put_online_cpus();
  609. }
  610. #ifdef CONFIG_SYSCTL
  611. /*
  612. * Update the run state of the lockup detectors.
  613. */
  614. static int proc_watchdog_update(void)
  615. {
  616. int err = 0;
  617. /*
  618. * Watchdog threads won't be started if they are already active.
  619. * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
  620. * care of this. If those threads are already active, the sample
  621. * period will be updated and the lockup detectors will be enabled
  622. * or disabled 'on the fly'.
  623. */
  624. if (watchdog_enabled && watchdog_thresh)
  625. err = watchdog_enable_all_cpus();
  626. else
  627. watchdog_disable_all_cpus();
  628. watchdog_nmi_reconfigure();
  629. return err;
  630. }
  631. /*
  632. * common function for watchdog, nmi_watchdog and soft_watchdog parameter
  633. *
  634. * caller | table->data points to | 'which' contains the flag(s)
  635. * -------------------|-----------------------|-----------------------------
  636. * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
  637. * | | with SOFT_WATCHDOG_ENABLED
  638. * -------------------|-----------------------|-----------------------------
  639. * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED
  640. * -------------------|-----------------------|-----------------------------
  641. * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
  642. */
  643. static int proc_watchdog_common(int which, struct ctl_table *table, int write,
  644. void __user *buffer, size_t *lenp, loff_t *ppos)
  645. {
  646. int err, old, new;
  647. int *watchdog_param = (int *)table->data;
  648. get_online_cpus();
  649. mutex_lock(&watchdog_proc_mutex);
  650. if (watchdog_suspended) {
  651. /* no parameter changes allowed while watchdog is suspended */
  652. err = -EAGAIN;
  653. goto out;
  654. }
  655. /*
  656. * If the parameter is being read return the state of the corresponding
  657. * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
  658. * run state of the lockup detectors.
  659. */
  660. if (!write) {
  661. *watchdog_param = (watchdog_enabled & which) != 0;
  662. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  663. } else {
  664. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  665. if (err)
  666. goto out;
  667. /*
  668. * There is a race window between fetching the current value
  669. * from 'watchdog_enabled' and storing the new value. During
  670. * this race window, watchdog_nmi_enable() can sneak in and
  671. * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
  672. * The 'cmpxchg' detects this race and the loop retries.
  673. */
  674. do {
  675. old = watchdog_enabled;
  676. /*
  677. * If the parameter value is not zero set the
  678. * corresponding bit(s), else clear it(them).
  679. */
  680. if (*watchdog_param)
  681. new = old | which;
  682. else
  683. new = old & ~which;
  684. } while (cmpxchg(&watchdog_enabled, old, new) != old);
  685. /*
  686. * Update the run state of the lockup detectors. There is _no_
  687. * need to check the value returned by proc_watchdog_update()
  688. * and to restore the previous value of 'watchdog_enabled' as
  689. * both lockup detectors are disabled if proc_watchdog_update()
  690. * returns an error.
  691. */
  692. if (old == new)
  693. goto out;
  694. err = proc_watchdog_update();
  695. }
  696. out:
  697. mutex_unlock(&watchdog_proc_mutex);
  698. put_online_cpus();
  699. return err;
  700. }
  701. /*
  702. * /proc/sys/kernel/watchdog
  703. */
  704. int proc_watchdog(struct ctl_table *table, int write,
  705. void __user *buffer, size_t *lenp, loff_t *ppos)
  706. {
  707. return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
  708. table, write, buffer, lenp, ppos);
  709. }
  710. /*
  711. * /proc/sys/kernel/nmi_watchdog
  712. */
  713. int proc_nmi_watchdog(struct ctl_table *table, int write,
  714. void __user *buffer, size_t *lenp, loff_t *ppos)
  715. {
  716. return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
  717. table, write, buffer, lenp, ppos);
  718. }
  719. /*
  720. * /proc/sys/kernel/soft_watchdog
  721. */
  722. int proc_soft_watchdog(struct ctl_table *table, int write,
  723. void __user *buffer, size_t *lenp, loff_t *ppos)
  724. {
  725. return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
  726. table, write, buffer, lenp, ppos);
  727. }
  728. /*
  729. * /proc/sys/kernel/watchdog_thresh
  730. */
  731. int proc_watchdog_thresh(struct ctl_table *table, int write,
  732. void __user *buffer, size_t *lenp, loff_t *ppos)
  733. {
  734. int err, old, new;
  735. get_online_cpus();
  736. mutex_lock(&watchdog_proc_mutex);
  737. if (watchdog_suspended) {
  738. /* no parameter changes allowed while watchdog is suspended */
  739. err = -EAGAIN;
  740. goto out;
  741. }
  742. old = ACCESS_ONCE(watchdog_thresh);
  743. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  744. if (err || !write)
  745. goto out;
  746. /*
  747. * Update the sample period. Restore on failure.
  748. */
  749. new = ACCESS_ONCE(watchdog_thresh);
  750. if (old == new)
  751. goto out;
  752. set_sample_period();
  753. err = proc_watchdog_update();
  754. if (err) {
  755. watchdog_thresh = old;
  756. set_sample_period();
  757. }
  758. out:
  759. mutex_unlock(&watchdog_proc_mutex);
  760. put_online_cpus();
  761. return err;
  762. }
  763. /*
  764. * The cpumask is the mask of possible cpus that the watchdog can run
  765. * on, not the mask of cpus it is actually running on. This allows the
  766. * user to specify a mask that will include cpus that have not yet
  767. * been brought online, if desired.
  768. */
  769. int proc_watchdog_cpumask(struct ctl_table *table, int write,
  770. void __user *buffer, size_t *lenp, loff_t *ppos)
  771. {
  772. int err;
  773. get_online_cpus();
  774. mutex_lock(&watchdog_proc_mutex);
  775. if (watchdog_suspended) {
  776. /* no parameter changes allowed while watchdog is suspended */
  777. err = -EAGAIN;
  778. goto out;
  779. }
  780. err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
  781. if (!err && write) {
  782. /* Remove impossible cpus to keep sysctl output cleaner. */
  783. cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
  784. cpu_possible_mask);
  785. if (watchdog_running) {
  786. /*
  787. * Failure would be due to being unable to allocate
  788. * a temporary cpumask, so we are likely not in a
  789. * position to do much else to make things better.
  790. */
  791. if (watchdog_update_cpus() != 0)
  792. pr_err("cpumask update failed\n");
  793. }
  794. watchdog_nmi_reconfigure();
  795. }
  796. out:
  797. mutex_unlock(&watchdog_proc_mutex);
  798. put_online_cpus();
  799. return err;
  800. }
  801. #endif /* CONFIG_SYSCTL */
  802. void __init lockup_detector_init(void)
  803. {
  804. set_sample_period();
  805. #ifdef CONFIG_NO_HZ_FULL
  806. if (tick_nohz_full_enabled()) {
  807. pr_info("Disabling watchdog on nohz_full cores by default\n");
  808. cpumask_copy(&watchdog_cpumask, housekeeping_mask);
  809. } else
  810. cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
  811. #else
  812. cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
  813. #endif
  814. if (watchdog_enabled)
  815. watchdog_enable_all_cpus();
  816. }