x86: Implement an HPET-based hardlockup detector
Subsystems (4)
v5 cover x86: Implement an HPET-based hardlockup detector
Hi,
This is the long overdue fifth attempt to implement a hardlockup detector that uses an HPET channel. Previous versions can be found in [1], [2], [3], and [4]. I will soon post in the IOMMU mailing list a separate series to deal with interrupt remapping. Thus, x86 reviewers may hold off on their review momentarily while the IOMMU experts decide on whether what I propose makes sense.
This RFC series has been reviewed by Tony Luck <[email protected]>
== Introduction ==
In CPU architectures that do not have an NMI watchdog, one can be constructed using a counter of the Performance Monitoring Unit (PMU). Counters in the PMU have high granularity and high visibility of the CPU. These capabilities and their limited number make these counters precious resources. Unfortunately, the perf-based hardlockup detector permanently consumes one of these counters per CPU. These counters could be freed if the hardlockup detector were driven by another timer.
The hardlockup detector runs relatively infrequently and does not require visibility of the CPU activity (in addition to detect locked-up CPUs). A timer that is external to the CPU (e.g., in the chipset) can be used to drive the detector.
A key requirement is that the timer needs to be capable of issuing a non-maskable interrupt to the CPU. In most cases, this can be achieved by tweaking the delivery mode of the interrupt. It is especially straightforward for MSI interrupts.
This implementation uses an HPET timer to deliver an NMI interrupt via an MSI message.
== Details of this implementation
Unlike the perf-based hardlockup detector, this implementation is driven by a single timer. Driving the detector with a single timer brings certain complexities to the implementation: accessing the HPET timer is slow, and the frequency and the affinity of the timer interrupt needs to be adjusted periodically. These operations need to happen in addition to servicing the NMI. In order to address these complexities, this design meets the following goals:
* Minimize updates to the affinity of the HPET timer interrupt and do it outside of NMI context.
* Avoid races with System Management Mode that may lead to see the HPET count stall or run backwards.
* Minimize the number of reads and writes to the HPET registers.
Also, as per feedback from Thomas Gleixner,
* Do not implement an IRQF_NMI to request an NMI interrupt for x86 due to the difficulty of identifying its source.
In order to meet the goals above, I implemented what Thomas suggested [5]:
a detector that mixes inter-processor interrupts (IPIs) and updating the affinity of the HPET interrupt in a round-robin fashion.
The CPUs that the hardlockup detector monitors are partitioned into groups. A CPU from such group, the handling CPU, handles the NMI interrupt from the HPET timer and then issues an NMI IPI to the rest of the CPUs in the group. Each of the CPUs looks for hardlockups upon reception of the NMI. To minimize IPIs among packages, a group constitutes all the CPUs in a package.
Since the monitored CPUs now have been partitioned in groups, the HPET timer needs to target the handling CPU of each group. Hence, the affinity of the HPET timer is updated in round-robin manner to sequentially target each group of CPUs. Each group of CPUs will be monitored every 1 second or less frequently. This is the frequency of the HPET timer interrupt. In the unlikely case of having more packages than the value watch_thresh, several packages will be grouped together to keep the HPET timer interrupt at 1 second.
In order to avoid reading HPET registers in every NMI, the time-stamp counter is used to determine whether the HPET caused the interrupt. At every timer expiration, we compute the value the time-stamp counter is expected to have the next time the timer expires. I have found experimentally that expected TSC value consistently has an error of less than 1%.
== Parts of this series ==
For clarity, patches are grouped as follows:
1) HPET updates. Patches 1-3 prepare the HPET code to accommodate the new detector: rework periodic programming, reserve and configure a timer for the detector and expose a few existing functions.
3) NMI watchdog. Patches 4-6 updates the existing hardlockup detector to uncouple it from perf, and introduces a new NMI handler category intended to run after the NMI_LOCAL handlers.
4) New HPET-based hardlockup detector. Patches 7-11 includes changes to probe the hardware resources, configure the interrupt, and rotate the destination of the interrupts among all monitored CPUs.
5) Hardlockup detector management. Patches 12-16 is a collection of miscellaneous patches to determine when to use the HPET hardlockup detector and stop it if necessary. It also includes an x86-specific shim hardlockup detector that selects between the perf- and hpet-based implementations. It also switches back to the perf implementation if the TSC becomes unstable.
== Testing ==
I tested this series in both client and server systems, single-package and dual-package systems. I let the system run normally and observed the NMI interrupts keep coming by inspecting the file /proc/interrupts.
I also implemented a test module to hog a CPU and observed the detector complain about soft and hardlockups.
Lastly, I wrote a script to keep only one CPU online at a time in a round- robin manner by doing 500 back-to-back CPU hotplug online/offline 500 times. During the test, I set /proc/sys/kernel/watchdog_thresh to 1 second. The HPET-based hardlockup detector is still functional after the test. I used the latest master branch from the tip tree.
The tests above were performed with interrupt remapping both enabled and disabled.
Thanks and BR,
Ricardo
Changes since v4:
* Added commentary on the tests performed on this feature. (Andi)
* Added a stub version of hardlockup_detector_switch_to_perf() for !CONFIG_HPET_TIMER. (lkp)
* Use switch to select the type of x86 hardlockup detector. (Andi)
* Renamed a local variable in update_ticks_per_group(). (Andi)
* Made this hardlockup detector available to X86_32.
* Reworked logic to kick the HPET timer to remove a local variable. (Andi)
* Added a comment on what type of timer channel will be assigned to the detector. (Andi)
* Reworded help comment in the X86_HARDLOCKUP_DETECTOR_HPET Kconfig option. (Andi)
* Removed unnecessary switch to level interrupt mode when disabling the timer. (Andi)
* Disabled the HPET timer to avoid a race between an incoming interrupt and an update of the MSI destination ID. (Ashok)
* Renamed hpet_hardlockup_detector_get_timer() as hpet_hld_get_timer()
* Added commentary on an undocumented udelay() when programming an HPET channel in periodic mode. (Ashok)
* Reworked code to use new enumeration apic_delivery_modes and reworked MSI message composition fields [6].
* Partitioned monitored CPUs into groups. Each CPU in the group is inspected for hardlockups using an IPI.
* Use a round-robin mechanism to update the affinity of the HPET timer. Affinity is updated every watchdog_thresh seconds to target the handling CPU of the group.
* Moved update of the HPET interrupt affinity to an irq_work. (Thomas Gleixner).
* Updated expiration of the HPET timer and the expected value of the TSC based on the number of groups of monitored CPUs.
* Renamed hpet_set_comparator() to hpet_set_comparator_periodic() to remove decision logic for periodic case. (Thomas Gleixner)
* Reworked timer reservation to use Thomas' rework on HPET channel management[7].
* Removed hard-coded channel number for the hardlockup detector.
* Provided more details on the sequence of HPET channel reservation. (Thomas Gleixner)
* Only reserve a channel for the hardlockup detector if enabled via kernel command line. The function reserving the channel is called from hardlockup detector. (Thomas Gleixner)
* Dropped hpet_hld_data::enabled_cpus and instead use cpumask_weight().
* Renamed hpet_hld_data::cpu_monitored_mask to hld_data_data.cpu_monitored_mask and converted it to cpumask_var_t.
* Flushed out any outstanding interrupt before enabling the HPET channel.
* Removed unnecessary MSI_DATA_LEVEL_ASSERT from the MSI message.
* Added comments in hardlockup_detector_nmi_handler() to explain how CPUs are targeted for an IPI.
* Updated code to only issue an IPI when needed (i.e., there are CPUs in the group other than the handling CPU).
* Reworked hardlockup_detector_hpet_init() for readability.
* Now reserve the cpumasks in the hardlockup detector code and not in the generic HPET code.
* Handle the case of watchdog_thresh = 0 when disabling the detector.
Change since v3:
* Fixed yet another bug in periodic programming of the HPET timer that prevented the system from booting.
* Fixed computation of HPET frequency to use hpet_readl() only.
* Added a missing #include in the watchdog_hld_hpet.c
* Fixed various typos and grammar errors (Randy Dunlap)
Changes since v2:
* Added functionality to switch to the perf-based hardlockup detector if the TSC becomes unstable (Thomas Gleixner).
* Brought back the round-robin mechanism proposed in v1 (this time not using the interrupt subsystem). This also requires computing expiration times as in v1 (Andi Kleen, Stephane Eranian).
* Fixed a bug in which using a periodic timer was not working(thanks to Suravee Suthikulpanit!).
* In this version, I incorporate support for interrupt remapping in the last 4 patches so that they can be reviewed separately if needed.
* Removed redundant documentation of functions (Thomas Gleixner).
* Added a new category of NMI handler, NMI_WATCHDOG, which executes after NMI_LOCAL handlers (Andi Kleen).
* Updated handling of "nmi_watchdog" to support comma-separated arguments.
* Undid split of the generic hardlockup detector into a separate file (Thomas Gleixner).
* Added a new intermediate symbol CONFIG_HARDLOCKUP_DETECTOR_CORE to select generic parts of the detector (Paul E. McKenney, Thomas Gleixner).
* Removed use of struct cpumask in favor of a variable length array in conjunction with kzalloc (Peter Zijlstra).
* Added CPU as argument hardlockup_detector_hpet_enable()/disable() (Thomas Gleixner).
* Remove unnecessary export of function declarations, flags, and bit fields (Thomas Gleixner).
* Removed unnecessary check for FSB support when reserving timer for the detector (Thomas Gleixner).
* Separated TSC code from HPET code in kick_timer() (Thomas Gleixner).
* Reworked condition to check if the expected TSC value is within the error margin to avoid conditional (Peter Zijlstra).
* Removed TSC error margin from struct hld_data; use global variable instead (Peter Zijlstra).
* Removed previously introduced watchdog_get_allowed_cpumask*() and reworked hardlockup_detector_hpet_enable()/disable() to not need access to watchdog_allowed_mask (Thomas Gleixner).
Changes since v1:
* Removed reads to HPET registers at every NMI. Instead use the time-stamp counter to infer the interrupt source (Thomas Gleixner, Andi Kleen).
* Do not target CPUs in a round-robin manner. Instead, the HPET timer always targets the same CPU; other CPUs are monitored via an interprocessor interrupt.
* Removed use of generic irq code to set interrupt affinity and NMI delivery. Instead, configure the interrupt directly in HPET registers (Thomas Gleixner).
* Removed the proposed ops structure for NMI watchdogs. Instead, split the existing implementation into a generic library and perf-specific infrastructure (Thomas Gleixner, Nicholas Piggin).
* Added an x86-specific shim hardlockup detector that selects between HPET and perf infrastructures as needed (Nicholas Piggin).
* Removed locks taken in NMI and !NMI context. This was wrong and is no longer needed (Thomas Gleixner).
* Fixed unconditional return NMI_HANDLED when the HPET timer is programmed for FSB/MSI delivery (Peter Zijlstra).
[1]. https://lore.kernel.org/lkml/1528851463-21140-1-git-send-email-ricardo.neri-calderon@linux.intel.com/
[2]. https://lore.kernel.org/lkml/1551283518-18922-1-git-send-email-ricardo.neri-calderon@linux.intel.com/
[3]. https://lore.kernel.org/lkml/1557842534-4266-1-git-send-email-ricardo.neri-calderon@linux.intel.com/
[4]. https://lore.kernel.org/lkml/1558660583-28561-1-git-send-email-ricardo.neri-calderon@linux.intel.com/
[5]. https://lore.kernel.org/lkml/[email protected]/
[6]. https://lore.kernel.org/r/[email protected]
[7]. https://lore.kernel.org/lkml/[email protected]/
Ricardo Neri (16):
x86/hpet: Expose hpet_writel() in header
x86/hpet: Add helper function hpet_set_comparator_periodic() x86/hpet: Reserve an HPET channel for the hardlockup detector watchdog/hardlockup: Define a generic function to detect hardlockups watchdog/hardlockup: Decouple the hardlockup detector from perf x86/nmi: Add an NMI_WATCHDOG NMI handler category x86/watchdog/hardlockup: Add an HPET-based hardlockup detector x86/watchdog/hardlockup/hpet: Introduce a target_cpumask watchdog/hardlockup/hpet: Group packages receiving IPIs when needed watchdog/hardlockup/hpet: Adjust timer expiration on the number of
monitored groups
x86/watchdog/hardlockup/hpet: Determine if HPET timer caused NMI watchdog/hardlockup: Use parse_option_str() to handle "nmi_watchdog" watchdog/hardlockup/hpet: Only enable the HPET watchdog via a boot
parameter
x86/watchdog: Add a shim hardlockup detector
watchdog: Expose lockup_detector_reconfigure()
x86/tsc: Switch to perf-based hardlockup detector if TSC become
unstable
.../admin-guide/kernel-parameters.txt | 8 +- arch/x86/Kconfig.debug | 14 + arch/x86/include/asm/hpet.h | 67 ++ arch/x86/include/asm/nmi.h | 1 + arch/x86/kernel/Makefile | 2 + arch/x86/kernel/hpet.c | 124 +++- arch/x86/kernel/nmi.c | 10 + arch/x86/kernel/tsc.c | 2 + arch/x86/kernel/watchdog_hld.c | 86 +++ arch/x86/kernel/watchdog_hld_hpet.c | 633 ++++++++++++++++++ include/linux/nmi.h | 8 +- kernel/Makefile | 2 +- kernel/watchdog.c | 12 +- kernel/watchdog_hld.c | 50 +- lib/Kconfig.debug | 4 + 15 files changed, 982 insertions(+), 41 deletions(-) create mode 100644 arch/x86/kernel/watchdog_hld.c create mode 100644 arch/x86/kernel/watchdog_hld_hpet.c
Signature · 1 line
2.17.1
RFC PATCH v5 1/16 x86/hpet: Expose hpet_writel() in header
In order to allow hpet_writel() to be used by other components (e.g., the HPET-based hardlockup detector), expose it in the HPET header file.
---
Changes since v4:
* Dropped exposing hpet_readq() as it is not needed.
Changes since v3:
* None
Changes since v2:
* None
Changes since v1:
* None
--- arch/x86/include/asm/hpet.h | 1 + arch/x86/kernel/hpet.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
arch/x86/include/asm/hpet.h +1 -0
@@ -72,6 +72,7 @@ extern int is_hpet_enabled(void);extern int hpet_enable (void );extern void hpet_disable (void );extern unsigned int hpet_readl (unsigned int a); +extern void hpet_writel (unsigned int d,unsigned int a);extern void force_hpet_resume (void );#ifdef CONFIG_HPET_EMULATE_RTC
arch/x86/kernel/hpet.c +1 -1
@@ -78,7 +78,7 @@ inline unsigned int hpet_readl(unsigned int a)return readl (hpet_virt_address + a); } -static inline void hpet_writel (unsigned int d,unsigned int a) +inline void hpet_writel (unsigned int d,unsigned int a) {writel (d, hpet_virt_address + a); }
Signature · 1 line
2.17.1
RFC PATCH v5 2/16 x86/hpet: Add helper function hpet_set_comparator_periodic()
In reply to Ricardo NeriProgramming an HPET channel as periodic requires setting the HPET_TN_SETVAL bit in the channel configuration. Plus, the comparator register must be written twice (once for the comparator value and once for the periodic value). Since this programming might be needed in several places (e.g., the HPET clocksource and the HPET-based hardlockup detector), add a helper function for this purpose.
A helper function hpet_set_comparator_oneshot() could also be implemented. However, such function would only program the comparator register and the function would be quite small. Hence, it is better to not bloat the code with such an obvious function.
---
When programming the HPET channel in periodic mode, a udelay(1) between the two successive writes to HPET_Tn_CMP was introduced in commit e9e2cdb41241 ("[PATCH] clockevents: i386 drivers"). The commit message does not give any reason for such delay. The hardware specification does not seem to require it. The refactoring in this patch simply carries such delay.
---
Changes since v4:
* Implement function only for periodic mode. This removed extra logic to to use a non-zero period value as a proxy for periodic mode programming. (Thomas)
* Added a comment on the history of the udelay() when programming the channel in periodic mode. (Ashok)
Changes since v3:
* Added back a missing hpet_writel() for time configuration.
Changes since v2:
* Introduced this patch.
Changes since v1:
* N/A
--- arch/x86/include/asm/hpet.h | 2 ++ arch/x86/kernel/hpet.c | 49 ++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-)
arch/x86/include/asm/hpet.h +2 -0
@@ -74,6 +74,8 @@ extern void hpet_disable(void);extern unsigned int hpet_readl (unsigned int a);extern void hpet_writel (unsigned int d,unsigned int a);extern void force_hpet_resume (void ); +extern void hpet_set_comparator_periodic (int channel,unsigned int cmp, +unsigned int period);#ifdef CONFIG_HPET_EMULATE_RTC
arch/x86/kernel/hpet.c +37 -12
62 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v5 3/16 x86/hpet: Reserve an HPET channel for the hardlockup detector
In reply to Ricardo NeriThe HPET hardlockup detector needs a dedicated HPET channel. Hence, create a new HPET_MODE_NMI_WATCHDOG mode category to indicate that it cannot be used for other purposes. Using MSI interrupts greatly simplifies the implementation of the detector. Specifically, it helps to avoid the complexities of routing the interrupt via the IO-APIC (e.g., potential race conditions that arise from re-programming the IO-APIC while also servicing an NMI). Therefore, only reserve the timer if it supports Front Side Bus interrupt delivery.
HPET channels are reserved at various stages. First, from x86_late_time_init(), hpet_time_init() checks if the HPET timer supports Legacy Replacement Routing. If this is the case, channels 0 and 1 are reserved as HPET_MODE_LEGACY.
At a later stage, from lockup_detector_init(), reserve the HPET channel for the hardlockup detector. Then, the HPET clocksource reserves the channels it needs and then the remaining channels are given to the HPET char driver via hpet_alloc().
Hence, the channel assigned to the HPET hardlockup detector depends on whether the first two channels are reserved for legacy mode.
Lastly, only reserve the channel for the hardlockup detector if enabled in the kernel command line.
---
Changes since v4:
* Reworked timer reservation to use Thomas' rework on HPET channel management.
* Removed hard-coded channel number for the hardlockup detector.
* Provided more details on the sequence of HPET channel reservations. (Thomas Gleixner)
* Only reserve a channel for the hardlockup detector if enabled via kernel command line. The function reserving the channel is called from hardlockup detector. (Thomas Gleixner)
* Shorten the name of hpet_hardlockup_detector_get_timer() to hpet_hld_get_timer(). (Andi)
* Simplify error handling when a channel is not found. (Tony)
Changes since v3:
* None
Changes since v2:
* None
Changes since v1:
* None
--- arch/x86/include/asm/hpet.h | 18 +++++++++ arch/x86/kernel/hpet.c | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+)
arch/x86/include/asm/hpet.h +18 -0
@@ -95,6 +95,24 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);#endif /* CONFIG_HPET_EMULATE_RTC */ +#ifdef CONFIG_X86_HARDLOCKUP_DETECTOR_HPET +/** +* struct hpet_hld_data - Data needed to operate the detector +* @has_periodic: The HPET channel supports periodic mode +* @channel: HPET channel assigned to the detector +* @ticks_per_second: Frequency of the HPET timer +* @irq: IRQ number assigned to the HPET channel +*/ +struct hpet_hld_data { +bool has_periodic; +u32 channel; +u64 ticks_per_second; +int irq; +}; + +extern struct hpet_hld_data *hpet_hld_get_timer (void ); +#endif /* CONFIG_X86_HARDLOCKUP_DETECTOR_HPET */ +#else /* CONFIG_HPET_TIMER */ static inline int hpet_enable (void ) {return 0 ; }
arch/x86/kernel/hpet.c +73 -0
89 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v5 4/16 watchdog/hardlockup: Define a generic function to detect hardlockups
In reply to Ricardo NeriThe procedure to detect hardlockups is independent of the underlying mechanism that generates the non-maskable interrupt used to drive the detector. Thus, it can be put in a separate, generic function. In this manner, it can be invoked by various implementations of the NMI watchdog.
For this purpose, move the bulk of watchdog_overflow_callback() to the new function inspect_for_hardlockups(). This function can then be called from the applicable NMI handlers.
---
Changes since v4:
* None
Changes since v3:
* None
Changes since v2:
* None
Changes since v1:
* None
--- include/linux/nmi.h | 1 + kernel/watchdog_hld.c | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-)
include/linux/nmi.h +1 -0
@@ -207,6 +207,7 @@ int proc_nmi_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *);int proc_soft_watchdog (struct ctl_table *,int ,void *,size_t *,loff_t *);int proc_watchdog_thresh (struct ctl_table *,int ,void *,size_t *,loff_t *);int proc_watchdog_cpumask (struct ctl_table *,int ,void *,size_t *,loff_t *); +void inspect_for_hardlockups (struct pt_regs *regs);#ifdef CONFIG_HAVE_ACPI_APEI_NMI #include <asm/nmi.h>
kernel/watchdog_hld.c +11 -7
@@ -106,14 +106,8 @@ static struct perf_event_attr wd_hw_attr = { .disabled = 1, }; -/* Callback function for perf event subsystem */ -static void watchdog_overflow_callback (struct perf_event *event, -struct perf_sample_data *data, -struct pt_regs *regs) +void inspect_for_hardlockups (struct pt_regs *regs) { -/* Ensure the watchdog never gets throttled */ - event->hw.interrupts =0 ; -if (__this_cpu_read (watchdog_nmi_touch) == true) {__this_cpu_write (watchdog_nmi_touch, false);return ;
@@ -163,6 +157,16 @@ static void watchdog_overflow_callback(struct perf_event *event,return ; } +/* Callback function for perf event subsystem */ +static void watchdog_overflow_callback (struct perf_event *event, +struct perf_sample_data *data, +struct pt_regs *regs) +{ +/* Ensure the watchdog never gets throttled */ + event->hw.interrupts =0 ; +inspect_for_hardlockups (regs); +} +static int hardlockup_detector_event_create (void ) {unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.17.1
RFC PATCH v5 5/16 watchdog/hardlockup: Decouple the hardlockup detector from perf
In reply to Ricardo NeriThe current default implementation of the hardlockup detector assumes that it is implemented using perf events. However, the hardlockup detector can be driven by other sources of non-maskable interrupts (e.g., a properly configured timer).
Group and wrap in #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF all the code specific to perf: create and manage perf events, stop and start the perf- based detector.
The generic portion of the detector (monitor the timers' thresholds, check timestamps and detect hardlockups as well as the implementation of arch_touch_nmi_watchdog()) is now selected with the new intermediate config symbol CONFIG_HARDLOCKUP_DETECTOR_CORE.
The perf-based implementation of the detector selects the new intermediate symbol. Other implementations should do the same.
---
Changes since v4:
* None
Changes since v3:
* Squashed into this patch a previous patch to make arch_touch_nmi_watchdog() part of the core detector code.
Changes since v2:
* Undid split of the generic hardlockup detector into a separate file. (Thomas Gleixner)
* Added a new intermediate symbol CONFIG_HARDLOCKUP_DETECTOR_CORE to select generic parts of the detector (Paul E. McKenney, Thomas Gleixner).
Changes since v1:
* Make the generic detector code with CONFIG_HARDLOCKUP_DETECTOR.
--- include/linux/nmi.h | 5 ++++- kernel/Makefile | 2 +- kernel/watchdog_hld.c | 32 ++++++++++++++++++++------------ lib/Kconfig.debug | 4 ++++ 4 files changed, 29 insertions(+), 14 deletions(-)
include/linux/nmi.h +4 -1
@@ -94,8 +94,11 @@ static inline void hardlockup_detector_disable(void) {}# define NMI_WATCHDOG_SYSCTL_PERM 0444#endif -#if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF ) +#if defined(CONFIG_HARDLOCKUP_DETECTOR_CORE )extern void arch_touch_nmi_watchdog (void ); +#endif + +#if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF )extern void hardlockup_detector_perf_stop (void );extern void hardlockup_detector_perf_restart (void );extern void hardlockup_detector_perf_disable (void );
kernel/Makefile +1 -1
@@ -95,7 +95,7 @@ obj-$(CONFIG_FAIL_FUNCTION) += fail_function.o obj-$(CONFIG_KGDB) += debug/ obj-$(CONFIG_DETECT_HUNG_TASK) += hung_task.o obj-$(CONFIG_LOCKUP_DETECTOR) += watchdog.o -obj-$(CONFIG_HARDLOCKUP_DETECTOR_PERF) += watchdog_hld.o +obj-$(CONFIG_HARDLOCKUP_DETECTOR_CORE) += watchdog_hld.o obj-$(CONFIG_SECCOMP) += seccomp.o obj-$(CONFIG_RELAY) += relay.o obj-$(CONFIG_SYSCTL) += utsname_sysctl.o
kernel/watchdog_hld.c +20 -12
@@ -22,12 +22,8 @@ static DEFINE_PER_CPU(bool, hard_watchdog_warn); static DEFINE_PER_CPU(bool, watchdog_nmi_touch); -static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); -static DEFINE_PER_CPU(struct perf_event *, dead_event); -static struct cpumask dead_events_mask;static unsigned long hardlockup_allcpu_dumped; -static atomic_t watchdog_cpus =ATOMIC_INIT (0 ); notrace void arch_touch_nmi_watchdog(void) {
@@ -98,14 +94,6 @@ static inline bool watchdog_check_timestamp(void) }#endif -static struct perf_event_attr wd_hw_attr = { - .type =PERF_TYPE_HARDWARE , - .config =PERF_COUNT_HW_CPU_CYCLES , - .size =sizeof (struct perf_event_attr ), - .pinned =1 , - .disabled =1 , -}; -void inspect_for_hardlockups (struct pt_regs *regs) {if (__this_cpu_read (watchdog_nmi_touch) == true) {
@@ -157,6 +145,24 @@ void inspect_for_hardlockups(struct pt_regs *regs)return ; } +#ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF +#undef pr_fmt +#define pr_fmt (fmt) "NMI perf watchdog: " fmt + +static DEFINE_PER_CPU(struct perf_event *, watchdog_ev); +static DEFINE_PER_CPU(struct perf_event *, dead_event); +static struct cpumask dead_events_mask; + +static atomic_t watchdog_cpus =ATOMIC_INIT (0 ); + +static struct perf_event_attr wd_hw_attr = { + .type =PERF_TYPE_HARDWARE , + .config =PERF_COUNT_HW_CPU_CYCLES , + .size =sizeof (struct perf_event_attr ), + .pinned =1 , + .disabled =1 , +}; +/* Callback function for perf event subsystem */ static void watchdog_overflow_callback(struct perf_event *event, struct perf_sample_data *data,
@@ -298,3 +304,5 @@ int __init hardlockup_detector_perf_init(void) }return ret; } + +#endif /* CONFIG_HARDLOCKUP_DETECTOR_PERF */
lib/Kconfig.debug +4 -0
@@ -1021,9 +1021,13 @@ config BOOTPARAM_SOFTLOCKUP_PANIC_VALUE default 0 if !BOOTPARAM_SOFTLOCKUP_PANIC default 1 if BOOTPARAM_SOFTLOCKUP_PANIC +config HARDLOCKUP_DETECTOR_CORE + bool + config HARDLOCKUP_DETECTOR_PERF bool select SOFTLOCKUP_DETECTOR + select HARDLOCKUP_DETECTOR_CORE # # Enables a timestamp based low pass filter to compensate for perf based
Signature · 1 line
2.17.1
RFC PATCH v5 6/16 x86/nmi: Add an NMI_WATCHDOG NMI handler category
In reply to Ricardo NeriAdd a NMI_WATCHDOG as a new category of NMI handler. This new category is to be used with the HPET-based hardlockup detector. This detector does not have a direct way of checking if the HPET timer is the source of the NMI. Instead, it indirectly estimates it using the time-stamp counter.
Therefore, we may have false-positives in case another NMI occurs within the estimated time window. For this reason, we want the handler of the detector to be called after all the NMI_LOCAL handlers. A simple way of achieving this with a new NMI handler category.
---
Changes since v4:
* None
Changes since v3:
* None
Changes since v2:
* Introduced this patch.
Changes since v1:
* N/A
--- arch/x86/include/asm/nmi.h | 1 + arch/x86/kernel/nmi.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
arch/x86/include/asm/nmi.h +1 -0
@@ -28,6 +28,7 @@ enum {NMI_UNKNOWN ,NMI_SERR ,NMI_IO_CHECK , +NMI_WATCHDOG , NMI_MAX };
arch/x86/kernel/nmi.c +10 -0
@@ -61,6 +61,10 @@ static struct nmi_desc nmi_desc[NMI_MAX] = .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock), .head = LIST_HEAD_INIT(nmi_desc[3].head), }, + { + .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[4].lock), + .head = LIST_HEAD_INIT(nmi_desc[4].head), + }, };
@@ -168,6 +172,8 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)*/ WARN_ON_ONCE (type ==NMI_SERR && !list_empty (&desc->head));WARN_ON_ONCE (type ==NMI_IO_CHECK && !list_empty (&desc->head)); +WARN_ON_ONCE (type ==NMI_WATCHDOG && !list_empty (&desc->head)); +/* * some handlers need to be executed first otherwise a fake
@@ -380,6 +386,10 @@ static noinstr void default_do_nmi(struct pt_regs *regs) }raw_spin_unlock (&nmi_reason_lock); + handled =nmi_handle (NMI_WATCHDOG , regs); +if (handled ==NMI_HANDLED ) +return ; +/* * Only one NMI can be latched at a time. To handle * this we may process multiple nmi handlers at once to
Signature · 1 line
2.17.1
RFC PATCH v5 7/16 x86/watchdog/hardlockup: Add an HPET-based hardlockup detector
In reply to Ricardo NeriImplement a hardlockup detector that uses the HPET timer as the source of the non-maskable interrupt. Implement the basic functionality, to start and stop the timer and configure it issue an MSI interrupt (write directly to the HPET MSI registers instead the interrupt subsystem). Hence, the detector relies on an HPET channel capable of Front Side Bus interrupts.
A single CPU, the handling CPU, will service the non-maskable interrupt from the HPET timer. Then, it will issue an inter-processor interrupt to the rest of the CPUs monitored by the detector.
The detector is not functional at this stage. A subsequent changeset will invoke the interfaces implemented in this changeset as well as functionality to determine if the HPET timer caused the NMI. For now, implement a stub function.
HPET registers are only accessed to kick the timer after looking for hardlockups. If the HPET channel is periodic, there is no need to write to the HPET registers at all in interrupt context. On all the systems I inspected, only the first channel of the timer is periodic and it will usually be reserved for the HPET legacy clockevent. Thus, in most of the cases the detector will be assigned a non-periodic channel.
---
Changes since v4:
* Dropped hpet_hld_data.enabled_cpus and instead use cpumask_weight().
* Renamed hpet_hld_data.cpu_monitored_mask to hld_data_data.cpu_monitored_mask and converted it to cpumask_var_t.
* Flushed out any outstanding interrupt before enabling the HPET channel.
* Removed unnecessary MSI_DATA_LEVEL_ASSERT from the MSI message.
* Added comments in hardlockup_detector_nmi_handler() to explain how CPUs are targeted for an IPI.
* Updated code to only issue an IPI when needed (i.e., there are monitored CPUs to be inspected via an IPI).
* Reworked hardlockup_detector_hpet_init() for readability.
* Now reserve the cpumasks in the hardlockup detector code and not in the generic HPET code.
* Handled the case of watchdog_thresh = 0 when disabling the detector.
* Made this detector available to i386.
* Reworked logic to kick the timer to remove a local variable. (Andi)
* Added a comment on what type of timer channel will be assigned to the detector. (Andi)
* Reworded prompt comment in Kconfig. (Andi)
* Removed unneeded switch to level interrupt mode when disabling the timer. (Andi)
* Disabled the HPET timer to avoid a race between an incoming interrupt and an update of the MSI destination ID. (Ashok)
* Corrected a typo in an inline comment. (Tony)
* Made the HPET hardlockup detector depend on HARDLOCKUP_DETECTOR instead of selecting it.
Changes since v3:
* Fixed typo in Kconfig.debug. (Randy Dunlap)
* Added missing slab.h to include the definition of kfree to fix a build break.
Changes since v2:
* Removed use of struct cpumask in favor of a variable length array in conjunction with kzalloc. (Peter Zijlstra)
* Removed redundant documentation of functions. (Thomas Gleixner)
* Added CPU as argument hardlockup_detector_hpet_enable()/disable(). (Thomas Gleixner).
Changes since v1:
* Do not target CPUs in a round-robin manner. Instead, the HPET timer always targets the same CPU; other CPUs are monitored via an interprocessor interrupt.
* Dropped support for IO APIC interrupts and instead use only MSI interrupts.
* Removed use of generic irq code to set interrupt affinity and NMI delivery. Instead, configure the interrupt directly in HPET registers. (Thomas Gleixner)
* Fixed unconditional return NMI_HANDLED when the HPET timer is programmed for FSB/MSI delivery. (Peter Zijlstra)
--- arch/x86/Kconfig.debug | 10 + arch/x86/include/asm/hpet.h | 21 ++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/watchdog_hld_hpet.c | 384 ++++++++++++++++++++++++++++ 4 files changed, 416 insertions(+) create mode 100644 arch/x86/kernel/watchdog_hld_hpet.c
arch/x86/Kconfig.debug +10 -0
@@ -117,6 +117,16 @@ config IOMMU_LEAK config HAVE_MMIOTRACE_SUPPORT def_bool y +config X86_HARDLOCKUP_DETECTOR_HPET + bool "HPET Timer for Hard Lockup Detection" + select HARDLOCKUP_DETECTOR_CORE + depends on HARDLOCKUP_DETECTOR && HPET_TIMER && HPET && (X86_64 || X86_32) + help + The hardlockup detector is driven by one counter of the Performance + Monitoring Unit (PMU) per CPU. Say y to instead drive the + hardlockup detector using a High-Precision Event Timer and make the + PMU counters available for other purposes. + config X86_DECODER_SELFTEST bool "x86 instruction decoder selftest" depends on DEBUG_KERNEL && INSTRUCTION_DECODER
arch/x86/include/asm/hpet.h +21 -0
@@ -3,6 +3,7 @@#define _ASM_X86_HPET_H#include <linux/msi.h> +#include <linux/irq_work.h> #ifdef CONFIG_HPET_TIMER
@@ -102,15 +103,35 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);* @channel: HPET channel assigned to the detector * @ticks_per_second: Frequency of the HPET timer * @irq: IRQ number assigned to the HPET channel +* @handling_cpu: CPU handling the HPET interrupt +* @msi_msg: MSI message to be written it the HPET registers +* @monitored_cpumask: CPUs monitored by the hardlockup detector +* @ipi_cpumask: Auxiliary mask to handle IPIs. Both sending and +* receiving CPUs write to it. Hence, we cannot +* reuse @monitored_cpumask. */ struct hpet_hld_data {bool has_periodic;u32 channel;u64 ticks_per_second;int irq; +u32 handling_cpu; +struct msi_msg msi_msg; +cpumask_var_t monitored_cpumask; +cpumask_var_t ipi_cpumask; };extern struct hpet_hld_data *hpet_hld_get_timer (void ); +extern int hardlockup_detector_hpet_init (void ); +extern void hardlockup_detector_hpet_stop (void ); +extern void hardlockup_detector_hpet_enable (unsigned int cpu); +extern void hardlockup_detector_hpet_disable (unsigned int cpu); +#else +static inline int hardlockup_detector_hpet_init (void ) +{return -ENODEV ; } +static inline void hardlockup_detector_hpet_stop (void ) {} +static inline void hardlockup_detector_hpet_enable (unsigned int cpu) {} +static inline void hardlockup_detector_hpet_disable (unsigned int cpu) {}#endif /* CONFIG_X86_HARDLOCKUP_DETECTOR_HPET */ #else /* CONFIG_HPET_TIMER */
arch/x86/kernel/Makefile +1 -0
@@ -115,6 +115,7 @@ obj-$(CONFIG_VM86) += vm86_32.o obj-$(CONFIG_EARLY_PRINTK) += early_printk.o obj-$(CONFIG_HPET_TIMER) += hpet.o +obj-$(CONFIG_X86_HARDLOCKUP_DETECTOR_HPET) += watchdog_hld_hpet.o obj-$(CONFIG_AMD_NB) += amd_nb.o obj-$(CONFIG_DEBUG_NMI_SELFTEST) += nmi_selftest.o
arch/x86/kernel/watchdog_hld_hpet.c new file +384 -0
384 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [RFC PATCH v5 07/16] x86/watchdog/hardlockup: Add an HPET-based hardlockup detector
Ricardo,
Quoted 12 lines · Ricardo Neri
arch/x86/kernel/watchdog_hld_hpet.c new file +12
-0+static int hardlockup_detector_nmi_handler (unsigned int type, +struct pt_regs *regs) +{ +struct hpet_hld_data *hdata = hld_data; +int cpu =smp_processor_id (); + +if (is_hpet_wdt_interrupt (hdata)) { +/* +* Make a copy of the target mask. We need this as once a CPU +* gets the watchdog NMI it will clear itself from ipi_cpumask. +* Also, target_cpumask will be updated in a workqueue for the +* next NMI IPI.
arch/x86/kernel/watchdog_hld_hpet.c line 206
Quoted 10 lines
arch/x86/kernel/watchdog_hld_hpet.c new file +10
-0+*/ +cpumask_copy (hld_data->ipi_cpumask, hld_data->monitored_cpumask); +/* +* Even though the NMI IPI will be sent to all CPUs but self, +* clear the CPU to identify a potential unrelated NMI. +*/ +cpumask_clear_cpu (cpu, hld_data->ipi_cpumask); +if (cpumask_weight (hld_data->ipi_cpumask)) + apic->send_IPI_mask_allbutself (hld_data->ipi_cpumask, +NMI_VECTOR );
How is this supposed to work correctly?
x2apic_cluster:
x2apic_send_IPI_mask_allbutself()
__x2apic_send_IPI_mask()
tmpmsk = this_cpu_cpumask_var_ptr(ipi_mask); cpumask_copy(tmpmsk, mask);
So if an NMI hits right after or in the middle of the cpumask_copy() then the IPI sent from that NMI overwrites tmpmask and when its done then tmpmask is empty. Similar to when it hits in the middle of processing, just with the difference that maybe a few IPIs have been sent already. But the not yet sent ones are lost...
Also anything which ends up in __default_send_IPI_dest_field() is borked:
__default_send_IPI_dest_field()
cfg = __prepare_ICR2(mask);
native_apic_mem_write(APIC_ICR2, cfg);
<- NMI hits and invokes IPI which invokes __default_send_IPI_dest_field()...
cfg = __prepare_ICR(0, vector, dest);
native_apic_mem_write(APIC_ICR, cfg);IOW, when the NMI returns ICR2 has been overwritten and the interrupted IPI goes into lala land.
Thanks,
tglx
RFC PATCH v5 8/16 x86/watchdog/hardlockup/hpet: Introduce a target_cpumask
In reply to Ricardo NeriThe HPET hardlockup detector uses struct hpet_hld_data::monitored_cpumask to keep track of the CPUs that must be monitored for hard lockups. When the HPET timer expires, the CPU handling the interrupt issues an IPI to a subset of those monitored CPUs. The IPI will be sent only to the monitored CPUs that reside in the same package as the CPU handling the HPET interrupt. Designate this subset of monitored CPUs as target CPUs and track them using hpet_hld_data::target_cpumask.
In order to check all the monitored CPUs at the HPET timer expiration, rotate target_cpumask to now target the CPUs in the next package. Also the CPU handling the next HPET interrupt accordingly. Do all this from irq_work started from the NMI handler.
Changes in the monitored CPUs mask may change the handling CPU or the set of CPUs targeted for the next HPET interrupt. Thus, update both when the hardlockup detector is enabled or disabled on any CPU.
---
Changes since v4:
* Introduced this patch.
* Moved configuration of IPI of target CPUs and affinity of the HPET interrupt to an irq_work. (Thomas Gleixner)
Changes since v3:
* N/A
Changes since v2:
* N/A
Changes since v1:
* N/A
--- arch/x86/include/asm/hpet.h | 7 ++ arch/x86/kernel/watchdog_hld_hpet.c | 114 +++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-)
arch/x86/include/asm/hpet.h +7 -0
@@ -105,10 +105,15 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);* @irq: IRQ number assigned to the HPET channel * @handling_cpu: CPU handling the HPET interrupt * @msi_msg: MSI message to be written it the HPET registers +* @affinity_work: Used to update the affinity of the detector +* interrupts, both IPI and NMI. * @monitored_cpumask: CPUs monitored by the hardlockup detector * @ipi_cpumask: Auxiliary mask to handle IPIs. Both sending and * receiving CPUs write to it. Hence, we cannot * reuse @monitored_cpumask. +* @target_cpumask: Subset of @monitored_cpumask receiving a +* particular IPI upon HPET interrupt. It changes +* based on which CPU handles such interrupt. */ struct hpet_hld_data {bool has_periodic;
@@ -117,8 +122,10 @@ struct hpet_hld_data {int irq;u32 handling_cpu;struct msi_msg msi_msg; +struct irq_work affinity_work;cpumask_var_t monitored_cpumask;cpumask_var_t ipi_cpumask; +cpumask_var_t target_cpumask; };extern struct hpet_hld_data *hpet_hld_get_timer (void );
arch/x86/kernel/watchdog_hld_hpet.c +112 -2
168 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v5 9/16 watchdog/hardlockup/hpet: Group packages receiving IPIs when needed
In reply to Ricardo NeriIn order to keep the HPET interrupts of the hardlockup detector at a rate of one per second or less frequent, the HPET timer only targets one of the CPUs monitored by the detector. This is the handling CPU. The rest of the CPUs are monitored via an IPI issued by the handling CPUs. Furthermore, the monitored CPUs are partitioned into groups. Groups are targeted by the HPET timer in a round-robin manner. A group is composed of of all the CPUs in a physical package.
There may be situations in which it is not possible to keep the aforementioned HPET interrupt rate. This may happen if, for instance, watchdog_thresh is set to 1 second and there are more than one package in the system. In such case, the HPET timer should expire 1/nr_packages seconds.
It is possible to keep the HPET timer expiration at one second or less frequent if the packages receiving the IPI are grouped together. Hence, in the example above, all packages would be grouped together.
This approach has the drawback of having to issue IPIs across packages However, these cases should be rare: only when there are more packages than the value of watchdog_thresh in seconds.
Implement functionality to use the logic above: when the hardlockup detector is enabled in a CPU, check if grouping is necessary based in the value of watchdog_thresh. When updating target_cpumask, do it as many times as packages in the group.
---
Changes since v4:
* Introduced this patch.
Changes since v3:
* N/A
Changes since v2:
* N/A
Changes since v1:
*N/A
--- arch/x86/include/asm/hpet.h | 6 +++ arch/x86/kernel/watchdog_hld_hpet.c | 75 ++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 13 deletions(-)
arch/x86/include/asm/hpet.h +6 -0
@@ -104,6 +104,10 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);* @ticks_per_second: Frequency of the HPET timer * @irq: IRQ number assigned to the HPET channel * @handling_cpu: CPU handling the HPET interrupt +* @pkgs_per_group: Number of physical packages in a group of CPUs +* receiving an IPI +* @nr_groups: Number of groups into which @monitored_cpumask +* is partitioned * @msi_msg: MSI message to be written it the HPET registers * @affinity_work: Used to update the affinity of the detector * interrupts, both IPI and NMI.
@@ -121,6 +125,8 @@ struct hpet_hld_data {u64 ticks_per_second;int irq;u32 handling_cpu; +u32 pkgs_per_group; +u32 nr_groups;struct msi_msg msi_msg;struct irq_work affinity_work;cpumask_var_t monitored_cpumask;
arch/x86/kernel/watchdog_hld_hpet.c +62 -13
100 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v5 10/16 watchdog/hardlockup/hpet: Adjust timer expiration on the number of monitored groups
In reply to Ricardo NeriThe CPUs monitored by the HPET hardlockup detector are partitioned into groups based on their location in a given package. The HPET timer only targets a single CPU per package (or group of packages) at expiration. The rest of the CPUs in the group are monitored via an IPI issued by the handling CPU.
Each monitored CPU must be checked for hardlockups every watch_thresh seconds. This also means that each group of CPUs must be monitored in the same interval. Therefore, the HPET timer expiration is determined by the watch_thresh divided by the number of groups to monitor.
Add a new member, hpet_hld_data::ticks_per_group represents the number of times the HPET timer must tick before interrupting the handling CPU. Derive this value from the frequency of the HPET timer and the number of groups of CPUs.
Furthermore, update the timer expiration whenever there is a change in the number of monitored CPUs. Namely, when enabling or disabling the hardlockup detector in a given CPU.
---
Changes since v4:
* Reworked computation for the number of package groups instead of the number of CPUs.
* Renamed local variable temp as ticks in update_ticks_per_group(). (Andi)
Changes since v3:
* None
Changes since v2:
* Since the round-robin mechanism set the affinity of the HPET timer interrupt is in use again, it also becomes necessary to adjust the timer expiration.
Changes since v1:
* Dropped this patch as there was no need to readjust the timer expiration when the HPET timer only targets a single CPU.
--- arch/x86/include/asm/hpet.h | 3 ++ arch/x86/kernel/watchdog_hld_hpet.c | 47 +++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-)
arch/x86/include/asm/hpet.h +3 -0
@@ -102,6 +102,8 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);* @has_periodic: The HPET channel supports periodic mode * @channel: HPET channel assigned to the detector * @ticks_per_second: Frequency of the HPET timer +* @ticks_per_group: HPET ticks per group that must elapse before +* the timer expires * @irq: IRQ number assigned to the HPET channel * @handling_cpu: CPU handling the HPET interrupt * @pkgs_per_group: Number of physical packages in a group of CPUs
@@ -123,6 +125,7 @@ struct hpet_hld_data {bool has_periodic;u32 channel;u64 ticks_per_second; +u64 ticks_per_group;int irq;u32 handling_cpu;u32 pkgs_per_group;
arch/x86/kernel/watchdog_hld_hpet.c +45 -2
91 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v5 11/16 x86/watchdog/hardlockup/hpet: Determine if HPET timer caused NMI
In reply to Ricardo NeriThe only direct method to determine whether an HPET timer caused an interrupt is to read the Interrupt Status register. Unfortunately, reading HPET registers is slow and, therefore, it is not recommended to read them while in NMI context. Furthermore, status is not available if the interrupt is generated via the Front Side Bus.
An indirect manner to infer if a given non-maskable interrupt was caused by the HPET timer is to use the time-stamp counter. Compute the value that the time-stamp counter should have at the next interrupt of the HPET timer. Since the hardlockup detector operates in seconds, high precision is not needed. This implementation considers that the HPET caused the NMI if the time-stamp counter reads the expected value -/+ 1.5%. This value is selected as it is equivalent to 1/64 and the division can be performed using a bit shift operation. Experimentally, the error in the estimation is consistently less than 1%.
The computation of the expected value of the time-stamp counter must be performed in relation to watchdog_thresh divided by the number of groups of packages with monitored CPUs. This quantity is stored in tsc_ticks_per_group and must be updated whenever the number of monitored CPUs changes. Namely, when enabling or disabling the hardlockup detector on a given CPU.
---
Changes since v4:
* Compute the TSC expected value at the next HPET interrupt based on the number of monitored packages and not the number of monitored CPUs.
Changes since v3:
* None
Changes since v2:
* Reworked condition to check if the expected TSC value is within the error margin to avoid an unnecessary conditional. (Peter Zijlstra)
* Removed TSC error margin from struct hld_data; use a global variable instead. (Peter Zijlstra)
Changes since v1:
* Introduced this patch.
--- arch/x86/include/asm/hpet.h | 6 ++++++ arch/x86/kernel/watchdog_hld_hpet.c | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-)
arch/x86/include/asm/hpet.h +6 -0
@@ -104,6 +104,10 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);* @ticks_per_second: Frequency of the HPET timer * @ticks_per_group: HPET ticks per group that must elapse before * the timer expires +* @tsc_next: Estimated value of the TSC at the next +* HPET timer interrupt +* @tsc_ticks_per_group: TSC ticks that must elapse for each group of +* monitored CPUs. * @irq: IRQ number assigned to the HPET channel * @handling_cpu: CPU handling the HPET interrupt * @pkgs_per_group: Number of physical packages in a group of CPUs
@@ -126,6 +130,8 @@ struct hpet_hld_data {u32 channel;u64 ticks_per_second;u64 ticks_per_group; +u64 tsc_next; +u64 tsc_ticks_per_group;int irq;u32 handling_cpu;u32 pkgs_per_group;
arch/x86/kernel/watchdog_hld_hpet.c +26 -1
@@ -24,6 +24,7 @@static struct hpet_hld_data *hld_data;static bool hardlockup_use_hpet; +static u64 tsc_next_error;/** * kick_timer() - Reprogram timer to expire in the future
@@ -33,10 +34,21 @@ static bool hardlockup_use_hpet;* Reprogram the timer to expire within watchdog_thresh seconds in the future. * If the timer supports periodic mode, it is not kicked unless @force is * true. +* +* Also, compute the expected value of the time-stamp counter at the time of +* expiration as well as a deviation from the expected value. The maximum +* deviation is of ~1.5%. This deviation can be easily computed by shifting +* by 6 positions the delta between the current and expected time-stamp values. */ static void kick_timer (struct hpet_hld_data *hdata,bool force) { -u64 new_compare, count, period =0 ; +u64 tsc_curr, tsc_delta, new_compare, count, period =0 ; + + tsc_curr =rdtsc (); + + tsc_delta = (unsigned long )watchdog_thresh * hdata->tsc_ticks_per_group; + hdata->tsc_next = tsc_curr + tsc_delta; + tsc_next_error = tsc_delta >>6 ;/* kick the timer only when needed */ if (!force && hdata->has_periodic)
@@ -113,6 +125,15 @@ static void enable_timer(struct hpet_hld_data *hdata)*/ static bool is_hpet_wdt_interrupt (struct hpet_hld_data *hdata) { +if (smp_processor_id () == hdata->handling_cpu) { +u64 tsc_curr; + + tsc_curr =rdtsc (); + +return (tsc_curr - hdata->tsc_next) + tsc_next_error < +2 * tsc_next_error; + } +return false; }
@@ -438,6 +459,10 @@ static void update_ticks_per_group(struct hpet_hld_data *hdata)do_div (ticks, hdata->nr_groups); hdata->ticks_per_group = ticks; + + ticks = (unsigned long )tsc_khz *1000L ; +do_div (ticks, hdata->nr_groups); + hdata->tsc_ticks_per_group = ticks; }/**
Signature · 1 line
2.17.1
RFC PATCH v5 12/16 watchdog/hardlockup: Use parse_option_str() to handle "nmi_watchdog"
In reply to Ricardo NeriPrepare hardlockup_panic_setup() to handle a comma-separated list of options. Thus, it can continue parsing its own command-line options while ignoring paremeters that are relevant only to specific implementations of the hardlockup detector. Such implementations may use an early_param to parse their own options.
---
Changes since v4:
* None
Changes since v3:
* None
Changes since v2:
* Introduced this patch.
Changes since v1:
* None
--- kernel/watchdog.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/watchdog.c +4 -4
@@ -73,13 +73,13 @@ void __init hardlockup_detector_disable(void)static int __init hardlockup_panic_setup (char *str) { -if (!strncmp (str,"panic" ,5 )) +if (parse_option_str (str,"panic" )) hardlockup_panic =1 ; -else if (!strncmp (str,"nopanic" ,7 )) +else if (parse_option_str (str,"nopanic" )) hardlockup_panic =0 ; -else if (!strncmp (str,"0" ,1 )) +else if (parse_option_str (str,"0" )) nmi_watchdog_user_enabled =0 ; -else if (!strncmp (str,"1" ,1 )) +else if (parse_option_str (str,"1" )) nmi_watchdog_user_enabled =1 ;return 1 ; }
Signature · 1 line
2.17.1
RFC PATCH v5 13/16 watchdog/hardlockup/hpet: Only enable the HPET watchdog via a boot parameter
In reply to Ricardo NeriKeep the HPET-based hardlockup detector disabled unless explicitly enabled via a command-line argument. If such parameter is not given, the initialization of the hpet-based hardlockup detector fails and the NMI watchdog will fall back to use the perf-based implementation.
Implement the command-line parsing using an early_param, as __setup("nmi_watchdog=") only parses generic options.
--
Changes since v4:
* None
Changes since v3:
* None
Changes since v2:
* Do not imply that using nmi_watchdog=hpet means the detector is enabled. Instead, print a warning in such case.
Changes since v1:
* Added documentation to the function handing the nmi_watchdog kernel command-line argument.
--- .../admin-guide/kernel-parameters.txt | 8 ++++++- arch/x86/kernel/watchdog_hld_hpet.c | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +7 -1
@@ -3136,7 +3136,7 @@ Format: [state][,regs][,debounce][,die] nmi_watchdog= [KNL,BUGS=X86] Debugging features for SMP kernels - Format: [panic,][nopanic,][num] + Format: [panic,][nopanic,][num,][hpet] Valid num: 0 or 1 0 - turn hardlockup detector in nmi_watchdog off 1 - turn hardlockup detector in nmi_watchdog on
@@ -3147,6 +3147,12 @@ please see 'nowatchdog'. This is useful when you use a panic=... timeout and need the box quickly up again. + When hpet is specified, the NMI watchdog will be driven + by an HPET timer, if available in the system. Otherwise, + it falls back to the default implementation (perf or + architecture-specific). Specifying hpet has no effect + if the NMI watchdog is not enabled (either at build time + or via the command line). These settings can be accessed at runtime via the nmi_watchdog and hardlockup_panic sysctls.
arch/x86/kernel/watchdog_hld_hpet.c +22 -0
@@ -548,6 +548,28 @@ void hardlockup_detector_hpet_stop(void)disable_timer (hld_data); } +/** +* hardlockup_detector_hpet_setup() - Parse command-line parameters +* @str: A string containing the kernel command line +* +* Parse the nmi_watchdog parameter from the kernel command line. If +* selected by the user, use this implementation to detect hardlockups. +*/ +static int __init hardlockup_detector_hpet_setup (char *str) +{ +if (!str) +return -EINVAL ; + +if (parse_option_str (str,"hpet" )) + hardlockup_use_hpet = true; + +if (!nmi_watchdog_user_enabled && hardlockup_use_hpet) +pr_err ("Selecting HPET NMI watchdog has no effect with NMI watchdog disabled\n" ); + +return 0 ; +} +early_param ("nmi_watchdog" , hardlockup_detector_hpet_setup); +/** * hardlockup_detector_hpet_init() - Initialize the hardlockup detector *
Signature · 1 line
2.17.1
RFC PATCH v5 14/16 x86/watchdog: Add a shim hardlockup detector
In reply to Ricardo NeriThe generic hardlockup detector is based on perf. It also provides a set of weak stubs that CPU architectures can override. Add a shim hardlockup detector for x86 that selects between perf and hpet implementations.
Specifically, this shim implementation is needed for the HPET-based hardlockup detector; it can also be used for future implementations.
---
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 | 4 ++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/watchdog_hld.c | 80 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 arch/x86/kernel/watchdog_hld.c
arch/x86/Kconfig.debug +4 -0
@@ -117,9 +117,13 @@ config IOMMU_LEAK config HAVE_MMIOTRACE_SUPPORT def_bool y +config X86_HARDLOCKUP_DETECTOR + bool + config X86_HARDLOCKUP_DETECTOR_HPET bool "HPET Timer for Hard Lockup Detection" select HARDLOCKUP_DETECTOR_CORE + select X86_HARDLOCKUP_DETECTOR depends on HARDLOCKUP_DETECTOR && HPET_TIMER && HPET && (X86_64 || X86_32) help The hardlockup detector is driven by one counter of the Performance
arch/x86/kernel/Makefile +1 -0
@@ -115,6 +115,7 @@ obj-$(CONFIG_VM86) += vm86_32.o obj-$(CONFIG_EARLY_PRINTK) += early_printk.o obj-$(CONFIG_HPET_TIMER) += hpet.o +obj-$(CONFIG_X86_HARDLOCKUP_DETECTOR) += watchdog_hld.o obj-$(CONFIG_X86_HARDLOCKUP_DETECTOR_HPET) += watchdog_hld_hpet.o obj-$(CONFIG_AMD_NB) += amd_nb.o
arch/x86/kernel/watchdog_hld.c new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v5 15/16 watchdog: Expose lockup_detector_reconfigure()
In reply to Ricardo NeriWhen there are more than one implementation of the NMI watchdog, there may be situations in which switching from one to another is needed. For if the time-stamp counter becomes unstable, the HPET-based NMI watchdog an no longer be used.
Switching to another hardlockup detector can be done cleanly by updating the arch-specific stub and then reconfiguring the whole lockup detector. Expose lockup_detector_reconfigure() to achieve this goal.
---
Changes since v4:
* Switching to the perf-based lockup detector under the hood is hacky. Instead, reconfigure the whole lockup detector.
Changes since v3:
* None
Changes since v2:
* Introduced this patch.
Changes since v1:
* N/A
--- include/linux/nmi.h | 2 ++ kernel/watchdog.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/nmi.h +2 -0
@@ -16,6 +16,7 @@ void lockup_detector_init(void);void lockup_detector_soft_poweroff (void );void lockup_detector_cleanup (void );bool is_hardlockup (void ); +void lockup_detector_reconfigure (void );extern int watchdog_user_enabled;extern int nmi_watchdog_user_enabled;
@@ -37,6 +38,7 @@ extern int sysctl_hardlockup_all_cpu_backtrace;static inline void lockup_detector_init (void ) { }static inline void lockup_detector_soft_poweroff (void ) { }static inline void lockup_detector_cleanup (void ) { } +static inline void lockup_detector_reconfigure (void ) { }#endif /* !CONFIG_LOCKUP_DETECTOR */ #ifdef CONFIG_SOFTLOCKUP_DETECTOR
kernel/watchdog.c +2 -2
@@ -531,7 +531,7 @@ int lockup_detector_offline_cpu(unsigned int cpu)return 0 ; } -static void lockup_detector_reconfigure (void ) +void lockup_detector_reconfigure (void ) {cpus_read_lock ();watchdog_nmi_stop ();
@@ -577,7 +577,7 @@ static __init void lockup_detector_setup(void) }#else /* CONFIG_SOFTLOCKUP_DETECTOR */ -static void lockup_detector_reconfigure (void ) +void lockup_detector_reconfigure (void ) {cpus_read_lock ();watchdog_nmi_stop ();
Signature · 1 line
2.17.1
RFC PATCH v5 16/16 x86/tsc: Switch to perf-based hardlockup detector if TSC become unstable
In reply to Ricardo NeriThe HPET-based hardlockup detector relies on the TSC to determine if an observed NMI interrupt was originated by HPET timer. Hence, this detector can no longer be used with an unstable TSC.
In such case, permanently stop the HPET-based hardlockup detector and start the perf-based detector.
Add stub versions of hardlockup_detector_switch_to_perf() to be used when the HPET hardlockup detector and/or when CONFIG_HPET_TIMER are not selected.
---
Changes since v4:
* Added a stub version of hardlockup_detector_switch_to_perf() for !CONFIG_HPET_TIMER. (lkp)
* Reconfigure the whole lockup detector instead of unconditionally starting the perf-based hardlockup detector.
Changes since v3:
* None
Changes since v2:
* Introduced this patch.
Changes since v1:
* N/A
--- arch/x86/include/asm/hpet.h | 3 +++ arch/x86/kernel/tsc.c | 2 ++ arch/x86/kernel/watchdog_hld.c | 6 ++++++ 3 files changed, 11 insertions(+)
arch/x86/include/asm/hpet.h +3 -0
@@ -148,18 +148,21 @@ extern int hardlockup_detector_hpet_init(void);extern void hardlockup_detector_hpet_stop (void );extern void hardlockup_detector_hpet_enable (unsigned int cpu);extern void hardlockup_detector_hpet_disable (unsigned int cpu); +extern void hardlockup_detector_switch_to_perf (void );#else static inline int hardlockup_detector_hpet_init (void ) {return -ENODEV ; }static inline void hardlockup_detector_hpet_stop (void ) {}static inline void hardlockup_detector_hpet_enable (unsigned int cpu) {}static inline void hardlockup_detector_hpet_disable (unsigned int cpu) {} +static inline void hardlockup_detector_switch_to_perf (void ) {}#endif /* CONFIG_X86_HARDLOCKUP_DETECTOR_HPET */ #else /* CONFIG_HPET_TIMER */ static inline int hpet_enable (void ) {return 0 ; }static inline int is_hpet_enabled (void ) {return 0 ; } +static inline void hardlockup_detector_switch_to_perf (void ) {}#define hpet_readl (a) 0#define default_setup_hpet_msi NULL
arch/x86/kernel/tsc.c +2 -0
@@ -1174,6 +1174,8 @@ void mark_tsc_unstable(char *reason)clocksource_mark_unstable (&clocksource_tsc_early);clocksource_mark_unstable (&clocksource_tsc); + +hardlockup_detector_switch_to_perf (); }EXPORT_SYMBOL_GPL (mark_tsc_unstable);
arch/x86/kernel/watchdog_hld.c +6 -0
@@ -78,3 +78,9 @@ void watchdog_nmi_stop(void)if (detector_type ==X86_HARDLOCKUP_DETECTOR_HPET )hardlockup_detector_hpet_stop (); } + +void hardlockup_detector_switch_to_perf (void ) +{ + detector_type =X86_HARDLOCKUP_DETECTOR_PERF ; +lockup_detector_reconfigure (); +}
Signature · 1 line
2.17.1