KVM: emulate: #GP when emulating rdpmc if CR0.PE is 1
PATCH 1/3 KVM: emulate: #GP when emulating rdpmc if CR0.PE is 1
From: Wanpeng Li <[email protected]>
SDM mentioned that, RDPMC:
IF (((CR4.PCE = 1) or (CPL = 0) or (CR0.PE = 0)) and (ECX indicates a supported counter))
THEN
EAX := counter[31:0];
EDX := ZeroExtend(counter[MSCB:32]);
ELSE (* ECX is not valid or CR4.PCE is 0 and CPL is 1, 2, or 3 and CR0.PE is 1 *)
#GP(0);
FI;Let's add the CR0.PE is 1 checking to rdpmc emulate.
--- arch/x86/kvm/emulate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +2 -1
@@ -4213,6 +4213,7 @@ static int check_rdtsc(struct x86_emulate_ctxt *ctxt)static int check_rdpmc (struct x86_emulate_ctxt *ctxt) {u64 cr4 = ctxt->ops->get_cr (ctxt,4 ); +u64 cr0 = ctxt->ops->get_cr (ctxt,0 );u64 rcx =reg_read (ctxt,VCPU_REGS_RCX );/*
@@ -4222,7 +4223,7 @@ static int check_rdpmc(struct x86_emulate_ctxt *ctxt)if (enable_vmware_backdoor &&is_vmware_backdoor_pmc (rcx))return X86EMUL_CONTINUE ; -if ((!(cr4 &X86_CR4_PCE ) && ctxt->ops->cpl (ctxt)) || +if ((!(cr4 &X86_CR4_PCE ) && ctxt->ops->cpl (ctxt) && (cr0 &X86_CR0_PE )) || ctxt->ops->check_pmc (ctxt, rcx))return emulate_gp (ctxt,0 );
Signature · 1 line
2.25.1
PATCH 2/3 KVM: vPMU: Fill get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL w/ 0
From: Wanpeng Li <[email protected]>
SDM section 18.2.3 mentioned that:
"IA32_PERF_GLOBAL_OVF_CTL MSR allows software to clear overflow indicator(s) of
any general-purpose or fixed-function counters via a single WRMSR."
It is R/W mentioned by SDM, we read this msr on bare-metal during perf testing, the value is always 0 for CLX/SKX boxes on hands. Let's fill get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL w/ 0 as hardware behavior.
---
Btw, xen also fills get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL 0.
arch/x86/kvm/vmx/pmu_intel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -365,7 +365,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = pmu->global_ctrl;return 0 ;case MSR_CORE_PERF_GLOBAL_OVF_CTRL : - msr_info->data = pmu->global_ovf_ctrl; + msr_info->data =0 ;return 0 ;default : if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
Signature · 1 line
2.25.1
Re: [PATCH 2/3] KVM: vPMU: Fill get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL w/ 0
cc Andi,
Quoted 23 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
SDM section 18.2.3 mentioned that:
"
IA32_PERF_GLOBAL_OVF_CTLMSR allows software to clear overflow indicator(s) of
any general-purpose or fixed-function counters via a single WRMSR."It is R/W mentioned by SDM, we read this msr on bare-metal during perf testing, the value is always 0 for CLX/SKX boxes on hands. Let's fill
get_msrMSR_CORE_PERF_GLOBAL_OVF_CTRLw/ 0 as hardware behavior.Signed-off-by Wanpeng Li <[email protected]>---
Btw, xen also fillsget_msrMSR_CORE_PERF_GLOBAL_OVF_CTRL0.arch/x86/kvm/vmx/pmu_intel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/vmx/pmu_intel.c +0
-0
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 368
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +1
-1@@ -365,7 +365,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = pmu->global_ctrl;return 0 ;case MSR_CORE_PERF_GLOBAL_OVF_CTRL : - msr_info->data = pmu->global_ovf_ctrl; + msr_info->data =0 ;
Further, better to drop 'u64 global_ovf_ctrl' directly.
Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +0
-0return 0 ;default : if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
Re: [PATCH 2/3] KVM: vPMU: Fill get_msr MSR_CORE_PERF_GLOBAL_OVF_CTRL w/ 0
Quoted 22 lines · Like Xu
cc Andi,
Quoted 18 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
SDM section 18.2.3 mentioned that:
"
IA32_PERF_GLOBAL_OVF_CTLMSR allows software to clear overflow indicator(s) of
any general-purpose or fixed-function counters via a single WRMSR."It is R/W mentioned by SDM, we read this msr on bare-metal during perf testing, the value is always 0 for CLX/SKX boxes on hands. Let's fill
get_msrMSR_CORE_PERF_GLOBAL_OVF_CTRLw/ 0 as hardware behavior.Signed-off-by Wanpeng Li <[email protected]>---
Btw, xen also fillsget_msrMSR_CORE_PERF_GLOBAL_OVF_CTRL0.arch/x86/kvm/vmx/pmu_intel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 368
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +1
-1@@ -365,7 +365,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = pmu->global_ctrl;return 0 ;case MSR_CORE_PERF_GLOBAL_OVF_CTRL : - msr_info->data = pmu->global_ovf_ctrl; + msr_info->data =0 ;Tested-by Like Xu <[email protected]>
Thanks.
Quoted 1 line
Further, better to drop 'u64
global_ovf_ctrl' directly.
Good suggestion. :)
Wanpeng
PATCH 3/3 KVM: LAPIC: Optimize PMI delivering overhead
In reply to Wanpeng LiFrom: Wanpeng Li <[email protected]>
The overhead of kvm_vcpu_kick() is huge since expensive rcu/memory barrier etc operations in rcuwait_wake_up(). It is worse when local delivery since the vCPU is scheduled and we still suffer from this. We can observe 12us+ for kvm_vcpu_kick() in kvm_pmu_deliver_pmi() path by ftrace before the patch and 6us+ after the optimization.
--- arch/x86/kvm/lapic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +2 -1
@@ -1120,7 +1120,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,case APIC_DM_NMI : result =1 ;kvm_inject_nmi (vcpu); -kvm_vcpu_kick (vcpu); +if (vcpu !=kvm_get_running_vcpu ()) +kvm_vcpu_kick (vcpu);break ;case APIC_DM_INIT :
Signature · 1 line
2.25.1
Re: [PATCH 3/3] KVM: LAPIC: Optimize PMI delivering overhead
Quoted 18 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
The overhead of
kvm_vcpu_kick()is huge since expensive rcu/memory barrier etc operations inrcuwait_wake_up(). It is worse when local delivery since the vCPU is scheduled and we still suffer from this. We can observe 12us+ forkvm_vcpu_kick()inkvm_pmu_deliver_pmi()path by ftrace before the patch and 6us+ after the optimization.Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/lapic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/lapic.c +0
-0
arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 1124
Quoted 6 lines
arch/x86/kvm/lapic.c +2
-1@@ -1120,7 +1120,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,case APIC_DM_NMI : result =1 ;kvm_inject_nmi (vcpu); -kvm_vcpu_kick (vcpu); +if (vcpu !=kvm_get_running_vcpu ()) +kvm_vcpu_kick (vcpu);
Out of curiosity,
can this be converted into a generic optimization for kvm_vcpu_kick() instead? I.e. if kvm_vcpu_kick() is called for the currently running vCPU, there's almost nothing to do, especially when we already have a request pending, right? (I didn't put too much though to it)
Quoted 3 lines
arch/x86/kvm/lapic.c +0
-0break ;case APIC_DM_INIT :
Signature · 1 line
Vitaly
Re: [PATCH 3/3] KVM: LAPIC: Optimize PMI delivering overhead
Quoted 30 lines · Vitaly Kuznetsov
Quoted 18 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
The overhead of
kvm_vcpu_kick()is huge since expensive rcu/memory barrier etc operations inrcuwait_wake_up(). It is worse when local delivery since the vCPU is scheduled and we still suffer from this. We can observe 12us+ forkvm_vcpu_kick()inkvm_pmu_deliver_pmi()path by ftrace before the patch and 6us+ after the optimization.Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/lapic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/lapic.c +0
-0arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 1124
Quoted 6 lines
arch/x86/kvm/lapic.c +2
-1@@ -1120,7 +1120,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,case APIC_DM_NMI : result =1 ;kvm_inject_nmi (vcpu); -kvm_vcpu_kick (vcpu); +if (vcpu !=kvm_get_running_vcpu ()) +kvm_vcpu_kick (vcpu);Out of curiosity,
Quoted 4 lines
can this be converted into a generic optimization for
kvm_vcpu_kick()instead? I.e. ifkvm_vcpu_kick()is called for the currently running vCPU, there's almost nothing to do, especially when we already have a request pending, right? (I didn't put too much though to it)
I thought about it before, I will do it in the next version since you also vote for it. :)
Wanpeng
Sean Christopherson
maintainerRe: [PATCH 3/3] KVM: LAPIC: Optimize PMI delivering overhead
Quoted 8 lines · Wanpeng Li
Quoted 7 lines · Vitaly Kuznetsov
Quoted 4 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
The overhead of
kvm_vcpu_kick()is huge since expensive rcu/memory barrier etc operations inrcuwait_wake_up(). It is worse when local
Memory barriers on x86 are just compiler barriers. The only meaningful overhead is the locked transaction in rcu_read_lock() => preempt_disable(). I suspect the performance benefit from this patch comes either comes from avoiding a second lock when disabling preemption again for get_cpu(), or by avoiding the cmpxchg() in kvm_vcpu_exiting_guest_mode().
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
delivery since the vCPU is scheduled and we still suffer from this. We can observe 12us+ for
kvm_vcpu_kick()inkvm_pmu_deliver_pmi()path by ftrace before the patch and 6us+ after the optimization.
Those numbers seem off, I wouldn't expect a few locks to take 6us.
Quoted 19 lines
Quoted 19 lines
Quoted 10 lines
Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/lapic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/lapic.c +0
-0arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 1124
Quoted 6 lines
arch/x86/kvm/lapic.c +2
-1@@ -1120,7 +1120,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,case APIC_DM_NMI : result =1 ;kvm_inject_nmi (vcpu); -kvm_vcpu_kick (vcpu); +if (vcpu !=kvm_get_running_vcpu ()) +kvm_vcpu_kick (vcpu);Out of curiosity,
Quoted 7 lines
Quoted 4 lines
can this be converted into a generic optimization for
kvm_vcpu_kick()instead? I.e. ifkvm_vcpu_kick()is called for the currently running vCPU, there's almost nothing to do, especially when we already have a request pending, right? (I didn't put too much though to it)I thought about it before, I will do it in the next version since you also vote for it. :)
Adding a kvm_get_running_vcpu() check before kvm_vcpu_wake_up() in kvm_vcpu_kick() is not functionally correct as it's possible to reach kvm_cpu_kick() from (soft) IRQ context, e.g. hrtimer => apic_timer_expired() and pi_wakeup_handler(). If the kick occurs after prepare_to_rcuwait() and the final kvm_vcpu_check_block(), but before the vCPU is scheduled out, then the kvm_vcpu_wake_up() is required to wake the vCPU, even if it is the current running vCPU.
The extra check might also degrade performance for many cases since the full kick path would need to disable preemption three times, though if the overhead is from x86's cmpxchg() then it's a moot point.
I think we'd want something like this to avoid extra preempt_disable() as well as the cmpxchg() when @vcpu is the running vCPU.
virt/kvm/kvm_main.c +10 -3
@@ -3349,8 +3349,15 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu) {int me, cpu; -if (kvm_vcpu_wake_up (vcpu)) -return ; + me =get_cpu (); + +if (rcuwait_active (&vcpu->wait) &&kvm_vcpu_wake_up (vcpu)) + goto out; + +if (vcpu ==__this_cpu_read (kvm_running_vcpu)) { +WARN_ON_ONCE (vcpu->mode ==IN_GUEST_MODE ); + goto out; + }/* * Note, the vCPU could get migrated to a different pCPU at any point
@@ -3359,12 +3366,12 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)* IPI is to force the vCPU to leave IN_GUEST_MODE, and migrating the * vCPU also requires it to leave IN_GUEST_MODE. */ - me =get_cpu ();if (kvm_arch_vcpu_should_kick (vcpu)) { cpu =READ_ONCE (vcpu->cpu);if (cpu != me && (unsigned )cpu < nr_cpu_ids &&cpu_online (cpu))smp_send_reschedule (cpu); } +out:put_cpu (); }EXPORT_SYMBOL_GPL (kvm_vcpu_kick);
Re: [PATCH 3/3] KVM: LAPIC: Optimize PMI delivering overhead
Quoted 17 lines · Sean Christopherson
Quoted 8 lines · Wanpeng Li
Quoted 7 lines · Vitaly Kuznetsov
Quoted 4 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
The overhead of
kvm_vcpu_kick()is huge since expensive rcu/memory barrier etc operations inrcuwait_wake_up(). It is worse when localMemory barriers on x86 are just compiler barriers. The only meaningful overhead is the locked transaction in
rcu_read_lock()=>preempt_disable(). I suspect the performance benefit from this patch comes either comes from avoiding a second lock when disabling preemption again forget_cpu(), or by avoiding thecmpxchg()inkvm_vcpu_exiting_guest_mode().
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
delivery since the vCPU is scheduled and we still suffer from this. We can observe 12us+ for
kvm_vcpu_kick()inkvm_pmu_deliver_pmi()path by ftrace before the patch and 6us+ after the optimization.Those numbers seem off, I wouldn't expect a few locks to take 6us.
Maybe the ftrace introduces more overhead.
Quoted 28 lines
Quoted 20 lines
Quoted 20 lines
Quoted 11 lines
Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/lapic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/lapic.c +0
-0arch/x86/kvm/lapic.c static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, · line 1124
Quoted 6 lines
arch/x86/kvm/lapic.c +2
-1@@ -1120,7 +1120,8 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,case APIC_DM_NMI : result =1 ;kvm_inject_nmi (vcpu); -kvm_vcpu_kick (vcpu); +if (vcpu !=kvm_get_running_vcpu ()) +kvm_vcpu_kick (vcpu);Out of curiosity,
Quoted 7 lines
Quoted 4 lines
can this be converted into a generic optimization for
kvm_vcpu_kick()instead? I.e. ifkvm_vcpu_kick()is called for the currently running vCPU, there's almost nothing to do, especially when we already have a request pending, right? (I didn't put too much though to it)I thought about it before, I will do it in the next version since you also vote for it. :)
Quoted 6 lines
Adding a
kvm_get_running_vcpu()check beforekvm_vcpu_wake_up()inkvm_vcpu_kick()is not functionally correct as it's possible to reachkvm_cpu_kick()from (soft) IRQ context, e.g. hrtimer =>apic_timer_expired()andpi_wakeup_handler(). If the kick occurs afterprepare_to_rcuwait()and the finalkvm_vcpu_check_block(), but before the vCPU is scheduled out, then thekvm_vcpu_wake_up()is required to wake the vCPU, even if it is the current running vCPU.
Good point.
Quoted 7 lines
The extra check might also degrade performance for many cases since the full kick path would need to disable preemption three times, though if the overhead is from x86's
cmpxchg()then it's a moot point.I think we'd want something like this to avoid extra
preempt_disable()as well as thecmpxchg()when @vcpu is the running vCPU.
Do it in v2, thanks for the suggestion.
Wanpeng
Sean Christopherson
maintainerThe shortlog makes it sound like "inject a #GP if CR0.PE=1", i.e. unconditionally inject #GP for RDMPC in protected mode. Maybe "Don't inject #GP when emulating RDMPC if CR0.PE=0"?
Quoted 32 lines · Wanpeng Li
From: Wanpeng Li <[email protected]>
SDM mentioned that, RDPMC:
IF (((CR4.PCE = 1) or (CPL = 0) or (CR0.PE = 0)) and (ECX indicates a supported counter)) THEN EAX := counter[31:0]; EDX := ZeroExtend(counter[MSCB:32]); ELSE (* ECX is not valid or CR4.PCE is 0 and CPL is 1, 2, or 3 and CR0.PE is 1 *) #GP(0); FI;Let's add the CR0.PE is 1 checking to rdpmc emulate.
Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/emulate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/emulate.c +1
-0@@ -4213,6 +4213,7 @@ static int check_rdtsc(struct x86_emulate_ctxt *ctxt)static int check_rdpmc (struct x86_emulate_ctxt *ctxt) {u64 cr4 = ctxt->ops->get_cr (ctxt,4 ); +u64 cr0 = ctxt->ops->get_cr (ctxt,0 );u64 rcx =reg_read (ctxt,VCPU_REGS_RCX );/*
arch/x86/kvm/emulate.c static int check_rdpmc(struct x86_emulate_ctxt *ctxt) · line 4226
Quoted 5 lines
arch/x86/kvm/emulate.c +1
-1@@ -4222,7 +4223,7 @@ static int check_rdpmc(struct x86_emulate_ctxt *ctxt)if (enable_vmware_backdoor &&is_vmware_backdoor_pmc (rcx))return X86EMUL_CONTINUE ; - if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || + if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt) && (cr0 & X86_CR0_PE)) ||
I don't think it's possible for CPL to be >0 if CR0.PE=0, e.g. we could probably WARN in the #GP path. Realistically it doesn't add value though, so maybe just add a blurb in the changelog saying this isn't strictly necessary?
Quoted 6 lines
arch/x86/kvm/emulate.c +0
-0ctxt->ops->check_pmc(ctxt, rcx))return emulate_gp (ctxt,0 );Signature · 1 line
2.25.1
Quoted 5 lines · Sean Christopherson
The shortlog makes it sound like "inject a #GP if CR0.PE=1", i.e. unconditionally inject #GP for RDMPC in protected mode. Maybe "Don't inject #GP when emulating RDMPC if CR0.PE=0"?
Agreed.
Quoted 36 lines · Wanpeng Li
Quoted 35 lines
From: Wanpeng Li <[email protected]>
SDM mentioned that, RDPMC:
IF (((CR4.PCE = 1) or (CPL = 0) or (CR0.PE = 0)) and (ECX indicates a supported counter)) THEN EAX := counter[31:0]; EDX := ZeroExtend(counter[MSCB:32]); ELSE (* ECX is not valid or CR4.PCE is 0 and CPL is 1, 2, or 3 and CR0.PE is 1 *) #GP(0); FI;Let's add the CR0.PE is 1 checking to rdpmc emulate.
Signed-off-by Wanpeng Li <[email protected]>--- arch/x86/kvm/emulate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/kvm/emulate.c +1
-0@@ -4213,6 +4213,7 @@ static int check_rdtsc(struct x86_emulate_ctxt *ctxt)static int check_rdpmc (struct x86_emulate_ctxt *ctxt) {u64 cr4 = ctxt->ops->get_cr (ctxt,4 ); +u64 cr0 = ctxt->ops->get_cr (ctxt,0 );u64 rcx =reg_read (ctxt,VCPU_REGS_RCX );/* @@ -4222,7 +4223,7 @@ static int check_rdpmc(struct x86_emulate_ctxt *ctxt)if (enable_vmware_backdoor &&is_vmware_backdoor_pmc (rcx))return X86EMUL_CONTINUE ;
Quoted 6 lines
arch/x86/kvm/emulate.c static int check_rdpmc(struct x86_emulate_ctxt *ctxt) · line 4226
Quoted 2 lines
arch/x86/kvm/emulate.c +1
-1- if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt)) || + if ((!(cr4 & X86_CR4_PCE) && ctxt->ops->cpl(ctxt) && (cr0 & X86_CR0_PE)) ||I don't think it's possible for CPL to be >0 if CR0.PE=0, e.g. we could probably WARN in the #GP path. Realistically it doesn't add value though, so maybe just add a blurb in the changelog saying this isn't strictly necessary?
Do it in v2.
Wanpeng