watchdog.c 24 KB

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