Skip to contentSkip to conversation

x86/nmi: correctly check MSB of P6 performance counter MSR in watchdog

PATCH In review Igor Druzhinin 2 participants 4 messages Feb 26, 2019 – Feb 28, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86/nmi: correctly check MSB of P6 performance counter MSR in watchdog

The logic currently tries to work out if a recent overflow (that indicates that NMI comes from the watchdog) happened by checking MSB of performance counter MSR that is initially sign extended from a negative value that we program it to. A possibly incorrect assumption here is that MSB is always bit 32 while on modern hardware it's usually 47 and the actual bit-width is reported through CPUID. Checking bit 32 for overflows is usually fine since we never program it to anything exceeding 32-bits and NMI is handled shortly after overflow occurs.

A problematic scenario that we saw occurs on systems where SMIs taking significant time are possible. In that case, NMI handling is deferred to the point firmware exits SMI which might take enough time for the counter to go through bit 32 and set it to 1 again. So the logic described above will misread it and report an unknown NMI erroneously.

Fortunately, we can use the actual MSB, which is usually higher than the currently hardcoded 32, and treat this case correctly at least on modern hardware.

---
Changes in v4:
* add special handling for zero-field case which is identical to max-leaf-too-small case

---
 xen/arch/x86/nmi.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
xen/arch/x86/nmi.c +14 -2
index bfe9777..1af4ba8 100644
@@ -37,6 +37,7 @@ unsigned int nmi_watchdog = NMI_NONE;
 static unsigned int nmi_hz = HZ;
 static unsigned int nmi_perfctr_msr;	/* the MSR to reset in NMI handler */
 static unsigned int nmi_p4_cccr_val;
+static unsigned int nmi_p6_event_width;
 static DEFINE_PER_CPU(struct timer, nmi_timer);
 static DEFINE_PER_CPU(unsigned int, nmi_timer_ticks);
 
@@ -123,7 +124,9 @@ int nmi_active;
 #define P6_EVNTSEL_USR		(1 << 16)
 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED	 0x79
 #define CORE_EVENT_CPU_CLOCKS_NOT_HALTED 0x3c
-#define P6_EVENT_WIDTH          32
+/* Bit width of IA32_PMCx MSRs is reported using CPUID.0AH:EAX[23:16]. */
+#define P6_EVENT_WIDTH_MASK	(((1 << 8) - 1) << 16)
+#define P6_EVENT_WIDTH_MIN	32
 
 #define P4_ESCR_EVENT_SELECT(N)	((N)<<25)
 #define P4_CCCR_OVF_PMI0	(1<<26)
@@ -323,6 +326,15 @@ static void setup_p6_watchdog(unsigned counter)
     unsigned int evntsel;
 
     nmi_perfctr_msr = MSR_P6_PERFCTR(0);
+    if ( !nmi_p6_event_width )
+    	nmi_p6_event_width = (current_cpu_data.cpuid_level >= 0xa) ?
+                             MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK) :
+                             P6_EVENT_WIDTH_MIN;
+    if ( !nmi_p6_event_width )
+        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;
+    if ( nmi_p6_event_width < P6_EVENT_WIDTH_MIN ||
+         nmi_p6_event_width > BITS_PER_LONG )
+        return;
 
     clear_msr_range(MSR_P6_EVNTSEL(0), 2);
     clear_msr_range(MSR_P6_PERFCTR(0), 2);
@@ -529,7 +541,7 @@ bool nmi_watchdog_tick(const struct cpu_user_regs *regs)
         else if ( nmi_perfctr_msr == MSR_P6_PERFCTR(0) )
         {
             rdmsrl(MSR_P6_PERFCTR(0), msr_content);
-            if ( msr_content & (1ULL << P6_EVENT_WIDTH) )
+            if ( msr_content & (1ULL << (nmi_p6_event_width - 1)) )
                 watchdog_tick = false;
 
             /*
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v4 1/1
#1 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 18:44, <[email protected]> wrote:

The logic currently tries to work out if a recent overflow (that indicates that NMI comes from the watchdog) happened by checking MSB of performance counter MSR that is initially sign extended from a negative value that we program it to. A possibly incorrect assumption here is that MSB is always bit 32 while on modern hardware it's usually 47 and the actual bit-width is reported through CPUID. Checking bit 32 for overflows is usually fine since we never program it to anything exceeding 32-bits and NMI is handled shortly after overflow occurs.

A problematic scenario that we saw occurs on systems where SMIs taking significant time are possible. In that case, NMI handling is deferred to the point firmware exits SMI which might take enough time for the counter to go through bit 32 and set it to 1 again. So the logic described above will misread it and report an unknown NMI erroneously.

Quoted 5 lines

Fortunately, we can use the actual MSB, which is usually higher than the currently hardcoded 32, and treat this case correctly at least on modern hardware.

albeit ...

xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 334

Quoted 10 lines
xen/arch/x86/nmi.c +6 -0
@@ -323,6 +326,15 @@ static void setup_p6_watchdog(unsigned counter)
     unsigned int evntsel;
 
     nmi_perfctr_msr = MSR_P6_PERFCTR(0);
+    if ( !nmi_p6_event_width )
+    	nmi_p6_event_width = (current_cpu_data.cpuid_level >= 0xa) ?
+                             MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK) :
+                             P6_EVENT_WIDTH_MIN;
+    if ( !nmi_p6_event_width )
+        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;

... I think this would now better be

    if ( !nmi_p6_event_width && current_cpu_data.cpuid_level >= 0xa )
        nmi_p6_event_width = MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK);
    if ( !nmi_p6_event_width )
        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;

Re-writing of which also mad me notice a hard tab in there. I'd be fine making the adjustment while committing, as long as you agree.

Btw, considering the combination of subject tag and Cc list I take it that you don't intend this to go into 4.12? I ask because generally I'd consider this a backporting candidate.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 17 lines · Jan Beulich

albeit ...

xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 334

Quoted 10 lines
xen/arch/x86/nmi.c +6 -0
@@ -323,6 +326,15 @@ static void setup_p6_watchdog(unsigned counter)
     unsigned int evntsel;
 
     nmi_perfctr_msr = MSR_P6_PERFCTR(0);
+    if ( !nmi_p6_event_width )
+    	nmi_p6_event_width = (current_cpu_data.cpuid_level >= 0xa) ?
+                             MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK) :
+                             P6_EVENT_WIDTH_MIN;
+    if ( !nmi_p6_event_width )
+        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;

... I think this would now better be

Quoted 7 lines
    if ( !nmi_p6_event_width && current_cpu_data.cpuid_level >= 0xa )
        nmi_p6_event_width = MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK);
    if ( !nmi_p6_event_width )
        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;

Re-writing of which also mad me notice a hard tab in there. I'd be fine making the adjustment while committing, as long as you agree.

Thanks, I also didn't like how it looked eventually. I'll make the same adjustment to my copy of the patch as well then.

Quoted 3 lines

Btw, considering the combination of subject tag and Cc list I take it that you don't intend this to go into 4.12? I ask because generally I'd consider this a backporting candidate.

Yes, I didn't intend it to target 4.12 as I don't consider it a serious issue - we've only seen it on one type of Supermicro machines (unfortunately, our lab is now almost 50% of them) so far with poor implementation of ERST. But I wouldn't mind if it was selected as a candidate for 4.12 and potential backporting.

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 13:07, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich

albeit ...

xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 331

Quoted 7 lines
xen/arch/x86/nmi.c +3 -0
@@ -323,6 +326,15 @@ static void setup_p6_watchdog(unsigned counter)
     unsigned int evntsel;
 
     nmi_perfctr_msr = MSR_P6_PERFCTR(0);
+    if ( !nmi_p6_event_width )
+    	nmi_p6_event_width = (current_cpu_data.cpuid_level >= 0xa) ?
+                             MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK) 

:

Quoted 6 lines

xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 334

Quoted 3 lines
xen/arch/x86/nmi.c +3 -0
+                             P6_EVENT_WIDTH_MIN;
+    if ( !nmi_p6_event_width )
+        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;

... I think this would now better be

Quoted 7 lines
    if ( !nmi_p6_event_width && current_cpu_data.cpuid_level >= 0xa )
        nmi_p6_event_width = MASK_EXTR(cpuid_eax(0xa), P6_EVENT_WIDTH_MASK);
    if ( !nmi_p6_event_width )
        nmi_p6_event_width = P6_EVENT_WIDTH_MIN;

Re-writing of which also mad me notice a hard tab in there. I'd be fine making the adjustment while committing, as long as you agree.

Thanks, I also didn't like how it looked eventually. I'll make the same adjustment to my copy of the patch as well then.

Quoted 3 lines

Btw, considering the combination of subject tag and Cc list I take it that you don't intend this to go into 4.12? I ask because generally I'd consider this a backporting candidate.

Quoted 5 lines

Yes, I didn't intend it to target 4.12 as I don't consider it a serious issue - we've only seen it on one type of Supermicro machines (unfortunately, our lab is now almost 50% of them) so far with poor implementation of ERST. But I wouldn't mind if it was selected as a candidate for 4.12 and potential backporting.

Jürgen, what do you think?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel