watchdog.c 24 KB

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