watchdog.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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/perf_event.h>
  25. #include <linux/kthread.h>
  26. /*
  27. * The run state of the lockup detectors is controlled by the content of the
  28. * 'watchdog_enabled' variable. Each lockup detector has its dedicated bit -
  29. * bit 0 for the hard lockup detector and bit 1 for the soft lockup detector.
  30. *
  31. * 'watchdog_user_enabled', 'nmi_watchdog_enabled' and 'soft_watchdog_enabled'
  32. * are variables that are only used as an 'interface' between the parameters
  33. * in /proc/sys/kernel and the internal state bits in 'watchdog_enabled'. The
  34. * 'watchdog_thresh' variable is handled differently because its value is not
  35. * boolean, and the lockup detectors are 'suspended' while 'watchdog_thresh'
  36. * is equal zero.
  37. */
  38. #define NMI_WATCHDOG_ENABLED_BIT 0
  39. #define SOFT_WATCHDOG_ENABLED_BIT 1
  40. #define NMI_WATCHDOG_ENABLED (1 << NMI_WATCHDOG_ENABLED_BIT)
  41. #define SOFT_WATCHDOG_ENABLED (1 << SOFT_WATCHDOG_ENABLED_BIT)
  42. static DEFINE_MUTEX(watchdog_proc_mutex);
  43. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  44. static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED;
  45. #else
  46. static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
  47. #endif
  48. int __read_mostly nmi_watchdog_enabled;
  49. int __read_mostly soft_watchdog_enabled;
  50. int __read_mostly watchdog_user_enabled;
  51. int __read_mostly watchdog_thresh = 10;
  52. #ifdef CONFIG_SMP
  53. int __read_mostly sysctl_softlockup_all_cpu_backtrace;
  54. int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
  55. #else
  56. #define sysctl_softlockup_all_cpu_backtrace 0
  57. #define sysctl_hardlockup_all_cpu_backtrace 0
  58. #endif
  59. static struct cpumask watchdog_cpumask __read_mostly;
  60. unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
  61. /* Helper for online, unparked cpus. */
  62. #define for_each_watchdog_cpu(cpu) \
  63. for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
  64. /*
  65. * The 'watchdog_running' variable is set to 1 when the watchdog threads
  66. * are registered/started and is set to 0 when the watchdog threads are
  67. * unregistered/stopped, so it is an indicator whether the threads exist.
  68. */
  69. static int __read_mostly watchdog_running;
  70. /*
  71. * If a subsystem has a need to deactivate the watchdog temporarily, it
  72. * can use the suspend/resume interface to achieve this. The content of
  73. * the 'watchdog_suspended' variable reflects this state. Existing threads
  74. * are parked/unparked by the lockup_detector_{suspend|resume} functions
  75. * (see comment blocks pertaining to those functions for further details).
  76. *
  77. * 'watchdog_suspended' also prevents threads from being registered/started
  78. * or unregistered/stopped via parameters in /proc/sys/kernel, so the state
  79. * of 'watchdog_running' cannot change while the watchdog is deactivated
  80. * temporarily (see related code in 'proc' handlers).
  81. */
  82. static int __read_mostly watchdog_suspended;
  83. static u64 __read_mostly sample_period;
  84. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  85. static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
  86. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  87. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  88. static DEFINE_PER_CPU(bool, soft_watchdog_warn);
  89. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
  90. static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
  91. static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
  92. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  93. static DEFINE_PER_CPU(bool, hard_watchdog_warn);
  94. static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
  95. static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
  96. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  97. #endif
  98. static unsigned long soft_lockup_nmi_warn;
  99. /* boot commands */
  100. /*
  101. * Should we panic when a soft-lockup or hard-lockup occurs:
  102. */
  103. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  104. unsigned int __read_mostly hardlockup_panic =
  105. CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
  106. static unsigned long hardlockup_allcpu_dumped;
  107. /*
  108. * We may not want to enable hard lockup detection by default in all cases,
  109. * for example when running the kernel as a guest on a hypervisor. In these
  110. * cases this function can be called to disable hard lockup detection. This
  111. * function should only be executed once by the boot processor before the
  112. * kernel command line parameters are parsed, because otherwise it is not
  113. * possible to override this in hardlockup_panic_setup().
  114. */
  115. void hardlockup_detector_disable(void)
  116. {
  117. watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
  118. }
  119. static int __init hardlockup_panic_setup(char *str)
  120. {
  121. if (!strncmp(str, "panic", 5))
  122. hardlockup_panic = 1;
  123. else if (!strncmp(str, "nopanic", 7))
  124. hardlockup_panic = 0;
  125. else if (!strncmp(str, "0", 1))
  126. watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
  127. else if (!strncmp(str, "1", 1))
  128. watchdog_enabled |= NMI_WATCHDOG_ENABLED;
  129. return 1;
  130. }
  131. __setup("nmi_watchdog=", hardlockup_panic_setup);
  132. #endif
  133. unsigned int __read_mostly softlockup_panic =
  134. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  135. static int __init softlockup_panic_setup(char *str)
  136. {
  137. softlockup_panic = simple_strtoul(str, NULL, 0);
  138. return 1;
  139. }
  140. __setup("softlockup_panic=", softlockup_panic_setup);
  141. static int __init nowatchdog_setup(char *str)
  142. {
  143. watchdog_enabled = 0;
  144. return 1;
  145. }
  146. __setup("nowatchdog", nowatchdog_setup);
  147. static int __init nosoftlockup_setup(char *str)
  148. {
  149. watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
  150. return 1;
  151. }
  152. __setup("nosoftlockup", nosoftlockup_setup);
  153. #ifdef CONFIG_SMP
  154. static int __init softlockup_all_cpu_backtrace_setup(char *str)
  155. {
  156. sysctl_softlockup_all_cpu_backtrace =
  157. !!simple_strtol(str, NULL, 0);
  158. return 1;
  159. }
  160. __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
  161. static int __init hardlockup_all_cpu_backtrace_setup(char *str)
  162. {
  163. sysctl_hardlockup_all_cpu_backtrace =
  164. !!simple_strtol(str, NULL, 0);
  165. return 1;
  166. }
  167. __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
  168. #endif
  169. /*
  170. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  171. * lockups can have false positives under extreme conditions. So we generally
  172. * want a higher threshold for soft lockups than for hard lockups. So we couple
  173. * the thresholds with a factor: we make the soft threshold twice the amount of
  174. * time the hard threshold is.
  175. */
  176. static int get_softlockup_thresh(void)
  177. {
  178. return watchdog_thresh * 2;
  179. }
  180. /*
  181. * Returns seconds, approximately. We don't need nanosecond
  182. * resolution, and we don't need to waste time with a big divide when
  183. * 2^30ns == 1.074s.
  184. */
  185. static unsigned long get_timestamp(void)
  186. {
  187. return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
  188. }
  189. static void set_sample_period(void)
  190. {
  191. /*
  192. * convert watchdog_thresh from seconds to ns
  193. * the divide by 5 is to give hrtimer several chances (two
  194. * or three with the current relation between the soft
  195. * and hard thresholds) to increment before the
  196. * hardlockup detector generates a warning
  197. */
  198. sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
  199. }
  200. /* Commands for resetting the watchdog */
  201. static void __touch_watchdog(void)
  202. {
  203. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  204. }
  205. /**
  206. * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
  207. *
  208. * Call when the scheduler may have stalled for legitimate reasons
  209. * preventing the watchdog task from executing - e.g. the scheduler
  210. * entering idle state. This should only be used for scheduler events.
  211. * Use touch_softlockup_watchdog() for everything else.
  212. */
  213. void touch_softlockup_watchdog_sched(void)
  214. {
  215. /*
  216. * Preemption can be enabled. It doesn't matter which CPU's timestamp
  217. * gets zeroed here, so use the raw_ operation.
  218. */
  219. raw_cpu_write(watchdog_touch_ts, 0);
  220. }
  221. void touch_softlockup_watchdog(void)
  222. {
  223. touch_softlockup_watchdog_sched();
  224. wq_watchdog_touch(raw_smp_processor_id());
  225. }
  226. EXPORT_SYMBOL(touch_softlockup_watchdog);
  227. void touch_all_softlockup_watchdogs(void)
  228. {
  229. int cpu;
  230. /*
  231. * this is done lockless
  232. * do we care if a 0 races with a timestamp?
  233. * all it means is the softlock check starts one cycle later
  234. */
  235. for_each_watchdog_cpu(cpu)
  236. per_cpu(watchdog_touch_ts, cpu) = 0;
  237. wq_watchdog_touch(-1);
  238. }
  239. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  240. void touch_nmi_watchdog(void)
  241. {
  242. /*
  243. * Using __raw here because some code paths have
  244. * preemption enabled. If preemption is enabled
  245. * then interrupts should be enabled too, in which
  246. * case we shouldn't have to worry about the watchdog
  247. * going off.
  248. */
  249. raw_cpu_write(watchdog_nmi_touch, true);
  250. touch_softlockup_watchdog();
  251. }
  252. EXPORT_SYMBOL(touch_nmi_watchdog);
  253. #endif
  254. void touch_softlockup_watchdog_sync(void)
  255. {
  256. __this_cpu_write(softlockup_touch_sync, true);
  257. __this_cpu_write(watchdog_touch_ts, 0);
  258. }
  259. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  260. /* watchdog detector functions */
  261. static bool is_hardlockup(void)
  262. {
  263. unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
  264. if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
  265. return true;
  266. __this_cpu_write(hrtimer_interrupts_saved, hrint);
  267. return false;
  268. }
  269. #endif
  270. static int is_softlockup(unsigned long touch_ts)
  271. {
  272. unsigned long now = get_timestamp();
  273. if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
  274. /* Warn about unreasonable delays. */
  275. if (time_after(now, touch_ts + get_softlockup_thresh()))
  276. return now - touch_ts;
  277. }
  278. return 0;
  279. }
  280. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  281. static struct perf_event_attr wd_hw_attr = {
  282. .type = PERF_TYPE_HARDWARE,
  283. .config = PERF_COUNT_HW_CPU_CYCLES,
  284. .size = sizeof(struct perf_event_attr),
  285. .pinned = 1,
  286. .disabled = 1,
  287. };
  288. /* Callback function for perf event subsystem */
  289. static void watchdog_overflow_callback(struct perf_event *event,
  290. struct perf_sample_data *data,
  291. struct pt_regs *regs)
  292. {
  293. /* Ensure the watchdog never gets throttled */
  294. event->hw.interrupts = 0;
  295. if (__this_cpu_read(watchdog_nmi_touch) == true) {
  296. __this_cpu_write(watchdog_nmi_touch, false);
  297. return;
  298. }
  299. /* check for a hardlockup
  300. * This is done by making sure our timer interrupt
  301. * is incrementing. The timer interrupt should have
  302. * fired multiple times before we overflow'd. If it hasn't
  303. * then this is a good indication the cpu is stuck
  304. */
  305. if (is_hardlockup()) {
  306. int this_cpu = smp_processor_id();
  307. /* only print hardlockups once */
  308. if (__this_cpu_read(hard_watchdog_warn) == true)
  309. return;
  310. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  311. print_modules();
  312. print_irqtrace_events(current);
  313. if (regs)
  314. show_regs(regs);
  315. else
  316. dump_stack();
  317. /*
  318. * Perform all-CPU dump only once to avoid multiple hardlockups
  319. * generating interleaving traces
  320. */
  321. if (sysctl_hardlockup_all_cpu_backtrace &&
  322. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  323. trigger_allbutself_cpu_backtrace();
  324. if (hardlockup_panic)
  325. nmi_panic(regs, "Hard LOCKUP");
  326. __this_cpu_write(hard_watchdog_warn, true);
  327. return;
  328. }
  329. __this_cpu_write(hard_watchdog_warn, false);
  330. return;
  331. }
  332. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  333. static void watchdog_interrupt_count(void)
  334. {
  335. __this_cpu_inc(hrtimer_interrupts);
  336. }
  337. static int watchdog_nmi_enable(unsigned int cpu);
  338. static void watchdog_nmi_disable(unsigned int cpu);
  339. static int watchdog_enable_all_cpus(void);
  340. static void watchdog_disable_all_cpus(void);
  341. /* watchdog kicker functions */
  342. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  343. {
  344. unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
  345. struct pt_regs *regs = get_irq_regs();
  346. int duration;
  347. int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
  348. /* kick the hardlockup detector */
  349. watchdog_interrupt_count();
  350. /* kick the softlockup detector */
  351. wake_up_process(__this_cpu_read(softlockup_watchdog));
  352. /* .. and repeat */
  353. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  354. if (touch_ts == 0) {
  355. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  356. /*
  357. * If the time stamp was touched atomically
  358. * make sure the scheduler tick is up to date.
  359. */
  360. __this_cpu_write(softlockup_touch_sync, false);
  361. sched_clock_tick();
  362. }
  363. /* Clear the guest paused flag on watchdog reset */
  364. kvm_check_and_clear_guest_paused();
  365. __touch_watchdog();
  366. return HRTIMER_RESTART;
  367. }
  368. /* check for a softlockup
  369. * This is done by making sure a high priority task is
  370. * being scheduled. The task touches the watchdog to
  371. * indicate it is getting cpu time. If it hasn't then
  372. * this is a good indication some task is hogging the cpu
  373. */
  374. duration = is_softlockup(touch_ts);
  375. if (unlikely(duration)) {
  376. /*
  377. * If a virtual machine is stopped by the host it can look to
  378. * the watchdog like a soft lockup, check to see if the host
  379. * stopped the vm before we issue the warning
  380. */
  381. if (kvm_check_and_clear_guest_paused())
  382. return HRTIMER_RESTART;
  383. /* only warn once */
  384. if (__this_cpu_read(soft_watchdog_warn) == true) {
  385. /*
  386. * When multiple processes are causing softlockups the
  387. * softlockup detector only warns on the first one
  388. * because the code relies on a full quiet cycle to
  389. * re-arm. The second process prevents the quiet cycle
  390. * and never gets reported. Use task pointers to detect
  391. * this.
  392. */
  393. if (__this_cpu_read(softlockup_task_ptr_saved) !=
  394. current) {
  395. __this_cpu_write(soft_watchdog_warn, false);
  396. __touch_watchdog();
  397. }
  398. return HRTIMER_RESTART;
  399. }
  400. if (softlockup_all_cpu_backtrace) {
  401. /* Prevent multiple soft-lockup reports if one cpu is already
  402. * engaged in dumping cpu back traces
  403. */
  404. if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
  405. /* Someone else will report us. Let's give up */
  406. __this_cpu_write(soft_watchdog_warn, true);
  407. return HRTIMER_RESTART;
  408. }
  409. }
  410. pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  411. smp_processor_id(), duration,
  412. current->comm, task_pid_nr(current));
  413. __this_cpu_write(softlockup_task_ptr_saved, current);
  414. print_modules();
  415. print_irqtrace_events(current);
  416. if (regs)
  417. show_regs(regs);
  418. else
  419. dump_stack();
  420. if (softlockup_all_cpu_backtrace) {
  421. /* Avoid generating two back traces for current
  422. * given that one is already made above
  423. */
  424. trigger_allbutself_cpu_backtrace();
  425. clear_bit(0, &soft_lockup_nmi_warn);
  426. /* Barrier to sync with other cpus */
  427. smp_mb__after_atomic();
  428. }
  429. add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
  430. if (softlockup_panic)
  431. panic("softlockup: hung tasks");
  432. __this_cpu_write(soft_watchdog_warn, true);
  433. } else
  434. __this_cpu_write(soft_watchdog_warn, false);
  435. return HRTIMER_RESTART;
  436. }
  437. static void watchdog_set_prio(unsigned int policy, unsigned int prio)
  438. {
  439. struct sched_param param = { .sched_priority = prio };
  440. sched_setscheduler(current, policy, &param);
  441. }
  442. static void watchdog_enable(unsigned int cpu)
  443. {
  444. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  445. /* kick off the timer for the hardlockup detector */
  446. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  447. hrtimer->function = watchdog_timer_fn;
  448. /* Enable the perf event */
  449. watchdog_nmi_enable(cpu);
  450. /* done here because hrtimer_start can only pin to smp_processor_id() */
  451. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  452. HRTIMER_MODE_REL_PINNED);
  453. /* initialize timestamp */
  454. watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
  455. __touch_watchdog();
  456. }
  457. static void watchdog_disable(unsigned int cpu)
  458. {
  459. struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
  460. watchdog_set_prio(SCHED_NORMAL, 0);
  461. hrtimer_cancel(hrtimer);
  462. /* disable the perf event */
  463. watchdog_nmi_disable(cpu);
  464. }
  465. static void watchdog_cleanup(unsigned int cpu, bool online)
  466. {
  467. watchdog_disable(cpu);
  468. }
  469. static int watchdog_should_run(unsigned int cpu)
  470. {
  471. return __this_cpu_read(hrtimer_interrupts) !=
  472. __this_cpu_read(soft_lockup_hrtimer_cnt);
  473. }
  474. /*
  475. * The watchdog thread function - touches the timestamp.
  476. *
  477. * It only runs once every sample_period seconds (4 seconds by
  478. * default) to reset the softlockup timestamp. If this gets delayed
  479. * for more than 2*watchdog_thresh seconds then the debug-printout
  480. * triggers in watchdog_timer_fn().
  481. */
  482. static void watchdog(unsigned int cpu)
  483. {
  484. __this_cpu_write(soft_lockup_hrtimer_cnt,
  485. __this_cpu_read(hrtimer_interrupts));
  486. __touch_watchdog();
  487. /*
  488. * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
  489. * failure path. Check for failures that can occur asynchronously -
  490. * for example, when CPUs are on-lined - and shut down the hardware
  491. * perf event on each CPU accordingly.
  492. *
  493. * The only non-obvious place this bit can be cleared is through
  494. * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a
  495. * pr_info here would be too noisy as it would result in a message
  496. * every few seconds if the hardlockup was disabled but the softlockup
  497. * enabled.
  498. */
  499. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  500. watchdog_nmi_disable(cpu);
  501. }
  502. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  503. /*
  504. * People like the simple clean cpu node info on boot.
  505. * Reduce the watchdog noise by only printing messages
  506. * that are different from what cpu0 displayed.
  507. */
  508. static unsigned long cpu0_err;
  509. static int watchdog_nmi_enable(unsigned int cpu)
  510. {
  511. struct perf_event_attr *wd_attr;
  512. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  513. /* nothing to do if the hard lockup detector is disabled */
  514. if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
  515. goto out;
  516. /* is it already setup and enabled? */
  517. if (event && event->state > PERF_EVENT_STATE_OFF)
  518. goto out;
  519. /* it is setup but not enabled */
  520. if (event != NULL)
  521. goto out_enable;
  522. wd_attr = &wd_hw_attr;
  523. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  524. /* Try to register using hardware perf events */
  525. event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
  526. /* save cpu0 error for future comparision */
  527. if (cpu == 0 && IS_ERR(event))
  528. cpu0_err = PTR_ERR(event);
  529. if (!IS_ERR(event)) {
  530. /* only print for cpu0 or different than cpu0 */
  531. if (cpu == 0 || cpu0_err)
  532. pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
  533. goto out_save;
  534. }
  535. /*
  536. * Disable the hard lockup detector if _any_ CPU fails to set up
  537. * set up the hardware perf event. The watchdog() function checks
  538. * the NMI_WATCHDOG_ENABLED bit periodically.
  539. *
  540. * The barriers are for syncing up watchdog_enabled across all the
  541. * cpus, as clear_bit() does not use barriers.
  542. */
  543. smp_mb__before_atomic();
  544. clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
  545. smp_mb__after_atomic();
  546. /* skip displaying the same error again */
  547. if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
  548. return PTR_ERR(event);
  549. /* vary the KERN level based on the returned errno */
  550. if (PTR_ERR(event) == -EOPNOTSUPP)
  551. pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
  552. else if (PTR_ERR(event) == -ENOENT)
  553. pr_warn("disabled (cpu%i): hardware events not enabled\n",
  554. cpu);
  555. else
  556. pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
  557. cpu, PTR_ERR(event));
  558. pr_info("Shutting down hard lockup detector on all cpus\n");
  559. return PTR_ERR(event);
  560. /* success path */
  561. out_save:
  562. per_cpu(watchdog_ev, cpu) = event;
  563. out_enable:
  564. perf_event_enable(per_cpu(watchdog_ev, cpu));
  565. out:
  566. return 0;
  567. }
  568. static void watchdog_nmi_disable(unsigned int cpu)
  569. {
  570. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  571. if (event) {
  572. perf_event_disable(event);
  573. per_cpu(watchdog_ev, cpu) = NULL;
  574. /* should be in cleanup, but blocks oprofile */
  575. perf_event_release_kernel(event);
  576. }
  577. if (cpu == 0) {
  578. /* watchdog_nmi_enable() expects this to be zero initially. */
  579. cpu0_err = 0;
  580. }
  581. }
  582. #else
  583. static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
  584. static void watchdog_nmi_disable(unsigned int cpu) { return; }
  585. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  586. static struct smp_hotplug_thread watchdog_threads = {
  587. .store = &softlockup_watchdog,
  588. .thread_should_run = watchdog_should_run,
  589. .thread_fn = watchdog,
  590. .thread_comm = "watchdog/%u",
  591. .setup = watchdog_enable,
  592. .cleanup = watchdog_cleanup,
  593. .park = watchdog_disable,
  594. .unpark = watchdog_enable,
  595. };
  596. /*
  597. * park all watchdog threads that are specified in 'watchdog_cpumask'
  598. *
  599. * This function returns an error if kthread_park() of a watchdog thread
  600. * fails. In this situation, the watchdog threads of some CPUs can already
  601. * be parked and the watchdog threads of other CPUs can still be runnable.
  602. * Callers are expected to handle this special condition as appropriate in
  603. * their context.
  604. *
  605. * This function may only be called in a context that is protected against
  606. * races with CPU hotplug - for example, via get_online_cpus().
  607. */
  608. static int watchdog_park_threads(void)
  609. {
  610. int cpu, ret = 0;
  611. for_each_watchdog_cpu(cpu) {
  612. ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
  613. if (ret)
  614. break;
  615. }
  616. return ret;
  617. }
  618. /*
  619. * unpark all watchdog threads that are specified in 'watchdog_cpumask'
  620. *
  621. * This function may only be called in a context that is protected against
  622. * races with CPU hotplug - for example, via get_online_cpus().
  623. */
  624. static void watchdog_unpark_threads(void)
  625. {
  626. int cpu;
  627. for_each_watchdog_cpu(cpu)
  628. kthread_unpark(per_cpu(softlockup_watchdog, cpu));
  629. }
  630. /*
  631. * Suspend the hard and soft lockup detector by parking the watchdog threads.
  632. */
  633. int lockup_detector_suspend(void)
  634. {
  635. int ret = 0;
  636. get_online_cpus();
  637. mutex_lock(&watchdog_proc_mutex);
  638. /*
  639. * Multiple suspend requests can be active in parallel (counted by
  640. * the 'watchdog_suspended' variable). If the watchdog threads are
  641. * running, the first caller takes care that they will be parked.
  642. * The state of 'watchdog_running' cannot change while a suspend
  643. * request is active (see related code in 'proc' handlers).
  644. */
  645. if (watchdog_running && !watchdog_suspended)
  646. ret = watchdog_park_threads();
  647. if (ret == 0)
  648. watchdog_suspended++;
  649. else {
  650. watchdog_disable_all_cpus();
  651. pr_err("Failed to suspend lockup detectors, disabled\n");
  652. watchdog_enabled = 0;
  653. }
  654. mutex_unlock(&watchdog_proc_mutex);
  655. return ret;
  656. }
  657. /*
  658. * Resume the hard and soft lockup detector by unparking the watchdog threads.
  659. */
  660. void lockup_detector_resume(void)
  661. {
  662. mutex_lock(&watchdog_proc_mutex);
  663. watchdog_suspended--;
  664. /*
  665. * The watchdog threads are unparked if they were previously running
  666. * and if there is no more active suspend request.
  667. */
  668. if (watchdog_running && !watchdog_suspended)
  669. watchdog_unpark_threads();
  670. mutex_unlock(&watchdog_proc_mutex);
  671. put_online_cpus();
  672. }
  673. static int update_watchdog_all_cpus(void)
  674. {
  675. int ret;
  676. ret = watchdog_park_threads();
  677. if (ret)
  678. return ret;
  679. watchdog_unpark_threads();
  680. return 0;
  681. }
  682. static int watchdog_enable_all_cpus(void)
  683. {
  684. int err = 0;
  685. if (!watchdog_running) {
  686. err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
  687. &watchdog_cpumask);
  688. if (err)
  689. pr_err("Failed to create watchdog threads, disabled\n");
  690. else
  691. watchdog_running = 1;
  692. } else {
  693. /*
  694. * Enable/disable the lockup detectors or
  695. * change the sample period 'on the fly'.
  696. */
  697. err = update_watchdog_all_cpus();
  698. if (err) {
  699. watchdog_disable_all_cpus();
  700. pr_err("Failed to update lockup detectors, disabled\n");
  701. }
  702. }
  703. if (err)
  704. watchdog_enabled = 0;
  705. return err;
  706. }
  707. static void watchdog_disable_all_cpus(void)
  708. {
  709. if (watchdog_running) {
  710. watchdog_running = 0;
  711. smpboot_unregister_percpu_thread(&watchdog_threads);
  712. }
  713. }
  714. #ifdef CONFIG_SYSCTL
  715. /*
  716. * Update the run state of the lockup detectors.
  717. */
  718. static int proc_watchdog_update(void)
  719. {
  720. int err = 0;
  721. /*
  722. * Watchdog threads won't be started if they are already active.
  723. * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
  724. * care of this. If those threads are already active, the sample
  725. * period will be updated and the lockup detectors will be enabled
  726. * or disabled 'on the fly'.
  727. */
  728. if (watchdog_enabled && watchdog_thresh)
  729. err = watchdog_enable_all_cpus();
  730. else
  731. watchdog_disable_all_cpus();
  732. return err;
  733. }
  734. /*
  735. * common function for watchdog, nmi_watchdog and soft_watchdog parameter
  736. *
  737. * caller | table->data points to | 'which' contains the flag(s)
  738. * -------------------|-----------------------|-----------------------------
  739. * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
  740. * | | with SOFT_WATCHDOG_ENABLED
  741. * -------------------|-----------------------|-----------------------------
  742. * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED
  743. * -------------------|-----------------------|-----------------------------
  744. * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
  745. */
  746. static int proc_watchdog_common(int which, struct ctl_table *table, int write,
  747. void __user *buffer, size_t *lenp, loff_t *ppos)
  748. {
  749. int err, old, new;
  750. int *watchdog_param = (int *)table->data;
  751. get_online_cpus();
  752. mutex_lock(&watchdog_proc_mutex);
  753. if (watchdog_suspended) {
  754. /* no parameter changes allowed while watchdog is suspended */
  755. err = -EAGAIN;
  756. goto out;
  757. }
  758. /*
  759. * If the parameter is being read return the state of the corresponding
  760. * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
  761. * run state of the lockup detectors.
  762. */
  763. if (!write) {
  764. *watchdog_param = (watchdog_enabled & which) != 0;
  765. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  766. } else {
  767. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  768. if (err)
  769. goto out;
  770. /*
  771. * There is a race window between fetching the current value
  772. * from 'watchdog_enabled' and storing the new value. During
  773. * this race window, watchdog_nmi_enable() can sneak in and
  774. * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
  775. * The 'cmpxchg' detects this race and the loop retries.
  776. */
  777. do {
  778. old = watchdog_enabled;
  779. /*
  780. * If the parameter value is not zero set the
  781. * corresponding bit(s), else clear it(them).
  782. */
  783. if (*watchdog_param)
  784. new = old | which;
  785. else
  786. new = old & ~which;
  787. } while (cmpxchg(&watchdog_enabled, old, new) != old);
  788. /*
  789. * Update the run state of the lockup detectors. There is _no_
  790. * need to check the value returned by proc_watchdog_update()
  791. * and to restore the previous value of 'watchdog_enabled' as
  792. * both lockup detectors are disabled if proc_watchdog_update()
  793. * returns an error.
  794. */
  795. if (old == new)
  796. goto out;
  797. err = proc_watchdog_update();
  798. }
  799. out:
  800. mutex_unlock(&watchdog_proc_mutex);
  801. put_online_cpus();
  802. return err;
  803. }
  804. /*
  805. * /proc/sys/kernel/watchdog
  806. */
  807. int proc_watchdog(struct ctl_table *table, int write,
  808. void __user *buffer, size_t *lenp, loff_t *ppos)
  809. {
  810. return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
  811. table, write, buffer, lenp, ppos);
  812. }
  813. /*
  814. * /proc/sys/kernel/nmi_watchdog
  815. */
  816. int proc_nmi_watchdog(struct ctl_table *table, int write,
  817. void __user *buffer, size_t *lenp, loff_t *ppos)
  818. {
  819. return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
  820. table, write, buffer, lenp, ppos);
  821. }
  822. /*
  823. * /proc/sys/kernel/soft_watchdog
  824. */
  825. int proc_soft_watchdog(struct ctl_table *table, int write,
  826. void __user *buffer, size_t *lenp, loff_t *ppos)
  827. {
  828. return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
  829. table, write, buffer, lenp, ppos);
  830. }
  831. /*
  832. * /proc/sys/kernel/watchdog_thresh
  833. */
  834. int proc_watchdog_thresh(struct ctl_table *table, int write,
  835. void __user *buffer, size_t *lenp, loff_t *ppos)
  836. {
  837. int err, old, new;
  838. get_online_cpus();
  839. mutex_lock(&watchdog_proc_mutex);
  840. if (watchdog_suspended) {
  841. /* no parameter changes allowed while watchdog is suspended */
  842. err = -EAGAIN;
  843. goto out;
  844. }
  845. old = ACCESS_ONCE(watchdog_thresh);
  846. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  847. if (err || !write)
  848. goto out;
  849. /*
  850. * Update the sample period. Restore on failure.
  851. */
  852. new = ACCESS_ONCE(watchdog_thresh);
  853. if (old == new)
  854. goto out;
  855. set_sample_period();
  856. err = proc_watchdog_update();
  857. if (err) {
  858. watchdog_thresh = old;
  859. set_sample_period();
  860. }
  861. out:
  862. mutex_unlock(&watchdog_proc_mutex);
  863. put_online_cpus();
  864. return err;
  865. }
  866. /*
  867. * The cpumask is the mask of possible cpus that the watchdog can run
  868. * on, not the mask of cpus it is actually running on. This allows the
  869. * user to specify a mask that will include cpus that have not yet
  870. * been brought online, if desired.
  871. */
  872. int proc_watchdog_cpumask(struct ctl_table *table, int write,
  873. void __user *buffer, size_t *lenp, loff_t *ppos)
  874. {
  875. int err;
  876. get_online_cpus();
  877. mutex_lock(&watchdog_proc_mutex);
  878. if (watchdog_suspended) {
  879. /* no parameter changes allowed while watchdog is suspended */
  880. err = -EAGAIN;
  881. goto out;
  882. }
  883. err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
  884. if (!err && write) {
  885. /* Remove impossible cpus to keep sysctl output cleaner. */
  886. cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
  887. cpu_possible_mask);
  888. if (watchdog_running) {
  889. /*
  890. * Failure would be due to being unable to allocate
  891. * a temporary cpumask, so we are likely not in a
  892. * position to do much else to make things better.
  893. */
  894. if (smpboot_update_cpumask_percpu_thread(
  895. &watchdog_threads, &watchdog_cpumask) != 0)
  896. pr_err("cpumask update failed\n");
  897. }
  898. }
  899. out:
  900. mutex_unlock(&watchdog_proc_mutex);
  901. put_online_cpus();
  902. return err;
  903. }
  904. #endif /* CONFIG_SYSCTL */
  905. void __init lockup_detector_init(void)
  906. {
  907. set_sample_period();
  908. #ifdef CONFIG_NO_HZ_FULL
  909. if (tick_nohz_full_enabled()) {
  910. pr_info("Disabling watchdog on nohz_full cores by default\n");
  911. cpumask_copy(&watchdog_cpumask, housekeeping_mask);
  912. } else
  913. cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
  914. #else
  915. cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
  916. #endif
  917. if (watchdog_enabled)
  918. watchdog_enable_all_cpus();
  919. }