watchdog_buddy.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/cpu.h>
  3. #include <linux/cpumask.h>
  4. #include <linux/kernel.h>
  5. #include <linux/nmi.h>
  6. #include <linux/percpu-defs.h>
  7. static cpumask_t __read_mostly watchdog_cpus;
  8. static unsigned int watchdog_next_cpu(unsigned int cpu)
  9. {
  10. unsigned int next_cpu;
  11. next_cpu = cpumask_next_wrap(cpu, &watchdog_cpus);
  12. if (next_cpu == cpu)
  13. return nr_cpu_ids;
  14. return next_cpu;
  15. }
  16. int __init watchdog_hardlockup_probe(void)
  17. {
  18. return 0;
  19. }
  20. void watchdog_hardlockup_enable(unsigned int cpu)
  21. {
  22. unsigned int next_cpu;
  23. /*
  24. * The new CPU will be marked online before the hrtimer interrupt
  25. * gets a chance to run on it. If another CPU tests for a
  26. * hardlockup on the new CPU before it has run its the hrtimer
  27. * interrupt, it will get a false positive. Touch the watchdog on
  28. * the new CPU to delay the check for at least 3 sampling periods
  29. * to guarantee one hrtimer has run on the new CPU.
  30. */
  31. watchdog_hardlockup_touch_cpu(cpu);
  32. /*
  33. * We are going to check the next CPU. Our watchdog_hrtimer
  34. * need not be zero if the CPU has already been online earlier.
  35. * Touch the watchdog on the next CPU to avoid false positive
  36. * if we try to check it in less then 3 interrupts.
  37. */
  38. next_cpu = watchdog_next_cpu(cpu);
  39. if (next_cpu < nr_cpu_ids)
  40. watchdog_hardlockup_touch_cpu(next_cpu);
  41. /*
  42. * Makes sure that watchdog is touched on this CPU before
  43. * other CPUs could see it in watchdog_cpus. The counter
  44. * part is in watchdog_buddy_check_hardlockup().
  45. */
  46. smp_wmb();
  47. cpumask_set_cpu(cpu, &watchdog_cpus);
  48. }
  49. void watchdog_hardlockup_disable(unsigned int cpu)
  50. {
  51. unsigned int next_cpu = watchdog_next_cpu(cpu);
  52. /*
  53. * Offlining this CPU will cause the CPU before this one to start
  54. * checking the one after this one. If this CPU just finished checking
  55. * the next CPU and updating hrtimer_interrupts_saved, and then the
  56. * previous CPU checks it within one sample period, it will trigger a
  57. * false positive. Touch the watchdog on the next CPU to prevent it.
  58. */
  59. if (next_cpu < nr_cpu_ids)
  60. watchdog_hardlockup_touch_cpu(next_cpu);
  61. /*
  62. * Makes sure that watchdog is touched on the next CPU before
  63. * this CPU disappear in watchdog_cpus. The counter part is in
  64. * watchdog_buddy_check_hardlockup().
  65. */
  66. smp_wmb();
  67. cpumask_clear_cpu(cpu, &watchdog_cpus);
  68. }
  69. void watchdog_buddy_check_hardlockup(int hrtimer_interrupts)
  70. {
  71. unsigned int next_cpu;
  72. /*
  73. * Test for hardlockups every 3 samples. The sample period is
  74. * watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
  75. * watchdog_thresh (over by 20%).
  76. */
  77. if (hrtimer_interrupts % 3 != 0)
  78. return;
  79. /* check for a hardlockup on the next CPU */
  80. next_cpu = watchdog_next_cpu(smp_processor_id());
  81. if (next_cpu >= nr_cpu_ids)
  82. return;
  83. /*
  84. * Make sure that the watchdog was touched on next CPU when
  85. * watchdog_next_cpu() returned another one because of
  86. * a change in watchdog_hardlockup_enable()/disable().
  87. */
  88. smp_rmb();
  89. watchdog_hardlockup_check(next_cpu, NULL);
  90. }