2018-02-09 05:08:33

by Chao Gao

[permalink] [raw]
Subject: [PATCH v2] x86/kvm/vmx: Don't halt vcpu when L1 is injecting events to L2

Although L2 is in halt state, it will be in the active state after
VM entry if the VM entry is vectoring according to SDM 26.6.2 Activity
State. Halting the vcpu here means the event won't be injected to L2
and this decision isn't reported to L1. Thus L0 drops an event that
should be injected to L2.

Cc: Liran Alon <[email protected]>
Signed-off-by: Chao Gao <[email protected]>
---
Changes in v2:
- Remove VID stuff. Only handle event injection in this patch.
---
arch/x86/kvm/vmx.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index bb5b488..42f39d9 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -10985,7 +10985,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
if (ret)
return ret;

- if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
+ /*
+ * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
+ * by event injection, halt vcpu for optimization.
+ */
+ if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
+ !(vmcs12->vm_entry_intr_info_field & VECTORING_INFO_VALID_MASK))
return kvm_vcpu_halt(vcpu);

vmx->nested.nested_run_pending = 1;
--
1.9.1



2018-02-09 12:45:07

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH v2] x86/kvm/vmx: Don't halt vcpu when L1 is injecting events to L2

On 09/02/2018 06:09, Chao Gao wrote:
> Although L2 is in halt state, it will be in the active state after
> VM entry if the VM entry is vectoring according to SDM 26.6.2 Activity
> State. Halting the vcpu here means the event won't be injected to L2
> and this decision isn't reported to L1. Thus L0 drops an event that
> should be injected to L2.

Can you please write a testcase for kvm-unit-tests?

Thanks,

Paolo

2018-02-10 00:32:38

by Liran Alon

[permalink] [raw]
Subject: Re: [PATCH v2] x86/kvm/vmx: Don't halt vcpu when L1 is injecting events to L2


----- [email protected] wrote:

> Although L2 is in halt state, it will be in the active state after
> VM entry if the VM entry is vectoring according to SDM 26.6.2
> Activity
> State. Halting the vcpu here means the event won't be injected to L2
> and this decision isn't reported to L1. Thus L0 drops an event that
> should be injected to L2.
>
> Cc: Liran Alon <[email protected]>
> Signed-off-by: Chao Gao <[email protected]>
> ---
> Changes in v2:
> - Remove VID stuff. Only handle event injection in this patch.
> ---
> arch/x86/kvm/vmx.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index bb5b488..42f39d9 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -10985,7 +10985,12 @@ static int nested_vmx_run(struct kvm_vcpu
> *vcpu, bool launch)
> if (ret)
> return ret;
>
> - if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
> + /*
> + * If we're entering a halted L2 vcpu and the L2 vcpu won't be
> woken
> + * by event injection, halt vcpu for optimization.

I would remove the "for optimization." from the comment.

> + */
> + if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
> + !(vmcs12->vm_entry_intr_info_field &
> VECTORING_INFO_VALID_MASK))

VECTORING_INFO_VALID_MASK is used in KVM code against vmcs12->idt_vectoring_info_field.
I think you should use INTR_INFO_VALID_MASK to be consistent with rest of code.

> return kvm_vcpu_halt(vcpu);
>
> vmx->nested.nested_run_pending = 1;
> --
> 1.9.1

In addition, commit title should be written in format of:
"KVM: VMX: Don't halt vcpu when L1 is injecting events to L2"
(Makes it easier to grep in git log).

Other than that, seems good to me. :)

Reviewed-by: Liran Alon <[email protected]>

Thanks,
-Liran