Replace vmx->nested.pi_pending,pi_active and posted_intr_nv with KVM_NESTED_PI_VECTOR_MASK,KVM_NESTED_PI_ACTIVE,KVM_NESTED_PI_PENDING flags of nested_guest_state. Since the nested state is cleared as a whole to 0, on each nested VM exit, explicit clearing of vmx->nested.posted_intr_nv and vmx->nested.pi_pending was dropped. Also, since nested entry sets nested_guest_state to KVM_NESTED_IN_GUEST, the publishing of KVM_NESTED_PI_VECTOR_MASK,KVM_NESTED_PI_ACTIVE is delayed to happen after enter_guest_mode(). Suggested-by: Paolo Bonzini Signed-off-by: Maxim Levitsky --- arch/x86/include/asm/kvm_host.h | 6 +++++- arch/x86/kvm/vmx/nested.c | 34 +++++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.c | 7 +++---- arch/x86/kvm/vmx/vmx.h | 2 -- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 562042d5589b..f7f1b7611a73 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -757,7 +757,11 @@ struct kvm_vcpu_arch { u64 arch_capabilities; u64 perf_capabilities; -#define KVM_NESTED_IN_GUEST BIT(8) +#define KVM_NESTED_PI_VECTOR_MASK 0xFF +#define KVM_NESTED_IN_GUEST BIT(8) +#define KVM_NESTED_PI_ACTIVE BIT(9) +#define KVM_NESTED_PI_PENDING BIT(10) + /* Non zero when the guest is executing another guest */ atomic_t nested_guest_state; diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index e336dfb8d6b9..8d242d46a245 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -351,7 +351,6 @@ static void free_nested(struct kvm_vcpu *vcpu) vmx->nested.smm.vmxon = false; vmx->nested.vmxon_ptr = INVALID_GPA; free_vpid(vmx->nested.vpid02); - vmx->nested.posted_intr_nv = -1; vmx->nested.current_vmptr = INVALID_GPA; if (enable_shadow_vmcs) { vmx_disable_shadow_vmcs(vmx); @@ -2445,13 +2444,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 ~PIN_BASED_VMX_PREEMPTION_TIMER); /* Posted interrupts setting is only taken from vmcs12. */ - vmx->nested.pi_pending = false; - if (nested_cpu_has_posted_intr(vmcs12)) { - vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; - } else { - vmx->nested.posted_intr_nv = -1; + if (!nested_cpu_has_posted_intr(vmcs12)) exec_control &= ~PIN_BASED_POSTED_INTR; - } + pin_controls_set(vmx, exec_control); /* @@ -3720,6 +3715,10 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); } + /* Publish nested posted interrupt state */ + if (nested_cpu_has_posted_intr(vmcs12)) + atomic_or(KVM_NESTED_PI_ACTIVE | vmcs12->posted_intr_nv, + &vcpu->arch.nested_guest_state); /* * Re-evaluate pending events if L1 had a pending IRQ/NMI/INIT/SIPI * when it executed VMLAUNCH/VMRESUME, as entering non-root mode can @@ -4033,13 +4032,15 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) void *vapic_page; u16 status; - if (!vmx->nested.pi_pending) + int nested_state = atomic_read(&vcpu->arch.nested_guest_state); + + if (!(nested_state & KVM_NESTED_PI_PENDING)) return 0; if (!vmx->nested.pi_desc) goto mmio_needed; - vmx->nested.pi_pending = false; + atomic_andnot(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state); /* * Don't test the value of PID.ON. @@ -4191,6 +4192,8 @@ static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection) void *vapic = vmx->nested.virtual_apic_map.hva; int max_irr, vppr; + int nested_state = atomic_read(&vcpu->arch.nested_guest_state); + if (nested_vmx_preemption_timer_pending(vcpu) || vmx->nested.mtf_pending) return true; @@ -4217,7 +4220,7 @@ static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection) if ((max_irr & 0xf0) > (vppr & 0xf0)) return true; - if (vmx->nested.pi_pending && vmx->nested.pi_desc) { + if ((nested_state & KVM_NESTED_PI_PENDING) && vmx->nested.pi_desc) { max_irr = pi_find_highest_vector(vmx->nested.pi_desc); if (max_irr > 0 && (max_irr & 0xf0) > (vppr & 0xf0)) return true; @@ -4448,7 +4451,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) } if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { - int irq; + int irq, nested_state; if (!nested_exit_on_intr(vcpu)) { if (block_nested_events) @@ -4481,6 +4484,8 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) goto no_vmexit; } + nested_state = atomic_read(&vcpu->arch.nested_guest_state); + /* * If the IRQ is L2's PI notification vector, process posted * interrupts for L2 instead of injecting VM-Exit, as the @@ -4489,7 +4494,8 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) * through the local APIC trigger posted interrupt processing, * and enabling posted interrupts requires ACK-on-exit. */ - if (irq == vmx->nested.posted_intr_nv) { + if ((nested_state & KVM_NESTED_PI_ACTIVE) && + ((nested_state & KVM_NESTED_PI_VECTOR_MASK) == irq)) { /* * Nested posted interrupts are delivered via RVI, i.e. * aren't injected by KVM, and so can be queued even if @@ -4498,7 +4504,8 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) if (block_non_injected_events) return -EBUSY; - vmx->nested.pi_pending = true; + atomic_or(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state); + kvm_apic_clear_irr(vcpu, irq); goto no_vmexit; } @@ -5587,7 +5594,6 @@ static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) copy_shadow_to_vmcs12(vmx); vmx_disable_shadow_vmcs(vmx); } - vmx->nested.posted_intr_nv = -1; /* Flush VMCS12 to guest memory */ kvm_vcpu_write_guest_page(vcpu, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 612ab07d4100..e6dd37a3ac44 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4370,7 +4370,7 @@ void vmx_recalc_intercepts(struct kvm_vcpu *vcpu) static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, int vector) { - struct vcpu_vmx *vmx = to_vmx(vcpu); + int nested_state = atomic_read(&vcpu->arch.nested_guest_state); /* * DO NOT query the vCPU's vmcs12, as vmcs12 is dynamically allocated @@ -4380,12 +4380,12 @@ static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, * L1 has enabled posted interrupts for L2. */ if (is_guest_mode(vcpu) && - vector == vmx->nested.posted_intr_nv) { + vector == (nested_state & KVM_NESTED_PI_VECTOR_MASK)) { /* * If a posted intr is not recognized by hardware, * we will accomplish it in the next vmentry. */ - vmx->nested.pi_pending = true; + atomic_or(KVM_NESTED_PI_PENDING, &vcpu->arch.nested_guest_state); kvm_make_request(KVM_REQ_EVENT, vcpu); /* @@ -5019,7 +5019,6 @@ static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu) vcpu_setup_sgx_lepubkeyhash(vcpu); - vmx->nested.posted_intr_nv = -1; vmx->nested.vmxon_ptr = INVALID_GPA; vmx->nested.current_vmptr = INVALID_GPA; diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index dc8517f15bc4..0632cd8908b3 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -151,8 +151,6 @@ struct nested_vmx { struct kvm_host_map pi_desc_map; struct pi_desc *pi_desc; - bool pi_pending; - u16 posted_intr_nv; struct hrtimer preemption_timer; u64 preemption_timer_deadline; -- 2.56.1