watchdog.c 26 KB

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