watchdog.c 23 KB

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