Skip to contentSkip to conversation

NMI watchdog fixes / enhancements

PATCH In review Jan Kiszka 2 participants 11 messages Dec 23, 2008 – Dec 29, 2008

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

v2 cover NMI watchdog fixes / enhancements

Changes since last round:
- Fix build breakage of trunk against 2.6.27
- Make rthal_nmi_release more robust (additional patch)
- Fix issue of last patch on 32 bit (untested last-minute changes...)

All patches are also available at
git://git.kiszka.org/xenomai.git nmi-wd-queue

Jan

author
#1 lore raw

PATCH v2 1/5 x86_32: Fix NMI watchdog build for 2.6.27

Fix build breakage against 2.6.27-x86-32 due to changes of die_nmi. At this chance, switch the internal function call wrapping to latest unified mainline instead of using the legacy prototype.

---

 ksrc/arch/x86/hal_32.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)
ksrc/arch/x86/hal_32.c +7 -3
index e8e1258..79b7c83 100644
@@ -107,7 +107,7 @@ extern void show_registers(struct pt_regs *regs);
 
 extern spinlock_t nmi_print_lock;
 
-void die_nmi(struct pt_regs *regs, const char *msg)
+void die_nmi(const char *msg, struct pt_regs *regs, int do_panic)
 {
 	spin_lock(&nmi_print_lock);
 	/*
@@ -124,7 +124,11 @@ void die_nmi(struct pt_regs *regs, const char *msg)
 	do_exit(SIGSEGV);
 }
 
-#endif /* Linux < 2.6 */
+#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
+#define die_nmi(msg, regs, do_panic)	die_nmi(regs, msg)
+#else /* Linux >= 2.6.27 */
+#include <asm/nmi.h>
+#endif /* Linux >= 2.6.27 */
 
 void rthal_latency_above_max(struct pt_regs *regs)
 {
@@ -136,7 +140,7 @@ void rthal_latency_above_max(struct pt_regs *regs)
 			 sizeof(buf),
 			 "NMI watchdog detected timer latency above %u us\n",
 			 rthal_maxlat_us);
-		die_nmi(regs, buf);
+		die_nmi(buf, regs, 1);
 	}
 }
 
author
#2 lore raw

PATCH v2 2/5 Update NMI watchdog for latest Intel CPUs

In reply to Jan Kiszka

Add performance-counter NMI watchdog support for recent Intel CPUs. This should also fix potential overrun (corner) cases for P6-type CPUs as the current code incorrectly assumes that more than 31 bits are available as watchdog delay counter.

Refactor some dispatching paths at this chance.

---

 include/asm-x86/bits/timer.h |    2 +-
 ksrc/arch/x86/nmi_32.c       |  128 ++++++++++++++++++++++++++----------------
 2 files changed, 81 insertions(+), 49 deletions(-)
include/asm-x86/bits/timer.h +1 -1
index b742763..d957c54 100644
@@ -37,7 +37,7 @@ static inline void xnarch_program_timer_shot(unsigned long delay)
 #ifdef CONFIG_XENO_HW_NMI_DEBUG_LATENCY
 	{
 		extern unsigned long rthal_maxlat_tsc;
-		if (delay <= (ULONG_MAX - rthal_maxlat_tsc))
+		if (delay <= (LONG_MAX - rthal_maxlat_tsc))
 			rthal_nmi_arm(delay + rthal_maxlat_tsc);
 	}
 #endif /* CONFIG_XENO_HW_NMI_DEBUG_LATENCY */
ksrc/arch/x86/nmi_32.c +80 -48

233 lines not on this page · raw message

author
#3 lore raw

PATCH v2 3/5 x86: Clear perfctr_msr on rthal_nmi_release

In reply to Jan Kiszka

Better safe than sorry: Clear all per-CPU perfctr_msr values so that no one can rearm the NMI watchdog after we released it.

---

 ksrc/arch/x86/nmi_32.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
ksrc/arch/x86/nmi_32.c +3 -0
index 78ba905..69a392a 100644
@@ -264,6 +264,7 @@ int rthal_nmi_request(void (*emergency) (struct pt_regs *))
 void rthal_nmi_release(void)
 {
 	rthal_nmi_wd_t *wd = &rthal_nmi_wds[rthal_processor_id()];
+	int i;
 
 	if (!rthal_linux_nmi_tick)
 		return;
@@ -276,6 +277,8 @@ void rthal_nmi_release(void)
 		wrmsr(wd->perfctr_msr, (u32)(0 - RTHAL_CPU_FREQ), 0);
 	else
 		wrmsrl(wd->perfctr_msr, 0 - RTHAL_CPU_FREQ);
+	for (i = 0; i < NR_CPUS; i++)
+		rthal_nmi_wds[i].perfctr_msr = 0;
 	touch_nmi_watchdog();
 	wmb();
 	nmi_watchdog_tick = rthal_linux_nmi_tick;
author
#4 lore raw

PATCH v2 4/5 NMI watchdog support for x86-64

In reply to Jan Kiszka

No need to lock the NMI away from x86-64 boxes, it just takes a bit refactoring.

NOTE: Whoever applies this to SVN, make sure to MOVE nmi_32.c to nmi.c!

---

 include/asm-x86/hal.h             |    2 +
 include/asm-x86/hal_32.h          |    2 -
 ksrc/arch/x86/Kconfig             |    4 ---
 ksrc/arch/x86/Makefile            |    4 +-
 ksrc/arch/x86/hal-common.c        |   48 ++++++++++++++++++++++++++++++++++++
 ksrc/arch/x86/hal_32.c            |   49 -------------------------------------
 ksrc/arch/x86/{nmi_32.c => nmi.c} |    0 
 7 files changed, 52 insertions(+), 57 deletions(-)
 rename ksrc/arch/x86/{nmi_32.c => nmi.c} (100%)
include/asm-x86/hal.h +2 -0
index 4df6bf6..158e3e6 100644
@@ -69,6 +69,8 @@ typedef int (*compat_emutick_t)(unsigned long evt,
 
 extern enum rthal_ktimer_mode rthal_ktimer_saved_mode;
 
+void rthal_latency_above_max(struct pt_regs *regs);
+
 #ifdef __i386__
 #include "hal_32.h"
 #else
include/asm-x86/hal_32.h +0 -2
index ddcec08..9a707d0 100644
@@ -234,6 +234,4 @@ static inline void rthal_setup_oneshot_apic(int vector)
 
 long rthal_strncpy_from_user(char *dst, const char __user * src, long count);
 
-void rthal_latency_above_max(struct pt_regs *regs);
-
 #endif /* !_XENO_ASM_X86_HAL_32_H */
ksrc/arch/x86/Kconfig +0 -4
index 865ade7..ad8a5de 100644
@@ -27,8 +27,6 @@ config XENO_HW_FPU
 	Float-Point Unit on the x86 platform at the following URL:
 	http://www.intel.com/design/intarch/techinfo/Pentium/fpu.htm
 
-if !X86_64
-
 menu "NMI watchdog"
 
 config XENO_HW_NMI_DEBUG_LATENCY
@@ -59,8 +57,6 @@ config XENO_HW_NMI_DEBUG_LATENCY_MAX
 
 endmenu
 
-endif
-
 menu "SMI workaround"
 
 config XENO_HW_SMI_DETECT_DISABLE
ksrc/arch/x86/Makefile +2 -2
index 71cc8ec..3296206 100644
@@ -12,7 +12,7 @@ obj-$(CONFIG_XENOMAI) += xeno_hal.o
 
 xeno_hal-y := hal_$(X86_MODE).o hal-common.o usercopy_$(X86_MODE).o
 
-xeno_hal-$(CONFIG_XENO_HW_NMI_DEBUG_LATENCY) += nmi_$(X86_MODE).o
+xeno_hal-$(CONFIG_XENO_HW_NMI_DEBUG_LATENCY) += nmi.o
 
 xeno_hal-$(CONFIG_XENO_HW_SMI_DETECT) += smi.o
 
@@ -28,7 +28,7 @@ O_TARGET := built-in.o
 
 obj-y := hal_32.o hal-common.o
 
-obj-$(CONFIG_XENO_HW_NMI_DEBUG_LATENCY) += nmi_32.o
+obj-$(CONFIG_XENO_HW_NMI_DEBUG_LATENCY) += nmi.o
 
 obj-$(CONFIG_XENO_HW_SMI_DETECT) += smi.o
 
ksrc/arch/x86/hal-common.c +48 -0

54 lines not on this page · raw message

ksrc/arch/x86/hal_32.c +0 -49

55 lines not on this page · raw message

ksrc/arch/x86/nmi.c renamed from ksrc/arch/x86/nmi_32.c +0 -0
similarity index 100%
rename from ksrc/arch/x86/nmi_32.c
rename to ksrc/arch/x86/nmi.c
author
#5 lore raw

PATCH v2 5/5 Rework x86 NMI watchdog pass-through

In reply to Jan Kiszka

Currently, Xenomai's NMI watchdog handler assumes to be called only on watchdog events. Other reasons are considered spurious, and a TSC-based method is used to detect such conditions. This has several issues
- the return code of the Linux handler is ignored
- KGDB's NMI events (CPU roundups) are not passed through
- early_shot mechanism suffers from a signedness bug and misses too early shots
- printk from NMI can cause lock-ups, but we also support non-fatal reports (ipipe tracer active)

This patch therefore switches to the watchdog detection pattern that Linux uses: Check for the highest perfctr bit being zero for true timeouts. In case the watchdog did not time out, the Linux handler is invoked and its return code is properly forwarded. Finally, the early_shot reporting is dropped as it becomes pointless when KGDB is in use (and I suspect that patch 1 of this series fixes most of the original reasons).

---

 ksrc/arch/x86/nmi.c |   95 ++++++++++++++++++++++----------------------------
 1 files changed, 42 insertions(+), 53 deletions(-)
ksrc/arch/x86/nmi.c +42 -53
index 69a392a..a9d9380 100644
@@ -65,13 +65,11 @@
 typedef union {
 	struct {
 		/* Xenomai watchdog data. */
-		unsigned int flags;
-		unsigned long perfctr_msr;
 		unsigned long long next_linux_check;
+		unsigned long perfctr_msr;
+		u64 perfctr_checkmask;
 		unsigned int p4_cccr_val;
-
-		unsigned early_shots;
-		unsigned long long tick_date;
+		unsigned int flags;
 	};
 	char __pad[SMP_CACHE_BYTES];
 } rthal_nmi_wd_t ____cacheline_aligned;
@@ -82,6 +80,15 @@ static void (*rthal_nmi_emergency) (struct pt_regs *);
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 #define MSR_ARCH_PERFMON_PERFCTR0	0xc1
 #define MSR_ARCH_PERFMON_PERFCTR1	0xc2
+union cpuid10_eax {
+	struct {
+		unsigned int version_id:8;
+		unsigned int num_counters:8;
+		unsigned int bit_width:8;
+		unsigned int mask_length:8;
+	} split;
+	unsigned int full;
+};
 static void (*rthal_linux_nmi_tick) (struct pt_regs *);
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
@@ -105,28 +112,28 @@ static int (*rthal_linux_nmi_tick) (struct pt_regs *, unsigned);
 #endif /* Linux >= 2.6.19 */
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
-#define CALL_LINUX_NMI		rthal_linux_nmi_tick(regs)
-#define NMI_RETURN		return
+#define CALL_LINUX_NMI		({ rthal_linux_nmi_tick(regs); 1; })
+#define NMI_RETURN(code)	return
 static void rthal_nmi_watchdog_tick(struct pt_regs *regs)
 #else /* Linux >= 2.6.19 */
 #define CALL_LINUX_NMI		rthal_linux_nmi_tick(regs, reason)
-#define NMI_RETURN		return 1
+#define NMI_RETURN(code)	return (code)
 static int rthal_nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
 #endif /* Linux >= 2.6.19 */
 {
 	int cpu = rthal_processor_id();
 	rthal_nmi_wd_t *wd = &rthal_nmi_wds[cpu];
 	unsigned long long now;
+	u64 perfctr;
 
-	if (wd->flags & NMI_WD_ARMED) {
-		if (rthal_rdtsc() - wd->tick_date < rthal_maxlat_tsc) {
-			++wd->early_shots;
-			wd->next_linux_check = wd->tick_date + rthal_maxlat_tsc;
-		} else {
-			printk("NMI early shots: %d\n", wd->early_shots);
-			rthal_nmi_emergency(regs);
-		}
-	}
+	rdmsrl(wd->perfctr_msr, perfctr);
+
+	if (perfctr & wd->perfctr_checkmask)
+		/* No watchdog tick, let Linux handle it. */
+		NMI_RETURN(CALL_LINUX_NMI);
+
+	if (wd->flags & NMI_WD_ARMED)
+		rthal_nmi_emergency(regs);
 
 	now = rthal_rdtsc();
 
@@ -162,36 +169,14 @@ static int rthal_nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
 	else
 		wrmsrl(wd->perfctr_msr, now - wd->next_linux_check);
 
-	NMI_RETURN;
+	NMI_RETURN(1);
 }
 
-#ifdef CONFIG_PROC_FS
-static int earlyshots_read_proc(char *page,
-				char **start,
-				off_t off, int count, int *eof, void *data)
-{
-	int i, len = 0;
-
-	for_each_online_cpu(i)
-		len += sprintf(page + len, "CPU#%d: %u\n",
-			       i, rthal_nmi_wds[i].early_shots);
-	len -= off;
-	if (len <= off + count)
-		*eof = 1;
-	*start = page + off;
-	if (len > count)
-		len = count;
-	if (len < 0)
-		len = 0;
-
-	return len;
-}
-#endif /* CONFIG_PROC_FS */
-
 int rthal_nmi_request(void (*emergency) (struct pt_regs *))
 {
 	unsigned long long next_linux_check;
 	unsigned long perfctr_msr;
+	u64 perfctr_checkmask;
 	unsigned int wd_flags = 0;
 	unsigned int p4_cccr_val = 0;
 	int i;
@@ -205,23 +190,30 @@ int rthal_nmi_request(void (*emergency) (struct pt_regs *))
 	switch (boot_cpu_data.x86_vendor) {
         case X86_VENDOR_AMD:
 		perfctr_msr = MSR_K7_PERFCTR0;
+		perfctr_checkmask = 1ULL << 47;
 		break;
         case X86_VENDOR_INTEL:
 		if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
+			union cpuid10_eax eax;
+
 			if (boot_cpu_data.x86 == 6 &&
 			    boot_cpu_data.x86_model == 14)
 				perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
 			else
 				perfctr_msr = MSR_ARCH_PERFMON_PERFCTR1;
+			cpuid(10, &eax.full, &i, &i, &i);
+			perfctr_checkmask = 1ULL << (eax.split.bit_width - 1);
 			wd_flags = NMI_WD_P6_OR_LATER | NMI_WD_31BITS;
 		} else
 			switch (boot_cpu_data.x86) {
 	                case 6:
 				perfctr_msr = MSR_P6_PERFCTR0;
+				perfctr_checkmask = 1ULL << 39;
 				wd_flags = NMI_WD_P6_OR_LATER | NMI_WD_31BITS;
 				break;
 	                case 15:
 				perfctr_msr = MSR_P4_IQ_COUNTER0;
+				perfctr_checkmask = 1ULL << 39;
 				p4_cccr_val = P4_NMI_IQ_CCCR0;
 #ifdef CONFIG_SMP
 				if (smp_num_siblings == 2)
@@ -244,6 +236,7 @@ int rthal_nmi_request(void (*emergency) (struct pt_regs *))
 
 		wd->flags = wd_flags;
 		wd->perfctr_msr = perfctr_msr;
+		wd->perfctr_checkmask = perfctr_checkmask;
 		wd->p4_cccr_val = p4_cccr_val;
 		wd->next_linux_check = next_linux_check;
 	}
@@ -252,12 +245,6 @@ int rthal_nmi_request(void (*emergency) (struct pt_regs *))
 	wmb();
 	nmi_watchdog_tick = &rthal_nmi_watchdog_tick;
 
-#ifdef CONFIG_PROC_FS
-	rthal_add_proc_leaf("nmi_early_shots",
-			    &earlyshots_read_proc,
-			    NULL, NULL, rthal_proc_root);
-#endif /* CONFIG_PROC_FS */
-
 	return 0;
 }
 
@@ -269,10 +256,6 @@ void rthal_nmi_release(void)
 	if (!rthal_linux_nmi_tick)
 		return;
 
-#ifdef CONFIG_PROC_FS
-	remove_proc_entry("nmi_early_shots", rthal_proc_root);
-#endif /* CONFIG_PROC_FS */
-
 	if (wd->flags & NMI_WD_31BITS)
 		wrmsr(wd->perfctr_msr, (u32)(0 - RTHAL_CPU_FREQ), 0);
 	else
@@ -299,6 +282,10 @@ void rthal_nmi_arm(unsigned long delay)
 		/* Protect from an interrupt handler calling rthal_nmi_arm. */
 		rthal_local_irq_save(flags);
 		wd->flags &= ~NMI_WD_ARMED;
+		/*
+		 * Our watchdog must be declared unarmed before we triger the
+		 * Linux watchdog NMI, entering rthal_nmi_watchdog_tick.
+		 */
 		wmb();
 		if (wd->flags & NMI_WD_31BITS)
 			wrmsr(wd->perfctr_msr, (u32)-1, 0);
@@ -308,12 +295,14 @@ void rthal_nmi_arm(unsigned long delay)
 		rthal_local_irq_restore(flags);
 	}
 
-	wd->tick_date = rthal_rdtsc() + (delay - rthal_maxlat_tsc);
-	wmb();
 	if (wd->flags & NMI_WD_31BITS)
 		wrmsr(wd->perfctr_msr, (u32)(0 - delay), 0);
 	else
 		wrmsrl(wd->perfctr_msr, 0 - delay);
+	/*
+	 * New perfctr must have been written before we can declare the
+	 * watchdog armed (avoid race with previously programmed value).
+	 */
 	wmb();
 	wd->flags |= NMI_WD_ARMED;
 }
#6 lore raw
In reply to Jan Kiszka
Quoted 7 lines · Jan Kiszka

Changes since last round:
- Fix build breakage of trunk against 2.6.27
- Make rthal_nmi_release more robust (additional patch)
- Fix issue of last patch on 32 bit (untested last-minute changes...)

All patches are also available at
git://git.kiszka.org/xenomai.git nmi-wd-queue

I remind you that trunk is currently broken for the posix skin on arm, a regression introduced by your FASTSYNCH patches. From my point of view, fixing this regression is more important than fixing an option that almost nobody uses with untested patches...

Signature · 1 line
					    Gilles.
author
#7 lore raw
Quoted 6 lines · Gilles Chanteperdrix
Quoted 5 lines · Jan Kiszka

Changes since last round:
- Fix build breakage of trunk against 2.6.27
- Make rthal_nmi_release more robust (additional patch)
- Fix issue of last patch on 32 bit (untested last-minute changes...)

Quoted 7 lines
Quoted 2 lines

All patches are also available at
git://git.kiszka.org/xenomai.git nmi-wd-queue

I remind you that trunk is currently broken for the posix skin on arm, a regression introduced by your FASTSYNCH patches. From my point of view, fixing this regression is more important than fixing an option that almost nobody uses with untested patches...

Many things are broken and awaiting a fix. I also have 6 posted bug fixes in my assorted patch queue for Xenomai ATM. But this doesn't mean everything else has to wait just because review or merging is delayed due to current overload.

And FYI, I've discussed the posix skin setup race with Wolfgang last week, suggesting a few things to try next on his target - what he will likely do as soon as time permits. I can't help at this point as A) the race is not yet obvious for me and B) I've no chance to reproduce it. But any further information on this is always welcome - if you happen to see the problem as well.

Regarding this thread, I would appreciate if we could just stay on topic. If you don't have the time for the patches, then it's absolutely OK, just leave them alone.

Jan

#8 lore raw
Quoted 3 lines · Jan Kiszka

Regarding this thread, I would appreciate if we could just stay on topic. If you don't have the time for the patches, then it's absolutely OK, just leave them alone.

I am reluctant to test patches which are explicitely labeled as "untested", and prefer to wait the tested ones. Trunk is already broken enough, that is my point.

Signature · 1 line
					    Gilles.
author
#9 lore raw
Quoted 9 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Regarding this thread, I would appreciate if we could just stay on topic. If you don't have the time for the patches, then it's absolutely OK, just leave them alone.

I am reluctant to test patches which are explicitely labeled as "untested", and prefer to wait the tested ones. Trunk is already broken enough, that is my point.

Oh, that was a misunderstanding due to my unfortunate changelog. Untested referred to a specific config set and a last-minute change of the previous version.

Jan

#10 lore raw
Quoted 5 lines · Jan Kiszka
Quoted 4 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Regarding this thread, I would appreciate if we could just stay on topic. If you don't have the time for the patches, then it's absolutely OK, just leave them alone.

Quoted 8 lines
Quoted 4 lines

I am reluctant to test patches which are explicitely labeled as "untested", and prefer to wait the tested ones. Trunk is already broken enough, that is my point.

Oh, that was a misunderstanding due to my unfortunate changelog. Untested referred to a specific config set and a last-minute change of the previous version.

Ok. Forget about solving the issue on ARM, I have to solve it now.

Signature · 1 line
                                                 Gilles.