watchdog.c 23 KB

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