watchdog.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. watchdog_update_hrtimer_threshold(sample_period);
  214. }
  215. /* Commands for resetting the watchdog */
  216. static void __touch_watchdog(void)
  217. {
  218. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  219. }
  220. /**
  221. * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
  222. *
  223. * Call when the scheduler may have stalled for legitimate reasons
  224. * preventing the watchdog task from executing - e.g. the scheduler
  225. * entering idle state. This should only be used for scheduler events.
  226. * Use touch_softlockup_watchdog() for everything else.
  227. */
  228. void touch_softlockup_watchdog_sched(void)
  229. {
  230. /*
  231. * Preemption can be enabled. It doesn't matter which CPU's timestamp
  232. * gets zeroed here, so use the raw_ operation.
  233. */
  234. raw_cpu_write(watchdog_touch_ts, 0);
  235. }
  236. void touch_softlockup_watchdog(void)
  237. {
  238. touch_softlockup_watchdog_sched();
  239. wq_watchdog_touch(raw_smp_processor_id());
  240. }
  241. EXPORT_SYMBOL(touch_softlockup_watchdog);
  242. void touch_all_softlockup_watchdogs(void)
  243. {
  244. int cpu;
  245. /*
  246. * this is done lockless
  247. * do we care if a 0 races with a timestamp?
  248. * all it means is the softlock check starts one cycle later
  249. */
  250. for_each_watchdog_cpu(cpu)
  251. per_cpu(watchdog_touch_ts, cpu) = 0;
  252. wq_watchdog_touch(-1);
  253. }
  254. void touch_softlockup_watchdog_sync(void)
  255. {
  256. __this_cpu_write(softlockup_touch_sync, true);
  257. __this_cpu_write(watchdog_touch_ts, 0);
  258. }
  259. static int is_softlockup(unsigned long touch_ts)
  260. {
  261. unsigned long now = get_timestamp();
  262. if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
  263. /* Warn about unreasonable delays. */
  264. if (time_after(now, touch_ts + get_softlockup_thresh()))
  265. return now - touch_ts;
  266. }
  267. return 0;
  268. }
  269. /* watchdog detector functions */
  270. bool is_hardlockup(void)
  271. {
  272. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  273. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  274. return true;
  275. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  276. return false;
  277. }
  278. static void watchdog_interrupt_count(void)
  279. {
  280. __this_cpu_inc(hrtimer_interrupts);
  281. }
  282. static int watchdog_enable_all_cpus(void);
  283. static void watchdog_disable_all_cpus(void);
  284. /* watchdog kicker functions */
  285. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  286. {
  287. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  288. struct pt_regs *regs = get_irq_regs();
  289. int duration;
  290. int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
  291. if (atomic_read(&watchdog_park_in_progress) != 0)
  292. return HRTIMER_NORESTART;
  293. /* kick the hardlockup detector */
  294. watchdog_interrupt_count();
  295. /* kick the softlockup detector */
  296. wake_up_process(__this_cpu_read(softlockup_watchdog));
  297. /* .. and repeat */
  298. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  299. if (touch_ts == 0) {
  300. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  301. /*
  302. * If the time stamp was touched atomically
  303. * make sure the scheduler tick is up to date.
  304. */
  305. __this_cpu_write(softlockup_touch_sync, false);
  306. sched_clock_tick();
  307. }
  308. /* Clear the guest paused flag on watchdog reset */
  309. kvm_check_and_clear_guest_paused();
  310. __touch_watchdog();
  311. return HRTIMER_RESTART;
  312. }
  313. /* check for a softlockup
  314. * This is done by making sure a high priority task is
  315. * being scheduled. The task touches the watchdog to
  316. * indicate it is getting cpu time. If it hasn't then
  317. * this is a good indication some task is hogging the cpu
  318. */
  319. duration = is_softlockup(touch_ts);
  320. if (unlikely(duration)) {
  321. /*
  322. * If a virtual machine is stopped by the host it can look to
  323. * the watchdog like a soft lockup, check to see if the host
  324. * stopped the vm before we issue the warning
  325. */
  326. if (kvm_check_and_clear_guest_paused())
  327. return HRTIMER_RESTART;
  328. /* only warn once */
  329. if (__this_cpu_read(soft_watchdog_warn) == true) {
  330. /*
  331. * When multiple processes are causing softlockups the
  332. * softlockup detector only warns on the first one
  333. * because the code relies on a full quiet cycle to
  334. * re-arm. The second process prevents the quiet cycle
  335. * and never gets reported. Use task pointers to detect
  336. * this.
  337. */
  338. if (__this_cpu_read(softlockup_task_ptr_saved) !=
  339. current) {
  340. __this_cpu_write(soft_watchdog_warn, false);
  341. __touch_watchdog();
  342. }
  343. return HRTIMER_RESTART;
  344. }
  345. if (softlockup_all_cpu_backtrace) {
  346. /* Prevent multiple soft-lockup reports if one cpu is already
  347. * engaged in dumping cpu back traces
  348. */
  349. if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
  350. /* Someone else will report us. Let's give up */
  351. __this_cpu_write(soft_watchdog_warn, true);
  352. return HRTIMER_RESTART;
  353. }
  354. }
  355. pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  356. smp_processor_id(), duration,
  357. current->comm, task_pid_nr(current));
  358. __this_cpu_write(softlockup_task_ptr_saved, current);
  359. print_modules();
  360. print_irqtrace_events(current);
  361. if (regs)
  362. show_regs(regs);
  363. else
  364. dump_stack();
  365. if (softlockup_all_cpu_backtrace) {
  366. /* Avoid generating two back traces for current
  367. * given that one is already made above
  368. */
  369. trigger_allbutself_cpu_backtrace();
  370. clear_bit(0, &soft_lockup_nmi_warn);
  371. /* Barrier to sync with other cpus */
  372. smp_mb__after_atomic();
  373. }
  374. add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
  375. if (softlockup_panic)
  376. panic("softlockup: hung tasks");
  377. __this_cpu_write(soft_watchdog_warn, true);
  378. } else
  379. __this_cpu_write(soft_watchdog_warn, false);
  380. return HRTIMER_RESTART;
  381. }
  382. static void watchdog_set_prio(unsigned int policy, unsigned int prio)
  383. {
  384. struct sched_param param = { .sched_priority = prio };
  385. sched_setscheduler(current, policy, &param);
  386. }
  387. static void watchdog_enable(unsigned int cpu)
  388. {
  389. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  390. /* kick off the timer for the hardlockup detector */
  391. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  392. hrtimer->function = watchdog_timer_fn;
  393. /* Enable the perf event */
  394. watchdog_nmi_enable(cpu);
  395. /* done here because hrtimer_start can only pin to smp_processor_id() */
  396. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  397. HRTIMER_MODE_REL_PINNED);
  398. /* initialize timestamp */
  399. watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
  400. __touch_watchdog();
  401. }
  402. static void watchdog_disable(unsigned int cpu)
  403. {
  404. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  405. watchdog_set_prio(SCHED_NORMAL, 0);
  406. hrtimer_cancel(hrtimer);
  407. /* disable the perf event */
  408. watchdog_nmi_disable(cpu);
  409. }
  410. static void watchdog_cleanup(unsigned int cpu, bool online)
  411. {
  412. watchdog_disable(cpu);
  413. }
  414. static int watchdog_should_run(unsigned int cpu)
  415. {
  416. return __this_cpu_read(hrtimer_interrupts) !=
  417. __this_cpu_read(soft_lockup_hrtimer_cnt);
  418. }
  419. /*
  420. * The watchdog thread function - touches the timestamp.
  421. *
  422. * It only runs once every sample_period seconds (4 seconds by
  423. * default) to reset the softlockup timestamp. If this gets delayed
  424. * for more than 2*watchdog_thresh seconds then the debug-printout
  425. * triggers in watchdog_timer_fn().
  426. */
  427. static void watchdog(unsigned int cpu)
  428. {
  429. __this_cpu_write(soft_lockup_hrtimer_cnt,
  430. __this_cpu_read(hrtimer_interrupts));
  431. __touch_watchdog();
  432. /*
  433. * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
  434. * failure path. Check for failures that can occur asynchronously -
  435. * for example, when CPUs are on-lined - and shut down the hardware
  436. * perf event on each CPU accordingly.
  437. *
  438. * The only non-obvious place this bit can be cleared is through
  439. * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a
  440. * pr_info here would be too noisy as it would result in a message
  441. * every few seconds if the hardlockup was disabled but the softlockup
  442. * enabled.
  443. */
  444. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  445. watchdog_nmi_disable(cpu);
  446. }
  447. static struct smp_hotplug_thread watchdog_threads = {
  448. .store = &softlockup_watchdog,
  449. .thread_should_run = watchdog_should_run,
  450. .thread_fn = watchdog,
  451. .thread_comm = "watchdog/%u",
  452. .setup = watchdog_enable,
  453. .cleanup = watchdog_cleanup,
  454. .park = watchdog_disable,
  455. .unpark = watchdog_enable,
  456. };
  457. /*
  458. * park all watchdog threads that are specified in 'watchdog_cpumask'
  459. *
  460. * This function returns an error if kthread_park() of a watchdog thread
  461. * fails. In this situation, the watchdog threads of some CPUs can already
  462. * be parked and the watchdog threads of other CPUs can still be runnable.
  463. * Callers are expected to handle this special condition as appropriate in
  464. * their context.
  465. *
  466. * This function may only be called in a context that is protected against
  467. * races with CPU hotplug - for example, via get_online_cpus().
  468. */
  469. static int watchdog_park_threads(void)
  470. {
  471. int cpu, ret = 0;
  472. atomic_set(&watchdog_park_in_progress, 1);
  473. for_each_watchdog_cpu(cpu) {
  474. ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
  475. if (ret)
  476. break;
  477. }
  478. atomic_set(&watchdog_park_in_progress, 0);
  479. return ret;
  480. }
  481. /*
  482. * unpark all watchdog threads that are specified in 'watchdog_cpumask'
  483. *
  484. * This function may only be called in a context that is protected against
  485. * races with CPU hotplug - for example, via get_online_cpus().
  486. */
  487. static void watchdog_unpark_threads(void)
  488. {
  489. int cpu;
  490. for_each_watchdog_cpu(cpu)
  491. kthread_unpark(per_cpu(softlockup_watchdog, cpu));
  492. }
  493. static int update_watchdog_all_cpus(void)
  494. {
  495. int ret;
  496. ret = watchdog_park_threads();
  497. if (ret)
  498. return ret;
  499. watchdog_unpark_threads();
  500. return 0;
  501. }
  502. static int watchdog_enable_all_cpus(void)
  503. {
  504. int err = 0;
  505. if (!watchdog_running) {
  506. err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
  507. &watchdog_cpumask);
  508. if (err)
  509. pr_err("Failed to create watchdog threads, disabled\n");
  510. else
  511. watchdog_running = 1;
  512. } else {
  513. /*
  514. * Enable/disable the lockup detectors or
  515. * change the sample period 'on the fly'.
  516. */
  517. err = update_watchdog_all_cpus();
  518. if (err) {
  519. watchdog_disable_all_cpus();
  520. pr_err("Failed to update lockup detectors, disabled\n");
  521. }
  522. }
  523. if (err)
  524. watchdog_enabled = 0;
  525. return err;
  526. }
  527. static void watchdog_disable_all_cpus(void)
  528. {
  529. if (watchdog_running) {
  530. watchdog_running = 0;
  531. smpboot_unregister_percpu_thread(&watchdog_threads);
  532. }
  533. }
  534. #ifdef CONFIG_SYSCTL
  535. static int watchdog_update_cpus(void)
  536. {
  537. return smpboot_update_cpumask_percpu_thread(
  538. &watchdog_threads, &watchdog_cpumask);
  539. }
  540. #endif
  541. #else /* SOFTLOCKUP */
  542. static int watchdog_park_threads(void)
  543. {
  544. return 0;
  545. }
  546. static void watchdog_unpark_threads(void)
  547. {
  548. }
  549. static int watchdog_enable_all_cpus(void)
  550. {
  551. return 0;
  552. }
  553. static void watchdog_disable_all_cpus(void)
  554. {
  555. }
  556. #ifdef CONFIG_SYSCTL
  557. static int watchdog_update_cpus(void)
  558. {
  559. return 0;
  560. }
  561. #endif
  562. static void set_sample_period(void)
  563. {
  564. }
  565. #endif /* SOFTLOCKUP */
  566. /*
  567. * Suspend the hard and soft lockup detector by parking the watchdog threads.
  568. */
  569. int lockup_detector_suspend(void)
  570. {
  571. int ret = 0;
  572. get_online_cpus();
  573. mutex_lock(&watchdog_proc_mutex);
  574. /*
  575. * Multiple suspend requests can be active in parallel (counted by
  576. * the 'watchdog_suspended' variable). If the watchdog threads are
  577. * running, the first caller takes care that they will be parked.
  578. * The state of 'watchdog_running' cannot change while a suspend
  579. * request is active (see related code in 'proc' handlers).
  580. */
  581. if (watchdog_running && !watchdog_suspended)
  582. ret = watchdog_park_threads();
  583. if (ret == 0)
  584. watchdog_suspended++;
  585. else {
  586. watchdog_disable_all_cpus();
  587. pr_err("Failed to suspend lockup detectors, disabled\n");
  588. watchdog_enabled = 0;
  589. }
  590. watchdog_nmi_reconfigure();
  591. mutex_unlock(&watchdog_proc_mutex);
  592. return ret;
  593. }
  594. /*
  595. * Resume the hard and soft lockup detector by unparking the watchdog threads.
  596. */
  597. void lockup_detector_resume(void)
  598. {
  599. mutex_lock(&watchdog_proc_mutex);
  600. watchdog_suspended--;
  601. /*
  602. * The watchdog threads are unparked if they were previously running
  603. * and if there is no more active suspend request.
  604. */
  605. if (watchdog_running && !watchdog_suspended)
  606. watchdog_unpark_threads();
  607. watchdog_nmi_reconfigure();
  608. mutex_unlock(&watchdog_proc_mutex);
  609. put_online_cpus();
  610. }
  611. #ifdef CONFIG_SYSCTL
  612. /*
  613. * Update the run state of the lockup detectors.
  614. */
  615. static int proc_watchdog_update(void)
  616. {
  617. int err = 0;
  618. /*
  619. * Watchdog threads won't be started if they are already active.
  620. * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
  621. * care of this. If those threads are already active, the sample
  622. * period will be updated and the lockup detectors will be enabled
  623. * or disabled 'on the fly'.
  624. */
  625. if (watchdog_enabled && watchdog_thresh)
  626. err = watchdog_enable_all_cpus();
  627. else
  628. watchdog_disable_all_cpus();
  629. watchdog_nmi_reconfigure();
  630. return err;
  631. }
  632. /*
  633. * common function for watchdog, nmi_watchdog and soft_watchdog parameter
  634. *
  635. * caller | table->data points to | 'which' contains the flag(s)
  636. * -------------------|-----------------------|-----------------------------
  637. * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
  638. * | | with SOFT_WATCHDOG_ENABLED
  639. * -------------------|-----------------------|-----------------------------
  640. * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED
  641. * -------------------|-----------------------|-----------------------------
  642. * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
  643. */
  644. static int proc_watchdog_common(int which, struct ctl_table *table, int write,
  645. void __user *buffer, size_t *lenp, loff_t *ppos)
  646. {
  647. int err, old, new;
  648. int *watchdog_param = (int *)table->data;
  649. get_online_cpus();
  650. mutex_lock(&watchdog_proc_mutex);
  651. if (watchdog_suspended) {
  652. /* no parameter changes allowed while watchdog is suspended */
  653. err = -EAGAIN;
  654. goto out;
  655. }
  656. /*
  657. * If the parameter is being read return the state of the corresponding
  658. * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
  659. * run state of the lockup detectors.
  660. */
  661. if (!write) {
  662. *watchdog_param = (watchdog_enabled & which) != 0;
  663. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  664. } else {
  665. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  666. if (err)
  667. goto out;
  668. /*
  669. * There is a race window between fetching the current value
  670. * from 'watchdog_enabled' and storing the new value. During
  671. * this race window, watchdog_nmi_enable() can sneak in and
  672. * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
  673. * The 'cmpxchg' detects this race and the loop retries.
  674. */
  675. do {
  676. old = watchdog_enabled;
  677. /*
  678. * If the parameter value is not zero set the
  679. * corresponding bit(s), else clear it(them).
  680. */
  681. if (*watchdog_param)
  682. new = old | which;
  683. else
  684. new = old & ~which;
  685. } while (cmpxchg(&watchdog_enabled, old, new) != old);
  686. /*
  687. * Update the run state of the lockup detectors. There is _no_
  688. * need to check the value returned by proc_watchdog_update()
  689. * and to restore the previous value of 'watchdog_enabled' as
  690. * both lockup detectors are disabled if proc_watchdog_update()
  691. * returns an error.
  692. */
  693. if (old == new)
  694. goto out;
  695. err = proc_watchdog_update();
  696. }
  697. out:
  698. mutex_unlock(&watchdog_proc_mutex);
  699. put_online_cpus();
  700. return err;
  701. }
  702. /*
  703. * /proc/sys/kernel/watchdog
  704. */
  705. int proc_watchdog(struct ctl_table *table, int write,
  706. void __user *buffer, size_t *lenp, loff_t *ppos)
  707. {
  708. return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
  709. table, write, buffer, lenp, ppos);
  710. }
  711. /*
  712. * /proc/sys/kernel/nmi_watchdog
  713. */
  714. int proc_nmi_watchdog(struct ctl_table *table, int write,
  715. void __user *buffer, size_t *lenp, loff_t *ppos)
  716. {
  717. return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
  718. table, write, buffer, lenp, ppos);
  719. }
  720. /*
  721. * /proc/sys/kernel/soft_watchdog
  722. */
  723. int proc_soft_watchdog(struct ctl_table *table, int write,
  724. void __user *buffer, size_t *lenp, loff_t *ppos)
  725. {
  726. return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
  727. table, write, buffer, lenp, ppos);
  728. }
  729. /*
  730. * /proc/sys/kernel/watchdog_thresh
  731. */
  732. int proc_watchdog_thresh(struct ctl_table *table, int write,
  733. void __user *buffer, size_t *lenp, loff_t *ppos)
  734. {
  735. int err, old, new;
  736. get_online_cpus();
  737. mutex_lock(&watchdog_proc_mutex);
  738. if (watchdog_suspended) {
  739. /* no parameter changes allowed while watchdog is suspended */
  740. err = -EAGAIN;
  741. goto out;
  742. }
  743. old = ACCESS_ONCE(watchdog_thresh);
  744. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  745. if (err || !write)
  746. goto out;
  747. /*
  748. * Update the sample period. Restore on failure.
  749. */
  750. new = ACCESS_ONCE(watchdog_thresh);
  751. if (old == new)
  752. goto out;
  753. set_sample_period();
  754. err = proc_watchdog_update();
  755. if (err) {
  756. watchdog_thresh = old;
  757. set_sample_period();
  758. }
  759. out:
  760. mutex_unlock(&watchdog_proc_mutex);
  761. put_online_cpus();
  762. return err;
  763. }
  764. /*
  765. * The cpumask is the mask of possible cpus that the watchdog can run
  766. * on, not the mask of cpus it is actually running on. This allows the
  767. * user to specify a mask that will include cpus that have not yet
  768. * been brought online, if desired.
  769. */
  770. int proc_watchdog_cpumask(struct ctl_table *table, int write,
  771. void __user *buffer, size_t *lenp, loff_t *ppos)
  772. {
  773. int err;
  774. get_online_cpus();
  775. mutex_lock(&watchdog_proc_mutex);
  776. if (watchdog_suspended) {
  777. /* no parameter changes allowed while watchdog is suspended */
  778. err = -EAGAIN;
  779. goto out;
  780. }
  781. err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
  782. if (!err && write) {
  783. /* Remove impossible cpus to keep sysctl output cleaner. */
  784. cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
  785. cpu_possible_mask);
  786. if (watchdog_running) {
  787. /*
  788. * Failure would be due to being unable to allocate
  789. * a temporary cpumask, so we are likely not in a
  790. * position to do much else to make things better.
  791. */
  792. if (watchdog_update_cpus() != 0)
  793. pr_err("cpumask update failed\n");
  794. }
  795. watchdog_nmi_reconfigure();
  796. }
  797. out:
  798. mutex_unlock(&watchdog_proc_mutex);
  799. put_online_cpus();
  800. return err;
  801. }
  802. #endif /* CONFIG_SYSCTL */
  803. void __init lockup_detector_init(void)
  804. {
  805. set_sample_period();
  806. #ifdef CONFIG_NO_HZ_FULL
  807. if (tick_nohz_full_enabled()) {
  808. pr_info("Disabling watchdog on nohz_full cores by default\n");
  809. cpumask_copy(&watchdog_cpumask, housekeeping_mask);
  810. } else
  811. cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
  812. #else
  813. cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
  814. #endif
  815. if (watchdog_enabled)
  816. watchdog_enable_all_cpus();
  817. }