2023-08-21 10:19:57

by Hao Xiang

[permalink] [raw]
Subject: [PATCH] kvm: x86: emulate MSR_PLATFORM_INFO msr bits

For intel platform, The BzyMhz field of Turbostat shows zero
due to the missing of part msr bits of MSR_PLATFORM_INFO.

Acquire necessary msr bits, and expose following msr info to guest,
to make sure guest can get correct turbo frequency info.

MSR_PLATFORM_INFO bits
bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO)
bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO)

Signed-off-by: Hao Xiang <[email protected]>
---
arch/x86/include/asm/msr-index.h | 4 ++++
arch/x86/kvm/x86.c | 25 ++++++++++++++++++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 1d11135..1c8a276 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -68,6 +68,10 @@
#define MSR_PLATFORM_INFO 0x000000ce
#define MSR_PLATFORM_INFO_CPUID_FAULT_BIT 31
#define MSR_PLATFORM_INFO_CPUID_FAULT BIT_ULL(MSR_PLATFORM_INFO_CPUID_FAULT_BIT)
+/* MSR_PLATFORM_INFO bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO) */
+#define MSR_PLATFORM_INFO_MAX_NON_TURBO_LIM_RATIO 0x00000000ff00
+/* MSR_PLATFORM_INFO bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO) */
+#define MSR_PLATFORM_INFO_MAX_EFFICIENCY_RATIO 0xff0000000000

#define MSR_IA32_UMWAIT_CONTROL 0xe1
#define MSR_IA32_UMWAIT_CONTROL_C02_DISABLE BIT(0)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c381770..621c3e1 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1679,6 +1679,29 @@ static u64 kvm_get_arch_capabilities(void)
return data;
}

+
+static u64 kvm_get_msr_platform_info(void)
+{
+ u64 msr_platform_info = 0;
+
+ rdmsrl_safe(MSR_PLATFORM_INFO, &msr_platform_info);
+ /*
+ * MSR_PLATFORM_INFO bits:
+ * bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO)
+ * bit 31, CPUID Faulting Enabled (CPUID_FAULTING_EN)
+ * bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO)
+ *
+ * Emulate part msr bits, expose above msr info to guest,
+ * to make sure guest can get correct turbo frequency info.
+ */
+
+ msr_platform_info &= (MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO |
+ MSR_PLATFORM_INFO_MAX_EFFICIENCY_RATIO);
+ msr_platform_info |= MSR_PLATFORM_INFO_CPUID_FAULT;
+
+ return msr_platform_info;
+}
+
static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
{
switch (msr->index) {
@@ -11919,7 +11942,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
goto free_guest_fpu;

vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
- vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
+ vcpu->arch.msr_platform_info = kvm_get_msr_platform_info();
kvm_xen_init_vcpu(vcpu);
kvm_vcpu_mtrr_init(vcpu);
vcpu_load(vcpu);
--
1.8.3.1



2023-08-21 10:46:43

by Hao Xiang

[permalink] [raw]
Subject: Re: [PATCH] kvm: x86: emulate MSR_PLATFORM_INFO msr bits

For reason that,

The turbo frequency info depends on specific machine type. And the msr
value of MSR_PLATFORM_INFO may be diferent on diffrent generation machine.

Get following msr bits (needed by turbostat on intel platform) by rdmsr
MSR_PLATFORM_INFO directly in KVM is more reasonable. And set these msr
bits as vcpu->arch.msr_platform_info default value.
-bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO)
-bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO)

On 2023/8/21 15:52, Chao Gao wrote:
> On Mon, Aug 21, 2023 at 11:26:32AM +0800, Hao Xiang wrote:
>> For intel platform, The BzyMhz field of Turbostat shows zero
>> due to the missing of part msr bits of MSR_PLATFORM_INFO.
>>
>> Acquire necessary msr bits, and expose following msr info to guest,
>> to make sure guest can get correct turbo frequency info.
>
> Userspace VMM (e.g., QEMU) can configure this MSR for guests. Please refer to
> tools/testing/selftests/kvm/x86_64/platform_info_test.c.
>
> The question is why KVM needs this patch given KVM already provides interfaces
> for QEMU to configure the MSR.

2023-08-21 13:13:45

by Hao Xiang

[permalink] [raw]
Subject: Re: [PATCH] kvm: x86: emulate MSR_PLATFORM_INFO msr bits



On 2023/8/21 18:44, Chao Gao wrote:
> On Mon, Aug 21, 2023 at 05:11:16PM +0800, Hao Xiang wrote:
>> For reason that,
>>
>> The turbo frequency info depends on specific machine type. And the msr value
>> of MSR_PLATFORM_INFO may be diferent on diffrent generation machine.
>>
>> Get following msr bits (needed by turbostat on intel platform) by rdmsr
>> MSR_PLATFORM_INFO directly in KVM is more reasonable. And set these msr bits
>> as vcpu->arch.msr_platform_info default value.
>> -bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO)
>> -bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO)
>
> I don't get why QEMU cannot do this with the existing interface, e.g.,
> KVM_SET_MSRS.
>
> will the MSR value be migrated during VM migration?
>
> looks we are in a dilemma. on one side, if the value is migrated, the value can
> become inconsisntent with hardware value. On the other side, changing the ratio
> bits at runtime isn't the architectural behavior.
>
> And the MSR is per-socket. In theory, a system can have two sockets with
> different values of the MSR. what if a vCPU is created on a socket and then
> later runs on the other socket?
>

Set these msr bits (needed by turbostat on intel platform) in KVM by
default.
Of cource, QEMU can also set MSR value by need. It does not conflict.

>>
>> On 2023/8/21 15:52, Chao Gao wrote:
>>> On Mon, Aug 21, 2023 at 11:26:32AM +0800, Hao Xiang wrote:
>>>> For intel platform, The BzyMhz field of Turbostat shows zero
>>>> due to the missing of part msr bits of MSR_PLATFORM_INFO.
>>>>
>>>> Acquire necessary msr bits, and expose following msr info to guest,
>>>> to make sure guest can get correct turbo frequency info.
>>>
>>> Userspace VMM (e.g., QEMU) can configure this MSR for guests. Please refer to
>>> tools/testing/selftests/kvm/x86_64/platform_info_test.c.
>>>
>>> The question is why KVM needs this patch given KVM already provides interfaces
>>> for QEMU to configure the MSR.