watchdog.c 23 KB

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