watchdog.c 22 KB

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