Skip to contentSkip to conversation

kvm: x86: emulate MSR_PLATFORM_INFO msr bits

PATCH In review Hao Xiang 3 participants 4 messages Aug 21, 2023 – Aug 23, 2023

Subsystems (3)
lore
Share & tools
lore

Hao Xiang

author
#0 lore raw

PATCH 1/1 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)

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/x86.c               | 25 ++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +4 -0
index 1d11135..1c8a276 100644
@@ -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)
arch/x86/kvm/x86.c +24 -1
index c381770..621c3e1 100644
@@ -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);
Signature · 1 line
1.8.3.1
#1 lore raw
kernel test robot noticed the following build errors:

Hi Hao,

kernel test robot noticed the following build errors:

[auto build test ERROR on kvm/queue]
[also build test ERROR on mst-vhost/linux-next tip/x86/core linus/master v6.5-rc7 next-20230822] [cannot apply to kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Hao-Xiang/kvm-x86-emulate-MSR_PLATFORM_INFO-msr-bits/20230821-125755
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link:    https://lore.kernel.org/r/1692588151-33716-1-git-send-email-hao.xiang%40linux.alibaba.com
patch subject: [PATCH] kvm: x86: emulate MSR_PLATFORM_INFO msr bits
config: i386-randconfig-003-20230822 (https://download.01.org/0day-ci/archive/20230823/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230823/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

arch/x86/kvm/x86.c: In function 'kvm_get_msr_platform_info':

>> arch/x86/kvm/x86.c:1695:31: error: 'MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO' undeclared (first use in this function); did you mean 'MSR_PLATFORM_INFO_MAX_NON_TURBO_LIM_RATIO'?
    1695 |         msr_platform_info &= (MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO |
         |                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                               MSR_PLATFORM_INFO_MAX_NON_TURBO_LIM_RATIO

arch/x86/kvm/x86.c:1695:31: note: each undeclared identifier is reported only once for each function it appears in

vim +1695 arch/x86/kvm/x86.c

1678
1679
1680 static u64 kvm_get_msr_platform_info(void)
1681 {
1682 u64 msr_platform_info = 0;
1683
1684 rdmsrl_safe(MSR_PLATFORM_INFO, &msr_platform_info);
1685 /*
1686 * MSR_PLATFORM_INFO bits:
1687 * bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO)
1688 * bit 31, CPUID Faulting Enabled (CPUID_FAULTING_EN)
1689 * bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO)
1690 *
1691 * Emulate part msr bits, expose above msr info to guest,
1692 * to make sure guest can get correct turbo frequency info.
1693 */
1694

> 1695		msr_platform_info &= (MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO |

1696 MSR_PLATFORM_INFO_MAX_EFFICIENCY_RATIO);
1697 msr_platform_info |= MSR_PLATFORM_INFO_CPUID_FAULT;
1698
1699 return msr_platform_info;
1700 }
1701

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Hao Xiang
kernel test robot noticed the following build errors:

Hi Hao,

kernel test robot noticed the following build errors:

[auto build test ERROR on kvm/queue]
[also build test ERROR on mst-vhost/linux-next tip/x86/core linus/master v6.5-rc7 next-20230822] [cannot apply to kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Hao-Xiang/kvm-x86-emulate-MSR_PLATFORM_INFO-msr-bits/20230821-125755
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link:    https://lore.kernel.org/r/1692588151-33716-1-git-send-email-hao.xiang%40linux.alibaba.com
patch subject: [PATCH] kvm: x86: emulate MSR_PLATFORM_INFO msr bits
config: i386-buildonly-randconfig-003-20230823 (https://download.01.org/0day-ci/archive/20230823/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce: (https://download.01.org/0day-ci/archive/20230823/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> arch/x86/kvm/x86.c:1695:24: error: use of undeclared identifier 'MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO'

msr_platform_info &= (MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO |
^
1 error generated.

vim +/MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO +1695 arch/x86/kvm/x86.c

1678
1679
1680 static u64 kvm_get_msr_platform_info(void)
1681 {
1682 u64 msr_platform_info = 0;
1683
1684 rdmsrl_safe(MSR_PLATFORM_INFO, &msr_platform_info);
1685 /*
1686 * MSR_PLATFORM_INFO bits:
1687 * bit 15:8, Maximum Non-Turbo Ratio (MAX_NON_TURBO_LIM_RATIO)
1688 * bit 31, CPUID Faulting Enabled (CPUID_FAULTING_EN)
1689 * bit 47:40, Maximum Efficiency Ratio (MAX_EFFICIENCY_RATIO)
1690 *
1691 * Emulate part msr bits, expose above msr info to guest,
1692 * to make sure guest can get correct turbo frequency info.
1693 */
1694

> 1695		msr_platform_info &= (MSR_PLATFORM_INFO_MAX_NON_TURBO_RATIO |

1696 MSR_PLATFORM_INFO_MAX_EFFICIENCY_RATIO);
1697 msr_platform_info |= MSR_PLATFORM_INFO_CPUID_FAULT;
1698
1699 return msr_platform_info;
1700 }
1701

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Hao Xiang
Quoted 9 lines · Hao Xiang

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)

I'm curious. How are they related to turbo frequency info?

bits 15:8, tell the Non-turbo frequency and bits 47:40 tells the min frequency.

Quoted 11 lines
---
  arch/x86/include/asm/msr-index.h |  4 ++++
  arch/x86/kvm/x86.c               | 25 ++++++++++++++++++++++++-
  2 files changed, 28 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +0 -0
index 1d11135..1c8a276 100644

arch/x86/include/asm/msr-index.h line 74

Quoted 7 lines
arch/x86/include/asm/msr-index.h +4 -0
@@ -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

They are mask not the value, please name them with _MASK suffix.

Quoted 46 lines
arch/x86/include/asm/msr-index.h +0 -0
  #define MSR_IA32_UMWAIT_CONTROL			0xe1
  #define MSR_IA32_UMWAIT_CONTROL_C02_DISABLE	BIT(0)
arch/x86/kvm/x86.c +24 -1
index c381770..621c3e1 100644
@@ -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);