Skip to content

Commit b517006

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: X86: Provide a capability to disable cstate msr read intercepts
Allow guest reads CORE cstate when exposing host CPU power management capabilities to the guest. PKG cstate is restricted to avoid a guest to get the whole package information in multi-tenant scenario. Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Radim Krčmář <rkrcmar@redhat.com> Cc: Sean Christopherson <sean.j.christopherson@intel.com> Cc: Liran Alon <liran.alon@oracle.com> Signed-off-by: Wanpeng Li <wanpengli@tencent.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 8ffdaa7 commit b517006

7 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,6 +4894,7 @@ Valid bits in args[0] are
48944894
#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)
48954895
#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)
48964896
#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)
4897+
#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3)
48974898

48984899
Enabling this capability on a VM provides userspace with a way to no
48994900
longer intercept some instructions for improved latency in some

‎arch/x86/include/asm/kvm_host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ struct kvm_arch {
882882
bool mwait_in_guest;
883883
bool hlt_in_guest;
884884
bool pause_in_guest;
885+
bool cstate_in_guest;
885886

886887
unsigned long irq_sources_bitmap;
887888
s64 kvmclock_offset;

‎arch/x86/kvm/vmx/vmx.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6637,6 +6637,12 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
66376637
vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
66386638
vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
66396639
vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
6640+
if (kvm_cstate_in_guest(kvm)) {
6641+
vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
6642+
vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6643+
vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6644+
vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
6645+
}
66406646
vmx->msr_bitmap_mode = 0;
66416647

66426648
vmx->loaded_vmcs = &vmx->vmcs01;

‎arch/x86/kvm/x86.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3098,7 +3098,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
30983098
r = KVM_CLOCK_TSC_STABLE;
30993099
break;
31003100
case KVM_CAP_X86_DISABLE_EXITS:
3101-
r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE;
3101+
r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3102+
KVM_X86_DISABLE_EXITS_CSTATE;
31023103
if(kvm_can_mwait_in_guest())
31033104
r |= KVM_X86_DISABLE_EXITS_MWAIT;
31043105
break;
@@ -4615,6 +4616,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
46154616
kvm->arch.hlt_in_guest = true;
46164617
if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
46174618
kvm->arch.pause_in_guest = true;
4619+
if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4620+
kvm->arch.cstate_in_guest = true;
46184621
r = 0;
46194622
break;
46204623
case KVM_CAP_MSR_PLATFORM_INFO:

‎arch/x86/kvm/x86.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ static inline bool kvm_pause_in_guest(struct kvm *kvm)
333333
return kvm->arch.pause_in_guest;
334334
}
335335

336+
static inline bool kvm_cstate_in_guest(struct kvm *kvm)
337+
{
338+
return kvm->arch.cstate_in_guest;
339+
}
340+
336341
DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu);
337342

338343
static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu)

‎include/uapi/linux/kvm.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,11 @@ struct kvm_ioeventfd {
696696
#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)
697697
#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)
698698
#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)
699+
#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3)
699700
#define KVM_X86_DISABLE_VALID_EXITS (KVM_X86_DISABLE_EXITS_MWAIT | \
700701
KVM_X86_DISABLE_EXITS_HLT | \
701-
KVM_X86_DISABLE_EXITS_PAUSE)
702+
KVM_X86_DISABLE_EXITS_PAUSE | \
703+
KVM_X86_DISABLE_EXITS_CSTATE)
702704

703705
/* for KVM_ENABLE_CAP */
704706
struct kvm_enable_cap {

‎tools/include/uapi/linux/kvm.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,11 @@ struct kvm_ioeventfd {
696696
#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)
697697
#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)
698698
#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)
699+
#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3)
699700
#define KVM_X86_DISABLE_VALID_EXITS (KVM_X86_DISABLE_EXITS_MWAIT | \
700701
KVM_X86_DISABLE_EXITS_HLT | \
701-
KVM_X86_DISABLE_EXITS_PAUSE)
702+
KVM_X86_DISABLE_EXITS_PAUSE | \
703+
KVM_X86_DISABLE_EXITS_CSTATE)
702704

703705
/* for KVM_ENABLE_CAP */
704706
struct kvm_enable_cap {

0 commit comments

Comments
 (0)