watchdog.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Detect hard and soft lockups on a system
  4. *
  5. * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  6. *
  7. * Note: Most of this code is borrowed heavily from the original softlockup
  8. * detector, so thanks to Ingo for the initial implementation.
  9. * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
  10. * to those contributors as well.
  11. */
  12. #define pr_fmt(fmt) "watchdog: " fmt
  13. #include <linux/cpu.h>
  14. #include <linux/init.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdesc.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/kvm_para.h>
  19. #include <linux/math64.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/nmi.h>
  23. #include <linux/stop_machine.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/tick.h>
  26. #include <linux/sched/clock.h>
  27. #include <linux/sched/debug.h>
  28. #include <linux/sched/isolation.h>
  29. #include <asm/irq_regs.h>
  30. static DEFINE_MUTEX(watchdog_mutex);
  31. #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HARDLOCKUP_DETECTOR_SPARC64)
  32. # define WATCHDOG_HARDLOCKUP_DEFAULT 1
  33. #else
  34. # define WATCHDOG_HARDLOCKUP_DEFAULT 0
  35. #endif
  36. #define NUM_SAMPLE_PERIODS 5
  37. unsigned long __read_mostly watchdog_enabled;
  38. int __read_mostly watchdog_user_enabled = 1;
  39. static int __read_mostly watchdog_hardlockup_user_enabled = WATCHDOG_HARDLOCKUP_DEFAULT;
  40. static int __read_mostly watchdog_softlockup_user_enabled = 1;
  41. int __read_mostly watchdog_thresh = 10;
  42. static int __read_mostly watchdog_hardlockup_available;
  43. struct cpumask watchdog_cpumask __read_mostly;
  44. unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
  45. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  46. # ifdef CONFIG_SMP
  47. int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
  48. # endif /* CONFIG_SMP */
  49. /*
  50. * Should we panic when a soft-lockup or hard-lockup occurs:
  51. */
  52. unsigned int __read_mostly hardlockup_panic =
  53. IS_ENABLED(CONFIG_BOOTPARAM_HARDLOCKUP_PANIC);
  54. /*
  55. * We may not want to enable hard lockup detection by default in all cases,
  56. * for example when running the kernel as a guest on a hypervisor. In these
  57. * cases this function can be called to disable hard lockup detection. This
  58. * function should only be executed once by the boot processor before the
  59. * kernel command line parameters are parsed, because otherwise it is not
  60. * possible to override this in hardlockup_panic_setup().
  61. */
  62. void __init hardlockup_detector_disable(void)
  63. {
  64. watchdog_hardlockup_user_enabled = 0;
  65. }
  66. static int __init hardlockup_panic_setup(char *str)
  67. {
  68. next:
  69. if (!strncmp(str, "panic", 5))
  70. hardlockup_panic = 1;
  71. else if (!strncmp(str, "nopanic", 7))
  72. hardlockup_panic = 0;
  73. else if (!strncmp(str, "0", 1))
  74. watchdog_hardlockup_user_enabled = 0;
  75. else if (!strncmp(str, "1", 1))
  76. watchdog_hardlockup_user_enabled = 1;
  77. else if (!strncmp(str, "r", 1))
  78. hardlockup_config_perf_event(str + 1);
  79. while (*(str++)) {
  80. if (*str == ',') {
  81. str++;
  82. goto next;
  83. }
  84. }
  85. return 1;
  86. }
  87. __setup("nmi_watchdog=", hardlockup_panic_setup);
  88. #endif /* CONFIG_HARDLOCKUP_DETECTOR */
  89. #if defined(CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER)
  90. static DEFINE_PER_CPU(atomic_t, hrtimer_interrupts);
  91. static DEFINE_PER_CPU(int, hrtimer_interrupts_saved);
  92. static DEFINE_PER_CPU(bool, watchdog_hardlockup_warned);
  93. static DEFINE_PER_CPU(bool, watchdog_hardlockup_touched);
  94. static unsigned long hard_lockup_nmi_warn;
  95. notrace void arch_touch_nmi_watchdog(void)
  96. {
  97. /*
  98. * Using __raw here because some code paths have
  99. * preemption enabled. If preemption is enabled
  100. * then interrupts should be enabled too, in which
  101. * case we shouldn't have to worry about the watchdog
  102. * going off.
  103. */
  104. raw_cpu_write(watchdog_hardlockup_touched, true);
  105. }
  106. EXPORT_SYMBOL(arch_touch_nmi_watchdog);
  107. void watchdog_hardlockup_touch_cpu(unsigned int cpu)
  108. {
  109. per_cpu(watchdog_hardlockup_touched, cpu) = true;
  110. }
  111. static bool is_hardlockup(unsigned int cpu)
  112. {
  113. int hrint = atomic_read(&per_cpu(hrtimer_interrupts, cpu));
  114. if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
  115. return true;
  116. /*
  117. * NOTE: we don't need any fancy atomic_t or READ_ONCE/WRITE_ONCE
  118. * for hrtimer_interrupts_saved. hrtimer_interrupts_saved is
  119. * written/read by a single CPU.
  120. */
  121. per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
  122. return false;
  123. }
  124. static void watchdog_hardlockup_kick(void)
  125. {
  126. int new_interrupts;
  127. new_interrupts = atomic_inc_return(this_cpu_ptr(&hrtimer_interrupts));
  128. watchdog_buddy_check_hardlockup(new_interrupts);
  129. }
  130. void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs)
  131. {
  132. if (per_cpu(watchdog_hardlockup_touched, cpu)) {
  133. per_cpu(watchdog_hardlockup_touched, cpu) = false;
  134. return;
  135. }
  136. /*
  137. * Check for a hardlockup by making sure the CPU's timer
  138. * interrupt is incrementing. The timer interrupt should have
  139. * fired multiple times before we overflow'd. If it hasn't
  140. * then this is a good indication the cpu is stuck
  141. */
  142. if (is_hardlockup(cpu)) {
  143. unsigned int this_cpu = smp_processor_id();
  144. unsigned long flags;
  145. /* Only print hardlockups once. */
  146. if (per_cpu(watchdog_hardlockup_warned, cpu))
  147. return;
  148. /*
  149. * Prevent multiple hard-lockup reports if one cpu is already
  150. * engaged in dumping all cpu back traces.
  151. */
  152. if (sysctl_hardlockup_all_cpu_backtrace) {
  153. if (test_and_set_bit_lock(0, &hard_lockup_nmi_warn))
  154. return;
  155. }
  156. /*
  157. * NOTE: we call printk_cpu_sync_get_irqsave() after printing
  158. * the lockup message. While it would be nice to serialize
  159. * that printout, we really want to make sure that if some
  160. * other CPU somehow locked up while holding the lock associated
  161. * with printk_cpu_sync_get_irqsave() that we can still at least
  162. * get the message about the lockup out.
  163. */
  164. pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", cpu);
  165. printk_cpu_sync_get_irqsave(flags);
  166. print_modules();
  167. print_irqtrace_events(current);
  168. if (cpu == this_cpu) {
  169. if (regs)
  170. show_regs(regs);
  171. else
  172. dump_stack();
  173. printk_cpu_sync_put_irqrestore(flags);
  174. } else {
  175. printk_cpu_sync_put_irqrestore(flags);
  176. trigger_single_cpu_backtrace(cpu);
  177. }
  178. if (sysctl_hardlockup_all_cpu_backtrace) {
  179. trigger_allbutcpu_cpu_backtrace(cpu);
  180. if (!hardlockup_panic)
  181. clear_bit_unlock(0, &hard_lockup_nmi_warn);
  182. }
  183. if (hardlockup_panic)
  184. nmi_panic(regs, "Hard LOCKUP");
  185. per_cpu(watchdog_hardlockup_warned, cpu) = true;
  186. } else {
  187. per_cpu(watchdog_hardlockup_warned, cpu) = false;
  188. }
  189. }
  190. #else /* CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
  191. static inline void watchdog_hardlockup_kick(void) { }
  192. #endif /* !CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
  193. /*
  194. * These functions can be overridden based on the configured hardlockdup detector.
  195. *
  196. * watchdog_hardlockup_enable/disable can be implemented to start and stop when
  197. * softlockup watchdog start and stop. The detector must select the
  198. * SOFTLOCKUP_DETECTOR Kconfig.
  199. */
  200. void __weak watchdog_hardlockup_enable(unsigned int cpu) { }
  201. void __weak watchdog_hardlockup_disable(unsigned int cpu) { }
  202. /*
  203. * Watchdog-detector specific API.
  204. *
  205. * Return 0 when hardlockup watchdog is available, negative value otherwise.
  206. * Note that the negative value means that a delayed probe might
  207. * succeed later.
  208. */
  209. int __weak __init watchdog_hardlockup_probe(void)
  210. {
  211. return -ENODEV;
  212. }
  213. /**
  214. * watchdog_hardlockup_stop - Stop the watchdog for reconfiguration
  215. *
  216. * The reconfiguration steps are:
  217. * watchdog_hardlockup_stop();
  218. * update_variables();
  219. * watchdog_hardlockup_start();
  220. */
  221. void __weak watchdog_hardlockup_stop(void) { }
  222. /**
  223. * watchdog_hardlockup_start - Start the watchdog after reconfiguration
  224. *
  225. * Counterpart to watchdog_hardlockup_stop().
  226. *
  227. * The following variables have been updated in update_variables() and
  228. * contain the currently valid configuration:
  229. * - watchdog_enabled
  230. * - watchdog_thresh
  231. * - watchdog_cpumask
  232. */
  233. void __weak watchdog_hardlockup_start(void) { }
  234. /**
  235. * lockup_detector_update_enable - Update the sysctl enable bit
  236. *
  237. * Caller needs to make sure that the hard watchdogs are off, so this
  238. * can't race with watchdog_hardlockup_disable().
  239. */
  240. static void lockup_detector_update_enable(void)
  241. {
  242. watchdog_enabled = 0;
  243. if (!watchdog_user_enabled)
  244. return;
  245. if (watchdog_hardlockup_available && watchdog_hardlockup_user_enabled)
  246. watchdog_enabled |= WATCHDOG_HARDLOCKUP_ENABLED;
  247. if (watchdog_softlockup_user_enabled)
  248. watchdog_enabled |= WATCHDOG_SOFTOCKUP_ENABLED;
  249. }
  250. #ifdef CONFIG_SOFTLOCKUP_DETECTOR
  251. /*
  252. * Delay the soflockup report when running a known slow code.
  253. * It does _not_ affect the timestamp of the last successdul reschedule.
  254. */
  255. #define SOFTLOCKUP_DELAY_REPORT ULONG_MAX
  256. #ifdef CONFIG_SMP
  257. int __read_mostly sysctl_softlockup_all_cpu_backtrace;
  258. #endif
  259. static struct cpumask watchdog_allowed_mask __read_mostly;
  260. /* Global variables, exported for sysctl */
  261. unsigned int __read_mostly softlockup_panic =
  262. IS_ENABLED(CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC);
  263. static bool softlockup_initialized __read_mostly;
  264. static u64 __read_mostly sample_period;
  265. /* Timestamp taken after the last successful reschedule. */
  266. static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
  267. /* Timestamp of the last softlockup report. */
  268. static DEFINE_PER_CPU(unsigned long, watchdog_report_ts);
  269. static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
  270. static DEFINE_PER_CPU(bool, softlockup_touch_sync);
  271. static unsigned long soft_lockup_nmi_warn;
  272. static int __init softlockup_panic_setup(char *str)
  273. {
  274. softlockup_panic = simple_strtoul(str, NULL, 0);
  275. return 1;
  276. }
  277. __setup("softlockup_panic=", softlockup_panic_setup);
  278. static int __init nowatchdog_setup(char *str)
  279. {
  280. watchdog_user_enabled = 0;
  281. return 1;
  282. }
  283. __setup("nowatchdog", nowatchdog_setup);
  284. static int __init nosoftlockup_setup(char *str)
  285. {
  286. watchdog_softlockup_user_enabled = 0;
  287. return 1;
  288. }
  289. __setup("nosoftlockup", nosoftlockup_setup);
  290. static int __init watchdog_thresh_setup(char *str)
  291. {
  292. get_option(&str, &watchdog_thresh);
  293. return 1;
  294. }
  295. __setup("watchdog_thresh=", watchdog_thresh_setup);
  296. static void __lockup_detector_cleanup(void);
  297. #ifdef CONFIG_SOFTLOCKUP_DETECTOR_INTR_STORM
  298. enum stats_per_group {
  299. STATS_SYSTEM,
  300. STATS_SOFTIRQ,
  301. STATS_HARDIRQ,
  302. STATS_IDLE,
  303. NUM_STATS_PER_GROUP,
  304. };
  305. static const enum cpu_usage_stat tracked_stats[NUM_STATS_PER_GROUP] = {
  306. CPUTIME_SYSTEM,
  307. CPUTIME_SOFTIRQ,
  308. CPUTIME_IRQ,
  309. CPUTIME_IDLE,
  310. };
  311. static DEFINE_PER_CPU(u16, cpustat_old[NUM_STATS_PER_GROUP]);
  312. static DEFINE_PER_CPU(u8, cpustat_util[NUM_SAMPLE_PERIODS][NUM_STATS_PER_GROUP]);
  313. static DEFINE_PER_CPU(u8, cpustat_tail);
  314. /*
  315. * We don't need nanosecond resolution. A granularity of 16ms is
  316. * sufficient for our precision, allowing us to use u16 to store
  317. * cpustats, which will roll over roughly every ~1000 seconds.
  318. * 2^24 ~= 16 * 10^6
  319. */
  320. static u16 get_16bit_precision(u64 data_ns)
  321. {
  322. return data_ns >> 24LL; /* 2^24ns ~= 16.8ms */
  323. }
  324. static void update_cpustat(void)
  325. {
  326. int i;
  327. u8 util;
  328. u16 old_stat, new_stat;
  329. struct kernel_cpustat kcpustat;
  330. u64 *cpustat = kcpustat.cpustat;
  331. u8 tail = __this_cpu_read(cpustat_tail);
  332. u16 sample_period_16 = get_16bit_precision(sample_period);
  333. kcpustat_cpu_fetch(&kcpustat, smp_processor_id());
  334. for (i = 0; i < NUM_STATS_PER_GROUP; i++) {
  335. old_stat = __this_cpu_read(cpustat_old[i]);
  336. new_stat = get_16bit_precision(cpustat[tracked_stats[i]]);
  337. util = DIV_ROUND_UP(100 * (new_stat - old_stat), sample_period_16);
  338. __this_cpu_write(cpustat_util[tail][i], util);
  339. __this_cpu_write(cpustat_old[i], new_stat);
  340. }
  341. __this_cpu_write(cpustat_tail, (tail + 1) % NUM_SAMPLE_PERIODS);
  342. }
  343. static void print_cpustat(void)
  344. {
  345. int i, group;
  346. u8 tail = __this_cpu_read(cpustat_tail);
  347. u64 sample_period_second = sample_period;
  348. do_div(sample_period_second, NSEC_PER_SEC);
  349. /*
  350. * Outputting the "watchdog" prefix on every line is redundant and not
  351. * concise, and the original alarm information is sufficient for
  352. * positioning in logs, hence here printk() is used instead of pr_crit().
  353. */
  354. printk(KERN_CRIT "CPU#%d Utilization every %llus during lockup:\n",
  355. smp_processor_id(), sample_period_second);
  356. for (i = 0; i < NUM_SAMPLE_PERIODS; i++) {
  357. group = (tail + i) % NUM_SAMPLE_PERIODS;
  358. printk(KERN_CRIT "\t#%d: %3u%% system,\t%3u%% softirq,\t"
  359. "%3u%% hardirq,\t%3u%% idle\n", i + 1,
  360. __this_cpu_read(cpustat_util[group][STATS_SYSTEM]),
  361. __this_cpu_read(cpustat_util[group][STATS_SOFTIRQ]),
  362. __this_cpu_read(cpustat_util[group][STATS_HARDIRQ]),
  363. __this_cpu_read(cpustat_util[group][STATS_IDLE]));
  364. }
  365. }
  366. #define HARDIRQ_PERCENT_THRESH 50
  367. #define NUM_HARDIRQ_REPORT 5
  368. struct irq_counts {
  369. int irq;
  370. u32 counts;
  371. };
  372. static DEFINE_PER_CPU(bool, snapshot_taken);
  373. /* Tabulate the most frequent interrupts. */
  374. static void tabulate_irq_count(struct irq_counts *irq_counts, int irq, u32 counts, int rank)
  375. {
  376. int i;
  377. struct irq_counts new_count = {irq, counts};
  378. for (i = 0; i < rank; i++) {
  379. if (counts > irq_counts[i].counts)
  380. swap(new_count, irq_counts[i]);
  381. }
  382. }
  383. /*
  384. * If the hardirq time exceeds HARDIRQ_PERCENT_THRESH% of the sample_period,
  385. * then the cause of softlockup might be interrupt storm. In this case, it
  386. * would be useful to start interrupt counting.
  387. */
  388. static bool need_counting_irqs(void)
  389. {
  390. u8 util;
  391. int tail = __this_cpu_read(cpustat_tail);
  392. tail = (tail + NUM_HARDIRQ_REPORT - 1) % NUM_HARDIRQ_REPORT;
  393. util = __this_cpu_read(cpustat_util[tail][STATS_HARDIRQ]);
  394. return util > HARDIRQ_PERCENT_THRESH;
  395. }
  396. static void start_counting_irqs(void)
  397. {
  398. if (!__this_cpu_read(snapshot_taken)) {
  399. kstat_snapshot_irqs();
  400. __this_cpu_write(snapshot_taken, true);
  401. }
  402. }
  403. static void stop_counting_irqs(void)
  404. {
  405. __this_cpu_write(snapshot_taken, false);
  406. }
  407. static void print_irq_counts(void)
  408. {
  409. unsigned int i, count;
  410. struct irq_counts irq_counts_sorted[NUM_HARDIRQ_REPORT] = {
  411. {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}
  412. };
  413. if (__this_cpu_read(snapshot_taken)) {
  414. for_each_active_irq(i) {
  415. count = kstat_get_irq_since_snapshot(i);
  416. tabulate_irq_count(irq_counts_sorted, i, count, NUM_HARDIRQ_REPORT);
  417. }
  418. /*
  419. * Outputting the "watchdog" prefix on every line is redundant and not
  420. * concise, and the original alarm information is sufficient for
  421. * positioning in logs, hence here printk() is used instead of pr_crit().
  422. */
  423. printk(KERN_CRIT "CPU#%d Detect HardIRQ Time exceeds %d%%. Most frequent HardIRQs:\n",
  424. smp_processor_id(), HARDIRQ_PERCENT_THRESH);
  425. for (i = 0; i < NUM_HARDIRQ_REPORT; i++) {
  426. if (irq_counts_sorted[i].irq == -1)
  427. break;
  428. printk(KERN_CRIT "\t#%u: %-10u\tirq#%d\n",
  429. i + 1, irq_counts_sorted[i].counts,
  430. irq_counts_sorted[i].irq);
  431. }
  432. /*
  433. * If the hardirq time is less than HARDIRQ_PERCENT_THRESH% in the last
  434. * sample_period, then we suspect the interrupt storm might be subsiding.
  435. */
  436. if (!need_counting_irqs())
  437. stop_counting_irqs();
  438. }
  439. }
  440. static void report_cpu_status(void)
  441. {
  442. print_cpustat();
  443. print_irq_counts();
  444. }
  445. #else
  446. static inline void update_cpustat(void) { }
  447. static inline void report_cpu_status(void) { }
  448. static inline bool need_counting_irqs(void) { return false; }
  449. static inline void start_counting_irqs(void) { }
  450. static inline void stop_counting_irqs(void) { }
  451. #endif
  452. /*
  453. * Hard-lockup warnings should be triggered after just a few seconds. Soft-
  454. * lockups can have false positives under extreme conditions. So we generally
  455. * want a higher threshold for soft lockups than for hard lockups. So we couple
  456. * the thresholds with a factor: we make the soft threshold twice the amount of
  457. * time the hard threshold is.
  458. */
  459. static int get_softlockup_thresh(void)
  460. {
  461. return watchdog_thresh * 2;
  462. }
  463. /*
  464. * Returns seconds, approximately. We don't need nanosecond
  465. * resolution, and we don't need to waste time with a big divide when
  466. * 2^30ns == 1.074s.
  467. */
  468. static unsigned long get_timestamp(void)
  469. {
  470. return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
  471. }
  472. static void set_sample_period(void)
  473. {
  474. /*
  475. * convert watchdog_thresh from seconds to ns
  476. * the divide by 5 is to give hrtimer several chances (two
  477. * or three with the current relation between the soft
  478. * and hard thresholds) to increment before the
  479. * hardlockup detector generates a warning
  480. */
  481. sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / NUM_SAMPLE_PERIODS);
  482. watchdog_update_hrtimer_threshold(sample_period);
  483. }
  484. static void update_report_ts(void)
  485. {
  486. __this_cpu_write(watchdog_report_ts, get_timestamp());
  487. }
  488. /* Commands for resetting the watchdog */
  489. static void update_touch_ts(void)
  490. {
  491. __this_cpu_write(watchdog_touch_ts, get_timestamp());
  492. update_report_ts();
  493. }
  494. /**
  495. * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
  496. *
  497. * Call when the scheduler may have stalled for legitimate reasons
  498. * preventing the watchdog task from executing - e.g. the scheduler
  499. * entering idle state. This should only be used for scheduler events.
  500. * Use touch_softlockup_watchdog() for everything else.
  501. */
  502. notrace void touch_softlockup_watchdog_sched(void)
  503. {
  504. /*
  505. * Preemption can be enabled. It doesn't matter which CPU's watchdog
  506. * report period gets restarted here, so use the raw_ operation.
  507. */
  508. raw_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
  509. }
  510. notrace void touch_softlockup_watchdog(void)
  511. {
  512. touch_softlockup_watchdog_sched();
  513. wq_watchdog_touch(raw_smp_processor_id());
  514. }
  515. EXPORT_SYMBOL(touch_softlockup_watchdog);
  516. void touch_all_softlockup_watchdogs(void)
  517. {
  518. int cpu;
  519. /*
  520. * watchdog_mutex cannpt be taken here, as this might be called
  521. * from (soft)interrupt context, so the access to
  522. * watchdog_allowed_cpumask might race with a concurrent update.
  523. *
  524. * The watchdog time stamp can race against a concurrent real
  525. * update as well, the only side effect might be a cycle delay for
  526. * the softlockup check.
  527. */
  528. for_each_cpu(cpu, &watchdog_allowed_mask) {
  529. per_cpu(watchdog_report_ts, cpu) = SOFTLOCKUP_DELAY_REPORT;
  530. wq_watchdog_touch(cpu);
  531. }
  532. }
  533. void touch_softlockup_watchdog_sync(void)
  534. {
  535. __this_cpu_write(softlockup_touch_sync, true);
  536. __this_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
  537. }
  538. static int is_softlockup(unsigned long touch_ts,
  539. unsigned long period_ts,
  540. unsigned long now)
  541. {
  542. if ((watchdog_enabled & WATCHDOG_SOFTOCKUP_ENABLED) && watchdog_thresh) {
  543. /*
  544. * If period_ts has not been updated during a sample_period, then
  545. * in the subsequent few sample_periods, period_ts might also not
  546. * be updated, which could indicate a potential softlockup. In
  547. * this case, if we suspect the cause of the potential softlockup
  548. * might be interrupt storm, then we need to count the interrupts
  549. * to find which interrupt is storming.
  550. */
  551. if (time_after_eq(now, period_ts + get_softlockup_thresh() / NUM_SAMPLE_PERIODS) &&
  552. need_counting_irqs())
  553. start_counting_irqs();
  554. /* Warn about unreasonable delays. */
  555. if (time_after(now, period_ts + get_softlockup_thresh()))
  556. return now - touch_ts;
  557. }
  558. return 0;
  559. }
  560. /* watchdog detector functions */
  561. static DEFINE_PER_CPU(struct completion, softlockup_completion);
  562. static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
  563. /*
  564. * The watchdog feed function - touches the timestamp.
  565. *
  566. * It only runs once every sample_period seconds (4 seconds by
  567. * default) to reset the softlockup timestamp. If this gets delayed
  568. * for more than 2*watchdog_thresh seconds then the debug-printout
  569. * triggers in watchdog_timer_fn().
  570. */
  571. static int softlockup_fn(void *data)
  572. {
  573. update_touch_ts();
  574. stop_counting_irqs();
  575. complete(this_cpu_ptr(&softlockup_completion));
  576. return 0;
  577. }
  578. /* watchdog kicker functions */
  579. static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
  580. {
  581. unsigned long touch_ts, period_ts, now;
  582. struct pt_regs *regs = get_irq_regs();
  583. int duration;
  584. int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
  585. unsigned long flags;
  586. if (!watchdog_enabled)
  587. return HRTIMER_NORESTART;
  588. watchdog_hardlockup_kick();
  589. /* kick the softlockup detector */
  590. if (completion_done(this_cpu_ptr(&softlockup_completion))) {
  591. reinit_completion(this_cpu_ptr(&softlockup_completion));
  592. stop_one_cpu_nowait(smp_processor_id(),
  593. softlockup_fn, NULL,
  594. this_cpu_ptr(&softlockup_stop_work));
  595. }
  596. /* .. and repeat */
  597. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  598. /*
  599. * Read the current timestamp first. It might become invalid anytime
  600. * when a virtual machine is stopped by the host or when the watchog
  601. * is touched from NMI.
  602. */
  603. now = get_timestamp();
  604. /*
  605. * If a virtual machine is stopped by the host it can look to
  606. * the watchdog like a soft lockup. This function touches the watchdog.
  607. */
  608. kvm_check_and_clear_guest_paused();
  609. /*
  610. * The stored timestamp is comparable with @now only when not touched.
  611. * It might get touched anytime from NMI. Make sure that is_softlockup()
  612. * uses the same (valid) value.
  613. */
  614. period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
  615. update_cpustat();
  616. /* Reset the interval when touched by known problematic code. */
  617. if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
  618. if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
  619. /*
  620. * If the time stamp was touched atomically
  621. * make sure the scheduler tick is up to date.
  622. */
  623. __this_cpu_write(softlockup_touch_sync, false);
  624. sched_clock_tick();
  625. }
  626. update_report_ts();
  627. return HRTIMER_RESTART;
  628. }
  629. /* Check for a softlockup. */
  630. touch_ts = __this_cpu_read(watchdog_touch_ts);
  631. duration = is_softlockup(touch_ts, period_ts, now);
  632. if (unlikely(duration)) {
  633. /*
  634. * Prevent multiple soft-lockup reports if one cpu is already
  635. * engaged in dumping all cpu back traces.
  636. */
  637. if (softlockup_all_cpu_backtrace) {
  638. if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
  639. return HRTIMER_RESTART;
  640. }
  641. /* Start period for the next softlockup warning. */
  642. update_report_ts();
  643. printk_cpu_sync_get_irqsave(flags);
  644. pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
  645. smp_processor_id(), duration,
  646. current->comm, task_pid_nr(current));
  647. report_cpu_status();
  648. print_modules();
  649. print_irqtrace_events(current);
  650. if (regs)
  651. show_regs(regs);
  652. else
  653. dump_stack();
  654. printk_cpu_sync_put_irqrestore(flags);
  655. if (softlockup_all_cpu_backtrace) {
  656. trigger_allbutcpu_cpu_backtrace(smp_processor_id());
  657. if (!softlockup_panic)
  658. clear_bit_unlock(0, &soft_lockup_nmi_warn);
  659. }
  660. add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
  661. if (softlockup_panic)
  662. panic("softlockup: hung tasks");
  663. }
  664. return HRTIMER_RESTART;
  665. }
  666. static void watchdog_enable(unsigned int cpu)
  667. {
  668. struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
  669. struct completion *done = this_cpu_ptr(&softlockup_completion);
  670. WARN_ON_ONCE(cpu != smp_processor_id());
  671. init_completion(done);
  672. complete(done);
  673. /*
  674. * Start the timer first to prevent the hardlockup watchdog triggering
  675. * before the timer has a chance to fire.
  676. */
  677. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  678. hrtimer->function = watchdog_timer_fn;
  679. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  680. HRTIMER_MODE_REL_PINNED_HARD);
  681. /* Initialize timestamp */
  682. update_touch_ts();
  683. /* Enable the hardlockup detector */
  684. if (watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)
  685. watchdog_hardlockup_enable(cpu);
  686. }
  687. static void watchdog_disable(unsigned int cpu)
  688. {
  689. struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
  690. WARN_ON_ONCE(cpu != smp_processor_id());
  691. /*
  692. * Disable the hardlockup detector first. That prevents that a large
  693. * delay between disabling the timer and disabling the hardlockup
  694. * detector causes a false positive.
  695. */
  696. watchdog_hardlockup_disable(cpu);
  697. hrtimer_cancel(hrtimer);
  698. wait_for_completion(this_cpu_ptr(&softlockup_completion));
  699. }
  700. static int softlockup_stop_fn(void *data)
  701. {
  702. watchdog_disable(smp_processor_id());
  703. return 0;
  704. }
  705. static void softlockup_stop_all(void)
  706. {
  707. int cpu;
  708. if (!softlockup_initialized)
  709. return;
  710. for_each_cpu(cpu, &watchdog_allowed_mask)
  711. smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
  712. cpumask_clear(&watchdog_allowed_mask);
  713. }
  714. static int softlockup_start_fn(void *data)
  715. {
  716. watchdog_enable(smp_processor_id());
  717. return 0;
  718. }
  719. static void softlockup_start_all(void)
  720. {
  721. int cpu;
  722. cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
  723. for_each_cpu(cpu, &watchdog_allowed_mask)
  724. smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
  725. }
  726. int lockup_detector_online_cpu(unsigned int cpu)
  727. {
  728. if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
  729. watchdog_enable(cpu);
  730. return 0;
  731. }
  732. int lockup_detector_offline_cpu(unsigned int cpu)
  733. {
  734. if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
  735. watchdog_disable(cpu);
  736. return 0;
  737. }
  738. static void __lockup_detector_reconfigure(void)
  739. {
  740. cpus_read_lock();
  741. watchdog_hardlockup_stop();
  742. softlockup_stop_all();
  743. set_sample_period();
  744. lockup_detector_update_enable();
  745. if (watchdog_enabled && watchdog_thresh)
  746. softlockup_start_all();
  747. watchdog_hardlockup_start();
  748. cpus_read_unlock();
  749. /*
  750. * Must be called outside the cpus locked section to prevent
  751. * recursive locking in the perf code.
  752. */
  753. __lockup_detector_cleanup();
  754. }
  755. void lockup_detector_reconfigure(void)
  756. {
  757. mutex_lock(&watchdog_mutex);
  758. __lockup_detector_reconfigure();
  759. mutex_unlock(&watchdog_mutex);
  760. }
  761. /*
  762. * Create the watchdog infrastructure and configure the detector(s).
  763. */
  764. static __init void lockup_detector_setup(void)
  765. {
  766. /*
  767. * If sysctl is off and watchdog got disabled on the command line,
  768. * nothing to do here.
  769. */
  770. lockup_detector_update_enable();
  771. if (!IS_ENABLED(CONFIG_SYSCTL) &&
  772. !(watchdog_enabled && watchdog_thresh))
  773. return;
  774. mutex_lock(&watchdog_mutex);
  775. __lockup_detector_reconfigure();
  776. softlockup_initialized = true;
  777. mutex_unlock(&watchdog_mutex);
  778. }
  779. #else /* CONFIG_SOFTLOCKUP_DETECTOR */
  780. static void __lockup_detector_reconfigure(void)
  781. {
  782. cpus_read_lock();
  783. watchdog_hardlockup_stop();
  784. lockup_detector_update_enable();
  785. watchdog_hardlockup_start();
  786. cpus_read_unlock();
  787. }
  788. void lockup_detector_reconfigure(void)
  789. {
  790. __lockup_detector_reconfigure();
  791. }
  792. static inline void lockup_detector_setup(void)
  793. {
  794. __lockup_detector_reconfigure();
  795. }
  796. #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
  797. static void __lockup_detector_cleanup(void)
  798. {
  799. lockdep_assert_held(&watchdog_mutex);
  800. hardlockup_detector_perf_cleanup();
  801. }
  802. /**
  803. * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
  804. *
  805. * Caller must not hold the cpu hotplug rwsem.
  806. */
  807. void lockup_detector_cleanup(void)
  808. {
  809. mutex_lock(&watchdog_mutex);
  810. __lockup_detector_cleanup();
  811. mutex_unlock(&watchdog_mutex);
  812. }
  813. /**
  814. * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
  815. *
  816. * Special interface for parisc. It prevents lockup detector warnings from
  817. * the default pm_poweroff() function which busy loops forever.
  818. */
  819. void lockup_detector_soft_poweroff(void)
  820. {
  821. watchdog_enabled = 0;
  822. }
  823. #ifdef CONFIG_SYSCTL
  824. /* Propagate any changes to the watchdog infrastructure */
  825. static void proc_watchdog_update(void)
  826. {
  827. /* Remove impossible cpus to keep sysctl output clean. */
  828. cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
  829. __lockup_detector_reconfigure();
  830. }
  831. /*
  832. * common function for watchdog, nmi_watchdog and soft_watchdog parameter
  833. *
  834. * caller | table->data points to | 'which'
  835. * -------------------|----------------------------------|-------------------------------
  836. * proc_watchdog | watchdog_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED |
  837. * | | WATCHDOG_SOFTOCKUP_ENABLED
  838. * -------------------|----------------------------------|-------------------------------
  839. * proc_nmi_watchdog | watchdog_hardlockup_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED
  840. * -------------------|----------------------------------|-------------------------------
  841. * proc_soft_watchdog | watchdog_softlockup_user_enabled | WATCHDOG_SOFTOCKUP_ENABLED
  842. */
  843. static int proc_watchdog_common(int which, const struct ctl_table *table, int write,
  844. void *buffer, size_t *lenp, loff_t *ppos)
  845. {
  846. int err, old, *param = table->data;
  847. mutex_lock(&watchdog_mutex);
  848. if (!write) {
  849. /*
  850. * On read synchronize the userspace interface. This is a
  851. * racy snapshot.
  852. */
  853. *param = (watchdog_enabled & which) != 0;
  854. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  855. } else {
  856. old = READ_ONCE(*param);
  857. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  858. if (!err && old != READ_ONCE(*param))
  859. proc_watchdog_update();
  860. }
  861. mutex_unlock(&watchdog_mutex);
  862. return err;
  863. }
  864. /*
  865. * /proc/sys/kernel/watchdog
  866. */
  867. static int proc_watchdog(const struct ctl_table *table, int write,
  868. void *buffer, size_t *lenp, loff_t *ppos)
  869. {
  870. return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED |
  871. WATCHDOG_SOFTOCKUP_ENABLED,
  872. table, write, buffer, lenp, ppos);
  873. }
  874. /*
  875. * /proc/sys/kernel/nmi_watchdog
  876. */
  877. static int proc_nmi_watchdog(const struct ctl_table *table, int write,
  878. void *buffer, size_t *lenp, loff_t *ppos)
  879. {
  880. if (!watchdog_hardlockup_available && write)
  881. return -ENOTSUPP;
  882. return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED,
  883. table, write, buffer, lenp, ppos);
  884. }
  885. #ifdef CONFIG_SOFTLOCKUP_DETECTOR
  886. /*
  887. * /proc/sys/kernel/soft_watchdog
  888. */
  889. static int proc_soft_watchdog(const struct ctl_table *table, int write,
  890. void *buffer, size_t *lenp, loff_t *ppos)
  891. {
  892. return proc_watchdog_common(WATCHDOG_SOFTOCKUP_ENABLED,
  893. table, write, buffer, lenp, ppos);
  894. }
  895. #endif
  896. /*
  897. * /proc/sys/kernel/watchdog_thresh
  898. */
  899. static int proc_watchdog_thresh(const struct ctl_table *table, int write,
  900. void *buffer, size_t *lenp, loff_t *ppos)
  901. {
  902. int err, old;
  903. mutex_lock(&watchdog_mutex);
  904. old = READ_ONCE(watchdog_thresh);
  905. err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  906. if (!err && write && old != READ_ONCE(watchdog_thresh))
  907. proc_watchdog_update();
  908. mutex_unlock(&watchdog_mutex);
  909. return err;
  910. }
  911. /*
  912. * The cpumask is the mask of possible cpus that the watchdog can run
  913. * on, not the mask of cpus it is actually running on. This allows the
  914. * user to specify a mask that will include cpus that have not yet
  915. * been brought online, if desired.
  916. */
  917. static int proc_watchdog_cpumask(const struct ctl_table *table, int write,
  918. void *buffer, size_t *lenp, loff_t *ppos)
  919. {
  920. int err;
  921. mutex_lock(&watchdog_mutex);
  922. err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
  923. if (!err && write)
  924. proc_watchdog_update();
  925. mutex_unlock(&watchdog_mutex);
  926. return err;
  927. }
  928. static const int sixty = 60;
  929. static struct ctl_table watchdog_sysctls[] = {
  930. {
  931. .procname = "watchdog",
  932. .data = &watchdog_user_enabled,
  933. .maxlen = sizeof(int),
  934. .mode = 0644,
  935. .proc_handler = proc_watchdog,
  936. .extra1 = SYSCTL_ZERO,
  937. .extra2 = SYSCTL_ONE,
  938. },
  939. {
  940. .procname = "watchdog_thresh",
  941. .data = &watchdog_thresh,
  942. .maxlen = sizeof(int),
  943. .mode = 0644,
  944. .proc_handler = proc_watchdog_thresh,
  945. .extra1 = SYSCTL_ZERO,
  946. .extra2 = (void *)&sixty,
  947. },
  948. {
  949. .procname = "watchdog_cpumask",
  950. .data = &watchdog_cpumask_bits,
  951. .maxlen = NR_CPUS,
  952. .mode = 0644,
  953. .proc_handler = proc_watchdog_cpumask,
  954. },
  955. #ifdef CONFIG_SOFTLOCKUP_DETECTOR
  956. {
  957. .procname = "soft_watchdog",
  958. .data = &watchdog_softlockup_user_enabled,
  959. .maxlen = sizeof(int),
  960. .mode = 0644,
  961. .proc_handler = proc_soft_watchdog,
  962. .extra1 = SYSCTL_ZERO,
  963. .extra2 = SYSCTL_ONE,
  964. },
  965. {
  966. .procname = "softlockup_panic",
  967. .data = &softlockup_panic,
  968. .maxlen = sizeof(int),
  969. .mode = 0644,
  970. .proc_handler = proc_dointvec_minmax,
  971. .extra1 = SYSCTL_ZERO,
  972. .extra2 = SYSCTL_ONE,
  973. },
  974. #ifdef CONFIG_SMP
  975. {
  976. .procname = "softlockup_all_cpu_backtrace",
  977. .data = &sysctl_softlockup_all_cpu_backtrace,
  978. .maxlen = sizeof(int),
  979. .mode = 0644,
  980. .proc_handler = proc_dointvec_minmax,
  981. .extra1 = SYSCTL_ZERO,
  982. .extra2 = SYSCTL_ONE,
  983. },
  984. #endif /* CONFIG_SMP */
  985. #endif
  986. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  987. {
  988. .procname = "hardlockup_panic",
  989. .data = &hardlockup_panic,
  990. .maxlen = sizeof(int),
  991. .mode = 0644,
  992. .proc_handler = proc_dointvec_minmax,
  993. .extra1 = SYSCTL_ZERO,
  994. .extra2 = SYSCTL_ONE,
  995. },
  996. #ifdef CONFIG_SMP
  997. {
  998. .procname = "hardlockup_all_cpu_backtrace",
  999. .data = &sysctl_hardlockup_all_cpu_backtrace,
  1000. .maxlen = sizeof(int),
  1001. .mode = 0644,
  1002. .proc_handler = proc_dointvec_minmax,
  1003. .extra1 = SYSCTL_ZERO,
  1004. .extra2 = SYSCTL_ONE,
  1005. },
  1006. #endif /* CONFIG_SMP */
  1007. #endif
  1008. };
  1009. static struct ctl_table watchdog_hardlockup_sysctl[] = {
  1010. {
  1011. .procname = "nmi_watchdog",
  1012. .data = &watchdog_hardlockup_user_enabled,
  1013. .maxlen = sizeof(int),
  1014. .mode = 0444,
  1015. .proc_handler = proc_nmi_watchdog,
  1016. .extra1 = SYSCTL_ZERO,
  1017. .extra2 = SYSCTL_ONE,
  1018. },
  1019. };
  1020. static void __init watchdog_sysctl_init(void)
  1021. {
  1022. register_sysctl_init("kernel", watchdog_sysctls);
  1023. if (watchdog_hardlockup_available)
  1024. watchdog_hardlockup_sysctl[0].mode = 0644;
  1025. register_sysctl_init("kernel", watchdog_hardlockup_sysctl);
  1026. }
  1027. #else
  1028. #define watchdog_sysctl_init() do { } while (0)
  1029. #endif /* CONFIG_SYSCTL */
  1030. static void __init lockup_detector_delay_init(struct work_struct *work);
  1031. static bool allow_lockup_detector_init_retry __initdata;
  1032. static struct work_struct detector_work __initdata =
  1033. __WORK_INITIALIZER(detector_work, lockup_detector_delay_init);
  1034. static void __init lockup_detector_delay_init(struct work_struct *work)
  1035. {
  1036. int ret;
  1037. ret = watchdog_hardlockup_probe();
  1038. if (ret) {
  1039. if (ret == -ENODEV)
  1040. pr_info("NMI not fully supported\n");
  1041. else
  1042. pr_info("Delayed init of the lockup detector failed: %d\n", ret);
  1043. pr_info("Hard watchdog permanently disabled\n");
  1044. return;
  1045. }
  1046. allow_lockup_detector_init_retry = false;
  1047. watchdog_hardlockup_available = true;
  1048. lockup_detector_setup();
  1049. }
  1050. /*
  1051. * lockup_detector_retry_init - retry init lockup detector if possible.
  1052. *
  1053. * Retry hardlockup detector init. It is useful when it requires some
  1054. * functionality that has to be initialized later on a particular
  1055. * platform.
  1056. */
  1057. void __init lockup_detector_retry_init(void)
  1058. {
  1059. /* Must be called before late init calls */
  1060. if (!allow_lockup_detector_init_retry)
  1061. return;
  1062. schedule_work(&detector_work);
  1063. }
  1064. /*
  1065. * Ensure that optional delayed hardlockup init is proceed before
  1066. * the init code and memory is freed.
  1067. */
  1068. static int __init lockup_detector_check(void)
  1069. {
  1070. /* Prevent any later retry. */
  1071. allow_lockup_detector_init_retry = false;
  1072. /* Make sure no work is pending. */
  1073. flush_work(&detector_work);
  1074. watchdog_sysctl_init();
  1075. return 0;
  1076. }
  1077. late_initcall_sync(lockup_detector_check);
  1078. void __init lockup_detector_init(void)
  1079. {
  1080. if (tick_nohz_full_enabled())
  1081. pr_info("Disabling watchdog on nohz_full cores by default\n");
  1082. cpumask_copy(&watchdog_cpumask,
  1083. housekeeping_cpumask(HK_TYPE_TIMER));
  1084. if (!watchdog_hardlockup_probe())
  1085. watchdog_hardlockup_available = true;
  1086. else
  1087. allow_lockup_detector_init_retry = true;
  1088. lockup_detector_setup();
  1089. }