watchdog.c 29 KB

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