Intel vPMU improvements
PATCH 5/6 x86/vpmu: Limit to using supported general/fixed counters
In reply to Teddy AstieHardware can support more than 8 general or 3 fixed counters, which could lead to inconsistent access to MSRs (thus failure to initialize vPMU in guest) as we don't support accessing more than these limits.
---
I'm not aware of a processor that have more than that, but the specification tells that this is possible.
xen/arch/x86/cpu-policy.c | 6 ++++++ xen/arch/x86/cpu/vpmu_intel.c | 15 +++++++++++++++ 2 files changed, 21 insertions(+)
xen/arch/x86/cpu-policy.c +6 -0
@@ -276,6 +276,12 @@ switch ( p->x86_vendor )if ( p->basic.pmu.version >2 ) p->basic.pmu.version =2 ; +/* Truncate control register count to what we support */ +if ( p->basic.pmu.num_gp_ctrs >8 ) + p->basic.pmu.num_gp_ctrs =8 ; + +if ( p->basic.pmu.num_fixed_ctr >3 ) + p->basic.pmu.num_fixed_ctr =3 ;break ; } }
xen/arch/x86/cpu/vpmu_intel.c +15 -0
@@ -924,7 +924,22 @@ const struct arch_vpmu_ops *__init core2_vpmu_init(void) } arch_pmc_cnt =core2_get_arch_pmc_count (); +if ( arch_pmc_cnt >8 ) + { +printk (XENLOG_INFO +"VPMU: Too many general counters (%u), emulating 8 registers\n" , + arch_pmc_cnt); + arch_pmc_cnt =8 ; + } + fixed_pmc_cnt =core2_get_fixed_pmc_count (); +if ( fixed_pmc_cnt >3 ) + { +printk (XENLOG_INFO +"VPMU: Too many fixed counters (%u), emulating 3 registers\n" , + arch_pmc_cnt); + fixed_pmc_cnt =3 ; + }if ( cpu_has_pdcm ) {
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
PATCH 3/6 x86: Define some Intel vPMU leafs
In reply to Teddy Astie--- xen/arch/x86/cpu/vpmu_intel.c | 4 ++-- xen/arch/x86/domain.c | 2 +- xen/include/xen/lib/x86/cpu-policy.h | 10 +++++++++- 3 files changed, 12 insertions(+), 4 deletions(-)
xen/arch/x86/cpu/vpmu_intel.c +2 -2
@@ -818,8 +818,8 @@ static int cf_check core2_vpmu_initialise(struct vcpu *v)u64 msr_content;static bool ds_warned; -if ( v->domain->arch.cpuid->basic.pmu_version <=1 || - v->domain->arch.cpuid->basic.pmu_version >=6 ) +if ( v->domain->arch.cpuid->basic.pmu.version <=1 || + v->domain->arch.cpuid->basic.pmu.version >=6 )return -EINVAL ;if ( (arch_pmc_cnt + fixed_pmc_cnt) ==0 )
xen/arch/x86/domain.c +1 -1
@@ -458,7 +458,7 @@ void domain_cpu_policy_changed(struct domain *d)/* If PMU version is zero then the guest doesn't have VPMU */ if ( boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL && - p->basic.pmu_version ==0 ) + p->basic.pmu.version ==0 )vpmu_destroy (v); } }
xen/include/xen/lib/x86/cpu-policy.h +9 -1
@@ -162,7 +162,15 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0x9 - DCA *//* Leaf 0xa - Intel PMU. */ -uint8_t pmu_version, _pmu[15 ]; +struct { +uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width, + event_enum_length; + uint32_t /* b */:32; +uint32_t /* c */ fixed_ctr_mask; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, + anythread_depreciation:1, slots_per_cyc:4, + :13 ; + } pmu; uint64_t :64, :64; /* Leaf 0xb - Topology. */ uint64_t :64, :64; /* Leaf 0xc - rsvd */
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
PATCH 4/6 x86/vpmu: Sanitise Intel PMU version
In reply to Teddy AstieCurrently, Xen exposes the host CPUID as-is, which is not correct on hardware that implements PMU features that are not supported by Xen (e.g AnyThread bit of PMU version 3, IA32_PERF_GLOBAL_INUSE of PMU version 4, and many other things).
As we're actually virtualizing PMU version 2 (as version 3 is the same as version 2 with the addition to AnyThread bit, which is we don't support since [1]), adjust the messages related to PMU emulation to account that.
[1] 9f5390441a6e ("x86/vpmu_intel: handle SMT consistently for programmable and fixed counters")
---
It could sound weird that we're now exposing version 2, while we were previously (wrongly) exposing version 3. Version 3 mostly works because the guest is not aware of hyperthreads, hence is not expected to try using AnyThread bit (which currently faults due to consistency checks). But we were still actually virtualizing PMU version 2.
xen/arch/x86/cpu-policy.c | 33 ++++++++++++++++++++++++++++++++- xen/arch/x86/cpu/vpmu_intel.c | 4 ++-- 2 files changed, 34 insertions(+), 3 deletions(-)
xen/arch/x86/cpu-policy.c +32 -1
@@ -253,6 +253,33 @@ static void recalculate_xstate(struct cpu_policy *p) } } +static void sanitise_vpmu (struct cpu_policy *p) +{ +switch ( p->x86_vendor ) + { +case X86_VENDOR_INTEL : +if ( !p->basic.pmu.version ) +return ; + +/* +* Expose up to PMU version 2 +* +* PMU version 3 introduced the AnyThread bit we don't want +* to support (see "AnyThread Counting and Software Evolution" +* regarding notes with virtualization). This is the only known +* feature introduced in PMU version 3. +* +* PMU version 5 deprecated AnyThread, and introduced a CPUID +* bit (ANYTHREAD_DEPRECATION) to indicate that this bit isn't supported. +* That CPUID bit should be set for PMU version 5. +*/ +if ( p->basic.pmu.version >2 ) + p->basic.pmu.version =2 ; + +break ; + } +} +/* * Misc adjustments to the policy. Mostly clobbering reserved fields and * duplicating shared fields. Intentionally hidden fields are annotated.
@@ -391,6 +418,8 @@ static void __init calculate_host_policy(void)/* When vPMU is disabled, drop it from the host policy. */ if ( vpmu_mode ==XENPMU_MODE_OFF ) p->basic.raw[0xa ] =EMPTY_LEAF ; +else +sanitise_vpmu (p);/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* probe_cpuid_faulting() sanity checks presence of MISC_FEATURES_ENABLES */
@@ -841,7 +870,7 @@ static void __init calculate_hvm_max_policy(void)/* Enable features which are always emulated. */ p->extd.raw[0xa ].d |= (1u <<SVM_FEATURE_VMCBCLEAN ); } - +guest_common_max_feature_adjustments (fs);guest_common_feature_adjustments (fs);
@@ -1054,6 +1083,8 @@ void recalculate_cpuid_policy(struct domain *d)if ( vpmu_mode ==XENPMU_MODE_OFF || ((vpmu_mode &XENPMU_MODE_ALL ) && !is_hardware_domain (d)) ) p->basic.raw[0xa ] =EMPTY_LEAF ; +else +sanitise_vpmu (p);if ( !p->extd.svm ) p->extd.raw[0xa ] =EMPTY_LEAF ;
xen/arch/x86/cpu/vpmu_intel.c +2 -2
@@ -901,14 +901,14 @@ const struct arch_vpmu_ops *__init core2_vpmu_init(void)switch ( version ) { +case 3 :case 4 :case 5 :printk (XENLOG_INFO "VPMU: PMU version %u is not fully supported. " -"Emulating version 3\n" , version); +"Emulating version 2\n" , version);/* FALLTHROUGH */ case 2 : -case 3 :break ;default :
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
PATCH 1/6 x86/vpmu: Expose up to 8 Intel event selectors in PV Dom0
In reply to Teddy AstieMost (if not all) Intel CPUs starting from Sandy Bridge have up to 8 event selectors per core, which could be halved per hyperthread.
However, current PV emulation logic doesn't support up to 8 event selector, leading to errors when trying to access them, hence, preventing Linux from driving the vPMU correctly.
Make sure up to MSR_P6_EVNTSEL(7) is usable, which is the same upper bound as used in VMX code.
The check if the event selector actually exist for the hardware is done in core2_vpmu_do_{rdmsr,wrmsr}, hence we're not allowing to access non-existent MSRs.
--- xen/arch/x86/pv/emul-priv-op.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/pv/emul-priv-op.c +2 -2
@@ -990,7 +990,7 @@ static int cf_check read_msr(return X86EMUL_OKAY ; case MSR_P6_PERFCTR(0) ... MSR_P6_PERFCTR(7): - case MSR_P6_EVNTSEL(0) ... MSR_P6_EVNTSEL(3): + case MSR_P6_EVNTSEL(0) ... MSR_P6_EVNTSEL(7): case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR2: case MSR_CORE_PERF_FIXED_CTR_CTRL ... MSR_CORE_PERF_GLOBAL_OVF_CTRL:if ( boot_cpu_data.vendor ==X86_VENDOR_INTEL )
@@ -1167,7 +1167,7 @@ static int cf_check write_msr(break ; case MSR_P6_PERFCTR(0) ... MSR_P6_PERFCTR(7): - case MSR_P6_EVNTSEL(0) ... MSR_P6_EVNTSEL(3): + case MSR_P6_EVNTSEL(0) ... MSR_P6_EVNTSEL(7): case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR2: case MSR_CORE_PERF_FIXED_CTR_CTRL ... MSR_CORE_PERF_GLOBAL_OVF_CTRL:if ( boot_cpu_data.vendor ==X86_VENDOR_INTEL )
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
cover Intel vPMU improvements
First patch fix a problem that prevents vPMU from properly working with a PV domain on a processor that doesn't have hyperthreading.
The second patch adds a missing PEBS/DS_AREA MSR handler for PV.
The next patches tries to introduce a proper CPUID handling and restrict some hardware limits to Xen ones.
Last patch allow vPMU to work with PMU version 6 (Intel Core Ultra and alike).
Teddy Astie (6):
x86/vpmu: Expose up to 8 Intel event selectors in PV Dom0 x86/vpmu: Expose PEBS and DS area in PV mode
x86: Define some Intel vPMU leafs
x86/vpmu: Sanitise Intel PMU version
x86/vpmu: Limit to using supported general/fixed counters x86/vpmu: Allow PMU version 6
xen/arch/x86/cpu-policy.c | 39 +++++++++++++++++++++++++++- xen/arch/x86/cpu/vpmu_intel.c | 24 ++++++++++++++--- xen/arch/x86/domain.c | 2 +- xen/arch/x86/pv/emul-priv-op.c | 8 ++++-- xen/include/xen/lib/x86/cpu-policy.h | 10 ++++++- 5 files changed, 74 insertions(+), 9 deletions(-)
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
PATCH 2/6 x86/vpmu: Expose PEBS and DS area in PV mode
I don't see any reason for them for not be available, especially since core2_vpmu_do_wrmsr has PV specific logic for MSR_IA32_DS_AREA.
--- xen/arch/x86/pv/emul-priv-op.c | 4 ++++ 1 file changed, 4 insertions(+)
xen/arch/x86/pv/emul-priv-op.c +4 -0
@@ -993,6 +993,8 @@ static int cf_check read_msr( case MSR_P6_EVNTSEL(0) ... MSR_P6_EVNTSEL(7): case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR2: case MSR_CORE_PERF_FIXED_CTR_CTRL ... MSR_CORE_PERF_GLOBAL_OVF_CTRL: +case MSR_IA32_PEBS_ENABLE : +case MSR_IA32_DS_AREA :if ( boot_cpu_data.vendor ==X86_VENDOR_INTEL ) { vpmu_msr = true;
@@ -1170,6 +1172,8 @@ static int cf_check write_msr( case MSR_P6_EVNTSEL(0) ... MSR_P6_EVNTSEL(7): case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR2: case MSR_CORE_PERF_FIXED_CTR_CTRL ... MSR_CORE_PERF_GLOBAL_OVF_CTRL: +case MSR_IA32_PEBS_ENABLE : +case MSR_IA32_DS_AREA :if ( boot_cpu_data.vendor ==X86_VENDOR_INTEL ) { vpmu_msr = true;
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
PATCH 6/6 x86/vpmu: Allow PMU version 6
In reply to Teddy AstiePMU version 6 (notably implemented in Intel Core Ultra) is backward compatible with older versions, as we're exposing version 2 to the guest, it is not expected not try using PMU version 6 features.
---
I don't have the hardware to test vPMU on Intel Core Ultra, but I guess it's supposed to work, as it only introduces new features and doesn't disable existing ones.
That could want a changelog entry regarding vPMU support on Intel Core Ultra and alike ?
xen/arch/x86/cpu/vpmu_intel.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/cpu/vpmu_intel.c +2 -1
@@ -819,7 +819,7 @@ static int cf_check core2_vpmu_initialise(struct vcpu *v)static bool ds_warned;if ( v->domain->arch.cpuid->basic.pmu.version <=1 || - v->domain->arch.cpuid->basic.pmu.version >=6 ) + v->domain->arch.cpuid->basic.pmu.version >=7 )return -EINVAL ;if ( (arch_pmc_cnt + fixed_pmc_cnt) ==0 )
@@ -904,6 +904,7 @@ const struct arch_vpmu_ops *__init core2_vpmu_init(void)case 3 :case 4 :case 5 : +case 6 :printk (XENLOG_INFO "VPMU: PMU version %u is not fully supported. " "Emulating version 2\n" , version);/* FALLTHROUGH */
Signature · 10 lines
2.53.0 -- Teddy Astie | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
Re: [PATCH 1/6] x86/vpmu: Expose up to 8 Intel event selectors in PV Dom0
In reply to Teddy AstieQuoted 10 lines · Teddy Astie
Most (if not all) Intel CPUs starting from Sandy Bridge have up to 8 event selectors per core, which could be halved per hyperthread.
However, current PV emulation logic doesn't support up to 8 event selector, leading to errors when trying to access them, hence, preventing Linux from driving the vPMU correctly.
Make sure up to
MSR_P6_EVNTSEL(7)is usable, which is the same upper bound as used in VMX code.
Quoted 6 lines
The check if the event selector actually exist for the hardware is done in core2_vpmu_do_{rdmsr,wrmsr}, hence we're not allowing to access non-existent MSRs.
Fixes 27c554198666 ("x86/VPMU: add support for PMU register handling on PV guests")Signed-off-by Teddy Astie <[email protected]>
Re: [PATCH 2/6] x86/vpmu: Expose PEBS and DS area in PV mode
In reply to Teddy AstieQuoted 2 lines · Teddy Astie
I don't see any reason for them for not be available, especially since
core2_vpmu_do_wrmsrhas PV specific logic forMSR_IA32_DS_AREA.
This is really dangerous: You allow PV domains to control whether the area is actually mapped. It lacking a mapping can, iirc, on at least some CPUs result in a complete hang. I do, in fact, have been carrying a patch to completely disallow DS area use for PV, eliminating the misleading code you refer to.
Also note that VPMU_CPU_HAS_DS cannot be set for PV vCPU-s anyway.
Quoted 1 line
Fixes 27c554198666 ("x86/VPMU: add support for PMU register handling on PV guests")
Not just because of the above, I'm pretty sure a Fixes: tag is inappropriate here.
Jan
Re: [PATCH 3/6] x86: Define some Intel vPMU leafs
In reply to Teddy AstieQuoted 6 lines · Teddy Astie
xen/include/xen/lib/x86/cpu-policy.h +0
-0@@ -162,7 +162,15 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0x9 - DCA *//* Leaf 0xa - Intel PMU. */
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 173
Quoted 10 lines
xen/include/xen/lib/x86/cpu-policy.h +9
-1-uint8_t pmu_version, _pmu[15 ]; +struct { +uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width, + event_enum_length; + uint32_t /* b */:32; +uint32_t /* c */ fixed_ctr_mask; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, + anythread_depreciation:1, slots_per_cyc:4, + :13 ; + } pmu;
Style-wise this looks to follow e.g. the cache leaf, so perhaps okay, even if I would have preferred you to follow what we did for leaf 6. The named boolean field, however, wants to be of type bool. And then the unnamed 1-bit field really wants to be 2 bits, for anythread_depreciation to be bit 15 (etc).
Jan
Re: [PATCH 2/6] x86/vpmu: Expose PEBS and DS area in PV mode
In reply to Jan BeulichQuoted 6 lines · Jan Beulich
Quoted 2 lines · Teddy Astie
I don't see any reason for them for not be available, especially since
core2_vpmu_do_wrmsrhas PV specific logic forMSR_IA32_DS_AREA.This is really dangerous: You allow PV domains to control whether the area is actually mapped. It lacking a mapping can, iirc, on at least some CPUs result in a complete hang.
It's ~all, and explicitly documented. SDM Vol3 20.4.9.3:
"The recording of branch records in the BTS buffer (or PEBS records in the PEBS buffer) may not operate properly if accesses to the linear addresses in any of the three DS save area sections cause page faults, VM exits, or the setting of accessed or dirty flags in the paging structures (ordinary or EPT). For that reason, system software should establish paging structures (both ordinary and EPT) to prevent such occurrences."
There are potentially uses for PEBS/DS, but it needs to be via explicit opt in only; it is absolutely not safe to let guests have in general.
One fun interaction would be a PV domain which gets shadowed (PV-L1TF, or migrated), which will instantly violate the #PF requirement.
~Andrew
Re: [PATCH 2/6] x86/vpmu: Expose PEBS and DS area in PV mode
Quoted 18 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Teddy Astie
I don't see any reason for them for not be available, especially since
core2_vpmu_do_wrmsrhas PV specific logic forMSR_IA32_DS_AREA.This is really dangerous: You allow PV domains to control whether the area is actually mapped. It lacking a mapping can, iirc, on at least some CPUs result in a complete hang.
It's ~all, and explicitly documented. SDM Vol3 20.4.9.3:
"The recording of branch records in the BTS buffer (or PEBS records in the PEBS buffer) may not operate properly if accesses to the linear addresses in any of the three DS save area sections cause page faults, VM exits, or the setting of accessed or dirty flags in the paging structures (ordinary or EPT). For that reason, system software should establish paging structures (both ordinary and EPT) to prevent such occurrences."
Quoted 2 lines
There are potentially uses for PEBS/DS, but it needs to be via explicit opt in only; it is absolutely not safe to let guests have in general.
That would extend to HVM as well then, wouldn't it?
Quoted 2 lines
One fun interaction would be a PV domain which gets shadowed (PV-L1TF, or migrated), which will instantly violate the #PF requirement.
Same here, just with EPT misconfig exits in place of #PF?
Jan
Re: [PATCH 2/6] x86/vpmu: Expose PEBS and DS area in PV mode
Quoted 18 lines · Jan Beulich
Quoted 17 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Teddy Astie
I don't see any reason for them for not be available, especially since
core2_vpmu_do_wrmsrhas PV specific logic forMSR_IA32_DS_AREA.This is really dangerous: You allow PV domains to control whether the area is actually mapped. It lacking a mapping can, iirc, on at least some CPUs result in a complete hang.
It's ~all, and explicitly documented. SDM Vol3 20.4.9.3:
"The recording of branch records in the BTS buffer (or PEBS records in the PEBS buffer) may not operate properly if accesses to the linear addresses in any of the three DS save area sections cause page faults, VM exits, or the setting of accessed or dirty flags in the paging structures (ordinary or EPT). For that reason, system software should establish paging structures (both ordinary and EPT) to prevent such occurrences."
Quoted 7 lines
Quoted 2 lines
There are potentially uses for PEBS/DS, but it needs to be via explicit opt in only; it is absolutely not safe to let guests have in general.
That would extend to HVM as well then, wouldn't it?
Quoted 2 lines
One fun interaction would be a PV domain which gets shadowed (PV-L1TF, or migrated), which will instantly violate the #PF requirement.
Same here, just with EPT misconfig exits in place of #PF?
Yes it does extend to HVM guests too. The difference is that it already exists for HVM guests, via the vpmu=dts option.
~Andrew
Re: [PATCH 4/6] x86/vpmu: Sanitise Intel PMU version
In reply to Teddy Astiexen/arch/x86/cpu-policy.c static void recalculate_xstate(struct cpu_policy *p) · line 258
Quoted 9 lines · Teddy Astie
xen/arch/x86/cpu-policy.c +3
-0@@ -253,6 +253,33 @@ static void recalculate_xstate(struct cpu_policy *p) } } +static void sanitise_vpmu (struct cpu_policy *p) +{ +switch ( p->x86_vendor )
Nit: Lack of indentation.
xen/arch/x86/cpu-policy.c static void recalculate_xstate(struct cpu_policy *p) · line 262
Quoted 4 lines
xen/arch/x86/cpu-policy.c +4
-0+ { +case X86_VENDOR_INTEL : +if ( !p->basic.pmu.version ) +return ;
Better use "break", but: Why is this check needed anyway? Nothing ...
Quoted 7 lines
xen/arch/x86/cpu-policy.c +7
-0+/* +* Expose up to PMU version 2 +* +* PMU version 3 introduced the AnyThread bit we don't want +* to support (see "AnyThread Counting and Software Evolution" +* regarding notes with virtualization). This is the only known +* feature introduced in PMU version 3.
xen/arch/x86/cpu-policy.c static void recalculate_xstate(struct cpu_policy *p) · line 280
Quoted 10 lines
xen/arch/x86/cpu-policy.c +10
-0+* +* PMU version 5 deprecated AnyThread, and introduced a CPUID +* bit (ANYTHREAD_DEPRECATION) to indicate that this bit isn't supported. +* That CPUID bit should be set for PMU version 5. +*/ +if ( p->basic.pmu.version >2 ) + p->basic.pmu.version =2 ; + +break ; + }
... here breaks if it was dropped.
Jan
Re: [PATCH 5/6] x86/vpmu: Limit to using supported general/fixed counters
In reply to Teddy Astiexen/arch/x86/cpu-policy.c switch ( p->x86_vendor ) · line 277
Quoted 5 lines · Teddy Astie
xen/arch/x86/cpu-policy.c +0
-0@@ -276,6 +276,12 @@ switch ( p->x86_vendor )if ( p->basic.pmu.version >2 ) p->basic.pmu.version =2 ;
The literal numbers here are already on the edge, but ...
xen/arch/x86/cpu-policy.c switch ( p->x86_vendor ) · line 284
Quoted 6 lines
xen/arch/x86/cpu-policy.c +6
-0+/* Truncate control register count to what we support */ +if ( p->basic.pmu.num_gp_ctrs >8 ) + p->basic.pmu.num_gp_ctrs =8 ; + +if ( p->basic.pmu.num_fixed_ctr >3 ) + p->basic.pmu.num_fixed_ctr =3 ;
... these surely go too far. How do you suggest would one (easily) identify all the 3-s or 8-s that belong together when wanting to bump the limit? (Altogether therefore already the earlier patch wants to introduce a #define for the max guest-visible version.)
Jan
Re: [PATCH 6/6] x86/vpmu: Allow PMU version 6
In reply to Teddy AstieQuoted 4 lines · Teddy Astie
PMU version 6 (notably implemented in Intel Core Ultra) is backward compatible with older versions, as we're exposing version 2 to the guest, it is not expected not try using PMU version 6 features.
Quoted 8 lines
Signed-off-by Teddy Astie <[email protected]>---
I don't have the hardware to test vPMU on Intel Core Ultra, but I guess it's supposed to work, as it only introduces new features and doesn't disable existing ones.That could want a changelog entry regarding vPMU support on Intel Core Ultra and alike ?
Perhaps.
Quoted 12 lines
xen/arch/x86/cpu/vpmu_intel.c +1
-1@@ -819,7 +819,7 @@ static int cf_check core2_vpmu_initialise(struct vcpu *v)static bool ds_warned;if ( v->domain->arch.cpuid->basic.pmu.version <=1 || - v->domain->arch.cpuid->basic.pmu.version >=6 ) + v->domain->arch.cpuid->basic.pmu.version >=7 )return -EINVAL ;if ( (arch_pmc_cnt + fixed_pmc_cnt) ==0 )
xen/arch/x86/cpu/vpmu_intel.c const struct arch_vpmu_ops *__init core2_vpmu_init(void) · line 907
Quoted 4 lines
xen/arch/x86/cpu/vpmu_intel.c +1
-0@@ -904,6 +904,7 @@ const struct arch_vpmu_ops *__init core2_vpmu_init(void)case 3 :case 4 :case 5 : +case 6 :
This is getting a little unwieldy imo - can we switch to "case 3 ... 6:"?
Jan
Quoted 3 lines
xen/arch/x86/cpu/vpmu_intel.c +0
-0printk (XENLOG_INFO "VPMU: PMU version %u is not fully supported. " "Emulating version 2\n" , version);/* FALLTHROUGH */
Re: [PATCH 3/6] x86: Define some Intel vPMU leafs
In reply to Jan BeulichLe 24/03/2026 à 10:25, Jan Beulich a écrit :
Quoted 6 lines · Teddy Astie
Quoted 5 lines
xen/include/xen/lib/x86/cpu-policy.h +0
-0@@ -162,7 +162,15 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0x9 - DCA */
Quoted 14 lines
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 173
Quoted 11 lines
xen/include/xen/lib/x86/cpu-policy.h +9
-1/* Leaf 0xa - Intel PMU. */ -uint8_t pmu_version, _pmu[15 ]; +struct { +uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width, + event_enum_length; + uint32_t /* b */:32; +uint32_t /* c */ fixed_ctr_mask; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, + anythread_depreciation:1, slots_per_cyc:4, + :13 ; + } pmu;Style-wise this looks to follow e.g. the cache leaf, so perhaps okay, even if I would have preferred you to follow what we did for leaf 6.
My idea was to put all that as .pmu.*, so I wouldn't need to prefix everything with "pmu_". I'm not sure if you're talking about a different approach.
Quoted 1 line
The named> boolean field, however, wants to be of type bool.
Which fields ?
Quoted 1 line
And then the unnamed 1-bit> field really wants to be 2 bits, for
anythread_depreciation to be bit 15
Quoted 2 lines
(etc).
Ah yes thanks, I got confused with the fields size for a second. I also found that slots_per_cyc is 3 bits instead of 4.
I think this diff fixes it overall.
(quoted diff) +0 -0
b/xen/include/xen/lib/x86/cpu-policy.h
index 9161e2ad8d..796c2edb0e 100644
xen/include/xen/lib/x86/cpu-policy.h +3 -3
@@ -167,9 +167,9 @@ struct cpu_policyevent_enum_length ; uint32_t /* b */:32;uint32_t /* c */ fixed_ctr_mask; - uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, - anythread_depreciation:1, slots_per_cyc:4, - :13; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :2, + anythread_depreciation:1, slots_per_cyc:3, + :11; } pmu; uint64_t :64, :64; /* Leaf 0xb - Topology. */
Making the first edx reserved gap actually 2 bits, slots_per_cyc actually 3 bits and adjusting the end reserved part that is actually 11 bits.
Quoted 2 lines
Jan
Teddy
--
Teddy Astie | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
Re: [PATCH 4/6] x86/vpmu: Sanitise Intel PMU version
In reply to Jan BeulichLe 24/03/2026 à 17:03, Jan Beulich a écrit :
Quoted 13 lines · Teddy Astie
xen/arch/x86/cpu-policy.c static void recalculate_xstate(struct cpu_policy *p) · line 258
Quoted 9 lines
xen/arch/x86/cpu-policy.c +3
-0@@ -253,6 +253,33 @@ static void recalculate_xstate(struct cpu_policy *p) } } +static void sanitise_vpmu (struct cpu_policy *p) +{ +switch ( p->x86_vendor )Nit: Lack of indentation.
Quoted 7 lines
xen/arch/x86/cpu-policy.c static void recalculate_xstate(struct cpu_policy *p) · line 262
Quoted 4 lines
xen/arch/x86/cpu-policy.c +4
-0+ { +case X86_VENDOR_INTEL : +if ( !p->basic.pmu.version ) +return ;Better use "break", but: Why is this check needed anyway? Nothing ...
My idea was to return early if there is no version (keeping everything 0). But as I only added checks for PMU version in this patch, this is effectively redundant at this moment.
Although, "x86/vpmu: Limit to using supported general/fixed counters" makes some additional adjustments to the CPUID if pmu.version != 0 making it non redundant anymore.
But yes, it wants 'break' here.
Quoted 18 lines
Quoted 7 lines
xen/arch/x86/cpu-policy.c +7
-0+/* +* Expose up to PMU version 2 +* +* PMU version 3 introduced the AnyThread bit we don't want +* to support (see "AnyThread Counting and Software Evolution" +* regarding notes with virtualization). This is the only known +* feature introduced in PMU version 3. xen/arch/x86/cpu-policy.c static void recalculate_xstate(struct cpu_policy *p) · line 280
Quoted 10 lines
xen/arch/x86/cpu-policy.c +10
-0+* +* PMU version 5 deprecated AnyThread, and introduced a CPUID +* bit (ANYTHREAD_DEPRECATION) to indicate that this bit isn't supported. +* That CPUID bit should be set for PMU version 5. +*/ +if ( p->basic.pmu.version >2 ) + p->basic.pmu.version =2 ; + +break ; + }
Quoted 4 lines
... here breaks if it was dropped.
Jan
Teddy
--
Teddy Astie | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
Re: [PATCH 2/6] x86/vpmu: Expose PEBS and DS area in PV mode
In reply to Jan BeulichLe 24/03/2026 à 10:14, Jan Beulich a écrit :
Quoted 10 lines · Teddy Astie
Quoted 2 lines
I don't see any reason for them for not be available, especially since
core2_vpmu_do_wrmsrhas PV specific logic forMSR_IA32_DS_AREA.This is really dangerous: You allow PV domains to control whether the area is actually mapped. It lacking a mapping can, iirc, on at least some CPUs result in a complete hang. I do, in fact, have been carrying a patch to completely disallow DS area use for PV, eliminating the misleading code you refer to.
While PV case is particularly quirky (especially with L1TF), the issues still exists for HVM.
I suppose things may be a bit better with "EPT-Friendly PEBS" though.
Regardless, we already say that the feature is potentially unsafe to use, and it still needs to be opted-in, so this patch just allows the guest to use something we advertise (with its eventual quirks).
Quoted 2 lines
Also note that
VPMU_CPU_HAS_DScannot be set for PV vCPU-s anyway.
Why is that ?
vpmu_set(vpmu, VPMU_CPU_HAS_DS); made in core2_vpmu_initialise is called in either PV and HVM cases.
Quoted 7 lines
Quoted 1 line
Fixes 27c554198666 ("x86/VPMU: add support for PMU register handling on PV guests")Not just because of the above, I'm pretty sure a Fixes: tag is inappropriate here.
Jan
Teddy
--
Teddy Astie | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
Re: [PATCH 2/6] x86/vpmu: Expose PEBS and DS area in PV mode
Quoted 20 lines · Teddy Astie
Le 24/03/2026 à 10:14, Jan Beulich a écrit :
Quoted 10 lines · Teddy Astie
Quoted 2 lines
I don't see any reason for them for not be available, especially since
core2_vpmu_do_wrmsrhas PV specific logic forMSR_IA32_DS_AREA.This is really dangerous: You allow PV domains to control whether the area is actually mapped. It lacking a mapping can, iirc, on at least some CPUs result in a complete hang. I do, in fact, have been carrying a patch to completely disallow DS area use for PV, eliminating the misleading code you refer to.
While PV case is particularly quirky (especially with L1TF), the issues still exists for HVM.
I suppose things may be a bit better with "EPT-Friendly PEBS" though.Regardless, we already say that the feature is potentially unsafe to use, and it still needs to be opted-in, so this patch just allows the guest to use something we advertise (with its eventual quirks).
Quoted 7 lines
Quoted 2 lines
Also note that
VPMU_CPU_HAS_DScannot be set for PV vCPU-s anyway.Why is that ?
vpmu_set(vpmu, VPMU_CPU_HAS_DS);made incore2_vpmu_initialiseis called in either PV and HVM cases.
Oh, I'm sorry, that's yet again a result of aforementioned patch.
Jan
Re: [PATCH 3/6] x86: Define some Intel vPMU leafs
In reply to Teddy AstieQuoted 22 lines · Teddy Astie
Le 24/03/2026 à 10:25, Jan Beulich a écrit :
Quoted 6 lines · Teddy Astie
Quoted 5 lines
xen/include/xen/lib/x86/cpu-policy.h +0
-0@@ -162,7 +162,15 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0x9 - DCA */Quoted 14 lines
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 173
Quoted 11 lines
xen/include/xen/lib/x86/cpu-policy.h +9
-1/* Leaf 0xa - Intel PMU. */ -uint8_t pmu_version, _pmu[15 ]; +struct { +uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width, + event_enum_length; + uint32_t /* b */:32; +uint32_t /* c */ fixed_ctr_mask; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, + anythread_depreciation:1, slots_per_cyc:4, + :13 ; + } pmu;Style-wise this looks to follow e.g. the cache leaf, so perhaps okay, even if I would have preferred you to follow what we did for leaf 6.
Quoted 3 lines
My idea was to put all that as .pmu.*, so I wouldn't need to prefix everything with "
pmu_". I'm not sure if you're talking about a different approach.
The "pmu" is fine. I'm talking of what's inside the struct {}.
Quoted 3 lines
Quoted 1 line
The named> boolean field, however, wants to be of type bool.
Which fields ?
There's only one named 1-bit field: anythread_depreciation.
Quoted 7 lines
Quoted 1 line
And then the unnamed 1-bit> field really wants to be 2 bits, for
anythread_depreciationto be bit 15Quoted 2 lines
(etc).
Ah yes thanks, I got confused with the fields size for a second. I also found that
slots_per_cycis 3 bits instead of 4.
Not as far as I can see.
Quoted 5 lines
I think this diff fixes it overall.
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 172
Quoted 9 lines
xen/include/xen/lib/x86/cpu-policy.h +3
-3@@ -167,9 +167,9 @@ struct cpu_policyevent_enum_length ; uint32_t /* b */:32;uint32_t /* c */ fixed_ctr_mask; - uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, - anythread_depreciation:1, slots_per_cyc:4, - :13; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :2, + anythread_depreciation:1, slots_per_cyc:3, + :11;
Why 11 all of the sudden?
Jan
Re: [PATCH 3/6] x86: Define some Intel vPMU leafs
Le 25/03/2026 à 12:43, Jan Beulich a écrit :
Quoted 23 lines · Teddy Astie
Quoted 22 lines
Le 24/03/2026 à 10:25, Jan Beulich a écrit :
Quoted 20 lines · Teddy Astie
Quoted 6 lines
xen/include/xen/lib/x86/cpu-policy.h +0
-0@@ -162,7 +162,15 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0x9 - DCA *//* Leaf 0xa - Intel PMU. */ xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 173
Quoted 10 lines
xen/include/xen/lib/x86/cpu-policy.h +9
-1-uint8_t pmu_version, _pmu[15 ]; +struct { +uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width, + event_enum_length; + uint32_t /* b */:32; +uint32_t /* c */ fixed_ctr_mask; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, + anythread_depreciation:1, slots_per_cyc:4, + :13 ; + } pmu;Style-wise this looks to follow e.g. the cache leaf, so perhaps okay, even if I would have preferred you to follow what we did for leaf 6.
Quoted 6 lines
Quoted 3 lines
My idea was to put all that as .pmu.*, so I wouldn't need to prefix everything with "
pmu_". I'm not sure if you're talking about a different approach.The "pmu" is fine. I'm talking of what's inside the struct {}.
Is it regarding having union and _aa, _ab, (...) fields or prefixing fields with pmu_ ?
Quoted 18 lines
Quoted 3 lines
Quoted 1 line
The named> boolean field, however, wants to be of type bool.
Which fields ?
There's only one named 1-bit field:
anythread_depreciation.Quoted 7 lines
Quoted 1 line
And then the unnamed 1-bit> field really wants to be 2 bits, for
anythread_depreciationto be bit 15Quoted 2 lines
(etc).
Ah yes thanks, I got confused with the fields size for a second. I also found that
slots_per_cycis 3 bits instead of 4.Not as far as I can see.
Quoted 2 lines
I think this diff fixes it overall.
Quoted 15 lines
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 172
Quoted 12 lines
xen/include/xen/lib/x86/cpu-policy.h +3
-3@@ -167,9 +167,9 @@ struct cpu_policyevent_enum_length ; uint32_t /* b */:32;uint32_t /* c */ fixed_ctr_mask; - uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, - anythread_depreciation:1, slots_per_cyc:4, - :13; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :2, + anythread_depreciation:1, slots_per_cyc:3, + :11;Why 11 all of the sudden?
Okay, I think I finally figured out field sizes (5-bit field confuses me).
struct {
uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width,
event_enum_length;
uint32_t /* b */:32;
uint32_t /* c */ fixed_ctr_mask;
uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :2;
bool anythread_depreciation:1;
uint32_t slots_per_cyc:4, :12;
} pmu;Quoted 2 lines
Jan
Teddy
--
Teddy Astie | Vates XCP-ng Developer
XCP-ng & Xen Orchestra - Vates solutions
web: https://vates.tech
Re: [PATCH 3/6] x86: Define some Intel vPMU leafs
Quoted 28 lines · Teddy Astie
Le 25/03/2026 à 12:43, Jan Beulich a écrit :
Quoted 27 lines · Teddy Astie
Quoted 25 lines
Le 24/03/2026 à 10:25, Jan Beulich a écrit :
Quoted 6 lines · Teddy Astie
Quoted 5 lines
xen/include/xen/lib/x86/cpu-policy.h +0
-0@@ -162,7 +162,15 @@ struct cpu_policy uint64_t :64, :64; /* Leaf 0x9 - DCA */Quoted 14 lines
xen/include/xen/lib/x86/cpu-policy.h struct cpu_policy · line 173
Quoted 11 lines
xen/include/xen/lib/x86/cpu-policy.h +9
-1/* Leaf 0xa - Intel PMU. */ -uint8_t pmu_version, _pmu[15 ]; +struct { +uint8_t /* a */ version, num_gp_ctrs, gp_ctr_width, + event_enum_length; + uint32_t /* b */:32; +uint32_t /* c */ fixed_ctr_mask; + uint32_t /* d */ num_fixed_ctr:5, fixed_ctr_width:8, :1, + anythread_depreciation:1, slots_per_cyc:4, + :13 ; + } pmu;Style-wise this looks to follow e.g. the cache leaf, so perhaps okay, even if I would have preferred you to follow what we did for leaf 6.
My idea was to put all that as .pmu.*, so I wouldn't need to prefix everything with "
pmu_". I'm not sure if you're talking about a different approach.
Quoted 5 lines
Quoted 2 lines
The "pmu" is fine. I'm talking of what's inside the struct {}.
Is it regarding having union and
_aa,_ab, (...) fields or prefixing fields withpmu_?
As said, the "pmu" name of the containing struct is fine. Obviously then there is no need for pmu_ prefixes. The differences between the cache leaf and leaf 6 go beyond the union aspect though, and I really mean all differences there. As Andrew wasn't overly happy with the _6a and _6c union members, I wouldn't insist on the introduction of counterparts here (and if they were omitted, unions wouldn't be needed either). I think it's advisable though, allowing to omit the trailing unnamed bitfield (the size of which you've now corrected a 2nd time).
Jan