x86/nmi: correctly check MSB of P6 performance counter MSR in watchdog
PATCH v2 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 v2:
* taken care of the case where leaf 0xa is missing or width is 0
* apply a cap in case width is lower than we're expecting to program
--- xen/arch/x86/nmi.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
xen/arch/x86/nmi.c +10 -2
@@ -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_DEFAULT 32#define P4_ESCR_EVENT_SELECT (N ) ((N)<<25)#define P4_CCCR_OVF_PMI0 (1<<26)
@@ -292,6 +295,7 @@ static inline void write_watchdog_counter(const char *descr)u64 count = (u64 )cpu_khz *1000 ;do_div (count, nmi_hz); + count =min (count, (1UL << (nmi_p6_event_width -1 )) -1 );if (descr)Dprintk ("setting %s to -%#" PRIx64"\n" , descr, count);wrmsrl (nmi_perfctr_msr,0 - count);
@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( 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_DEFAULT ;clear_msr_range (MSR_P6_EVNTSEL (0 ),2 );clear_msr_range (MSR_P6_PERFCTR (0 ),2 );
@@ -529,7 +537,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
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 333
Quoted 8 lines · Igor Druzhinin
xen/arch/x86/nmi.c +4
-0@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( 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_DEFAULT ;
This is called on each cpu, but writes to a shared variable. This entire block wants to be something like:
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_DEFAULT;
(suitably wrapped) so it gets filled only once.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 14 lines · Andrew Cooper
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 333
Quoted 8 lines · Igor Druzhinin
xen/arch/x86/nmi.c +4
-0@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( 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_DEFAULT ;This is called on each cpu, but writes to a shared variable. This entire block wants to be something like:
Quoted 6 lines
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_DEFAULT;(suitably wrapped) so it gets filled only once.
This pattern seems to persist over the whole file - see nmi_perfctr_msr above or setup_p4_watchdog, etc. I either need to fix all of them or leave it like that as it's not harmful or anything.
Igor
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 14:36, <[email protected]> wrote:
Quoted 8 lines · Andrew Cooper
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 331
Quoted 6 lines · Igor Druzhinin
xen/arch/x86/nmi.c +2
-0@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( current_cpu_data.cpuid_level >=0xa ) + nmi_p6_event_width = MASK_EXTR(cpuid_eax(0xa),
P6_EVENT_WIDTH_MASK);Quoted 10 lines
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 333
Quoted 2 lines
xen/arch/x86/nmi.c +2
-0+if ( !nmi_p6_event_width ) + nmi_p6_event_width =P6_EVENT_WIDTH_DEFAULT ;This is called on each cpu, but writes to a shared variable. This entire block wants to be something like:
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_DEFAULT;
Quoted 6 lines
Quoted 2 lines
(suitably wrapped) so it gets filled only once.
This pattern seems to persist over the whole file - see
nmi_perfctr_msrabove orsetup_p4_watchdog, etc. I either need to fix all of them or leave it like that as it's not harmful or anything.
I don't think it hurts to do better than the surrounding code for new code additions, without as a prereq fixing all prior less optimal coding (but you're of course welcome to do so).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 14:25, <[email protected]> wrote:
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 333
Quoted 8 lines · Igor Druzhinin
xen/arch/x86/nmi.c +4
-0@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( 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_DEFAULT ;This is called on each cpu, but writes to a shared variable. This entire block wants to be something like:
Quoted 5 lines
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_DEFAULT;(suitably wrapped) so it gets filled only once.
Plus the low and high capping of the value read, as requested in reply to v1.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 14:25, <[email protected]> wrote:
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 333
Quoted 8 lines · Igor Druzhinin
xen/arch/x86/nmi.c +4
-0@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( 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_DEFAULT ;This is called on each cpu, but writes to a shared variable. This entire block wants to be something like:
Quoted 8 lines
Quoted 5 lines
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_DEFAULT;(suitably wrapped) so it gets filled only once.
Plus the low and high capping of the value read, as requested in reply to v1.
Isn't it the counter clipping you requested (which was implemented in v2)? What is the point in high capping? Is it safe to continue if the value read is lower than P6_EVENT_WIDTH_DEFAULT or should I panic here? Obviously, I'd need to clip the counter in that case as the rest of the code wouldn't work otherwise.
Igor
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 16:13, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 14:25, <[email protected]> wrote:
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 331
Quoted 6 lines · Igor Druzhinin
xen/arch/x86/nmi.c +2
-0@@ -323,6 +327,10 @@ static void setup_p6_watchdog(unsigned counter)unsigned int evntsel; nmi_perfctr_msr =MSR_P6_PERFCTR (0 ); +if ( current_cpu_data.cpuid_level >=0xa ) + nmi_p6_event_width = MASK_EXTR(cpuid_eax(0xa),
P6_EVENT_WIDTH_MASK);Quoted 10 lines
Quoted 10 lines
xen/arch/x86/nmi.c static void setup_p6_watchdog(unsigned counter) · line 333
Quoted 2 lines
xen/arch/x86/nmi.c +2
-0+if ( !nmi_p6_event_width ) + nmi_p6_event_width =P6_EVENT_WIDTH_DEFAULT ;This is called on each cpu, but writes to a shared variable. This entire block wants to be something like:
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_DEFAULT;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
(suitably wrapped) so it gets filled only once.
Plus the low and high capping of the value read, as requested in reply to v1.
Isn't it the counter clipping you requested (which was implemented in v2)? What is the point in high capping? Is it safe to continue if the value read is lower than
P6_EVENT_WIDTH_DEFAULTor should I panic here?
I think panic()ing is an opportune action in this situation, but I wouldn't mind just going with 32 instead in this case, or failing to enable the watchdog. On real hardware the value isn't going to be any lower, and in virtualized environments we'd have to figure why the value is smaller anyway.
As to the high capping - as said in the v1 thread, I'd like this to be there in particular for the shift operations to not become undefined.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 52 lines · Igor Druzhinin
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.
Signed-off-by Igor Druzhinin <[email protected]>---
Changes in v2:
* taken care of the case where leaf0xais missing or width is 0
* apply a cap in case width is lower than we're expecting to program--- xen/arch/x86/nmi.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)xen/arch/x86/nmi.c +4
-1@@ -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_DEFAULT 32#define P4_ESCR_EVENT_SELECT (N ) ((N)<<25)#define P4_CCCR_OVF_PMI0 (1<<26)
xen/arch/x86/nmi.c static inline void write_watchdog_counter(const char *descr) · line 301
Quoted 7 lines
xen/arch/x86/nmi.c +1
-0@@ -292,6 +295,7 @@ static inline void write_watchdog_counter(const char *descr)u64 count = (u64 )cpu_khz *1000 ;do_div (count, nmi_hz); + count =min (count, (1UL << (nmi_p6_event_width -1 )) -1 );if (descr)Dprintk ("setting %s to -%#" PRIx64"\n" , descr, count);wrmsrl (nmi_perfctr_msr,0 - count);
I mistakenly used nmi_p6_event_width in a shared call for all architectures - I'll fix it in v3.
Igor
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 14:33, <[email protected]> wrote:
Quoted 3 lines · Igor Druzhinin
xen/arch/x86/nmi.c +0
-0@@ -292,6 +295,7 @@ static inline void write_watchdog_counter(const char *descr)u64 count = (u64 )cpu_khz *1000 ;
Quoted 8 lines
xen/arch/x86/nmi.c static inline void write_watchdog_counter(const char *descr) · line 301
Quoted 5 lines
xen/arch/x86/nmi.c +1
-0do_div (count, nmi_hz); + count =min (count, (1UL << (nmi_p6_event_width -1 )) -1 );if (descr)Dprintk ("setting %s to -%#" PRIx64"\n" , descr, count);wrmsrl (nmi_perfctr_msr,0 - count);I mistakenly used
nmi_p6_event_widthin a shared call for all architectures - I'll fix it in v3.
But is clipping the count valid at all, no matter where you do it? Won't the NMI get raised earlier than expected then? Wouldn't it rather be nmi_hz that would then need changing if count ends up to large?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 14 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 26.02.19 at 14:33, <[email protected]> wrote:
Quoted 3 lines · Igor Druzhinin
xen/arch/x86/nmi.c +0
-0@@ -292,6 +295,7 @@ static inline void write_watchdog_counter(const char *descr)u64 count = (u64 )cpu_khz *1000 ;Quoted 8 lines
xen/arch/x86/nmi.c static inline void write_watchdog_counter(const char *descr) · line 301
Quoted 5 lines
xen/arch/x86/nmi.c +1
-0do_div (count, nmi_hz); + count =min (count, (1UL << (nmi_p6_event_width -1 )) -1 );if (descr)Dprintk ("setting %s to -%#" PRIx64"\n" , descr, count);wrmsrl (nmi_perfctr_msr,0 - count);I mistakenly used
nmi_p6_event_widthin a shared call for all architectures - I'll fix it in v3.
Quoted 5 lines
But is clipping the count valid at all, no matter where you do it? Won't the NMI get raised earlier than expected then? Wouldn't it rather be
nmi_hzthat would then need changing if count ends up to large?
From my understanding, the logic is that we're trying our best to make the delay between ticks as long as possible but not longer than 0.5 sec. This, of course, works only if the invariant of the highest bit being 1 is preserved.
Although, I wouldn't mind to drop this part since it basically worked before even without clipping.
Igor
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel