watchdog.c 30 KB

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