watchdog_perf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Detect hard lockups on a system using perf
  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) "NMI watchdog: " fmt
  13. #include <linux/nmi.h>
  14. #include <linux/atomic.h>
  15. #include <linux/module.h>
  16. #include <linux/sched/debug.h>
  17. #include <asm/irq_regs.h>
  18. #include <linux/perf_event.h>
  19. static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
  20. static DEFINE_PER_CPU(struct perf_event *, dead_event);
  21. static struct cpumask dead_events_mask;
  22. static atomic_t watchdog_cpus = ATOMIC_INIT(0);
  23. #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
  24. static DEFINE_PER_CPU(ktime_t, last_timestamp);
  25. static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
  26. static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
  27. void watchdog_update_hrtimer_threshold(u64 period)
  28. {
  29. /*
  30. * The hrtimer runs with a period of (watchdog_threshold * 2) / 5
  31. *
  32. * So it runs effectively with 2.5 times the rate of the NMI
  33. * watchdog. That means the hrtimer should fire 2-3 times before
  34. * the NMI watchdog expires. The NMI watchdog on x86 is based on
  35. * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles
  36. * might run way faster than expected and the NMI fires in a
  37. * smaller period than the one deduced from the nominal CPU
  38. * frequency. Depending on the Turbo-Mode factor this might be fast
  39. * enough to get the NMI period smaller than the hrtimer watchdog
  40. * period and trigger false positives.
  41. *
  42. * The sample threshold is used to check in the NMI handler whether
  43. * the minimum time between two NMI samples has elapsed. That
  44. * prevents false positives.
  45. *
  46. * Set this to 4/5 of the actual watchdog threshold period so the
  47. * hrtimer is guaranteed to fire at least once within the real
  48. * watchdog threshold.
  49. */
  50. watchdog_hrtimer_sample_threshold = period * 2;
  51. }
  52. static bool watchdog_check_timestamp(void)
  53. {
  54. ktime_t delta, now = ktime_get_mono_fast_ns();
  55. delta = now - __this_cpu_read(last_timestamp);
  56. if (delta < watchdog_hrtimer_sample_threshold) {
  57. /*
  58. * If ktime is jiffies based, a stalled timer would prevent
  59. * jiffies from being incremented and the filter would look
  60. * at a stale timestamp and never trigger.
  61. */
  62. if (__this_cpu_inc_return(nmi_rearmed) < 10)
  63. return false;
  64. }
  65. __this_cpu_write(nmi_rearmed, 0);
  66. __this_cpu_write(last_timestamp, now);
  67. return true;
  68. }
  69. static void watchdog_init_timestamp(void)
  70. {
  71. __this_cpu_write(nmi_rearmed, 0);
  72. __this_cpu_write(last_timestamp, ktime_get_mono_fast_ns());
  73. }
  74. #else
  75. static inline bool watchdog_check_timestamp(void) { return true; }
  76. static inline void watchdog_init_timestamp(void) { }
  77. #endif
  78. static struct perf_event_attr wd_hw_attr = {
  79. .type = PERF_TYPE_HARDWARE,
  80. .config = PERF_COUNT_HW_CPU_CYCLES,
  81. .size = sizeof(struct perf_event_attr),
  82. .pinned = 1,
  83. .disabled = 1,
  84. };
  85. static struct perf_event_attr fallback_wd_hw_attr = {
  86. .type = PERF_TYPE_HARDWARE,
  87. .config = PERF_COUNT_HW_CPU_CYCLES,
  88. .size = sizeof(struct perf_event_attr),
  89. .pinned = 1,
  90. .disabled = 1,
  91. };
  92. /* Callback function for perf event subsystem */
  93. static void watchdog_overflow_callback(struct perf_event *event,
  94. struct perf_sample_data *data,
  95. struct pt_regs *regs)
  96. {
  97. /* Ensure the watchdog never gets throttled */
  98. event->hw.interrupts = 0;
  99. if (!watchdog_check_timestamp())
  100. return;
  101. watchdog_hardlockup_check(smp_processor_id(), regs);
  102. }
  103. static int hardlockup_detector_event_create(void)
  104. {
  105. unsigned int cpu;
  106. struct perf_event_attr *wd_attr;
  107. struct perf_event *evt;
  108. /*
  109. * Preemption is not disabled because memory will be allocated.
  110. * Ensure CPU-locality by calling this in per-CPU kthread.
  111. */
  112. WARN_ON(!is_percpu_thread());
  113. cpu = raw_smp_processor_id();
  114. wd_attr = &wd_hw_attr;
  115. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  116. /* Try to register using hardware perf events */
  117. evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
  118. watchdog_overflow_callback, NULL);
  119. if (IS_ERR(evt)) {
  120. wd_attr = &fallback_wd_hw_attr;
  121. wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
  122. evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
  123. watchdog_overflow_callback, NULL);
  124. }
  125. if (IS_ERR(evt)) {
  126. pr_debug("Perf event create on CPU %d failed with %ld\n", cpu,
  127. PTR_ERR(evt));
  128. return PTR_ERR(evt);
  129. }
  130. this_cpu_write(watchdog_ev, evt);
  131. return 0;
  132. }
  133. /**
  134. * watchdog_hardlockup_enable - Enable the local event
  135. * @cpu: The CPU to enable hard lockup on.
  136. */
  137. void watchdog_hardlockup_enable(unsigned int cpu)
  138. {
  139. WARN_ON_ONCE(cpu != smp_processor_id());
  140. if (hardlockup_detector_event_create())
  141. return;
  142. /* use original value for check */
  143. if (!atomic_fetch_inc(&watchdog_cpus))
  144. pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
  145. watchdog_init_timestamp();
  146. perf_event_enable(this_cpu_read(watchdog_ev));
  147. }
  148. /**
  149. * watchdog_hardlockup_disable - Disable the local event
  150. * @cpu: The CPU to enable hard lockup on.
  151. */
  152. void watchdog_hardlockup_disable(unsigned int cpu)
  153. {
  154. struct perf_event *event = this_cpu_read(watchdog_ev);
  155. WARN_ON_ONCE(cpu != smp_processor_id());
  156. if (event) {
  157. perf_event_disable(event);
  158. this_cpu_write(watchdog_ev, NULL);
  159. this_cpu_write(dead_event, event);
  160. cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
  161. atomic_dec(&watchdog_cpus);
  162. }
  163. }
  164. /**
  165. * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them
  166. *
  167. * Called from lockup_detector_cleanup(). Serialized by the caller.
  168. */
  169. void hardlockup_detector_perf_cleanup(void)
  170. {
  171. int cpu;
  172. for_each_cpu(cpu, &dead_events_mask) {
  173. struct perf_event *event = per_cpu(dead_event, cpu);
  174. /*
  175. * Required because for_each_cpu() reports unconditionally
  176. * CPU0 as set on UP kernels. Sigh.
  177. */
  178. if (event)
  179. perf_event_release_kernel(event);
  180. per_cpu(dead_event, cpu) = NULL;
  181. }
  182. cpumask_clear(&dead_events_mask);
  183. }
  184. /**
  185. * hardlockup_detector_perf_stop - Globally stop watchdog events
  186. *
  187. * Special interface for x86 to handle the perf HT bug.
  188. */
  189. void __init hardlockup_detector_perf_stop(void)
  190. {
  191. int cpu;
  192. lockdep_assert_cpus_held();
  193. for_each_online_cpu(cpu) {
  194. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  195. if (event)
  196. perf_event_disable(event);
  197. }
  198. }
  199. /**
  200. * hardlockup_detector_perf_restart - Globally restart watchdog events
  201. *
  202. * Special interface for x86 to handle the perf HT bug.
  203. */
  204. void __init hardlockup_detector_perf_restart(void)
  205. {
  206. int cpu;
  207. lockdep_assert_cpus_held();
  208. if (!(watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED))
  209. return;
  210. for_each_online_cpu(cpu) {
  211. struct perf_event *event = per_cpu(watchdog_ev, cpu);
  212. if (event)
  213. perf_event_enable(event);
  214. }
  215. }
  216. bool __weak __init arch_perf_nmi_is_available(void)
  217. {
  218. return true;
  219. }
  220. /**
  221. * watchdog_hardlockup_probe - Probe whether NMI event is available at all
  222. */
  223. int __init watchdog_hardlockup_probe(void)
  224. {
  225. int ret;
  226. if (!arch_perf_nmi_is_available())
  227. return -ENODEV;
  228. ret = hardlockup_detector_event_create();
  229. if (ret) {
  230. pr_info("Perf NMI watchdog permanently disabled\n");
  231. } else {
  232. perf_event_release_kernel(this_cpu_read(watchdog_ev));
  233. this_cpu_write(watchdog_ev, NULL);
  234. }
  235. return ret;
  236. }
  237. /**
  238. * hardlockup_config_perf_event - Overwrite config of wd_hw_attr.
  239. * @str: number which identifies the raw perf event to use
  240. */
  241. void __init hardlockup_config_perf_event(const char *str)
  242. {
  243. u64 config;
  244. char buf[24];
  245. char *comma = strchr(str, ',');
  246. if (!comma) {
  247. if (kstrtoull(str, 16, &config))
  248. return;
  249. } else {
  250. unsigned int len = comma - str;
  251. if (len >= sizeof(buf))
  252. return;
  253. if (strscpy(buf, str, sizeof(buf)) < 0)
  254. return;
  255. buf[len] = 0;
  256. if (kstrtoull(buf, 16, &config))
  257. return;
  258. }
  259. wd_hw_attr.type = PERF_TYPE_RAW;
  260. wd_hw_attr.config = config;
  261. }