[PATCH v6 26/29] x86/watchdog: Add a shim hardlockup detector

From: Ricardo Neri
Date: Thu May 05 2022 - 20:00:58 EST


The generic hardlockup detector is based on perf. It also provides a set
of weak functions that CPU architectures can override. Add a shim
hardlockup detector for x86 that overrides such functions and can
select between perf and HPET implementations of the detector.

For clarity, add the intermediate Kconfig symbol X86_HARDLOCKUP_DETECTOR
that is selected whenever the core of the hardlockup detector is
selected.

Cc: Andi Kleen <ak@xxxxxxxxxxxxxxx>
Cc: Stephane Eranian <eranian@xxxxxxxxxx>
Cc: "Ravi V. Shankar" <ravi.v.shankar@xxxxxxxxx>
Cc: iommu@xxxxxxxxxxxxxxxxxxxxxxxxxx
Cc: linuxppc-dev@xxxxxxxxxxxxxxxx
Cc: x86@xxxxxxxxxx
Suggested-by: Nicholas Piggin <npiggin@xxxxxxxxx>
Reviewed-by: Tony Luck <tony.luck@xxxxxxxxx>
Signed-off-by: Ricardo Neri <ricardo.neri-calderon@xxxxxxxxxxxxxxx>
---
Changes since v5:
* Added watchdog_nmi_start() to be used when tsc_khz is recalibrated.
* Always build the x86-specific hardlockup detector shim; not only
when the HPET-based detector is selected.
* Corrected a typo in comment in watchdog_nmi_probe() (Ani)
* Removed useless local ret variable in watchdog_nmi_enable(). (Ani)

Changes since v4:
* Use a switch to enable and disable the various available detectors.
(Andi)

Changes since v3:
* Fixed style in multi-line comment. (Randy Dunlap)

Changes since v2:
* Pass cpu number as argument to hardlockup_detector_[enable|disable].
(Thomas Gleixner)

Changes since v1:
* Introduced this patch: Added an x86-specific shim hardlockup
detector. (Nicholas Piggin)
---
arch/x86/Kconfig.debug | 3 ++
arch/x86/kernel/Makefile | 2 +
arch/x86/kernel/watchdog_hld.c | 85 ++++++++++++++++++++++++++++++++++
3 files changed, 90 insertions(+)
create mode 100644 arch/x86/kernel/watchdog_hld.c

diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug
index bc34239589db..599001157847 100644
--- a/arch/x86/Kconfig.debug
+++ b/arch/x86/Kconfig.debug
@@ -6,6 +6,9 @@ config TRACE_IRQFLAGS_NMI_SUPPORT
config EARLY_PRINTK_USB
bool

+config X86_HARDLOCKUP_DETECTOR
+ def_bool y if HARDLOCKUP_DETECTOR_CORE
+
config X86_VERBOSE_BOOTUP
bool "Enable verbose x86 bootup info messages"
default y
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index c700b00a2d86..af3d54e4c836 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -114,6 +114,8 @@ obj-$(CONFIG_KGDB) += kgdb.o
obj-$(CONFIG_VM86) += vm86_32.o
obj-$(CONFIG_EARLY_PRINTK) += early_printk.o

+obj-$(CONFIG_X86_HARDLOCKUP_DETECTOR) += watchdog_hld.o
+
obj-$(CONFIG_HPET_TIMER) += hpet.o
obj-$(CONFIG_X86_HARDLOCKUP_DETECTOR_HPET) += watchdog_hld_hpet.o

diff --git a/arch/x86/kernel/watchdog_hld.c b/arch/x86/kernel/watchdog_hld.c
new file mode 100644
index 000000000000..ef11f0af4ef5
--- /dev/null
+++ b/arch/x86/kernel/watchdog_hld.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A shim hardlockup detector. It overrides the weak stubs of the generic
+ * implementation to select between the perf- or the hpet-based implementation.
+ *
+ * Copyright (C) Intel Corporation 2022
+ */
+
+#include <linux/nmi.h>
+#include <asm/hpet.h>
+
+enum x86_hardlockup_detector {
+ X86_HARDLOCKUP_DETECTOR_PERF,
+ X86_HARDLOCKUP_DETECTOR_HPET,
+};
+
+static enum __read_mostly x86_hardlockup_detector detector_type;
+
+int watchdog_nmi_enable(unsigned int cpu)
+{
+ switch (detector_type) {
+ case X86_HARDLOCKUP_DETECTOR_PERF:
+ hardlockup_detector_perf_enable();
+ break;
+ case X86_HARDLOCKUP_DETECTOR_HPET:
+ hardlockup_detector_hpet_enable(cpu);
+ break;
+ default:
+ return -ENODEV;
+ }
+
+ return 0;
+}
+
+void watchdog_nmi_disable(unsigned int cpu)
+{
+ switch (detector_type) {
+ case X86_HARDLOCKUP_DETECTOR_PERF:
+ hardlockup_detector_perf_disable();
+ break;
+ case X86_HARDLOCKUP_DETECTOR_HPET:
+ hardlockup_detector_hpet_disable(cpu);
+ break;
+ }
+}
+
+int __init watchdog_nmi_probe(void)
+{
+ int ret;
+
+ /*
+ * Try first with the HPET hardlockup detector. It will only
+ * succeed if selected at build time and requested in the
+ * nmi_watchdog command-line parameter. This ensures that the
+ * perf-based detector is used by default, if selected at
+ * build time.
+ */
+ ret = hardlockup_detector_hpet_init();
+ if (!ret) {
+ detector_type = X86_HARDLOCKUP_DETECTOR_HPET;
+ return ret;
+ }
+
+ ret = hardlockup_detector_perf_init();
+ if (!ret) {
+ detector_type = X86_HARDLOCKUP_DETECTOR_PERF;
+ return ret;
+ }
+
+ return 0;
+}
+
+void watchdog_nmi_stop(void)
+{
+ /* Only the HPET lockup detector defines a stop function. */
+ if (detector_type == X86_HARDLOCKUP_DETECTOR_HPET)
+ hardlockup_detector_hpet_stop();
+}
+
+void watchdog_nmi_start(void)
+{
+ /* Only the HPET lockup detector defines a start function. */
+ if (detector_type == X86_HARDLOCKUP_DETECTOR_HPET)
+ hardlockup_detector_hpet_start();
+}
--
2.17.1