Skip to contentSkip to conversation

x86/vpmu_intel: Handle SMT consistently for programmable and fixed counters

PATCH Superseded Mohit Gambhir 4 participants 6 messages Mar 31, 2017 – Apr 5, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vpmu_intel: Handle SMT consistently for programmable and fixed counters

This patch masks .AnyThread bits in IA32_FIXED_CTR_CTRL MSR for all versions of Intel Arhcitectural Performance Monitoring. Note that .AnyThread bit (21) is already masked in IA32_PERFEVTSELx MSRs since hyperthreading is not exposed to guests and Intel SDM discourages the use of .AnyThread bit in virtualized environments (per section 18.2.3.1 AnyThread Counting and Software Evolution)

---
 xen/arch/x86/cpu/vpmu_intel.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/x86/cpu/vpmu_intel.c +1 -2
index 0d66ecb..7944e22 100644
@@ -979,8 +979,7 @@ int __init core2_vpmu_init(void)
     full_width_write = (caps >> 13) & 1;
 
     fixed_ctrl_mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 1);
-    if ( version == 2 )
-        fixed_ctrl_mask |= 0x444;
+    fixed_ctrl_mask |= 0x444;
     fixed_counters_mask = ~((1ull << core2_get_bitwidth_fix_count()) - 1);
     global_ctrl_mask = ~((((1ULL << fixed_pmc_cnt) - 1) << 32) |
                          ((1ULL << arch_pmc_cnt) - 1));
Signature · 7 lines
2.9.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Mohit Gambhir

This patch masks .AnyThread bits in IA32_FIXED_CTR_CTRL MSR for all versions of Intel Arhcitectural Performance Monitoring. Note that .AnyThread bit (21) is already masked in IA32_PERFEVTSELx MSRs since hyperthreading is not exposed to guests and Intel SDM discourages the use of .AnyThread bit in virtualized environments (per section 18.2.3.1 AnyThread Counting and Software Evolution)

(+Andrew and Jan)

Quoted 9 lines
---
 xen/arch/x86/cpu/vpmu_intel.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/x86/cpu/vpmu_intel.c +0 -0
index 0d66ecb..7944e22 100644

xen/arch/x86/cpu/vpmu_intel.c int __init core2_vpmu_init(void) · line 985

Quoted 9 lines
xen/arch/x86/cpu/vpmu_intel.c +1 -2
@@ -979,8 +979,7 @@ int __init core2_vpmu_init(void)
     full_width_write = (caps >> 13) & 1;
 
     fixed_ctrl_mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 1);
-    if ( version == 2 )
-        fixed_ctrl_mask |= 0x444;
+    fixed_ctrl_mask |= 0x444;
     fixed_counters_mask = ~((1ull << core2_get_bitwidth_fix_count()) - 1);
     global_ctrl_mask = ~((((1ULL << fixed_pmc_cnt) - 1) << 32) |
                          ((1ULL << arch_pmc_cnt) - 1));

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

#2 lore raw
In reply to Mohit Gambhir
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 31.03.17 at 16:46, <[email protected]> wrote:

This patch masks .AnyThread bits in IA32_FIXED_CTR_CTRL MSR for all versions of Intel Arhcitectural Performance Monitoring. Note that .AnyThread bit (21) is already masked in IA32_PERFEVTSELx MSRs since hyperthreading is not exposed to guests and Intel SDM discourages the use of .AnyThread bit in virtualized environments (per section 18.2.3.1 AnyThread Counting and Software Evolution)

All nice and presumably correct, but the main thing is missing: The bits aren't defined prior to version 3 afaics, so ...

xen/arch/x86/cpu/vpmu_intel.c int __init core2_vpmu_init(void) · line 982

Quoted 9 lines
xen/arch/x86/cpu/vpmu_intel.c +1 -2
@@ -979,8 +979,7 @@ int __init core2_vpmu_init(void)
     full_width_write = (caps >> 13) & 1;
 
     fixed_ctrl_mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 1);
-    if ( version == 2 )
-        fixed_ctrl_mask |= 0x444;
+    fixed_ctrl_mask |= 0x444;

... the main thing to explain is why removing the conditional is
(a) correct and (b) necessary (going through the uses of the variable I can see (a) to be true, but not (b)). And of course it would be quite helpful if the literal number changed to a manifest constant at once, or a comment was attached to clarify what the number represents.

Jan

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

#3 lore raw
Quoted 15 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 31.03.17 at 16:46, <[email protected]> wrote:

This patch masks .AnyThread bits in IA32_FIXED_CTR_CTRL MSR for all versions of Intel Arhcitectural Performance Monitoring. Note that .AnyThread bit (21) is already masked in IA32_PERFEVTSELx MSRs since hyperthreading is not exposed to guests and Intel SDM discourages the use of .AnyThread bit in virtualized environments (per section 18.2.3.1 AnyThread Counting and Software Evolution)

All nice and presumably correct, but the main thing is missing: The bits aren't defined prior to version 3 afaics, so ...

Quoted 5 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -979,8 +979,7 @@ int __init core2_vpmu_init(void)
      full_width_write = (caps >> 13) & 1;
 
Quoted 10 lines

xen/arch/x86/cpu/vpmu_intel.c int __init core2_vpmu_init(void) · line 982

Quoted 4 lines
xen/arch/x86/cpu/vpmu_intel.c +1 -2
      fixed_ctrl_mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 1);
-    if ( version == 2 )
-        fixed_ctrl_mask |= 0x444;
+    fixed_ctrl_mask |= 0x444;

... the main thing to explain is why removing the conditional is
(a) correct and (b) necessary (going through the uses of the variable I can see (a) to be true, but not (b)). And of course it would be quite helpful if the literal number changed to a manifest constant at once, or a comment was attached to clarify what the number represents.

I do agree that replacing the hard coded constant with a macro would be nice and I will update the patch with that.

The answer to why this change is (b) necessary is two folds -

1. We need to be consistent in the implementation. As said in the commit log - we disable .Anythread bit in programmable counters (regardless of the version) by masking bit 21 in IA32_PERFEVTSELx. (See code snippet below from vpmu_intel.c)

/* Masks used for testing whether and MSR is valid */ #define ARCH_CTRL_MASK (~((1ull << 32) - 1) | (1ull << 21))

But we leave it enabled in fixed function counters for version 3. Removing the condition disables the bit in fixed function counters regardless of the version, which is consistent with what is done for programmable counters.

2. We don't want to expose event counts from another guest (or hypervisor) which can happen if .AnyThread bit is not masked and a VCPU is only scheduled to run on one of the hardware threads in a hyper-threaded CPU.

Quoted 7 lines

Jan

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

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

#4 lore raw
Quoted 50 lines
Quoted 1 line
Quoted 1 line · On 03.04.17 at 21:23, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 31.03.17 at 16:46, <[email protected]> wrote:

This patch masks .AnyThread bits in IA32_FIXED_CTR_CTRL MSR for all versions of Intel Arhcitectural Performance Monitoring. Note that .AnyThread bit (21) is already masked in IA32_PERFEVTSELx MSRs since hyperthreading is not exposed to guests and Intel SDM discourages the use of .AnyThread bit in virtualized environments (per section 18.2.3.1 AnyThread Counting and Software Evolution)

All nice and presumably correct, but the main thing is missing: The bits aren't defined prior to version 3 afaics, so ...

Quoted 6 lines

xen/arch/x86/cpu/vpmu_intel.c int __init core2_vpmu_init(void) · line 981

Quoted 6 lines
xen/arch/x86/cpu/vpmu_intel.c +0 -0
@@ -979,8 +979,7 @@ int __init core2_vpmu_init(void)
      full_width_write = (caps >> 13) & 1;
 
      fixed_ctrl_mask = ~((1ull << (fixed_pmc_cnt * FIXED_CTR_CTRL_BITS)) - 

1);

Quoted 9 lines

xen/arch/x86/cpu/vpmu_intel.c int __init core2_vpmu_init(void) · line 982

Quoted 3 lines
xen/arch/x86/cpu/vpmu_intel.c +1 -2
-    if ( version == 2 )
-        fixed_ctrl_mask |= 0x444;
+    fixed_ctrl_mask |= 0x444;

... the main thing to explain is why removing the conditional is
(a) correct and (b) necessary (going through the uses of the variable I can see (a) to be true, but not (b)). And of course it would be quite helpful if the literal number changed to a manifest constant at once, or a comment was attached to clarify what the number represents.

I do agree that replacing the hard coded constant with a macro would be nice and I will update the patch with that.

The answer to why this change is (b) necessary is two folds -

1. We need to be consistent in the implementation. As said in the commit log - we disable .Anythread bit in
programmable counters (regardless of the version) by masking bit 21 in IA32_PERFEVTSELx. (See code snippet
below from vpmu_intel.c)

/* Masks used for testing whether and MSR is valid */ #define ARCH_CTRL_MASK (~((1ull << 32) - 1) | (1ull << 21))

But we leave it enabled in fixed function counters for version 3. Removing the condition disables the bit in fixed function counters regardless of the version, which is consistent with what is done for programmable counters.

Quoted 4 lines

2. We don't want to expose event counts from another guest (or hypervisor) which can happen if .AnyThread bit is not masked and a VCPU is only scheduled to run on one of the hardware threads in a hyper-threaded CPU.

Okay, I can see now that I could, with some difficulty, have read this out of the original description. I'd appreciate if this was made more clear though (along the lines of the explanation you gave in reply to my question).

Jan

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

Reviewed-by on v1 1/1
#5 lore raw
In reply to Mohit Gambhir
Quoted 11 lines

From: Mohit Gambhir [mailto:[email protected]]
Sent: Friday, March 31, 2017 10:46 PM

This patch masks .AnyThread bits in IA32_FIXED_CTR_CTRL MSR for all versions of Intel Arhcitectural Performance Monitoring. Note that .AnyThread bit (21) is already masked in IA32_PERFEVTSELx MSRs since hyperthreading is not exposed to guests and Intel SDM discourages the use of
.AnyThread bit in virtualized environments (per section 18.2.3.1 AnyThread Counting and Software Evolution)

Quoted 1 line

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